"Fossies" - the Fresh Open Source Software Archive  

Source code changes of the file "src/utils/test/js_identifier_ctx_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_identifier_ctx_test.cc  (snort3-3.1.29.0):js_identifier_ctx_test.cc  (snort3-3.1.30.0)
skipping to change at line 36 skipping to change at line 36
#include <cstring> #include <cstring>
#include <iomanip> #include <iomanip>
#include <sstream> #include <sstream>
#include <vector> #include <vector>
#include "utils/js_identifier_ctx.h" #include "utils/js_identifier_ctx.h"
#define DEPTH 65536 #define DEPTH 65536
#define SCOPE_DEPTH 256 #define SCOPE_DEPTH 256
static const std::unordered_set<std::string> s_ignored_ids { "console" }; static const std::unordered_set<std::string> s_ignored_ids { "console", "v" };
static const std::unordered_set<std::string> s_ignored_props { "watch", "w" };
TEST_CASE("JSIdentifierCtx::substitute()", "[JSIdentifierCtx]") TEST_CASE("JSIdentifierCtx::substitute()", "[JSIdentifierCtx]")
{ {
SECTION("same name") SECTION("same name")
{ {
JSIdentifierCtx ident_ctx(DEPTH, SCOPE_DEPTH, s_ignored_ids); JSIdentifierCtx ident_ctx(DEPTH, SCOPE_DEPTH, s_ignored_ids, s_ignored_p rops);
CHECK(!strcmp(ident_ctx.substitute("a"), "var_0000")); CHECK(!strcmp(ident_ctx.substitute("a", false), "var_0000"));
CHECK(!strcmp(ident_ctx.substitute("a"), "var_0000")); CHECK(!strcmp(ident_ctx.substitute("a", false), "var_0000"));
} }
SECTION("different names") SECTION("different names")
{ {
JSIdentifierCtx ident_ctx(DEPTH, SCOPE_DEPTH, s_ignored_ids); JSIdentifierCtx ident_ctx(DEPTH, SCOPE_DEPTH, s_ignored_ids, s_ignored_p rops);
CHECK(!strcmp(ident_ctx.substitute("a"), "var_0000")); CHECK(!strcmp(ident_ctx.substitute("a", false), "var_0000"));
CHECK(!strcmp(ident_ctx.substitute("b"), "var_0001")); CHECK(!strcmp(ident_ctx.substitute("b", false), "var_0001"));
CHECK(!strcmp(ident_ctx.substitute("a"), "var_0000")); CHECK(!strcmp(ident_ctx.substitute("a", false), "var_0000"));
} }
SECTION("depth reached") SECTION("depth reached")
{ {
JSIdentifierCtx ident_ctx(2, SCOPE_DEPTH, s_ignored_ids); JSIdentifierCtx ident_ctx(2, SCOPE_DEPTH, s_ignored_ids, s_ignored_props );
CHECK(!strcmp(ident_ctx.substitute("a"), "var_0000")); CHECK(!strcmp(ident_ctx.substitute("a", false), "var_0000"));
CHECK(!strcmp(ident_ctx.substitute("b"), "var_0001")); CHECK(!strcmp(ident_ctx.substitute("b", false), "var_0001"));
CHECK(ident_ctx.substitute("c") == nullptr); CHECK(ident_ctx.substitute("c", false) == nullptr);
CHECK(ident_ctx.substitute("d") == nullptr); CHECK(ident_ctx.substitute("d", false) == nullptr);
CHECK(!strcmp(ident_ctx.substitute("a"), "var_0000")); CHECK(!strcmp(ident_ctx.substitute("a", false), "var_0000"));
} }
SECTION("max names") SECTION("max names")
{ {
JSIdentifierCtx ident_ctx(DEPTH + 2, SCOPE_DEPTH, s_ignored_ids); JSIdentifierCtx ident_ctx(DEPTH + 2, SCOPE_DEPTH, s_ignored_ids, s_ignor ed_props);
std::vector<std::string> n, e; std::vector<std::string> n, e;
n.reserve(DEPTH + 2); n.reserve(DEPTH + 2);
e.reserve(DEPTH); e.reserve(DEPTH);
for (int it = 0; it < DEPTH + 2; ++it) for (int it = 0; it < DEPTH + 2; ++it)
n.push_back("n" + std::to_string(it)); n.push_back("n" + std::to_string(it));
for (int it_name = 0; it_name < DEPTH; ++it_name) for (int it_name = 0; it_name < DEPTH; ++it_name)
{ {
std::stringstream stream; std::stringstream stream;
stream << std::setfill ('0') << std::setw(4) stream << std::setfill ('0') << std::setw(4)
<< std::hex << it_name; << std::hex << it_name;
e.push_back("var_" + stream.str()); e.push_back("var_" + stream.str());
} }
for (int it = 0; it < DEPTH; ++it) for (int it = 0; it < DEPTH; ++it)
CHECK(!strcmp(ident_ctx.substitute(n[it].c_str()), e[it].c_str())); CHECK(!strcmp(ident_ctx.substitute(n[it].c_str(), false), e[it].c_st r()));
CHECK(ident_ctx.substitute(n[DEPTH].c_str()) == nullptr); CHECK(ident_ctx.substitute(n[DEPTH].c_str(), false) == nullptr);
CHECK(ident_ctx.substitute(n[DEPTH + 1].c_str()) == nullptr); CHECK(ident_ctx.substitute(n[DEPTH + 1].c_str(), false) == nullptr);
}
SECTION("ignored identifier - single char")
{
JSIdentifierCtx ident_ctx(DEPTH, SCOPE_DEPTH, s_ignored_ids, s_ignored_p
rops);
CHECK(!strcmp(ident_ctx.substitute("v", false), "v"));
CHECK(!strcmp(ident_ctx.substitute("v", true), "var_0000"));
CHECK(!strcmp(ident_ctx.substitute("w", false), "var_0001"));
CHECK(!strcmp(ident_ctx.substitute("w", true), "w"));
}
SECTION("ignored identifier - multiple chars")
{
JSIdentifierCtx ident_ctx(DEPTH, SCOPE_DEPTH, s_ignored_ids, s_ignored_p
rops);
CHECK(!strcmp(ident_ctx.substitute("console", false), "console"));
CHECK(!strcmp(ident_ctx.substitute("console", true), "var_0000"));
CHECK(!strcmp(ident_ctx.substitute("watch", false), "var_0001"));
CHECK(!strcmp(ident_ctx.substitute("watch", true), "watch"));
} }
} }
TEST_CASE("JSIdentifierCtx::is_ignored()", "[JSIdentifierCtx]") TEST_CASE("JSIdentifierCtx::is_ignored()", "[JSIdentifierCtx]")
{ {
JSIdentifierCtx ident_ctx(DEPTH, SCOPE_DEPTH, s_ignored_ids); SECTION("single char identifier")
{
JSIdentifierCtx ident_ctx(DEPTH, SCOPE_DEPTH, s_ignored_ids, s_ignored_p
rops);
auto v1 = ident_ctx.substitute("v", false);
auto v2 = ident_ctx.substitute("a", false);
auto v3 = ident_ctx.substitute("w", false);
auto v4 = ident_ctx.substitute("w", true);
CHECK(ident_ctx.is_ignored(v1) == true);
CHECK(ident_ctx.is_ignored(v2) == false);
CHECK(ident_ctx.is_ignored(v3) == false);
CHECK(ident_ctx.is_ignored(v4) == true);
}
SECTION("multiple chars identifier")
{
JSIdentifierCtx ident_ctx(DEPTH, SCOPE_DEPTH, s_ignored_ids, s_ignored_p
rops);
auto v1 = ident_ctx.substitute("console"); auto v1 = ident_ctx.substitute("console", false);
auto v2 = ident_ctx.substitute("foo"); auto v2 = ident_ctx.substitute("foo", false);
auto v3 = ident_ctx.substitute("watch", false);
auto v4 = ident_ctx.substitute("watch", true);
CHECK(ident_ctx.is_ignored(v1) == true); CHECK(ident_ctx.is_ignored(v1) == true);
CHECK(ident_ctx.is_ignored(v2) == false); CHECK(ident_ctx.is_ignored(v2) == false);
CHECK(ident_ctx.is_ignored(v3) == false);
CHECK(ident_ctx.is_ignored(v4) == true);
}
} }
TEST_CASE("JSIdentifierCtx::scopes", "[JSIdentifierCtx]") TEST_CASE("JSIdentifierCtx::scopes", "[JSIdentifierCtx]")
{ {
JSIdentifierCtx ident_ctx(DEPTH, SCOPE_DEPTH, s_ignored_ids); JSIdentifierCtx ident_ctx(DEPTH, SCOPE_DEPTH, s_ignored_ids, s_ignored_props );
SECTION("scope stack") SECTION("scope stack")
{ {
CHECK(ident_ctx.scope_check({GLOBAL})); CHECK(ident_ctx.scope_check({GLOBAL}));
ident_ctx.scope_push(JSProgramScopeType::FUNCTION); ident_ctx.scope_push(JSProgramScopeType::FUNCTION);
ident_ctx.scope_push(JSProgramScopeType::BLOCK); ident_ctx.scope_push(JSProgramScopeType::BLOCK);
ident_ctx.scope_push(JSProgramScopeType::BLOCK); ident_ctx.scope_push(JSProgramScopeType::BLOCK);
CHECK(ident_ctx.scope_check({GLOBAL, FUNCTION, BLOCK, BLOCK})); CHECK(ident_ctx.scope_check({GLOBAL, FUNCTION, BLOCK, BLOCK}));
skipping to change at line 161 skipping to change at line 201
CHECK(!ident_ctx.scope_check({FUNCTION})); CHECK(!ident_ctx.scope_check({FUNCTION}));
CHECK(ident_ctx.scope_push(JSProgramScopeType::FUNCTION)); CHECK(ident_ctx.scope_push(JSProgramScopeType::FUNCTION));
CHECK(ident_ctx.scope_check({GLOBAL, FUNCTION})); CHECK(ident_ctx.scope_check({GLOBAL, FUNCTION}));
CHECK(!ident_ctx.scope_pop(JSProgramScopeType::BLOCK)); CHECK(!ident_ctx.scope_pop(JSProgramScopeType::BLOCK));
CHECK(ident_ctx.scope_check({GLOBAL, FUNCTION})); CHECK(ident_ctx.scope_check({GLOBAL, FUNCTION}));
CHECK(!ident_ctx.scope_check({GLOBAL})); CHECK(!ident_ctx.scope_check({GLOBAL}));
} }
SECTION("scope max nesting") SECTION("scope max nesting")
{ {
JSIdentifierCtx ident_ctx_limited(DEPTH, 2, s_ignored_ids); JSIdentifierCtx ident_ctx_limited(DEPTH, 2, s_ignored_ids, s_ignored_pro ps);
CHECK(ident_ctx_limited.scope_push(JSProgramScopeType::FUNCTION)); CHECK(ident_ctx_limited.scope_push(JSProgramScopeType::FUNCTION));
CHECK(ident_ctx_limited.scope_check({GLOBAL, FUNCTION})); CHECK(ident_ctx_limited.scope_check({GLOBAL, FUNCTION}));
CHECK(!ident_ctx_limited.scope_push(JSProgramScopeType::FUNCTION)); CHECK(!ident_ctx_limited.scope_push(JSProgramScopeType::FUNCTION));
CHECK(ident_ctx_limited.scope_check({GLOBAL, FUNCTION})); CHECK(ident_ctx_limited.scope_check({GLOBAL, FUNCTION}));
CHECK(!ident_ctx_limited.scope_push(JSProgramScopeType::FUNCTION)); CHECK(!ident_ctx_limited.scope_push(JSProgramScopeType::FUNCTION));
CHECK(ident_ctx_limited.scope_check({GLOBAL, FUNCTION})); CHECK(ident_ctx_limited.scope_check({GLOBAL, FUNCTION}));
CHECK(ident_ctx_limited.scope_pop(JSProgramScopeType::FUNCTION)); CHECK(ident_ctx_limited.scope_pop(JSProgramScopeType::FUNCTION));
 End of changes. 15 change blocks. 
25 lines changed or deleted 69 lines changed or added

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