added copies of libs so can be built standalone
[obnox/ctdb.git] / lib / replace / test / testsuite.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    libreplace tests
5
6    Copyright (C) Jelmer Vernooij 2006
7
8      ** NOTE! The following LGPL license applies to the talloc
9      ** library. This does NOT imply that all of Samba is released
10      ** under the LGPL
11    
12    This library is free software; you can redistribute it and/or
13    modify it under the terms of the GNU Lesser General Public
14    License as published by the Free Software Foundation; either
15    version 2 of the License, or (at your option) any later version.
16
17    This library is distributed in the hope that it will be useful,
18    but WITHOUT ANY WARRANTY; without even the implied warranty of
19    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
20    Lesser General Public License for more details.
21
22    You should have received a copy of the GNU Lesser General Public
23    License along with this library; if not, write to the Free Software
24    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
25 */
26
27 #include "replace.h"
28
29 /*
30   we include all the system/ include files here so that libreplace tests
31   them in the build farm
32 */
33 #include "system/capability.h"
34 #include "system/dir.h"
35 #include "system/filesys.h"
36 #include "system/glob.h"
37 #include "system/iconv.h"
38 #include "system/locale.h"
39 #include "system/network.h"
40 #include "system/passwd.h"
41 #include "system/printing.h"
42 #include "system/readline.h"
43 #include "system/select.h"
44 #include "system/shmem.h"
45 #include "system/syslog.h"
46 #include "system/terminal.h"
47 #include "system/time.h"
48 #include "system/wait.h"
49 #include "system/aio.h"
50
51 #define TESTFILE "testfile.dat"
52
53 /*
54   test ftruncate() function
55  */
56 static int test_ftruncate(void)
57 {
58         struct stat st;
59         int fd;
60         const int size = 1234;
61         printf("test: ftruncate\n");
62         unlink(TESTFILE);
63         fd = open(TESTFILE, O_RDWR|O_CREAT, 0600);
64         if (fd == -1) {
65                 printf("failure: ftruncate [\n"
66                            "creating '%s' failed - %s\n]\n", TESTFILE, strerror(errno));
67                 return false;
68         }
69         if (ftruncate(fd, size) != 0) {
70                 printf("failure: ftruncate [\n%s\n]\n", strerror(errno));
71                 return false;
72         }
73         if (fstat(fd, &st) != 0) {
74                 printf("failure: ftruncate [\nfstat failed - %s\n]\n", strerror(errno));
75                 return false;
76         }
77         if (st.st_size != size) {
78                 printf("failure: ftruncate [\ngave wrong size %d - expected %d\n]\n",
79                        (int)st.st_size, size);
80                 return false;
81         }
82         unlink(TESTFILE);
83         printf("success: ftruncate\n");
84         return true;
85 }
86
87 /*
88   test strlcpy() function.
89   see http://www.gratisoft.us/todd/papers/strlcpy.html
90  */
91 static int test_strlcpy(void)
92 {
93         char buf[4];
94         const struct {
95                 const char *src;
96                 size_t result;
97         } tests[] = {
98                 { "abc", 3 },
99                 { "abcdef", 6 },
100                 { "abcd", 4 },
101                 { "", 0 },
102                 { NULL, 0 }
103         };
104         int i;
105         printf("test: strlcpy\n");
106         for (i=0;tests[i].src;i++) {
107                 if (strlcpy(buf, tests[i].src, sizeof(buf)) != tests[i].result) {
108                         printf("failure: strlcpy [\ntest %d failed\n]\n", i);
109                         return false;
110                 }
111         }
112         printf("success: strlcpy\n");
113         return true;
114 }
115
116 static int test_strlcat(void)
117 {
118         /* FIXME */
119         return true;
120 }
121
122 static int test_mktime(void)
123 {
124         /* FIXME */
125         return true;
126 }
127
128 static int test_innetgr(void)
129 {
130         /* FIXME */
131         return true;
132 }
133
134 static int test_initgroups(void)
135 {
136         /* FIXME */
137         return true;
138 }
139
140 static int test_memmove(void)
141 {
142         /* FIXME */
143         return true;
144 }
145
146 static int test_strdup(void)
147 {
148         /* FIXME */
149         return true;
150 }       
151
152 static int test_setlinebuf(void)
153 {
154         printf("test: setlinebuf\n");
155         setlinebuf(stdout);
156         printf("success: setlinebuf\n");
157         return true;
158 }
159
160 static int test_vsyslog(void)
161 {
162         /* FIXME */
163         return true;
164 }
165
166 static int test_timegm(void)
167 {
168         /* FIXME */
169         return true;
170 }
171
172 static int test_setenv(void)
173 {
174         /* FIXME */
175         return true;
176 }
177
178 static int test_strndup(void)
179 {
180         /* FIXME */
181         return true;
182 }
183
184 static int test_strnlen(void)
185 {
186         /* FIXME */
187         return true;
188 }
189
190 static int test_waitpid(void)
191 {
192         /* FIXME */
193         return true;
194 }
195
196 static int test_seteuid(void)
197 {
198         /* FIXME */
199         return true;
200 }
201
202 static int test_setegid(void)
203 {
204         /* FIXME */
205         return true;
206 }
207
208 static int test_asprintf(void)
209 {
210         /* FIXME */
211         return true;
212 }
213
214 static int test_snprintf(void)
215 {
216         /* FIXME */
217         return true;
218 }
219
220 static int test_vasprintf(void)
221 {
222         /* FIXME */
223         return true;
224 }
225
226 static int test_vsnprintf(void)
227 {
228         /* FIXME */
229         return true;
230 }
231
232 static int test_opendir(void)
233 {
234         /* FIXME */
235         return true;
236 }
237
238 extern int test_readdir_os2_delete(void);
239
240 static int test_readdir(void)
241 {
242         printf("test: readdir\n");
243         if (test_readdir_os2_delete() != 0) {
244                 return false;
245         }
246         printf("success: readdir\n");
247         return true;
248 }
249
250 static int test_telldir(void)
251 {
252         /* FIXME */
253         return true;
254 }
255
256 static int test_seekdir(void)
257 {
258         /* FIXME */
259         return true;
260 }
261
262 static int test_dlopen(void)
263 {
264         /* FIXME: test dlopen, dlsym, dlclose, dlerror */
265         return true;
266 }
267
268
269 static int test_chroot(void)
270 {
271         /* FIXME: chroot() */
272         return true;
273 }
274
275 static int test_bzero(void)
276 {
277         /* FIXME: bzero */
278         return true;
279 }
280
281 static int test_strerror(void)
282 {
283         /* FIXME */
284         return true;
285 }
286
287 static int test_errno(void)
288 {
289         /* FIXME */
290         return true;
291 }
292
293 static int test_mkdtemp(void)
294 {
295         /* FIXME */
296         return true;
297 }
298
299 static int test_mkstemp(void)
300 {
301         /* FIXME */
302         return true;
303 }
304
305 static int test_pread(void)
306 {
307         /* FIXME */
308         return true;
309 }
310
311 static int test_pwrite(void)
312 {
313         /* FIXME */
314         return true;
315 }
316
317 static int test_getpass(void)
318 {
319         /* FIXME */
320         return true;
321 }
322
323 static int test_inet_ntoa(void)
324 {
325         /* FIXME */
326         return true;
327 }
328
329 static int test_strtoll(void)
330 {
331         /* FIXME */
332         return true;
333 }
334
335 static int test_strtoull(void)
336 {
337         /* FIXME */
338         return true;
339 }
340
341 /* 
342 FIXME:
343 Types:
344 bool
345 socklen_t
346 uint_t
347 uint{8,16,32,64}_t
348 int{8,16,32,64}_t
349 intptr_t
350
351 Constants:
352 PATH_NAME_MAX
353 UINT{16,32,64}_MAX
354 INT32_MAX
355 */
356
357 static int test_va_copy(void)
358 {
359         /* FIXME */
360         return true;
361 }
362
363 static int test_FUNCTION(void)
364 {
365         /* FIXME: test __FUNCTION__ macro */
366         return true;
367 }
368
369 static int test_MIN(void)
370 {
371         /* FIXME */
372         return true;
373 }
374
375 static int test_MAX(void)
376 {
377         /* FIXME */
378         return true;
379 }
380
381 static int test_socketpair(void)
382 {
383         int sock[2];
384         char buf[20];
385
386         printf("test: socketpair\n");
387
388         if (socketpair(AF_UNIX, SOCK_STREAM, 0, sock) == -1) {
389                 printf("failure: socketpair [\n"
390                            "socketpair() failed\n"
391                            "]\n");
392                 return false;
393         }
394
395         if (write(sock[1], "automatisch", 12) == -1) {
396                 printf("failure: socketpair [\n"
397                            "write() failed: %s\n"
398                            "]\n", strerror(errno));
399                 return false;
400         }
401
402         if (read(sock[0], buf, 12) == -1) {
403                 printf("failure: socketpair [\n"
404                            "read() failed: %s\n"
405                            "]\n", strerror(errno));
406                 return false;
407         }
408
409         if (strcmp(buf, "automatisch") != 0) {
410                 printf("failure: socketpair [\n"
411                            "expected: automatisch, got: %s\n"
412                            "]\n", buf);
413                 return false;
414         }
415
416         printf("success: socketpair\n");
417
418         return true;
419 }
420
421 struct torture_context;
422 bool torture_local_replace(struct torture_context *ctx)
423 {
424         bool ret = true;
425         ret &= test_ftruncate();
426         ret &= test_strlcpy();
427         ret &= test_strlcat();
428         ret &= test_mktime();
429         ret &= test_innetgr();
430         ret &= test_initgroups();
431         ret &= test_memmove();
432         ret &= test_strdup();
433         ret &= test_setlinebuf();
434         ret &= test_vsyslog();
435         ret &= test_timegm();
436         ret &= test_setenv();
437         ret &= test_strndup();
438         ret &= test_strnlen();
439         ret &= test_waitpid();
440         ret &= test_seteuid();
441         ret &= test_setegid();
442         ret &= test_asprintf();
443         ret &= test_snprintf();
444         ret &= test_vasprintf();
445         ret &= test_vsnprintf();
446         ret &= test_opendir();
447         ret &= test_readdir();
448         ret &= test_telldir();
449         ret &= test_seekdir();
450         ret &= test_dlopen();
451         ret &= test_chroot();
452         ret &= test_bzero();
453         ret &= test_strerror();
454         ret &= test_errno();
455         ret &= test_mkdtemp();
456         ret &= test_mkstemp();
457         ret &= test_pread();
458         ret &= test_pwrite();
459         ret &= test_getpass();
460         ret &= test_inet_ntoa();
461         ret &= test_strtoll();
462         ret &= test_strtoll();
463         ret &= test_strtoull();
464         ret &= test_va_copy();
465         ret &= test_FUNCTION();
466         ret &= test_MIN();
467         ret &= test_MAX();
468         ret &= test_socketpair();
469
470         return ret;
471 }
472
473 #if _SAMBA_BUILD_<4
474 int main(void)
475 {
476         bool ret = torture_local_replace(NULL);
477         if (ret) 
478                 return 0;
479         return -1;
480 }
481 #endif