Line data Source code
1 : /*
2 : Unix SMB/CIFS implementation.
3 :
4 : util_file testing
5 :
6 : Copyright (C) Jelmer Vernooij 2005
7 :
8 : This program is free software; you can redistribute it and/or modify
9 : it under the terms of the GNU General Public License as published by
10 : the Free Software Foundation; either version 3 of the License, or
11 : (at your option) any later version.
12 :
13 : This program is distributed in the hope that it will be useful,
14 : but WITHOUT ANY WARRANTY; without even the implied warranty of
15 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 : GNU General Public License for more details.
17 :
18 : You should have received a copy of the GNU General Public License
19 : along with this program. If not, see <http://www.gnu.org/licenses/>.
20 : */
21 :
22 : #include "includes.h"
23 : #include "system/filesys.h"
24 : #include "torture/torture.h"
25 : #include "torture/local/proto.h"
26 :
27 : #define TEST_FILENAME "utilfile.test"
28 : #define TEST_LINE1 "This is list line 1..."
29 : #define TEST_LINE2 ".. and this is line 2"
30 : #define TEST_LINE3 "and end of the file"
31 :
32 : #define TEST_DATA TEST_LINE1 "\n" TEST_LINE2 "\n" TEST_LINE3
33 :
34 1 : static bool test_file_load_save(struct torture_context *tctx)
35 : {
36 1 : size_t len;
37 1 : char *data;
38 1 : TALLOC_CTX *mem_ctx = tctx;
39 :
40 1 : torture_assert(tctx, file_save(TEST_FILENAME, TEST_DATA, strlen(TEST_DATA)),
41 : "saving file");
42 :
43 1 : torture_assert_file_contains_text(tctx, TEST_FILENAME, TEST_DATA,
44 : "file contents");
45 :
46 1 : data = file_load(TEST_FILENAME, &len, 0, mem_ctx);
47 1 : torture_assert(tctx, data, "loading file");
48 :
49 1 : torture_assert_int_equal(tctx, len, strlen(TEST_DATA), "Length");
50 :
51 1 : torture_assert_mem_equal(tctx, data, TEST_DATA, len, "Contents");
52 :
53 1 : data = file_load(TEST_FILENAME, &len, 5, mem_ctx);
54 :
55 1 : torture_assert_int_equal(tctx, len, 5, "Length");
56 :
57 1 : torture_assert_mem_equal(tctx, data, TEST_DATA, len, "Contents");
58 :
59 1 : unlink(TEST_FILENAME);
60 1 : return true;
61 : }
62 :
63 : #define TEST_DATA_WITH_NEWLINE TEST_DATA "\n"
64 : #define TEST_DATA_NO_NEWLINE TEST_DATA
65 : #define TEST_DATA_EMPTY ""
66 : #define TEST_DATA_BLANKS_ONLY "\n\n\n\n\n"
67 : #define TEST_DATA_WITH_TRAILING_BLANKS TEST_DATA TEST_DATA_BLANKS_ONLY
68 :
69 1 : static bool test_file_lines_load(struct torture_context *tctx)
70 : {
71 1 : char **lines;
72 1 : int numlines;
73 1 : TALLOC_CTX *mem_ctx = tctx;
74 :
75 : /*
76 : * Last line has trailing whitespace
77 : */
78 :
79 1 : torture_assert(tctx,
80 : file_save(TEST_FILENAME,
81 : TEST_DATA_WITH_NEWLINE,
82 : strlen(TEST_DATA_WITH_NEWLINE)),
83 : "saving file");
84 :
85 1 : lines = file_lines_load(TEST_FILENAME, &numlines, 0, mem_ctx);
86 :
87 1 : torture_assert_int_equal(tctx, numlines, 3, "Lines");
88 :
89 1 : torture_assert_mem_equal(tctx,
90 : lines[0],
91 : TEST_LINE1,
92 : strlen(TEST_LINE1),
93 : "Line 1");
94 :
95 1 : torture_assert_mem_equal(tctx,
96 : lines[1],
97 : TEST_LINE2,
98 : strlen(TEST_LINE2),
99 : "Line 2");
100 :
101 1 : torture_assert_mem_equal(tctx,
102 : lines[2],
103 : TEST_LINE3,
104 : strlen(TEST_LINE3),
105 : "Line 3");
106 :
107 1 : unlink(TEST_FILENAME);
108 :
109 : /*
110 : * Last line has NO trailing whitespace
111 : */
112 :
113 1 : torture_assert(tctx,
114 : file_save(TEST_FILENAME,
115 : TEST_DATA_NO_NEWLINE,
116 : strlen(TEST_DATA_NO_NEWLINE)),
117 : "saving file");
118 :
119 1 : lines = file_lines_load(TEST_FILENAME, &numlines, 0, mem_ctx);
120 :
121 1 : torture_assert_int_equal(tctx, numlines, 3, "Lines");
122 :
123 1 : torture_assert_mem_equal(tctx,
124 : lines[0],
125 : TEST_LINE1,
126 : strlen(TEST_LINE1),
127 : "Line 1");
128 :
129 1 : torture_assert_mem_equal(tctx,
130 : lines[1],
131 : TEST_LINE2,
132 : strlen(TEST_LINE2),
133 : "Line 2");
134 :
135 1 : torture_assert_mem_equal(tctx,
136 : lines[2],
137 : TEST_LINE3,
138 : strlen(TEST_LINE3),
139 : "Line 3");
140 :
141 1 : unlink(TEST_FILENAME);
142 :
143 : /*
144 : * Empty file
145 : */
146 :
147 1 : torture_assert(tctx,
148 : file_save(TEST_FILENAME,
149 : TEST_DATA_EMPTY,
150 : strlen(TEST_DATA_EMPTY)),
151 : "saving file");
152 :
153 1 : (void)file_lines_load(TEST_FILENAME, &numlines, 0, mem_ctx);
154 :
155 1 : torture_assert_int_equal(tctx, numlines, 0, "Lines");
156 :
157 1 : unlink(TEST_FILENAME);
158 :
159 : /*
160 : * Just blank lines
161 : */
162 :
163 1 : torture_assert(tctx,
164 : file_save(TEST_FILENAME,
165 : TEST_DATA_BLANKS_ONLY,
166 : strlen(TEST_DATA_BLANKS_ONLY)),
167 : "saving file");
168 :
169 1 : lines = file_lines_load(TEST_FILENAME, &numlines, 0, mem_ctx);
170 :
171 1 : torture_assert_int_equal(tctx, numlines, 0, "Lines");
172 :
173 1 : unlink(TEST_FILENAME);
174 :
175 : /*
176 : * Several trailing blank lines
177 : */
178 :
179 1 : torture_assert(tctx,
180 : file_save(TEST_FILENAME,
181 : TEST_DATA_WITH_TRAILING_BLANKS,
182 : strlen(TEST_DATA_WITH_TRAILING_BLANKS)),
183 : "saving file");
184 :
185 1 : lines = file_lines_load(TEST_FILENAME, &numlines, 0, mem_ctx);
186 :
187 1 : torture_assert_int_equal(tctx, numlines, 3, "Lines");
188 :
189 1 : torture_assert_mem_equal(tctx,
190 : lines[0],
191 : TEST_LINE1,
192 : strlen(TEST_LINE1),
193 : "Line 1");
194 :
195 1 : torture_assert_mem_equal(tctx,
196 : lines[1],
197 : TEST_LINE2,
198 : strlen(TEST_LINE2),
199 : "Line 2");
200 :
201 1 : torture_assert_mem_equal(tctx,
202 : lines[2],
203 : TEST_LINE3,
204 : strlen(TEST_LINE3),
205 : "Line 3");
206 :
207 1 : unlink(TEST_FILENAME);
208 :
209 1 : return true;
210 : }
211 :
212 1 : static bool test_afdgets(struct torture_context *tctx)
213 : {
214 1 : int fd;
215 1 : char *line;
216 1 : TALLOC_CTX *mem_ctx = tctx;
217 1 : bool ret = false;
218 :
219 1 : torture_assert(tctx, file_save(TEST_FILENAME, (const void *)TEST_DATA,
220 : strlen(TEST_DATA)),
221 : "saving file");
222 :
223 1 : fd = open(TEST_FILENAME, O_RDONLY);
224 :
225 1 : torture_assert(tctx, fd != -1, "opening file");
226 :
227 1 : line = afdgets(fd, mem_ctx, 8);
228 1 : torture_assert_goto(tctx, strcmp(line, TEST_LINE1) == 0, ret, done,
229 : "line 1 mismatch");
230 :
231 1 : line = afdgets(fd, mem_ctx, 8);
232 1 : torture_assert_goto(tctx, strcmp(line, TEST_LINE2) == 0, ret, done,
233 : "line 2 mismatch");
234 :
235 1 : line = afdgets(fd, mem_ctx, 8);
236 1 : torture_assert_goto(tctx, strcmp(line, TEST_LINE3) == 0, ret, done,
237 : "line 3 mismatch");
238 0 : ret = true;
239 1 : done:
240 1 : close(fd);
241 :
242 1 : unlink(TEST_FILENAME);
243 1 : return ret;
244 : }
245 :
246 1 : static bool test_file_lines_parse(struct torture_context *tctx)
247 : {
248 1 : char **lines;
249 1 : int numlines;
250 1 : TALLOC_CTX *mem_ctx = tctx;
251 1 : char *buf;
252 1 : size_t size;
253 :
254 1 : torture_assert(tctx, file_save(TEST_FILENAME,
255 : (const void *)TEST_DATA,
256 : strlen(TEST_DATA)),
257 : "saving file");
258 :
259 1 : buf = file_load(TEST_FILENAME, &size, 0, mem_ctx);
260 1 : torture_assert(tctx, buf, "failed to load file");
261 1 : unlink(TEST_FILENAME);
262 :
263 1 : lines = file_lines_parse(buf,
264 : size,
265 : &numlines,
266 : mem_ctx);
267 1 : torture_assert(tctx, lines, "failed to parse lines");
268 :
269 1 : TALLOC_FREE(lines);
270 1 : TALLOC_FREE(buf);
271 1 : return true;
272 : }
273 :
274 2358 : struct torture_suite *torture_local_util_file(TALLOC_CTX *mem_ctx)
275 : {
276 2358 : struct torture_suite *suite = torture_suite_create(mem_ctx, "file");
277 :
278 2358 : torture_suite_add_simple_test(suite, "file_load_save",
279 : test_file_load_save);
280 :
281 2358 : torture_suite_add_simple_test(suite,
282 : "file_lines_load",
283 : test_file_lines_load);
284 :
285 2358 : torture_suite_add_simple_test(suite, "afdgets", test_afdgets);
286 :
287 2358 : torture_suite_add_simple_test(suite, "file_lines_parse",
288 : test_file_lines_parse);
289 :
290 2358 : return suite;
291 : }
|