aboutsummaryrefslogtreecommitdiff
path: root/lib/mlibc/tests/posix/realpath.c
blob: e44de818421829700c96a5a2070af16ea4413f0f (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
#include <unistd.h>
#include <sys/stat.h>
#include <assert.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <signal.h>

#ifdef USE_HOST_LIBC
#define TEST_BASE "/tmp/mlibc-realpath-host-libc"
#else
#define TEST_BASE "/tmp/mlibc-realpath"
#endif

void prepare() {
	assert(!mkdir(TEST_BASE "/", S_IRWXU));
	assert(!mkdir(TEST_BASE "/dir1", S_IRWXU));
	assert(!mkdir(TEST_BASE "/dir2", S_IRWXU));
	assert(!symlink(TEST_BASE "/dir2/", TEST_BASE "/dir1/abs-link"));
	assert(!chdir(TEST_BASE "/dir1"));
	assert(!symlink("../dir2/", TEST_BASE "/dir1/rel-link"));
}

void cleanup(int do_assert) {
	if (do_assert) {
		assert(!unlink(TEST_BASE "/dir1/rel-link"));
		assert(!unlink(TEST_BASE "/dir1/abs-link"));
		assert(!rmdir(TEST_BASE "/dir2"));
		assert(!rmdir(TEST_BASE "/dir1"));
		assert(!rmdir(TEST_BASE "/"));
	} else {
		unlink(TEST_BASE "/dir1/rel-link");
		unlink(TEST_BASE "/dir1/abs-link");
		rmdir(TEST_BASE "/dir2");
		rmdir(TEST_BASE "/dir1");
		rmdir(TEST_BASE "/");
	}
}

void signal_handler(int sig, siginfo_t *info, void *ctx) {
	(void)sig;
	(void)info;
	(void)ctx;

	cleanup(0);

	struct sigaction sa;
	sigemptyset(&sa.sa_mask);
	sa.sa_handler = SIG_DFL;
	sa.sa_flags = 0;

	sigaction(SIGABRT, &sa, NULL);
	abort();
}

int main() {
	char *path;

	struct sigaction sa;
	sigemptyset(&sa.sa_mask);
	sa.sa_sigaction = signal_handler;
	sa.sa_flags = SA_SIGINFO;
	sigaction(SIGABRT, &sa, NULL);

	prepare();

	path = realpath(TEST_BASE "/dir1/", NULL);
	assert(path);
	assert(!strcmp(path, TEST_BASE "/dir1"));
	free(path);

	path = realpath(TEST_BASE "/dir1/../dir2", NULL);
	assert(path);
	assert(!strcmp(path, TEST_BASE "/dir2"));
	free(path);

	path = realpath(TEST_BASE "/dir1/abs-link/", NULL);
	assert(path);
	assert(!strcmp(path, TEST_BASE "/dir2"));
	free(path);

	path = realpath(TEST_BASE "/dir1/rel-link/", NULL);
	assert(path);
	assert(!strcmp(path, TEST_BASE "/dir2"));
	free(path);

	path = realpath(TEST_BASE "/dir1/abs-link/../", NULL);
	assert(path);
	assert(!strcmp(path, TEST_BASE ""));
	free(path);

	path = realpath(TEST_BASE "/dir1/rel-link/../", NULL);
	assert(path);
	assert(!strcmp(path, TEST_BASE ""));
	free(path);

	path = realpath(TEST_BASE "/dir1/abs-link/../dir1/abs-link/", NULL);
	assert(path);
	assert(!strcmp(path, TEST_BASE "/dir2"));
	free(path);

	path = realpath(TEST_BASE "/dir1/rel-link/../dir1/rel-link/", NULL);
	assert(path);
	assert(!strcmp(path, TEST_BASE "/dir2"));
	free(path);

	path = realpath(TEST_BASE "/dir1/abs-link/../dir1/rel-link/", NULL);
	assert(path);
	assert(!strcmp(path, TEST_BASE "/dir2"));
	free(path);

	path = realpath(TEST_BASE "/dir1/rel-link/../dir1/abs-link/", NULL);
	assert(path);
	assert(!strcmp(path, TEST_BASE "/dir2"));
	free(path);

	path = realpath("/tmp", NULL);
	assert(path);
	assert(!strcmp(path, "/tmp"));
	free(path);

	path = realpath("/", NULL);
	assert(path);
	assert(!strcmp(path, "/"));
	free(path);

	path = realpath("//", NULL);
	assert(path);
	assert(!strcmp(path, "/"));
	free(path);

	cleanup(1);
}