2 Unix SMB/CIFS implementation.
6 Copyright (C) Jelmer Vernooij 2005
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.
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.
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/>.
23 #include "lib/util/util_file.h"
24 #include "system/filesys.h"
25 #include "torture/torture.h"
26 #include "torture/local/proto.h"
28 #define TEST_FILENAME "utilfile.test"
29 #define TEST_LINE1 "This is list line 1..."
30 #define TEST_LINE2 ".. and this is line 2"
31 #define TEST_LINE3 "and end of the file"
33 #define TEST_DATA TEST_LINE1 "\n" TEST_LINE2 "\n" TEST_LINE3
35 static bool test_file_load_save(struct torture_context *tctx)
39 TALLOC_CTX *mem_ctx = tctx;
41 torture_assert(tctx, file_save(TEST_FILENAME, TEST_DATA, strlen(TEST_DATA)),
44 torture_assert_file_contains_text(tctx, TEST_FILENAME, TEST_DATA,
47 data = file_load(TEST_FILENAME, &len, 0, mem_ctx);
48 torture_assert(tctx, data, "loading file");
50 torture_assert_int_equal(tctx, len, strlen(TEST_DATA), "Length");
52 torture_assert_mem_equal(tctx, data, TEST_DATA, len, "Contents");
54 data = file_load(TEST_FILENAME, &len, 5, mem_ctx);
56 torture_assert_int_equal(tctx, len, 5, "Length");
58 torture_assert_mem_equal(tctx, data, TEST_DATA, len, "Contents");
60 unlink(TEST_FILENAME);
64 #define TEST_DATA_WITH_NEWLINE TEST_DATA "\n"
65 #define TEST_DATA_NO_NEWLINE TEST_DATA
66 #define TEST_DATA_EMPTY ""
67 #define TEST_DATA_BLANKS_ONLY "\n\n\n\n\n"
68 #define TEST_DATA_WITH_TRAILING_BLANKS TEST_DATA TEST_DATA_BLANKS_ONLY
70 static bool test_file_lines_load(struct torture_context *tctx)
74 TALLOC_CTX *mem_ctx = tctx;
77 * Last line has trailing whitespace
81 file_save(TEST_FILENAME,
82 TEST_DATA_WITH_NEWLINE,
83 strlen(TEST_DATA_WITH_NEWLINE)),
86 lines = file_lines_load(TEST_FILENAME, &numlines, 0, mem_ctx);
88 torture_assert_int_equal(tctx, numlines, 3, "Lines");
90 torture_assert_mem_equal(tctx,
96 torture_assert_mem_equal(tctx,
102 torture_assert_mem_equal(tctx,
108 unlink(TEST_FILENAME);
111 * Last line has NO trailing whitespace
115 file_save(TEST_FILENAME,
116 TEST_DATA_NO_NEWLINE,
117 strlen(TEST_DATA_NO_NEWLINE)),
120 lines = file_lines_load(TEST_FILENAME, &numlines, 0, mem_ctx);
122 torture_assert_int_equal(tctx, numlines, 3, "Lines");
124 torture_assert_mem_equal(tctx,
130 torture_assert_mem_equal(tctx,
136 torture_assert_mem_equal(tctx,
142 unlink(TEST_FILENAME);
149 file_save(TEST_FILENAME,
151 strlen(TEST_DATA_EMPTY)),
154 (void)file_lines_load(TEST_FILENAME, &numlines, 0, mem_ctx);
156 torture_assert_int_equal(tctx, numlines, 0, "Lines");
158 unlink(TEST_FILENAME);
165 file_save(TEST_FILENAME,
166 TEST_DATA_BLANKS_ONLY,
167 strlen(TEST_DATA_BLANKS_ONLY)),
170 lines = file_lines_load(TEST_FILENAME, &numlines, 0, mem_ctx);
172 torture_assert_int_equal(tctx, numlines, 0, "Lines");
174 unlink(TEST_FILENAME);
177 * Several trailing blank lines
181 file_save(TEST_FILENAME,
182 TEST_DATA_WITH_TRAILING_BLANKS,
183 strlen(TEST_DATA_WITH_TRAILING_BLANKS)),
186 lines = file_lines_load(TEST_FILENAME, &numlines, 0, mem_ctx);
188 torture_assert_int_equal(tctx, numlines, 3, "Lines");
190 torture_assert_mem_equal(tctx,
196 torture_assert_mem_equal(tctx,
202 torture_assert_mem_equal(tctx,
208 unlink(TEST_FILENAME);
213 static bool test_afdgets(struct torture_context *tctx)
217 TALLOC_CTX *mem_ctx = tctx;
220 torture_assert(tctx, file_save(TEST_FILENAME, (const void *)TEST_DATA,
224 fd = open(TEST_FILENAME, O_RDONLY);
226 torture_assert(tctx, fd != -1, "opening file");
228 line = afdgets(fd, mem_ctx, 8);
229 torture_assert_goto(tctx, strcmp(line, TEST_LINE1) == 0, ret, done,
232 line = afdgets(fd, mem_ctx, 8);
233 torture_assert_goto(tctx, strcmp(line, TEST_LINE2) == 0, ret, done,
236 line = afdgets(fd, mem_ctx, 8);
237 torture_assert_goto(tctx, strcmp(line, TEST_LINE3) == 0, ret, done,
243 unlink(TEST_FILENAME);
247 static bool test_file_lines_parse(struct torture_context *tctx)
251 TALLOC_CTX *mem_ctx = tctx;
255 torture_assert(tctx, file_save(TEST_FILENAME,
256 (const void *)TEST_DATA,
260 buf = file_load(TEST_FILENAME, &size, 0, mem_ctx);
261 torture_assert(tctx, buf, "failed to load file");
262 unlink(TEST_FILENAME);
264 lines = file_lines_parse(buf,
268 torture_assert(tctx, lines, "failed to parse lines");
275 struct torture_suite *torture_local_util_file(TALLOC_CTX *mem_ctx)
277 struct torture_suite *suite = torture_suite_create(mem_ctx, "file");
279 torture_suite_add_simple_test(suite, "file_load_save",
280 test_file_load_save);
282 torture_suite_add_simple_test(suite,
284 test_file_lines_load);
286 torture_suite_add_simple_test(suite, "afdgets", test_afdgets);
288 torture_suite_add_simple_test(suite, "file_lines_parse",
289 test_file_lines_parse);