summaryrefslogtreecommitdiff
path: root/src/libdisfluid/disfluid-tests.h
blob: e77b497b87cf20857caddd6f28b198de2075cd00 (plain)
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
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
#ifndef DISFLUID_TESTS_INCLUDED
# define DISFLUID_TESTS_INCLUDED

struct disfluid_tests_report;

static inline char *run_tests (size_t *n_tests, size_t *n_errors);

# include <check.h>

# include "disfluid-trie-node.h"
# include "disfluid-init.h"
# include "disfluid-version.h"
# include "disfluid-cache-entry.h"
# include "disfluid-cache-entry-key.h"
# include "disfluid-db.h"
# include "string-desc.h"

# define BYTES * 1

# define KILOBYTES * 1024 BYTES

# define MEGABYTES * 1024 KILOBYTES

# define THOUSAND * 1000

# define MILLION THOUSAND THOUSAND

# define BILLION THOUSAND MILLION

static void
prepare_database (char **filename)
{
  if (ALLOC_N (*filename, strlen ("/tmp/check-disfluid-db-XXXXXX") + 1) < 0)
    {
      abort ();
    }
  strcpy (*filename, "/tmp/check-disfluid-db-XXXXXX");
  if (mkdtemp (*filename) == NULL)
    {
      abort ();
    }
}

static int
cleanup_database (char **filename)
{
  int error = 0;
  ck_assert_int_eq (db_unmark (*filename), 0);
  ck_assert_int_eq (db_collect (*filename), 0);
  for (size_t i = 0; i < 256; i++)
    {
      char *group_name = NULL;
      ck_assert_int_ge (ALLOC_N
			(group_name,
			 strlen (*filename) + strlen ("/") + 2 + 1), 0);
      strcpy (group_name, *filename);
      strcat (group_name, "/");
      static const char *alpha[] = {
	"0", "1", "2", "3", "4", "5", "6", "7",
	"8", "9", "a", "b", "c", "d", "e", "f"
      };
      strcat (group_name, alpha[i / 16]);
      strcat (group_name, alpha[i % 16]);
      rmdir (group_name);
      FREE (group_name);
    }
  rmdir (*filename);
  FREE (*filename);
}

/* *INDENT-OFF* */
START_TEST (test_check_version)
/* *INDENT-ON* */

{
  const char *lib_version = version ();
  ck_assert_str_eq (lib_version, VERSION);
}
/* *INDENT-OFF* */
END_TEST
/* *INDENT-ON* */

/* *INDENT-OFF* */
START_TEST (test_read_invalid_magic_number)
/* *INDENT-ON* */

{
  char *database;
  prepare_database (&database);
  string_desc_t file_id = { 0 };
  string_desc_t lit_id = { 0 };
  char data[16 + 12 + 12 + 32 + 32 + 32 + 1] = { 0 };
  memcpy (data, "invalid magic no", 16);
  const string_desc_t file_data = {._nbytes = sizeof (data),._data = data };
  static const char *lit_data = "hello :)";
  const string_desc_t lit = {._nbytes = strlen (lit_data),._data =
      (char *) lit_data
  };
  ck_assert_int_eq (db_write (database, &lit_id, &lit), 0);
  memcpy (data + 16 + 12 + 12, lit_id._data, lit_id._nbytes);
  memcpy (data + 16 + 12 + 12 + 32, lit_id._data, lit_id._nbytes);
  memcpy (data + 16 + 12 + 12 + 32 + 32, lit_id._data, lit_id._nbytes);
  FREE (lit_id._data);
  ck_assert_int_eq (db_write (database, &file_id, &file_data), 0);
  struct timespec request_date, response_date;
  bool invalidated;
  string_desc_t key_id = { 0 }, response_header_id = { 0 }, response_body_id =
    { 0 };
  ck_assert_int_lt (db_read_cache_entry
		    (database, &file_id, &request_date, &response_date,
		     &invalidated, &key_id, &response_header_id,
		     &response_body_id), 0);
  FREE (key_id._data);
  FREE (response_header_id._data);
  FREE (response_body_id._data);
  FREE (file_id._data);
  cleanup_database (&database);
}
/* *INDENT-OFF* */
END_TEST
/* *INDENT-ON* */

/* *INDENT-OFF* */
START_TEST (test_read_normal)
/* *INDENT-ON* */

