Statistics
| Branch: | Revision:

root / tests / test_path.c @ 7fb9a24e

History | View | Annotate | Download (4.5 kB)

1 7fb9a24e bellard
/* Test path override code */
2 7fb9a24e bellard
#define _GNU_SOURCE
3 7fb9a24e bellard
#include "../path.c"
4 7fb9a24e bellard
#include <stdarg.h>
5 7fb9a24e bellard
#include <sys/stat.h>
6 7fb9a24e bellard
#include <fcntl.h>
7 7fb9a24e bellard
8 7fb9a24e bellard
/* Any log message kills the test. */
9 7fb9a24e bellard
void gemu_log(const char *fmt, ...)
10 7fb9a24e bellard
{
11 7fb9a24e bellard
    va_list ap;
12 7fb9a24e bellard
13 7fb9a24e bellard
    fprintf(stderr, "FATAL: ");
14 7fb9a24e bellard
    va_start(ap, fmt);
15 7fb9a24e bellard
    vfprintf(stderr, fmt, ap);
16 7fb9a24e bellard
    va_end(ap);
17 7fb9a24e bellard
    exit(1);
18 7fb9a24e bellard
}
19 7fb9a24e bellard
20 7fb9a24e bellard
#define NO_CHANGE(_path)                                                \
21 7fb9a24e bellard
        do {                                                                \
22 7fb9a24e bellard
            if (strcmp(path(_path), _path) != 0) return __LINE__;        \
23 7fb9a24e bellard
        } while(0)
24 7fb9a24e bellard
25 7fb9a24e bellard
#define CHANGE_TO(_path, _newpath)                                        \
26 7fb9a24e bellard
        do {                                                                \
27 7fb9a24e bellard
            if (strcmp(path(_path), _newpath) != 0) return __LINE__;        \
28 7fb9a24e bellard
        } while(0)
