TEST(BstrTest, IndexOf) { bstr *haystack = bstr_dup_mem("ABCDEFGHIJKL\000NOPQRSTUVWXYZ", 20); bstr *p1 = bstr_dup_c("NOPQ"); bstr *p2 = bstr_dup_c("siej"); bstr *p3 = bstr_dup_c("TUVWXYZ"); bstr *p4 = bstr_dup_c("nopq"); EXPECT_EQ(13, bstr_index_of(haystack, p1)); EXPECT_EQ(-1, bstr_index_of(haystack, p2)); EXPECT_EQ(-1, bstr_index_of(haystack, p3)); EXPECT_EQ(-1, bstr_index_of(haystack, p4)); EXPECT_EQ(13, bstr_index_of_nocase(haystack, p4)); EXPECT_EQ(16, bstr_index_of_c(haystack, "QRS")); EXPECT_EQ(-1, bstr_index_of_c(haystack, "qrs")); EXPECT_EQ(16, bstr_index_of_c_nocase(haystack, "qrs")); EXPECT_EQ(16, bstr_index_of_mem(haystack, "QRSSDF",3)); EXPECT_EQ(-1, bstr_index_of_mem(haystack, "qrssdf",3)); EXPECT_EQ(16, bstr_index_of_mem_nocase(haystack, "qrssdf",3)); bstr_free(p1); bstr_free(p2); bstr_free(p3); bstr_free(p4); bstr_free(haystack); }
TEST(BstrBuilder, Append) { bstr_builder_t *bb = bstr_builder_create(); bstr *str1 = bstr_dup_c("0123456789"); bstr *str2 = bstr_dup_c("abcdefghijklmnopqrstuvwxyz"); EXPECT_EQ(0, bstr_builder_size(bb)); bstr_builder_appendn(bb, str1); bstr_builder_append_c(bb, "#"); bstr_builder_appendn(bb, str2); bstr_builder_append_c(bb, "#"); bstr_builder_append_mem(bb, "!@#$%^&*()", 4); EXPECT_EQ(5, bstr_builder_size(bb)); bstr *result = bstr_builder_to_str(bb); EXPECT_EQ(42, bstr_len(result)); EXPECT_EQ(0, memcmp("0123456789#abcdefghijklmnopqrstuvwxyz#!@#$", bstr_ptr(result),42)); bstr_free(result); bstr_builder_clear(bb); EXPECT_EQ(0, bstr_builder_size(bb)); bstr_builder_destroy(bb); }
TEST(BstrTest, BeginsWith2) { bstr *haystack = bstr_dup_c("ABC"); bstr *p1 = bstr_dup_c("ABCD"); bstr *p2 = bstr_dup_c("EDFG"); EXPECT_EQ(0, bstr_begins_with_mem(haystack, bstr_ptr(p1), bstr_len(p1))); EXPECT_EQ(0, bstr_begins_with_mem_nocase(haystack, bstr_ptr(p1), bstr_len(p1))); EXPECT_EQ(0, bstr_begins_with_mem_nocase(haystack, bstr_ptr(p2), bstr_len(p2))); bstr_free(p1); bstr_free(p2); bstr_free(haystack); }
TEST(UtilTest, ParseContentType6) { bstr *i = bstr_dup_c("multipart/form-data\t boundary=X"); bstr *e = bstr_dup_c("multipart/form-data\t"); bstr *ct = NULL; ASSERT_EQ(HTP_OK, htp_parse_ct_header(i, &ct)); ASSERT_TRUE(ct != NULL); ASSERT_TRUE(bstr_cmp(e, ct) == 0); bstr_free(ct); bstr_free(e); bstr_free(i); }
TEST(BstrTest, Add) { bstr *src1; bstr *src2; bstr *dest; src1 = bstr_dup_c("testtest"); src2 = bstr_dup_c("0123456789abcdefghijklmnopqrstuvwxyz"); dest = bstr_add(src1, src2); EXPECT_EQ(0, bstr_cmp_c(dest, "testtest0123456789abcdefghijklmnopqrstuvwxyz")); // src1 is either invalid or the same as dest after bstr_add bstr_free(src2); bstr_free(dest); }
TEST(UtilTest, Method) { bstr *method = bstr_dup_c("GET"); EXPECT_EQ(HTP_M_GET, htp_convert_method_to_number(method)); bstr_free(method); }
TEST(UtilTest, ParseContentLength) { bstr *str = bstr_dup_c("134"); EXPECT_EQ(134, htp_parse_content_length(str)); bstr_free(str); }
/** * Parses a single v0 request cookie and places the results into tx->request_cookies. * * @param[in] connp * @param[in] data * @param[in] len * @return HTP_OK on success, HTP_ERROR on error. */ int htp_parse_single_cookie_v0(htp_connp_t *connp, unsigned char *data, size_t len) { if (len == 0) return HTP_OK; size_t pos = 0; // Look for '=' while ((pos < len) && (data[pos] != '=')) pos++; if (pos == 0) return HTP_OK; // Ignore nameless cookies bstr *name = bstr_dup_mem(data, pos); if (name == NULL) return HTP_ERROR; bstr *value = NULL; if (pos == len) { // Cookie is empty value = bstr_dup_c(""); } else { // Cookie is not empty value = bstr_dup_mem(data + pos + 1, len - pos - 1); } if (value == NULL) { bstr_free(name); return HTP_ERROR; } // Add cookie directly htp_table_addn(connp->in_tx->request_cookies, name, value); return HTP_OK; }
TEST(BstrTest, RChr) { bstr *b = bstr_dup_c("---I---I---"); EXPECT_EQ(bstr_rchr(b, 'I'), 7); EXPECT_EQ(bstr_rchr(b, 'M'), -1); bstr_free(b); }
TEST(BstrTest, CmpNocase) { bstr *p1; bstr *p2; bstr *p3; p1 = bstr_dup_c("arfarf"); p2 = bstr_dup_c("arfarf"); p3 = bstr_dup_c("arfArf"); EXPECT_EQ(0, bstr_cmp_nocase(p1,p1)); EXPECT_EQ(0, bstr_cmp_nocase(p1,p2)); EXPECT_EQ(0, bstr_cmp_nocase(p2,p1)); EXPECT_EQ(0, bstr_cmp_nocase(p1,p3)); EXPECT_EQ(0, bstr_cmp_nocase(p3,p1)); bstr_free(p1); bstr_free(p2); bstr_free(p3); }
TEST(UtilTest, ParseHostPort10) { bstr *i = bstr_dup_c("www.example.com:65536"); bstr *e = bstr_dup_c("www.example.com"); bstr *host = NULL; int port; uint64_t flags = 0; ASSERT_EQ(HTP_OK, htp_parse_hostport(i, &host, &port, &flags)); ASSERT_TRUE(host != NULL); ASSERT_TRUE(bstr_cmp(e, host) == 0); ASSERT_EQ(-1, port); ASSERT_TRUE(flags & HTP_HOST_INVALID); bstr_free(host); bstr_free(e); bstr_free(i); }
TEST(BstrTest, ChrRchr) { bstr *p1 = bstr_dup_c("0123456789abcdefghijklmnopqrstuvwxyz"); EXPECT_EQ(13, bstr_chr(p1, 'd')); EXPECT_EQ(-1, bstr_chr(p1, '?')); EXPECT_EQ(13, bstr_chr(p1, 'd')); EXPECT_EQ(-1, bstr_chr(p1, '?')); bstr_free(p1); }
TEST(UtilTest, ParseHostPort6) { bstr *i = bstr_dup_c("www.example.com..:8001"); bstr *e = bstr_dup_c("www.example.com."); bstr *host = NULL; int port; uint64_t flags = 0; ASSERT_EQ(HTP_OK, htp_parse_hostport(i, &host, &port, &flags)); ASSERT_TRUE(host != NULL); ASSERT_TRUE(bstr_cmp(e, host) == 0); ASSERT_EQ(8001, port); ASSERT_EQ(0, flags); bstr_free(host); bstr_free(e); bstr_free(i); }
TEST(BstrTest, AdjustLen) { bstr *p1 = bstr_dup_c("abcdef"); bstr_adjust_len(p1, 3); EXPECT_EQ(3, bstr_len(p1)); EXPECT_EQ(0, bstr_cmp_c(p1,"abc")); bstr_free(p1); }
TEST(BstrTest, DupC) { bstr *p1; p1 = bstr_dup_c("arfarf"); EXPECT_EQ(6, bstr_size(p1)); EXPECT_EQ(6, bstr_len(p1)); EXPECT_EQ(0, memcmp("arfarf", bstr_ptr(p1), 6)); bstr_free(p1); }
TEST(BstrTest, CmpCNocase) { bstr *p1; p1 = bstr_dup_c("arfarf"); EXPECT_EQ(0, bstr_cmp_c_nocase(p1, "arfarf")); EXPECT_EQ(0, bstr_cmp_c_nocase(p1, "arfARF")); EXPECT_EQ(1, bstr_cmp_c_nocase(p1, "ArF")); EXPECT_EQ(-1, bstr_cmp_c_nocase(p1, "Not equal")); bstr_free(p1); }
TEST(BstrTest, CmpC) { bstr *p1; p1 = bstr_dup_c("arfarf"); EXPECT_EQ(0, bstr_cmp_c(p1, "arfarf")); EXPECT_EQ(-1, bstr_cmp_c(p1, "arfarf2")); EXPECT_EQ(1, bstr_cmp_c(p1, "arf")); EXPECT_EQ(-1, bstr_cmp_c(p1, "not equal")); bstr_free(p1); }
TEST(BstrTest, AddMem) { bstr *p1; bstr *p2; p1 = bstr_dup_c("testtest"); p2 = bstr_add_mem(p1, "12345678", 4); EXPECT_EQ(0, bstr_cmp_c(p2, "testtest1234")); bstr_free(p2); }
TEST(BstrTest, DupLower) { bstr *p1; bstr *p2; p1 = bstr_dup_c("0123456789ABCDEFGhIJKL"); p2 = bstr_dup_lower(p1); EXPECT_EQ(0, memcmp("0123456789abcdefghijkl", bstr_ptr(p2), 22)); bstr_free(p1); bstr_free(p2); }
TEST(BstrTest, AdjustRealPtr) { bstr *b = bstr_dup_c("ABCDEFGHIJKLMNOPQRSTUVWXYZ"); char c[] = "0123456789"; bstr_adjust_realptr(b, c); bstr_adjust_len(b, strlen(c)); EXPECT_TRUE((char *)bstr_ptr(b) == c); bstr_free(b); }
TEST(BstrTest, DupStr) { bstr *p1; bstr *p2; p1 = bstr_dup_c("s0123456789abcdefghijklmnopqrstuvwxyz"); p2 = bstr_dup(p1); EXPECT_EQ(bstr_len(p1), bstr_len(p2)); EXPECT_EQ(0, memcmp(bstr_ptr(p1), bstr_ptr(p2), bstr_len(p1))); bstr_free(p1); bstr_free(p2); }
TEST(BstrTest, ToLowercase) { bstr *p1; bstr *p2; p1 = bstr_dup_c("aRf3ArF"); p2 = bstr_to_lowercase(p1); EXPECT_EQ(p1, p2); EXPECT_EQ(1, bstr_cmp_c(p1, "aRf3ArF")); EXPECT_EQ(0, bstr_cmp_c(p1, "arf3arf")); bstr_free(p1); }
TEST(BstrTest, Chop) { bstr *p1 = bstr_dup_c("abcdef"); bstr *p2 = bstr_alloc(10); bstr_chop(p1); EXPECT_EQ(0, bstr_cmp_c(p1,"abcde")); bstr_chop(p2); EXPECT_EQ(0, bstr_len(p2)); bstr_free(p1); bstr_free(p2); }
TEST(BstrTest, BeginsWith) { bstr *haystack = bstr_dup_mem("ABCDEFGHIJKL\000NOPQRSTUVWXYZ", 20); bstr *p1 = bstr_dup_c("ABCD"); bstr *p2 = bstr_dup_c("aBcD"); EXPECT_EQ(1, bstr_begins_with(haystack,p1)); EXPECT_NE(1, bstr_begins_with(haystack,p2)); EXPECT_EQ(1, bstr_begins_with_nocase(haystack,p2)); EXPECT_EQ(1, bstr_begins_with_c(haystack, "AB")); EXPECT_NE(1, bstr_begins_with_c(haystack, "ab")); EXPECT_EQ(1, bstr_begins_with_c_nocase(haystack, "ab")); EXPECT_EQ(1, bstr_begins_with_mem(haystack, "ABq",2)); EXPECT_NE(1, bstr_begins_with_mem(haystack, "abq",2)); EXPECT_EQ(1, bstr_begins_with_mem_nocase(haystack, "abq",2)); bstr_free(p1); bstr_free(p2); bstr_free(haystack); }
TEST(BstrTest, DupEx) { bstr *p1; bstr *p2; p1 = bstr_dup_c("0123456789abcdefghijkl"); p2 = bstr_dup_ex(p1, 4, 10); EXPECT_EQ(10, bstr_size(p2)); EXPECT_EQ(10, bstr_len(p2)); EXPECT_EQ(0, memcmp("456789abcd", bstr_ptr(p2),10)); bstr_free(p1); bstr_free(p2); }
TEST(UtilTest, HtpParseUri) { bstr *input = NULL; htp_uri_t *uri = NULL; uri_test *test; input = bstr_dup_c(""); EXPECT_EQ(HTP_OK, htp_parse_uri(input, &uri)); bstr_free(input); free_htp_uri_t(&uri); test = uri_tests; while (test->uri != NULL) { input = bstr_dup_c(test->uri); EXPECT_EQ(HTP_OK, htp_parse_uri(input, &uri)); EXPECT_PRED_FORMAT2(UriIsExpected, test->expected, uri) << "Failed URI = " << test->uri << std::endl; bstr_free(input); free_htp_uri_t(&uri); ++test; } }
TEST(BstrTest, Cmp) { bstr *p1; bstr *p2; bstr *p3; bstr *p4; p1 = bstr_dup_c("arfarf"); p2 = bstr_dup_c("arfarf"); p3 = bstr_dup_c("arfArf"); p4 = bstr_dup_c("arfarf2"); EXPECT_EQ(0, bstr_cmp(p1,p1)); EXPECT_EQ(0, bstr_cmp(p1,p2)); EXPECT_EQ(0, bstr_cmp(p2,p1)); EXPECT_EQ(1, bstr_cmp(p1,p3)); EXPECT_EQ(-1, bstr_cmp(p3,p1)); EXPECT_EQ(-1, bstr_cmp(p1,p4)); EXPECT_EQ(1, bstr_cmp(p4,p1)); bstr_free(p1); bstr_free(p2); bstr_free(p3); bstr_free(p4); }
TEST(BstrTest, AddNoex) { bstr *p1; bstr *p2; bstr *p3; p1 = bstr_alloc(10); p1 = bstr_add_c(p1, "12345"); p2 = bstr_dup_c("abcdef"); p3 = bstr_add_noex(p1,p2); EXPECT_EQ(p1,p3); EXPECT_EQ(0,bstr_cmp_c(p3,"12345abcde")); bstr_free(p1); bstr_free(p2); }
TEST(UtilTest, ParseHostPort1) { bstr *i = bstr_dup_c("www.example.com"); bstr *host; int port; uint64_t flags = 0; ASSERT_EQ(HTP_OK, htp_parse_hostport(i, &host, &port, &flags)); ASSERT_TRUE(bstr_cmp(i, host) == 0); ASSERT_EQ(-1, port); ASSERT_EQ(0, flags); bstr_free(host); bstr_free(i); }
virtual void SetUp() { home = getenv("srcdir"); if (home == NULL) { fprintf(stderr, "This program needs environment variable 'srcdir' set."); exit(EXIT_FAILURE); } cfg = htp_config_create(); htp_config_set_server_personality(cfg, HTP_SERVER_APACHE_2); connp = htp_connp_create(cfg); tx = htp_connp_tx_create(connp); htp_tx_set_user_data(tx, &output); decompressor = htp_gzip_decompressor_create(connp, HTP_COMPRESSION_GZIP); decompressor->callback = GUnzip_decompressor_callback; o_boxing_wizards = bstr_dup_c("The five boxing wizards jump quickly."); output = NULL; }