{
  static const char *key_value = "GET http://example.com\r\n";
  static const char *header_value =
    "HTTP/1.1 200 OK\r\nContent-Type: text/plain\r\n\r\n";
  static const char *body_value = "Hello, world!";
  const string_desc_t key = {._nbytes = strlen (key_value),._data =
      (char *) key_value
  };
  const string_desc_t header = {._nbytes = strlen (header_value),._data =
      (char *) header_value
  };
  const string_desc_t body = {._nbytes = strlen (body_value),._data =
      (char *) body_value
  };
  string_desc_t key_id = { 0 };
  string_desc_t header_id = { 0 };
  string_desc_t body_id = { 0 };
  string_desc_t entry_id = { 0 };
  const struct timespec request_date = {.tv_sec = 12345,.tv_nsec = 678910 };
  const struct timespec response_date = {.tv_sec = 111213,.tv_nsec = 141516 };
  char *database;
  prepare_database (&database);
  ck_assert_int_eq (db_write (database, &key_id, &key), 0);
  ck_assert_int_eq (db_write (database, &header_id, &header), 0);
  ck_assert_int_eq (db_write (database, &body_id, &body), 0);
  ck_assert_int_eq (db_write_cache_entry
		    (database, &entry_id, &request_date, &response_date, true,
		     &key_id, &header_id, &body_id), 0);
  string_desc_t check_key_id = { 0 };
  string_desc_t check_header_id = { 0 };
  string_desc_t check_body_id = { 0 };
  struct timespec check_request_date, check_response_date;
  bool check_invalidated;
  ck_assert_int_eq (db_read_cache_entry
		    (database, &entry_id, &check_request_date,
		     &check_response_date, &check_invalidated, &check_key_id,
		     &check_header_id, &check_body_id), 0);
  ck_assert_int_eq (check_request_date.tv_sec, 12345);
  ck_assert_int_eq (check_request_date.tv_nsec, 678910);
  ck_assert_int_eq (check_response_date.tv_sec, 111213);
  ck_assert_int_eq (check_response_date.tv_nsec, 141516);
  ck_assert (check_invalidated);
  ck_assert_int_eq (check_key_id._nbytes, 32);
  ck_assert_int_eq (check_header_id._nbytes, 32);
  ck_assert_int_eq (check_body_id._nbytes, 32);
  ck_assert_int_eq (memcmp (check_key_id._data, key_id._data, 32), 0);
  ck_assert_int_eq (memcmp (check_header_id._data, header_id._data, 32), 0);
  ck_assert_int_eq (memcmp (check_body_id._data, body_id._data, 32), 0);
  FREE (check_key_id._data);
  FREE (check_header_id._data);
  FREE (check_body_id._data);
  FREE (key_id._data);
  FREE (header_id._data);
  FREE (body_id._data);
  FREE (entry_id._data);
  cleanup_database (&database);
}
/* *INDENT-OFF* */
END_TEST
/* *INDENT-ON* */

/* *INDENT-OFF* */
START_TEST (test_cache_key_no_host)
/* *INDENT-ON* */

{
  static const char *request = "\
GET /example HTTP/1.1\r\n\
If-None-Match: W/\"hello\", \"world\"\r\n\
If-None-Match: W/\"hi :)\"\r\n\
\r\n";
  static const char *response = "\
HTTP/1.1 200 Hi\r\n\
Content-Type: text/plain\r\n\
\r\n\
Hi :)";
  char memory[512];
  int error =
    compute_cache_key ("https", request, response, sizeof (memory), memory);
  ck_assert_int_eq (error, -1);
}
/* *INDENT-OFF* */
END_TEST
/* *INDENT-ON* */

/* *INDENT-OFF* */
START_TEST (test_cache_key_invalid_request_line)
/* *INDENT-ON* */

{
  static const char *request = "\
GET /example HTTP/1.1\r\n\
Hello\r\n\
Host: example.com\r\n\
If-None-Match: W/\"hello\", \"world\"\r\n\
If-None-Match: W/\"hi :)\"\r\n\
\r\n";
  static const char *response = "\
HTTP/1.1 200 Hi\r\n\
Content-Type: text/plain\r\n\
\r\n\
Hi :)";
  char memory[512];
  int error =
    compute_cache_key ("https", request, response, sizeof (memory), memory);
  ck_assert_int_eq (error, -1);
}
/* *INDENT-OFF* */
END_TEST
/* *INDENT-ON* */

