1 #include "Config.h"
2
3 #include <stdio.h>
4 #include <assert.h>
5 #include <string.h>
6 #include <sys/types.h>
7 #include <sys/stat.h>
8 #include <unistd.h>
9 #include <limits.h>
10 #include <stdarg.h>
11
12 #include "Bootstrap.h"
13 #include "Str.h"
14 #include "File.h"
15
16 /**
17 * File.c unity tests.
18 */
19
20
main(void)21 int main(void) {
22 char path[STRLEN];
23
24 Bootstrap(); // Need to initialize library
25
26 printf("============> Start File Tests\n\n");
27
28 snprintf(path, STRLEN, "/tmp/.FileTest.%d", getpid());
29
30 printf("=> Test1: open/close\n");
31 {
32 int fd;
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);
47 assert((fd = File_open(NULL, NULL)) == -1);
48 }
49 printf("=> Test1: OK\n\n");
50
51 printf("=> Test2: check properties (class)\n");
52 {
53 char c;
54 int i;
55 long j;
56 long long k;
57 struct stat s;
58 assert(stat(path, &s) == 0);
59 assert((j = File_mtime(path)) > 0);
60 printf("\tmodification time: %ld\n", j);
61 assert(j == s.st_mtime);
62 assert((j = File_mtime(NULL)) == -1);
63 assert((j = File_ctime(path)) > 0);
64 printf("\tchange time: %ld\n", j);
65 assert(j == s.st_ctime);
66 assert((j = File_ctime(NULL)) == -1);
67 assert((j = File_atime(path)) > 0);
68 printf("\taccess time: %ld\n", j);
69 assert(j == s.st_atime);
70 assert((j = File_atime(NULL)) == -1);
71 assert((k = File_size(path)) >= 0);
72 printf("\tsize: %lld B\n", k);
73 assert(k == s.st_size);
74 assert(File_size(NULL) == -1);
75 assert(File_size("blabla123") == -1);
76 assert((c = File_isFile(path)) == true);
77 assert((c = File_isFile(NULL)) == false);
78 assert((c = File_type(path)) == 'r');
79 assert(File_type(NULL) == '?');
80 printf("\ttype: regular file\n");
81 assert((File_exist(path)) == true);
82 assert((File_exist(NULL)) == false);
83 printf("\texist: yes\n");
84 assert((i = File_mod(path)) > 0);
85 printf("\tpermission mode: %o\n", i & 07777);
86 assert(i == (int)s.st_mode);
87 assert(File_chmod(path, 00640) == true);
88 assert((File_mod(path) & 07777) == 00640);
89 assert(File_isReadable(path) == true);
90 assert(File_isReadable(NULL) == false);
91 assert(File_isWritable(path) == true);
92 assert(File_isWritable(NULL) == false);
93 assert(File_chmod(NULL, 00640) == false);
94 assert(File_delete(NULL) == false);
95 assert(File_rename(NULL, NULL) == false);
96 assert(File_basename(NULL) == NULL);
97 assert(File_extension(NULL) == NULL);
98 assert(File_extension("") == NULL);
99 #if defined(SOLARIS) || defined (AIX) || defined(DRAGONFLY)
100 /* Some systems return X_OK if the process has appropriate privilege even if none of the execute file permission bits are set. */
101 if (getuid() == 0)
102 assert(File_isExecutable(path) == true);
103 else
104 #endif
105 assert(File_isExecutable(path) == false);
106 assert(File_isExecutable(NULL) == false);
107 }
108 printf("=> Test2: OK\n\n");
109
110 printf("=> Test3: check directory\n");
111 {
112 assert(File_isDirectory("/") == true);
113 assert(File_isDirectory(NULL) == false);
114 assert(File_type("/") == 'd');
115 assert(File_isDirectory(path) == false);
116 }
117 printf("=> Test3: OK\n\n");
118
119 printf("=> Test4: check umask\n");
120 {
121 int i;
122 assert((i = File_umask()) >= 0);
123 printf("\tumask: %o\n", i & 07777);
124 assert((int)File_setUmask(0002) == i);
125 assert(File_setUmask(i) == 0002);
126 }
127 printf("=> Test4: OK\n\n");
128
129 printf("=> Test5: rename\n");
130 {
131 File_rename(path, "/tmp/.FileTest.abcde");
132 snprintf(path, STRLEN, "/tmp/.FileTest.abcde"); // the file is kept for later tests
133 }
134 printf("=> Test5: OK\n\n");
135
136 printf("=> Test6: path parsing\n");
137 {
138 char s[STRLEN];
139 assert(Str_isEqual(File_basename(path), ".FileTest.abcde"));
140 snprintf(s, STRLEN, "%s", path);
141 assert(Str_isEqual(File_dirname(s), "/tmp/"));
142 assert(Str_isEqual(File_extension(path), "abcde"));
143 snprintf(s, STRLEN, "tmp");
144 assert(Str_isEqual(File_dirname(s), "."));
145 assert(File_extension(NULL) == NULL);
146 char dir_path[] = "/tmp/";
147 assert(Str_isEqual(File_removeTrailingSeparator(dir_path), "/tmp"));
148 }
149 printf("=> Test6: OK\n\n");
150
151 printf("=> Test7: normalize path\n");
152 {
153 char s[PATH_MAX];
154 assert(File_getRealPath(NULL, s) == NULL);
155 assert(File_getRealPath(s, NULL) == NULL);
156 assert(File_getRealPath(NULL, NULL) == NULL);
157 assert(File_getRealPath("/././tmp/../tmp", s) != NULL);
158 #ifdef DARWIN
159 /* On Darwin /tmp is a link to /private/tmp */
160 assert(Str_isEqual(s, "/private/tmp"));
161 #else
162 assert(Str_isEqual(s, "/tmp"));
163 #endif
164 }
165 printf("=> Test7: OK\n\n");
166
167 printf("=> Test8: delete\n");
168 {
169 assert(File_delete(path) == true);
170 assert(File_exist(path) == false);
171 }
172 printf("=> Test8: OK\n\n");
173
174 printf("=> Test9: removeTrailingSeparator\n");
175 {
176 char a[] = "/abc/def/";
177 char b[] = "/abc/def";
178 assert(Str_isEqual(File_removeTrailingSeparator(a), "/abc/def"));
179 assert(Str_isEqual(File_removeTrailingSeparator(b), "/abc/def"));
180 assert(Str_isEqual(File_removeTrailingSeparator(""), ""));
181 assert(File_removeTrailingSeparator(NULL) == NULL);
182 }
183 printf("=> Test9: OK\n\n");
184
185 printf("=> Test10: check socket\n");
186 {
187 assert(File_isSocket("/") == false);
188 assert(File_isSocket(NULL) == false);
189 //FIXME: add socket test that'll return true
190 }
191 printf("=> Test10: OK\n\n");
192
193 printf("============> File Tests: OK\n\n");
194
195 return 0;
196 }
197
198
199