END_TEST START_TEST (_iter_odd) { hpack_set_t b_set1; hpack_set_t b_set2; hpack_set_iterator_t iter; int i; int i_next; hpack_set_init (b_set1, false); for (i=1; i < HPACK_MAX_HEADER_TABLE_ENTRIES; i += 2) hpack_set_add (b_set1, i); hpack_set_set (b_set2, b_set1); i = 1; hpack_set_iter_init (&iter, b_set1); do { i_next = hpack_set_iter_next (&iter); if (HPACK_MAX_HEADER_TABLE_ENTRIES < i) { ch_assert (-1 == i_next); } else { ch_assert (i_next == i); } i += 2; } while (-1 != i_next); /* Confirm that the original set is unaltered */ ch_assert (hpack_set_equals (b_set1, b_set2)); }
END_TEST START_TEST (_set_max_no_table_evict_all) { ret_t ret; hpack_header_table_t *table; hpack_header_field_t field; hpack_set_t evicted; ret = hpack_header_table_new (&table); ch_assert (ret == ret_ok); hpack_header_field_init (&field); chula_buffer_add_str (&field.name, "custom-key"); chula_buffer_add_str (&field.value, "custom-header"); for (int i=0; i<10; ++i) { ret = hpack_header_table_add (table, &field, evicted); ch_assert (ret_ok == ret); ch_assert (hpack_set_is_empty(evicted)); } ret = hpack_header_table_set_max (table, 0, evicted); ch_assert (ret == ret_ok); hpack_set_is_full(evicted); ch_assert (table->max_data == 0); check_table_empty (table); hpack_header_field_mrproper (&field); hpack_header_table_free (table); }
END_TEST START_TEST (_iter_all) { hpack_set_t b_set; hpack_set_iterator_t iter; int i; int i_next; hpack_set_init (b_set, true); i = 0; hpack_set_iter_init (&iter, b_set); do { i_next = hpack_set_iter_next (&iter); if (HPACK_MAX_HEADER_TABLE_ENTRIES == i) { ch_assert (-1 == i_next); } else { ch_assert (i_next == i); } ++i; } while (-1 != i_next); /* Confirm that the original set is unaltered */ ch_assert (hpack_set_is_full (b_set)); }
void check_table_empty (hpack_header_table_t *table) { ch_assert (table->num_headers == 0); ch_assert (table->used_data == 0); ch_assert (table->headers_offsets.head == table->headers_offsets.tail); ch_assert (table->headers_data.head == table->headers_data.tail); }
END_TEST START_TEST (stress) { ret_t ret; chula_avl_t avl; int pi[] = {3, 14, 15, 92, 65, 35, 89, 79, 32, 38, 46, 26, 43, 38, 32, 79, 50}; ret = chula_avl_init (&avl); ch_assert (ret == ret_ok); /* Add */ for (int i=0; i<16; i++) { char tmp[10]; snprintf (tmp, 10, "%d", pi[i]); chula_avl_add_ptr (&avl, tmp, NULL); } /* Remove */ for (int i=15; i>=0; i--) { char tmp[10]; snprintf (tmp, 10, "%d", pi[i]); chula_avl_del_ptr (&avl, tmp, NULL); } ret = chula_avl_mrproper (AVL_GENERIC(&avl), fake_free); ch_assert (ret == ret_ok); }
END_TEST START_TEST (len) { ret_t ret; size_t len; chula_avl_t avl; /* None */ chula_avl_init (&avl); ch_assert (chula_avl_is_empty (AVL_GENERIC(&avl))); ret = chula_avl_len (AVL_GENERIC(&avl), &len); ch_assert (ret == ret_ok); ch_assert (len == 0); /* Single */ chula_avl_add_ptr (&avl, "uno", INT_TO_POINTER(1)); ch_assert (ret == ret_ok); ch_assert (! chula_avl_is_empty (AVL_GENERIC(&avl))); chula_avl_len (AVL_GENERIC(&avl), &len); ch_assert (len == 1); /* Multiple */ chula_avl_add_ptr (&avl, "dos", INT_TO_POINTER(2)); ch_assert (ret == ret_ok); ch_assert (! chula_avl_is_empty (AVL_GENERIC(&avl))); chula_avl_len (AVL_GENERIC(&avl), &len); ch_assert (len == 2); chula_avl_mrproper (AVL_GENERIC(&avl), NULL); }
END_TEST START_TEST (_strncasestrn) { char *p; char *string = "This is a testing string"; int stringlen = strlen(string); p = strncasestrn (string, stringlen, "iS A", 4); ch_assert (p != NULL); p = strncasestrn (string, stringlen, "tHIS IS A TESTING STRiNG", stringlen); ch_assert (p != NULL); ch_assert (p == string); p = strncasestrn (string, stringlen, string, stringlen); ch_assert (p != NULL); ch_assert (p == string); p = strncasestrn (string, stringlen, "TeStInG", 7); ch_assert (p != NULL); p = strncasestrn (string, stringlen, "is a trick that I'm playing", 4); ch_assert (p != NULL); p = strncasestrn (string, stringlen, "foobar", 6); ch_assert (p == NULL); }
END_TEST START_TEST (_add_all) { ret_t ret; hpack_set_t b_set; hpack_set_init (b_set, false); for (int i=0; i < HPACK_MAX_HEADER_TABLE_ENTRIES; ++i) { ret = hpack_set_add (b_set, i); ch_assert (ret == ret_ok); } ch_assert (hpack_set_is_full (b_set)); }
END_TEST START_TEST (_remove_all) { ret_t ret; hpack_set_t b_set; hpack_set_init (b_set, true); for (int i=0; i < HPACK_MAX_HEADER_TABLE_ENTRIES; ++i) { ret = hpack_set_remove (b_set, i); ch_assert (ret == ret_ok); } ch_assert (hpack_set_is_empty (b_set)); }
END_TEST START_TEST (_buf) { ret_t ret; void *p; chula_avl_t avl; chula_buffer_t uno = CHULA_BUF_INIT; chula_buffer_t dos = CHULA_BUF_INIT; chula_buffer_t tres = CHULA_BUF_INIT; chula_avl_init (&avl); chula_buffer_add_str (&uno, "uno"); chula_buffer_add_str (&dos, "dos"); chula_buffer_add_str (&tres, "tres"); ret = chula_avl_add (&avl, &uno, INT_TO_POINTER(1)); ch_assert (ret == ret_ok); ret = chula_avl_add (&avl, &dos, INT_TO_POINTER(2)); ch_assert (ret == ret_ok); ret = chula_avl_add (&avl, &tres, INT_TO_POINTER(3)); ch_assert (ret == ret_ok); chula_avl_get (&avl, &dos, &p); ch_assert (POINTER_TO_INT(p) == 2); chula_avl_get (&avl, &tres, &p); ch_assert (POINTER_TO_INT(p) == 3); chula_avl_get (&avl, &uno, &p); ch_assert (POINTER_TO_INT(p) == 1); chula_avl_del (&avl, &dos, NULL); chula_avl_get (&avl, &uno, &p); ch_assert (POINTER_TO_INT(p) == 1); p = NULL; ret = chula_avl_get (&avl, &dos, &p); ch_assert (ret != ret_ok); ch_assert (p == NULL); ret = chula_avl_check (AVL_GENERIC(&avl)); ch_assert (ret == ret_ok); chula_buffer_mrproper (&uno); chula_buffer_mrproper (&dos); chula_buffer_mrproper (&tres); chula_avl_mrproper (AVL_GENERIC(&avl), NULL); }
END_TEST START_TEST (_is_empty) { hpack_set_t b_set; hpack_set_init (b_set, false); ch_assert (hpack_set_is_empty (b_set)); }
END_TEST START_TEST (_clear) { ret_t ret; hpack_header_table_t *table; ret = hpack_header_table_new (&table); ch_assert (ret == ret_ok); ret += hpack_header_table_clear (table); ch_assert (ret == ret_ok); ch_assert (table->max_data == SETTINGS_HEADER_TABLE_SIZE); check_table_empty (table); hpack_header_table_free (table); }
END_TEST START_TEST (_is_full) { hpack_set_t b_set; hpack_set_init (b_set, true); ch_assert (hpack_set_is_full (b_set)); }
END_TEST START_TEST (_strnstr) { char *p; char *string = "This is a testing string"; p = strnstr (string, "is a", strlen(string)); ch_assert (p != NULL); p = strnstr (string, string, strlen(string)); ch_assert (p != NULL); ch_assert (p == string); p = strnstr (string, "testing", 9); ch_assert (p == NULL); p = strnstr (string, "foobar", strlen(string)); ch_assert (p == NULL); }
END_TEST START_TEST (_remove_odd) { ret_t ret; hpack_set_t b_set; hpack_set_entry_t e; hpack_set_init (b_set, true); for (int i=1; i<HPACK_MAX_HEADER_TABLE_ENTRIES; i+=2) { ret = hpack_set_remove (b_set, i); ch_assert (ret == ret_ok); } EVEN_SET(e); for (int i=0; i < HPACK_SET_NUM_ENTRIES; ++i) ch_assert (e == b_set[i]); }
END_TEST START_TEST (_strncasestr) { char *p; char *string = "This is a testing string"; int stringlen = strlen(string); p = strncasestr (string, "iS A", stringlen); ch_assert (p != NULL); }
END_TEST START_TEST (_equals_each) { ret_t ret; hpack_set_t b_set1; hpack_set_t b_set2; hpack_set_init (b_set1, false); hpack_set_init (b_set2, false); for (int i=0; i < HPACK_MAX_HEADER_TABLE_ENTRIES; ++i) { ret = hpack_set_add (b_set1, i); ch_assert (ret == ret_ok); hpack_set_set (b_set2, b_set1); ch_assert (hpack_set_equals (b_set1, b_set2)); hpack_set_clear (b_set1); } }
END_TEST START_TEST (_init_full) { hpack_set_t b_set; hpack_set_init (b_set, true); for (int i=0; i < HPACK_SET_NUM_ENTRIES; ++i) ch_assert ((hpack_set_entry_t) ~0 == b_set[i]); }
END_TEST START_TEST (_add_even) { ret_t ret; hpack_set_t b_set; hpack_set_entry_t e; hpack_set_init (b_set, false); for (int i=0; i < HPACK_MAX_HEADER_TABLE_ENTRIES; i+=2) { ret = hpack_set_add (b_set, i); ch_assert (ret == ret_ok); } EVEN_SET(e); for (int i=0; i < HPACK_SET_NUM_ENTRIES; ++i) ch_assert (e == b_set[i]); }
END_TEST START_TEST (_set_max_no_table) { ret_t ret; hpack_header_table_t *table; hpack_set_t evicted; ret = hpack_header_table_new (&table); ch_assert (ret == ret_ok); ret = hpack_header_table_set_max (table, 0, evicted); ch_assert (ret == ret_ok); ch_assert (hpack_set_is_empty (evicted)); ch_assert (table->max_data == 0); check_table_empty (table); hpack_header_table_free (table); }
END_TEST START_TEST (_add_fail) { ret_t ret; hpack_set_t b_set; hpack_set_init (b_set, false); ret = hpack_set_add (b_set, HPACK_MAX_HEADER_TABLE_ENTRIES); ch_assert (ret == ret_error); }
END_TEST START_TEST (_remove_fail) { ret_t ret; hpack_set_t b_set; hpack_set_init (b_set, true); ret = hpack_set_remove (b_set, HPACK_MAX_HEADER_TABLE_ENTRIES); ch_assert (ret == ret_error); }
END_TEST START_TEST (_equals_odd) { ret_t ret; hpack_set_t b_set1; hpack_set_t b_set2; hpack_set_init (b_set1, false); hpack_set_init (b_set2, false); for (int i=1; i < HPACK_MAX_HEADER_TABLE_ENTRIES; i+=2) { ret = hpack_set_add (b_set1, i); ch_assert (ret == ret_ok); ret = hpack_set_add (b_set2, i); ch_assert (ret == ret_ok); ch_assert (hpack_set_equals (b_set1, b_set2)); } }
END_TEST START_TEST (empty) { ret_t ret; chula_avl_t avl; ret = chula_avl_init (&avl); ch_assert (ret == ret_ok); chula_avl_mrproper (AVL_GENERIC(&avl), NULL); }
END_TEST START_TEST (_add_doesnt_fit) { ret_t ret; hpack_header_table_t *table; hpack_set_t evicted_set; hpack_header_field_t field; ret = hpack_header_table_new (&table); ch_assert (ret == ret_ok); hpack_header_field_init (&field); ret = chula_buffer_ensure_size (&field.name, (size_t) (table->max_data/2)); ch_assert (ret_ok == ret); ret = chula_buffer_ensure_size (&field.value, (size_t) (table->max_data/2)); ch_assert (ret_ok == ret); for (unsigned int i=0; i<field.name.len; ++i) { ret = chula_buffer_add_char (&field.name, '0' + (i%10)); ch_assert (ret_ok == ret); ret = chula_buffer_add_char (&field.value, '9' - (i%10)); ch_assert (ret_ok == ret); } ret = hpack_header_table_add (table, &field, evicted_set); ch_assert (ret_ok == ret); hpack_set_is_full (evicted_set); /* Clean up */ hpack_header_field_mrproper (&field); hpack_header_table_free (table); }
END_TEST START_TEST (_strlcat) { size_t len; char s1[12]; char s2[24]; /* Concat */ strncpy (s1, "hi ", sizeof(s1)); strncpy (s2, "there!", sizeof(s2)); len = strlcat (s1, s2, sizeof(s1)); ch_assert_str_eq (s1, "hi there!"); ch_assert (len == strlen("hi there!")); /* Too long */ strncpy (s2, "12345678901234567890", sizeof(s2)); len = strlcat (s1, s2, sizeof(s1)); ch_assert (len > sizeof(s1)); }
END_TEST START_TEST (mrproper) { ret_t ret; chula_avl_t avl; chula_avl_init (&avl); for (int i=0; i<16; i++) { char tmp[3]; snprintf (tmp, 3, "%d", i); chula_avl_add_ptr (&avl, tmp, NULL); } ret = chula_avl_mrproper (AVL_GENERIC(&avl), fake_free); ch_assert (ret == ret_ok); }
END_TEST // //START_TEST (_add_multi_evac) { //END_TEST // START_TEST (field_get_len) { ret_t ret; uint64_t len; hpack_header_field_t field; ret = hpack_header_field_init (&field); ch_assert (ret == ret_ok); len = 1234; ret = hpack_header_field_get_size (&field, &len); ch_assert (ret == ret_ok); ch_assert (len == 0); /* (s = 55) custom-key: custom-header */ chula_buffer_add_str (&field.name, "custom-key"); chula_buffer_add_str (&field.value, "custom-header"); ret = hpack_header_field_get_size (&field, &len); ch_assert (ret == ret_ok); ch_assert (len == 55); /* (s = 42) :method: GET */ hpack_header_field_clean (&field); chula_buffer_add_str (&field.name, ":method"); chula_buffer_add_str (&field.value, "GET"); ret = hpack_header_field_get_size (&field, &len); ch_assert (ret == ret_ok); ch_assert (len == 42); ret = hpack_header_field_mrproper (&field); ch_assert (ret == ret_ok); }
END_TEST START_TEST (_strcasestr) { char *p; char *string = "This is a testing string"; p = strcasestr (string, "iS A"); ch_assert (p != NULL); p = strcasestr (string, "tHIS IS A TESTING STRiNG"); ch_assert (p != NULL); ch_assert (p == string); p = strcasestr (string, string); ch_assert (p != NULL); ch_assert (p == string); p = strcasestr (string, "TeStInG"); ch_assert (p != NULL); p = strcasestr (string, "foobar"); ch_assert (p == NULL); }
END_TEST START_TEST (_ptr) { ret_t ret; void *p; chula_avl_t avl; chula_avl_init (&avl); ret = chula_avl_add_ptr (&avl, "uno", INT_TO_POINTER(1)); ch_assert (ret == ret_ok); ret = chula_avl_add_ptr (&avl, "dos", INT_TO_POINTER(2)); ch_assert (ret == ret_ok); ret = chula_avl_add_ptr (&avl, "tres", INT_TO_POINTER(3)); ch_assert (ret == ret_ok); chula_avl_get_ptr (&avl, "dos", &p); ch_assert (POINTER_TO_INT(p) == 2); chula_avl_get_ptr (&avl, "tres", &p); ch_assert (POINTER_TO_INT(p) == 3); chula_avl_get_ptr (&avl, "uno", &p); ch_assert (POINTER_TO_INT(p) == 1); chula_avl_set_case (&avl, true); chula_avl_get_ptr (&avl, "TrEs", &p); ch_assert (POINTER_TO_INT(p) == 3); chula_avl_set_case (&avl, false); chula_avl_del_ptr (&avl, "dos", NULL); chula_avl_get_ptr (&avl, "uno", &p); ch_assert (POINTER_TO_INT(p) == 1); p = NULL; ret = chula_avl_get_ptr (&avl, "dos", &p); ch_assert (ret != ret_ok); ch_assert (p == NULL); ret = chula_avl_check (AVL_GENERIC(&avl)); ch_assert (ret == ret_ok); chula_avl_mrproper (AVL_GENERIC(&avl), NULL); }