}
extern const BaseApi* se_ac_bnfa;
+extern const BaseApi* se_ac_full;
Mpse* mpse = nullptr;
Mpse* MpseManager::get_search_engine(const char *type)
{
- assert(!strcmp(type, "ac_bnfa"));
+ const MpseApi* api;
+
+ if ( !strcmp(type, "ac_bnfa") )
+ api = (MpseApi*)se_ac_bnfa;
+
+ else if ( !strcmp(type, "ac_full") )
+ api = (MpseApi*)se_ac_full;
+
+ else
+ return nullptr;
+
+ api->init();
+ mpse = api->ctor(snort_conf, nullptr, &s_agent);
- const MpseApi* mpse_api = (MpseApi*)se_ac_bnfa;
- mpse_api->init();
- mpse = mpse_api->ctor(snort_conf, nullptr, &s_agent);
CHECK(mpse);
+ mpse->set_api(api);
return mpse;
}
-void MpseManager::delete_search_engine(Mpse*)
+void MpseManager::delete_search_engine(Mpse* eng)
{
- const MpseApi* mpse_api = (MpseApi*)se_ac_bnfa;
- mpse_api->dtor(mpse);
+ const MpseApi* api = eng->get_api();
+ api->dtor(eng);
}
+struct ExpectedMatch
+{
+ int id;
+ int offset;
+};
+
+static const ExpectedMatch* s_expect = nullptr;
+static int s_found = 0;
-static int pattern_id = 0;
static int Test_SearchStrFound(
- void* /*id*/, void* /*tree*/, int /*index*/, void* /*context*/, void* /*neg_list*/)
+ void* pid, void* /*tree*/, int index, void* /*context*/, void* /*neg_list*/)
{
- //printf("found str with id=%ld, index=%d\n", (long)id, index);
- return 0;
+ auto id = reinterpret_cast<std::uintptr_t>(pid);
+
+ if ( s_expect and s_found >= 0 and
+ s_expect[s_found].id == (int)id and
+ s_expect[s_found].offset == index )
+ {
+ ++s_found;
+ }
+ else s_found = -1;
+
+ return s_found == -1;
}
-TEST_GROUP(search_tool_tests)
+//-------------------------------------------------------------------------
+// ac_bnfa tests
+//-------------------------------------------------------------------------
+
+TEST_GROUP(search_tool_bnfa)
{
+ SearchTool* stool;
+
void setup() override
- { CHECK(se_ac_bnfa); }
+ {
+ CHECK(se_ac_bnfa);
+ stool = new SearchTool("ac_bnfa");
+
+ CHECK(stool->mpse);
+
+ int pattern_id = 1;
+ stool->add("the", 3, pattern_id);
+ CHECK(stool->max_len == 3);
+
+ pattern_id = 77;
+ stool->add("tuba", 4, pattern_id);
+ CHECK(stool->max_len == 4);
+
+ pattern_id = 78;
+ stool->add("uba", 3, pattern_id);
+ CHECK(stool->max_len == 4);
+
+ pattern_id = 2112;
+ stool->add("away", 4, pattern_id);
+ CHECK(stool->max_len == 4);
+
+ pattern_id = 1000;
+ stool->add("nothere", 7, pattern_id);
+ CHECK(stool->max_len == 7);
+
+ stool->prep();
+
+ }
+ void teardown() override
+ {
+ delete stool;
+ }
};
-TEST(search_tool_tests, ac_bnfa)
+TEST(search_tool_bnfa, search)
{
- SearchTool *stool = new SearchTool("ac_bnfa");
- CHECK(stool->mpse);
+ // 0 1 2 3
+ // 0123456789012345678901234567890
+ const char* datastr = "the tuba ran away with the tuna";
+ const ExpectedMatch xm[] =
+ {
+ { 1, 3 },
+ { 78, 8 },
+ { 2112, 17 },
+ { 1, 26 },
+ { 0, 0 }
+ };
- pattern_id = 1;
- stool->add("the", 3, pattern_id);
- CHECK(stool->max_len == 3);
+ s_expect = xm;
+ s_found = 0;
- pattern_id = 77;
- stool->add("uba", 3, pattern_id);
- CHECK(stool->max_len == 3);
+ int result = stool->find(datastr, strlen(datastr), Test_SearchStrFound);
+
+ CHECK(result == 4);
+ CHECK(s_found == 4);
+}
- pattern_id = 2112;
- stool->add("away", 4, pattern_id);
- CHECK(stool->max_len == 4);
+TEST(search_tool_bnfa, search_all)
+{
+ // 0 1 2 3
+ // 0123456789012345678901234567890
+ const char* datastr = "the tuba ran away with the tuna";
+ const ExpectedMatch xm[] =
+ {
+ { 1, 3 },
+ { 78, 8 },
+ { 2112, 17 },
+ { 1, 26 },
+ { 0, 0 }
+ };
- pattern_id = 1000;
- stool->add("nothere", 7, pattern_id);
- CHECK(stool->max_len == 7);
+ s_expect = xm;
+ s_found = 0;
- stool->prep();
+ int result = stool->find_all(datastr, strlen(datastr), Test_SearchStrFound);
- const char *datastr = "the tuba ran away";
- int result = stool->find(datastr, strlen(datastr), Test_SearchStrFound);
- CHECK(result == 3);
- delete stool;
+ CHECK(result == 4);
+ CHECK(s_found == 4);
}
-TEST(search_tool_tests, search_all_ac_bnfa)
+//-------------------------------------------------------------------------
+// ac_full tests
+//-------------------------------------------------------------------------
+
+TEST_GROUP(search_tool_full)
+{
+ SearchTool* stool;
+
+ void setup() override
+ {
+ CHECK(se_ac_full);
+ stool = new SearchTool("ac_full", true);
+
+ CHECK(stool->mpse);
+
+ int pattern_id = 1;
+ stool->add("the", 3, pattern_id);
+ CHECK(stool->max_len == 3);
+
+ pattern_id = 77;
+ stool->add("tuba", 4, pattern_id);
+ CHECK(stool->max_len == 4);
+
+ pattern_id = 78;
+ stool->add("uba", 3, pattern_id);
+ CHECK(stool->max_len == 4);
+
+ pattern_id = 2112;
+ stool->add("away", 4, pattern_id);
+ CHECK(stool->max_len == 4);
+
+ pattern_id = 1000;
+ stool->add("nothere", 7, pattern_id);
+ CHECK(stool->max_len == 7);
+
+ stool->prep();
+
+ }
+ void teardown() override
+ {
+ delete stool;
+ }
+};
+
+TEST(search_tool_full, search)
{
- SearchTool *stool = new SearchTool("ac_bnfa");
- CHECK(stool->mpse);
+ // 0 1 2 3
+ // 0123456789012345678901234567890
+ const char* datastr = "the tuba ran away with the tuna";
+ const ExpectedMatch xm[] =
+ {
+ { 1, 3 },
+ { 78, 8 },
+ { 2112, 17 },
+ { 1, 26 },
+ { 0, 0 }
+ };
- pattern_id = 1;
- stool->add("the", 3, pattern_id);
- CHECK(stool->max_len == 3);
+ s_expect = xm;
+ s_found = 0;
- pattern_id = 77;
- stool->add("uba", 3, pattern_id);
- CHECK(stool->max_len == 3);
+ int result = stool->find(datastr, strlen(datastr), Test_SearchStrFound);
- pattern_id = 2112;
- stool->add("away", 4, pattern_id);
- CHECK(stool->max_len == 4);
+ CHECK(result == 4);
+ CHECK(s_found == 4);
+}
- pattern_id = 1000;
- stool->add("nothere", 7, pattern_id);
- CHECK(stool->max_len == 7);
+TEST(search_tool_full, search_all)
+{
+ // 0 1 2 3
+ // 0123456789012345678901234567890
+ const char* datastr = "the tuba ran away with the tuna";
+ const ExpectedMatch xm[] =
+ {
+ { 1, 3 },
+ { 78, 8 },
+ { 77, 8 },
+ { 2112, 17 },
+ { 1, 26 },
+ { 0, 0 }
+ };
- stool->prep();
+ s_expect = xm;
+ s_found = 0;
- const char *datastr = "the tuba ran away";
int result = stool->find_all(datastr, strlen(datastr), Test_SearchStrFound);
- CHECK(result == 3);
- delete stool;
+
+ CHECK(result == 5);
+ CHECK(s_found == 5);
}
//-------------------------------------------------------------------------