1 | /*
|
---|
2 | * Copyright (c) 2018 Jiri Svoboda
|
---|
3 | * All rights reserved.
|
---|
4 | *
|
---|
5 | * Redistribution and use in source and binary forms, with or without
|
---|
6 | * modification, are permitted provided that the following conditions
|
---|
7 | * are met:
|
---|
8 | *
|
---|
9 | * - Redistributions of source code must retain the above copyright
|
---|
10 | * notice, this list of conditions and the following disclaimer.
|
---|
11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
12 | * notice, this list of conditions and the following disclaimer in the
|
---|
13 | * documentation and/or other materials provided with the distribution.
|
---|
14 | * - The name of the author may not be used to endorse or promote products
|
---|
15 | * derived from this software without specific prior written permission.
|
---|
16 | *
|
---|
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
27 | */
|
---|
28 |
|
---|
29 | #include <pcut/pcut.h>
|
---|
30 | #include <str.h>
|
---|
31 | #include <fcntl.h>
|
---|
32 | #include <stdio.h>
|
---|
33 | #include <stdlib.h>
|
---|
34 | #include <unistd.h>
|
---|
35 |
|
---|
36 | #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
|
---|
37 |
|
---|
38 | PCUT_INIT;
|
---|
39 |
|
---|
40 | PCUT_TEST_SUITE(stdlib);
|
---|
41 |
|
---|
42 | #define MKSTEMP_TEMPL "/tmp/tmp.XXXXXX"
|
---|
43 | #define MKTEMP_BAD_TEMPL "/tmp/tmp.XXXXX"
|
---|
44 | #define MKTEMP_SHORT_TEMPL "XXXXX"
|
---|
45 |
|
---|
46 | /** mktemp function */
|
---|
47 | PCUT_TEST(mktemp)
|
---|
48 | {
|
---|
49 | int file;
|
---|
50 | char buf[sizeof(MKSTEMP_TEMPL)];
|
---|
51 | char *p;
|
---|
52 |
|
---|
53 | str_cpy(buf, sizeof(buf), MKSTEMP_TEMPL);
|
---|
54 |
|
---|
55 | p = mktemp(buf);
|
---|
56 | PCUT_ASSERT_TRUE(p == buf);
|
---|
57 | PCUT_ASSERT_TRUE(str_lcmp(p, MKSTEMP_TEMPL,
|
---|
58 | str_length(MKSTEMP_TEMPL) - 6) == 0);
|
---|
59 |
|
---|
60 | file = open(p, O_CREAT | O_EXCL | O_RDWR, 0600);
|
---|
61 | PCUT_ASSERT_TRUE(file >= 0);
|
---|
62 | close(file);
|
---|
63 |
|
---|
64 | (void) unlink(p);
|
---|
65 | }
|
---|
66 |
|
---|
67 | /** mktemp function called twice should return different names */
|
---|
68 | PCUT_TEST(mktemp_twice)
|
---|
69 | {
|
---|
70 | char buf1[sizeof(MKSTEMP_TEMPL)];
|
---|
71 | char buf2[sizeof(MKSTEMP_TEMPL)];
|
---|
72 | char *p;
|
---|
73 |
|
---|
74 | str_cpy(buf1, sizeof(buf1), MKSTEMP_TEMPL);
|
---|
75 | str_cpy(buf2, sizeof(buf2), MKSTEMP_TEMPL);
|
---|
76 |
|
---|
77 | p = mktemp(buf1);
|
---|
78 | PCUT_ASSERT_TRUE(p == buf1);
|
---|
79 | PCUT_ASSERT_TRUE(str_lcmp(p, MKSTEMP_TEMPL,
|
---|
80 | str_length(MKSTEMP_TEMPL) - 6) == 0);
|
---|
81 |
|
---|
82 | p = mktemp(buf2);
|
---|
83 | PCUT_ASSERT_TRUE(p == buf2);
|
---|
84 | PCUT_ASSERT_TRUE(str_lcmp(p, MKSTEMP_TEMPL,
|
---|
85 | str_length(MKSTEMP_TEMPL) - 6) == 0);
|
---|
86 |
|
---|
87 | PCUT_ASSERT_TRUE(str_cmp(buf1, buf2) != 0);
|
---|
88 | }
|
---|
89 |
|
---|
90 | /** mktemp function with malformed template */
|
---|
91 | PCUT_TEST(mktemp_bad_templ)
|
---|
92 | {
|
---|
93 | char buf[sizeof(MKTEMP_BAD_TEMPL)];
|
---|
94 | char *p;
|
---|
95 |
|
---|
96 | str_cpy(buf, sizeof(buf), MKTEMP_BAD_TEMPL);
|
---|
97 |
|
---|
98 | p = mktemp(buf);
|
---|
99 | PCUT_ASSERT_TRUE(p == buf);
|
---|
100 | PCUT_ASSERT_TRUE(p[0] == '\0');
|
---|
101 | }
|
---|
102 |
|
---|
103 | /** mktemp function with too short template */
|
---|
104 | PCUT_TEST(mktemp_short_templ)
|
---|
105 | {
|
---|
106 | char buf[sizeof(MKTEMP_SHORT_TEMPL)];
|
---|
107 | char *p;
|
---|
108 |
|
---|
109 | str_cpy(buf, sizeof(buf), MKTEMP_SHORT_TEMPL);
|
---|
110 |
|
---|
111 | p = mktemp(buf);
|
---|
112 | PCUT_ASSERT_TRUE(p == buf);
|
---|
113 | PCUT_ASSERT_TRUE(p[0] == '\0');
|
---|
114 | }
|
---|
115 |
|
---|
116 | /** mkstemp function */
|
---|
117 | PCUT_TEST(mkstemp)
|
---|
118 | {
|
---|
119 | int file;
|
---|
120 | char buf[sizeof(MKSTEMP_TEMPL)];
|
---|
121 | ssize_t rc;
|
---|
122 | char c;
|
---|
123 |
|
---|
124 | str_cpy(buf, sizeof(buf), MKSTEMP_TEMPL);
|
---|
125 |
|
---|
126 | file = mkstemp(buf);
|
---|
127 | PCUT_ASSERT_TRUE(file >= 0);
|
---|
128 |
|
---|
129 | (void) unlink(buf);
|
---|
130 |
|
---|
131 | c = 'x';
|
---|
132 | rc = write(file, &c, sizeof(c));
|
---|
133 | PCUT_ASSERT_TRUE(rc == sizeof(c));
|
---|
134 |
|
---|
135 | (void) lseek(file, 0, SEEK_SET);
|
---|
136 | c = '\0';
|
---|
137 | rc = read(file, &c, sizeof(c));
|
---|
138 | PCUT_ASSERT_TRUE(rc == sizeof(c));
|
---|
139 | PCUT_ASSERT_INT_EQUALS('x', c);
|
---|
140 |
|
---|
141 | close(file);
|
---|
142 | }
|
---|
143 |
|
---|
144 | PCUT_EXPORT(stdlib);
|
---|