mirror of
https://github.com/VectorCamp/vectorscan.git
synced 2025-06-28 16:41:01 +03:00
serialize: parameterize on pattern as well
This commit is contained in:
parent
a97ec56aee
commit
1376f3849a
@ -45,46 +45,64 @@ namespace {
|
|||||||
using namespace std;
|
using namespace std;
|
||||||
using namespace testing;
|
using namespace testing;
|
||||||
|
|
||||||
static constexpr unsigned validModes[] = {
|
static const unsigned validModes[] = {
|
||||||
HS_MODE_STREAM,
|
|
||||||
HS_MODE_NOSTREAM,
|
HS_MODE_NOSTREAM,
|
||||||
|
HS_MODE_STREAM | HS_MODE_SOM_HORIZON_LARGE,
|
||||||
HS_MODE_VECTORED
|
HS_MODE_VECTORED
|
||||||
};
|
};
|
||||||
|
|
||||||
class Serializep : public TestWithParam<unsigned> {
|
static const pattern testPatterns[] = {
|
||||||
|
pattern("hatstand.*teakettle.*badgerbrush", HS_FLAG_CASELESS, 1000),
|
||||||
|
pattern("hatstand.*teakettle.*badgerbrush", HS_FLAG_DOTALL, 1001),
|
||||||
|
pattern("hatstand|teakettle|badgerbrush", 0, 1002),
|
||||||
|
pattern("^hatstand|teakettle|badgerbrush$", 0, 1003),
|
||||||
|
pattern("foobar.{10,1000}xyzzy", HS_FLAG_DOTALL, 1004),
|
||||||
|
pattern("foobar.{2,501}roobar", 0, 1005),
|
||||||
|
pattern("abc.*def.*ghi", HS_FLAG_SOM_LEFTMOST, 1006),
|
||||||
|
pattern("(\\p{L}){4}", HS_FLAG_UTF8|HS_FLAG_UCP, 1007),
|
||||||
|
pattern("\\.(exe|pdf|gif|jpg|png|wav|riff|mp4)\\z", 0, 1008)
|
||||||
};
|
};
|
||||||
|
|
||||||
|
class SerializeP : public TestWithParam<tuple<unsigned, pattern>> {};
|
||||||
|
|
||||||
|
static
|
||||||
|
const char *getModeString(unsigned mode) {
|
||||||
|
if (mode & HS_MODE_STREAM) {
|
||||||
|
return "STREAM";
|
||||||
|
}
|
||||||
|
if (mode & HS_MODE_BLOCK) {
|
||||||
|
return "BLOCK";
|
||||||
|
}
|
||||||
|
if (mode & HS_MODE_VECTORED) {
|
||||||
|
return "VECTORED";
|
||||||
|
}
|
||||||
|
return "UNKNOWN";
|
||||||
|
}
|
||||||
|
|
||||||
// Check that we can deserialize from a char array at any alignment and the info
|
// Check that we can deserialize from a char array at any alignment and the info
|
||||||
// is consistent
|
// is consistent
|
||||||
TEST_P(Serializep, DeserializeFromAnyAlignment) {
|
TEST_P(SerializeP, DeserializeFromAnyAlignment) {
|
||||||
const unsigned mode = GetParam();
|
const unsigned mode = get<0>(GetParam());
|
||||||
|
const pattern &pat = get<1>(GetParam());
|
||||||
SCOPED_TRACE(mode);
|
SCOPED_TRACE(mode);
|
||||||
|
SCOPED_TRACE(pat);
|
||||||
|
|
||||||
hs_error_t err;
|
hs_error_t err;
|
||||||
hs_database_t *db = buildDB("hatstand.*teakettle.*badgerbrush",
|
hs_database_t *db = buildDB(pat, mode);
|
||||||
HS_FLAG_CASELESS, 1000, mode);
|
|
||||||
ASSERT_TRUE(db != nullptr) << "database build failed.";
|
ASSERT_TRUE(db != nullptr) << "database build failed.";
|
||||||
|
|
||||||
char *original_info = nullptr;
|
char *original_info = nullptr;
|
||||||
err = hs_database_info(db, &original_info);
|
err = hs_database_info(db, &original_info);
|
||||||
ASSERT_EQ(HS_SUCCESS, err);
|
ASSERT_EQ(HS_SUCCESS, err);
|
||||||
|
|
||||||
const char *mode_string = nullptr;
|
const char *mode_string = getModeString(mode);
|
||||||
switch (mode) {
|
|
||||||
case HS_MODE_STREAM:
|
|
||||||
mode_string = "STREAM";
|
|
||||||
break;
|
|
||||||
case HS_MODE_NOSTREAM:
|
|
||||||
mode_string = "BLOCK";
|
|
||||||
break;
|
|
||||||
case HS_MODE_VECTORED:
|
|
||||||
mode_string = "VECTORED";
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
ASSERT_NE(nullptr, original_info) << "hs_serialized_database_info returned null.";
|
ASSERT_NE(nullptr, original_info)
|
||||||
|
<< "hs_serialized_database_info returned null.";
|
||||||
ASSERT_STREQ("Version:", string(original_info).substr(0, 8).c_str());
|
ASSERT_STREQ("Version:", string(original_info).substr(0, 8).c_str());
|
||||||
ASSERT_TRUE(strstr(original_info, mode_string) != nullptr);
|
ASSERT_TRUE(strstr(original_info, mode_string) != nullptr)
|
||||||
|
<< "Original info \"" << original_info
|
||||||
|
<< "\" does not contain " << mode_string;
|
||||||
|
|
||||||
char *bytes = nullptr;
|
char *bytes = nullptr;
|
||||||
size_t length = 0;
|
size_t length = 0;
|
||||||
@ -138,35 +156,28 @@ TEST_P(Serializep, DeserializeFromAnyAlignment) {
|
|||||||
|
|
||||||
// Check that we can deserialize_at from a char array at any alignment and the
|
// Check that we can deserialize_at from a char array at any alignment and the
|
||||||
// info is consistent
|
// info is consistent
|
||||||
TEST_P(Serializep, DeserializeAtFromAnyAlignment) {
|
TEST_P(SerializeP, DeserializeAtFromAnyAlignment) {
|
||||||
const unsigned mode = GetParam();
|
const unsigned mode = get<0>(GetParam());
|
||||||
|
const pattern &pat = get<1>(GetParam());
|
||||||
SCOPED_TRACE(mode);
|
SCOPED_TRACE(mode);
|
||||||
|
SCOPED_TRACE(pat);
|
||||||
|
|
||||||
hs_error_t err;
|
hs_error_t err;
|
||||||
hs_database_t *db = buildDB("hatstand.*teakettle.*badgerbrush",
|
hs_database_t *db = buildDB(pat, mode);
|
||||||
HS_FLAG_CASELESS, 1000, mode);
|
|
||||||
ASSERT_TRUE(db != nullptr) << "database build failed.";
|
ASSERT_TRUE(db != nullptr) << "database build failed.";
|
||||||
|
|
||||||
char *original_info;
|
char *original_info;
|
||||||
err = hs_database_info(db, &original_info);
|
err = hs_database_info(db, &original_info);
|
||||||
ASSERT_EQ(HS_SUCCESS, err);
|
ASSERT_EQ(HS_SUCCESS, err);
|
||||||
|
|
||||||
const char *mode_string = nullptr;
|
const char *mode_string = getModeString(mode);
|
||||||
switch (mode) {
|
|
||||||
case HS_MODE_STREAM:
|
|
||||||
mode_string = "STREAM";
|
|
||||||
break;
|
|
||||||
case HS_MODE_NOSTREAM:
|
|
||||||
mode_string = "BLOCK";
|
|
||||||
break;
|
|
||||||
case HS_MODE_VECTORED:
|
|
||||||
mode_string = "VECTORED";
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
ASSERT_NE(nullptr, original_info) << "hs_serialized_database_info returned null.";
|
ASSERT_NE(nullptr, original_info)
|
||||||
|
<< "hs_serialized_database_info returned null.";
|
||||||
ASSERT_STREQ("Version:", string(original_info).substr(0, 8).c_str());
|
ASSERT_STREQ("Version:", string(original_info).substr(0, 8).c_str());
|
||||||
ASSERT_TRUE(strstr(original_info, mode_string) != nullptr);
|
ASSERT_TRUE(strstr(original_info, mode_string) != nullptr)
|
||||||
|
<< "Original info \"" << original_info
|
||||||
|
<< "\" does not contain " << mode_string;
|
||||||
|
|
||||||
char *bytes = nullptr;
|
char *bytes = nullptr;
|
||||||
size_t length = 0;
|
size_t length = 0;
|
||||||
@ -226,8 +237,8 @@ TEST_P(Serializep, DeserializeAtFromAnyAlignment) {
|
|||||||
delete[] mem;
|
delete[] mem;
|
||||||
}
|
}
|
||||||
|
|
||||||
INSTANTIATE_TEST_CASE_P(Serialize, Serializep,
|
INSTANTIATE_TEST_CASE_P(Serialize, SerializeP,
|
||||||
ValuesIn(validModes));
|
Combine(ValuesIn(validModes), ValuesIn(testPatterns)));
|
||||||
|
|
||||||
// Attempt to reproduce the scenario in UE-1946.
|
// Attempt to reproduce the scenario in UE-1946.
|
||||||
TEST(Serialize, CrossCompileSom) {
|
TEST(Serialize, CrossCompileSom) {
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright (c) 2015, Intel Corporation
|
* Copyright (c) 2015-2017, Intel Corporation
|
||||||
*
|
*
|
||||||
* Redistribution and use in source and binary forms, with or without
|
* Redistribution and use in source and binary forms, with or without
|
||||||
* modification, are permitted provided that the following conditions are met:
|
* modification, are permitted provided that the following conditions are met:
|
||||||
@ -48,10 +48,15 @@ int record_cb(unsigned id, unsigned long long, unsigned long long to,
|
|||||||
return (int)c->halt;
|
return (int)c->halt;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::ostream &operator<< (std::ostream &o, const MatchRecord &m) {
|
std::ostream &operator<<(std::ostream &o, const MatchRecord &m) {
|
||||||
return o << "[" << m.to << ", " << m.id << "]";
|
return o << "[" << m.to << ", " << m.id << "]";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::ostream &operator<<(std::ostream &o, const pattern &p) {
|
||||||
|
return o << "[" << "expr=\"" << p.expression << "\", flags=" << p.flags
|
||||||
|
<< ", id=" << p.id << "]";
|
||||||
|
}
|
||||||
|
|
||||||
hs_database_t *buildDB(const vector<pattern> &patterns, unsigned int mode,
|
hs_database_t *buildDB(const vector<pattern> &patterns, unsigned int mode,
|
||||||
hs_platform_info *plat) {
|
hs_platform_info *plat) {
|
||||||
vector<const char *> expressions;
|
vector<const char *> expressions;
|
||||||
|
@ -53,7 +53,7 @@ struct MatchRecord {
|
|||||||
int id;
|
int id;
|
||||||
};
|
};
|
||||||
|
|
||||||
std::ostream &operator<< (std::ostream &o, const MatchRecord &m);
|
std::ostream &operator<<(std::ostream &o, const MatchRecord &m);
|
||||||
|
|
||||||
struct CallBackContext {
|
struct CallBackContext {
|
||||||
CallBackContext() : halt(false) {}
|
CallBackContext() : halt(false) {}
|
||||||
@ -79,22 +79,29 @@ int dummy_cb(unsigned, unsigned long long, unsigned long long, unsigned,
|
|||||||
|
|
||||||
struct pattern {
|
struct pattern {
|
||||||
std::string expression;
|
std::string expression;
|
||||||
unsigned int flags;
|
unsigned int flags = 0;
|
||||||
unsigned int id;
|
unsigned int id = 0;
|
||||||
hs_expr_ext ext;
|
hs_expr_ext ext;
|
||||||
|
|
||||||
pattern(const std::string &expression_in, unsigned int flags_in = 0,
|
// We need a default constructor for combining in parameterised tests.
|
||||||
unsigned int id_in = 0) : expression(expression_in),
|
pattern() {
|
||||||
flags(flags_in), id(id_in) {
|
|
||||||
memset(&ext, 0, sizeof(ext));
|
memset(&ext, 0, sizeof(ext));
|
||||||
}
|
}
|
||||||
|
|
||||||
pattern(const std::string &expression_in, unsigned int flags_in,
|
explicit pattern(std::string expression_in,
|
||||||
unsigned int id_in, const hs_expr_ext &ext_in) :
|
unsigned int flags_in = 0, unsigned int id_in = 0)
|
||||||
expression(expression_in), flags(flags_in), id(id_in),
|
: expression(std::move(expression_in)), flags(flags_in), id(id_in) {
|
||||||
ext(ext_in) { }
|
memset(&ext, 0, sizeof(ext));
|
||||||
|
}
|
||||||
|
|
||||||
|
pattern(std::string expression_in, unsigned int flags_in,
|
||||||
|
unsigned int id_in, hs_expr_ext ext_in)
|
||||||
|
: expression(std::move(expression_in)), flags(flags_in), id(id_in),
|
||||||
|
ext(std::move(ext_in)) {}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
std::ostream &operator<<(std::ostream &o, const pattern &p);
|
||||||
|
|
||||||
hs_database_t *buildDB(const std::vector<pattern> &patterns, unsigned int mode,
|
hs_database_t *buildDB(const std::vector<pattern> &patterns, unsigned int mode,
|
||||||
hs_platform_info *plat = nullptr);
|
hs_platform_info *plat = nullptr);
|
||||||
hs_database_t *buildDB(const pattern &pat, unsigned int mode);
|
hs_database_t *buildDB(const pattern &pat, unsigned int mode);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user