SQL语句匿名化

Signed-off-by: bjd <baijidong@huawei.com>
This commit is contained in:
bjd 2024-11-15 11:44:00 +08:00
parent 6ae6ce8190
commit 6b3c848d28
3 changed files with 109 additions and 5 deletions

View File

@ -57,7 +57,7 @@ public:
static bool RenameFile(const std::string &srcFile, const std::string &destFile);
static bool CopyFile(const std::string &srcFile, const std::string &destFile);
static std::string Anonymous(const std::string &srcFile);
static std::string AnonymousSql(const std::string &sql);
static std::string AnonySql(const std::string &sql);
static ssize_t GetFileSize(const std::string &fileName);
static bool IsSlaveDbName(const std::string &fileName);
static std::string GetSlavePath(const std::string& name);

View File

@ -319,7 +319,7 @@ std::string ProcessString(const std::string &input)
return result;
}
std::string SqliteUtils::AnonymousSql(const std::string &sql)
std::string SqliteUtils::AnonySql(const std::string &sql)
{
std::regex SELECT_REGEX("SELECT\\s+(.)\\s+FROM\\s+([^\\s;]+)", std::regex_constants::icase);
std::regex INSERT_REGEX("INSERT\\s+INTO\\s+([^\\s;]+)", std::regex_constants::icase);
@ -354,7 +354,7 @@ std::string SqliteUtils::AnonymousSql(const std::string &sql)
return MaskedSql;
} else if (std::regex_search(sql, match, CREATE_TABLE_REGEX)) {
std::string MaskedSql =
sql.substr(START_SIZE, CREATE_TABLE_SIZE) + ProcessString(sql.substr(CREATE_DATABASE_SIZE));
sql.substr(START_SIZE, CREATE_TABLE_SIZE) + ProcessString(sql.substr(CREATE_TABLE_SIZE));
return MaskedSql;
} else if (std::regex_search(sql, match, DROP_TABLE_IF_EXITS_REGEX)) {
std::string MaskedSql =
@ -378,8 +378,8 @@ std::string SqliteUtils::AnonymousSql(const std::string &sql)
std::string MaskedSql = sql.substr(START_SIZE, PRAGMA_SIZE) + ProcessString(sql.substr(PRAGMA_SIZE));
return MaskedSql;
}
std::string maskedSql = sql.substr(START_SIZE, OTHER_SIZE) + ProcessString(sql.substr(OTHER_SIZE));
return maskedSql;
std::string MaskedSql = sql.substr(START_SIZE, OTHER_SIZE) + ProcessString(sql.substr(OTHER_SIZE));
return MaskedSql;
}
ssize_t SqliteUtils::GetFileSize(const std::string &fileName)

View File

