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