12
#include "Bootstrap.h"
24
Bootstrap(); // Need to initialize library
26
printf("============> Start File Tests\n\n");
28
snprintf(path, STRLEN, "/tmp/.FileTest.%d", getpid());
30
printf("=> Test1: open/close\n");
33
assert((fd = File_open(path, "w")) != -1);
34
assert(File_close(fd) == true);
35
assert((fd = File_open(path, "w+")) != -1);
36
assert(File_close(fd) == true);
37
assert((fd = File_open("/a/b/c/d", "r")) == -1);
38
assert((fd = File_open(path, "r")) != -1);
39
assert(File_close(fd) == true);
40
assert((fd = File_open(path, "r+")) != -1);
41
assert(File_close(fd) == true);
42
assert((fd = File_open(path, "a")) != -1);
43
assert(File_close(fd) == true);
44
assert((fd = File_open(path, "a+")) != -1);
45
assert(write(fd, "something", sizeof("something") - 1) > 0);
46
assert(File_close(fd) == true);
48
printf("=> Test1: OK\n\n");
50
printf("=> Test2: check properties (class)\n");
57
assert(stat(path, &s) == 0);
58
assert((j = File_mtime(path)) > 0);
59
printf("\tmodification time: %ld\n", j);
60
assert(j == s.st_mtime);
61
assert((j = File_ctime(path)) > 0);
62
printf("\tchange time: %ld\n", j);
63
assert(j == s.st_ctime);
64
assert((j = File_atime(path)) > 0);
65
printf("\taccess time: %ld\n", j);
66
assert(j == s.st_atime);
67
assert((k = File_size(path)) >= 0);
68
printf("\tsize: %lld B\n", k);
69
assert(k == s.st_size);
70
assert((c = File_isFile(path)) == true);
71
assert((c = File_type(path)) == 'r');
72
printf("\ttype: regular file\n");
73
assert((File_exist(path)) == true);
74
printf("\texist: yes\n");
75
assert((i = File_mod(path)) > 0);
76
printf("\tpermission mode: %o\n", i & 07777);
77
assert(i == s.st_mode);
78
assert(File_chmod(path, 00640) == true);
79
assert((File_mod(path) & 07777) == 00640);
80
assert(File_isReadable(path) == true);
81
assert(File_isWritable(path) == true);
82
#if defined(SOLARIS) || defined (FREEBSD) || defined (AIX)
83
/* Some systems like Solaris and FreeBSD return X_OK if the process has
84
* appropriate privilege even if none of the execute file permission bits
87
assert(File_isExecutable(path) == true);
90
assert(File_isExecutable(path) == false);
92
printf("=> Test2: OK\n\n");
94
printf("=> Test3: check directory\n");
96
assert(File_isDirectory("/") == true);
97
assert(File_type("/") == 'd');
98
assert(File_isDirectory(path) == false);
100
printf("=> Test3: OK\n\n");
102
printf("=> Test4: check umask\n");
105
assert((i = File_umask()) >= 0);
106
printf("\tumask: %o\n", i & 07777);
107
assert(File_setUmask(0002) == i);
108
assert(File_setUmask(i) == 0002);
110
printf("=> Test4: OK\n\n");
112
printf("=> Test5: rename\n");
114
File_rename(path, "/tmp/.FileTest.abcde");
115
snprintf(path, STRLEN, "/tmp/.FileTest.abcde"); // the file is kept for later tests
117
printf("=> Test5: OK\n\n");
119
printf("=> Test6: path parsing\n");
122
assert(Str_isEqual(File_basename(path), ".FileTest.abcde"));
123
snprintf(s, STRLEN, "%s", path);
124
assert(Str_isEqual(File_dirname(s), "/tmp/"));
125
assert(Str_isEqual(File_extension(path), "abcde"));
126
char dir_path[] = "/tmp/";
127
assert(Str_isEqual(File_removeTrailingSeparator(dir_path), "/tmp"));
129
printf("=> Test6: OK\n\n");
131
printf("=> Test7: normalize path\n");
134
assert(File_getRealPath("/././tmp/../tmp", s) != NULL);
136
/* On Darwin /tmp is a link to /private/tmp */
137
assert(Str_isEqual(s, "/private/tmp"));
139
assert(Str_isEqual(s, "/tmp"));
142
printf("=> Test7: OK\n\n");
144
printf("=> Test8: delete\n");
146
assert(File_delete(path) == true);
147
assert(File_exist(path) == false);
149
printf("=> Test8: OK\n\n");
151
printf("=> Test9: removeTrailingSeparator\n");
153
char a[] = "/abc/def/";
154
char b[] = "/abc/def";
155
assert(Str_isEqual(File_removeTrailingSeparator(a), "/abc/def"));
156
assert(Str_isEqual(File_removeTrailingSeparator(b), "/abc/def"));
157
assert(Str_isEqual(File_removeTrailingSeparator(""), ""));
158
assert(File_removeTrailingSeparator(NULL) == NULL);
160
printf("=> Test9: OK\n\n");
162
printf("============> File Tests: OK\n\n");