File: test_pathcmp.c

package info (click to toggle)
burp 3.1.4-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,684 kB
  • sloc: ansic: 50,989; sh: 3,612; cpp: 2,859; makefile: 868
file content (164 lines) | stat: -rw-r--r-- 3,355 bytes parent folder | download | duplicates (4)
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
153
154
155
156
157
158
159
160
161
162
163
164
#include <check.h>
#include <stdlib.h>
#include "test.h"
#include "../src/pathcmp.h"

#define SAME	0
#define APRIOR	-1
#define ALATER	1

struct data
{
	int expected;
	const char *a;
	const char *b;
};

static struct data p[] = {
	{ SAME,		NULL,		NULL },
	{ APRIOR,	NULL,		"" },
	{ ALATER,	"",		NULL },
	{ SAME,		"",		"" },
	{ SAME,		"/",		"/" },
	{ SAME,		"C:",		"C:" },
	{ APRIOR,	"a",		"b" },
	{ ALATER,	"b",		"a" },
	{ APRIOR,	"A",		"a" },
	{ ALATER,	"a",		"B" },
	{ SAME,		"/some/path",	"/some/path" },
	{ ALATER,	"/some/path/",	"/some/path" },
	{ APRIOR,	"/some/path",	"/some/path/" },
	{ APRIOR,	"/some/path",	"/some/pathy" },
	{ ALATER,	"/some/path",	"/som/path" },
	{ ALATER,	"/long/p/a/t/h","/long/p/a/s/t" },
	{ APRIOR,	"/some/path",	"/someppath" },
	{ APRIOR,	"/some/path",	"/some path" },
};

START_TEST(test_pathcmp)
{
	FOREACH(p) fail_unless(pathcmp(p[i].a, p[i].b)==p[i].expected);
}
END_TEST

struct data_s
{
	int expected;
	int a;
	int b;
};

// pathcmp has a bug that we have to live with, where it compares signed chars
// instead of unsigned chars. It means that it orders chars above 127 (0x7F)
// first.
static struct data_s p_s[] = {
	{ APRIOR,	0x00,		0xFF },
	{ APRIOR,	0x7E,		0x7F },
	{ APRIOR,	0x80,		0x7F },
	{ APRIOR,	0x80,		0x81 },
};

START_TEST(test_pathcmp_s)
{
	char a[2]="";
	char b[2]="";
	FOREACH(p_s)
	{
		snprintf(a, sizeof(a), "%c", p_s[i].a);
		snprintf(b, sizeof(b), "%c", p_s[i].b);
		fail_unless(pathcmp(a, b)==p_s[i].expected);
	}
}
END_TEST

static struct data s[] = {
	{ 0,		NULL,		NULL },
	{ 0,		"",		NULL },
	{ 0,		NULL,		"" },
	{ 1,		"",		"" },
	{ 2,		"/",		"/" },
	{ 1,		"",		"/" },
	{ 3,		"/some/path",	"/some/path" },
	{ 0,		"/some/path",	"/some/pathx" },
	{ 0,		"/some/pathx",	"/some/path" },
	{ 2,		"/",		"/a/b/c" },
	{ 3,		"/a/b",		"/a/b/c/d" },
	{ 0,		"/d/c/b/a",	"/a/b/c/d" },
	{ 2,		"/bin",		"/bin/bash" },
	{ 3,		"/bin/",	"/bin/bash" },
	{ 1,		"C:",		"C:" },
	{ 1,		"C:",		"C:/" },
	{ 0,		"C:/",		"C:" },
	{ 2,		"C:/",		"C:/Program Files" },
};

START_TEST(test_is_subdir)
{
	FOREACH(s) fail_unless(is_subdir(s[i].a, s[i].b)==s[i].expected);
}
END_TEST

struct abs
{
	int expected_dot;
	int expected_abs;
	const char *a;
};

static struct abs a[] = {
	{ 0, 0, "foo/bar" },
#ifndef HAVE_WIN32
	{ 0, 1, "/foo/bar" },
	{ 0, 1, "/foo..bar" },
	{ 0, 1, "/foo/bar.." },
	{ 0, 1, "/foo../bar" },
#endif
	{ 0, 0, ":/foo/bar" },
	{ 1, 0, ".." },
	{ 1, 0, "../" },
	{ 1, 0, "/foo/.." },
	{ 1, 0, "/foo/../" },
	{ 1, 0, "/foo/../bar" },
	{ 1, 0, "." },
	{ 1, 0, "./" },
	{ 1, 0, "/foo/." },
	{ 1, 0, "/foo/./" },
	{ 1, 0, "/foo/./bar" },
	{ 0, 1, "C:/foo/bar" },
	{ 0, 1, "D:/foo/bar" },
	{ 0, 1, "C:/foo..bar" },
	{ 0, 1, "C:/foo/bar.." },
	{ 0, 1, "C:/foo../bar" },
	{ 0, 0, "CD:/foo/bar" },
};

START_TEST(test_has_dot_component)
{
	FOREACH(a) fail_unless(has_dot_component(a[i].a)==a[i].expected_dot);
}
END_TEST

START_TEST(test_is_absolute)
{
	FOREACH(a) fail_unless(is_absolute(a[i].a)==a[i].expected_abs);
}
END_TEST

Suite *suite_pathcmp(void)
{
	Suite *s;
	TCase *tc_core;

	s=suite_create("pathcmp");

	tc_core=tcase_create("Core");

	tcase_add_test(tc_core, test_pathcmp);
	tcase_add_test(tc_core, test_pathcmp_s);
	tcase_add_test(tc_core, test_is_subdir);
	tcase_add_test(tc_core, test_has_dot_component);
	tcase_add_test(tc_core, test_is_absolute);
	suite_add_tcase(s, tc_core);

	return s;
}