ldb: version 1.1.27
[Samba.git] / lib / util / tests / file.c
blobf349c214f084f6ce9dc18a3c3a1e89573631d615
1 /*
2 Unix SMB/CIFS implementation.
4 util_file testing
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/>.
22 #include "includes.h"
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)
36 size_t len;
37 char *data;
38 TALLOC_CTX *mem_ctx = tctx;
40 torture_assert(tctx, file_save(TEST_FILENAME, TEST_DATA, strlen(TEST_DATA)),
41 "saving file");
43 torture_assert_file_contains_text(tctx, TEST_FILENAME, TEST_DATA,
44 "file contents");
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);
60 return true;
64 static bool test_afdgets(struct torture_context *tctx)
66 int fd;
67 char *line;
68 TALLOC_CTX *mem_ctx = tctx;
69 bool ret = false;
71 torture_assert(tctx, file_save(TEST_FILENAME, (const void *)TEST_DATA,
72 strlen(TEST_DATA)),
73 "saving file");
75 fd = open(TEST_FILENAME, O_RDONLY);
77 torture_assert(tctx, fd != -1, "opening file");
79 line = afdgets(fd, mem_ctx, 8);
80 torture_assert_goto(tctx, strcmp(line, TEST_LINE1) == 0, ret, done,
81 "line 1 mismatch");
83 line = afdgets(fd, mem_ctx, 8);
84 torture_assert_goto(tctx, strcmp(line, TEST_LINE2) == 0, ret, done,
85 "line 2 mismatch");
87 line = afdgets(fd, mem_ctx, 8);
88 torture_assert_goto(tctx, strcmp(line, TEST_LINE3) == 0, ret, done,
89 "line 3 mismatch");
90 ret = true;
91 done:
92 close(fd);
94 unlink(TEST_FILENAME);
95 return ret;
98 struct torture_suite *torture_local_util_file(TALLOC_CTX *mem_ctx)
100 struct torture_suite *suite = torture_suite_create(mem_ctx, "file");
102 torture_suite_add_simple_test(suite, "file_load_save",
103 test_file_load_save);
105 torture_suite_add_simple_test(suite, "afdgets", test_afdgets);
107 return suite;