@ -160,4 +160,108 @@ HWTEST_F(SqliteUtilsTest, SqliteUtils_Test_0023, TestSize.Level1)
{
EXPECT_EQ(SqliteUtils::Anonymous("file /data/stage/el2/database/rdb/ddddddd/linker_reborn.db-wal"),
"file /***/el2/***/linker_reborn.db-wal");
}
HWTEST_F(SqliteUtilsTest, SqliteUtils_Test_0024, TestSize.Level1)
{
EXPECT_EQ(SqliteUtils::AnonySql("select value1, value2 from bigint_table WHERE case = 1."),
"select v***e*, v***e* f*** big*******le W***E c*** = *.");
}
HWTEST_F(SqliteUtilsTest, SqliteUtils_Test_0025, TestSize.Level1)
{
EXPECT_EQ(SqliteUtils::AnonySql("select value1, value2 from bigint_table."), "select v***e*, v***e* f*** big*******le.");
}
HWTEST_F(SqliteUtilsTest, SqliteUtils_Test_0026, TestSize.Level1)
{
EXPECT_EQ(SqliteUtils::AnonySql("SELECT * FROM test."), "SELECT * F*** t***.");
}
HWTEST_F(SqliteUtilsTest, SqliteUtils_Test_0027, TestSize.Level1)
{
EXPECT_EQ(SqliteUtils::AnonySql("INSERT INTO test VALUES(1, '[1.2, 0.3, 3.2, 1.6, 2.5, 3.1, 0.8, 0.4]');"),
"INSERT INTO t*** V***ES(*, *[*.*, *.*, *.*, *.*, *.*, *.*, *.*, *.*]');");
}
HWTEST_F(SqliteUtilsTest, SqliteUtils_Test_0028, TestSize.Level1)
{
EXPECT_EQ(SqliteUtils::AnonySql("INSERT INTO test (data1, data2, data3, data4) VALUES (?, ?, ?, ?);"),
"INSERT INTO t*** (d****, d****, d****, d****) V***ES (?, ?, ?, ?);");
}
HWTEST_F(SqliteUtilsTest, SqliteUtils_Test_0029, TestSize.Level1)
{
EXPECT_EQ(SqliteUtils::AnonySql("UPDATE test SET age = 18 WHERE id = 1."), "UPDATE t*** *ET *ge = ** W***E *d = *.");
}
HWTEST_F(SqliteUtilsTest, SqliteUtils_Test_0030, TestSize.Level1)
{
EXPECT_EQ(SqliteUtils::AnonySql("DELETE FROM test;"), "DELETE FROM t***;");
}
HWTEST_F(SqliteUtilsTest, SqliteUtils_Test_0031, TestSize.Level1)
{
EXPECT_EQ(SqliteUtils::AnonySql("DELETE FROM test WHERE time = 30;"), "DELETE FROM t*** W***E t*** = **;");
}
HWTEST_F(SqliteUtilsTest, SqliteUtils_Test_0032, TestSize.Level1)
{
EXPECT_EQ(SqliteUtils::AnonySql("CREATE DATABASE DBtest.db;"), "CREATE DATABASE D***st.*b;");
}
HWTEST_F(SqliteUtilsTest, SqliteUtils_Test_0033, TestSize.Level1)
{
EXPECT_EQ(
SqliteUtils::AnonySql(
"CREATE TABLE IF NOT EXISTS TEST (id INT PRIMARY KEY, name TEXT, extend BLOB, code REAL, years UNLIMITED INT, attachment ASSET, attachments ASSETS)."),
"CREATE TABLE *F *OT E***TS T*** (*d *NT P***ARY *EY, n*** T***, e***nd B***, c*** R***, y***s UN*****ED *NT, at*****ent A***T, at*****ents A***TS).");
}
HWTEST_F(SqliteUtilsTest, SqliteUtils_Test_0034, TestSize.Level1)
{
EXPECT_EQ(
SqliteUtils::AnonySql(
"CREATE TABLE TEST (id INT PRIMARY KEY, name TEXT, extend BLOB, code REAL, years UNLIMITED INT, attachment ASSET, attachments ASSETS)."),
"CREATE TABLE T*** (*d *NT P***ARY *EY, n*** T***, e***nd B***, c*** R***, y***s UN*****ED *NT, at*****ent A***T, at*****ents A***TS).");
}
HWTEST_F(SqliteUtilsTest, SqliteUtils_Test_0035, TestSize.Level1)
{
EXPECT_EQ(SqliteUtils::AnonySql("DROP TABLE IF EXISTS bigint_table;"), "DROP TABLE IF EXISTS big*******le;");
}
HWTEST_F(SqliteUtilsTest, SqliteUtils_Test_0036, TestSize.Level1)
{
EXPECT_EQ(SqliteUtils::AnonySql("DROP TABLE bigint_table;"), "DROP TABLE big*******le;");
}
HWTEST_F(SqliteUtilsTest, SqliteUtils_Test_0037, TestSize.Level1)
{
EXPECT_EQ(
SqliteUtils::AnonySql("DROP DATABASE IF EXISTS database_name;"), "DROP DATABASE IF EXISTS dat*******ame;");
}
HWTEST_F(SqliteUtilsTest, SqliteUtils_Test_0038, TestSize.Level1)
{
EXPECT_EQ(SqliteUtils::AnonySql("DROP DATABASE database_name;"), "DROP DATABASE dat*******ame;");
}
HWTEST_F(SqliteUtilsTest, SqliteUtils_Test_0039, TestSize.Level1)
{
EXPECT_EQ(SqliteUtils::AnonySql("PRAGMA user_version = 3"), "PRAGMA use*******on = *");
}
HWTEST_F(SqliteUtilsTest, SqliteUtils_Test_0040, TestSize.Level1)
{
EXPECT_EQ(
SqliteUtils::AnonySql("ALTER TABLE test ADD COLUMN address TEXT;"), "ALTER TABLE t*** *DD C***MN a***ess T***;");
}
HWTEST_F(SqliteUtilsTest, SqliteUtils_Test_0041, TestSize.Level1)
{
EXPECT_EQ(
SqliteUtils::AnonySql(
"CREATE TABLE TEST (id INT PRIMARY KEY, name TEXT, extend BLOB, code REAL, years UNLIMITED INT, attachment ASSET, attachments ASSETS)."),
"CREATE T***E T*** (*d *NT P***ARY *EY, n*** T***, e***nd B***, c*** R***, y***s UN*****ED *NT, at*****ent A***T, at*****ents A***TS).");
}