[go: up one dir, main page]

File: runtest.c

package info (click to toggle)
avfs 1.1.4-2
  • links: PTS
  • area: main
  • in suites: bookworm
  • size: 3,756 kB
  • sloc: ansic: 30,979; sh: 6,276; perl: 1,916; makefile: 348
file content (296 lines) | stat: -rw-r--r-- 5,591 bytes parent folder | download | duplicates (7)
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
/*  
    AVFS: A Virtual File System Library
    Copyright (C) 1998  Miklos Szeredi <miklos@szeredi.hu>
    
    This program can be distributed under the terms of the GNU GPL.
    See the file COPYING.
*/

#include "virtual.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>

#define TESTDIR "/tmp/avfstest"

typedef int (*testfunc) (void *);

struct test {
    char *name;
    struct test *next;

    struct test *sub;
    void *data;
    testfunc func;
};


#define TESTFILESIZE 102400
struct filetest {
    const char *filename;
    unsigned char data[TESTFILESIZE];
};

static struct test *test_new(struct test *tg, const char *name)
{
    struct test *ng;
    struct test **tgp;

    for(tgp = &tg->sub; *tgp != NULL; tgp = &(*tgp)->next);

    ng = malloc(sizeof(*ng));
    ng->sub = NULL;
    ng->next = NULL;
    ng->name = strdup(name);
    ng->data = NULL;
    ng->func = NULL;
    
    *tgp = ng;

    return ng;
}

static void test_add(struct test *tg, const char *name, void *data,
                     testfunc func)
{
    struct test *tc;

    tc = test_new(tg, name);
    tc->data = data;
    tc->func = func;
}

static void tab(int i)
{
    for(; i > 0; i--)
        putchar(' ');
}

static int test_run_path(struct test *tg, const char *path)
{
    int ok;
    int res;
    char *newpath;

    if(tg == NULL)
        return 0;

    newpath = malloc(strlen(path) + 1 + strlen(tg->name) + 1);

    sprintf(newpath, "%s.%s", path, tg->name);
    ok = test_run_path(tg->sub, newpath);
    if(tg->func != NULL) {
        int len = printf("%s:", newpath);
        tab(60 - len);
        
        res = tg->func(tg->data);
        printf("%s\n", res ? "OK" : "FAILED");
        if(!res)
            ok = 0;
    }

    free(newpath);

    res = test_run_path(tg->next, path);
    if(!res)
        ok = 0;

    return ok;
}

static int test_run(struct test *tg)
{
    return test_run_path(tg, "");
}

static int test_rmr(const char *file)
{
    int res;
    DIR *dirp;
    struct dirent *ent;
    char *name;

    res = unlink(file);
    if(res == 0)
        return 0;

    res = rmdir(file);
    if(res == 0)
        return 0;

    dirp = opendir(file);
    if(dirp == NULL)
        return -1;

    while((ent = readdir(dirp)) != NULL) {
        name = ent->d_name;
    
        if(name[0] != '.' || (name[1] && (name[1] != '.' || name[2]))) {
            char *newname;

            newname = malloc(strlen(file) + 1 + strlen(name) + 1);
            sprintf(newname, "%s/%s", file, name);
            test_rmr(newname);
            free(newname);
        }
    }
    closedir(dirp);

    return rmdir(file);
}

static void test_init()
{
    test_rmr(TESTDIR);
    mkdir(TESTDIR, 0777);
}

static char *test_file(const char *name)
{
    char *fullname = malloc(strlen(TESTDIR) + 1 + strlen(name) + 1);

    sprintf(fullname, "%s/%s", TESTDIR, name);

    return fullname;
}

static int file_create(struct filetest *ft)
{
    int res;
    int fd;
    off_t off;
    size_t size;

    res = virt_open(ft->filename, O_WRONLY | O_CREAT | O_EXCL, 0666);
    if(res == -1)
        return 0;

    fd = res;
    for(off = 0; off < TESTFILESIZE; off += size) {
        size = rand() % TESTFILESIZE / 2;

        if(off + size > TESTFILESIZE)
            size = TESTFILESIZE - off;
        
        res = virt_write(fd, ft->data + off, size);
        if(res != size)
            return 0;
    }

    res = virt_close(fd);
    if(res == -1)
        return 0;

    return 1;
}

static int flush_cache()
{
    int res;
    int fd;

    fd = virt_open("/#avfsstat/cache/clear", O_WRONLY, 0);
    if(fd == -1)
        return -1;

    res = virt_write(fd, "1", 1);
    if(res == -1)
        return -1;

    res = virt_close(fd);
    if(res == -1)
        return -1;

    return 0;
}

static int file_contents(struct filetest *ft)
{
    int res;
    int fd;
    off_t off;
    size_t size;
    char buf[TESTFILESIZE / 2];

    res = flush_cache();
    if(res == -1)
        return 0;

    res = virt_open(ft->filename, O_RDONLY, 0);
    if(res == -1)
        return 0;

    fd = res;
    for(off = 0; off < TESTFILESIZE; off += size) {
        size = rand() % TESTFILESIZE / 2;

        res = virt_read(fd, buf, size);

        if(off + size > TESTFILESIZE)
            size = TESTFILESIZE - off;

        if(res != size)
            return 0;

        if(memcmp(ft->data + off, buf, size) != 0)
            return 0;
    }
    
    res = virt_read(fd, buf, 100);
    if(res != 0)
        return 0;

    res = virt_close(fd);
    if(res == -1)
        return 0;

    return 1;
}



static void add_filetest(struct test *tg, const char *testname,
                         const char *filename)
{
    struct filetest *ft;
    struct test *ftg;
    int i;

    srand(1);
    ft = (struct filetest *) malloc(sizeof(*ft));

    ftg = test_new(tg, testname);

    ft->filename = filename;
    for(i = 0; i < TESTFILESIZE; i++)
        ft->data[i] = (i + (!(rand() % 30) ? rand() : 0)) % 0x100;

    test_add(ftg, "create", ft, (testfunc) file_create);
    test_add(ftg, "contents", ft, (testfunc) file_contents);
}


int main(int argc, char *argv[])
{
    int res;
    struct test root;
    struct test *tg;

    test_init();

    root.sub = NULL;
    tg = test_new(&root, "filetest");
    add_filetest(tg, "ugzip", test_file("t.gz#ugzip"));
    add_filetest(tg, "ubzip2", test_file("t.bz2#ubzip2"));
    add_filetest(tg, "volatile", "/#volatile/testfile");
    
    res = test_run(tg);

    if(res == 0)
        return 1;
    else
        return 0;
}