"Fossies" - the Fresh Open Source Software Archive  

Source code changes of the file "src/utils/test/js_normalizer_test.cc" between
snort3-3.1.29.0.tar.gz and snort3-3.1.30.0.tar.gz

About: Snort 3 is a network intrusion prevention and detection system (IDS/IPS) combining the benefits of signature, protocol and anomaly-based inspection.

js_normalizer_test.cc  (snort3-3.1.29.0):js_normalizer_test.cc  (snort3-3.1.30.0)
skipping to change at line 116 skipping to change at line 116
{ \ { \
const char end[] = "</script>"; \ const char end[] = "</script>"; \
auto ret = norm.normalize(end, sizeof(end) - 1); \ auto ret = norm.normalize(end, sizeof(end) - 1); \
CHECK(ret == JSTokenizer::SCRIPT_ENDED); \ CHECK(ret == JSTokenizer::SCRIPT_ENDED); \
} }
#define NORMALIZE_S(src1, exp1) \ #define NORMALIZE_S(src1, exp1) \
{ \ { \
char dst1[sizeof(exp1)]; \ char dst1[sizeof(exp1)]; \
\ \
JSIdentifierCtx ident_ctx(norm_depth, max_scope_depth, s_ignored_ids); \ JSIdentifierCtx ident_ctx(norm_depth, max_scope_depth, s_ignored_ids, s_ ignored_props); \
JSNormalizer norm(ident_ctx, norm_depth, max_template_nesting, max_brack et_depth); \ JSNormalizer norm(ident_ctx, norm_depth, max_template_nesting, max_brack et_depth); \
\ \
DO(src1, sizeof(src1) - 1, dst1, sizeof(dst1) - 1); \ DO(src1, sizeof(src1) - 1, dst1, sizeof(dst1) - 1); \
CHECK(!memcmp(exp1, dst1, sizeof(exp1) - 1)); \ CHECK(!memcmp(exp1, dst1, sizeof(exp1) - 1)); \
\ \
CLOSE(); \ CLOSE(); \
} }
#define NORMALIZE_T(src1, src2, exp1, exp2) \ #define NORMALIZE_T(src1, src2, exp1, exp2) \
{ \ { \
char dst1[sizeof(exp1)]; \ char dst1[sizeof(exp1)]; \
char dst2[sizeof(exp2)]; \ char dst2[sizeof(exp2)]; \
\ \
JSIdentifierCtx ident_ctx(norm_depth, max_scope_depth, s_ignored_ids); \ JSIdentifierCtx ident_ctx(norm_depth, max_scope_depth, s_ignored_ids, s_ ignored_props); \
JSNormalizer norm(ident_ctx, norm_depth, max_template_nesting, max_brack et_depth); \ JSNormalizer norm(ident_ctx, norm_depth, max_template_nesting, max_brack et_depth); \
\ \
DO(src1, sizeof(src1) - 1, dst1, sizeof(dst1) - 1); \ DO(src1, sizeof(src1) - 1, dst1, sizeof(dst1) - 1); \
CHECK(!memcmp(exp1, dst1, sizeof(exp1) - 1)); \ CHECK(!memcmp(exp1, dst1, sizeof(exp1) - 1)); \
\ \
DO(src2, sizeof(src2) - 1, dst2, sizeof(dst2) - 1); \ DO(src2, sizeof(src2) - 1, dst2, sizeof(dst2) - 1); \
CHECK(!memcmp(exp2, dst2, sizeof(exp2) - 1)); \ CHECK(!memcmp(exp2, dst2, sizeof(exp2) - 1)); \
\ \
CLOSE(); \ CLOSE(); \
} }
skipping to change at line 365 skipping to change at line 365
\ \
size_t act_len = norm.script_size(); \ size_t act_len = norm.script_size(); \
REQUIRE(act_len == sizeof(exp) - 1); \ REQUIRE(act_len == sizeof(exp) - 1); \
\ \
const char* dst = norm.get_script(); \ const char* dst = norm.get_script(); \
CHECK(!memcmp(exp, dst, sizeof(exp) - 1)); \ CHECK(!memcmp(exp, dst, sizeof(exp) - 1)); \
} }
#define NORM_COMBINED_S_2(src1, src2, exp) \ #define NORM_COMBINED_S_2(src1, src2, exp) \
{ \ { \
JSIdentifierCtx ident_ctx(norm_depth, max_scope_depth, s_ignored_ids); \ JSIdentifierCtx ident_ctx(norm_depth, max_scope_depth, s_ignored_ids, s_ ignored_props); \
JSNormalizer norm(ident_ctx, norm_depth, max_template_nesting, max_brack et_depth); \ JSNormalizer norm(ident_ctx, norm_depth, max_template_nesting, max_brack et_depth); \
\ \
auto ret = norm.normalize(src1, sizeof(src1) - 1); \ auto ret = norm.normalize(src1, sizeof(src1) - 1); \
REQUIRE(ret == JSTokenizer::SCRIPT_CONTINUE); \ REQUIRE(ret == JSTokenizer::SCRIPT_CONTINUE); \
\ \
ret = norm.normalize(src2, sizeof(src2) - 1); \ ret = norm.normalize(src2, sizeof(src2) - 1); \
REQUIRE(ret == JSTokenizer::SCRIPT_CONTINUE); \ REQUIRE(ret == JSTokenizer::SCRIPT_CONTINUE); \
\ \
const char end[] = "</script>"; \ const char end[] = "</script>"; \
ret = norm.normalize(end, sizeof(end) - 1); \ ret = norm.normalize(end, sizeof(end) - 1); \
skipping to change at line 675 skipping to change at line 675
static const char all_patterns_expected8[] = static const char all_patterns_expected8[] =
"a=\" \\\" \\\\\\\" \\\\\";" "a=\" \\\" \\\\\\\" \\\\\";"
"b=` \\` \\\\\\` \\\\`;" "b=` \\` \\\\\\` \\\\`;"
"c=' \\' \\\\\\' \\\\';" "c=' \\' \\\\\\' \\\\';"
"d=/ \\/ \\\\\\/ \\\\/;" "d=/ \\/ \\\\\\/ \\\\/;"
"a+b;"; "a+b;";
static const char all_patterns_buf9[] = static const char all_patterns_buf9[] =
"var r = /^(?:(?:https?|mailto|ftp):|[^:/?#]*(?:[/?#]|$))/i;" "var r = /^(?:(?:https?|mailto|ftp):|[^:/?#]*(?:[/?#]|$))/i;"
"new Lb(function(a){return /^[^:]*([/?#]|$)/.test(a)})"; "new Lb(function(a){return /^[^:]*([/?#]|$)/.test(a)});"
"pa=/^((https:)?\\/\\/[0-9a-z.:[\\]-]+\\/|\\/[^/\\\\]|"
"[^:/\\\\%]+\\/|[^:/\\\\%]*[?#]|about:blank#)/i;"
"/[/ a b c / 1]/ a b c / 1;";
static const char all_patterns_expected9[] = static const char all_patterns_expected9[] =
"var r=/^(?:(?:https?|mailto|ftp):|[^:/?#]*(?:[/?#]|$))/i;" "var r=/^(?:(?:https?|mailto|ftp):|[^:/?#]*(?:[/?#]|$))/i;"
"new Lb(function(a){return /^[^:]*([/?#]|$)/.test(a)})"; "new Lb(function(a){return /^[^:]*([/?#]|$)/.test(a)});"
"pa=/^((https:)?\\/\\/[0-9a-z.:[\\]-]+\\/|\\/[^/\\\\]"
"|[^:/\\\\%]+\\/|[^:/\\\\%]*[?#]|about:blank#)/i;"
"/[/ a b c / 1]/ a b c/1;";
TEST_CASE("all patterns", "[JSNormalizer]") TEST_CASE("all patterns", "[JSNormalizer]")
{ {
SECTION("whitespaces and special characters") SECTION("whitespaces and special characters")
{ {
NORMALIZE(all_patterns_buf0); NORMALIZE(all_patterns_buf0);
VALIDATE(all_patterns_buf0, all_patterns_expected0); VALIDATE(all_patterns_buf0, all_patterns_expected0);
} }
SECTION("comments") SECTION("comments")
{ {
skipping to change at line 1106 skipping to change at line 1112
static const char syntax_cases_expected22[] = static const char syntax_cases_expected22[] =
"tag `template\n \\\\\\${ } \\\\${a+` template ${1+c}`}`"; "tag `template\n \\\\\\${ } \\\\${a+` template ${1+c}`}`";
static const char syntax_cases_buf23[] = static const char syntax_cases_buf23[] =
"`${`${`${`${`${}`}`}`}`}`}"; "`${`${`${`${`${}`}`}`}`}`}";
static const char syntax_cases_expected23[] = static const char syntax_cases_expected23[] =
"`${`${`${`${`"; "`${`${`${`${`";
static const char syntax_cases_buf24[] = static const char syntax_cases_buf24[] =
"var a=/[[[[/]]]]/;" "var a=/{{{{/}}}}/;"
"var b=/[[[[[/]]]]]/;"; "var b=/{{{{{/}}}}}/;";
static const char syntax_cases_expected24[] = static const char syntax_cases_expected24[] =
"var a=/[[[[/]]]]/;" "var a=/{{{{/}}}}/;"
"var b=/[[[["; "var b=/{{{{";
static const char syntax_cases_buf25[] =
"return /regex0/.foo + /regex1/.bar ;"
"return /regex2/.baz * /regex3/.qux ;";
static const char syntax_cases_expected25[] =
"return /regex0/.var_0000+/regex1/.var_0001;"
"return /regex2/.var_0002*/regex3/.var_0003;";
TEST_CASE("syntax cases", "[JSNormalizer]") TEST_CASE("syntax cases", "[JSNormalizer]")
{ {
SECTION("variables") SECTION("variables")
{ {
NORMALIZE(syntax_cases_buf0); NORMALIZE(syntax_cases_buf0);
VALIDATE(syntax_cases_buf0, syntax_cases_expected0); VALIDATE(syntax_cases_buf0, syntax_cases_expected0);
} }
SECTION("operators") SECTION("operators")
{ {
skipping to change at line 1195 skipping to change at line 1209
SECTION("white space between '+'<-->'++' and '-'<-->'--'") SECTION("white space between '+'<-->'++' and '-'<-->'--'")
{ {
NORMALIZE(syntax_cases_buf14); NORMALIZE(syntax_cases_buf14);
VALIDATE(syntax_cases_buf14, syntax_cases_expected14); VALIDATE(syntax_cases_buf14, syntax_cases_expected14);
} }
SECTION("template literals") SECTION("template literals")
{ {
NORMALIZE(syntax_cases_buf22); NORMALIZE(syntax_cases_buf22);
VALIDATE(syntax_cases_buf22, syntax_cases_expected22); VALIDATE(syntax_cases_buf22, syntax_cases_expected22);
} }
SECTION("regex after operator")
{
NORMALIZE_S(syntax_cases_buf25, syntax_cases_expected25);
}
} }
TEST_CASE("bad tokens", "[JSNormalizer]") TEST_CASE("bad tokens", "[JSNormalizer]")
{ {
SECTION("LS chars within literal") SECTION("LS chars within literal")
{ {
NORMALIZE(syntax_cases_buf15); NORMALIZE(syntax_cases_buf15);
VALIDATE_FAIL(syntax_cases_buf15, syntax_cases_expected15, JSTokenizer:: BAD_TOKEN, 25); VALIDATE_FAIL(syntax_cases_buf15, syntax_cases_expected15, JSTokenizer:: BAD_TOKEN, 25);
} }
SECTION("PS chars within literal") SECTION("PS chars within literal")
skipping to change at line 2766 skipping to change at line 2784
const char exp1[] = "var_0000 var_0001"; const char exp1[] = "var_0000 var_0001";
const char exp2[] = " var_0002 var_0003"; const char exp2[] = " var_0002 var_0003";
const char exp3[] = " var_0002 var_0003"; const char exp3[] = " var_0002 var_0003";
const char exp4[] = " var_0002 var_0003"; const char exp4[] = " var_0002 var_0003";
char dst1[sizeof(exp1)]; char dst1[sizeof(exp1)];
char dst2[sizeof(exp2)]; char dst2[sizeof(exp2)];
char dst3[sizeof(exp3)]; char dst3[sizeof(exp3)];
char dst4[sizeof(exp4)]; char dst4[sizeof(exp4)];
JSIdentifierCtx ident_ctx(norm_depth, max_scope_depth, s_ignored_ids); JSIdentifierCtx ident_ctx(norm_depth, max_scope_depth, s_ignored_ids, s_ ignored_props);
JSNormalizer norm(ident_ctx, norm_depth, max_template_nesting, max_brack et_depth); JSNormalizer norm(ident_ctx, norm_depth, max_template_nesting, max_brack et_depth);
DO(src1, sizeof(src1) - 1, dst1, sizeof(dst1) - 1); DO(src1, sizeof(src1) - 1, dst1, sizeof(dst1) - 1);
CHECK(!memcmp(exp1, dst1, sizeof(exp1) - 1)); CHECK(!memcmp(exp1, dst1, sizeof(exp1) - 1));
TRY(src2, sizeof(src2) - 1, dst2, sizeof(dst2) - 1, JSTokenizer::SCRIPT_ ENDED); TRY(src2, sizeof(src2) - 1, dst2, sizeof(dst2) - 1, JSTokenizer::SCRIPT_ ENDED);
CHECK(!memcmp(exp2, dst2, sizeof(exp2) - 1)); CHECK(!memcmp(exp2, dst2, sizeof(exp2) - 1));
TRY(src3, sizeof(src3) - 1, dst3, sizeof(dst3) - 1, JSTokenizer::SCRIPT_ ENDED); TRY(src3, sizeof(src3) - 1, dst3, sizeof(dst3) - 1, JSTokenizer::SCRIPT_ ENDED);
CHECK(!memcmp(exp3, dst3, sizeof(exp3) - 1)); CHECK(!memcmp(exp3, dst3, sizeof(exp3) - 1));
skipping to change at line 2801 skipping to change at line 2819
const char exp3[] = ""; const char exp3[] = "";
const char tmp_buf1[] = "<"; const char tmp_buf1[] = "<";
const char tmp_buf2[] = "<!-"; const char tmp_buf2[] = "<!-";
const char tmp_buf3[] = "<!--comment\n"; const char tmp_buf3[] = "<!--comment\n";
char dst1[sizeof(exp1)]; char dst1[sizeof(exp1)];
char dst2[sizeof(exp2)]; char dst2[sizeof(exp2)];
char dst3[sizeof(exp3)]; char dst3[sizeof(exp3)];
JSIdentifierCtx ident_ctx(norm_depth, max_scope_depth, s_ignored_ids); JSIdentifierCtx ident_ctx(norm_depth, max_scope_depth, s_ignored_ids, s_ ignored_props);
JSNormalizer norm(ident_ctx, norm_depth, max_template_nesting, max_brack et_depth); JSNormalizer norm(ident_ctx, norm_depth, max_template_nesting, max_brack et_depth);
TRY(src1, sizeof(src1) - 1, dst1, sizeof(dst1) - 1, JSTokenizer::SCRIPT_ CONTINUE); TRY(src1, sizeof(src1) - 1, dst1, sizeof(dst1) - 1, JSTokenizer::SCRIPT_ CONTINUE);
CHECK(!memcmp(exp1, dst1, sizeof(exp1) - 1)); CHECK(!memcmp(exp1, dst1, sizeof(exp1) - 1));
REQUIRE(norm.get_tmp_buf_size() == sizeof(tmp_buf1) - 1); REQUIRE(norm.get_tmp_buf_size() == sizeof(tmp_buf1) - 1);
CHECK(!memcmp(norm.get_tmp_buf(), tmp_buf1, sizeof(tmp_buf1) - 1)); CHECK(!memcmp(norm.get_tmp_buf(), tmp_buf1, sizeof(tmp_buf1) - 1));
TRY(src2, sizeof(src2) - 1, dst2, sizeof(dst2) - 1, JSTokenizer::SCRIPT_ CONTINUE); TRY(src2, sizeof(src2) - 1, dst2, sizeof(dst2) - 1, JSTokenizer::SCRIPT_ CONTINUE);
CHECK(!memcmp(exp2, dst2, sizeof(exp2) - 1)); CHECK(!memcmp(exp2, dst2, sizeof(exp2) - 1));
REQUIRE(norm.get_tmp_buf_size() == sizeof(tmp_buf2) - 1); REQUIRE(norm.get_tmp_buf_size() == sizeof(tmp_buf2) - 1);
skipping to change at line 3411 skipping to change at line 3429
const char exp3[] = "c=/{{{}}}/;"; const char exp3[] = "c=/{{{}}}/;";
NORMALIZE_1(dat1, exp1); NORMALIZE_1(dat1, exp1);
NORMALIZE_1(dat2, exp2); NORMALIZE_1(dat2, exp2);
NORMALIZE_1(dat3, exp3); NORMALIZE_1(dat3, exp3);
} }
SECTION("square brackets") SECTION("square brackets")
{ {
const char dat1[] = "a=/[]/;"; const char dat1[] = "a=/[]/;";
const char dat2[] = "b=/[][][]/;"; const char dat2[] = "b=/[][][]/;";
const char dat3[] = "c=/[[[]]]/;"; const char dat3[] = "c=/[[[[[]/;";
const char dat4[] = "d=/[/]/;";
const char exp1[] = "a=/[]/;"; const char exp1[] = "a=/[]/;";
const char exp2[] = "b=/[][][]/;"; const char exp2[] = "b=/[][][]/;";
const char exp3[] = "c=/[[[]]]/;"; const char exp3[] = "c=/[[[[[]/;";
const char exp4[] = "d=/[/]/;";
NORMALIZE_1(dat1, exp1); NORMALIZE_1(dat1, exp1);
NORMALIZE_1(dat2, exp2); NORMALIZE_1(dat2, exp2);
NORMALIZE_1(dat3, exp3); NORMALIZE_1(dat3, exp3);
NORMALIZE_1(dat4, exp4);
} }
SECTION("mix of brackets") SECTION("mix of brackets")
{ {
const char dat1[] = "a=/(){}[]/"; const char dat1[] = "a=/(){}[]/";
const char dat2[] = "b=/({})[]/"; const char dat2[] = "b=/({})[]/";
const char dat3[] = "c=/(){[]}/"; const char dat3[] = "c=/(){[]}/";
const char exp1[] = "a=/(){}[]/"; const char exp1[] = "a=/(){}[]/";
const char exp2[] = "b=/({})[]/"; const char exp2[] = "b=/({})[]/";
const char exp3[] = "c=/(){[]}/"; const char exp3[] = "c=/(){[]}/";
NORMALIZE_1(dat1, exp1); NORMALIZE_1(dat1, exp1);
NORMALIZE_1(dat2, exp2); NORMALIZE_1(dat2, exp2);
NORMALIZE_1(dat3, exp3); NORMALIZE_1(dat3, exp3);
} }
SECTION("parentheses - wrong closing symbol") SECTION("parentheses - wrong closing symbol")
{ {
const char dat1[] = "/({[ (} ]})/"; const char dat1[] = "/({ (} })/";
const char dat2[] = "/({[ (] ]})/"; const char dat2[] = "/({ (] })/";
const char exp1[] = "/({[ ("; const char exp1[] = "/({ (";
const char exp2[] = "/({[ ("; const char exp2[] = "/({ (";
NORM_BAD_1(dat1, exp1, JSTokenizer::BAD_TOKEN); NORM_BAD_1(dat1, exp1, JSTokenizer::BAD_TOKEN);
NORM_BAD_1(dat2, exp2, JSTokenizer::BAD_TOKEN); NORM_BAD_1(dat2, exp2, JSTokenizer::BAD_TOKEN);
} }
SECTION("curly braces - wrong closing symbol") SECTION("curly braces - wrong closing symbol")
{ {
const char dat1[] = "/({[ {) ]})/"; const char dat1[] = "/({ {) })/";
const char dat2[] = "/({[ {] ]})/"; const char dat2[] = "/({ {] })/";
const char exp1[] = "/({[ {"; const char exp1[] = "/({ {";
const char exp2[] = "/({[ {"; const char exp2[] = "/({ {";
NORM_BAD_1(dat1, exp1, JSTokenizer::BAD_TOKEN); NORM_BAD_1(dat1, exp1, JSTokenizer::BAD_TOKEN);
NORM_BAD_1(dat2, exp2, JSTokenizer::BAD_TOKEN); NORM_BAD_1(dat2, exp2, JSTokenizer::BAD_TOKEN);
} }
SECTION("square brackets - wrong closing symbol") SECTION("square brackets - raw bracket")
{ {
const char dat1[] = "/([{ [) }])/"; const char dat1[] = "/]/";
const char dat2[] = "/([{ [} }])/"; const char dat2[] = "/[]]/";
const char exp1[] = "/([{ ["; const char dat3[] = "/][]]/g";
const char exp2[] = "/([{ ["; const char exp1[] = "/]/";
const char exp2[] = "/[]]/";
const char exp3[] = "/][]]/g";
NORM_BAD_1(dat1, exp1, JSTokenizer::BAD_TOKEN); NORMALIZE_1(dat1, exp1);
NORM_BAD_1(dat2, exp2, JSTokenizer::BAD_TOKEN); NORMALIZE_1(dat2, exp2);
NORMALIZE_1(dat3, exp3);
} }
SECTION("parentheses - mismatch") SECTION("parentheses - mismatch")
{ {
const char dat1[] = "/)/"; const char dat1[] = "/)/";
const char dat2[] = "/())/"; const char dat2[] = "/())/";
const char dat3[] = "/({[ ()) ]})/"; const char dat3[] = "/({{ ()) }})/";
const char exp1[] = "/"; const char exp1[] = "/";
const char exp2[] = "/()"; const char exp2[] = "/()";
const char exp3[] = "/({[ ()"; const char exp3[] = "/({{ ()";
NORM_BAD_1(dat1, exp1, JSTokenizer::BAD_TOKEN); NORM_BAD_1(dat1, exp1, JSTokenizer::BAD_TOKEN);
NORM_BAD_1(dat2, exp2, JSTokenizer::BAD_TOKEN); NORM_BAD_1(dat2, exp2, JSTokenizer::BAD_TOKEN);
NORM_BAD_1(dat3, exp3, JSTokenizer::BAD_TOKEN); NORM_BAD_1(dat3, exp3, JSTokenizer::BAD_TOKEN);
} }
SECTION("curly braces - mismatch") SECTION("curly braces - mismatch")
{ {
const char dat1[] = "/}/"; const char dat1[] = "/}/";
const char dat2[] = "/{}}/"; const char dat2[] = "/{}}/";
const char dat3[] = "/({[ {}} ]})/"; const char dat3[] = "/({( {}} )})/";
const char exp1[] = "/"; const char exp1[] = "/";
const char exp2[] = "/{}"; const char exp2[] = "/{}";
const char exp3[] = "/({[ {}"; const char exp3[] = "/({( {}";
NORM_BAD_1(dat1, exp1, JSTokenizer::BAD_TOKEN);
NORM_BAD_1(dat2, exp2, JSTokenizer::BAD_TOKEN);
NORM_BAD_1(dat3, exp3, JSTokenizer::BAD_TOKEN);
}
SECTION("square brackets - mismatch")
{
const char dat1[] = "/]/";
const char dat2[] = "/[]]/";
const char dat3[] = "/([{ []] }])/";
const char exp1[] = "/";
const char exp2[] = "/[]";
const char exp3[] = "/([{ []";
NORM_BAD_1(dat1, exp1, JSTokenizer::BAD_TOKEN); NORM_BAD_1(dat1, exp1, JSTokenizer::BAD_TOKEN);
NORM_BAD_1(dat2, exp2, JSTokenizer::BAD_TOKEN); NORM_BAD_1(dat2, exp2, JSTokenizer::BAD_TOKEN);
NORM_BAD_1(dat3, exp3, JSTokenizer::BAD_TOKEN); NORM_BAD_1(dat3, exp3, JSTokenizer::BAD_TOKEN);
} }
SECTION("parentheses - continuation") SECTION("parentheses - continuation")
{ {
const char dat1[] = "/(("; const char dat1[] = "/((";
const char dat2[] = "))/"; const char dat2[] = "))/";
const char exp1[] = "/(("; const char exp1[] = "/((";
skipping to change at line 3527 skipping to change at line 3538
const char exp1[] = "/{{"; const char exp1[] = "/{{";
const char exp2[] = "}}/"; const char exp2[] = "}}/";
const char exp[] = "/{{}}/"; const char exp[] = "/{{}}/";
NORMALIZE_2(dat1, dat2, exp1, exp2); NORMALIZE_2(dat1, dat2, exp1, exp2);
NORM_COMBINED_2(dat1, dat2, exp); NORM_COMBINED_2(dat1, dat2, exp);
} }
SECTION("square brackets - continuation") SECTION("square brackets - continuation")
{ {
const char dat1[] = "/[["; const char dat1[] = "/[[";
const char dat2[] = "]]/"; const char dat2[] = "[]/";
const char exp1[] = "/[["; const char exp1[] = "/[[";
const char exp2[] = "]]/"; const char exp2[] = "[]/";
const char exp[] = "/[[]]/"; const char exp[] = "/[[[]/";
NORMALIZE_2(dat1, dat2, exp1, exp2); NORMALIZE_2(dat1, dat2, exp1, exp2);
NORM_COMBINED_2(dat1, dat2, exp); NORM_COMBINED_2(dat1, dat2, exp);
} }
SECTION("parentheses - mismatch in continuation") SECTION("parentheses - mismatch in continuation")
{ {
const char dat1[] = "/("; const char dat1[] = "/(";
const char dat2[] = "))/"; const char dat2[] = "))/";
const char exp1[] = "/("; const char exp1[] = "/(";
const char exp2[] = ")"; const char exp2[] = ")";
skipping to change at line 3557 skipping to change at line 3568
{ {
const char dat1[] = "/{"; const char dat1[] = "/{";
const char dat2[] = "}}/"; const char dat2[] = "}}/";
const char exp1[] = "/{"; const char exp1[] = "/{";
const char exp2[] = "}"; const char exp2[] = "}";
const char exp[] = "/{}"; const char exp[] = "/{}";
NORM_BAD_2(dat1, dat2, exp1, exp2, JSTokenizer::BAD_TOKEN); NORM_BAD_2(dat1, dat2, exp1, exp2, JSTokenizer::BAD_TOKEN);
NORM_COMBINED_BAD_2(dat1, dat2, exp, JSTokenizer::BAD_TOKEN); NORM_COMBINED_BAD_2(dat1, dat2, exp, JSTokenizer::BAD_TOKEN);
} }
SECTION("square brackets - mismatch in continuation")
{
const char dat1[] = "/[";
const char dat2[] = "]]/";
const char exp1[] = "/[";
const char exp2[] = "]";
const char exp[] = "/[]";
NORM_BAD_2(dat1, dat2, exp1, exp2, JSTokenizer::BAD_TOKEN);
NORM_COMBINED_BAD_2(dat1, dat2, exp, JSTokenizer::BAD_TOKEN);
}
} }
TEST_CASE("ignored identifiers", "[JSNormalizer]") TEST_CASE("ignored identifiers", "[JSNormalizer]")
{ {
// 'console' 'eval' 'document' are in the ignore list // 'console' 'eval' 'document' are in the ignore list
SECTION("a standalone identifier") SECTION("a standalone identifier")
{ {
const char dat1[] = "alpha bravo console delta eval"; const char dat1[] = "alpha bravo console delta eval";
const char dat2[] = "var a = 0; console = 1;"; const char dat2[] = "var a = 0; console = 1;";
skipping to change at line 4040 skipping to change at line 4040
const char exp5[] = "var_0000[1]=eval"; const char exp5[] = "var_0000[1]=eval";
NORMALIZE_S(dat1, exp1); NORMALIZE_S(dat1, exp1);
NORMALIZE_S(dat2, exp2); NORMALIZE_S(dat2, exp2);
NORMALIZE_S(dat3, exp3); NORMALIZE_S(dat3, exp3);
NORMALIZE_S(dat4, exp4); NORMALIZE_S(dat4, exp4);
NORMALIZE_S(dat5, exp5); NORMALIZE_S(dat5, exp5);
} }
} }
TEST_CASE("ignored properties", "[JSNormalizer]")
{
SECTION("basic")
{
const char dat1[] = "foo.bar ;";
const char dat2[] = "foo.bar() ;";
const char dat3[] = "foo.watch ;";
const char dat4[] = "foo.unwatch() ;";
const char dat5[] = "console.watch ;";
const char dat6[] = "console.unwatch() ;";
const char dat7[] = "console.foo.watch ;";
const char dat8[] = "console.foo.unwatch() ;";
const char dat9[] = "foo.console.watch ;";
const char dat10[] = "foo.console.unwatch() ;";
const char dat11[] = "foo['bar'] ;";
const char dat12[] = "foo[\"bar\"]() ;";
const char dat13[] = "foo['watch'] ;";
const char dat14[] = "foo[\"unwatch\"]() ;";
const char dat15[] = "console['watch'] ;";
const char dat16[] = "console[\"unwatch\"]() ;";
const char dat17[] = "console['foo']['watch'] ;";
const char dat18[] = "console[\"foo\"][\"unwatch\"]() ;";
const char dat19[] = "foo['console']['watch'] ;";
const char dat20[] = "foo[\"console\"][\"unwatch\"]() ;";
const char exp1[] = "var_0000.var_0001;";
const char exp2[] = "var_0000.var_0001();";
const char exp3[] = "var_0000.watch;";
const char exp4[] = "var_0000.unwatch();";
const char exp5[] = "console.watch;";
const char exp6[] = "console.unwatch();";
const char exp7[] = "console.foo.watch;";
const char exp8[] = "console.foo.unwatch();";
const char exp9[] = "var_0000.var_0001.watch;";
const char exp10[] = "var_0000.var_0001.unwatch();";
const char exp11[] = "var_0000['bar'];";
const char exp12[] = "var_0000[\"bar\"]();";
const char exp13[] = "var_0000['watch'];";
const char exp14[] = "var_0000[\"unwatch\"]();";
const char exp15[] = "console['watch'];";
const char exp16[] = "console[\"unwatch\"]();";
const char exp17[] = "console['foo']['watch'];";
const char exp18[] = "console[\"foo\"][\"unwatch\"]();";
const char exp19[] = "var_0000['console']['watch'];";
const char exp20[] = "var_0000[\"console\"][\"unwatch\"]();";
NORMALIZE_S(dat1, exp1);
NORMALIZE_S(dat2, exp2);
NORMALIZE_S(dat3, exp3);
NORMALIZE_S(dat4, exp4);
NORMALIZE_S(dat5, exp5);
NORMALIZE_S(dat6, exp6);
NORMALIZE_S(dat7, exp7);
NORMALIZE_S(dat8, exp8);
NORMALIZE_S(dat9, exp9);
NORMALIZE_S(dat10, exp10);
NORMALIZE_S(dat11, exp11);
NORMALIZE_S(dat12, exp12);
NORMALIZE_S(dat13, exp13);
NORMALIZE_S(dat14, exp14);
NORMALIZE_S(dat15, exp15);
NORMALIZE_S(dat16, exp16);
NORMALIZE_S(dat17, exp17);
NORMALIZE_S(dat18, exp18);
NORMALIZE_S(dat19, exp19);
NORMALIZE_S(dat20, exp20);
}
SECTION("chain tracking")
{
const char dat1[] = "foo.watch.bar ;";
const char dat2[] = "foo['watch'].bar ;";
const char dat3[] = "foo.bar.watch.bar ;";
const char dat4[] = "foo['bar'].watch['bar'] ;";
const char dat5[] = "foo['bar'].watch['bar'].baz ;";
const char dat6[] = "foo.unwatch().bar ;";
const char dat7[] = "foo['unwatch']().bar ;";
const char dat8[] = "foo.bar.unwatch().bar ;";
const char dat9[] = "foo['bar'].unwatch()['bar'] ;";
const char dat10[] = "foo['bar'].unwatch()['bar'].baz ;";
const char dat11[] = "foo . watch \n . bar ;";
const char dat12[] = "foo ['watch'] \n . bar ;";
const char dat13[] = "foo . /*multiline*/ watch //oneline\n . bar ;";
const char dat14[] = "foo . unwatch () \n . bar ;";
const char dat15[] = "foo ['unwatch'] () \n . bar ;";
const char dat16[] = "foo /*multiline*/ . unwatch ( ) . // oneline \n ba
r ;";
const char dat17[] = "foo . + watch . bar ;";
const char dat18[] = "foo . + ['watch'] . bar ;";
const char dat19[] = "foo . + unwatch() . bar ;";
const char dat20[] = "foo . + ['unwatch']() . bar ;";
// FIXIT-L: add support for proper tracking of bracket accessors.
// Current behavior: foo['watch'].bar -> var_0000['watch'].var_0001
// Expected behavior: foo['watch'].bar -> var_0000['watch'].bar
const char exp1[] = "var_0000.watch.bar;";
const char exp2[] = "var_0000['watch'].var_0001;";
const char exp3[] = "var_0000.var_0001.watch.bar;";
const char exp4[] = "var_0000['bar'].watch['bar'];";
const char exp5[] = "var_0000['bar'].watch['bar'].baz;";
const char exp6[] = "var_0000.unwatch().bar;";
const char exp7[] = "var_0000['unwatch']().var_0001;";
const char exp8[] = "var_0000.var_0001.unwatch().bar;";
const char exp9[] = "var_0000['bar'].unwatch()['bar'];";
const char exp10[] = "var_0000['bar'].unwatch()['bar'].baz;";
const char exp11[] = "var_0000.watch.bar;";
const char exp12[] = "var_0000['watch'].var_0001;";
const char exp13[] = "var_0000.watch.bar;";
const char exp14[] = "var_0000.unwatch().bar;";
const char exp15[] = "var_0000['unwatch']().var_0001;";
const char exp16[] = "var_0000.unwatch().bar;";
const char exp17[] = "var_0000.+var_0001.var_0002;";
const char exp18[] = "var_0000.+['watch'].var_0001;";
const char exp19[] = "var_0000.+var_0001().var_0002;";
const char exp20[] = "var_0000.+['unwatch']().var_0001;";
NORMALIZE_S(dat1, exp1);
NORMALIZE_S(dat2, exp2);
NORMALIZE_S(dat3, exp3);
NORMALIZE_S(dat4, exp4);
NORMALIZE_S(dat5, exp5);
NORMALIZE_S(dat6, exp6);
NORMALIZE_S(dat7, exp7);
NORMALIZE_S(dat8, exp8);
NORMALIZE_S(dat9, exp9);
NORMALIZE_S(dat10, exp10);
NORMALIZE_S(dat11, exp11);
NORMALIZE_S(dat12, exp12);
NORMALIZE_S(dat13, exp13);
NORMALIZE_S(dat14, exp14);
NORMALIZE_S(dat15, exp15);
NORMALIZE_S(dat16, exp16);
NORMALIZE_S(dat17, exp17);
NORMALIZE_S(dat18, exp18);
NORMALIZE_S(dat19, exp19);
NORMALIZE_S(dat20, exp20);
}
SECTION("scope tracking")
{
const char dat1[] = "foo.(watch).bar ;";
const char dat2[] = "foo(['watch']).bar ;";
const char dat3[] = "foo.bar(baz.unwatch.eval).eval ;";
const char dat4[] = "foo.bar(baz['unwatch'].eval).eval ;";
const char exp1[] = "var_0000.(var_0001).var_0002;";
const char exp2[] = "var_0000(['watch']).var_0001;";
const char exp3[] = "var_0000.var_0001(var_0002.unwatch.eval).var_0003;"
;
const char exp4[] = "var_0000.var_0001(var_0002['unwatch'].var_0003).var
_0003;";
NORMALIZE_S(dat1, exp1);
NORMALIZE_S(dat2, exp2);
NORMALIZE_S(dat3, exp3);
NORMALIZE_S(dat4, exp4);
}
SECTION("corner cases")
{
const char dat1[] = ".watch ;";
const char dat2[] = ".unwatch() ;";
const char dat3[] = "'foo'.watch ;";
const char dat4[] = "\"foo\".unwatch() ;";
const char dat5[] = "''.split('').reverse().join('') ;";
const char dat6[] = "\"\".split(\"\").reverse().join(\"\") ;";
const char dat7[] = "watch () ;";
const char dat8[] = "watch.watch() ;";
// 'name' is present in both ignore lists
const char dat9[] = "name.foo ;";
const char dat10[] = "foo.name ;";
const char dat11[] = "name.name ;";
const char dat12[] = "name ;";
const char dat13[] = "foo.foo ;";
const char dat14[] = "console.console; console;";
const char dat15[] = "watch.watch; watch;";
const char dat16[] = "foo.console; console.foo; foo.watch; watch.foo ;";
const char dat17[] = "console.foo; foo.console; watch.foo; foo.watch ;";
const char dat18[] = "a.a ;";
const char dat19[] = "u.u; u;";
const char dat20[] = "w.w; w;";
const char dat21[] = "a.u; u.a; a.w; w.a ;";
const char dat22[] = "u.a; a.u; w.a; a.w ;";
const char exp1[] = ".watch;";
const char exp2[] = ".unwatch();";
const char exp3[] = "'foo'.watch;";
const char exp4[] = "\"foo\".unwatch();";
const char exp5[] = "''.split('').reverse().join('');";
const char exp6[] = "\"\".split(\"\").reverse().join(\"\");";
const char exp7[] = "var_0000();";
const char exp8[] = "var_0000.watch();";
const char exp9[] = "name.foo;";
const char exp10[] = "var_0000.name;";
const char exp11[] = "name.name;";
const char exp12[] = "name;";
const char exp13[] = "var_0000.var_0000;";
const char exp14[] = "console.console;console;";
const char exp15[] = "var_0000.watch;var_0000;";
const char exp16[] = "var_0000.var_0001;console.foo;var_0000.watch;var_0
002.var_0000;";
const char exp17[] = "console.foo;var_0000.var_0001;var_0002.var_0000;va
r_0000.watch;";
const char exp18[] = "var_0000.var_0000;";
const char exp19[] = "u.u;u;";
const char exp20[] = "var_0000.w;var_0000;";
const char exp21[] = "var_0000.var_0001;u.a;var_0000.w;var_0002.var_0000
;";
const char exp22[] = "u.a;var_0000.var_0001;var_0002.var_0000;var_0000.w
;";
NORMALIZE_S(dat1, exp1);
NORMALIZE_S(dat2, exp2);
NORMALIZE_S(dat3, exp3);
NORMALIZE_S(dat4, exp4);
NORMALIZE_S(dat5, exp5);
NORMALIZE_S(dat6, exp6);
NORMALIZE_S(dat7, exp7);
NORMALIZE_S(dat8, exp8);
NORMALIZE_S(dat9, exp9);
NORMALIZE_S(dat10, exp10);
NORMALIZE_S(dat11, exp11);
NORMALIZE_S(dat12, exp12);
NORMALIZE_S(dat13, exp13);
NORMALIZE_S(dat14, exp14);
NORMALIZE_S(dat15, exp15);
NORMALIZE_S(dat16, exp16);
NORMALIZE_S(dat17, exp17);
NORMALIZE_S(dat18, exp18);
NORMALIZE_S(dat19, exp19);
NORMALIZE_S(dat20, exp20);
NORMALIZE_S(dat21, exp21);
NORMALIZE_S(dat22, exp22);
}
}
TEST_CASE("ignored identifier split", "[JSNormalizer]") TEST_CASE("ignored identifier split", "[JSNormalizer]")
{ {
#if JSTOKENIZER_MAX_STATES != 8 #if JSTOKENIZER_MAX_STATES != 8
#error "ignored identifier split" tests are designed for 8 states depth #error "ignored identifier split" tests are designed for 8 states depth
#endif #endif
SECTION("a standalone identifier") SECTION("a standalone identifier")
{ {
const char dat1[] = "con"; const char dat1[] = "con";
skipping to change at line 4232 skipping to change at line 4500
{ {
const char dat1[] = "!eval"; const char dat1[] = "!eval";
const char dat2[] = "()"; const char dat2[] = "()";
const char exp1[] = "!eval"; const char exp1[] = "!eval";
const char exp2[] = "()"; const char exp2[] = "()";
NORMALIZE_T(dat1, dat2, exp1, exp2); NORMALIZE_T(dat1, dat2, exp1, exp2);
} }
} }
TEST_CASE("ignored properties split", "[JSNormalizer]")
{
#if JSTOKENIZER_MAX_STATES != 8
#error "ignored properties split" tests are designed for 8 states depth
#endif
SECTION("a standalone property")
{
const char dat1[] = "foo.un";
const char dat2[] = "watch ;";
const char exp1[] = "var_0000.var_0001";
const char exp2[] = "unwatch;";
const char exp_comb_1[] = "var_0000.unwatch;";
const char dat3[] = "foo. un";
const char dat4[] = "watch () ;";
const char exp3[] = "var_0000.var_0001";
const char exp4[] = "unwatch();";
const char exp_comb_2[] = "var_0000.unwatch();";
const char dat5[] = "fo";
const char dat6[] = "o . watch ;";
const char exp5[] = "var_0000";
const char exp6[] = "var_0001.watch;";
const char exp_comb_3[] = "var_0001.watch;";
const char dat7[] = "foo. ";
const char dat8[] = "watch ;";
const char exp7[] = "var_0000.";
const char exp8[] = "watch;";
const char exp_comb_4[] = "var_0000.watch;";
const char dat9[] = "foo ";
const char dat10[] = ". watch ;";
const char exp9[] = "var_0000";
const char exp10[] = ".watch;";
const char exp_comb_5[] = "var_0000.watch;";
NORMALIZE_T(dat1, dat2, exp1, exp2);
NORM_COMBINED_S_2(dat1, dat2, exp_comb_1);
NORMALIZE_T(dat3, dat4, exp3, exp4);
NORM_COMBINED_S_2(dat3, dat4, exp_comb_2);
NORMALIZE_T(dat5, dat6, exp5, exp6);
NORM_COMBINED_S_2(dat5, dat6, exp_comb_3);
NORMALIZE_T(dat7, dat8, exp7, exp8);
NORM_COMBINED_S_2(dat7, dat8, exp_comb_4);
NORMALIZE_T(dat9, dat10, exp9, exp10);
NORM_COMBINED_S_2(dat9, dat10, exp_comb_5);
}
SECTION("chain tracking")
{
const char dat1[] = "foo.un";
const char dat2[] = "watch.bar ;";
const char exp1[] = "var_0000.var_0001";
const char exp2[] = "unwatch.bar;";
const char exp_comb_1[] = "var_0000.unwatch.bar;";
const char dat3[] = "foo.un";
const char dat4[] = "watch().bar ;";
const char exp3[] = "var_0000.var_0001";
const char exp4[] = "unwatch().bar;";
const char exp_comb_2[] = "var_0000.unwatch().bar;";
const char dat5[] = "foo['un";
const char dat6[] = "watch'].bar ;";
const char exp5[] = "var_0000['un";
const char exp6[] = "unwatch'].var_0001;";
const char exp_comb_3[] = "var_0000['unwatch'].var_0001;";
const char dat7[] = "foo['un";
const char dat8[] = "watch']().bar ;";
const char exp7[] = "var_0000['un";
const char exp8[] = "unwatch']().var_0001;";
const char exp_comb_4[] = "var_0000['unwatch']().var_0001;";
const char dat9[] = "foo. /*multi";
const char dat10[] = "line*/ watch . bar ;";
const char exp9[] = "var_0000.";
const char exp10[] = "watch.bar;";
const char exp_comb_5[] = "var_0000.watch.bar;";
const char dat11[] = "foo //one";
const char dat12[] = "line \n . watch . bar ;";
const char exp11[] = "var_0000";
const char exp12[] = ".watch.bar;";
const char exp_comb_6[] = "var_0000.watch.bar;";
const char dat13[] = ".";
const char dat14[] = "watch ( ) . bar ;";
const char exp13[] = ".";
const char exp14[] = "watch().bar;";
const char exp_comb_7[] = ".watch().bar;";
const char dat15[] = ".un";
const char dat16[] = "watch ( ) . bar ;";
const char exp15[] = ".var_0000";
const char exp16[] = "unwatch().bar;";
const char exp_comb_8[] = ".unwatch().bar;";
const char dat17[] = "foo.watch ";
const char dat18[] = "+ bar ;";
const char exp17[] = "var_0000.watch";
const char exp18[] = "+var_0001;";
const char exp_comb_9[] = "var_0000.watch+var_0001;";
const char dat19[] = "foo.unwatch ( ) +";
const char dat20[] = "bar ;";
const char exp19[] = "var_0000.unwatch()+";
const char exp20[] = "var_0001;";
const char exp_comb_10[] = "var_0000.unwatch()+var_0001;";
NORMALIZE_T(dat1, dat2, exp1, exp2);
NORM_COMBINED_S_2(dat1, dat2, exp_comb_1);
NORMALIZE_T(dat3, dat4, exp3, exp4);
NORM_COMBINED_S_2(dat3, dat4, exp_comb_2);
NORMALIZE_T(dat5, dat6, exp5, exp6);
NORM_COMBINED_S_2(dat5, dat6, exp_comb_3);
NORMALIZE_T(dat7, dat8, exp7, exp8);
NORM_COMBINED_S_2(dat7, dat8, exp_comb_4);
NORMALIZE_T(dat9, dat10, exp9, exp10);
NORM_COMBINED_S_2(dat9, dat10, exp_comb_5);
NORMALIZE_T(dat11, dat12, exp11, exp12);
NORM_COMBINED_S_2(dat11, dat12, exp_comb_6);
NORMALIZE_T(dat13, dat14, exp13, exp14);
NORM_COMBINED_S_2(dat13, dat14, exp_comb_7);
NORMALIZE_T(dat15, dat16, exp15, exp16);
NORM_COMBINED_S_2(dat15, dat16, exp_comb_8);
NORMALIZE_T(dat17, dat18, exp17, exp18);
NORM_COMBINED_S_2(dat17, dat18, exp_comb_9);
NORMALIZE_T(dat19, dat20, exp19, exp20);
NORM_COMBINED_S_2(dat19, dat20, exp_comb_10);
}
SECTION("scope tracking")
{
const char dat1[] = "foo.(un";
const char dat2[] = "watch).bar ;";
const char exp1[] = "var_0000.(var_0001";
const char exp2[] = "var_0002).var_0003;";
const char exp_comb_1[] = "var_0000.(var_0002).var_0003;";
const char dat3[] = "foo(['un";
const char dat4[] = "watch']).bar ;";
const char exp3[] = "var_0000(['un";
const char exp4[] = "unwatch']).var_0001;";
const char exp_comb_2[] = "var_0000(['unwatch']).var_0001;";
const char dat5[] = "foo.bar(baz.un";
const char dat6[] = "watch() . bar ) . foo ;";
const char exp5[] = "var_0000.var_0001(var_0002.var_0003";
const char exp6[] = "unwatch().bar).var_0000;";
const char exp_comb_3[] = "var_0000.var_0001(var_0002.unwatch().bar).var
_0000;";
const char dat7[] = "foo.bar(baz['un";
const char dat8[] = "watch']() . bar ) . foo ;";
const char exp7[] = "var_0000.var_0001(var_0002['un";
const char exp8[] = "unwatch']().var_0001).var_0000;";
const char exp_comb_4[] = "var_0000.var_0001(var_0002['unwatch']().var_0
001).var_0000;";
NORMALIZE_T(dat1, dat2, exp1, exp2);
NORM_COMBINED_S_2(dat1, dat2, exp_comb_1);
NORMALIZE_T(dat3, dat4, exp3, exp4);
NORM_COMBINED_S_2(dat3, dat4, exp_comb_2);
NORMALIZE_T(dat5, dat6, exp5, exp6);
NORM_COMBINED_S_2(dat5, dat6, exp_comb_3);
NORMALIZE_T(dat7, dat8, exp7, exp8);
NORM_COMBINED_S_2(dat7, dat8, exp_comb_4);
}
}
TEST_CASE("Scope tracking - basic","[JSNormalizer]") TEST_CASE("Scope tracking - basic","[JSNormalizer]")
{ {
SECTION("Global only") SECTION("Global only")
test_scope("",{GLOBAL}); test_scope("",{GLOBAL});
SECTION("Function scope - named function") SECTION("Function scope - named function")
test_scope("function f(){",{GLOBAL,FUNCTION}); test_scope("function f(){",{GLOBAL,FUNCTION});
SECTION("Function scope - anonymous function") SECTION("Function scope - anonymous function")
test_scope("var f = function(){",{GLOBAL,FUNCTION}); test_scope("var f = function(){",{GLOBAL,FUNCTION});
skipping to change at line 4724 skipping to change at line 5180
"{{function", "{{function",
JSTokenizer::WRONG_CLOSING_SYMBOL JSTokenizer::WRONG_CLOSING_SYMBOL
); );
SECTION("scope nesting overflow") SECTION("scope nesting overflow")
{ {
const char src[] = "function() { if (true) { } }"; const char src[] = "function() { if (true) { } }";
const char exp[] = "function(){if"; const char exp[] = "function(){if";
uint32_t scope_depth = 2; uint32_t scope_depth = 2;
JSIdentifierCtx ident_ctx(norm_depth, scope_depth, s_ignored_ids); JSIdentifierCtx ident_ctx(norm_depth, scope_depth, s_ignored_ids, s_igno red_props);
JSNormalizer normalizer(ident_ctx, norm_depth, max_template_nesting, max _bracket_depth); JSNormalizer normalizer(ident_ctx, norm_depth, max_template_nesting, max _bracket_depth);
auto ret = normalizer.normalize(src, strlen(src)); auto ret = normalizer.normalize(src, strlen(src));
std::string dst(normalizer.get_script(), normalizer.script_size()); std::string dst(normalizer.get_script(), normalizer.script_size());
CHECK(ret == JSTokenizer::SCOPE_NESTING_OVERFLOW); CHECK(ret == JSTokenizer::SCOPE_NESTING_OVERFLOW);
CHECK(dst == exp); CHECK(dst == exp);
} }
} }
TEST_CASE("Function call tracking - basic", "[JSNormalizer]") TEST_CASE("Function call tracking - basic", "[JSNormalizer]")
{ {
JSTokenizerTester tester(norm_depth, max_scope_depth, s_ignored_ids, max_tem JSTokenizerTester tester(norm_depth, max_scope_depth, s_ignored_ids, s_ignor
plate_nesting, ed_props,
max_bracket_depth); max_template_nesting, max_bracket_depth);
using FuncType = JSTokenizerTester::FuncType; using FuncType = JSTokenizerTester::FuncType;
SECTION("Global only") SECTION("Global only")
{ {
tester.test_function_scopes({{ "", "", {FuncType::NOT_FUNC}}}); tester.test_function_scopes({{ "", "", {FuncType::NOT_FUNC}}});
} }
SECTION("General function call") SECTION("General function call")
{ {
SECTION("in arguments") SECTION("in arguments")
skipping to change at line 4787 skipping to change at line 5243
SECTION("fake defined function identifier") SECTION("fake defined function identifier")
{ {
tester.test_function_scopes({ tester.test_function_scopes({
{"fake_unescape(", "var_0000(", {FuncType::NOT_FUNC, FuncType::G ENERAL}} {"fake_unescape(", "var_0000(", {FuncType::NOT_FUNC, FuncType::G ENERAL}}
}); });
} }
SECTION("ignored fake defined function identifier") SECTION("ignored fake defined function identifier")
{ {
const std::unordered_set<std::string> s_ignored_ids_fake {"fake_unes cape"}; const std::unordered_set<std::string> s_ignored_ids_fake {"fake_unes cape"};
JSTokenizerTester tester_fake(norm_depth, max_scope_depth, s_ignored _ids_fake, JSTokenizerTester tester_fake(norm_depth, max_scope_depth, s_ignored _ids_fake,
max_template_nesting, max_bracket_depth); s_ignored_props, max_template_nesting, max_bracket_depth);
tester_fake.test_function_scopes({ tester_fake.test_function_scopes({
{"fake_unescape(", "fake_unescape(", {FuncType::NOT_FUNC, FuncTy pe::GENERAL}} {"fake_unescape(", "fake_unescape(", {FuncType::NOT_FUNC, FuncTy pe::GENERAL}}
}); });
} }
SECTION("as a template literal substitution") SECTION("as a template literal substitution")
{ {
tester.test_function_scopes({ tester.test_function_scopes({
{"`unescape ${general(", "`unescape ${var_0000(", {"`unescape ${general(", "`unescape ${var_0000(",
{FuncType::NOT_FUNC, FuncType::NOT_FUNC, FuncType::GENERAL}} {FuncType::NOT_FUNC, FuncType::NOT_FUNC, FuncType::GENERAL}}
}); });
skipping to change at line 5019 skipping to change at line 5475
tester.test_function_scopes({ tester.test_function_scopes({
{"`literal ${String.fromCharCode(", "`literal ${'", {"`literal ${String.fromCharCode(", "`literal ${'",
{FuncType::NOT_FUNC, FuncType::NOT_FUNC, FuncType::CHAR_CODE}} {FuncType::NOT_FUNC, FuncType::NOT_FUNC, FuncType::CHAR_CODE}}
}); });
} }
} }
} }
TEST_CASE("Function call tracking - nesting", "[JSNormalizer]") TEST_CASE("Function call tracking - nesting", "[JSNormalizer]")
{ {
JSTokenizerTester tester(norm_depth, max_scope_depth, s_ignored_ids, max_tem JSTokenizerTester tester(norm_depth, max_scope_depth, s_ignored_ids, s_ignor
plate_nesting, ed_props,
max_bracket_depth); max_template_nesting, max_bracket_depth);
using FuncType = JSTokenizerTester::FuncType; using FuncType = JSTokenizerTester::FuncType;
SECTION("Opening") SECTION("Opening")
{ {
SECTION("Multiple general functions") SECTION("Multiple general functions")
{ {
tester.test_function_scopes({ tester.test_function_scopes({
{ "general( general( general(", "var_0000(var_0000(var_0000(", { "general( general( general(", "var_0000(var_0000(var_0000(",
{ FuncType::NOT_FUNC, FuncType::GENERAL, FuncType::GENERAL, Func Type::GENERAL}} { FuncType::NOT_FUNC, FuncType::GENERAL, FuncType::GENERAL, Func Type::GENERAL}}
skipping to change at line 5111 skipping to change at line 5567
{"general( unescape( String.fromCharCode( 65, 0x42 ) )", {"general( unescape( String.fromCharCode( 65, 0x42 ) )",
"var_0000('AB'", "var_0000('AB'",
{FuncType::NOT_FUNC, FuncType::GENERAL}} {FuncType::NOT_FUNC, FuncType::GENERAL}}
}); });
} }
} }
} }
TEST_CASE("Function call tracking - over multiple PDU", "[JSNormalizer]") TEST_CASE("Function call tracking - over multiple PDU", "[JSNormalizer]")
{ {
JSTokenizerTester tester(norm_depth, max_scope_depth, s_ignored_ids, max_tem JSTokenizerTester tester(norm_depth, max_scope_depth, s_ignored_ids, s_ignor
plate_nesting, ed_props,
max_bracket_depth); max_template_nesting, max_bracket_depth);
using FuncType = JSTokenizerTester::FuncType; using FuncType = JSTokenizerTester::FuncType;
SECTION("split in the middle of the identifier") SECTION("split in the middle of the identifier")
{ {
tester.test_function_scopes({ tester.test_function_scopes({
{"un", "var_0000", {FuncType::NOT_FUNC}}, {"un", "var_0000", {FuncType::NOT_FUNC}},
{"escape", "unescape", {FuncType::NOT_FUNC}}, {"escape", "unescape", {FuncType::NOT_FUNC}},
{"(", "", {FuncType::NOT_FUNC, {"(", "", {FuncType::NOT_FUNC,
FuncType::UNESCAPE}}, FuncType::UNESCAPE}},
skipping to change at line 5312 skipping to change at line 5768
test_normalization("unescape(\"%66%6f%6f\") + \"bar\"", "\"foobar\"" ); test_normalization("unescape(\"%66%6f%6f\") + \"bar\"", "\"foobar\"" );
SECTION("string + unescape + string") SECTION("string + unescape + string")
test_normalization("'foo' + unescape('%62%61%72') + 'baz'", "'foobar baz'"); test_normalization("'foo' + unescape('%62%61%72') + 'baz'", "'foobar baz'");
SECTION("unescape + unescape") SECTION("unescape + unescape")
test_normalization("unescape('%66%6f%6f') + unescape('%62%61%72')", "'foobar'"); test_normalization("unescape('%66%6f%6f') + unescape('%62%61%72')", "'foobar'");
SECTION("inside function call arguments") SECTION("inside function call arguments")
test_normalization("unescape('foo' + '%62' + '%61' + '%72')", "'foob ar'"); test_normalization("unescape('foo' + '%62' + '%61' + '%72')", "'foob ar'");
SECTION("Latin-1 encoding - string + unescape")
test_normalization("'foo ' + unescape('%eb')", "'foo \xEB'");
SECTION("Latin-1 encoding - unescape + string")
test_normalization("unescape('%eb') + ' foo'", "'\xEB foo'");
SECTION("Latin-1 encoding - unescape + unescape")
test_normalization("unescape('%eb') + unescape('%eb')", "'\xEB\xEB'"
);
} }
SECTION("String.fromCharCode") SECTION("String.fromCharCode")
{ {
SECTION("single quoted string + String.fromCharCode") SECTION("single quoted string + String.fromCharCode")
test_normalization("'foo' + String.fromCharCode(98, 97, 114)", "'foo bar'"); test_normalization("'foo' + String.fromCharCode(98, 97, 114)", "'foo bar'");
SECTION("double quoted string + String.fromCharCode") SECTION("double quoted string + String.fromCharCode")
test_normalization("\"foo\" + String.fromCharCode(98, 97, 114)", "\" foobar'"); test_normalization("\"foo\" + String.fromCharCode(98, 97, 114)", "\" foobar'");
SECTION("String.fromCharCode + single quoted string") SECTION("String.fromCharCode + single quoted string")
skipping to change at line 5545 skipping to change at line 6010
max_template_nesting, max_bracket_depth); max_template_nesting, max_bracket_depth);
REQUIRE(norm_ret(normalizer_wo_ident, input) == JSTokenizer::SCRIPT_ENDED); REQUIRE(norm_ret(normalizer_wo_ident, input) == JSTokenizer::SCRIPT_ENDED);
BENCHMARK("without substitution") BENCHMARK("without substitution")
{ {
normalizer_wo_ident.rewind_output(); normalizer_wo_ident.rewind_output();
return normalizer_wo_ident.normalize(input.c_str(), input.size()); return normalizer_wo_ident.normalize(input.c_str(), input.size());
}; };
const std::unordered_set<std::string> ids{}; const std::unordered_set<std::string> ids{};
JSIdentifierCtx ident_ctx(norm_depth, max_scope_depth, ids); const std::unordered_set<std::string> props{};
JSIdentifierCtx ident_ctx(norm_depth, max_scope_depth, ids, props);
JSNormalizer normalizer_w_ident(ident_ctx, unlim_depth, max_template_nesting , max_bracket_depth); JSNormalizer normalizer_w_ident(ident_ctx, unlim_depth, max_template_nesting , max_bracket_depth);
REQUIRE(norm_ret(normalizer_w_ident, input) == JSTokenizer::SCRIPT_ENDED); REQUIRE(norm_ret(normalizer_w_ident, input) == JSTokenizer::SCRIPT_ENDED);
BENCHMARK("with substitution") BENCHMARK("with substitution")
{ {
normalizer_w_ident.rewind_output(); normalizer_w_ident.rewind_output();
return normalizer_w_ident.normalize(input.c_str(), input.size()); return normalizer_w_ident.normalize(input.c_str(), input.size());
}; };
const std::unordered_set<std::string> ids_n { "n" }; const std::unordered_set<std::string> ids_n { "n" };
JSIdentifierCtx ident_ctx_ids_n(norm_depth, max_scope_depth, ids_n); const std::unordered_set<std::string> props_n { "n" };
JSIdentifierCtx ident_ctx_ids_n(norm_depth, max_scope_depth, ids_n, props_n)
;
JSNormalizer normalizer_iids(ident_ctx_ids_n, unlim_depth, JSNormalizer normalizer_iids(ident_ctx_ids_n, unlim_depth,
max_template_nesting, max_bracket_depth); max_template_nesting, max_bracket_depth);
REQUIRE(norm_ret(normalizer_iids, input) == JSTokenizer::SCRIPT_ENDED); REQUIRE(norm_ret(normalizer_iids, input) == JSTokenizer::SCRIPT_ENDED);
BENCHMARK("with ignored identifiers") BENCHMARK("with ignored identifiers")
{ {
normalizer_iids.rewind_output(); normalizer_iids.rewind_output();
return normalizer_iids.normalize(input.c_str(), input.size()); return normalizer_iids.normalize(input.c_str(), input.size());
}; };
} }
skipping to change at line 5642 skipping to change at line 6109
} }
TEST_CASE("JS Normalizer, unescape", "[JSNormalizer]") TEST_CASE("JS Normalizer, unescape", "[JSNormalizer]")
{ {
auto str_unescape = make_input("'", "\\u0061", "'", norm_depth); auto str_unescape = make_input("'", "\\u0061", "'", norm_depth);
auto f_unescape = make_input_repeat("unescape('')", norm_depth); auto f_unescape = make_input_repeat("unescape('')", norm_depth);
const char* src_str_unescape = str_unescape.c_str(); const char* src_str_unescape = str_unescape.c_str();
const char* src_f_unescape = f_unescape.c_str(); const char* src_f_unescape = f_unescape.c_str();
size_t src_len = norm_depth; size_t src_len = norm_depth;
JSIdentifierCtx ident_ctx(norm_depth, max_scope_depth, s_ignored_ids); JSIdentifierCtx ident_ctx(norm_depth, max_scope_depth, s_ignored_ids, s_igno red_props);
JSNormalizer norm(ident_ctx, unlim_depth, max_template_nesting, norm_depth); JSNormalizer norm(ident_ctx, unlim_depth, max_template_nesting, norm_depth);
REQUIRE(norm_ret(norm, str_unescape) == JSTokenizer::SCRIPT_ENDED); REQUIRE(norm_ret(norm, str_unescape) == JSTokenizer::SCRIPT_ENDED);
BENCHMARK("unescape sequence") BENCHMARK("unescape sequence")
{ {
norm.rewind_output(); norm.rewind_output();
return norm.normalize(src_str_unescape, src_len); return norm.normalize(src_str_unescape, src_len);
}; };
REQUIRE(norm_ret(norm, f_unescape) == JSTokenizer::SCRIPT_ENDED); REQUIRE(norm_ret(norm, f_unescape) == JSTokenizer::SCRIPT_ENDED);
 End of changes. 36 change blocks. 
73 lines changed or deleted 551 lines changed or added

Home  |  About  |  Features  |  All  |  Newest  |  Dox  |  Diffs  |  RSS Feeds  |  Screenshots  |  Comments  |  Imprint  |  Privacy  |  HTTP(S)