summaryrefslogtreecommitdiff
path: root/lib/util/tests/file.c
blob: 55c9d4cec9a6e5adffb1a48299307223be11e781 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
/* 
   Unix SMB/CIFS implementation.

   util_file testing

   Copyright (C) Jelmer Vernooij 2005
   
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3 of the License, or
   (at your option) any later version.
   
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
   
   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "includes.h"
#include "system/filesys.h"
#include "torture/torture.h"
#include "torture/local/proto.h"

#define TEST_FILENAME "utilfile.test"
#define TEST_LINE1 "This is list line 1..."
#define TEST_LINE2 ".. and this is line 2"
#define TEST_LINE3 "and end of the file"

#define TEST_DATA TEST_LINE1 "\n" TEST_LINE2 "\n" TEST_LINE3

static bool test_file_load_save(struct torture_context *tctx)
{
	size_t len;
	char *data;
	TALLOC_CTX *mem_ctx = tctx;
	
	torture_assert(tctx, file_save(TEST_FILENAME, TEST_DATA, strlen(TEST_DATA)),
				   "saving file");

	torture_assert_file_contains_text(tctx, TEST_FILENAME, TEST_DATA, 
								      "file contents");

	data = file_load(TEST_FILENAME, &len, 0, mem_ctx);
	torture_assert(tctx, data, "loading file");

	torture_assert_int_equal(tctx, len, strlen(TEST_DATA), "Length");
	
	torture_assert_mem_equal(tctx, data, TEST_DATA, len, "Contents");

	data = file_load(TEST_FILENAME, &len, 5, mem_ctx);

	torture_assert_int_equal(tctx, len, 5, "Length");

	torture_assert_mem_equal(tctx, data, TEST_DATA, len, "Contents");

	unlink(TEST_FILENAME);
	return true;
}

#define TEST_DATA_WITH_NEWLINE TEST_DATA "\n"
#define TEST_DATA_NO_NEWLINE TEST_DATA
#define TEST_DATA_EMPTY ""
#define TEST_DATA_BLANKS_ONLY "\n\n\n\n\n"
#define TEST_DATA_WITH_TRAILING_BLANKS TEST_DATA TEST_DATA_BLANKS_ONLY

static bool test_file_lines_load(struct torture_context *tctx)
{
	char **lines;
	int numlines;
	TALLOC_CTX *mem_ctx = tctx;

	/*
	 * Last line has trailing whitespace
	 */

	torture_assert(tctx,
		       file_save(TEST_FILENAME,
				 TEST_DATA_WITH_NEWLINE,
				 strlen(TEST_DATA_WITH_NEWLINE)),
		       "saving file");

	lines = file_lines_load(TEST_FILENAME, &numlines, 0, mem_ctx);

	torture_assert_int_equal(tctx, numlines, 3, "Lines");

	torture_assert_mem_equal(tctx,
				 lines[0],
				 TEST_LINE1,
				 strlen(TEST_LINE1),
				 "Line 1");

	torture_assert_mem_equal(tctx,
				 lines[1],
				 TEST_LINE2,
				 strlen(TEST_LINE2),
				 "Line 2");

	torture_assert_mem_equal(tctx,
				 lines[2],
				 TEST_LINE3,
				 strlen(TEST_LINE3),
				 "Line 3");

	unlink(TEST_FILENAME);

	/*
	 * Last line has NO trailing whitespace
	 */

	torture_assert(tctx,
		       file_save(TEST_FILENAME,
				 TEST_DATA_NO_NEWLINE,
				 strlen(TEST_DATA_NO_NEWLINE)),
		       "saving file");

	lines = file_lines_load(TEST_FILENAME, &numlines, 0, mem_ctx);

	torture_assert_int_equal(tctx, numlines, 3, "Lines");

	torture_assert_mem_equal(tctx,
				 lines[0],
				 TEST_LINE1,
				 strlen(TEST_LINE1),
				 "Line 1");

	torture_assert_mem_equal(tctx,
				 lines[1],
				 TEST_LINE2,
				 strlen(TEST_LINE2),
				 "Line 2");

	torture_assert_mem_equal(tctx,
				 lines[2],
				 TEST_LINE3,
				 strlen(TEST_LINE3),
				 "Line 3");

	unlink(TEST_FILENAME);

	/*
	 * Empty file
	 */

	torture_assert(tctx,
		       file_save(TEST_FILENAME,
				 TEST_DATA_EMPTY,
				 strlen(TEST_DATA_EMPTY)),
		       "saving file");

	(void)file_lines_load(TEST_FILENAME, &numlines, 0, mem_ctx);

	torture_assert_int_equal(tctx, numlines, 0, "Lines");

	unlink(TEST_FILENAME);

	/*
	 * Just blank lines
	 */

	torture_assert(tctx,
		       file_save(TEST_FILENAME,
				 TEST_DATA_BLANKS_ONLY,
				 strlen(TEST_DATA_BLANKS_ONLY)),
		       "saving file");

	lines = file_lines_load(TEST_FILENAME, &numlines, 0, mem_ctx);

	torture_assert_int_equal(tctx, numlines, 0, "Lines");

	unlink(TEST_FILENAME);

	/*
	 * Several trailing blank lines
	 */

	torture_assert(tctx,
		       file_save(TEST_FILENAME,
				 TEST_DATA_WITH_TRAILING_BLANKS,
				 strlen(TEST_DATA_WITH_TRAILING_BLANKS)),
		       "saving file");

	lines = file_lines_load(TEST_FILENAME, &numlines, 0, mem_ctx);

	torture_assert_int_equal(tctx, numlines, 3, "Lines");

	torture_assert_mem_equal(tctx,
				 lines[0],
				 TEST_LINE1,
				 strlen(TEST_LINE1),
				 "Line 1");

	torture_assert_mem_equal(tctx,
				 lines[1],
				 TEST_LINE2,
				 strlen(TEST_LINE2),
				 "Line 2");

	torture_assert_mem_equal(tctx,
				 lines[2],
				 TEST_LINE3,
				 strlen(TEST_LINE3),
				 "Line 3");

	unlink(TEST_FILENAME);

	return true;
}

