aboutsummaryrefslogtreecommitdiff
path: root/tests/test_path.c
blob: 7d6e831767f187202527827dbb246a12fa757c17 (plain)
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
/* Test path override code */
#define _GNU_SOURCE
#include "../path.c"
#include <stdarg.h>
#include <sys/stat.h>
#include <fcntl.h>

/* Any log message kills the test. */
void gemu_log(const char *fmt, ...)
{
    va_list ap;

    fprintf(stderr, "FATAL: ");
    va_start(ap, fmt);
    vfprintf(stderr, fmt, ap);
    va_end(ap);
    exit(1);
}

#define NO_CHANGE(_path)						\
	do {								\
	    if (strcmp(path(_path), _path) != 0) return __LINE__;	\
	} while(0)

#define CHANGE_TO(_path, _newpath)					\
	do {								\
	    if (strcmp(path(_path), _newpath) != 0) return __LINE__;	\
	} while(0)

static void cleanup(void)
{
    unlink("/tmp/qemu-test_path/DIR1/DIR2/FILE");
    unlink("/tmp/qemu-test_path/DIR1/DIR2/FILE2");
    unlink("/tmp/qemu-test_path/DIR1/DIR2/FILE3");
    unlink("/tmp/qemu-test_path/DIR1/DIR2/FILE4");
    unlink("/tmp/qemu-test_path/DIR1/DIR2/FILE5");
    rmdir("/tmp/qemu-test_path/DIR1/DIR2");
    rmdir("/tmp/qemu-test_path/DIR1/DIR3");
    rmdir("/tmp/qemu-test_path/DIR1");
    rmdir("/tmp/qemu-test_path");
}

