1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
|
/*
* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; version 2 of the
* License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA
*/
#include "wb_helpers.h"
#include "grtpp.h"
#include "grtsqlparser/mysql_parser_services.h"
using namespace parser;
// Contains tests for the parser module implementing the ANTLR based parser services.
// Many of the APIs are also used in other tests.
BEGIN_TEST_DATA_CLASS(mysql_parser_module_tests)
protected:
WBTester _tester;
MySQLParserServices::Ref _services;
ParserContext::Ref _context;
TEST_DATA_CONSTRUCTOR(mysql_parser_module_tests)
{
populate_grt(_tester.grt, _tester);
_services = MySQLParserServices::get(_tester.grt);
GrtVersionRef version(_tester.grt);
version->majorNumber(5);
version->minorNumber(7);
version->releaseNumber(10);
_context = MySQLParserServices::createParserContext(_tester.get_rdbms()->characterSets(), version, true);
}
END_TEST_DATA_CLASS
TEST_MODULE(mysql_parser_module_tests, "parser module tests");
// Tests for parseStatement.
// Each test function tests a group of statements from the grammar, as listed in the top rule.
// Not all query types are implemented in parseStatement. So most of the functions are empty atm.
// alter_statement
TEST_FUNCTION(5)
{
}
// create_statement
// drop_statement
// rename_table_statement
// truncate_table_statement
// call_statement
// delete_statement
// do_statement
// handler_statement
// insert_statement
// load_statement
// replace_statement
// select_statement
// update_statement
// partitioning
// transaction_or_locking_statement
// replication_statement
// prepared_statement
// account_management_statement
TEST_FUNCTION(95)
{
// ----- Grant statements.
grt::DictRef result = _services->parseStatement(_context, _tester.grt, "grant all privileges on table a to current_user");
grt::StringListRef privileges = grt::StringListRef::cast_from(result["privileges"]);
ensure("95.1", privileges.is_valid());
ensure("95.2", privileges.get_index("all privileges") == 0);
ensure_equals("95.3", *grt::StringRef::cast_from(result["target"]), "table a");
grt::DictRef users = grt::DictRef::cast_from(result["users"]);
ensure("95.4", users.is_valid());
ensure("95.5", users.has_key("current_user"));
grt::DictRef user = grt::DictRef::cast_from(users["current_user"]);
ensure("95.6", user.has_key("user"));
ensure_equals("95.7", *grt::StringRef::cast_from(user["user"]), "current_user");
result = _services->parseStatement(_context, _tester.grt, "grant all privileges on table *.* to CURRENT_USER() identified by password 'blah'");
privileges = grt::StringListRef::cast_from(result["privileges"]);
ensure("95.8", privileges.is_valid());
ensure_equals("95.9", *grt::StringRef::cast_from(privileges[0]), "all privileges");
ensure_equals("95.10", *grt::StringRef::cast_from(result["target"]), "table *.*");
users = grt::DictRef::cast_from(result["users"]);
ensure("95.11", users.is_valid());
user = grt::DictRef::cast_from(users["CURRENT_USER"]);
ensure_equals("95.12", *grt::StringRef::cast_from(user["user"]), "CURRENT_USER");
ensure_equals("95.13", *grt::StringRef::cast_from(user["id_method"]), "PASSWORD");
ensure_equals("95.14", *grt::StringRef::cast_from(user["id_string"]), "blah");
result = _services->parseStatement(_context, _tester.grt, "grant all privileges on x.* to mike identified with 'blah' by 'blubb'");
privileges = grt::StringListRef::cast_from(result["privileges"]);
ensure("95.15", privileges.is_valid());
ensure_equals("95.16", *grt::StringRef::cast_from(privileges[0]), "all privileges");
ensure_equals("95.17", *grt::StringRef::cast_from(result["target"]), "x.*");
users = grt::DictRef::cast_from(result["users"]);
ensure("95.18", users.is_valid());
user = grt::DictRef::cast_from(users["mike"]);
ensure("95.19", user.is_valid());
ensure_equals("95.20", *grt::StringRef::cast_from(user["user"]), "mike");
ensure_equals("95.21", *grt::StringRef::cast_from(user["id_method"]), "blah");
ensure_equals("95.22", *grt::StringRef::cast_from(user["id_string"]), "blubb");
result = _services->parseStatement(_context, _tester.grt, "grant all privileges on function x.y to mike\t@\nhome");
privileges = grt::StringListRef::cast_from(result["privileges"]);
ensure("95.23", privileges.is_valid());
ensure_equals("95.24", *grt::StringRef::cast_from(privileges[0]), "all privileges");
ensure_equals("95.25", *grt::StringRef::cast_from(result["target"]), "function x.y");
users = grt::DictRef::cast_from(result["users"]);
ensure("95.26", users.is_valid());
user = grt::DictRef::cast_from(users["mike"]);
ensure("95.27", user.is_valid());
ensure_equals("95.28", *grt::StringRef::cast_from(user["user"]), "mike");
ensure("95.29", !user["id_method"].is_valid());
ensure("95.30", !user["id_string"].is_valid());
ensure_equals("95.31", *grt::StringRef::cast_from(user["host"]), "home");
result = _services->parseStatement(_context, _tester.grt, "grant select on pizza to me require X509 with grant option");
privileges = grt::StringListRef::cast_from(result["privileges"]);
ensure("95.32", privileges.is_valid());
ensure_equals("95.33", *grt::StringRef::cast_from(privileges[0]), "select");
ensure_equals("95.34", *grt::StringRef::cast_from(result["target"]), "pizza");
users = grt::DictRef::cast_from(result["users"]);
ensure("95.35", users.is_valid());
user = grt::DictRef::cast_from(users["me"]);
ensure("95.36", user.is_valid());
ensure_equals("95.37", *grt::StringRef::cast_from(user["user"]), "me");
ensure_equals("95.38", user.count(), 1UL);
// Everything possible in a grant statements.
std::string sql = base::wstring_to_string(L"grant insert (a), insert (b), insert(c), update(a), "
L"alter routine, create routine, "
L"create tablespace, create\t\t\t temporary tables, create user, create view, delete, drop, event, "
L"execute, file, grant option, index, insert, insert (a, b, c, d, ⌚️, ♨️), lock tables, process, proxy, "
L"references (a, b, c, d, ⌚️, ♨️), reload, replication client, select, select (a, b, c, d, ⌚️, ♨️), "
L"show databases, show view, shutdown, super, trigger, update, update (a, b, c, d, ⌚️, ♨️), usage "
L"on *.* to current_user, CURRENT_USER() identified by password 'blah', mike identified with 'blah' by 'blubb', "
L"mike@home require cipher 'abc' and cipher 'xyz' issuer 'a' subject 'b' and issuer '⌚️' with "
L"grant option max_queries_per_hour 1 max_updates_per_hour 2 max_connections_per_hour 3 "
L"max_user_connections 4 max_queries_per_hour 111 max_queries_per_hour 111 max_queries_per_hour 222");
result = _services->parseStatement(_context, _tester.grt, sql);
privileges = grt::StringListRef::cast_from(result["privileges"]);
ensure("95.39", privileges.is_valid());
ensure_equals("95.40", *grt::StringRef::cast_from(privileges[0]), "insert (a)");
ensure_equals("95.41", *grt::StringRef::cast_from(privileges[7]), "create temporary tables");
ensure_equals("95.42", *grt::StringRef::cast_from(privileges[11]), "drop");
ensure_equals("95.43", *grt::StringRef::cast_from(privileges[15]), "grant option");
ensure_equals("95.44", *grt::StringRef::cast_from(privileges[18]), base::wstring_to_string(L"insert (a, b, c, d, ⌚️, ♨️)"));
ensure_equals("95.45", *grt::StringRef::cast_from(privileges[27]), "show databases");
ensure_equals("95.46", *grt::StringRef::cast_from(result["target"]), "*.*");
users = grt::DictRef::cast_from(result["users"]);
ensure("95.48", users.is_valid());
ensure_equals("95.49", users.count(), 3UL);
ensure("95.40", users["current_user"].is_valid());
ensure("95.50", users["CURRENT_USER"].is_valid());
ensure("95.51", users["mike"].is_valid());
user = grt::DictRef::cast_from(users["CURRENT_USER"]);
ensure_equals("95.52", *grt::StringRef::cast_from(user["user"]), "CURRENT_USER");
ensure_equals("95.53", user.count(), 3UL);
grt::DictRef options = grt::DictRef::cast_from(result["options"]);
ensure("95.54", options.is_valid());
ensure_equals("95.55", options.count(), 5UL);
ensure_equals("95.56", *grt::StringRef::cast_from(options["grant"]), "");
ensure_equals("95.57", *grt::StringRef::cast_from(options["max_queries_per_hour"]), "222");
grt::DictRef requirements = grt::DictRef::cast_from(result["requirements"]);
ensure("95.58", requirements.is_valid());
ensure_equals("95.59", requirements.count(), 3UL);
ensure_equals("95.60", *grt::StringRef::cast_from(requirements["cipher"]), "xyz");
ensure_equals("95.61", *grt::StringRef::cast_from(requirements["issuer"]), base::wstring_to_string(L"⌚️"));
}
// table_administration_statement
// install_uninstall_statment
// set_statement
// show_statement
// other_administrative_statement
// utility_statement
END_TESTS
|