/* *INDENT-OFF* */
START_TEST (test_cache_key_invalid_response_line)
/* *INDENT-ON* */

{
  static const char *request = "\
GET /example HTTP/1.1\r\n\
Host: example.com\r\n\
If-None-Match: W/\"hello\", \"world\"\r\n\
If-None-Match: W/\"hi :)\"\r\n\
\r\n";
  static const char *response = "\
HTTP/1.1 200 Hi\r\n\
Content-Type: text/plain\r\n\
This line is not HTTP/1.1.\r\n\
\r\n\
Hi :)";
  char memory[512];
  int error =
    compute_cache_key ("https", request, response, sizeof (memory), memory);
  ck_assert_int_eq (error, -1);
}
/* *INDENT-OFF* */
END_TEST
/* *INDENT-ON* */

/* *INDENT-OFF* */
START_TEST (test_simple_cache_key)
/* *INDENT-ON* */

{
  static const char *request = "\
GET /example HTTP/1.1\r\n\
Host: example.com\r\n\
If-None-Match: W/\"hello\", \"world\"\r\n\
If-None-Match: W/\"hi :)\"\r\n\
\r\n";
  static const char *response = "\
HTTP/1.1 200 Hi\r\n\
Content-Type: text/plain\r\n\
\r\n\
Hi :)";
  char memory[512];
  int error = compute_cache_key ("https", request, response, 0, memory);
  ck_assert_int_eq (error, -2);
  error =
    compute_cache_key ("https", request, response, sizeof (memory), memory);
  ck_assert_int_eq (error, 0);
  ck_assert_str_eq (memory, "GET https://example.com/example\r\n");
}
/* *INDENT-OFF* */
END_TEST
/* *INDENT-ON* */

/* *INDENT-OFF* */
START_TEST (test_simply_varied_cache_key)
/* *INDENT-ON* */

{
  static const char *request = "\
GET /example HTTP/1.1\r\n\
Host: example.com\r\n\
Accept: text/plain\r\n\
Foo: bar\r\n\
Foo: other, thing\r\n\
If-None-Match: W/\"hello\", \"world\"\r\n\
If-None-Match: W/\"hi :)\"\r\n\
\r\n";
  static const char *response = "\
HTTP/1.1 200 Hi\r\n\
Content-Type: text/plain\r\n\
ETag: W/\"hello\"\r\n\
Vary: accept, foo\r\n\
\r\n\
Hi :)";
  char memory[512];
  int error = compute_cache_key ("https", request, response, 0, memory);
  ck_assert_int_eq (error, -2);
  error =
    compute_cache_key ("https", request, response, sizeof (memory), memory);
  ck_assert_int_eq (error, 0);
  ck_assert_str_eq (memory, "\
GET https://example.com/example\r\n\
text/plain\r\n\
bar,other, thing\r\n");
  /* FIXME: "other, thing" is not touched so the space is
     kept. Should disfluid try and parse comma-separated lists? Also
     see test_multiply_varied_cache_key for another occurence of the
     same problem. */
}
/* *INDENT-OFF* */
END_TEST
/* *INDENT-ON* */

/* *INDENT-OFF* */
START_TEST (test_multiply_varied_cache_key)
/* *INDENT-ON* */

{
  static const char *request = "\
GET /example HTTP/1.1\r\n\
Host: example.com\r\n\
Accept: text/plain\r\n\
Foo: bar\r\n\
Foo: other, thing\r\n\
If-None-Match: W/\"hello\", \"world\"\r\n\
If-None-Match: W/\"hi :)\"\r\n\
\r\n";
  static const char *response = "\
HTTP/1.1 200 Hi\r\n\
Content-Type: text/plain\r\n\
ETag: W/\"hello\"\r\n\
Vary: accept, foo\r\n\
Vary: accept\r\n\
\r\n\
Hi :)";
  char memory[512];
  int error = compute_cache_key ("https", request, response, 0, memory);
  ck_assert_int_eq (error, -2);
  error =
    compute_cache_key ("https", request, response, sizeof (memory), memory);
  ck_assert_int_eq (error, 0);
  ck_assert_str_eq (memory, "\
GET https://example.com/example\r\n\
text/plain\r\n\
bar,other, thing\r\n\
text/plain\r\n");
  /* FIXME: see the test_simply_varied_cache_key test. */
}
/* *INDENT-OFF* */
END_TEST
/* *INDENT-ON* */

