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 "system/filesys.h"
24 #include "torture/torture.h"
25 #include "torture/local/proto.h"
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"
32 #define TEST_DATA TEST_LINE1 "\n" TEST_LINE2 "\n" TEST_LINE3
34 static bool test_file_load_save(struct torture_context
*tctx
)
38 TALLOC_CTX
*mem_ctx
= tctx
;
40 torture_assert(tctx
, file_save(TEST_FILENAME
, TEST_DATA
, strlen(TEST_DATA
)),
43 torture_assert_file_contains_text(tctx
, TEST_FILENAME
, TEST_DATA
,
46 data
= file_load(TEST_FILENAME
, &len
, 0, mem_ctx
);
47 torture_assert(tctx
, data
, "loading file");
49 torture_assert_int_equal(tctx
, len
, strlen(TEST_DATA
), "Length");
51 torture_assert_mem_equal(tctx
, data
, TEST_DATA
, len
, "Contents");
53 data
= file_load(TEST_FILENAME
, &len
, 5, mem_ctx
);
55 torture_assert_int_equal(tctx
, len
, 5, "Length");
57 torture_assert_mem_equal(tctx
, data
, TEST_DATA
, len
, "Contents");
59 unlink(TEST_FILENAME
);
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
69 static bool test_file_lines_load(struct torture_context
*tctx
)
73 TALLOC_CTX
*mem_ctx
= tctx
;
76 * Last line has trailing whitespace
80 file_save(TEST_FILENAME
,
81 TEST_DATA_WITH_NEWLINE
,
82 strlen(TEST_DATA_WITH_NEWLINE
)),
85 lines
= file_lines_load(TEST_FILENAME
, &numlines
, 0, mem_ctx
);
87 torture_assert_int_equal(tctx
, numlines
, 3, "Lines");
89 torture_assert_mem_equal(tctx
,
95 torture_assert_mem_equal(tctx
,
101 torture_assert_mem_equal(tctx
,
107 unlink(TEST_FILENAME
);
110 * Last line has NO trailing whitespace
114 file_save(TEST_FILENAME
,
115 TEST_DATA_NO_NEWLINE
,
116 strlen(TEST_DATA_NO_NEWLINE
)),
119 lines
= file_lines_load(TEST_FILENAME
, &numlines
, 0, mem_ctx
);
121 torture_assert_int_equal(tctx
, numlines
, 3, "Lines");
123 torture_assert_mem_equal(tctx
,
129 torture_assert_mem_equal(tctx
,
135 torture_assert_mem_equal(tctx
,
141 unlink(TEST_FILENAME
);
148 file_save(TEST_FILENAME
,
150 strlen(TEST_DATA_EMPTY
)),
153 lines
= file_lines_load(TEST_FILENAME
, &numlines
, 0, mem_ctx
);
155 torture_assert_int_equal(tctx
, numlines
, 0, "Lines");
157 unlink(TEST_FILENAME
);
164 file_save(TEST_FILENAME
,
165 TEST_DATA_BLANKS_ONLY
,
166 strlen(TEST_DATA_BLANKS_ONLY
)),
169 lines
= file_lines_load(TEST_FILENAME
, &numlines
, 0, mem_ctx
);
171 torture_assert_int_equal(tctx
, numlines
, 0, "Lines");
173 unlink(TEST_FILENAME
);
176 * Several trailing blank lines
180 file_save(TEST_FILENAME
,
181 TEST_DATA_WITH_TRAILING_BLANKS
,
182 strlen(TEST_DATA_WITH_TRAILING_BLANKS
)),
185 lines
= file_lines_load(TEST_FILENAME
, &numlines
, 0, mem_ctx
);
187 torture_assert_int_equal(tctx
, numlines
, 3, "Lines");
189 torture_assert_mem_equal(tctx
,
195 torture_assert_mem_equal(tctx
,
201 torture_assert_mem_equal(tctx
,
207 unlink(TEST_FILENAME
);
212 static bool test_afdgets(struct torture_context
*tctx
)
216 TALLOC_CTX
*mem_ctx
= tctx
;
219 torture_assert(tctx
, file_save(TEST_FILENAME
, (const void *)TEST_DATA
,
223 fd
= open(TEST_FILENAME
, O_RDONLY
);
225 torture_assert(tctx
, fd
!= -1, "opening file");
227 line
= afdgets(fd
, mem_ctx
, 8);
228 torture_assert_goto(tctx
, strcmp(line
, TEST_LINE1
) == 0, ret
, done
,
231 line
= afdgets(fd
, mem_ctx
, 8);
232 torture_assert_goto(tctx
, strcmp(line
, TEST_LINE2
) == 0, ret
, done
,
235 line
= afdgets(fd
, mem_ctx
, 8);
236 torture_assert_goto(tctx
, strcmp(line
, TEST_LINE3
) == 0, ret
, done
,
242 unlink(TEST_FILENAME
);
246 struct torture_suite
*torture_local_util_file(TALLOC_CTX
*mem_ctx
)
248 struct torture_suite
*suite
= torture_suite_create(mem_ctx
, "file");
250 torture_suite_add_simple_test(suite
, "file_load_save",
251 test_file_load_save
);
253 torture_suite_add_simple_test(suite
,
255 test_file_lines_load
);
257 torture_suite_add_simple_test(suite
, "afdgets", test_afdgets
);