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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
|
#include <stdio.h>
#include <string.h>
#include "browse.h"
#include "delete.h"
#include "download.h"
#include "http.h"
#include "upload.h"
#include "url.h"
/* minunit, see https://jera.com/techinfo/jtns/jtn002 */
#define mu_assert(message, test) do { if (!(test)) return message; } while (0)
#define mu_run_test(test) do { char* message = test(); tests_run++; \
if(message) return message; } while (0)
#define BUF_SZ 1000
int tests_run;
static char *
test_browse_invalid_traversal()
{
struct kreq r;
struct http_ret ret;
/* attempt to traverse out of DATA_DIR... */
r = (struct kreq) {
.path = "..",
.suffix = "",
.mime = KMIME_TEXT_HTML
};
ret = browse(&r);
/* ...should return an error */
mu_assert("error, browse allowed invalid traversal!",
ret.code >= KHTTP_400);
return 0;
}
static char *
test_browse_path_too_long()
{
struct kreq r;
struct http_ret ret;
/* a lengthy path should cause URL overflow... */
r = (struct kreq) {
.path = "this/is/a/very/very/lengthy/path/that/should/overflow/the/maximum/allowed/path/length/of/PATH_MAX/well/of/course/this/limit/is/platform/dependent",
.suffix = "",
.mime = KMIME_TEXT_HTML
};
ret = browse(&r);
mu_assert("error, browse allowed invalid traversal!",
ret.code >= KHTTP_400);
return 0;
}
static char *
test_upload_post()
{
char *file_content;
struct kreq r;
struct http_ret ret;
struct kpair fields[2];
file_content = "text file";
fields[0] = (struct kpair) {
.key = "file",
.file = "bob.txt",
.val = file_content,
.valsz = strlen(file_content)
};
fields[1] = (struct kpair) {
.key = "file",
.file = "alice.txt",
.val = file_content,
.valsz = strlen(file_content)
};
r = (struct kreq) {
.pname = "/vault",
.path = "",
.method = KMETHOD_POST,
.fieldsz = 2,
.fields = fields
};
ret = upload(&r);
mu_assert("error, POST upload failed!", ret.code <= KHTTP_400);
return 0;
}
static char *
test_download()
{
struct kreq r;
struct http_ret ret;
r = (struct kreq) {
.pname = "/vault",
.path = "a",
.suffix = "txt",
};
ret = download(&r);
mu_assert("error, download failed!", ret.code <= KHTTP_400);
return 0;
}
static char *
test_delete_get()
{
struct kreq r;
struct http_ret ret;
r = (struct kreq) {
.pname = "/vault",
.path = "a",
.suffix = "txt",
.method = KMETHOD_GET,
};
ret = del(&r);
mu_assert("error, GET delete failed!", ret.code <= KHTTP_400);
return 0;
}
static char *
test_delete_post()
{
struct kreq r;
struct http_ret ret;
r = (struct kreq) {
.pname = "/vault",
.path = "Folder",
.suffix = "",
.method = KMETHOD_POST,
};
ret = del(&r);
mu_assert("error, POST delete failed!", ret.code <= KHTTP_400);
return 0;
}
static char *
test_url_build()
{
char dst[BUF_SZ], *expected;
size_t dst_len;
expected = "/vault/browse";
dst_len = url_build(dst, BUF_SZ, "/vault/browse", "", NULL);
mu_assert("test_url_build failed", dst_len > 0 && dst_len < BUF_SZ);
mu_assert("test_url_build failed", strncmp(expected, dst, BUF_SZ) == 0);
expected = "/vault/browse/hello/world";
dst_len = url_build(dst, BUF_SZ, "/vault/browse", "hello", "world",
NULL);
mu_assert("test_url_build failed", dst_len > 0 && dst_len < BUF_SZ);
mu_assert("test_url_build failed", strncmp(expected, dst, BUF_SZ) == 0);
expected = "/vault/browse";
dst_len = url_build(dst, BUF_SZ, "/vault/", "browse/", NULL);
mu_assert("test_url_build failed", dst_len > 0 && dst_len < BUF_SZ);
mu_assert("test_url_build failed", strncmp(expected, dst, BUF_SZ) == 0);
dst_len = url_build(dst, BUF_SZ, "", "", NULL);
mu_assert("test_url_build failed", dst_len == 0);
dst_len = url_build(dst, BUF_SZ, NULL);
mu_assert("test_url_build failed", dst_len == 0);
return 0;
}
static char *
all_tests()
{
mu_run_test(test_browse_invalid_traversal);
mu_run_test(test_browse_path_too_long);
mu_run_test(test_url_build);
mu_run_test(test_download);
mu_run_test(test_upload_post);
mu_run_test(test_delete_get);
mu_run_test(test_delete_post);
return 0;
}
int
main(void)
{
char *result = all_tests();
if (result != 0) {
printf("%s\n", result);
} else {
printf("ALL TESTS PASSED\n");
}
printf("Tests run: %d\n", tests_run);
return result != 0;
}
|