Debian Codegraph @

unittest/gunit/parsertest.h

raw symbols SCIP
1 #ifndef PARSERTEST_INCLUDED
2 #define PARSERTEST_INCLUDED
3 /* Copyright (c) 2012, 2026, Oracle and/or its affiliates.
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License, version 2.0,
7 as published by the Free Software Foundation.
8
9 This program is designed to work with certain software (including
10 but not limited to OpenSSL) that is licensed under separate terms,
11 as designated in a particular file or component or in included license
12 documentation. The authors of MySQL hereby grant you an additional
13 permission to link the program and your derivative works with the
14 separately licensed software that they have either included with
15 the program or referenced in the documentation.
16
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License, version 2.0, for more details.
21
22 You should have received a copy of the GNU General Public License
23 along with this program; if not, write to the Free Software
24 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
25
26 /**
27 @file parsertest.h
28
29 */
30
31 #include "sql/sql_class.h"
32 #include "sql/sql_lex.h"
33 #include "sql/sql_parse.h"
34 #include "unittest/gunit/test_utils.h"
35
36 using my_testing::Mock_error_handler;
37 using my_testing::Server_initializer;
38
39 /*
40 Parses a query and returns a parse tree. In our parser this is
41 called a Query_block.
42 */
43 static Query_block *parse(const Server_initializer *initializer,
44 const char *query, int expected_error_code) {
45 Parser_state state;
46
47 size_t length = strlen(query);
48 char *mutable_query = const_cast<char *>(query);
49
50 state.init(initializer->thd(), mutable_query, length);
51
52 /*
53 This tricks the server to parse the query and then stop,
54 without executing.
55 */
56 initializer->set_expected_error(ER_MUST_CHANGE_PASSWORD);
57 initializer->thd()->security_context()->set_password_expired(true);
58
59 Mock_error_handler handler(initializer->thd(), expected_error_code);
60 lex_start(initializer->thd());
61
62 if (initializer->thd()->db().str == nullptr) {
63 // The THD DTOR will do my_free() on this.
64 char *db = static_cast<char *>(my_malloc(PSI_NOT_INSTRUMENTED, 3, MYF(0)));
65 sprintf(db, "db");
66 LEX_CSTRING db_lex_cstr = {db, strlen(db)};
67 initializer->thd()->reset_db(db_lex_cstr);
68 }
69
70 lex_start(initializer->thd());
71 mysql_reset_thd_for_next_command(initializer->thd());
72 parse_sql(initializer->thd(), &state, nullptr);
73
74 return initializer->thd()->lex->current_query_block();
75 }
76
77 /*
78 A class for unit testing the parser.
79 */
80 class ParserTest : public ::testing::Test {
81 protected:
82 void SetUp() override { initializer.SetUp(); }
83 void TearDown() override { initializer.TearDown(); }
84
85 THD *thd() const { return initializer.thd(); }
86
87 Server_initializer initializer;
88
89 void assert_eq(int x, int y) const { ASSERT_EQ(x, y); }
90
91 Query_block *parse(const char *query, int expected_error_code) const {
92 return ::parse(&initializer, query, expected_error_code);
93 }
94
95 Query_block *parse(const char *query) const { return parse(query, 0); }
96 };
97
98 #endif // PARSERTEST_INCLUDED