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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
|
#include "web.h"
#include "mytime.h"
#include <assert.h>
#include <curl/curl.h>
#include <stdlib.h>
#include <string.h>
#include <cjson/cJSON.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;
}
int main(int argc, char **argv) {
enable_fake_time();
// @DEP: The shared-cache mode is deprecated in sqlite, but it's still
// there and useful right now.
struct App app = {
.dbname = "file::memory:?cache=shared",
};
prepare_database(&app);
struct Server server = {
.app = &app,
};
web_begin(&server, 8080, "admin", "changeme");
curl_global_init(CURL_GLOBAL_ALL);
CURLcode curlRes;
CURL *curl = curl_easy_init();
assert(curl != NULL);
// Default page requires auth, should redirect
{
curlRes = curl_easy_setopt(curl, CURLOPT_URL, "http://localhost:8080/");
assert(curlRes == CURLE_OK);
curlRes = curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_to_memory);
assert(curlRes == CURLE_OK);
curlRes = curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 0L);
assert(curlRes == CURLE_OK);
struct memory body = {0};
curlRes = curl_easy_setopt(curl, CURLOPT_WRITEDATA, &body);
assert(curlRes == CURLE_OK);
curlRes = curl_easy_perform(curl);
assert(curlRes == CURLE_OK);
long code;
curlRes = curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &code);
assert(code == 302); // Redirect to login
}
char client_secret[CLIENT_SECRET_LEN] = {0};
{
curlRes = curl_easy_setopt(curl, CURLOPT_URL, "http://localhost:8080/api/client");
assert(curlRes == CURLE_OK);
curlRes = curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
assert(curlRes == CURLE_OK);
curlRes = curl_easy_setopt(curl, CURLOPT_POST, 1L);
assert(curlRes == CURLE_OK);
curlRes = curl_easy_setopt(curl, CURLOPT_READFUNCTION, read_from_memory);
assert(curlRes == CURLE_OK);
struct memory req_body = {
.body = "{\"name\": \"client1\"}",
.size = strlen(req_body.body),
};
curlRes = curl_easy_setopt(curl, CURLOPT_READDATA, &req_body);
assert(curlRes == CURLE_OK);
curlRes = curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_to_memory);
assert(curlRes == CURLE_OK);
struct memory body = {0};
curlRes = curl_easy_setopt(curl, CURLOPT_WRITEDATA, &body);
assert(curlRes == CURLE_OK);
curlRes = curl_easy_perform(curl);
assert(curlRes == CURLE_OK);
long code;
curlRes = curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &code);
assert(code == 200);
char *ct;
curlRes = curl_easy_getinfo(curl, CURLINFO_CONTENT_TYPE, &ct);
assert(curlRes == CURLE_OK);
assert(ct != NULL);
assert(strcmp(ct, "application/json") == 0);
assert(body.size >= 0);
// Parse response to extract secret
cJSON *rootJson = cJSON_ParseWithLength(body.body, body.size);
assert(rootJson != NULL);
cJSON *idJson = cJSON_GetObjectItemCaseSensitive(rootJson, "id");
assert(cJSON_IsString(idJson));
assert(strcmp(idJson->valuestring, "1") == 0);
cJSON *secretJson = cJSON_GetObjectItemCaseSensitive(rootJson, "secret");
assert(cJSON_IsString(secretJson));
assert(strlen(secretJson->valuestring) == 32);
strncpy(client_secret, secretJson->valuestring, CLIENT_SECRET_LEN);
cJSON_Delete(rootJson);
free(body.body);
body = (struct memory){0};
}
{
curlRes = curl_easy_setopt(curl, CURLOPT_URL, "http://localhost:8080/api/report");
assert(curlRes == CURLE_OK);
curlRes = curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
assert(curlRes == CURLE_OK);
curlRes = curl_easy_setopt(curl, CURLOPT_POST, 1L);
assert(curlRes == CURLE_OK);
curlRes = curl_easy_setopt(curl, CURLOPT_READFUNCTION, read_from_memory);
assert(curlRes == CURLE_OK);
char req_buf[256];
snprintf(req_buf, sizeof(req_buf), "{\"client\": \"client1\", \"secret\": \"%s\", \"event\": \"begin_backup\"}", client_secret);
struct memory req_body = {
.body = req_buf,
.size = strlen(req_buf),
};
curlRes = curl_easy_setopt(curl, CURLOPT_READDATA, &req_body);
assert(curlRes == CURLE_OK);
curlRes = curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_to_memory);
assert(curlRes == CURLE_OK);
struct memory body = {0};
curlRes = curl_easy_setopt(curl, CURLOPT_WRITEDATA, &body);
assert(curlRes == CURLE_OK);
curlRes = curl_easy_perform(curl);
assert(curlRes == CURLE_OK);
long code;
curlRes = curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &code);
assert(code == 204);
char *ct;
curlRes = curl_easy_getinfo(curl, CURLINFO_CONTENT_TYPE, &ct);
assert(curlRes == CURLE_OK);
assert(ct != NULL);
assert(strcmp(ct, "application/json") == 0);
assert(body.size == 0);
body = (struct memory){0};
}
progress_time(TIME_JOIN(0, 30, 0));
{
curlRes = curl_easy_setopt(curl, CURLOPT_URL, "http://localhost:8080/api/report");
assert(curlRes == CURLE_OK);
curlRes = curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
assert(curlRes == CURLE_OK);
curlRes = curl_easy_setopt(curl, CURLOPT_POST, 1L);
assert(curlRes == CURLE_OK);
curlRes = curl_easy_setopt(curl, CURLOPT_READFUNCTION, read_from_memory);
assert(curlRes == CURLE_OK);
char req_buf[256];
snprintf(req_buf, sizeof(req_buf), "{\"client\": \"client1\", \"secret\": \"%s\", \"event\": \"end_backup\"}", client_secret);
struct memory req_body = {
.body = req_buf,
.size = strlen(req_buf),
};
curlRes = curl_easy_setopt(curl, CURLOPT_READDATA, &req_body);
assert(curlRes == CURLE_OK);
curlRes = curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_to_memory);
assert(curlRes == CURLE_OK);
struct memory body = {0};
curlRes = curl_easy_setopt(curl, CURLOPT_WRITEDATA, &body);
assert(curlRes == CURLE_OK);
curlRes = curl_easy_perform(curl);
assert(curlRes == CURLE_OK);
long code;
curlRes = curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &code);
assert(code == 204);
char *ct;
curlRes = curl_easy_getinfo(curl, CURLINFO_CONTENT_TYPE, &ct);
assert(curlRes == CURLE_OK);
assert(ct != NULL);
assert(strcmp(ct, "application/json") == 0);
assert(body.size == 0);
body = (struct memory){0};
}
// Test: wrong secret should return 401
{
curlRes = curl_easy_setopt(curl, CURLOPT_URL, "http://localhost:8080/api/report");
assert(curlRes == CURLE_OK);
curlRes = curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
assert(curlRes == CURLE_OK);
curlRes = curl_easy_setopt(curl, CURLOPT_POST, 1L);
assert(curlRes == CURLE_OK);
curlRes = curl_easy_setopt(curl, CURLOPT_READFUNCTION, read_from_memory);
assert(curlRes == CURLE_OK);
struct memory req_body = {
.body = "{\"client\": \"client1\", \"secret\": \"wrongsecret\", \"event\": \"begin_backup\"}",
.size = strlen("{\"client\": \"client1\", \"secret\": \"wrongsecret\", \"event\": \"begin_backup\"}"),
};
curlRes = curl_easy_setopt(curl, CURLOPT_READDATA, &req_body);
assert(curlRes == CURLE_OK);
curlRes = curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_to_memory);
assert(curlRes == CURLE_OK);
struct memory body = {0};
curlRes = curl_easy_setopt(curl, CURLOPT_WRITEDATA, &body);
assert(curlRes == CURLE_OK);
curlRes = curl_easy_perform(curl);
assert(curlRes == CURLE_OK);
long code;
curlRes = curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &code);
assert(code == 401); // Unauthorized
}
// Test: Starting a new backup should interrupt the previous RUNNING backup
{
// Begin first backup
curlRes = curl_easy_setopt(curl, CURLOPT_URL, "http://localhost:8080/api/report");
assert(curlRes == CURLE_OK);
curlRes = curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
assert(curlRes == CURLE_OK);
curlRes = curl_easy_setopt(curl, CURLOPT_POST, 1L);
assert(curlRes == CURLE_OK);
curlRes = curl_easy_setopt(curl, CURLOPT_READFUNCTION, read_from_memory);
assert(curlRes == CURLE_OK);
char req_buf[256];
snprintf(req_buf, sizeof(req_buf),
"{\"client\": \"client1\", \"secret\": \"%s\", \"event\": \"begin_backup\"}",
client_secret);
struct memory req_body = { .body = req_buf, .size = strlen(req_buf) };
curlRes = curl_easy_setopt(curl, CURLOPT_READDATA, &req_body);
assert(curlRes == CURLE_OK);
curlRes = curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_to_memory);
assert(curlRes == CURLE_OK);
struct memory body = {0};
curlRes = curl_easy_setopt(curl, CURLOPT_WRITEDATA, &body);
assert(curlRes == CURLE_OK);
curlRes = curl_easy_perform(curl);
assert(curlRes == CURLE_OK);
long code;
curlRes = curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &code);
assert(code == 204);
body = (struct memory){0};
progress_time(TIME_JOIN(0, 10, 0)); // Advance 10 minutes
// Begin second backup (should interrupt the first)
snprintf(req_buf, sizeof(req_buf),
"{\"client\": \"client1\", \"secret\": \"%s\", \"event\": \"begin_backup\"}",
client_secret);
req_body = (struct memory){ .body = req_buf, .size = strlen(req_buf) };
curlRes = curl_easy_setopt(curl, CURLOPT_READDATA, &req_body);
assert(curlRes == CURLE_OK);
body = (struct memory){0};
curlRes = curl_easy_setopt(curl, CURLOPT_WRITEDATA, &body);
assert(curlRes == CURLE_OK);
curlRes = curl_easy_perform(curl);
assert(curlRes == CURLE_OK);
curlRes = curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &code);
assert(code == 204);
body = (struct memory){0};
// Use internal function to verify backup statuses
struct Request req = { .server = &server };
struct GetClientOnlyResult client_result;
int ret = get_client(&req, 1, &client_result);
assert(ret == 0);
assert(client_result.found == 1);
struct GetBackupsResult *backups_result = malloc(sizeof(struct GetBackupsResult) + sizeof(struct BackupRecord) * MAX_BACKUPS_DISPLAY);
backups_result->backups_len = MAX_BACKUPS_DISPLAY;
ret = get_client_backups(&req, 1, backups_result);
assert(ret == 0);
// Should have at least 2 backups (the interrupted one and the running one)
// Note: There may be more from earlier tests
assert(backups_result->backups_len >= 2);
// Backups are ordered by started DESC, so newest first
// The most recent backup should be RUNNING
assert(strcmp(backups_result->backups[0].status, "RUNNING") == 0);
// The previous backup should be INTERRUPTED
assert(strcmp(backups_result->backups[1].status, "INTERRUPTED") == 0);
// The interrupted backup should have a completed timestamp
assert(backups_result->backups[1].completed.tv_sec > 0);
free(backups_result);
}
// Test: Full login flow
{
// Use fresh curl with cookie jar
CURL *auth_curl = curl_easy_init();
assert(auth_curl != NULL);
curl_easy_setopt(auth_curl, CURLOPT_COOKIEFILE, ""); // Enable cookie engine
curl_easy_setopt(auth_curl, CURLOPT_FOLLOWLOCATION, 0L);
curl_easy_setopt(auth_curl, CURLOPT_WRITEFUNCTION, write_to_memory);
struct memory body = {0};
curl_easy_setopt(auth_curl, CURLOPT_WRITEDATA, &body);
// Step 1: Unauthenticated access redirects to /login
curl_easy_setopt(auth_curl, CURLOPT_URL, "http://localhost:8080/clients");
curl_easy_perform(auth_curl);
long code;
curl_easy_getinfo(auth_curl, CURLINFO_RESPONSE_CODE, &code);
assert(code == 302);
// Step 2: Wrong credentials show error (not redirect)
free(body.body); body = (struct memory){0};
curl_easy_setopt(auth_curl, CURLOPT_URL, "http://localhost:8080/login");
curl_easy_setopt(auth_curl, CURLOPT_POSTFIELDS, "username=admin&password=wrong");
curl_easy_perform(auth_curl);
curl_easy_getinfo(auth_curl, CURLINFO_RESPONSE_CODE, &code);
assert(code == 200);
assert(strstr(body.body, "Invalid") != NULL);
// Step 3: Correct credentials redirect and set cookie
free(body.body); body = (struct memory){0};
curl_easy_setopt(auth_curl, CURLOPT_POSTFIELDS, "username=admin&password=changeme");
curl_easy_perform(auth_curl);
curl_easy_getinfo(auth_curl, CURLINFO_RESPONSE_CODE, &code);
assert(code == 303);
// Step 4: Authenticated access works
free(body.body); body = (struct memory){0};
curl_easy_setopt(auth_curl, CURLOPT_HTTPGET, 1L);
curl_easy_setopt(auth_curl, CURLOPT_URL, "http://localhost:8080/clients");
curl_easy_perform(auth_curl);
curl_easy_getinfo(auth_curl, CURLINFO_RESPONSE_CODE, &code);
assert(code == 200);
assert(strstr(body.body, "Clients") != NULL);
// Step 5: Logout clears cookie and redirects
free(body.body); body = (struct memory){0};
curl_easy_setopt(auth_curl, CURLOPT_URL, "http://localhost:8080/logout");
curl_easy_setopt(auth_curl, CURLOPT_POST, 1L);
curl_easy_setopt(auth_curl, CURLOPT_POSTFIELDS, "");
curl_easy_perform(auth_curl);
curl_easy_getinfo(auth_curl, CURLINFO_RESPONSE_CODE, &code);
assert(code == 303); // Redirect to login
// Step 6: No longer authenticated after logout
free(body.body); body = (struct memory){0};
curl_easy_setopt(auth_curl, CURLOPT_HTTPGET, 1L);
curl_easy_setopt(auth_curl, CURLOPT_URL, "http://localhost:8080/clients");
curl_easy_perform(auth_curl);
curl_easy_getinfo(auth_curl, CURLINFO_RESPONSE_CODE, &code);
assert(code == 302); // Redirect to login (not authenticated)
free(body.body);
curl_easy_cleanup(auth_curl);
}
// Test: Invalid session cookie rejected
{
CURL *bad_curl = curl_easy_init();
assert(bad_curl != NULL);
curl_easy_setopt(bad_curl, CURLOPT_FOLLOWLOCATION, 0L);
curl_easy_setopt(bad_curl, CURLOPT_COOKIE, "session=invalidtoken");
curl_easy_setopt(bad_curl, CURLOPT_URL, "http://localhost:8080/clients");
curl_easy_setopt(bad_curl, CURLOPT_WRITEFUNCTION, write_to_memory);
struct memory body = {0};
curl_easy_setopt(bad_curl, CURLOPT_WRITEDATA, &body);
curl_easy_perform(bad_curl);
long code;
curl_easy_getinfo(bad_curl, CURLINFO_RESPONSE_CODE, &code);
assert(code == 302); // Redirect to login
free(body.body);
curl_easy_cleanup(bad_curl);
}
web_join(&server);
}
|