Debian Codegraph @

unittest/gunit/pattern_matcher-t.cc

raw symbols SCIP
1 /*
2 Copyright (c) 2017, 2026, Oracle and/or its affiliates.
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License, version 2.0,
6 as published by the Free Software Foundation.
7
8 This program is designed to work with certain software (including
9 but not limited to OpenSSL) that is licensed under separate terms,
10 as designated in a particular file or component or in included license
11 documentation. The authors of MySQL hereby grant you an additional
12 permission to link the program and your derivative works with the
13 separately licensed software that they have either included with
14 the program or referenced in the documentation.
15
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License, version 2.0, for more details.
20
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software
23 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24 */
25
26 #include <gtest/gtest.h>
27 #include <my_sys.h>
28 #include "client/pattern_matcher.cc"
29
30 namespace mysql_client_test_ns {
31
32 class PatternMatcherTest : public ::testing::Test {
33 public:
34 CHARSET_INFO *cs_info;
35
36 protected:
37 void SetUp() override {
38 MY_CHARSET_LOADER loader;
39 cs_info = my_collation_get_by_name(&loader, "utf8mb4_0900_ai_ci", MYF(0));
40 }
41 };
42
43 /** Verifies that pattern list parsing is done correctly */
44 TEST_F(PatternMatcherTest, PatternParser) {
45 Pattern_matcher matcher;
46
47 EXPECT_EQ((size_t)0, matcher.add_patterns(":::"));
48 EXPECT_EQ((size_t)1, matcher.add_patterns(":First:"));
49 EXPECT_EQ((size_t)2, matcher.add_patterns("::First*:Second:"));
50 EXPECT_EQ((size_t)3, matcher.add_patterns(":1:Second::Third:"));
51 }
52
53 /** Verifies that default set of patterns is working */
54 TEST_F(PatternMatcherTest, DefaultPatterns) {
55 Pattern_matcher matcher;
56 matcher.add_patterns("*IDENTIFIED*:*PASSWORD*");
57
58 // positive tests - text should be matched
59 EXPECT_TRUE(matcher.is_matching("set password = 'mypass';", cs_info));
60 EXPECT_TRUE(matcher.is_matching("SET PASSWORD = 'mypass';", cs_info));
61 EXPECT_TRUE(matcher.is_matching(
62 "create user 'myuser'@'localhost' identified by 'mypass';", cs_info));
63 EXPECT_TRUE(matcher.is_matching(
64 "CREATE USER 'myuser'@'localhost' IDENTIFIED BY 'mypass';", cs_info));
65
66 // negative tests - text mustn't match
67 EXPECT_FALSE(matcher.is_matching("SELECT * FROM my_table;", cs_info));
68 EXPECT_FALSE(matcher.is_matching("DROP TABLE IF EXISTS my_table", cs_info));
69 EXPECT_FALSE(matcher.is_matching(
70 "UPDATE my_table SET name='Sakila' WHERE id=1", cs_info));
71 EXPECT_FALSE(matcher.is_matching(
72 "INSERT INTO my_table (my_col1,my_col2) VALUES(1,2);", cs_info));
73 EXPECT_FALSE(matcher.is_matching(
74 "GRANT ALL PRIVILEGES ON *.* TO 'myuser'@'localhost';", cs_info));
75 }
76
77 /** Various wildcard pattern testing */
78 TEST_F(PatternMatcherTest, WildCard) {
79 Pattern_matcher matcher;
80
81 // ========== SINGLE CHARACTER PATTERNS ========== //
82
83 matcher.add_patterns("??SQL");
84 EXPECT_TRUE(matcher.is_matching("MySQL", cs_info));
85 EXPECT_TRUE(matcher.is_matching("NoSQL", cs_info));
86 EXPECT_FALSE(matcher.is_matching("SQL", cs_info));
87 EXPECT_FALSE(matcher.is_matching("xSQL", cs_info));
88 EXPECT_FALSE(matcher.is_matching("123SQL", cs_info));
89 matcher.clear();
90
91 matcher.add_patterns("S?L");
92 EXPECT_TRUE(matcher.is_matching("SQL", cs_info));
93 EXPECT_TRUE(matcher.is_matching("STL", cs_info));
94 EXPECT_FALSE(matcher.is_matching("SL", cs_info));
95 EXPECT_FALSE(matcher.is_matching("S123L", cs_info));
96 matcher.clear();
97
98 // ========== MULTI CHARACTER PATTERNS =========== //
99
100 matcher.add_patterns("My*");
101 EXPECT_TRUE(matcher.is_matching("My", cs_info));
102 EXPECT_TRUE(matcher.is_matching("MySQL", cs_info));
103 EXPECT_FALSE(matcher.is_matching("y123", cs_info));
104 matcher.clear();
105
106 matcher.add_patterns("FROM *-users");
107 EXPECT_TRUE(matcher.is_matching("FROM admin-users", cs_info));
108 EXPECT_TRUE(matcher.is_matching("FROM regular-users", cs_info));
109 EXPECT_FALSE(matcher.is_matching("FROM users", cs_info));
110 matcher.clear();
111 }
112
113 } // namespace mysql_client_test_ns