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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
|
#include "log.h"
#include "unity.h"
#include "api.h"
#include "base64.h"
#include <assert.h>
#include <curl/curl.h>
#include <stdlib.h>
#include <string.h>
// @PASTE Stolen from libcurl documentation
struct memory {
char *body;
size_t size;
};
static size_t write_to_memory(char *data, size_t size, size_t nmemb, void *clientp) {
size_t realsize = size * nmemb;
struct memory *mem = (struct memory *)clientp;
char *ptr = realloc(mem->body, mem->size + realsize + 1);
if(!ptr) return 0; /* out of memory */
mem->body = ptr;
memcpy(&(mem->body[mem->size]), data, realsize);
mem->size += realsize;
mem->body[mem->size] = 0;
return realsize;
}
static size_t read_from_memory(char *data, size_t size, size_t nmemb, void *clientp) {
size_t realsize = size * nmemb;
struct memory *mem = (struct memory *)clientp;
realsize = realsize > mem->size ? mem->size : realsize;
memcpy(data, mem->body, realsize);
mem->size -= realsize;
mem->body += realsize;
return realsize;
}
void test_base64_decode() {
size_t res = 0;
uint8_t buf[256];
res = base64_decode((unsigned char*)"AAAA", 4, buf, 256);
TEST_ASSERT_EQUAL(3, res);
TEST_ASSERT_EQUAL_MEMORY("\0\0\0", buf, 3);
res = base64_decode((unsigned char*)"AA==", 4, buf, 256);
TEST_ASSERT_EQUAL(1, res);
TEST_ASSERT_EQUAL_MEMORY("\0", buf, 1);
res = base64_decode((unsigned char*)"MQ==", 4, buf, 256);
TEST_ASSERT_EQUAL(1, res);
TEST_ASSERT_EQUAL_MEMORY("1", buf, 1);
res = base64_decode((unsigned char*)"AAA=", 4, buf, 256);
TEST_ASSERT_EQUAL(2, res);
TEST_ASSERT_EQUAL_MEMORY("\0\0", buf, 2);
// Too short of an input string
res = base64_decode((unsigned char*)"AA=", 3, buf, 256);
TEST_ASSERT_EQUAL(-1, res);
// Too little space in the output buffer
res = base64_decode((unsigned char*)"AAA=", 4, buf, 1);
TEST_ASSERT_EQUAL(-1, res);
// Too little space in the output buffer
res = base64_decode((unsigned char*)"dGVzdA==", 8, buf, 256);
TEST_ASSERT_EQUAL(4, res);
TEST_ASSERT_EQUAL_MEMORY("test", buf, 4);
// A null byte in the middle of the input
res = base64_decode((unsigned char*)"AA\0=", 4, buf, 256);
TEST_ASSERT_EQUAL(-1, res);
// It should stop at the first equals
res = base64_decode_incr((unsigned char*)"AA==W", 5, buf, 256);
TEST_ASSERT_EQUAL(1, res);
TEST_ASSERT_EQUAL_MEMORY("\0", buf, 1);
res = base64_decode_incr((unsigned char*)"AAAA\"", 5, buf, 256);
TEST_ASSERT_EQUAL(3, res);
TEST_ASSERT_EQUAL_MEMORY("\0\0\0", buf, 3);
res = base64_decode_incr((unsigned char*)"AAAA}", 5, buf, 256);
TEST_ASSERT_EQUAL(3, res);
TEST_ASSERT_EQUAL_MEMORY("\0\0\0", buf, 3);
// We are a little overpermissive when it comes to the padding equals. This
// isn't technically valid, but due to some implementation details we still
// accept it. I think that's fine
res = base64_decode_incr((unsigned char*)"AA=W", 4, buf, 256);
TEST_ASSERT_EQUAL(1, res);
TEST_ASSERT_EQUAL_MEMORY("\0", buf, 1);
}
void test_root_get() {
CURLcode curlRes;
CURL *curl = curl_easy_init();
TEST_ASSERT_NOT_NULL(curl);
struct dht dht = {0};
dht.self = (struct nodeid){.inner={0x42424242, 0x42424242, 0x42424242, 0x42424242, 0x42424242}};
api_init(&dht);
curlRes = curl_easy_setopt(curl, CURLOPT_URL, "http://localhost:6982/");
TEST_ASSERT_EQUAL(CURLE_OK, curlRes);
curlRes = curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_to_memory);
TEST_ASSERT_EQUAL(CURLE_OK, curlRes);
struct memory body = {0};
curlRes = curl_easy_setopt(curl, CURLOPT_WRITEDATA, &body);
TEST_ASSERT_EQUAL(CURLE_OK, curlRes);
curlRes = curl_easy_perform(curl);
TEST_ASSERT_EQUAL(CURLE_OK, curlRes);
char *ct;
curlRes = curl_easy_getinfo(curl, CURLINFO_CONTENT_TYPE, &ct);
TEST_ASSERT_EQUAL(CURLE_OK, curlRes);
TEST_ASSERT_EQUAL_STRING("application/json", ct);
TEST_ASSERT_EQUAL_STRING("{ \"id\": \"QkJCQkJCQkJCQkJCQkJCQkJCQkI=\" }", body.body);
body = (struct memory){0};
free(body.body);
curl_easy_cleanup(curl);
api_end();
}
void test_lookup_get() {
curl_global_init(CURL_GLOBAL_ALL);
CURLcode curlRes;
CURL *curl = curl_easy_init();
TEST_ASSERT_NOT_NULL(curl);
struct dht dht = {0};
dht.self = (struct nodeid){.inner={0x42424242, 0x42424242, 0x42424242, 0x42424242, 0x42424242}};
api_init(&dht);
curlRes = curl_easy_setopt(curl, CURLOPT_URL, "http://localhost:6982/lookup");
TEST_ASSERT_EQUAL(CURLE_OK, curlRes);
curlRes = curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_to_memory);
TEST_ASSERT_EQUAL(CURLE_OK, curlRes);
struct memory body = {0};
curlRes = curl_easy_setopt(curl, CURLOPT_WRITEDATA, &body);
TEST_ASSERT_EQUAL(CURLE_OK, curlRes);
curlRes = curl_easy_perform(curl);
TEST_ASSERT_EQUAL(CURLE_OK, curlRes);
char *ct;
curlRes = curl_easy_getinfo(curl, CURLINFO_CONTENT_TYPE, &ct);
TEST_ASSERT_EQUAL(CURLE_OK, curlRes);
TEST_ASSERT_EQUAL_STRING("application/json", ct);
TEST_ASSERT_EQUAL_STRING(
"{ "
"\"state\": \"empty\" "
"}",
body.body
);
body = (struct memory){0};
{
curlRes = curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
TEST_ASSERT_EQUAL(CURLE_OK, curlRes);
curlRes = curl_easy_setopt(curl, CURLOPT_READFUNCTION, read_from_memory);
TEST_ASSERT_EQUAL(CURLE_OK, curlRes);
struct memory req_body = {
.body = "{\"target\": \"BAAAAAAAAAAAAAAAAAAAAAAAAAA=\", \"state\": \"pending\"}",
.size = strlen(req_body.body),
};
curlRes = curl_easy_setopt(curl, CURLOPT_READDATA, &req_body);
TEST_ASSERT_EQUAL(CURLE_OK, curlRes);
curlRes = curl_easy_perform(curl);
TEST_ASSERT_EQUAL(CURLE_OK, curlRes);
curlRes = curl_easy_getinfo(curl, CURLINFO_CONTENT_TYPE, &ct);
TEST_ASSERT_EQUAL(CURLE_OK, curlRes);
TEST_ASSERT_EQUAL_STRING("application/json", ct);
TEST_ASSERT_EQUAL_STRING(
"{ "
"\"state\": \"pending\", "
"\"outstanding\": 0, "
"\"target\": \"BAAAAAAAAAAAAAAAAAAAAAAAAAA=\" "
"}",
body.body
);
body = (struct memory){0};
}
// The protocol does whatever and complete the lookup
dht.lookup.state = OP_COMPLETED;
{
curlRes = curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
TEST_ASSERT_EQUAL(CURLE_OK, curlRes);
curlRes = curl_easy_setopt(curl, CURLOPT_READFUNCTION, read_from_memory);
TEST_ASSERT_EQUAL(CURLE_OK, curlRes);
struct memory req_body = {
.body = "{\"state\": \"empty\"}",
.size = strlen(req_body.body),
};
curlRes = curl_easy_setopt(curl, CURLOPT_READDATA, &req_body);
TEST_ASSERT_EQUAL(CURLE_OK, curlRes);
curlRes = curl_easy_perform(curl);
TEST_ASSERT_EQUAL(CURLE_OK, curlRes);
curlRes = curl_easy_getinfo(curl, CURLINFO_CONTENT_TYPE, &ct);
TEST_ASSERT_EQUAL(CURLE_OK, curlRes);
TEST_ASSERT_EQUAL_STRING("application/json", ct);
TEST_ASSERT_EQUAL_STRING(
"{ "
"\"state\": \"empty\" "
"}",
body.body
);
body = (struct memory){0};
}
free(body.body);
curl_easy_cleanup(curl);
curl_global_cleanup();
api_end();
}
|