/
test.c
170 lines (155 loc) · 4.06 KB
/
test.c
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
/*
* This file is part of undup
* Copyright (C) 2015, Alejandro Liu
*
* undup is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* undup is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, see <http://www.gnu.org/licenses>
*/
#ifdef __GLIBC__
#include <mcheck.h>
#else
#define mtrace()
#define muntrace()
#endif
#include <cu.h>
#include <unistd.h>
#include "utils.h"
static void _mywrite(int a,const void *b,size_t c,const char *f,int l) {
if (write(a,b,c) == -1) errormsg("write(%s,%d):",f,l);
}
#define write(a,b,c) _mywrite(a,b,c,__FILE__,__LINE__)
#include <cu.c>
#include "test.h"
#include <stdlib.h>
#include <stdio.h>
#include "cu-t.h"
#include <time.h>
#include <unistd.h>
#include <fcntl.h>
#define CLUSTER 100
enum ftypes_t { S_FILE, S_DIR, S_END };
static enum ftypes_t selectors[] = {
S_FILE,
S_FILE,
S_FILE,
S_FILE,
S_FILE,
S_DIR,
S_END,
S_END,
S_END,
};
static void _populate(char *base, int *cnt, int max) {
char buf[64], *p;
int fd;
while (*cnt < max) {
int i = rand() % (sizeof(selectors)/sizeof(selectors[0]));
switch (selectors[i]) {
case S_FILE:
/* Create a file */
snprintf(buf,sizeof buf,"%x",rand());
p = mystrcat(base,"/",buf);
fd = open(p,O_WRONLY|O_CREAT|O_EXCL,0666);
free(p);
if (fd == -1) continue;
snprintf(buf,sizeof buf,"%d", rand() % CLUSTER);
write(fd,buf,strlen(buf));
close(fd);
++(*cnt);
break;
case S_DIR:
/* Create a directory */
snprintf(buf,sizeof buf,"%x",rand());
p = mystrcat(base,"/",buf);
fd = mkdir(p,0777);
if (fd == 0) {
++(*cnt);
_populate(p, cnt, max);
}
free(p);
break;
case S_END:
return;
}
}
}
char *populate(char *base, int max) {
int cnt = 0;
strcpy(base,"/tmp/tmpdirXXXXXXX");
base = mkdtemp(base);
if (base == NULL) return NULL;
while (cnt < max) _populate(base,&cnt,max);
return base;
}
void rm_rf(const char *dir) {
char buf[4096];
snprintf(buf,sizeof buf,"rm -rf \"%s\"", dir);
if (system(buf) == -1) errormsg("system(%s)",buf);
}
void mkfile(const char *dir,const char *fn,const char *data) {
char *p = mystrcat(dir,"/",fn);
int fd = open(p,O_WRONLY|O_CREAT|O_EXCL,0666);
free(p);
write(fd,data,strlen(data));
close(fd);
}
void mklink(const char *dir,const char *old,const char *new) {
char *dold = mystrcat(dir,"/",old);
char *dnew = mystrcat(dir,"/",new);
if (link(dold,dnew) == -1) errormsg("link(%s,%s)",dold,dnew);
free(dnew);
free(dold);
}
ino_t getino(const char *dir, const char *file) {
char *p =mystrcat(dir,"/",file);
struct stat stb;
stb.st_ino = 0;
lstat(p,&stb);
free(p);
return stb.st_ino;
}
int forktest(int xcode) {
fflush(stdout);
fflush(stderr);
pid_t child = fork();
assertNotEquals(child,-1);
if (child == 0) return 1;
if (child && child != -1) {
int status = 0;
pid_t waited;
waited = waitpid(child,&status,0);
assertEquals(child,waited);
assertTrue(WIFEXITED(status));
assertEquals(WEXITSTATUS(status), xcode);
}
return 0;
}
int main(int argc, char **argv) {
char *env;
mtrace();
if ((env = getenv("CU_SEED")) != NULL)
srand(atoi(env));
else {
int seed = ((((unsigned)getpid())*((unsigned)time(NULL)))^((unsigned)time(NULL)))+((unsigned)clock());
printf("repeat run with CU_SEED=%d\n",seed);
srand(seed);
}
// Set up directory where are stored files with outputs from test
// suites
CU_SET_OUT_PREFIX("regressions/");
// Run all test suites
CU_RUN(argc, argv);
muntrace();
if (cu_fail_tests || cu_fail_test_suites) exit(1);
return 0;
}