29 7fb9a24e bellard
30 7fb9a24e bellard
static void cleanup(void)
31 7fb9a24e bellard
{
32 7fb9a24e bellard
    unlink("/tmp/qemu-test_path/DIR1/DIR2/FILE");
33 7fb9a24e bellard
    unlink("/tmp/qemu-test_path/DIR1/DIR2/FILE2");
34 7fb9a24e bellard
    unlink("/tmp/qemu-test_path/DIR1/DIR2/FILE3");
35 7fb9a24e bellard
    unlink("/tmp/qemu-test_path/DIR1/DIR2/FILE4");
36 7fb9a24e bellard
    unlink("/tmp/qemu-test_path/DIR1/DIR2/FILE5");
37 7fb9a24e bellard
    rmdir("/tmp/qemu-test_path/DIR1/DIR2");
38 7fb9a24e bellard
    rmdir("/tmp/qemu-test_path/DIR1/DIR3");
39 7fb9a24e bellard
    rmdir("/tmp/qemu-test_path/DIR1");
40 7fb9a24e bellard
    rmdir("/tmp/qemu-test_path");
41 7fb9a24e bellard
}
42 7fb9a24e bellard
43 7fb9a24e bellard
static unsigned int do_test(void)
44 7fb9a24e bellard
{
45 7fb9a24e bellard
    if (mkdir("/tmp/qemu-test_path", 0700) != 0)
46 7fb9a24e bellard
        return __LINE__;
47 7fb9a24e bellard
48 7fb9a24e bellard
    if (mkdir("/tmp/qemu-test_path/DIR1", 0700) != 0)
49 7fb9a24e bellard
        return __LINE__;
50 7fb9a24e bellard
51 7fb9a24e bellard
    if (mkdir("/tmp/qemu-test_path/DIR1/DIR2", 0700) != 0)
52 7fb9a24e bellard
        return __LINE__;
53 7fb9a24e bellard
54 7fb9a24e bellard
    if (mkdir("/tmp/qemu-test_path/DIR1/DIR3", 0700) != 0)
55 7fb9a24e bellard
        return __LINE__;
56 7fb9a24e bellard
57 7fb9a24e bellard
    if (close(creat("/tmp/qemu-test_path/DIR1/DIR2/FILE", 0600)) != 0)
58 7fb9a24e bellard
        return __LINE__;
59 7fb9a24e bellard
60 7fb9a24e bellard
    if (close(creat("/tmp/qemu-test_path/DIR1/DIR2/FILE2", 0600)) != 0)
61 7fb9a24e bellard
        return __LINE__;
62 7fb9a24e bellard
63 7fb9a24e bellard
    if (close(creat("/tmp/qemu-test_path/DIR1/DIR2/FILE3", 0600)) != 0)
64 7fb9a24e bellard
        return __LINE__;
65 7fb9a24e bellard
66 7fb9a24e bellard
    if (close(creat("/tmp/qemu-test_path/DIR1/DIR2/FILE4", 0600)) != 0)
67 7fb9a24e bellard
        return __LINE__;
68 7fb9a24e bellard
69 7fb9a24e bellard
    if (close(creat("/tmp/qemu-test_path/DIR1/DIR2/FILE5", 0600)) != 0)
70 7fb9a24e bellard
        return __LINE__;
71 7fb9a24e bellard
72 7fb9a24e bellard
    init_paths("/tmp/qemu-test_path");
73 7fb9a24e bellard
74 7fb9a24e bellard
    NO_CHANGE("/tmp");
75 7fb9a24e bellard
    NO_CHANGE("/tmp/");
76 7fb9a24e bellard
    NO_CHANGE("/tmp/qemu-test_path");
77 7fb9a24e bellard
    NO_CHANGE("/tmp/qemu-test_path/");
78 7fb9a24e bellard
    NO_CHANGE("/tmp/qemu-test_path/D");
79 7fb9a24e bellard
    NO_CHANGE("/tmp/qemu-test_path/DI");
80 7fb9a24e bellard
    NO_CHANGE("/tmp/qemu-test_path/DIR");
81 7fb9a24e bellard
    NO_CHANGE("/tmp/qemu-test_path/DIR1");
82 7fb9a24e bellard
    NO_CHANGE("/tmp/qemu-test_path/DIR1/");
83 7fb9a24e bellard
84 7fb9a24e bellard
    NO_CHANGE("/D");
85 7fb9a24e bellard
    NO_CHANGE("/DI");
86 7fb9a24e bellard
    NO_CHANGE("/DIR");
87 7fb9a24e bellard
    NO_CHANGE("/DIR2");
88 7fb9a24e bellard
    NO_CHANGE("/DIR1.");
89 7fb9a24e bellard
90 7fb9a24e bellard
    CHANGE_TO("/DIR1", "/tmp/qemu-test_path/DIR1");
91 7fb9a24e bellard
    CHANGE_TO("/DIR1/", "/tmp/qemu-test_path/DIR1");
92 7fb9a24e bellard
93 7fb9a24e bellard
    NO_CHANGE("/DIR1/D");
94 7fb9a24e bellard
    NO_CHANGE("/DIR1/DI");
95 7fb9a24e bellard
    NO_CHANGE("/DIR1/DIR");
96 7fb9a24e bellard
    NO_CHANGE("/DIR1/DIR1");
97 7fb9a24e bellard
98 7fb9a24e bellard
    CHANGE_TO("/DIR1/DIR2", "/tmp/qemu-test_path/DIR1/DIR2");
99 7fb9a24e bellard
    CHANGE_TO("/DIR1/DIR2/", "/tmp/qemu-test_path/DIR1/DIR2");
100 7fb9a24e bellard
101 7fb9a24e bellard
    CHANGE_TO("/DIR1/DIR3", "/tmp/qemu-test_path/DIR1/DIR3");
102 7fb9a24e bellard
    CHANGE_TO("/DIR1/DIR3/", "/tmp/qemu-test_path/DIR1/DIR3");
103 7fb9a24e bellard
104 7fb9a24e bellard
    NO_CHANGE("/DIR1/DIR2/F");
105 7fb9a24e bellard
    NO_CHANGE("/DIR1/DIR2/FI");
106 7fb9a24e bellard
    NO_CHANGE("/DIR1/DIR2/FIL");
107 7fb9a24e bellard
    NO_CHANGE("/DIR1/DIR2/FIL.");
108 7fb9a24e bellard
109 7fb9a24e bellard
    CHANGE_TO("/DIR1/DIR2/FILE", "/tmp/qemu-test_path/DIR1/DIR2/FILE");
110 7fb9a24e bellard
    CHANGE_TO("/DIR1/DIR2/FILE2", "/tmp/qemu-test_path/DIR1/DIR2/FILE2");
111 7fb9a24e bellard
    CHANGE_TO("/DIR1/DIR2/FILE3", "/tmp/qemu-test_path/DIR1/DIR2/FILE3");
112 7fb9a24e bellard
    CHANGE_TO("/DIR1/DIR2/FILE4", "/tmp/qemu-test_path/DIR1/DIR2/FILE4");
113 7fb9a24e bellard
    CHANGE_TO("/DIR1/DIR2/FILE5", "/tmp/qemu-test_path/DIR1/DIR2/FILE5");
114 7fb9a24e bellard
115 7fb9a24e bellard
    NO_CHANGE("/DIR1/DIR2/FILE6");
116 7fb9a24e bellard
    NO_CHANGE("/DIR1/DIR2/FILE/X");
117 7fb9a24e bellard
118 7fb9a24e bellard
    CHANGE_TO("/DIR1/../DIR1", "/tmp/qemu-test_path/DIR1");
119 7fb9a24e bellard
    CHANGE_TO("/DIR1/../DIR1/", "/tmp/qemu-test_path/DIR1");
120 7fb9a24e bellard
    CHANGE_TO("/../DIR1", "/tmp/qemu-test_path/DIR1");
121 7fb9a24e bellard
    CHANGE_TO("/../DIR1/", "/tmp/qemu-test_path/DIR1");
122 7fb9a24e bellard
    CHANGE_TO("/DIR1/DIR2/../DIR2", "/tmp/qemu-test_path/DIR1/DIR2");
123 7fb9a24e bellard
    CHANGE_TO("/DIR1/DIR2/../DIR2/../../DIR1/DIR2/FILE", "/tmp/qemu-test_path/DIR1/DIR2/FILE");
124 7fb9a24e bellard
    CHANGE_TO("/DIR1/DIR2/../DIR2/FILE", "/tmp/qemu-test_path/DIR1/DIR2/FILE");
125 7fb9a24e bellard
126 7fb9a24e bellard
    NO_CHANGE("/DIR1/DIR2/../DIR1");
127 7fb9a24e bellard
    NO_CHANGE("/DIR1/DIR2/../FILE");
128 7fb9a24e bellard
129 7fb9a24e bellard
    CHANGE_TO("/./DIR1/DIR2/FILE", "/tmp/qemu-test_path/DIR1/DIR2/FILE");
130 7fb9a24e bellard
    CHANGE_TO("/././DIR1/DIR2/FILE", "/tmp/qemu-test_path/DIR1/DIR2/FILE");
131 7fb9a24e bellard
    CHANGE_TO("/DIR1/./DIR2/FILE", "/tmp/qemu-test_path/DIR1/DIR2/FILE");
132 7fb9a24e bellard
    CHANGE_TO("/DIR1/././DIR2/FILE", "/tmp/qemu-test_path/DIR1/DIR2/FILE");
133 7fb9a24e bellard
    CHANGE_TO("/DIR1/DIR2/./FILE", "/tmp/qemu-test_path/DIR1/DIR2/FILE");
134 7fb9a24e bellard
    CHANGE_TO("/DIR1/DIR2/././FILE", "/tmp/qemu-test_path/DIR1/DIR2/FILE");
135 7fb9a24e bellard
    CHANGE_TO("/./DIR1/./DIR2/./FILE", "/tmp/qemu-test_path/DIR1/DIR2/FILE");
136 7fb9a24e bellard
137 7fb9a24e bellard
    return 0;
138 7fb9a24e bellard
}
139 7fb9a24e bellard
140 7fb9a24e bellard
int main(int argc, char *argv[])
141 7fb9a24e bellard
{
142 7fb9a24e bellard
    int ret;
143 7fb9a24e bellard
144 7fb9a24e bellard
    ret = do_test();
145 7fb9a24e bellard
    cleanup();
146 7fb9a24e bellard
    if (ret) {
147 7fb9a24e bellard
        fprintf(stderr, "test_path: failed on line %i\n", ret);
148 7fb9a24e bellard
        return 1;
149 7fb9a24e bellard
    }
150 7fb9a24e bellard
    return 0;
151 7fb9a24e bellard
}
152 7fb9a24e bellard