static bool test_afdgets(struct torture_context *tctx)
{
	int fd;
	char *line;
	TALLOC_CTX *mem_ctx = tctx;
	bool ret = false;
	
	torture_assert(tctx, file_save(TEST_FILENAME, (const void *)TEST_DATA, 
							 strlen(TEST_DATA)),
				   "saving file");

	fd = open(TEST_FILENAME, O_RDONLY);
	
	torture_assert(tctx, fd != -1, "opening file");

	line = afdgets(fd, mem_ctx, 8);
	torture_assert_goto(tctx, strcmp(line, TEST_LINE1) == 0, ret, done,
			    "line 1 mismatch");

	line = afdgets(fd, mem_ctx, 8);
	torture_assert_goto(tctx, strcmp(line, TEST_LINE2) == 0, ret, done,
			    "line 2 mismatch");

	line = afdgets(fd, mem_ctx, 8);
	torture_assert_goto(tctx, strcmp(line, TEST_LINE3) == 0, ret, done,
			    "line 3 mismatch");
	ret = true;
done:
	close(fd);

	unlink(TEST_FILENAME);
	return ret;
}

struct torture_suite *torture_local_util_file(TALLOC_CTX *mem_ctx)
{
	struct torture_suite *suite = torture_suite_create(mem_ctx, "file");

	torture_suite_add_simple_test(suite, "file_load_save", 
				      test_file_load_save);

	torture_suite_add_simple_test(suite,
				      "file_lines_load",
				      test_file_lines_load);

	torture_suite_add_simple_test(suite, "afdgets", test_afdgets);

	return suite;
}