From 4c124168f0e22fff1af5d975728d3645536f5825 Mon Sep 17 00:00:00 2001 From: Xavier Chi Date: Mon, 17 Sep 2018 15:12:27 -0500 Subject: [PATCH] Test: Convert multiple tests to Catch --- .gitignore | 4 - CMakeLists.txt | 10 +- src/tscore/Makefile.am | 21 +-- src/tscore/{ => unit_tests}/test_List.cc | 13 +- src/tscore/{ => unit_tests}/test_Map.cc | 148 +++++++++--------- src/tscore/{ => unit_tests}/test_Vec.cc | 82 +++++----- .../test_X509HostnameValidator.cc | 89 +++++------ src/tscore/{ => unit_tests}/test_arena.cc | 47 +----- 8 files changed, 168 insertions(+), 246 deletions(-) rename src/tscore/{ => unit_tests}/test_List.cc (90%) rename src/tscore/{ => unit_tests}/test_Map.cc (81%) rename src/tscore/{ => unit_tests}/test_Vec.cc (80%) rename src/tscore/{ => unit_tests}/test_X509HostnameValidator.cc (57%) rename src/tscore/{ => unit_tests}/test_arena.cc (77%) diff --git a/.gitignore b/.gitignore index 0b846b3612c..a1ba7ea5d90 100644 --- a/.gitignore +++ b/.gitignore @@ -79,12 +79,8 @@ src/tscore/ParseRulesCType src/tscore/ParseRulesCTypeToLower src/tscore/ParseRulesCTypeToUpper src/tscore/mkdfa -src/tscore/test_List -src/tscore/test_arena src/tscore/test_atomic src/tscore/test_freelist -src/tscore/test_Map -src/tscore/test_Vec src/tscore/test_geometry src/tscore/test_Regex src/tscore/test_X509HostnameValidator diff --git a/CMakeLists.txt b/CMakeLists.txt index 72db98f042a..e52ac74ed1b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1065,14 +1065,9 @@ add_library(libtscore SHARED include/tscore/SimpleTokenizer.h src/tscore/SourceLocation.cc include/tscore/SourceLocation.h - src/tscore/test_arena.cc src/tscore/test_atomic.cc src/tscore/test_freelist.cc src/tscore/test_geometry.cc - src/tscore/test_List.cc - src/tscore/test_Map.cc - src/tscore/test_Vec.cc - src/tscore/test_X509HostnameValidator.cc include/tscore/TestBox.h src/tscore/TestHttpHeader.cc src/tscore/tests.cc @@ -1090,6 +1085,7 @@ add_library(libtscore SHARED add_executable(test_tscore src/tscore/unit_tests/unit_test_main.cc + src/tscore/unit_tests/test_arena.cc src/tscore/unit_tests/test_BufferWriter.cc src/tscore/unit_tests/test_BufferWriterFormat.cc src/tscore/unit_tests/test_History.cc @@ -1099,6 +1095,8 @@ add_executable(test_tscore src/tscore/unit_tests/test_IntrusivePtr.cc src/tscore/unit_tests/test_IpMap.cc src/tscore/unit_tests/test_layout.cc + src/tscore/unit_tests/test_List.cc + src/tscore/unit_tests/test_Map.cc src/tscore/unit_tests/test_MemArena.cc src/tscore/unit_tests/test_MemSpan.cc src/tscore/unit_tests/test_MT_hashtable.cc @@ -1107,6 +1105,8 @@ add_executable(test_tscore src/tscore/unit_tests/test_Regex.cc src/tscore/unit_tests/test_Scalar.cc src/tscore/unit_tests/test_scoped_resource.cc + src/tscore/unit_tests/test_Vec.cc + src/tscore/unit_tests/test_X509HostnameValidator.cc ) # iocore/cache diff --git a/src/tscore/Makefile.am b/src/tscore/Makefile.am index 1045dd0a001..fdf76684eda 100644 --- a/src/tscore/Makefile.am +++ b/src/tscore/Makefile.am @@ -19,7 +19,7 @@ include $(top_srcdir)/build/tidy.mk noinst_PROGRAMS = mkdfa CompileParseRules -check_PROGRAMS = test_arena test_atomic test_freelist test_geometry test_List test_Map test_Vec test_X509HostnameValidator test_tscore +check_PROGRAMS = test_atomic test_freelist test_geometry test_X509HostnameValidator test_tscore TESTS_ENVIRONMENT = LSAN_OPTIONS=suppressions=suppression.txt @@ -234,21 +234,12 @@ test_atomic_LDADD = libtscore.la $(top_builddir)/src/tscpp/util/libtscpputil.la test_freelist_SOURCES = test_freelist.cc test_freelist_LDADD = libtscore.la $(top_builddir)/src/tscpp/util/libtscpputil.la @LIBTCL@ @LIBPCRE@ -test_arena_SOURCES = test_arena.cc -test_arena_LDADD = libtscore.la $(top_builddir)/src/tscpp/util/libtscpputil.la @LIBTCL@ @LIBPCRE@ - -test_List_SOURCES = test_List.cc -test_Map_SOURCES = test_Map.cc -test_Map_LDADD = libtscore.la $(top_builddir)/src/tscpp/util/libtscpputil.la @LIBTCL@ @LIBPCRE@ - -test_Vec_SOURCES = test_Vec.cc -test_Vec_LDADD = libtscore.la $(top_builddir)/src/tscpp/util/libtscpputil.la @LIBTCL@ @LIBPCRE@ - test_geometry_SOURCES = test_geometry.cc test_geometry_LDADD = libtscore.la $(top_builddir)/src/tscpp/util/libtscpputil.la @LIBTCL@ @LIBPCRE@ -test_X509HostnameValidator_SOURCES = test_X509HostnameValidator.cc +test_X509HostnameValidator_CPPFLAGS = $(AM_CPPFLAGS) -I$(abs_top_srcdir)/tests/include test_X509HostnameValidator_LDADD = libtscore.la $(top_builddir)/src/tscpp/util/libtscpputil.la @LIBTCL@ @LIBPCRE@ @OPENSSL_LIBS@ +test_X509HostnameValidator_SOURCES = unit_tests/test_X509HostnameValidator.cc test_tscore_CPPFLAGS = $(AM_CPPFLAGS)\ -I$(abs_top_srcdir)/tests/include @@ -258,6 +249,7 @@ test_tscore_LDADD = libtscore.la $(top_builddir)/src/tscpp/util/libtscpputil.la test_tscore_SOURCES = \ unit_tests/unit_test_main.cc \ unit_tests/test_AcidPtr.cc \ + unit_tests/test_arena.cc \ unit_tests/test_BufferWriter.cc \ unit_tests/test_BufferWriterFormat.cc \ unit_tests/test_Extendible.cc \ @@ -268,6 +260,8 @@ test_tscore_SOURCES = \ unit_tests/test_IntrusivePtr.cc \ unit_tests/test_IpMap.cc \ unit_tests/test_layout.cc \ + unit_tests/test_List.cc \ + unit_tests/test_Map.cc \ unit_tests/test_MemSpan.cc \ unit_tests/test_MemArena.cc \ unit_tests/test_MT_hashtable.cc \ @@ -275,7 +269,8 @@ test_tscore_SOURCES = \ unit_tests/test_Ptr.cc \ unit_tests/test_Regex.cc \ unit_tests/test_Scalar.cc \ - unit_tests/test_scoped_resource.cc + unit_tests/test_scoped_resource.cc \ + unit_tests/test_Vec.cc CompileParseRules_SOURCES = CompileParseRules.cc diff --git a/src/tscore/test_List.cc b/src/tscore/unit_tests/test_List.cc similarity index 90% rename from src/tscore/test_List.cc rename to src/tscore/unit_tests/test_List.cc index 0407ed7f94d..16131189947 100644 --- a/src/tscore/test_List.cc +++ b/src/tscore/unit_tests/test_List.cc @@ -21,6 +21,8 @@ limitations under the License. */ +#include "catch.hpp" + #include "tscore/List.h" class Foo @@ -39,8 +41,7 @@ class Foo Foo(int i = 0) : x(i) {} }; -int -main() +TEST_CASE("test list", "[List]") { SList(Foo, slink) s; DList(Foo, dlink) d; @@ -59,11 +60,5 @@ main() tot += foo->x; delete foo; } - if (tot != 4957) { - printf("test_List FAILED\n"); - exit(1); - } else { - printf("test_List PASSED\n"); - exit(0); - } + REQUIRE(tot == 4957); } diff --git a/src/tscore/test_Map.cc b/src/tscore/unit_tests/test_Map.cc similarity index 81% rename from src/tscore/test_Map.cc rename to src/tscore/unit_tests/test_Map.cc index bc2b4ffc33d..5fd66d8de59 100644 --- a/src/tscore/test_Map.cc +++ b/src/tscore/unit_tests/test_Map.cc @@ -20,6 +20,9 @@ See the License for the specific language governing permissions and limitations under the License. */ + +#include "catch.hpp" + #include #include "tscore/Map.h" #include @@ -63,67 +66,6 @@ Item::Hash::equal(Key lhs, Key rhs) using Table = TSHashTable; -void -test_TSHashTable() -{ - static uint32_t const N = 270; - Table t; - Item *item = nullptr; - Table::Location loc; - std::list to_delete; - - for (uint32_t i = 1; i <= N; ++i) { - item = new Item(i); - t.insert(item); - to_delete.push_back(item); - } - - for (uint32_t i = 1; i <= N; ++i) { - Table::Location l = t.find(i); - ink_assert(l.isValid()); - ink_assert(i == l->_value); - } - - ink_assert(!(t.find(N * 2).isValid())); - - loc = t.find(N / 2 | 1); - if (loc) { - t.remove(loc); - } else { - ink_assert(!"Did not find expected value"); - } - - if (!loc) { - ; // compiler check. - } - - ink_assert(!(t.find(N / 2 | 1).isValid())); - - for (uint32_t i = 1; i <= N; i += 2) { - t.remove(i); - } - - for (uint32_t i = 1; i <= N; ++i) { - Table::Location l = t.find(i); - if (1 & i) { - ink_assert(!l.isValid()); - } else { - ink_assert(l.isValid()); - } - } - - int n = 0; - for (Table::iterator spot = t.begin(), limit = t.end(); spot != limit; ++spot) { - ++n; - ink_assert((spot->_value & 1) == 0); - } - ink_assert(n == N / 2); - - for (auto it : to_delete) { - delete it; - } -} - class testHashMap { private: @@ -143,8 +85,7 @@ class testHashMap } }; -int -main(int /* argc ATS_UNUSED */, char ** /*argv ATS_UNUSED */) +TEST_CASE("test Map", "[Map]") { typedef Map SSMap; typedef MapElem SSMapElem; @@ -177,30 +118,85 @@ main(int /* argc ATS_UNUSED */, char ** /*argv ATS_UNUSED */) sh.put(ho, 2); sh.put(hum, 3); sh.put(hhi, 4); - ink_assert(sh.get(hi) == 4); - ink_assert(sh.get(ho) == 2); - ink_assert(sh.get(hum) == 3); + REQUIRE(sh.get(hi) == 4); + REQUIRE(sh.get(ho) == 2); + REQUIRE(sh.get(hum) == 3); sh.put("aa", 5); sh.put("ab", 6); sh.put("ac", 7); sh.put("ad", 8); sh.put("ae", 9); sh.put("af", 10); - ink_assert(sh.get(hi) == 4); - ink_assert(sh.get(ho) == 2); - ink_assert(sh.get(hum) == 3); - ink_assert(sh.get("af") == 10); - ink_assert(sh.get("ac") == 7); + REQUIRE(sh.get(hi) == 4); + REQUIRE(sh.get(ho) == 2); + REQUIRE(sh.get(hum) == 3); + REQUIRE(sh.get("af") == 10); + REQUIRE(sh.get("ac") == 7); HashMap sh2(-99); // return -99 if key not found sh2.put("aa", 15); sh2.put("ab", 16); testsh.put("aa", 15); testsh.put("ab", 16); - ink_assert(sh2.get("aa") == 15); - ink_assert(sh2.get("ac") == -99); - ink_assert(testsh.get("aa") == 15); - test_TSHashTable(); + REQUIRE(sh2.get("aa") == 15); + REQUIRE(sh2.get("ac") == -99); + REQUIRE(testsh.get("aa") == 15); + + // test_TSHashTable + static uint32_t const N = 270; + Table t; + Item *item = nullptr; + Table::Location loc; + std::list to_delete; + + for (uint32_t i = 1; i <= N; ++i) { + item = new Item(i); + t.insert(item); + to_delete.push_back(item); + } + + for (uint32_t i = 1; i <= N; ++i) { + Table::Location l = t.find(i); + REQUIRE(l.isValid()); + REQUIRE(i == l->_value); + } + + REQUIRE(!(t.find(N * 2).isValid())); + + loc = t.find(N / 2 | 1); + if (loc) { + t.remove(loc); + } else { + REQUIRE(!"Did not find expected value"); + } - printf("test_Map PASSED\n"); + if (!loc) { + ; // compiler check. + } + + REQUIRE(!(t.find(N / 2 | 1).isValid())); + + for (uint32_t i = 1; i <= N; i += 2) { + t.remove(i); + } + + for (uint32_t i = 1; i <= N; ++i) { + Table::Location l = t.find(i); + if (1 & i) { + REQUIRE(!l.isValid()); + } else { + REQUIRE(l.isValid()); + } + } + + int n = 0; + for (Table::iterator spot = t.begin(), limit = t.end(); spot != limit; ++spot) { + ++n; + REQUIRE((spot->_value & 1) == 0); + } + REQUIRE(n == N / 2); + + for (auto it : to_delete) { + delete it; + } } diff --git a/src/tscore/test_Vec.cc b/src/tscore/unit_tests/test_Vec.cc similarity index 80% rename from src/tscore/test_Vec.cc rename to src/tscore/unit_tests/test_Vec.cc index 53a6fa86ef0..480e222573f 100644 --- a/src/tscore/test_Vec.cc +++ b/src/tscore/unit_tests/test_Vec.cc @@ -22,6 +22,8 @@ /* UnionFind after Tarjan */ +#include "catch.hpp" + #include #include #include "tscore/ink_assert.h" @@ -202,8 +204,7 @@ UnionFind::unify(int n, int m) } } -static void -test_append() +TEST_CASE("test append", "[Vec]") { static const char value[] = "this is a string"; unsigned int len = (int)sizeof(value) - 1; @@ -211,25 +212,24 @@ test_append() Vec str; str.append(value, 0); - ink_assert(str.length() == 0); + REQUIRE(str.length() == 0); str.append(value, len); - ink_assert(memcmp(&str[0], value, len) == 0); - ink_assert(str.length() == len); + REQUIRE(memcmp(&str[0], value, len) == 0); + REQUIRE(str.length() == len); str.clear(); - ink_assert(str.length() == 0); + REQUIRE(str.length() == 0); for (unsigned i = 0; i < 1000; ++i) { str.append(value, len); - ink_assert(memcmp(&str[i * len], value, len) == 0); + REQUIRE(memcmp(&str[i * len], value, len) == 0); } - ink_assert(str.length() == 1000 * len); + REQUIRE(str.length() == 1000 * len); } -static void -test_basic() +TEST_CASE("test basic", "[Vec]") { Vec v, vv, vvv; int tt = 99 * 50, t = 0; @@ -240,7 +240,7 @@ test_basic() for (size_t i = 0; i < 100; i++) { t += (int)(intptr_t)v.v[i]; } - ink_assert(t == tt); + REQUIRE(t == tt); t = 0; for (size_t i = 1; i < 100; i++) { @@ -258,7 +258,7 @@ test_basic() t += (int)(intptr_t)vv.v[i]; } } - ink_assert(t == tt + 1000 * tt); + REQUIRE(t == tt + 1000 * tt); v.clear(); v.reserve(1000); @@ -269,42 +269,42 @@ test_basic() for (size_t i = 0; i < 1000; i++) { t += (int)(intptr_t)v.v[i]; } - ink_assert(t == 999 * 500); + REQUIRE(t == 999 * 500); printf("%zu %zu\n", v.n, v.i); Intervals in; in.insert(1); - ink_assert(in.n == 2); + REQUIRE(in.n == 2); in.insert(2); - ink_assert(in.n == 2); + REQUIRE(in.n == 2); in.insert(6); - ink_assert(in.n == 4); + REQUIRE(in.n == 4); in.insert(7); - ink_assert(in.n == 4); + REQUIRE(in.n == 4); in.insert(9); - ink_assert(in.n == 6); + REQUIRE(in.n == 6); in.insert(4); - ink_assert(in.n == 8); + REQUIRE(in.n == 8); in.insert(5); - ink_assert(in.n == 6); + REQUIRE(in.n == 6); in.insert(3); - ink_assert(in.n == 4); + REQUIRE(in.n == 4); in.insert(8); - ink_assert(in.n == 2); + REQUIRE(in.n == 2); UnionFind uf; uf.size(4); uf.unify(0, 1); uf.unify(2, 3); - ink_assert(uf.find(2) == uf.find(3)); - ink_assert(uf.find(0) == uf.find(1)); - ink_assert(uf.find(0) != uf.find(3)); - ink_assert(uf.find(1) != uf.find(3)); - ink_assert(uf.find(1) != uf.find(2)); - ink_assert(uf.find(0) != uf.find(2)); + REQUIRE(uf.find(2) == uf.find(3)); + REQUIRE(uf.find(0) == uf.find(1)); + REQUIRE(uf.find(0) != uf.find(3)); + REQUIRE(uf.find(1) != uf.find(3)); + REQUIRE(uf.find(1) != uf.find(2)); + REQUIRE(uf.find(0) != uf.find(2)); uf.unify(1, 2); - ink_assert(uf.find(0) == uf.find(3)); - ink_assert(uf.find(1) == uf.find(3)); + REQUIRE(uf.find(0) == uf.find(3)); + REQUIRE(uf.find(1) == uf.find(3)); } static bool @@ -313,8 +313,7 @@ compare(void *a, void *b) return a < b; } -static void -test_sort() +TEST_CASE("test sort", "[Vec]") { Vec v; for (long i = 1; i <= 1000; ++i) { @@ -322,7 +321,7 @@ test_sort() } v.qsort(&compare); for (int i = 0; i < 1000; ++i) { - ink_assert(reinterpret_cast(static_cast(i + 1)) == v[i]); + REQUIRE(reinterpret_cast(static_cast(i + 1)) == v[i]); } v.clear(); @@ -332,7 +331,7 @@ test_sort() v.qsort(&compare); for (long i = 0; i < 1000000; ++i) { - ink_assert(reinterpret_cast(static_cast(i + 1)) == v[i]); + REQUIRE(reinterpret_cast(static_cast(i + 1)) == v[i]); } v.clear(); @@ -343,7 +342,7 @@ test_sort() v.qsort(&compare); for (long i = 0; i < 1000000; ++i) { - ink_assert(reinterpret_cast(static_cast((i / 2) + 1)) == v[i]); + REQUIRE(reinterpret_cast(static_cast((i / 2) + 1)) == v[i]); } // Very long array, already sorted. This is what broke before. @@ -353,7 +352,7 @@ test_sort() } v.qsort(&compare); for (long i = 0; i < 10000000; ++i) { - ink_assert(reinterpret_cast(static_cast(i + 1)) == v[i]); + REQUIRE(reinterpret_cast(static_cast(i + 1)) == v[i]); } // very long, reverse sorted. @@ -363,15 +362,6 @@ test_sort() } v.qsort(&compare); for (long i = 0; i < 10000000; ++i) { - ink_assert(reinterpret_cast(static_cast(i + 1)) == v[i]); + REQUIRE(reinterpret_cast(static_cast(i + 1)) == v[i]); } } - -int -main(int /* argc ATS_UNUSED */, char ** /* argv ATS_UNUSED */) -{ - test_append(); - test_basic(); - test_sort(); - printf("test_Vec PASSED\n"); -} diff --git a/src/tscore/test_X509HostnameValidator.cc b/src/tscore/unit_tests/test_X509HostnameValidator.cc similarity index 57% rename from src/tscore/test_X509HostnameValidator.cc rename to src/tscore/unit_tests/test_X509HostnameValidator.cc index 1ced1b8d449..a9fdee946b7 100644 --- a/src/tscore/test_X509HostnameValidator.cc +++ b/src/tscore/unit_tests/test_X509HostnameValidator.cc @@ -21,6 +21,9 @@ limitations under the License. */ +#define CATCH_CONFIG_RUNNER +#include "catch.hpp" + #include #include #include @@ -31,7 +34,6 @@ #include "tscore/ink_resource.h" #include "tscore/ink_queue.h" #include "tscore/X509HostnameValidator.h" -#include "tscore/TestBox.h" // clang-format off @@ -99,90 +101,71 @@ load_cert_from_string(const char *cert_string) return PEM_read_bio_X509(bio, nullptr, nullptr, nullptr); } -REGRESSION_TEST(CN_match)(RegressionTest *t, int /* atype ATS_UNUSED */, int *pstatus) +TEST_CASE("CN_match", "[X509HostnameValidator]") { - TestBox box(t, pstatus); char *matching; - - box = REGRESSION_TEST_PASSED; X509 *x = load_cert_from_string(test_certificate_cn); - box.check(x != nullptr, "failed to load the test certificate"); - box.check(validate_hostname(x, (unsigned char *)test_certificate_cn_name, false, &matching) == true, "Hostname should match"); - box.check(strcmp(test_certificate_cn_name, matching) == 0, "Return hostname doesn't match lookup"); - box.check(validate_hostname(x, (unsigned char *)test_certificate_cn_name + 1, false, nullptr) == false, - "Hostname shouldn't match"); + REQUIRE(x != nullptr); + REQUIRE(validate_hostname(x, (unsigned char *)test_certificate_cn_name, false, &matching) == true); + REQUIRE(strcmp(test_certificate_cn_name, matching) == 0); + REQUIRE(validate_hostname(x, (unsigned char *)test_certificate_cn_name + 1, false, nullptr) == false); ats_free(matching); } -REGRESSION_TEST(bad_wildcard_SANs)(RegressionTest *t, int /* atype ATS_UNUSED */, int *pstatus) +TEST_CASE("bad_wildcard_SANs", "[X509HostnameValidator]") { - TestBox box(t, pstatus); - - box = REGRESSION_TEST_PASSED; X509 *x = load_cert_from_string(test_certificate_bad_sans); - box.check(x != nullptr, "failed to load the test certificate"); - box.check(validate_hostname(x, (unsigned char *)"something.or.other", false, nullptr) == false, "Hostname shouldn't match"); - box.check(validate_hostname(x, (unsigned char *)"a.b.c", false, nullptr) == false, "Hostname shouldn't match"); - box.check(validate_hostname(x, (unsigned char *)"0.0.0.0", true, nullptr) == false, "Hostname shouldn't match"); - box.check(validate_hostname(x, (unsigned char *)"......", true, nullptr) == false, "Hostname shouldn't match"); - box.check(validate_hostname(x, (unsigned char *)"a.b", true, nullptr) == false, "Hostname shouldn't match"); + REQUIRE(x != nullptr); + REQUIRE(validate_hostname(x, (unsigned char *)"something.or.other", false, nullptr) == false); + REQUIRE(validate_hostname(x, (unsigned char *)"a.b.c", false, nullptr) == false); + REQUIRE(validate_hostname(x, (unsigned char *)"0.0.0.0", true, nullptr) == false); + REQUIRE(validate_hostname(x, (unsigned char *)"......", true, nullptr) == false); + REQUIRE(validate_hostname(x, (unsigned char *)"a.b", true, nullptr) == false); } -REGRESSION_TEST(wildcard_SAN_and_CN)(RegressionTest *t, int /* atype ATS_UNUSED */, int *pstatus) +TEST_CASE("wildcard_SAN_and_CN", "[X509HostnameValidator]") { - TestBox box(t, pstatus); char *matching; - - box = REGRESSION_TEST_PASSED; X509 *x = load_cert_from_string(test_certificate_cn_and_SANs); - box.check(x != nullptr, "failed to load the test certificate"); - box.check(validate_hostname(x, (unsigned char *)test_certificate_cn_name, false, &matching) == true, "Hostname should match"); - box.check(strcmp(test_certificate_cn_name, matching) == 0, "Return hostname doesn't match lookup"); + REQUIRE(x != nullptr); + REQUIRE(validate_hostname(x, (unsigned char *)test_certificate_cn_name, false, &matching) == true); + REQUIRE(strcmp(test_certificate_cn_name, matching) == 0); ats_free(matching); - box.check(validate_hostname(x, (unsigned char *)"a.trafficserver.org", false, &matching) == true, "Hostname should match"); - box.check(strcmp("*.trafficserver.org", matching) == 0, "Return hostname doesn't match lookup"); + REQUIRE(validate_hostname(x, (unsigned char *)"a.trafficserver.org", false, &matching) == true); + REQUIRE(strcmp("*.trafficserver.org", matching) == 0); - box.check(validate_hostname(x, (unsigned char *)"a.*.trafficserver.org", false, nullptr) == false, "Hostname shouldn't match"); + REQUIRE(validate_hostname(x, (unsigned char *)"a.*.trafficserver.org", false, nullptr) == false); ats_free(matching); } -REGRESSION_TEST(IDNA_hostnames)(RegressionTest *t, int /* atype ATS_UNUSED */, int *pstatus) +TEST_CASE("IDNA_hostnames", "[X509HostnameValidator]") { - TestBox box(t, pstatus); char *matching; - box = REGRESSION_TEST_PASSED; X509 *x = load_cert_from_string(test_certificate_cn_and_SANs); - box.check(x != nullptr, "failed to load the test certificate"); - box.check(validate_hostname(x, (unsigned char *)"xn--foobar.trafficserver.org", false, &matching) == true, - "Hostname should match"); - box.check(strcmp("*.trafficserver.org", matching) == 0, "Return hostname doesn't match lookup"); + REQUIRE(x != nullptr); + REQUIRE(validate_hostname(x, (unsigned char *)"xn--foobar.trafficserver.org", false, &matching) == true); + REQUIRE(strcmp("*.trafficserver.org", matching) == 0); ats_free(matching); // IDNA means wildcard must match full label - box.check(validate_hostname(x, (unsigned char *)"xn--foobar.trafficserver.net", false, &matching) == false, - "Hostname shouldn't match"); + REQUIRE(validate_hostname(x, (unsigned char *)"xn--foobar.trafficserver.net", false, &matching) == false); } -REGRESSION_TEST(middle_label_match)(RegressionTest *t, int /* atype ATS_UNUSED */, int *pstatus) +TEST_CASE("middle_label_match", "[X509HostnameValidator]") { - TestBox box(t, pstatus); char *matching; - box = REGRESSION_TEST_PASSED; X509 *x = load_cert_from_string(test_certificate_cn_and_SANs); - box.check(x != nullptr, "failed to load the test certificate"); - box.check(validate_hostname(x, (unsigned char *)"foosomething.trafficserver.com", false, &matching) == true, - "Hostname should match"); - box.check(strcmp("foo*.trafficserver.com", matching) == 0, "Return hostname doesn't match lookup"); + REQUIRE(x != nullptr); + REQUIRE(validate_hostname(x, (unsigned char *)"foosomething.trafficserver.com", false, &matching) == true); + REQUIRE(strcmp("foo*.trafficserver.com", matching) == 0); ats_free(matching); - box.check(validate_hostname(x, (unsigned char *)"somethingbar.trafficserver.net", false, &matching) == true, - "Hostname should match"); - box.check(strcmp("*bar.trafficserver.net", matching) == 0, "Return hostname doesn't match lookup"); + REQUIRE(validate_hostname(x, (unsigned char *)"somethingbar.trafficserver.net", false, &matching) == true); + REQUIRE(strcmp("*bar.trafficserver.net", matching) == 0); ats_free(matching); - box.check(validate_hostname(x, (unsigned char *)"a.bar.trafficserver.net", false, nullptr) == false, "Hostname shouldn't match"); - box.check(validate_hostname(x, (unsigned char *)"foo.bar.trafficserver.net", false, nullptr) == false, - "Hostname shouldn't match"); + REQUIRE(validate_hostname(x, (unsigned char *)"a.bar.trafficserver.net", false, nullptr) == false); + REQUIRE(validate_hostname(x, (unsigned char *)"foo.bar.trafficserver.net", false, nullptr) == false); } int @@ -195,7 +178,7 @@ main(int argc, const char **argv) SSL_library_init(); ink_freelists_snap_baseline(); - int status = RegressionTest::main(argc, argv, REGRESSION_TEST_QUICK); + int status = Catch::Session().run(); ink_freelists_dump(stdout); return status; diff --git a/src/tscore/test_arena.cc b/src/tscore/unit_tests/test_arena.cc similarity index 77% rename from src/tscore/test_arena.cc rename to src/tscore/unit_tests/test_arena.cc index 17e7c6dfbbc..9d99a161422 100644 --- a/src/tscore/test_arena.cc +++ b/src/tscore/unit_tests/test_arena.cc @@ -33,6 +33,8 @@ ****************************************************************************/ +#include "catch.hpp" + #include "tscore/Arena.h" #include @@ -47,29 +49,11 @@ fill_test_data(char *ptr, int size, int seed) } } -int -check_test_data(char *ptr, int size, int seed) -{ - int fail = 0; - char a = 'a' + (seed % 52); - - for (int i = 0; i < size; i++) { - if (ptr[i] != a) { - fail++; - } - a = (a + 1) % 52; - } - - return fail; -} - -int -test_block_boundries() +TEST_CASE("test arena", "[arena]") { const int sizes_to_test = 12; const int regions_to_test = 1024 * 2; char **test_regions = new char *[regions_to_test]; - int failures = 0; Arena *a = new Arena(); for (int i = 0; i < sizes_to_test; i++) { @@ -89,14 +73,12 @@ test_block_boundries() // Now check to make sure the data is correct for (j = 0; j < regions_to_test; j++) { - int f = check_test_data(test_regions[j], test_size, j); - - if (f != 0) { - fprintf(stderr, "block_boundries test failed. size %d region %d\n", test_size, j); - failures++; + char a = 'a' + (j % 52); + for (int k = 0; k < test_size; k++) { + REQUIRE(test_regions[j][k] == a); + a = (a + 1) % 52; } } - // Now free the regions for (j = 0; j < regions_to_test; j++) { a->free(test_regions[j], test_size); @@ -107,19 +89,4 @@ test_block_boundries() delete[] test_regions; delete a; - return failures; -} - -int -main() -{ - int failures = 0; - - failures += test_block_boundries(); - - if (failures) { - return 1; - } else { - return 0; - } }