1
+// SPDX-License-Identifier: GPL-3.0-or-later
2
+
3
+#include "../../../libnetdata/libnetdata.h"
4
+#include "../../../libnetdata/required_dummies.h"
5
+#include "../../../database/rrd.h"
6
+#include "../../../web/server/web_client.h"
7
+#include <setjmp.h>
8
+#include <cmocka.h>
9
+#include <stdbool.h>
10
+
11
+void repr(char *result, int result_size, char const *buf, int size)
12
+{
13
+ int n;
14
+ char *end = result + result_size - 1;
15
+ unsigned char const *ubuf = (unsigned char const *)buf;
16
+ while (size && result_size > 0) {
17
+ if (*ubuf <= 0x20 || *ubuf >= 0x80) {
18
+ n = snprintf(result, result_size, "\\%02X", *ubuf);
19
+ } else {
20
+ *result = *ubuf;
21
+ n = 1;
22
+ }
23
+ result += n;
24
+ result_size -= n;
25
+ ubuf++;
26
+ size--;
27
+ }
28
+ if (result_size > 0)
29
+ *(result++) = 0;
30
+ else
31
+ *end = 0;
32
+}
33
+
34
+// ---------------------------------- Mocking accesses from web_client ------------------------------------------------
35
+
36
+ssize_t send(int sockfd, const void *buf, size_t len, int flags)
37
+{
38
+ info("Mocking send: %zu bytes\n", len);
39
+ (void)sockfd;
40
+ (void)buf;
41
+ (void)flags;
42
+ return len;
43
+}
44
+
45
+RRDHOST *__wrap_rrdhost_find_by_hostname(const char *hostname, uint32_t hash)
46
+{
47
+ (void)hostname;
48
+ (void)hash;
49
+ return NULL;
50
+}
51
+
52
+/* Note: we've got some intricate code inside the global statistics module, might be useful to pull it inside the
53
+ test set instead of mocking it. */
54
+void __wrap_finished_web_request_statistics(
55
+ uint64_t dt, uint64_t bytes_received, uint64_t bytes_sent, uint64_t content_size, uint64_t compressed_content_size)
56
+{
57
+ (void)dt;
58
+ (void)bytes_received;
59
+ (void)bytes_sent;
60
+ (void)content_size;
61
+ (void)compressed_content_size;
62
+}
63
+
64
+char *__wrap_config_get(struct config *root, const char *section, const char *name, const char *default_value)
65
+{
66
+ if (!strcmp(section, CONFIG_SECTION_WEB) && !strcmp(name, "web files owner"))
67
+ return "netdata";
68
+ (void)root;
69
+ (void)default_value;
70
+ return "UNKNOWN FIX ME";
71
+}
72
+
73
+int __wrap_web_client_api_request_v1(RRDHOST *host, struct web_client *w, char *url)
74
+{
75
+ char url_repr[160];
76
+ repr(url_repr, sizeof(url_repr), url, strlen(url));
77
+ printf("web_client_api_request_v1(url=\"%s\")\n", url_repr);
78
+ check_expected_ptr(host);
79
+ check_expected_ptr(w);
80
+ check_expected_ptr(url_repr);
81
+ return HTTP_RESP_OK;
82
+}
83
+
84
+int __wrap_mysendfile(struct web_client *w, char *filename)
85
+{
86
+ (void)w;
87
+ printf("mysendfile(filename=\"%s\"\n", filename);
88
+ check_expected_ptr(filename);
89
+ return HTTP_RESP_OK;
90
+}
91
+
92
+int __wrap_rrdpush_receiver_thread_spawn(RRDHOST *host, struct web_client *w, char *url)
93
+{
94
+ (void)host;
95
+ (void)w;
96
+ (void)url;
97
+ return 0;
98
+}
99
+
100
+RRDHOST *__wrap_rrdhost_find_by_guid(const char *guid, uint32_t hash)
101
+{
102
+ (void)guid;
103
+ (void)hash;
104
+ printf("FIXME: rrdset_find_guid\n");
105
+ return NULL;
106
+}
107
+
108
+RRDSET *__wrap_rrdset_find_byname(RRDHOST *host, const char *name)
109
+{
110
+ (void)host;
111
+ (void)name;
112
+ printf("FIXME: rrdset_find_byname\n");
113
+ return NULL;
114
+}
115
+
116
+RRDSET *__wrap_rrdset_find(RRDHOST *host, const char *id)
117
+{
118
+ (void)host;
119
+ (void)id;
120
+ printf("FIXME: rrdset_find\n");
121
+ return NULL;
122
+}
123
+
124
+// -------------------------------- Mocking the log - dump straight through --------------------------------------------
125
+
126
+void __wrap_debug_int(const char *file, const char *function, const unsigned long line, const char *fmt, ...)
127
+{
128
+ (void)file;
129
+ (void)function;
130
+ (void)line;
131
+ va_list args;
132
+ va_start(args, fmt);
133
+ printf(" DEBUG: ");
134
+ printf(fmt, args);
135
+ printf("\n");
136
+ va_end(args);
137
+}
138
+
139
+void __wrap_info_int(const char *file, const char *function, const unsigned long line, const char *fmt, ...)
140
+{
141
+ (void)file;
142
+ (void)function;
143
+ (void)line;
144
+ va_list args;
145
+ va_start(args, fmt);
146
+ printf(" INFO: ");
147
+ printf(fmt, args);
148
+ printf("\n");
149
+ va_end(args);
150
+}
151
+
152
+void __wrap_error_int(
153
+ const char *prefix, const char *file, const char *function, const unsigned long line, const char *fmt, ...)
154
+{
155
+ (void)prefix;
156
+ (void)file;
157
+ (void)function;
158
+ (void)line;
159
+ va_list args;
160
+ va_start(args, fmt);
161
+ printf(" ERROR: ");
162
+ printf(fmt, args);
163
+ printf("\n");
164
+ va_end(args);
165
+}
166
+
167
+void __wrap_fatal_int(const char *file, const char *function, const unsigned long line, const char *fmt, ...)
168
+{
169
+ (void)file;
170
+ (void)function;
171
+ (void)line;
172
+ va_list args;
173
+ va_start(args, fmt);
174
+ printf("FATAL: ");
175
+ printf(fmt, args);
176
+ printf("\n");
177
+ va_end(args);
178
+ fail();
179
+}
180
+
181
+WEB_SERVER_MODE web_server_mode = WEB_SERVER_MODE_STATIC_THREADED;
182
+char *netdata_configured_web_dir = "UNKNOWN FIXME";
183
+RRDHOST *localhost = NULL;
184
+
185
+struct config netdata_config = { .sections = NULL,
186
+ .mutex = NETDATA_MUTEX_INITIALIZER,
187
+ .index = { .avl_tree = { .root = NULL, .compar = appconfig_section_compare },
188
+ .rwlock = AVL_LOCK_INITIALIZER } };
189
+
190
+/* Note: this is not a CMocka group_test_setup/teardown pair. This is performed per-test.
191
+*/
192
+static struct web_client *setup_fresh_web_client()
193
+{
194
+ struct web_client *w = (struct web_client *)malloc(sizeof(struct web_client));
195
+ memset(w, 0, sizeof(struct web_client));
196
+ w->response.data = buffer_create(NETDATA_WEB_RESPONSE_INITIAL_SIZE);
197
+ w->response.header = buffer_create(NETDATA_WEB_RESPONSE_HEADER_SIZE);
198
+ w->response.header_output = buffer_create(NETDATA_WEB_RESPONSE_HEADER_SIZE);
199
+ strcpy(w->origin, "*"); // Simulate web_client_create_on_fd()
200
+ w->cookie1[0] = 0; // Simulate web_client_create_on_fd()
201
+ w->cookie2[0] = 0; // Simulate web_client_create_on_fd()
202
+ w->acl = 0x1f; // Everything on
203
+ return w;
204
+}
205
+
206
+static void destroy_web_client(struct web_client *w)
207
+{
208
+ buffer_free(w->response.data);
209
+ buffer_free(w->response.header);
210
+ buffer_free(w->response.header_output);
211
+ free(w);
212
+}
213
+
214
+//////////////////////////// Test cases ///////////////////////////////////////////////////////////////////////////////
215
+
216
+static void only_root(void **state)
217
+{
218
+ (void)state;
219
+
220
+ if (localhost != NULL)
221
+ free(localhost);
222
+ localhost = malloc(sizeof(RRDHOST));
223
+
224
+ struct web_client *w = setup_fresh_web_client();
225
+ buffer_strcat(w->response.data, "GET / HTTP/1.1\r\n\r\n");
226
+
227
+ char debug[4096];
228
+ repr(debug, sizeof(debug), w->response.data->buffer, w->response.data->len);
229
+ printf("-> \"%s\"\n", debug);
230
+
231
+ //char expected_url_repr[4096];
232
+ //repr(expected_url_repr, sizeof(expected_url_repr), def->url_out_repr, strlen(def->url_out_repr));
233
+
234
+ expect_string(__wrap_mysendfile, filename, "/");
235
+
236
+ web_client_process_request(w);
237
+
238
+ //assert_string_equal(w->decoded_query_string, def->query_out);
239
+ destroy_web_client(w);
240
+ free(localhost);
241
+ localhost = NULL;
242
+}
243
+
244
+static void two_slashes(void **state)
245
+{
246
+ (void)state;
247
+
248
+ if (localhost != NULL)
249
+ free(localhost);
250
+ localhost = malloc(sizeof(RRDHOST));
251
+
252
+ struct web_client *w = setup_fresh_web_client();
253
+ buffer_strcat(w->response.data, "GET // HTTP/1.1\r\n\r\n");
254
+
255
+ char debug[4096];
256
+ repr(debug, sizeof(debug), w->response.data->buffer, w->response.data->len);
257
+ printf("-> \"%s\"\n", debug);
258
+
259
+ //char expected_url_repr[4096];
260
+ //repr(expected_url_repr, sizeof(expected_url_repr), def->url_out_repr, strlen(def->url_out_repr));
261
+
262
+ expect_string(__wrap_mysendfile, filename, "//");
263
+
264
+ web_client_process_request(w);
265
+
266
+ //assert_string_equal(w->decoded_query_string, def->query_out);
267
+ destroy_web_client(w);
268
+ free(localhost);
269
+ localhost = NULL;
270
+}
271
+
272
+static void absolute_url(void **state)
273
+{
274
+ (void)state;
275
+
276
+ if (localhost != NULL)
277
+ free(localhost);
278
+ localhost = malloc(sizeof(RRDHOST));
279
+
280
+ struct web_client *w = setup_fresh_web_client();
281
+ buffer_strcat(w->response.data, "GET http://localhost:19999/api/v1/info HTTP/1.1\r\n\r\n");
282
+
283
+ char debug[4096];
284
+ repr(debug, sizeof(debug), w->response.data->buffer, w->response.data->len);
285
+ printf("-> \"%s\"\n", debug);
286
+
287
+ //char expected_url_repr[4096];
288
+ //repr(expected_url_repr, sizeof(expected_url_repr), def->url_out_repr, strlen(def->url_out_repr));
289
+
290
+ expect_value(__wrap_web_client_api_request_v1, host, localhost);
291
+ expect_value(__wrap_web_client_api_request_v1, w, w);
292
+ expect_string(__wrap_web_client_api_request_v1, url_repr, "info");
293
+
294
+ web_client_process_request(w);
295
+
296
+ assert_string_equal(w->decoded_query_string, "?blah");
297
+ destroy_web_client(w);
298
+ free(localhost);
299
+ localhost = NULL;
300
+}
301
+
302
+static void valid_url(void **state)
303
+{
304
+ (void)state;
305
+
306
+ if (localhost != NULL)
307
+ free(localhost);
308
+ localhost = malloc(sizeof(RRDHOST));
309
+
310
+ struct web_client *w = setup_fresh_web_client();
311
+ buffer_strcat(w->response.data, "GET /api/v1/info?blah HTTP/1.1\r\n\r\n");
312
+
313
+ char debug[4096];
314
+ repr(debug, sizeof(debug), w->response.data->buffer, w->response.data->len);
315
+ printf("-> \"%s\"\n", debug);
316
+
317
+ //char expected_url_repr[4096];
318
+ //repr(expected_url_repr, sizeof(expected_url_repr), def->url_out_repr, strlen(def->url_out_repr));
319
+
320
+ expect_value(__wrap_web_client_api_request_v1, host, localhost);
321
+ expect_value(__wrap_web_client_api_request_v1, w, w);
322
+ expect_string(__wrap_web_client_api_request_v1, url_repr, "info");
323
+
324
+ web_client_process_request(w);
325
+
326
+ assert_string_equal(w->decoded_query_string, "?blah");
327
+ destroy_web_client(w);
328
+ free(localhost);
329
+ localhost = NULL;
330
+}
331
+
332
+/* RFC2616, section 4.1:
333
+
334
+ In the interest of robustness, servers SHOULD ignore any empty
335
+ line(s) received where a Request-Line is expected. In other words, if
336
+ the server is reading the protocol stream at the beginning of a
337
+ message and receives a CRLF first, it should ignore the CRLF.
338
+*/
339
+static void leading_blanks(void **state)
340
+{
341
+ (void)state;
342
+
343
+ if (localhost != NULL)
344
+ free(localhost);
345
+ localhost = malloc(sizeof(RRDHOST));
346
+
347
+ struct web_client *w = setup_fresh_web_client();
348
+ buffer_strcat(w->response.data, "\r\n\r\nGET /api/v1/info?blah HTTP/1.1\r\n\r\n");
349
+
350
+ char debug[4096];
351
+ repr(debug, sizeof(debug), w->response.data->buffer, w->response.data->len);
352
+ printf("-> \"%s\"\n", debug);
353
+
354
+ //char expected_url_repr[4096];
355
+ //repr(expected_url_repr, sizeof(expected_url_repr), def->url_out_repr, strlen(def->url_out_repr));
356
+
357
+ expect_value(__wrap_web_client_api_request_v1, host, localhost);
358
+ expect_value(__wrap_web_client_api_request_v1, w, w);
359
+ expect_string(__wrap_web_client_api_request_v1, url_repr, "info");
360
+
361
+ web_client_process_request(w);
362
+
363
+ assert_string_equal(w->decoded_query_string, "?blah");
364
+ destroy_web_client(w);
365
+ free(localhost);
366
+ localhost = NULL;
367
+}
368
+
369
+static void empty_url(void **state)
370
+{
371
+ (void)state;
372
+
373
+ if (localhost != NULL)
374
+ free(localhost);
375
+ localhost = malloc(sizeof(RRDHOST));
376
+
377
+ struct web_client *w = setup_fresh_web_client();
378
+ buffer_strcat(w->response.data, "GET HTTP/1.1\r\n\r\n");
379
+
380
+ char debug[4096];
381
+ repr(debug, sizeof(debug), w->response.data->buffer, w->response.data->len);
382
+ printf("-> \"%s\"\n", debug);
383
+
384
+ //char expected_url_repr[4096];
385
+ //repr(expected_url_repr, sizeof(expected_url_repr), def->url_out_repr, strlen(def->url_out_repr));
386
+
387
+ expect_value(__wrap_web_client_api_request_v1, host, localhost);
388
+ expect_value(__wrap_web_client_api_request_v1, w, w);
389
+ expect_string(__wrap_web_client_api_request_v1, url_repr, "info");
390
+
391
+ web_client_process_request(w);
392
+
393
+ assert_string_equal(w->decoded_query_string, "?blah");
394
+ destroy_web_client(w);
395
+ free(localhost);
396
+ localhost = NULL;
397
+}
398
+
399
+/* If the %-escape is being performed at the correct time then the url should not be treated as a query, but instead
400
+ as a path "/api/v1/info?blah?" which should despatch into the API with the given values.
401
+*/
402
+static void not_a_query(void **state)
403
+{
404
+ (void)state;
405
+ localhost = malloc(sizeof(RRDHOST));
406
+
407
+ struct web_client *w = setup_fresh_web_client();
408
+ buffer_strcat(w->response.data, "GET /api/v1/info%3fblah%3f HTTP/1.1\r\n\r\n");
409
+
410
+ char debug[160];
411
+ repr(debug, sizeof(debug), w->response.data->buffer, w->response.data->len);
412
+ printf("->%s\n", debug);
413
+
414
+ char expected_url_repr[160];
415
+ repr(expected_url_repr, sizeof(expected_url_repr), "info?blah?", 10);
416
+
417
+ expect_value(__wrap_web_client_api_request_v1, host, localhost);
418
+ expect_value(__wrap_web_client_api_request_v1, w, w);
419
+ expect_string(__wrap_web_client_api_request_v1, url_repr, expected_url_repr);
420
+
421
+ web_client_process_request(w);
422
+
423
+ assert_string_equal(w->decoded_query_string, "");
424
+ destroy_web_client(w);
425
+ free(localhost);
426
+}
427
+
428
+static void cr_in_url(void **state)
429
+{
430
+ (void)state;
431
+ localhost = malloc(sizeof(RRDHOST));
432
+
433
+ struct web_client *w = setup_fresh_web_client();
434
+ buffer_strcat(w->response.data, "GET /api/v1/inf\ro\t?blah HTTP/1.1\r\n\r\n");
435
+
436
+ char debug[160];
437
+ repr(debug, sizeof(debug), w->response.data->buffer, w->response.data->len);
438
+ printf("->%s\n", debug);
439
+
440
+ char expected_url_repr[160];
441
+ repr(expected_url_repr, sizeof(expected_url_repr), "inf\no\t", 6);
442
+
443
+ web_client_process_request(w);
444
+
445
+ assert_int_equal(w->response.code, HTTP_RESP_BAD_REQUEST);
446
+
447
+ destroy_web_client(w);
448
+ free(localhost);
449
+}
450
+static void newline_in_url(void **state)
451
+{
452
+ (void)state;
453
+ localhost = malloc(sizeof(RRDHOST));
454
+
455
+ struct web_client *w = setup_fresh_web_client();
456
+ buffer_strcat(w->response.data, "GET /api/v1/inf\no\t?blah HTTP/1.1\r\n\r\n");
457
+
458
+ char debug[160];
459
+ repr(debug, sizeof(debug), w->response.data->buffer, w->response.data->len);
460
+ printf("->%s\n", debug);
461
+
462
+ char expected_url_repr[160];
463
+ repr(expected_url_repr, sizeof(expected_url_repr), "inf\no\t", 6);
464
+
465
+ web_client_process_request(w);
466
+
467
+ assert_int_equal(w->response.code, HTTP_RESP_BAD_REQUEST);
468
+
469
+ destroy_web_client(w);
470
+ free(localhost);
471
+}
472
+
473
+static void bad_version(void **state)
474
+{
475
+ (void)state;
476
+ localhost = malloc(sizeof(RRDHOST));
477
+
478
+ struct web_client *w = setup_fresh_web_client();
479
+ buffer_strcat(w->response.data, "GET /api/v1/info?blah HTTP/1.2\r\n\r\n");
480
+
481
+ char debug[160];
482
+ repr(debug, sizeof(debug), w->response.data->buffer, w->response.data->len);
483
+ printf("->%s\n", debug);
484
+
485
+ char expected_url_repr[160];
486
+ repr(expected_url_repr, sizeof(expected_url_repr), "inf\no\t", 6);
487
+
488
+ web_client_process_request(w);
489
+
490
+ assert_int_equal(w->response.code, HTTP_RESP_BAD_REQUEST);
491
+
492
+ destroy_web_client(w);
493
+ free(localhost);
494
+}
495
+
496
+static void pathless_query(void **state)
497
+{
498
+ (void)state;
499
+ localhost = malloc(sizeof(RRDHOST));
500
+
501
+ struct web_client *w = setup_fresh_web_client();
502
+ buffer_strcat(w->response.data, "GET ?blah HTTP/1.1\r\n\r\n");
503
+
504
+ char debug[160];
505
+ repr(debug, sizeof(debug), w->response.data->buffer, w->response.data->len);
506
+ printf("->%s\n", debug);
507
+
508
+ char expected_url_repr[160];
509
+ repr(expected_url_repr, sizeof(expected_url_repr), "inf\no\t", 6);
510
+
511
+ web_client_process_request(w);
512
+
513
+ assert_int_equal(w->response.code, HTTP_RESP_BAD_REQUEST);
514
+
515
+ destroy_web_client(w);
516
+ free(localhost);
517
+}
518
+
519
+static void pathless_fragment(void **state)
520
+{
521
+ (void)state;
522
+ localhost = malloc(sizeof(RRDHOST));
523
+
524
+ struct web_client *w = setup_fresh_web_client();
525
+ buffer_strcat(w->response.data, "GET #blah HTTP/1.1\r\n\r\n");
526
+
527
+ char debug[160];
528
+ repr(debug, sizeof(debug), w->response.data->buffer, w->response.data->len);
529
+ printf("->%s\n", debug);
530
+
531
+ char expected_url_repr[160];
532
+ repr(expected_url_repr, sizeof(expected_url_repr), "inf\no\t", 6);
533
+
534
+ web_client_process_request(w);
535
+
536
+ assert_int_equal(w->response.code, HTTP_RESP_BAD_REQUEST);
537
+
538
+ destroy_web_client(w);
539
+ free(localhost);
540
+}
541
+
542
+static void short_percent(void **state)
543
+{
544
+ (void)state;
545
+ localhost = malloc(sizeof(RRDHOST));
546
+
547
+ struct web_client *w = setup_fresh_web_client();
548
+ buffer_strcat(w->response.data, "GET % HTTP/1.1\r\n\r\n");
549
+
550
+ char debug[160];
551
+ repr(debug, sizeof(debug), w->response.data->buffer, w->response.data->len);
552
+ printf("->%s\n", debug);
553
+
554
+ char expected_url_repr[160];
555
+ repr(expected_url_repr, sizeof(expected_url_repr), "inf\no\t", 6);
556
+
557
+ web_client_process_request(w);
558
+
559
+ assert_int_equal(w->response.code, HTTP_RESP_BAD_REQUEST);
560
+
561
+ destroy_web_client(w);
562
+ free(localhost);
563
+}
564
+
565
+static void short_percent2(void **state)
566
+{
567
+ (void)state;
568
+ localhost = malloc(sizeof(RRDHOST));
569
+
570
+ struct web_client *w = setup_fresh_web_client();
571
+ buffer_strcat(w->response.data, "GET %0 HTTP/1.1\r\n\r\n");
572
+
573
+ char debug[160];
574
+ repr(debug, sizeof(debug), w->response.data->buffer, w->response.data->len);
575
+ printf("->%s\n", debug);
576
+
577
+ char expected_url_repr[160];
578
+ repr(expected_url_repr, sizeof(expected_url_repr), "inf\no\t", 6);
579
+
580
+ web_client_process_request(w);
581
+
582
+ assert_int_equal(w->response.code, HTTP_RESP_BAD_REQUEST);
583
+
584
+ destroy_web_client(w);
585
+ free(localhost);
586
+}
587
+
588
+static void short_percent3(void **state)
589
+{
590
+ (void)state;
591
+ localhost = malloc(sizeof(RRDHOST));
592
+
593
+ struct web_client *w = setup_fresh_web_client();
594
+ buffer_strcat(w->response.data, "GET %");
595
+
596
+ char debug[160];
597
+ repr(debug, sizeof(debug), w->response.data->buffer, w->response.data->len);
598
+ printf("->%s\n", debug);
599
+
600
+ char expected_url_repr[160];
601
+ repr(expected_url_repr, sizeof(expected_url_repr), "inf\no\t", 6);
602
+
603
+ web_client_process_request(w);
604
+
605
+ assert_int_equal(w->response.code, HTTP_RESP_BAD_REQUEST);
606
+
607
+ destroy_web_client(w);
608
+ free(localhost);
609
+}
610
+
611
+static void percent_nulls(void **state)
612
+{
613
+ (void)state;
614
+ localhost = malloc(sizeof(RRDHOST));
615
+
616
+ struct web_client *w = setup_fresh_web_client();
617
+ buffer_strcat(w->response.data, "GET %00%00%00%00%00%00 HTTP/1.1\r\n");
618
+
619
+ char debug[160];
620
+ repr(debug, sizeof(debug), w->response.data->buffer, w->response.data->len);
621
+ printf("->%s\n", debug);
622
+
623
+ char expected_url_repr[160];
624
+ repr(expected_url_repr, sizeof(expected_url_repr), "inf\no\t", 6);
625
+
626
+ web_client_process_request(w);
627
+
628
+ assert_int_equal(w->response.code, HTTP_RESP_BAD_REQUEST);
629
+
630
+ destroy_web_client(w);
631
+ free(localhost);
632
+}
633
+
634
+static void percent_invalid(void **state)
635
+{
636
+ (void)state;
637
+ localhost = malloc(sizeof(RRDHOST));
638
+
639
+ struct web_client *w = setup_fresh_web_client();
640
+ buffer_strcat(w->response.data, "GET /%x%x%x%x%x%x HTTP/1.1\r\n");
641
+
642
+ char debug[160];
643
+ repr(debug, sizeof(debug), w->response.data->buffer, w->response.data->len);
644
+ printf("->%s\n", debug);
645
+
646
+ char expected_url_repr[160];
647
+ repr(expected_url_repr, sizeof(expected_url_repr), "inf\no\t", 6);
648
+
649
+ web_client_process_request(w);
650
+
651
+ assert_int_equal(w->response.code, HTTP_RESP_BAD_REQUEST);
652
+
653
+ destroy_web_client(w);
654
+ free(localhost);
655
+}
656
+
657
+static void space_in_url(void **state)
658
+{
659
+ (void)state;
660
+ localhost = malloc(sizeof(RRDHOST));
661
+
662
+ struct web_client *w = setup_fresh_web_client();
663
+ buffer_strcat(w->response.data, "GET / / HTTP/1.1\r\n\r\n");
664
+
665
+ char debug[160];
666
+ repr(debug, sizeof(debug), w->response.data->buffer, w->response.data->len);
667
+ printf("->%s\n", debug);
668
+
669
+ char expected_url_repr[160];
670
+ repr(expected_url_repr, sizeof(expected_url_repr), "inf\no\t", 6);
671
+
672
+ web_client_process_request(w);
673
+
674
+ assert_int_equal(w->response.code, HTTP_RESP_BAD_REQUEST);
675
+
676
+ destroy_web_client(w);
677
+ free(localhost);
678
+}
679
+
680
+static void random_sploit1(void **state)
681
+{
682
+ (void)state;
683
+ localhost = malloc(sizeof(RRDHOST));
684
+
685
+ struct web_client *w = setup_fresh_web_client();
686
+ // FIXME: Encoding probably needs to go through printf
687
+ buffer_need_bytes(w->response.data, 55);
688
+ memcpy(
689
+ w->response.data->buffer,
690
+ "GET \x03\x00\x00/*\xE0\x00\x00\x00\x00\x00Cookie: mstshash=Administr HTTP/1.1\r\n\r\n", 54);
691
+ w->response.data->len = 54;
692
+
693
+ char debug[160];
694
+ repr(debug, sizeof(debug), w->response.data->buffer, w->response.data->len);
695
+ printf("->%s\n", debug);
696
+
697
+ char expected_url_repr[160];
698
+ repr(expected_url_repr, sizeof(expected_url_repr), "inf\no\t", 6);
699
+
700
+ web_client_process_request(w);
701
+
702
+ assert_int_equal(w->response.code, HTTP_RESP_BAD_REQUEST);
703
+
704
+ destroy_web_client(w);
705
+ free(localhost);
706
+}
707
+
708
+static void null_in_url(void **state)
709
+{
710
+ (void)state;
711
+ localhost = malloc(sizeof(RRDHOST));
712
+
713
+ struct web_client *w = setup_fresh_web_client();
714
+ buffer_strcat(w->response.data, "GET / / HTTP/1.1\r\n\r\n");
715
+ w->response.data->buffer[5] = 0;
716
+
717
+ char debug[160];
718
+ repr(debug, sizeof(debug), w->response.data->buffer, w->response.data->len);
719
+ printf("->%s\n", debug);
720
+
721
+ char expected_url_repr[160];
722
+ repr(expected_url_repr, sizeof(expected_url_repr), "inf\no\t", 6);
723
+
724
+ web_client_process_request(w);
725
+
726
+ assert_int_equal(w->response.code, HTTP_RESP_BAD_REQUEST);
727
+
728
+ destroy_web_client(w);
729
+ free(localhost);
730
+}
731
+static void many_ands(void **state)
732
+{
733
+ (void)state;
734
+ localhost = malloc(sizeof(RRDHOST));
735
+
736
+ struct web_client *w = setup_fresh_web_client();
737
+ buffer_strcat(w->response.data, "GET foo?");
738
+ for (size_t i = 0; i < 600; i++)
739
+ buffer_strcat(w->response.data, "&");
740
+ buffer_strcat(w->response.data, " HTTP/1.1\r\n\r\n");
741
+
742
+ char debug[2048];
743
+ repr(debug, sizeof(debug), w->response.data->buffer, w->response.data->len);
744
+ printf("->%s\n", debug);
745
+
746
+ char expected_url_repr[160];
747
+ repr(expected_url_repr, sizeof(expected_url_repr), "inf\no\t", 6);
748
+
749
+ web_client_process_request(w);
750
+
751
+ assert_int_equal(w->response.code, HTTP_RESP_BAD_REQUEST);
752
+
753
+ destroy_web_client(w);
754
+ free(localhost);
755
+}
756
+
757
+int main(void)
758
+{
759
+ debug_flags = 0xffffffffffff;
760
+ int fails = 0;
761
+
762
+ struct CMUnitTest static_tests[] = {
763
+ cmocka_unit_test(only_root), cmocka_unit_test(two_slashes), cmocka_unit_test(valid_url),
764
+ cmocka_unit_test(leading_blanks), cmocka_unit_test(empty_url), cmocka_unit_test(newline_in_url),
765
+ cmocka_unit_test(not_a_query), cmocka_unit_test(cr_in_url), cmocka_unit_test(pathless_query),
766
+ cmocka_unit_test(pathless_fragment), cmocka_unit_test(short_percent), cmocka_unit_test(short_percent2),
767
+ cmocka_unit_test(short_percent3), cmocka_unit_test(percent_nulls), cmocka_unit_test(percent_invalid),
768
+ cmocka_unit_test(space_in_url), cmocka_unit_test(random_sploit1), cmocka_unit_test(null_in_url),
769
+ cmocka_unit_test(absolute_url),
770
+ // cmocka_unit_test(many_ands), CMocka cannot recover after this crash
771
+ cmocka_unit_test(bad_version)
772
+ };
773
+ (void)many_ands;
774
+
775
+ fails += cmocka_run_group_tests_name("static_tests", static_tests, NULL, NULL);
776
+ return fails;
777
+}