-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtests.c
167 lines (150 loc) · 6.04 KB
/
tests.c
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <sys/random.h>
#include "purr.h"
#include "mmap_file.h"
#include "gemini.h"
int rv = 0;
static void compare_strings(const char *expected, const char *result, const char *function)
{
printf("%s(): ", function);
if (result == NULL || strcmp(expected, result)) {
rv += 1;
puts("failure");
printf(" expected: %s\n got: %s\n", expected, result);
} else {
puts("success");
}
}
static void compare_arrays(const uint8_t *expected, const uint8_t *result, size_t len, const char *function)
{
printf("%s(): ", function);
if (memcmp(expected, result, len)) {
rv += 1;
puts("failure");
printf(" expected: %s\n got: %s\n", print_hex(expected, len, false), print_hex(result, len, false));
} else {
puts("success");
}
}
int main()
{
{
/* formats.c */
uint8_t buf[] = {0x12, 0x02, 0x12, 0x4c, 0xa8};
const char *expected = "1202124ca8";
const char *result = print_hex(buf, sizeof buf, false);
compare_strings(expected, result, "print_hex");
const char *hex = "0403124574";
uint8_t buf_expected[] = {0x04, 0x03, 0x12, 0x45, 0x74};
uint8_t buf_result[sizeof buf_expected];
int err = decode_hex(hex, buf_result, sizeof buf_result);
compare_arrays(buf_expected, buf_result, sizeof buf_result, "decode_hex");
assert(err == 0);
}
{
/* urls.c */
const char *dirty = "https://hello.com/ash";
//char scheme[4096], clean[4096], path[4096], port[16];
char *scheme = NULL, *clean = NULL, *path = NULL, *port = NULL;
int portn = clean_up_link(dirty, &scheme, &clean, &path, &port);
compare_strings("https://", scheme, "clean_up_link");
compare_strings("hello.com", clean, "clean_up_link");
compare_strings("/ash", path, "clean_up_link");
compare_strings("443", port, "clean_up_link");
assert(portn == HTTPS_PORT);
free(scheme); scheme = NULL;
free(clean); clean = NULL;
free(path); path = NULL;
free(port); port = NULL;
dirty = "http://hello.com";
portn = clean_up_link(dirty, &scheme, &clean, &path, &port);
compare_strings("http://", scheme, "clean_up_link");
compare_strings("hello.com", clean, "clean_up_link");
compare_strings("/", path, "clean_up_link");
compare_strings("80", port, "clean_up_link");
assert(portn == HTTP_PORT);
free(scheme); scheme = NULL;
free(clean); clean = NULL;
free(path); path = NULL;
free(port); port = NULL;
dirty = "hello.com";
portn = clean_up_link(dirty, &scheme, &clean, &path, &port);
compare_strings("http://", scheme, "clean_up_link");
compare_strings("hello.com", clean, "clean_up_link");
compare_strings("/", path, "clean_up_link");
compare_strings("80", port, "clean_up_link");
assert(portn == HTTP_PORT);
free(scheme); scheme = NULL;
free(clean); clean = NULL;
free(path); path = NULL;
free(port); port = NULL;
dirty = "https://bsd.ac/paste.html#sieqaqk_73fe_df51";
portn = clean_up_link(dirty, &scheme, &clean, &path, &port);
compare_strings("https://", scheme, "clean_up_link");
compare_strings("bsd.ac", clean, "clean_up_link");
compare_strings("/paste.html#sieqaqk_73fe_df51", path, "clean_up_link");
compare_strings("443", port, "clean_up_link");
assert(portn == HTTPS_PORT);
uint8_t key_exc[KEY_LEN] = {0x73, 0xfe};
uint8_t iv_exc[IV_LEN] = {0xdf, 0x51};
uint8_t *key, *iv;
int err = get_encryption_params(path, &key, &iv);
compare_strings("/sieqaqk", path, "get_encryption_params");
compare_arrays(key_exc, key, KEY_LEN, "get_encryption_params");
compare_arrays(iv_exc, iv, IV_LEN, "get_encryption_params");
assert(err == 0);
}
{
/* mmap_file.c */
int write_size = 1024 * 1024;
uint8_t *tmp = calloc(write_size, 4);
struct mmap_file f = {.size = 2 * write_size, .prot = PROT_MEM, .flags = MAP_MEM};
assert(allocate_mmap(&f));
uint8_t *data = malloc(write_size);
getrandom(data, write_size, 0);
assert(data);
int written = write_into_mmap(&f, data, write_size);
assert(f.offset == written);
RESET_MMAP(f);
read_from_mmap(&f, tmp, write_size);
compare_arrays(data, f.data, write_size, "{write_into,read_from}_mmap");
}
{
/* gemini.c */
const char *gmi_file = "=> aha/ Aha";
struct gemini_link_node *head = NULL;
int n = get_links_from_gmi(gmi_file, &head);
compare_strings("aha/", head->path, "get_links_from_gmi");
compare_strings("Aha", head->name, "get_links_from_gmi");
assert(n == 1);
gmi_file = "=> two-a Two spaces\r\n=> three\tThree";
head = NULL;
n = get_links_from_gmi(gmi_file, &head);
compare_strings("two-a", head->path, "get_links_from_gmi");
compare_strings("Two spaces", head->name, "get_links_from_gmi");
compare_strings("three", head->next->path, "get_links_from_gmi");
compare_strings("Three", head->next->name, "get_links_from_gmi");
assert(n == 2);
const char *root = "/hello";
const char *add = "piper";
char *path = walk_gemini_path(root, add);
compare_strings("/hello/piper", path, "walk_gemini_path");
root = "/hello/";
add = "/piper/hi/../../a";
path = walk_gemini_path(root, add);
compare_strings("/hello/a", path, "walk_gemini_path");
root = "/hello/";
add = "/piper/hi/";
path = walk_gemini_path(root, add);
compare_strings("/hello/piper/hi/", path, "walk_gemini_path");
root = "/hello";
add = "../../../../..";
path = walk_gemini_path(root, add);
compare_strings("/", path, "walk_gemini_path");
}
printf("Total errors: %d\n", rv);
return rv;
}