From: Mike Stepanek (mstepane) Date: Tue, 14 Dec 2021 16:37:52 +0000 (+0000) Subject: Pull request #3217: Refactoring JS normalizer tests. X-Git-Tag: 3.1.19.0~4 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=c1ab3ad794d2d0e6d793ddf614f07b64db837884;p=thirdparty%2Fsnort3.git Pull request #3217: Refactoring JS normalizer tests. Merge in SNORT/snort3 from ~OSHUMEIK/snort3:js_utest_refactor to master Squashed commit of the following: commit 9ed93df3d297cb83ed90adcfffd470fb2213eeae Author: Oleksii Shumeiko Date: Tue Dec 14 16:16:42 2021 +0200 utils: update JS normalizer unit tests Common configurations are moved to a single place. A variable with a list of ignored words is renamed. --- diff --git a/src/utils/test/js_normalizer_test.cc b/src/utils/test/js_normalizer_test.cc index ec9001f07..53ea0b68b 100644 --- a/src/utils/test/js_normalizer_test.cc +++ b/src/utils/test/js_normalizer_test.cc @@ -35,18 +35,11 @@ using namespace snort; #ifdef CATCH_TEST_BUILD -#define DEPTH 65535 -#define MAX_TEMPLATE_NESTING 4 -#define MAX_BRACKET_DEPTH 256 -#define MAX_SCOPE_DEPTH 256 - -static const std::unordered_set s_ignored_ids { "console", "eval", "document" }; - #define DST_SIZE 512 #define NORMALIZE(src) \ JSIdentifierCtxStub ident_ctx; \ - JSNormalizer norm(ident_ctx, DEPTH, MAX_TEMPLATE_NESTING, MAX_BRACKET_DEPTH); \ + JSNormalizer norm(ident_ctx, norm_depth, max_template_nesting, max_bracket_depth); \ auto ret = norm.normalize(src, sizeof(src)); \ const char* ptr = norm.get_src_next(); \ int act_len = norm.script_size(); \ @@ -70,7 +63,7 @@ static const std::unordered_set s_ignored_ids { "console", "eval", #define NORMALIZE_L(src, src_len, dst, dst_len, depth, ret, ptr, len) \ { \ JSIdentifierCtxStub ident_ctx; \ - JSNormalizer norm(ident_ctx, depth, MAX_TEMPLATE_NESTING, MAX_BRACKET_DEPTH); \ + JSNormalizer norm(ident_ctx, depth, max_template_nesting, max_bracket_depth); \ ret = norm.normalize(src, src_len); \ ptr = norm.get_src_next(); \ len = norm.script_size(); \ @@ -113,8 +106,8 @@ static const std::unordered_set s_ignored_ids { "console", "eval", { \ char dst1[sizeof(exp1)]; \ \ - JSIdentifierCtx ident_ctx(DEPTH, MAX_SCOPE_DEPTH, s_ignored_ids); \ - JSNormalizer norm(ident_ctx, DEPTH, MAX_TEMPLATE_NESTING, MAX_BRACKET_DEPTH); \ + JSIdentifierCtx ident_ctx(norm_depth, max_scope_depth, s_ignored_ids); \ + JSNormalizer norm(ident_ctx, norm_depth, max_template_nesting, max_bracket_depth); \ \ DO(src1, sizeof(src1) - 1, dst1, sizeof(dst1) - 1); \ CHECK(!memcmp(exp1, dst1, sizeof(exp1) - 1)); \ @@ -127,8 +120,8 @@ static const std::unordered_set s_ignored_ids { "console", "eval", char dst1[sizeof(exp1)]; \ char dst2[sizeof(exp2)]; \ \ - JSIdentifierCtx ident_ctx(DEPTH, MAX_SCOPE_DEPTH, s_ignored_ids); \ - JSNormalizer norm(ident_ctx, DEPTH, MAX_TEMPLATE_NESTING, MAX_BRACKET_DEPTH); \ + JSIdentifierCtx ident_ctx(norm_depth, max_scope_depth, s_ignored_ids); \ + JSNormalizer norm(ident_ctx, norm_depth, max_template_nesting, max_bracket_depth); \ \ DO(src1, sizeof(src1) - 1, dst1, sizeof(dst1) - 1); \ CHECK(!memcmp(exp1, dst1, sizeof(exp1) - 1)); \ @@ -144,7 +137,7 @@ static const std::unordered_set s_ignored_ids { "console", "eval", char dst1[sizeof(exp1)]; \ \ JSIdentifierCtxStub ident_ctx; \ - JSNormalizer norm(ident_ctx, DEPTH, MAX_TEMPLATE_NESTING, MAX_BRACKET_DEPTH); \ + JSNormalizer norm(ident_ctx, norm_depth, max_template_nesting, max_bracket_depth); \ \ DO(src1, sizeof(src1) - 1, dst1, sizeof(dst1) - 1); \ CHECK(!memcmp(exp1, dst1, sizeof(exp1) - 1)); \ @@ -158,7 +151,7 @@ static const std::unordered_set s_ignored_ids { "console", "eval", char dst2[sizeof(exp2)]; \ \ JSIdentifierCtxStub ident_ctx; \ - JSNormalizer norm(ident_ctx, DEPTH, MAX_TEMPLATE_NESTING, MAX_BRACKET_DEPTH); \ + JSNormalizer norm(ident_ctx, norm_depth, max_template_nesting, max_bracket_depth); \ \ DO(src1, sizeof(src1) - 1, dst1, sizeof(dst1) - 1); \ CHECK(!memcmp(exp1, dst1, sizeof(exp1) - 1)); \ @@ -176,7 +169,7 @@ static const std::unordered_set s_ignored_ids { "console", "eval", char dst3[sizeof(exp3)]; \ \ JSIdentifierCtxStub ident_ctx; \ - JSNormalizer norm(ident_ctx, DEPTH, MAX_TEMPLATE_NESTING, MAX_BRACKET_DEPTH); \ + JSNormalizer norm(ident_ctx, norm_depth, max_template_nesting, max_bracket_depth); \ \ DO(src1, sizeof(src1) - 1, dst1, sizeof(dst1) - 1); \ CHECK(!memcmp(exp1, dst1, sizeof(exp1) - 1)); \ @@ -195,7 +188,7 @@ static const std::unordered_set s_ignored_ids { "console", "eval", char dst1[sizeof(exp1)]; \ \ JSIdentifierCtxStub ident_ctx; \ - JSNormalizer norm(ident_ctx, DEPTH, MAX_TEMPLATE_NESTING, MAX_BRACKET_DEPTH); \ + JSNormalizer norm(ident_ctx, norm_depth, max_template_nesting, max_bracket_depth); \ \ TRY(src1, sizeof(src1) - 1, dst1, sizeof(dst1) - 1, code); \ CHECK(!memcmp(exp1, dst1, sizeof(exp1) - 1)); \ @@ -207,7 +200,7 @@ static const std::unordered_set s_ignored_ids { "console", "eval", char dst2[sizeof(exp2)]; \ \ JSIdentifierCtxStub ident_ctx; \ - JSNormalizer norm(ident_ctx, DEPTH, MAX_TEMPLATE_NESTING, MAX_BRACKET_DEPTH); \ + JSNormalizer norm(ident_ctx, norm_depth, max_template_nesting, max_bracket_depth); \ \ DO(src1, sizeof(src1) - 1, dst1, sizeof(dst1) - 1); \ CHECK(!memcmp(exp1, dst1, sizeof(exp1) - 1)); \ @@ -223,7 +216,7 @@ static const std::unordered_set s_ignored_ids { "console", "eval", char dst3[sizeof(exp3)]; \ \ JSIdentifierCtxStub ident_ctx; \ - JSNormalizer norm(ident_ctx, DEPTH, MAX_TEMPLATE_NESTING, MAX_BRACKET_DEPTH); \ + JSNormalizer norm(ident_ctx, norm_depth, max_template_nesting, max_bracket_depth); \ \ DO(src1, sizeof(src1) - 1, dst1, sizeof(dst1) - 1); \ CHECK(!memcmp(exp1, dst1, sizeof(exp1) - 1)); \ @@ -241,7 +234,7 @@ static const std::unordered_set s_ignored_ids { "console", "eval", char dst2[sizeof(exp2)]; \ \ JSIdentifierCtxStub ident_ctx; \ - JSNormalizer norm(ident_ctx, DEPTH, MAX_TEMPLATE_NESTING, MAX_BRACKET_DEPTH, limit); \ + JSNormalizer norm(ident_ctx, norm_depth, max_template_nesting, max_bracket_depth, limit); \ \ DO(src1, sizeof(src1) - 1, dst1, sizeof(dst1) - 1); \ CHECK(!memcmp(exp1, dst1, sizeof(exp1) - 1)); \ @@ -252,134 +245,134 @@ static const std::unordered_set s_ignored_ids { "console", "eval", CLOSE(); \ } -#define NORM_COMBINED_2(src1, src2, exp) \ - { \ - JSIdentifierCtxStub ident_ctx; \ - JSNormalizer norm(ident_ctx, DEPTH, MAX_TEMPLATE_NESTING, MAX_BRACKET_DEPTH); \ - \ - auto ret = norm.normalize(src1, sizeof(src1) - 1); \ - REQUIRE(ret == JSTokenizer::SCRIPT_CONTINUE); \ - \ - ret = norm.normalize(src2, sizeof(src2) - 1); \ - REQUIRE(ret == JSTokenizer::SCRIPT_CONTINUE); \ - \ - const char end[] = ""; \ - ret = norm.normalize(end, sizeof(end) - 1); \ - REQUIRE(ret == JSTokenizer::SCRIPT_ENDED); \ - \ - size_t act_len = norm.script_size(); \ - REQUIRE(act_len == sizeof(exp) - 1); \ - \ - const char* dst = norm.get_script(); \ - CHECK(!memcmp(exp, dst, sizeof(exp) - 1)); \ - } - -#define NORM_COMBINED_3(src1, src2, src3, exp) \ - { \ - JSIdentifierCtxStub ident_ctx; \ - JSNormalizer norm(ident_ctx, DEPTH, MAX_TEMPLATE_NESTING, MAX_BRACKET_DEPTH); \ - \ - auto ret = norm.normalize(src1, sizeof(src1) - 1); \ - REQUIRE(ret == JSTokenizer::SCRIPT_CONTINUE); \ - \ - ret = norm.normalize(src2, sizeof(src2) - 1); \ - REQUIRE(ret == JSTokenizer::SCRIPT_CONTINUE); \ - \ - ret = norm.normalize(src3, sizeof(src3) - 1); \ - REQUIRE(ret == JSTokenizer::SCRIPT_CONTINUE); \ - \ - const char end[] = ""; \ - ret = norm.normalize(end, sizeof(end) - 1); \ - REQUIRE(ret == JSTokenizer::SCRIPT_ENDED); \ - \ - size_t act_len = norm.script_size(); \ - REQUIRE(act_len == sizeof(exp) - 1); \ - \ - const char* dst = norm.get_script(); \ - CHECK(!memcmp(exp, dst, sizeof(exp) - 1)); \ - } - -#define NORM_COMBINED_BAD_2(src1, src2, exp, eret) \ - { \ - JSIdentifierCtxStub ident_ctx; \ - JSNormalizer norm(ident_ctx, DEPTH, MAX_TEMPLATE_NESTING, MAX_BRACKET_DEPTH); \ - \ - auto ret = norm.normalize(src1, sizeof(src1) - 1); \ - REQUIRE(ret == JSTokenizer::SCRIPT_CONTINUE); \ - \ - ret = norm.normalize(src2, sizeof(src2) - 1); \ - REQUIRE(ret == eret); \ - \ - size_t act_len = norm.script_size(); \ - REQUIRE(act_len == sizeof(exp) - 1); \ - \ - const char* dst = norm.get_script(); \ - CHECK(!memcmp(exp, dst, sizeof(exp) - 1)); \ - } - -#define NORM_COMBINED_BAD_3(src1, src2, src3, exp, eret) \ - { \ - JSIdentifierCtxStub ident_ctx; \ - JSNormalizer norm(ident_ctx, DEPTH, MAX_TEMPLATE_NESTING, MAX_BRACKET_DEPTH); \ - \ - auto ret = norm.normalize(src1, sizeof(src1) - 1); \ - REQUIRE(ret == JSTokenizer::SCRIPT_CONTINUE); \ - \ - ret = norm.normalize(src2, sizeof(src2) - 1); \ - REQUIRE(ret == JSTokenizer::SCRIPT_CONTINUE); \ - \ - ret = norm.normalize(src3, sizeof(src3) - 1); \ - REQUIRE(ret == eret); \ - \ - size_t act_len = norm.script_size(); \ - REQUIRE(act_len == sizeof(exp) - 1); \ - \ - const char* dst = norm.get_script(); \ - CHECK(!memcmp(exp, dst, sizeof(exp) - 1)); \ - } - -#define NORM_COMBINED_LIMITED_2(limit, src1, src2, exp) \ - { \ - JSIdentifierCtxStub ident_ctx; \ - JSNormalizer norm(ident_ctx, DEPTH, MAX_TEMPLATE_NESTING, MAX_BRACKET_DEPTH, limit); \ - \ - auto ret = norm.normalize(src1, sizeof(src1) - 1); \ - REQUIRE(ret == JSTokenizer::SCRIPT_CONTINUE); \ - \ - ret = norm.normalize(src2, sizeof(src2) - 1); \ - REQUIRE(ret == JSTokenizer::SCRIPT_CONTINUE); \ - \ - const char end[] = ""; \ - ret = norm.normalize(end, sizeof(end) - 1); \ - REQUIRE(ret == JSTokenizer::SCRIPT_ENDED); \ - \ - size_t act_len = norm.script_size(); \ - REQUIRE(act_len == sizeof(exp) - 1); \ - \ - const char* dst = norm.get_script(); \ - CHECK(!memcmp(exp, dst, sizeof(exp) - 1)); \ - } - -#define NORM_COMBINED_S_2(src1, src2, exp) \ - { \ - JSIdentifierCtx ident_ctx(DEPTH, MAX_SCOPE_DEPTH, s_ignored_ids); \ - JSNormalizer norm(ident_ctx, DEPTH, MAX_TEMPLATE_NESTING, MAX_BRACKET_DEPTH); \ - \ - auto ret = norm.normalize(src1, sizeof(src1) - 1); \ - REQUIRE(ret == JSTokenizer::SCRIPT_CONTINUE); \ - \ - ret = norm.normalize(src2, sizeof(src2) - 1); \ - REQUIRE(ret == JSTokenizer::SCRIPT_CONTINUE); \ - \ - const char end[] = ""; \ - ret = norm.normalize(end, sizeof(end) - 1); \ - REQUIRE(ret == JSTokenizer::SCRIPT_ENDED); \ - \ - size_t act_len = norm.script_size(); \ - REQUIRE(act_len == sizeof(exp) - 1); \ - \ - const char* dst = norm.get_script(); \ - CHECK(!memcmp(exp, dst, sizeof(exp) - 1)); \ +#define NORM_COMBINED_2(src1, src2, exp) \ + { \ + JSIdentifierCtxStub ident_ctx; \ + JSNormalizer norm(ident_ctx, norm_depth, max_template_nesting, max_bracket_depth); \ + \ + auto ret = norm.normalize(src1, sizeof(src1) - 1); \ + REQUIRE(ret == JSTokenizer::SCRIPT_CONTINUE); \ + \ + ret = norm.normalize(src2, sizeof(src2) - 1); \ + REQUIRE(ret == JSTokenizer::SCRIPT_CONTINUE); \ + \ + const char end[] = ""; \ + ret = norm.normalize(end, sizeof(end) - 1); \ + REQUIRE(ret == JSTokenizer::SCRIPT_ENDED); \ + \ + size_t act_len = norm.script_size(); \ + REQUIRE(act_len == sizeof(exp) - 1); \ + \ + const char* dst = norm.get_script(); \ + CHECK(!memcmp(exp, dst, sizeof(exp) - 1)); \ + } + +#define NORM_COMBINED_3(src1, src2, src3, exp) \ + { \ + JSIdentifierCtxStub ident_ctx; \ + JSNormalizer norm(ident_ctx, norm_depth, max_template_nesting, max_bracket_depth); \ + \ + auto ret = norm.normalize(src1, sizeof(src1) - 1); \ + REQUIRE(ret == JSTokenizer::SCRIPT_CONTINUE); \ + \ + ret = norm.normalize(src2, sizeof(src2) - 1); \ + REQUIRE(ret == JSTokenizer::SCRIPT_CONTINUE); \ + \ + ret = norm.normalize(src3, sizeof(src3) - 1); \ + REQUIRE(ret == JSTokenizer::SCRIPT_CONTINUE); \ + \ + const char end[] = ""; \ + ret = norm.normalize(end, sizeof(end) - 1); \ + REQUIRE(ret == JSTokenizer::SCRIPT_ENDED); \ + \ + size_t act_len = norm.script_size(); \ + REQUIRE(act_len == sizeof(exp) - 1); \ + \ + const char* dst = norm.get_script(); \ + CHECK(!memcmp(exp, dst, sizeof(exp) - 1)); \ + } + +#define NORM_COMBINED_BAD_2(src1, src2, exp, eret) \ + { \ + JSIdentifierCtxStub ident_ctx; \ + JSNormalizer norm(ident_ctx, norm_depth, max_template_nesting, max_bracket_depth); \ + \ + auto ret = norm.normalize(src1, sizeof(src1) - 1); \ + REQUIRE(ret == JSTokenizer::SCRIPT_CONTINUE); \ + \ + ret = norm.normalize(src2, sizeof(src2) - 1); \ + REQUIRE(ret == eret); \ + \ + size_t act_len = norm.script_size(); \ + REQUIRE(act_len == sizeof(exp) - 1); \ + \ + const char* dst = norm.get_script(); \ + CHECK(!memcmp(exp, dst, sizeof(exp) - 1)); \ + } + +#define NORM_COMBINED_BAD_3(src1, src2, src3, exp, eret) \ + { \ + JSIdentifierCtxStub ident_ctx; \ + JSNormalizer norm(ident_ctx, norm_depth, max_template_nesting, max_bracket_depth); \ + \ + auto ret = norm.normalize(src1, sizeof(src1) - 1); \ + REQUIRE(ret == JSTokenizer::SCRIPT_CONTINUE); \ + \ + ret = norm.normalize(src2, sizeof(src2) - 1); \ + REQUIRE(ret == JSTokenizer::SCRIPT_CONTINUE); \ + \ + ret = norm.normalize(src3, sizeof(src3) - 1); \ + REQUIRE(ret == eret); \ + \ + size_t act_len = norm.script_size(); \ + REQUIRE(act_len == sizeof(exp) - 1); \ + \ + const char* dst = norm.get_script(); \ + CHECK(!memcmp(exp, dst, sizeof(exp) - 1)); \ + } + +#define NORM_COMBINED_LIMITED_2(limit, src1, src2, exp) \ + { \ + JSIdentifierCtxStub ident_ctx; \ + JSNormalizer norm(ident_ctx, norm_depth, max_template_nesting, max_bracket_depth, limit); \ + \ + auto ret = norm.normalize(src1, sizeof(src1) - 1); \ + REQUIRE(ret == JSTokenizer::SCRIPT_CONTINUE); \ + \ + ret = norm.normalize(src2, sizeof(src2) - 1); \ + REQUIRE(ret == JSTokenizer::SCRIPT_CONTINUE); \ + \ + const char end[] = ""; \ + ret = norm.normalize(end, sizeof(end) - 1); \ + REQUIRE(ret == JSTokenizer::SCRIPT_ENDED); \ + \ + size_t act_len = norm.script_size(); \ + REQUIRE(act_len == sizeof(exp) - 1); \ + \ + const char* dst = norm.get_script(); \ + CHECK(!memcmp(exp, dst, sizeof(exp) - 1)); \ + } + +#define NORM_COMBINED_S_2(src1, src2, exp) \ + { \ + JSIdentifierCtx ident_ctx(norm_depth, max_scope_depth, s_ignored_ids); \ + JSNormalizer norm(ident_ctx, norm_depth, max_template_nesting, max_bracket_depth); \ + \ + auto ret = norm.normalize(src1, sizeof(src1) - 1); \ + REQUIRE(ret == JSTokenizer::SCRIPT_CONTINUE); \ + \ + ret = norm.normalize(src2, sizeof(src2) - 1); \ + REQUIRE(ret == JSTokenizer::SCRIPT_CONTINUE); \ + \ + const char end[] = ""; \ + ret = norm.normalize(end, sizeof(end) - 1); \ + REQUIRE(ret == JSTokenizer::SCRIPT_ENDED); \ + \ + size_t act_len = norm.script_size(); \ + REQUIRE(act_len == sizeof(exp) - 1); \ + \ + const char* dst = norm.get_script(); \ + CHECK(!memcmp(exp, dst, sizeof(exp) - 1)); \ } // ClamAV test vectors from: https://github.com/Cisco-Talos/clamav/blob/main/unit_tests/check_jsnorm.c @@ -682,11 +675,11 @@ TEST_CASE("all patterns", "[JSNormalizer]") const char *ptr0, *ptr1, *ptr2, *ptr3, *ptr4; int act_len0, act_len1, act_len2, act_len3, act_len4; - NORMALIZE_L(src0, sizeof(src0), dst0, sizeof(dst0), DEPTH, ret0, ptr0, act_len0); - NORMALIZE_L(src1, sizeof(src1), dst1, sizeof(dst1), DEPTH, ret1, ptr1, act_len1); - NORMALIZE_L(src2, sizeof(src2), dst2, sizeof(dst2), DEPTH, ret2, ptr2, act_len2); - NORMALIZE_L(src3, sizeof(src3), dst3, sizeof(dst3), DEPTH, ret3, ptr3, act_len3); - NORMALIZE_L(src4, sizeof(src4), dst4, sizeof(dst4), DEPTH, ret4, ptr4, act_len4); + NORMALIZE_L(src0, sizeof(src0), dst0, sizeof(dst0), norm_depth, ret0, ptr0, act_len0); + NORMALIZE_L(src1, sizeof(src1), dst1, sizeof(dst1), norm_depth, ret1, ptr1, act_len1); + NORMALIZE_L(src2, sizeof(src2), dst2, sizeof(dst2), norm_depth, ret2, ptr2, act_len2); + NORMALIZE_L(src3, sizeof(src3), dst3, sizeof(dst3), norm_depth, ret3, ptr3, act_len3); + NORMALIZE_L(src4, sizeof(src4), dst4, sizeof(dst4), norm_depth, ret4, ptr4, act_len4); CHECK(ret0 == JSTokenizer::SCRIPT_CONTINUE); CHECK((ptr0 - src0) == sizeof(src0)); @@ -1533,7 +1526,7 @@ TEST_CASE("endings", "[JSNormalizer]") const char* ptr; int ret; - NORMALIZE_L(src, sizeof(src), dst, sizeof(dst), DEPTH, ret, ptr, act_len); + NORMALIZE_L(src, sizeof(src), dst, sizeof(dst), norm_depth, ret, ptr, act_len); CHECK(ret == JSTokenizer::SCRIPT_ENDED); CHECK(act_len == sizeof(expected) - 1); @@ -1549,7 +1542,7 @@ TEST_CASE("endings", "[JSNormalizer]") int ret; JSIdentifierCtxStub ident_ctx; - JSNormalizer norm(ident_ctx, 7, MAX_TEMPLATE_NESTING, MAX_BRACKET_DEPTH); + JSNormalizer norm(ident_ctx, 7, max_template_nesting, max_bracket_depth); ret = norm.normalize(src, sizeof(src)); ptr = norm.get_src_next(); int act_len1 = norm.script_size(); @@ -2424,8 +2417,8 @@ TEST_CASE("split and continuation combined", "[JSNormalizer]") char dst3[sizeof(exp3)]; char dst4[sizeof(exp4)]; - JSIdentifierCtx ident_ctx(DEPTH, MAX_SCOPE_DEPTH, s_ignored_ids); - JSNormalizer norm(ident_ctx, DEPTH, MAX_TEMPLATE_NESTING, MAX_BRACKET_DEPTH); + JSIdentifierCtx ident_ctx(norm_depth, max_scope_depth, s_ignored_ids); + JSNormalizer norm(ident_ctx, norm_depth, max_template_nesting, max_bracket_depth); DO(src1, sizeof(src1) - 1, dst1, sizeof(dst1) - 1); CHECK(!memcmp(exp1, dst1, sizeof(exp1) - 1)); @@ -2459,8 +2452,8 @@ TEST_CASE("split and continuation combined", "[JSNormalizer]") char dst2[sizeof(exp2)]; char dst3[sizeof(exp3)]; - JSIdentifierCtx ident_ctx(DEPTH, MAX_SCOPE_DEPTH, s_ignored_ids); - JSNormalizer norm(ident_ctx, DEPTH, MAX_TEMPLATE_NESTING, MAX_BRACKET_DEPTH); + JSIdentifierCtx ident_ctx(norm_depth, max_scope_depth, s_ignored_ids); + JSNormalizer norm(ident_ctx, norm_depth, max_template_nesting, max_bracket_depth); TRY(src1, sizeof(src1) - 1, dst1, sizeof(dst1) - 1, JSTokenizer::SCRIPT_CONTINUE); CHECK(!memcmp(exp1, dst1, sizeof(exp1) - 1)); @@ -2787,7 +2780,7 @@ TEST_CASE("scope misc", "[JSNormalizer]") char* act = new char[exp_len]; JSIdentifierCtxStub ident_ctx; - JSNormalizer norm(ident_ctx, DEPTH, MAX_TEMPLATE_NESTING, MAX_BRACKET_DEPTH); + JSNormalizer norm(ident_ctx, norm_depth, max_template_nesting, max_bracket_depth); DO(dat, dat_len, act, exp_len); CHECK(!memcmp(exp, act, exp_len)); @@ -2817,7 +2810,7 @@ TEST_CASE("scope misc", "[JSNormalizer]") char* act = new char[exp_len]; JSIdentifierCtxStub ident_ctx; - JSNormalizer norm(ident_ctx, DEPTH, MAX_TEMPLATE_NESTING, MAX_BRACKET_DEPTH); + JSNormalizer norm(ident_ctx, norm_depth, max_template_nesting, max_bracket_depth); TRY(dat, dat_len, act, exp_len, JSTokenizer::BRACKET_NESTING_OVERFLOW); CHECK(!memcmp(exp, act, exp_len)); @@ -4171,8 +4164,8 @@ TEST_CASE("Scope tracking - error handling", "[JSNormalizer]") const char exp[] = "function(){if"; uint32_t scope_depth = 2; - JSIdentifierCtx ident_ctx(DEPTH, scope_depth, s_ignored_ids); - JSNormalizer normalizer(ident_ctx, DEPTH, MAX_TEMPLATE_NESTING, MAX_BRACKET_DEPTH); + JSIdentifierCtx ident_ctx(norm_depth, scope_depth, s_ignored_ids); + JSNormalizer normalizer(ident_ctx, norm_depth, max_template_nesting, max_bracket_depth); auto ret = normalizer.normalize(src, strlen(src)); std::string dst(normalizer.get_script(), normalizer.script_size()); @@ -4187,8 +4180,6 @@ TEST_CASE("Scope tracking - error handling", "[JSNormalizer]") #ifdef BENCHMARK_TEST -#define UNLIM_DEPTH -1 - static constexpr const char* s_closing_tag = ""; static const std::string make_input(const char* begin, const char* mid, @@ -4223,7 +4214,7 @@ static JSTokenizer::JSRet norm_ret(JSNormalizer& normalizer, const std::string& TEST_CASE("JS Normalizer, literals by 8 K", "[JSNormalizer]") { JSIdentifierCtxStub ident_ctx; - JSNormalizer normalizer(ident_ctx, UNLIM_DEPTH, MAX_TEMPLATE_NESTING, MAX_BRACKET_DEPTH); + JSNormalizer normalizer(ident_ctx, unlim_depth, max_template_nesting, max_bracket_depth); char dst[DEPTH]; constexpr size_t size = 1 << 13; @@ -4263,7 +4254,7 @@ TEST_CASE("JS Normalizer, literals by 8 K", "[JSNormalizer]") TEST_CASE("JS Normalizer, literals by 64 K", "[JSNormalizer]") { JSIdentifierCtxStub ident_ctx; - JSNormalizer normalizer(ident_ctx, UNLIM_DEPTH, MAX_TEMPLATE_NESTING, MAX_SCOPE_DEPTH); + JSNormalizer normalizer(ident_ctx, unlim_depth, max_template_nesting, max_scope_depth); char dst[DEPTH]; constexpr size_t size = 1 << 16; @@ -4311,8 +4302,8 @@ TEST_CASE("JS Normalizer, id normalization", "[JSNormalizer]") input.append(s_closing_tag, strlen(s_closing_tag)); JSIdentifierCtxStub ident_ctx_mock; - JSNormalizer normalizer_wo_ident(ident_ctx_mock, UNLIM_DEPTH, - MAX_TEMPLATE_NESTING, MAX_BRACKET_DEPTH); + JSNormalizer normalizer_wo_ident(ident_ctx_mock, unlim_depth, + max_template_nesting, max_bracket_depth); REQUIRE(norm_ret(normalizer_wo_ident, input) == JSTokenizer::SCRIPT_ENDED); BENCHMARK("without substitution") @@ -4322,8 +4313,8 @@ TEST_CASE("JS Normalizer, id normalization", "[JSNormalizer]") }; const std::unordered_set ids{}; - JSIdentifierCtx ident_ctx(DEPTH, MAX_SCOPE_DEPTH, ids); - JSNormalizer normalizer_w_ident(ident_ctx, UNLIM_DEPTH, MAX_TEMPLATE_NESTING, MAX_BRACKET_DEPTH); + JSIdentifierCtx ident_ctx(norm_depth, max_scope_depth, ids); + JSNormalizer normalizer_w_ident(ident_ctx, unlim_depth, max_template_nesting, max_bracket_depth); REQUIRE(norm_ret(normalizer_w_ident, input) == JSTokenizer::SCRIPT_ENDED); BENCHMARK("with substitution") @@ -4333,9 +4324,9 @@ TEST_CASE("JS Normalizer, id normalization", "[JSNormalizer]") }; const std::unordered_set ids_n { "n" }; - JSIdentifierCtx ident_ctx_ids_n(DEPTH, MAX_SCOPE_DEPTH, ids_n); - JSNormalizer normalizer_iids(ident_ctx_ids_n, UNLIM_DEPTH, - MAX_TEMPLATE_NESTING, MAX_BRACKET_DEPTH); + JSIdentifierCtx ident_ctx_ids_n(norm_depth, max_scope_depth, ids_n); + JSNormalizer normalizer_iids(ident_ctx_ids_n, unlim_depth, + max_template_nesting, max_bracket_depth); REQUIRE(norm_ret(normalizer_iids, input) == JSTokenizer::SCRIPT_ENDED); BENCHMARK("with ignored identifiers") @@ -4349,7 +4340,7 @@ TEST_CASE("JS Normalizer, scope tracking", "[JSNormalizer]") { constexpr uint32_t depth = 65535; JSIdentifierCtxStub ident_ctx; - JSNormalizer normalizer(ident_ctx, UNLIM_DEPTH, MAX_TEMPLATE_NESTING, depth); + JSNormalizer normalizer(ident_ctx, unlim_depth, max_template_nesting, depth); auto src_ws = make_input("", " ", "", depth); auto src_brace_rep = make_input_repeat("{}", depth); @@ -4387,14 +4378,14 @@ TEST_CASE("JS Normalizer, scope tracking", "[JSNormalizer]") TEST_CASE("JS Normalizer, automatic semicolon", "[JSNormalizer]") { - auto w_semicolons = make_input("", "a;\n", "", DEPTH); - auto wo_semicolons = make_input("", "a \n", "", DEPTH); + auto w_semicolons = make_input("", "a;\n", "", depth); + auto wo_semicolons = make_input("", "a \n", "", depth); const char* src_w_semicolons = w_semicolons.c_str(); const char* src_wo_semicolons = wo_semicolons.c_str(); size_t src_len = w_semicolons.size(); JSIdentifierCtxStub ident_ctx_mock; - JSNormalizer normalizer_wo_ident(ident_ctx_mock, UNLIM_DEPTH, MAX_TEMPLATE_NESTING, DEPTH); + JSNormalizer normalizer_wo_ident(ident_ctx_mock, unlim_depth, max_template_nesting, depth); REQUIRE(norm_ret(normalizer_wo_ident, w_semicolons) == JSTokenizer::SCRIPT_ENDED); BENCHMARK("without semicolon insertion") diff --git a/src/utils/test/js_test_utils.cc b/src/utils/test/js_test_utils.cc index 62914ffb8..71f2e333c 100644 --- a/src/utils/test/js_test_utils.cc +++ b/src/utils/test/js_test_utils.cc @@ -42,7 +42,7 @@ void test_scope(const char* context, std::list stack) { std::string buf(context); buf += ""; - JSIdentifierCtx ident_ctx(norm_depth, max_scope_depth, s_ident_built_in); + JSIdentifierCtx ident_ctx(norm_depth, max_scope_depth, s_ignored_ids); JSNormalizer normalizer(ident_ctx, norm_depth, max_template_nesting, max_bracket_depth); normalizer.normalize(buf.c_str(), buf.size()); CHECK(ident_ctx.get_types() == stack); @@ -50,7 +50,7 @@ void test_scope(const char* context, std::list stack) void test_normalization(const char* source, const char* expected) { - JSIdentifierCtx ident_ctx(norm_depth, max_scope_depth, s_ident_built_in); + JSIdentifierCtx ident_ctx(norm_depth, max_scope_depth, s_ignored_ids); JSNormalizer normalizer(ident_ctx, norm_depth, max_template_nesting, max_bracket_depth); normalizer.normalize(source, strlen(source)); std::string result_buf(normalizer.get_script(), normalizer.script_size()); @@ -59,7 +59,7 @@ void test_normalization(const char* source, const char* expected) void test_normalization_bad(const char* source, const char* expected, JSTokenizer::JSRet eret) { - JSIdentifierCtx ident_ctx(norm_depth, max_scope_depth, s_ident_built_in); + JSIdentifierCtx ident_ctx(norm_depth, max_scope_depth, s_ignored_ids); JSNormalizer normalizer(ident_ctx, norm_depth, max_template_nesting, max_bracket_depth); auto ret = normalizer.normalize(source, strlen(source)); std::string result_buf(normalizer.get_script(), normalizer.script_size()); @@ -69,7 +69,7 @@ void test_normalization_bad(const char* source, const char* expected, JSTokenize void test_normalization(const std::vector& pdus) { - JSIdentifierCtx ident_ctx(norm_depth, max_scope_depth, s_ident_built_in); + JSIdentifierCtx ident_ctx(norm_depth, max_scope_depth, s_ignored_ids); JSNormalizer normalizer(ident_ctx, norm_depth, max_template_nesting, max_bracket_depth); for (const auto& pdu : pdus) @@ -84,7 +84,7 @@ void test_normalization(const std::vector& pdus) void test_normalization(std::list pdus) { - JSIdentifierCtx ident_ctx(norm_depth, max_scope_depth, s_ident_built_in); + JSIdentifierCtx ident_ctx(norm_depth, max_scope_depth, s_ignored_ids); JSNormalizer normalizer(ident_ctx, norm_depth, max_template_nesting, max_bracket_depth); for (auto pdu:pdus) { diff --git a/src/utils/test/js_test_utils.h b/src/utils/test/js_test_utils.h index 89f962df6..504ccdae5 100644 --- a/src/utils/test/js_test_utils.h +++ b/src/utils/test/js_test_utils.h @@ -29,10 +29,12 @@ #include "utils/js_identifier_ctx.h" #include "utils/js_normalizer.h" -constexpr int norm_depth=65535; -constexpr int max_template_nesting=4; -constexpr int max_bracket_depth=256; -constexpr int max_scope_depth=256; +constexpr int unlim_depth = -1; +constexpr int norm_depth = 65535; +constexpr int max_template_nesting = 4; +constexpr int max_bracket_depth = 256; +constexpr int max_scope_depth = 256; +static const std::unordered_set s_ignored_ids { "console", "eval", "document" }; namespace snort { @@ -58,8 +60,6 @@ public: size_t size() const override { return 0; } }; -static const std::unordered_set s_ident_built_in { "console", "eval", "document" }; - void test_scope(const char* context, std::list stack); void test_normalization(const char* source, const char* expected); void test_normalization_bad(const char* source, const char* expected, JSTokenizer::JSRet eret);