static unsigned int do_test(void)
{
    if (mkdir("/tmp/qemu-test_path", 0700) != 0)
	return __LINE__;

    if (mkdir("/tmp/qemu-test_path/DIR1", 0700) != 0)
	return __LINE__;

    if (mkdir("/tmp/qemu-test_path/DIR1/DIR2", 0700) != 0)
	return __LINE__;

    if (mkdir("/tmp/qemu-test_path/DIR1/DIR3", 0700) != 0)
	return __LINE__;

    if (close(creat("/tmp/qemu-test_path/DIR1/DIR2/FILE", 0600)) != 0)
	return __LINE__;

    if (close(creat("/tmp/qemu-test_path/DIR1/DIR2/FILE2", 0600)) != 0)
	return __LINE__;

    if (close(creat("/tmp/qemu-test_path/DIR1/DIR2/FILE3", 0600)) != 0)
	return __LINE__;

    if (close(creat("/tmp/qemu-test_path/DIR1/DIR2/FILE4", 0600)) != 0)
	return __LINE__;

    if (close(creat("/tmp/qemu-test_path/DIR1/DIR2/FILE5", 0600)) != 0)
	return __LINE__;

    init_paths("/tmp/qemu-test_path");

    NO_CHANGE("/tmp");
    NO_CHANGE("/tmp/");
    NO_CHANGE("/tmp/qemu-test_path");
    NO_CHANGE("/tmp/qemu-test_path/");
    NO_CHANGE("/tmp/qemu-test_path/D");
    NO_CHANGE("/tmp/qemu-test_path/DI");
    NO_CHANGE("/tmp/qemu-test_path/DIR");
    NO_CHANGE("/tmp/qemu-test_path/DIR1");
    NO_CHANGE("/tmp/qemu-test_path/DIR1/");

    NO_CHANGE("/D");
    NO_CHANGE("/DI");
    NO_CHANGE("/DIR");
    NO_CHANGE("/DIR2");
    NO_CHANGE("/DIR1.");

    CHANGE_TO("/DIR1", "/tmp/qemu-test_path/DIR1");
    CHANGE_TO("/DIR1/", "/tmp/qemu-test_path/DIR1");

    NO_CHANGE("/DIR1/D");
    NO_CHANGE("/DIR1/DI");
    NO_CHANGE("/DIR1/DIR");
    NO_CHANGE("/DIR1/DIR1");

    CHANGE_TO("/DIR1/DIR2", "/tmp/qemu-test_path/DIR1/DIR2");
    CHANGE_TO("/DIR1/DIR2/", "/tmp/qemu-test_path/DIR1/DIR2");

    CHANGE_TO("/DIR1/DIR3", "/tmp/qemu-test_path/DIR1/DIR3");
    CHANGE_TO("/DIR1/DIR3/", "/tmp/qemu-test_path/DIR1/DIR3");

    NO_CHANGE("/DIR1/DIR2/F");
    NO_CHANGE("/DIR1/DIR2/FI");
    NO_CHANGE("/DIR1/DIR2/FIL");
    NO_CHANGE("/DIR1/DIR2/FIL.");

    CHANGE_TO("/DIR1/DIR2/FILE", "/tmp/qemu-test_path/DIR1/DIR2/FILE");
    CHANGE_TO("/DIR1/DIR2/FILE2", "/tmp/qemu-test_path/DIR1/DIR2/FILE2");
    CHANGE_TO("/DIR1/DIR2/FILE3", "/tmp/qemu-test_path/DIR1/DIR2/FILE3");
    CHANGE_TO("/DIR1/DIR2/FILE4", "/tmp/qemu-test_path/DIR1/DIR2/FILE4");
    CHANGE_TO("/DIR1/DIR2/FILE5", "/tmp/qemu-test_path/DIR1/DIR2/FILE5");

    NO_CHANGE("/DIR1/DIR2/FILE6");
    NO_CHANGE("/DIR1/DIR2/FILE/X");

    CHANGE_TO("/DIR1/../DIR1", "/tmp/qemu-test_path/DIR1");
    CHANGE_TO("/DIR1/../DIR1/", "/tmp/qemu-test_path/DIR1");
    CHANGE_TO("/../DIR1", "/tmp/qemu-test_path/DIR1");
    CHANGE_TO("/../DIR1/", "/tmp/qemu-test_path/DIR1");
    CHANGE_TO("/DIR1/DIR2/../DIR2", "/tmp/qemu-test_path/DIR1/DIR2");
    CHANGE_TO("/DIR1/DIR2/../DIR2/../../DIR1/DIR2/FILE", "/tmp/qemu-test_path/DIR1/DIR2/FILE");
    CHANGE_TO("/DIR1/DIR2/../DIR2/FILE", "/tmp/qemu-test_path/DIR1/DIR2/FILE");

    NO_CHANGE("/DIR1/DIR2/../DIR1");
    NO_CHANGE("/DIR1/DIR2/../FILE");

    CHANGE_TO("/./DIR1/DIR2/FILE", "/tmp/qemu-test_path/DIR1/DIR2/FILE");
    CHANGE_TO("/././DIR1/DIR2/FILE", "/tmp/qemu-test_path/DIR1/DIR2/FILE");
    CHANGE_TO("/DIR1/./DIR2/FILE", "/tmp/qemu-test_path/DIR1/DIR2/FILE");
    CHANGE_TO("/DIR1/././DIR2/FILE", "/tmp/qemu-test_path/DIR1/DIR2/FILE");
    CHANGE_TO("/DIR1/DIR2/./FILE", "/tmp/qemu-test_path/DIR1/DIR2/FILE");
    CHANGE_TO("/DIR1/DIR2/././FILE", "/tmp/qemu-test_path/DIR1/DIR2/FILE");
    CHANGE_TO("/./DIR1/./DIR2/./FILE", "/tmp/qemu-test_path/DIR1/DIR2/FILE");

    return 0;
}

int main(int argc, char *argv[])
{
    int ret;

    ret = do_test();
    cleanup();
    if (ret) {
	fprintf(stderr, "test_path: failed on line %i\n", ret);
	return 1;
    }
    return 0;
}