/* *INDENT-OFF* */
START_TEST (test_partial_cache_key)
/* *INDENT-ON* */

{
  static const char *request = "\
GET /example HTTP/1.1\r\n\
Host: example.com\r\n\
Accept: text/plain\r\n\
\r\n";
  static const char *response = "\
HTTP/1.1 200 Hi\r\n\
Content-Type: text/plain\r\n\
Vary: a\r\n\
\r\n\
Hi :)";
  char memory[512];
  int error =
    compute_cache_key ("https", request, response, sizeof (memory), memory);
  ck_assert_int_eq (error, 0);
  ck_assert_str_eq (memory, "\
GET https://example.com/example\r\n\
\r\n");				/* Notice how the "a" header is not present in the request, so
				   the value is just empty. */
}
/* *INDENT-OFF* */
END_TEST
/* *INDENT-ON* */

/* *INDENT-OFF* */
START_TEST (test_cache_hash)
/* *INDENT-ON* */

{
  static const char *method = "GET";
  static const char *uri = "https://example.com";
  static const char *password = "hello :)";
  char output[65];
  int error =
    hash_primary_cache_key (method, uri, password, strlen (password),
			    sizeof (output), output);
  ck_assert_int_eq (error, 0);
  ck_assert_str_eq (output, "\
3ee6f03368423b1e7da285a42f049a8e\
9af16f60337cc02fb1bfe5d7aeab6fc8");
}
/* *INDENT-OFF* */
END_TEST
/* *INDENT-ON* */

/* *INDENT-OFF* */
START_TEST (test_db)
/* *INDENT-ON* */

{
  ck_assert_int_eq (db_check (), 0);
}
/* *INDENT-OFF* */
END_TEST
/* *INDENT-ON* */

/* *INDENT-OFF* */
START_TEST (test_db_trie_inner)
/* *INDENT-ON* */

{
  char *database;
  prepare_database (&database);
  static const char *hello = "hello";
  string_desc_t common_part = {
    ._nbytes = 5,
    ._data = (char *) hello
  };
  static const char series[32] = { 1, 2, 3, 4, 5 };
  string_desc_t interesting_branch = {
    ._nbytes = 32,
    ._data = (char *) series
  };
  ck_assert_int_eq (common_part._nbytes, strlen (hello));
  ck_assert_int_eq (interesting_branch._nbytes, sizeof (series));
  const struct trie_node node = {
    .common_part = common_part,
    .branches = {{0}, interesting_branch, {0}},
    .is_leaf = false
  };
  string_desc_t node_id = { 0 };
  ck_assert_int_eq (db_write_trie_node (database, &node_id, &node), 0);
  struct trie_node *actual_node = NULL;
  ck_assert_int_eq (db_read_trie_node (database, node_id, &actual_node), 0);
  ck_assert (!trie_node_is_leaf (actual_node));
  const string_desc_t actual_common_part =
    trie_node_common_part (actual_node);
  const string_desc_t actual_branches[2] = {
    trie_node_branch (actual_node, 0),
    trie_node_branch (actual_node, 1)
  };
  ck_assert_int_eq (actual_common_part._nbytes, common_part._nbytes);
  ck_assert_int_eq (memcmp
		    (actual_common_part._data, common_part._data,
		     common_part._nbytes), 0);
  ck_assert_int_eq (actual_branches[0]._nbytes, 0);
  ck_assert_ptr_null (actual_branches[0]._data);
  ck_assert_int_eq (actual_branches[1]._nbytes, 32);
  ck_assert_int_eq (memcmp (actual_branches[1]._data, series, 32), 0);
  trie_node_free (&actual_node);
  FREE (node_id._data);
  cleanup_database (&database);
}
/* *INDENT-OFF* */
END_TEST
/* *INDENT-ON* */

/* *INDENT-OFF* */
START_TEST (test_db_trie_leaf)
/* *INDENT-ON* */

