r21656: Move tests a bit closer to the things they test, should make syncing with...
[ira/wip.git] / source / lib / util / tests / file.c
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 2 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, write to the Free Software
20    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23 #include "includes.h"
24 #include "system/filesys.h"
25 #include "torture/torture.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 static bool test_file_load_save(struct torture_context *tctx)
35 {
36         size_t len;
37         char *data;
38         TALLOC_CTX *mem_ctx = tctx;
39         
40         torture_assert(tctx, file_save(TEST_FILENAME, TEST_DATA, strlen(TEST_DATA)),
41                                    "saving file");
42
43         data = file_load(TEST_FILENAME, &len, mem_ctx);
44         torture_assert(tctx, data, "loading file");
45
46         torture_assert(tctx, len == strlen(TEST_DATA), "Length");
47         
48         torture_assert(tctx, memcmp(data, TEST_DATA, len) == 0, "Contents");
49
50         unlink(TEST_FILENAME);
51         return true;
52 }
53
54
55 static bool test_afdgets(struct torture_context *tctx)
56 {
57         int fd;
58         char *line;
59         TALLOC_CTX *mem_ctx = tctx;
60         
61         torture_assert(tctx, file_save(TEST_FILENAME, (const void *)TEST_DATA, 
62                                                          strlen(TEST_DATA)),
63                                    "saving file");
64
65         fd = open(TEST_FILENAME, O_RDONLY);
66         
67         torture_assert(tctx, fd != -1, "opening file");
68
69         line = afdgets(fd, mem_ctx, 8);
70         torture_assert(tctx, strcmp(line, TEST_LINE1) == 0, "line 1 mismatch");
71
72         line = afdgets(fd, mem_ctx, 8);
73         torture_assert(tctx, strcmp(line, TEST_LINE2) == 0, "line 2 mismatch");
74
75         line = afdgets(fd, mem_ctx, 8);
76         torture_assert(tctx, strcmp(line, TEST_LINE3) == 0, "line 3 mismatch");
77
78         close(fd);
79
80         unlink(TEST_FILENAME);
81         return true;
82 }
83
84 struct torture_suite *torture_local_util_file(TALLOC_CTX *mem_ctx)
85 {
86         struct torture_suite *suite = torture_suite_create(mem_ctx, "FILE");
87
88         torture_suite_add_simple_test(suite, "file_load_save", 
89                                                                    test_file_load_save);
90
91         torture_suite_add_simple_test(suite, "afdgets", 
92                                                                    test_afdgets);
93
94         return suite;
95 }