~ubuntu-branches/ubuntu/trusty/monit/trusty

« back to all changes in this revision

Viewing changes to .pc/11_enable_hurd.patch/libmonit/test/FileTest.c

  • Committer: Package Import Robot
  • Author(s): Sergey B Kirpichev
  • Date: 2012-12-12 22:52:55 UTC
  • Revision ID: package-import@ubuntu.com-20121212225255-cf91fsgh5x8ylbd3
Tags: 1:5.5-4
* Fix (I think) FTBFS on ia64 due to installed automake
* Update patch 11: fix PATH_MAX issues for Hurd

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 
 
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
        }
 
48
        printf("=> Test1: OK\n\n");
 
49
 
 
50
        printf("=> Test2: check properties (class)\n");
 
51
        {
 
52
                char c;
 
53
                int i;
 
54
                long j;
 
55
                long long k;
 
56
                struct stat s;
 
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
 
85
                 * are set. */
 
86
                if (getuid() == 0)
 
87
                        assert(File_isExecutable(path) == true);
 
88
                else
 
89
#endif
 
90
                assert(File_isExecutable(path) == false);
 
91
        }
 
92
        printf("=> Test2: OK\n\n");
 
93
 
 
94
        printf("=> Test3: check directory\n");
 
95
        {
 
96
                assert(File_isDirectory("/") == true);
 
97
                assert(File_type("/") == 'd');
 
98
                assert(File_isDirectory(path) == false);
 
99
        }
 
100
        printf("=> Test3: OK\n\n");
 
101
 
 
102
        printf("=> Test4: check umask\n");
 
103
        {
 
104
                int i;
 
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);
 
109
        }
 
110
        printf("=> Test4: OK\n\n");
 
111
 
 
112
        printf("=> Test5: rename\n");
 
113
        {
 
114
                File_rename(path, "/tmp/.FileTest.abcde");
 
115
                snprintf(path, STRLEN, "/tmp/.FileTest.abcde"); // the file is kept for later tests
 
116
        }
 
117
        printf("=> Test5: OK\n\n");
 
118
 
 
119
        printf("=> Test6: path parsing\n");
 
120
        {
 
121
                char s[STRLEN];
 
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"));
 
128
        }
 
129
        printf("=> Test6: OK\n\n");
 
130
 
 
131
        printf("=> Test7: normalize path\n");
 
132
        {
 
133
                char s[PATH_MAX];
 
134
                assert(File_getRealPath("/././tmp/../tmp", s) != NULL);
 
135
#ifdef DARWIN
 
136
                /* On Darwin /tmp is a link to /private/tmp */
 
137
                assert(Str_isEqual(s, "/private/tmp")); 
 
138
#else
 
139
                assert(Str_isEqual(s, "/tmp"));
 
140
#endif
 
141
        }
 
142
        printf("=> Test7: OK\n\n");
 
143
 
 
144
        printf("=> Test8: delete\n");
 
145
        {
 
146
                assert(File_delete(path) == true);
 
147
                assert(File_exist(path) == false);
 
148
        }
 
149
        printf("=> Test8: OK\n\n");
 
150
 
 
151
        printf("=> Test9: removeTrailingSeparator\n");
 
152
        {
 
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);
 
159
        }
 
160
        printf("=> Test9: OK\n\n");
 
161
 
 
162
        printf("============> File Tests: OK\n\n");
 
163
 
 
164
        return 0;
 
165
}
 
166
 
 
167