File: FileTest.c

package info (click to toggle)
monit 1%3A5.35.2-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 8,228 kB
  • sloc: ansic: 31,202; yacc: 4,634; sh: 4,071; lex: 1,190; pascal: 480; makefile: 285
file content (198 lines) | stat: -rw-r--r-- 7,469 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#include "Config.h"

#include <stdio.h>
#include <assert.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <limits.h>
#include <stdarg.h>

#include "Bootstrap.h"
#include "Str.h"
#include "File.h"

/**
 * File.c unity tests.
 */


int main(void) {
        char path[STRLEN];

        Bootstrap(); // Need to initialize library

        printf("============> Start File Tests\n\n");

        snprintf(path, STRLEN, "/tmp/.FileTest.%d", getpid());

        printf("=> Test1: open/close\n");
        {
                int fd;
                assert((fd = File_open(path, "w")) != -1);
                assert(File_close(fd) == true);
                assert((fd = File_open(path, "w+")) != -1);
                assert(File_close(fd) == true);
                assert((fd = File_open("/a/b/c/d", "r")) == -1);
                assert((fd = File_open(path, "r")) != -1);
                assert(File_close(fd) == true);
                assert((fd = File_open(path, "r+")) != -1);
                assert(File_close(fd) == true);
                assert((fd = File_open(path, "a")) != -1);
                assert(File_close(fd) == true);
                assert((fd = File_open(path, "a+")) != -1);
                assert(write(fd, "something", sizeof("something") - 1) > 0);
                assert(File_close(fd) == true);
                assert((fd = File_open(NULL, NULL)) == -1);
        }
        printf("=> Test1: OK\n\n");

        printf("=> Test2: check properties (class)\n");
        {
                char c;
                int i;
                long j;
                long long k;
                struct stat s;
                assert(stat(path, &s) == 0);
                assert((j = File_mtime(path)) > 0);
                printf("\tmodification time: %ld\n", j);
                assert(j == s.st_mtime);
                assert((j = File_mtime(NULL)) == -1);
                assert((j = File_ctime(path)) > 0);
                printf("\tchange time: %ld\n", j);
                assert(j == s.st_ctime);
                assert((j = File_ctime(NULL)) == -1);
                assert((j = File_atime(path)) > 0);
                printf("\taccess time: %ld\n", j);
                assert(j == s.st_atime);
                assert((j = File_atime(NULL)) == -1);
                assert((k = File_size(path)) >= 0);
                printf("\tsize: %lld B\n", k);
                assert(k == s.st_size);
                assert(File_size(NULL) == -1);
                assert(File_size("blabla123") == -1);
                assert((c = File_isFile(path)) == true);
                assert((c = File_isFile(NULL)) == false);
                assert((c = File_type(path)) == 'r');
                assert(File_type(NULL) == '?');
                printf("\ttype: regular file\n");
                assert((File_exist(path)) == true);
                assert((File_exist(NULL)) == false);
                printf("\texist: yes\n");
                assert((i = File_mod(path)) > 0);
                printf("\tpermission mode: %o\n", i & 07777);
                assert(i == (int)s.st_mode);
                assert(File_chmod(path, 00640) == true);
                assert((File_mod(path) & 07777) == 00640);
                assert(File_isReadable(path) == true);
                assert(File_isReadable(NULL) == false);
                assert(File_isWritable(path) == true);
                assert(File_isWritable(NULL) == false);
                assert(File_chmod(NULL, 00640) == false);
                assert(File_delete(NULL) == false);
                assert(File_rename(NULL, NULL) == false);
                assert(File_basename(NULL) == NULL);
                assert(File_extension(NULL) == NULL);
                assert(File_extension("") == NULL);
#if defined(SOLARIS) || defined (AIX) || defined(DRAGONFLY)
                /* Some systems return X_OK if the process has appropriate privilege even if none of the execute file permission bits are set. */
                if (getuid() == 0)
                        assert(File_isExecutable(path) == true);
                else
#endif
                assert(File_isExecutable(path) == false);
                assert(File_isExecutable(NULL) == false);
        }
        printf("=> Test2: OK\n\n");

        printf("=> Test3: check directory\n");
        {
                assert(File_isDirectory("/") == true);
                assert(File_isDirectory(NULL) == false);
                assert(File_type("/") == 'd');
                assert(File_isDirectory(path) == false);
        }
        printf("=> Test3: OK\n\n");

        printf("=> Test4: check umask\n");
        {
                int i;
                assert((i = File_umask()) >= 0);
                printf("\tumask: %o\n", i & 07777);
                assert((int)File_setUmask(0002) == i);
                assert(File_setUmask(i) == 0002);
        }
        printf("=> Test4: OK\n\n");

        printf("=> Test5: rename\n");
        {
                File_rename(path, "/tmp/.FileTest.abcde");
                snprintf(path, STRLEN, "/tmp/.FileTest.abcde"); // the file is kept for later tests
        }
        printf("=> Test5: OK\n\n");

        printf("=> Test6: path parsing\n");
        {
                char s[STRLEN];
                assert(Str_isEqual(File_basename(path), ".FileTest.abcde"));
                snprintf(s, STRLEN, "%s", path);
                assert(Str_isEqual(File_dirname(s), "/tmp/"));
                assert(Str_isEqual(File_extension(path), "abcde"));
                snprintf(s, STRLEN, "tmp");
                assert(Str_isEqual(File_dirname(s), "."));
                assert(File_extension(NULL) == NULL);
                char dir_path[] = "/tmp/";
                assert(Str_isEqual(File_removeTrailingSeparator(dir_path), "/tmp"));
        }
        printf("=> Test6: OK\n\n");

        printf("=> Test7: normalize path\n");
        {
                char s[PATH_MAX];
                assert(File_realPath(NULL, s) == NULL);
                assert(File_realPath(s, NULL) == NULL);
                assert(File_realPath(NULL, NULL) == NULL);
                assert(File_realPath("/././tmp/../tmp", s) != NULL);
#ifdef DARWIN
                /* On Darwin /tmp is a link to /private/tmp */
                assert(Str_isEqual(s, "/private/tmp"));
#else
                assert(Str_isEqual(s, "/tmp"));
#endif
        }
        printf("=> Test7: OK\n\n");

        printf("=> Test8: delete\n");
        {
                assert(File_delete(path) == true);
                assert(File_exist(path) == false);
        }
        printf("=> Test8: OK\n\n");

        printf("=> Test9: removeTrailingSeparator\n");
        {
                char a[] = "/abc/def/";
                char b[] = "/abc/def";
                assert(Str_isEqual(File_removeTrailingSeparator(a), "/abc/def"));
                assert(Str_isEqual(File_removeTrailingSeparator(b), "/abc/def"));
                assert(Str_isEqual(File_removeTrailingSeparator(""), ""));
                assert(File_removeTrailingSeparator(NULL) == NULL);
        }
        printf("=> Test9: OK\n\n");

        printf("=> Test10: check socket\n");
        {
                assert(File_isSocket("/") == false);
                assert(File_isSocket(NULL) == false);
                //FIXME: add socket test that'll return true
        }
        printf("=> Test10: OK\n\n");

        printf("============> File Tests: OK\n\n");

        return 0;
}