{
  char *database;
  prepare_database (&database);
  static const char *hello = "hello";
  const string_desc_t common_part = {
    ._nbytes = 5,
    ._data = (char *) hello
  };
  static const char series[32] = { 1, 2, 3, 4, 5 };
  const string_desc_t leaf_id = {
    ._nbytes = 32,
    ._data = (char *) series
  };
  ck_assert_int_eq (common_part._nbytes, strlen (hello));
  ck_assert_int_eq (leaf_id._nbytes, sizeof (series));
  const struct trie_node node = {
    .common_part = common_part,
    .branches = {leaf_id, {0}},
    .is_leaf = true
  };
  string_desc_t node_id = { 0 };
  ck_assert_int_eq (db_write_trie_node (database, &node_id, &node), 0);
  struct trie_node *actual_node = NULL;
  ck_assert_int_eq (db_read_trie_node (database, node_id, &actual_node), 0);
  const string_desc_t actual_common_part =
    trie_node_common_part (actual_node);
  ck_assert (trie_node_is_leaf (actual_node));
  const string_desc_t actual_leaf_id = trie_node_leaf_id (actual_node);
  ck_assert_int_eq (actual_common_part._nbytes, common_part._nbytes);
  ck_assert_int_eq (memcmp
		    (actual_common_part._data, common_part._data,
		     common_part._nbytes), 0);
  ck_assert_int_eq (memcmp
		    (actual_leaf_id._data, leaf_id._data, leaf_id._nbytes),
		    0);
  trie_node_free (&actual_node);
  FREE (node_id._data);
  cleanup_database (&database);
}
/* *INDENT-OFF* */
END_TEST
/* *INDENT-ON* */

static inline char *
tests_read_whole_file (int file)
{
  off_t size = lseek (file, 0, SEEK_END);
  lseek (file, 0, SEEK_SET);
  char *ret = malloc (size + 1);
  if (ret == NULL)
    {
      return NULL;
    }
  size_t n_total = 0;
  while ((off_t) n_total < size)
    {
      ssize_t next = read (file, ret + n_total, size - n_total);
      if (next <= 0)
	{
	  free (ret);
	  return NULL;
	}
      n_total += next;
    }
  ret[size] = '\0';
  return ret;
}

static inline char *
run_tests (size_t *n_tests, size_t *n_errors)
{
  ensure_init ();
  Suite *suite = suite_create (_("disfluid unit tests"));
  TCase *general = tcase_create (_("disfluid general tests"));
  tcase_add_test (general, test_check_version);
  suite_add_tcase (suite, general);
  TCase *cache_entry = tcase_create (_("disfluid cache entry files"));
  tcase_add_test (cache_entry, test_read_normal);
  tcase_add_test (cache_entry, test_read_invalid_magic_number);
  suite_add_tcase (suite, cache_entry);
  TCase *cache_key = tcase_create (_("disfluid cache key"));
  tcase_add_test (cache_key, test_cache_key_no_host);
  tcase_add_test (cache_key, test_cache_key_invalid_request_line);
  tcase_add_test (cache_key, test_cache_key_invalid_response_line);
  tcase_add_test (cache_key, test_simple_cache_key);
  tcase_add_test (cache_key, test_simply_varied_cache_key);
  tcase_add_test (cache_key, test_multiply_varied_cache_key);
  tcase_add_test (cache_key, test_partial_cache_key);
  suite_add_tcase (suite, cache_key);
  TCase *cache_hash = tcase_create (_("disfluid cache hash key"));
  tcase_add_test (cache_hash, test_cache_hash);
  suite_add_tcase (suite, cache_hash);
  TCase *db = tcase_create (_("disfluid database"));
  tcase_add_test (db, test_db);
  tcase_add_test (db, test_db_trie_inner);
  tcase_add_test (db, test_db_trie_leaf);
  suite_add_tcase (suite, db);
  SRunner *runner = srunner_create (suite);
  char log_file_name[] = "/tmp/disfluid-unit-tests-XXXXXX";
  int log_file = mkstemp (log_file_name);
  srunner_set_log (runner, log_file_name);
  srunner_run_all (runner, CK_NORMAL);
  char *result = tests_read_whole_file (log_file);
  if (n_tests)
    {
      *n_tests = srunner_ntests_run (runner);
    }
  if (n_errors)
    {
      *n_errors = srunner_ntests_failed (runner);
    }
  srunner_free (runner);
  close (log_file);
  remove (log_file_name);
  return result;
}

#endif /* DISFLUID_TESTS_INCLUDED */