aboutsummaryrefslogtreecommitdiff
path: root/tests/libfstree/canonicalize_name.c
blob: f117a0dac8ac924cc31c9fdb765807a775b486b1 (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
/* SPDX-License-Identifier: GPL-3.0-or-later */
/*
 * canonicalize_name.c
 *
 * Copyright (C) 2019 David Oberhollenzer <goliath@infraroot.at>
 */
#include "config.h"
#include "fstree.h"
#include "util/test.h"

static const struct {
	const char *in;
	const char *out;
} must_work[] = {
	{ "", "" },
	{ "/", "" },
	{ "\\", "\\" },
	{ "///", "" },
	{ "\\\\\\", "\\\\\\" },
	{ "/\\//\\\\/", "\\/\\\\" },
	{ "foo/bar/test", "foo/bar/test" },
	{ "foo\\bar\\test", "foo\\bar\\test" },
	{ "/foo/bar/test/", "foo/bar/test" },
	{ "\\foo\\bar\\test\\", "\\foo\\bar\\test\\" },
	{ "///foo//bar//test///", "foo/bar/test" },
	{ "./foo/././bar/test/./.", "foo/bar/test" },
	{ "./foo/././", "foo" },
	{ ".", "" },
	{ "./", "" },
	{ "./.", "" },
	{ "foo/.../bar", "foo/.../bar" },
	{ "foo/.test/bar", "foo/.test/bar" },
};

static const char *must_not_work[] = {
	"..",
	"foo/../bar",
	"../foo/bar",
	"foo/bar/..",
	"foo/bar/../",
};

int main(int argc, char **argv)
{
	char buffer[512];
	size_t i;
	(void)argc; (void)argv;

	for (i = 0; i < sizeof(must_work) / sizeof(must_work[0]); ++i) {
		strcpy(buffer, must_work[i].in);

		if (canonicalize_name(buffer)) {
			fprintf(stderr, "Test case rejected: '%s'\n",
				must_work[i].in);
			return EXIT_FAILURE;
		}

		if (strcmp(buffer, must_work[i].out) != 0) {
			fprintf(stderr, "Expected result: %s\n",
				must_work[i].out);
			fprintf(stderr, "Actual result: %s\n", buffer);
			return EXIT_FAILURE;
		}
	}

	for (i = 0; i < sizeof(must_not_work) / sizeof(must_not_work[0]); ++i) {
		strcpy(buffer, must_not_work[i]);

		if (canonicalize_name(buffer) == 0) {
			fprintf(stderr, "Test case accepted: '%s'\n",
				must_not_work[i]);
			fprintf(stderr, "Transformed into: '%s'\n", buffer);
			return EXIT_FAILURE;
		}
	}

	return EXIT_SUCCESS;
}