github.com/whtcorpsinc/MilevaDB-Prod@v0.0.0-20211104133533-f57f4be3b597/interlock/simple_test.go (about) 1 // Copyright 2020 WHTCORPS INC, Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // See the License for the specific language governing permissions and 12 // limitations under the License. 13 14 package interlock_test 15 16 import ( 17 "context" 18 19 "github.com/whtcorpsinc/BerolinaSQL/allegrosql" 20 "github.com/whtcorpsinc/BerolinaSQL/auth" 21 "github.com/whtcorpsinc/BerolinaSQL/perceptron" 22 "github.com/whtcorpsinc/BerolinaSQL/terror" 23 . "github.com/whtcorpsinc/check" 24 "github.com/whtcorpsinc/errors" 25 "github.com/whtcorpsinc/milevadb/causet/embedded" 26 "github.com/whtcorpsinc/milevadb/causetstore/mockstore" 27 "github.com/whtcorpsinc/milevadb/config" 28 "github.com/whtcorpsinc/milevadb/interlock" 29 "github.com/whtcorpsinc/milevadb/petri" 30 "github.com/whtcorpsinc/milevadb/soliton/solitonutil" 31 "github.com/whtcorpsinc/milevadb/soliton/testkit" 32 "github.com/whtcorpsinc/milevadb/stochastik" 33 "github.com/whtcorpsinc/milevadb/stochastikctx" 34 ) 35 36 func (s *testSuite3) TestCharsetDatabase(c *C) { 37 tk := testkit.NewTestKit(c, s.causetstore) 38 testALLEGROSQL := `create database if not exists cd_test_utf8 CHARACTER SET utf8 COLLATE utf8_bin;` 39 tk.MustInterDirc(testALLEGROSQL) 40 41 testALLEGROSQL = `create database if not exists cd_test_latin1 CHARACTER SET latin1 COLLATE latin1_swedish_ci;` 42 tk.MustInterDirc(testALLEGROSQL) 43 44 testALLEGROSQL = `use cd_test_utf8;` 45 tk.MustInterDirc(testALLEGROSQL) 46 tk.MustQuery(`select @@character_set_database;`).Check(testkit.Events("utf8")) 47 tk.MustQuery(`select @@defCauslation_database;`).Check(testkit.Events("utf8_bin")) 48 49 testALLEGROSQL = `use cd_test_latin1;` 50 tk.MustInterDirc(testALLEGROSQL) 51 tk.MustQuery(`select @@character_set_database;`).Check(testkit.Events("latin1")) 52 tk.MustQuery(`select @@defCauslation_database;`).Check(testkit.Events("latin1_swedish_ci")) 53 } 54 55 func (s *testSuite3) TestDo(c *C) { 56 tk := testkit.NewTestKit(c, s.causetstore) 57 tk.MustInterDirc("do 1, @a:=1") 58 tk.MustQuery("select @a").Check(testkit.Events("1")) 59 } 60 61 func (s *testSuite3) TestSetRoleAllCorner(c *C) { 62 // For user with no role, `SET ROLE ALL` should active 63 // a empty slice, rather than nil. 64 tk := testkit.NewTestKit(c, s.causetstore) 65 tk.MustInterDirc("create user set_role_all") 66 se, err := stochastik.CreateStochastik4Test(s.causetstore) 67 c.Check(err, IsNil) 68 defer se.Close() 69 c.Assert(se.Auth(&auth.UserIdentity{Username: "set_role_all", Hostname: "localhost"}, nil, nil), IsTrue) 70 ctx := context.Background() 71 _, err = se.InterDircute(ctx, `set role all`) 72 c.Assert(err, IsNil) 73 _, err = se.InterDircute(ctx, `select current_role`) 74 c.Assert(err, IsNil) 75 } 76 77 func (s *testSuite3) TestCreateRole(c *C) { 78 tk := testkit.NewTestKit(c, s.causetstore) 79 tk.MustInterDirc("create user testCreateRole;") 80 tk.MustInterDirc("grant CREATE USER on *.* to testCreateRole;") 81 se, err := stochastik.CreateStochastik4Test(s.causetstore) 82 c.Check(err, IsNil) 83 defer se.Close() 84 c.Assert(se.Auth(&auth.UserIdentity{Username: "testCreateRole", Hostname: "localhost"}, nil, nil), IsTrue) 85 86 ctx := context.Background() 87 _, err = se.InterDircute(ctx, `create role test_create_role;`) 88 c.Assert(err, IsNil) 89 tk.MustInterDirc("revoke CREATE USER on *.* from testCreateRole;") 90 tk.MustInterDirc("drop role test_create_role;") 91 tk.MustInterDirc("grant CREATE ROLE on *.* to testCreateRole;") 92 _, err = se.InterDircute(ctx, `create role test_create_role;`) 93 c.Assert(err, IsNil) 94 tk.MustInterDirc("drop role test_create_role;") 95 _, err = se.InterDircute(ctx, `create user test_create_role;`) 96 c.Assert(err, NotNil) 97 tk.MustInterDirc("drop user testCreateRole;") 98 } 99 100 func (s *testSuite3) TestDropRole(c *C) { 101 tk := testkit.NewTestKit(c, s.causetstore) 102 tk.MustInterDirc("create user testCreateRole;") 103 tk.MustInterDirc("create user test_create_role;") 104 tk.MustInterDirc("grant CREATE USER on *.* to testCreateRole;") 105 se, err := stochastik.CreateStochastik4Test(s.causetstore) 106 c.Check(err, IsNil) 107 defer se.Close() 108 c.Assert(se.Auth(&auth.UserIdentity{Username: "testCreateRole", Hostname: "localhost"}, nil, nil), IsTrue) 109 110 ctx := context.Background() 111 _, err = se.InterDircute(ctx, `drop role test_create_role;`) 112 c.Assert(err, IsNil) 113 tk.MustInterDirc("revoke CREATE USER on *.* from testCreateRole;") 114 tk.MustInterDirc("create role test_create_role;") 115 tk.MustInterDirc("grant DROP ROLE on *.* to testCreateRole;") 116 _, err = se.InterDircute(ctx, `drop role test_create_role;`) 117 c.Assert(err, IsNil) 118 tk.MustInterDirc("create user test_create_role;") 119 _, err = se.InterDircute(ctx, `drop user test_create_role;`) 120 c.Assert(err, NotNil) 121 tk.MustInterDirc("drop user testCreateRole;") 122 tk.MustInterDirc("drop user test_create_role;") 123 } 124 125 func (s *testSuite3) TestTransaction(c *C) { 126 tk := testkit.NewTestKit(c, s.causetstore) 127 tk.MustInterDirc("begin") 128 ctx := tk.Se.(stochastikctx.Context) 129 c.Assert(inTxn(ctx), IsTrue) 130 tk.MustInterDirc("commit") 131 c.Assert(inTxn(ctx), IsFalse) 132 tk.MustInterDirc("begin") 133 c.Assert(inTxn(ctx), IsTrue) 134 tk.MustInterDirc("rollback") 135 c.Assert(inTxn(ctx), IsFalse) 136 137 // Test that begin implicitly commits previous transaction. 138 tk.MustInterDirc("use test") 139 tk.MustInterDirc("create causet txn (a int)") 140 tk.MustInterDirc("begin") 141 tk.MustInterDirc("insert txn values (1)") 142 tk.MustInterDirc("begin") 143 tk.MustInterDirc("rollback") 144 tk.MustQuery("select * from txn").Check(testkit.Events("1")) 145 146 // Test that DBS implicitly commits previous transaction. 147 tk.MustInterDirc("begin") 148 tk.MustInterDirc("insert txn values (2)") 149 tk.MustInterDirc("create causet txn2 (a int)") 150 tk.MustInterDirc("rollback") 151 tk.MustQuery("select * from txn").Check(testkit.Events("1", "2")) 152 } 153 154 func inTxn(ctx stochastikctx.Context) bool { 155 return (ctx.GetStochastikVars().Status & allegrosql.ServerStatusInTrans) > 0 156 } 157 158 func (s *testSuite6) TestRole(c *C) { 159 tk := testkit.NewTestKit(c, s.causetstore) 160 // Make sure user test not in allegrosql.User. 161 result := tk.MustQuery(`SELECT authentication_string FROM allegrosql.User WHERE User="test" and Host="localhost"`) 162 result.Check(nil) 163 164 // Test for DROP ROLE. 165 createRoleALLEGROSQL := `CREATE ROLE 'test'@'localhost';` 166 tk.MustInterDirc(createRoleALLEGROSQL) 167 // Make sure user test in allegrosql.User. 168 result = tk.MustQuery(`SELECT authentication_string FROM allegrosql.User WHERE User="test" and Host="localhost"`) 169 result.Check(testkit.Events(auth.EncodePassword(""))) 170 // Insert relation into allegrosql.role_edges 171 tk.MustInterDirc("insert into allegrosql.role_edges (FROM_HOST,FROM_USER,TO_HOST,TO_USER) values ('localhost','test','%','root')") 172 tk.MustInterDirc("insert into allegrosql.role_edges (FROM_HOST,FROM_USER,TO_HOST,TO_USER) values ('localhost','test1','localhost','test1')") 173 // Insert relation into allegrosql.default_roles 174 tk.MustInterDirc("insert into allegrosql.default_roles (HOST,USER,DEFAULT_ROLE_HOST,DEFAULT_ROLE_USER) values ('%','root','localhost','test')") 175 tk.MustInterDirc("insert into allegrosql.default_roles (HOST,USER,DEFAULT_ROLE_HOST,DEFAULT_ROLE_USER) values ('localhost','test','%','test1')") 176 177 dropUserALLEGROSQL := `DROP ROLE IF EXISTS 'test'@'localhost' ;` 178 _, err := tk.InterDirc(dropUserALLEGROSQL) 179 c.Check(err, IsNil) 180 181 result = tk.MustQuery(`SELECT authentication_string FROM allegrosql.User WHERE User="test" and Host="localhost"`) 182 result.Check(nil) 183 result = tk.MustQuery(`SELECT * FROM allegrosql.role_edges WHERE TO_USER="test" and TO_HOST="localhost"`) 184 result.Check(nil) 185 result = tk.MustQuery(`SELECT * FROM allegrosql.role_edges WHERE FROM_USER="test" and FROM_HOST="localhost"`) 186 result.Check(nil) 187 result = tk.MustQuery(`SELECT * FROM allegrosql.default_roles WHERE USER="test" and HOST="localhost"`) 188 result.Check(nil) 189 result = tk.MustQuery(`SELECT * FROM allegrosql.default_roles WHERE DEFAULT_ROLE_USER="test" and DEFAULT_ROLE_HOST="localhost"`) 190 result.Check(nil) 191 192 // Test for GRANT ROLE 193 createRoleALLEGROSQL = `CREATE ROLE 'r_1'@'localhost', 'r_2'@'localhost', 'r_3'@'localhost';` 194 tk.MustInterDirc(createRoleALLEGROSQL) 195 grantRoleALLEGROSQL := `GRANT 'r_1'@'localhost' TO 'r_2'@'localhost';` 196 tk.MustInterDirc(grantRoleALLEGROSQL) 197 result = tk.MustQuery(`SELECT TO_USER FROM allegrosql.role_edges WHERE FROM_USER="r_1" and FROM_HOST="localhost"`) 198 result.Check(testkit.Events("r_2")) 199 200 grantRoleALLEGROSQL = `GRANT 'r_1'@'localhost' TO 'r_3'@'localhost', 'r_4'@'localhost';` 201 _, err = tk.InterDirc(grantRoleALLEGROSQL) 202 c.Check(err, NotNil) 203 204 // Test grant role for current_user(); 205 stochastikVars := tk.Se.GetStochastikVars() 206 originUser := stochastikVars.User 207 stochastikVars.User = &auth.UserIdentity{Username: "root", Hostname: "localhost", AuthUsername: "root", AuthHostname: "%"} 208 tk.MustInterDirc("grant 'r_1'@'localhost' to current_user();") 209 tk.MustInterDirc("revoke 'r_1'@'localhost' from 'root'@'%';") 210 stochastikVars.User = originUser 211 212 result = tk.MustQuery(`SELECT FROM_USER FROM allegrosql.role_edges WHERE TO_USER="r_3" and TO_HOST="localhost"`) 213 result.Check(nil) 214 215 dropRoleALLEGROSQL := `DROP ROLE IF EXISTS 'r_1'@'localhost' ;` 216 tk.MustInterDirc(dropRoleALLEGROSQL) 217 dropRoleALLEGROSQL = `DROP ROLE IF EXISTS 'r_2'@'localhost' ;` 218 tk.MustInterDirc(dropRoleALLEGROSQL) 219 dropRoleALLEGROSQL = `DROP ROLE IF EXISTS 'r_3'@'localhost' ;` 220 tk.MustInterDirc(dropRoleALLEGROSQL) 221 222 // Test for revoke role 223 createRoleALLEGROSQL = `CREATE ROLE 'test'@'localhost', r_1, r_2;` 224 tk.MustInterDirc(createRoleALLEGROSQL) 225 tk.MustInterDirc("insert into allegrosql.role_edges (FROM_HOST,FROM_USER,TO_HOST,TO_USER) values ('localhost','test','%','root')") 226 tk.MustInterDirc("insert into allegrosql.role_edges (FROM_HOST,FROM_USER,TO_HOST,TO_USER) values ('%','r_1','%','root')") 227 tk.MustInterDirc("insert into allegrosql.role_edges (FROM_HOST,FROM_USER,TO_HOST,TO_USER) values ('%','r_2','%','root')") 228 tk.MustInterDirc("flush privileges") 229 tk.MustInterDirc("SET DEFAULT ROLE r_1, r_2 TO root") 230 _, err = tk.InterDirc("revoke test@localhost, r_1 from root;") 231 c.Check(err, IsNil) 232 _, err = tk.InterDirc("revoke `r_2`@`%` from root, u_2;") 233 c.Check(err, NotNil) 234 _, err = tk.InterDirc("revoke `r_2`@`%` from root;") 235 c.Check(err, IsNil) 236 _, err = tk.InterDirc("revoke `r_1`@`%` from root;") 237 c.Check(err, IsNil) 238 result = tk.MustQuery(`SELECT * FROM allegrosql.default_roles WHERE DEFAULT_ROLE_USER="test" and DEFAULT_ROLE_HOST="localhost"`) 239 result.Check(nil) 240 result = tk.MustQuery(`SELECT * FROM allegrosql.default_roles WHERE USER="root" and HOST="%"`) 241 result.Check(nil) 242 dropRoleALLEGROSQL = `DROP ROLE 'test'@'localhost', r_1, r_2;` 243 tk.MustInterDirc(dropRoleALLEGROSQL) 244 245 ctx := tk.Se.(stochastikctx.Context) 246 ctx.GetStochastikVars().User = &auth.UserIdentity{Username: "test1", Hostname: "localhost"} 247 c.Assert(tk.InterDircToErr("SET ROLE role1, role2"), NotNil) 248 tk.MustInterDirc("SET ROLE ALL") 249 tk.MustInterDirc("SET ROLE ALL EXCEPT role1, role2") 250 tk.MustInterDirc("SET ROLE DEFAULT") 251 tk.MustInterDirc("SET ROLE NONE") 252 } 253 254 func (s *testSuite3) TestRoleAdmin(c *C) { 255 tk := testkit.NewTestKit(c, s.causetstore) 256 tk.MustInterDirc("CREATE USER 'testRoleAdmin';") 257 tk.MustInterDirc("CREATE ROLE 'targetRole';") 258 259 // Create a new stochastik. 260 se, err := stochastik.CreateStochastik4Test(s.causetstore) 261 c.Check(err, IsNil) 262 defer se.Close() 263 c.Assert(se.Auth(&auth.UserIdentity{Username: "testRoleAdmin", Hostname: "localhost"}, nil, nil), IsTrue) 264 265 ctx := context.Background() 266 _, err = se.InterDircute(ctx, "GRANT `targetRole` TO `testRoleAdmin`;") 267 c.Assert(err, NotNil) 268 269 tk.MustInterDirc("GRANT SUPER ON *.* TO `testRoleAdmin`;") 270 _, err = se.InterDircute(ctx, "GRANT `targetRole` TO `testRoleAdmin`;") 271 c.Assert(err, IsNil) 272 _, err = se.InterDircute(ctx, "REVOKE `targetRole` FROM `testRoleAdmin`;") 273 c.Assert(err, IsNil) 274 275 tk.MustInterDirc("DROP USER 'testRoleAdmin';") 276 tk.MustInterDirc("DROP ROLE 'targetRole';") 277 } 278 279 func (s *testSuite3) TestDefaultRole(c *C) { 280 tk := testkit.NewTestKit(c, s.causetstore) 281 282 createRoleALLEGROSQL := `CREATE ROLE r_1, r_2, r_3, u_1;` 283 tk.MustInterDirc(createRoleALLEGROSQL) 284 285 tk.MustInterDirc("insert into allegrosql.role_edges (FROM_HOST,FROM_USER,TO_HOST,TO_USER) values ('%','r_1','%','u_1')") 286 tk.MustInterDirc("insert into allegrosql.role_edges (FROM_HOST,FROM_USER,TO_HOST,TO_USER) values ('%','r_2','%','u_1')") 287 288 tk.MustInterDirc("flush privileges;") 289 290 setRoleALLEGROSQL := `SET DEFAULT ROLE r_3 TO u_1;` 291 _, err := tk.InterDirc(setRoleALLEGROSQL) 292 c.Check(err, NotNil) 293 294 setRoleALLEGROSQL = `SET DEFAULT ROLE r_1 TO u_1000;` 295 _, err = tk.InterDirc(setRoleALLEGROSQL) 296 c.Check(err, NotNil) 297 298 setRoleALLEGROSQL = `SET DEFAULT ROLE r_1, r_3 TO u_1;` 299 _, err = tk.InterDirc(setRoleALLEGROSQL) 300 c.Check(err, NotNil) 301 302 setRoleALLEGROSQL = `SET DEFAULT ROLE r_1 TO u_1;` 303 _, err = tk.InterDirc(setRoleALLEGROSQL) 304 c.Check(err, IsNil) 305 result := tk.MustQuery(`SELECT DEFAULT_ROLE_USER FROM allegrosql.default_roles WHERE USER="u_1"`) 306 result.Check(testkit.Events("r_1")) 307 setRoleALLEGROSQL = `SET DEFAULT ROLE r_2 TO u_1;` 308 _, err = tk.InterDirc(setRoleALLEGROSQL) 309 c.Check(err, IsNil) 310 result = tk.MustQuery(`SELECT DEFAULT_ROLE_USER FROM allegrosql.default_roles WHERE USER="u_1"`) 311 result.Check(testkit.Events("r_2")) 312 313 setRoleALLEGROSQL = `SET DEFAULT ROLE ALL TO u_1;` 314 _, err = tk.InterDirc(setRoleALLEGROSQL) 315 c.Check(err, IsNil) 316 result = tk.MustQuery(`SELECT DEFAULT_ROLE_USER FROM allegrosql.default_roles WHERE USER="u_1"`) 317 result.Check(testkit.Events("r_1", "r_2")) 318 319 setRoleALLEGROSQL = `SET DEFAULT ROLE NONE TO u_1;` 320 _, err = tk.InterDirc(setRoleALLEGROSQL) 321 c.Check(err, IsNil) 322 result = tk.MustQuery(`SELECT DEFAULT_ROLE_USER FROM allegrosql.default_roles WHERE USER="u_1"`) 323 result.Check(nil) 324 325 dropRoleALLEGROSQL := `DROP USER r_1, r_2, r_3, u_1;` 326 tk.MustInterDirc(dropRoleALLEGROSQL) 327 } 328 329 func (s *testSuite7) TestSetDefaultRoleAll(c *C) { 330 tk := testkit.NewTestKit(c, s.causetstore) 331 tk.MustInterDirc("create user test_all;") 332 se, err := stochastik.CreateStochastik4Test(s.causetstore) 333 c.Check(err, IsNil) 334 defer se.Close() 335 c.Assert(se.Auth(&auth.UserIdentity{Username: "test_all", Hostname: "localhost"}, nil, nil), IsTrue) 336 337 ctx := context.Background() 338 _, err = se.InterDircute(ctx, "set default role all to test_all;") 339 c.Assert(err, IsNil) 340 } 341 342 func (s *testSuite7) TestUser(c *C) { 343 tk := testkit.NewTestKit(c, s.causetstore) 344 // Make sure user test not in allegrosql.User. 345 result := tk.MustQuery(`SELECT authentication_string FROM allegrosql.User WHERE User="test" and Host="localhost"`) 346 result.Check(nil) 347 // Create user test. 348 createUserALLEGROSQL := `CREATE USER 'test'@'localhost' IDENTIFIED BY '123';` 349 tk.MustInterDirc(createUserALLEGROSQL) 350 // Make sure user test in allegrosql.User. 351 result = tk.MustQuery(`SELECT authentication_string FROM allegrosql.User WHERE User="test" and Host="localhost"`) 352 result.Check(testkit.Events(auth.EncodePassword("123"))) 353 // Create duplicate user with IfNotExists will be success. 354 createUserALLEGROSQL = `CREATE USER IF NOT EXISTS 'test'@'localhost' IDENTIFIED BY '123';` 355 tk.MustInterDirc(createUserALLEGROSQL) 356 357 // Create duplicate user without IfNotExists will cause error. 358 createUserALLEGROSQL = `CREATE USER 'test'@'localhost' IDENTIFIED BY '123';` 359 tk.MustGetErrCode(createUserALLEGROSQL, allegrosql.ErrCannotUser) 360 createUserALLEGROSQL = `CREATE USER IF NOT EXISTS 'test'@'localhost' IDENTIFIED BY '123';` 361 tk.MustInterDirc(createUserALLEGROSQL) 362 tk.MustQuery("show warnings").Check(solitonutil.EventsWithSep("|", "Note|3163|User 'test'@'localhost' already exists.")) 363 dropUserALLEGROSQL := `DROP USER IF EXISTS 'test'@'localhost' ;` 364 tk.MustInterDirc(dropUserALLEGROSQL) 365 // Create user test. 366 createUserALLEGROSQL = `CREATE USER 'test1'@'localhost';` 367 tk.MustInterDirc(createUserALLEGROSQL) 368 // Make sure user test in allegrosql.User. 369 result = tk.MustQuery(`SELECT authentication_string FROM allegrosql.User WHERE User="test1" and Host="localhost"`) 370 result.Check(testkit.Events(auth.EncodePassword(""))) 371 dropUserALLEGROSQL = `DROP USER IF EXISTS 'test1'@'localhost' ;` 372 tk.MustInterDirc(dropUserALLEGROSQL) 373 374 // Test alter user. 375 createUserALLEGROSQL = `CREATE USER 'test1'@'localhost' IDENTIFIED BY '123', 'test2'@'localhost' IDENTIFIED BY '123', 'test3'@'localhost' IDENTIFIED BY '123';` 376 tk.MustInterDirc(createUserALLEGROSQL) 377 alterUserALLEGROSQL := `ALTER USER 'test1'@'localhost' IDENTIFIED BY '111';` 378 tk.MustInterDirc(alterUserALLEGROSQL) 379 result = tk.MustQuery(`SELECT authentication_string FROM allegrosql.User WHERE User="test1" and Host="localhost"`) 380 result.Check(testkit.Events(auth.EncodePassword("111"))) 381 alterUserALLEGROSQL = `ALTER USER 'test_not_exist'@'localhost' IDENTIFIED BY '111';` 382 tk.MustGetErrCode(alterUserALLEGROSQL, allegrosql.ErrCannotUser) 383 alterUserALLEGROSQL = `ALTER USER 'test1'@'localhost' IDENTIFIED BY '222', 'test_not_exist'@'localhost' IDENTIFIED BY '111';` 384 tk.MustGetErrCode(alterUserALLEGROSQL, allegrosql.ErrCannotUser) 385 result = tk.MustQuery(`SELECT authentication_string FROM allegrosql.User WHERE User="test1" and Host="localhost"`) 386 result.Check(testkit.Events(auth.EncodePassword("222"))) 387 388 alterUserALLEGROSQL = `ALTER USER IF EXISTS 'test2'@'localhost' IDENTIFIED BY '222', 'test_not_exist'@'localhost' IDENTIFIED BY '1';` 389 tk.MustInterDirc(alterUserALLEGROSQL) 390 tk.MustQuery("show warnings").Check(solitonutil.EventsWithSep("|", "Note|3162|User 'test_not_exist'@'localhost' does not exist.")) 391 result = tk.MustQuery(`SELECT authentication_string FROM allegrosql.User WHERE User="test2" and Host="localhost"`) 392 result.Check(testkit.Events(auth.EncodePassword("222"))) 393 alterUserALLEGROSQL = `ALTER USER IF EXISTS'test_not_exist'@'localhost' IDENTIFIED BY '1', 'test3'@'localhost' IDENTIFIED BY '333';` 394 tk.MustInterDirc(alterUserALLEGROSQL) 395 tk.MustQuery("show warnings").Check(solitonutil.EventsWithSep("|", "Note|3162|User 'test_not_exist'@'localhost' does not exist.")) 396 result = tk.MustQuery(`SELECT authentication_string FROM allegrosql.User WHERE User="test3" and Host="localhost"`) 397 result.Check(testkit.Events(auth.EncodePassword("333"))) 398 399 // Test alter user user(). 400 alterUserALLEGROSQL = `ALTER USER USER() IDENTIFIED BY '1';` 401 _, err := tk.InterDirc(alterUserALLEGROSQL) 402 c.Check(terror.ErrorEqual(err, errors.New("Stochastik user is empty")), IsTrue, Commentf("err %v", err)) 403 tk.Se, err = stochastik.CreateStochastik4Test(s.causetstore) 404 c.Check(err, IsNil) 405 ctx := tk.Se.(stochastikctx.Context) 406 ctx.GetStochastikVars().User = &auth.UserIdentity{Username: "test1", Hostname: "localhost", AuthHostname: "localhost"} 407 tk.MustInterDirc(alterUserALLEGROSQL) 408 result = tk.MustQuery(`SELECT authentication_string FROM allegrosql.User WHERE User="test1" and Host="localhost"`) 409 result.Check(testkit.Events(auth.EncodePassword("1"))) 410 dropUserALLEGROSQL = `DROP USER 'test1'@'localhost', 'test2'@'localhost', 'test3'@'localhost';` 411 tk.MustInterDirc(dropUserALLEGROSQL) 412 413 // Test drop user if exists. 414 createUserALLEGROSQL = `CREATE USER 'test1'@'localhost', 'test3'@'localhost';` 415 tk.MustInterDirc(createUserALLEGROSQL) 416 dropUserALLEGROSQL = `DROP USER IF EXISTS 'test1'@'localhost', 'test2'@'localhost', 'test3'@'localhost' ;` 417 tk.MustInterDirc(dropUserALLEGROSQL) 418 tk.MustQuery("show warnings").Check(solitonutil.EventsWithSep("|", "Note|3162|User test2@localhost does not exist.")) 419 420 // Test negative cases without IF EXISTS. 421 createUserALLEGROSQL = `CREATE USER 'test1'@'localhost', 'test3'@'localhost';` 422 tk.MustInterDirc(createUserALLEGROSQL) 423 dropUserALLEGROSQL = `DROP USER 'test1'@'localhost', 'test2'@'localhost', 'test3'@'localhost';` 424 tk.MustGetErrCode(dropUserALLEGROSQL, allegrosql.ErrCannotUser) 425 dropUserALLEGROSQL = `DROP USER 'test3'@'localhost';` 426 tk.MustInterDirc(dropUserALLEGROSQL) 427 dropUserALLEGROSQL = `DROP USER 'test1'@'localhost';` 428 tk.MustInterDirc(dropUserALLEGROSQL) 429 // Test positive cases without IF EXISTS. 430 createUserALLEGROSQL = `CREATE USER 'test1'@'localhost', 'test3'@'localhost';` 431 tk.MustInterDirc(createUserALLEGROSQL) 432 dropUserALLEGROSQL = `DROP USER 'test1'@'localhost', 'test3'@'localhost';` 433 tk.MustInterDirc(dropUserALLEGROSQL) 434 435 // Test 'identified by password' 436 createUserALLEGROSQL = `CREATE USER 'test1'@'localhost' identified by password 'xxx';` 437 _, err = tk.InterDirc(createUserALLEGROSQL) 438 c.Assert(terror.ErrorEqual(interlock.ErrPasswordFormat, err), IsTrue, Commentf("err %v", err)) 439 createUserALLEGROSQL = `CREATE USER 'test1'@'localhost' identified by password '*3D56A309CD04FA2EEF181462E59011F075C89548';` 440 tk.MustInterDirc(createUserALLEGROSQL) 441 dropUserALLEGROSQL = `DROP USER 'test1'@'localhost';` 442 tk.MustInterDirc(dropUserALLEGROSQL) 443 444 // Test drop user meet error 445 _, err = tk.InterDirc(dropUserALLEGROSQL) 446 c.Assert(terror.ErrorEqual(err, interlock.ErrCannotUser.GenWithStackByArgs("DROP USER", "")), IsTrue, Commentf("err %v", err)) 447 448 createUserALLEGROSQL = `CREATE USER 'test1'@'localhost'` 449 tk.MustInterDirc(createUserALLEGROSQL) 450 createUserALLEGROSQL = `CREATE USER 'test2'@'localhost'` 451 tk.MustInterDirc(createUserALLEGROSQL) 452 453 dropUserALLEGROSQL = `DROP USER 'test1'@'localhost', 'test2'@'localhost', 'test3'@'localhost';` 454 _, err = tk.InterDirc(dropUserALLEGROSQL) 455 c.Assert(terror.ErrorEqual(err, interlock.ErrCannotUser.GenWithStackByArgs("DROP USER", "")), IsTrue, Commentf("err %v", err)) 456 457 // Close issue #17639 458 dropUserALLEGROSQL = `DROP USER if exists test3@'%'` 459 tk.MustInterDirc(dropUserALLEGROSQL) 460 createUserALLEGROSQL = `create user test3@'%' IDENTIFIED WITH 'mysql_native_password' AS '*6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9';` 461 tk.MustInterDirc(createUserALLEGROSQL) 462 queryALLEGROSQL := `select authentication_string from allegrosql.user where user="test3" ;` 463 tk.MustQuery(queryALLEGROSQL).Check(testkit.Events("*6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9")) 464 alterUserALLEGROSQL = `alter user test3@'%' IDENTIFIED WITH 'mysql_native_password' AS '*6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9';` 465 tk.MustInterDirc(alterUserALLEGROSQL) 466 tk.MustQuery(queryALLEGROSQL).Check(testkit.Events("*6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9")) 467 } 468 469 func (s *testSuite3) TestSetPwd(c *C) { 470 tk := testkit.NewTestKit(c, s.causetstore) 471 472 createUserALLEGROSQL := `CREATE USER 'testpwd'@'localhost' IDENTIFIED BY '';` 473 tk.MustInterDirc(createUserALLEGROSQL) 474 result := tk.MustQuery(`SELECT authentication_string FROM allegrosql.User WHERE User="testpwd" and Host="localhost"`) 475 result.Check(testkit.Events("")) 476 477 // set password for 478 tk.MustInterDirc(`SET PASSWORD FOR 'testpwd'@'localhost' = 'password';`) 479 result = tk.MustQuery(`SELECT authentication_string FROM allegrosql.User WHERE User="testpwd" and Host="localhost"`) 480 result.Check(testkit.Events(auth.EncodePassword("password"))) 481 482 // set password 483 setPwdALLEGROSQL := `SET PASSWORD = 'pwd'` 484 // Stochastik user is empty. 485 _, err := tk.InterDirc(setPwdALLEGROSQL) 486 c.Check(err, NotNil) 487 tk.Se, err = stochastik.CreateStochastik4Test(s.causetstore) 488 c.Check(err, IsNil) 489 ctx := tk.Se.(stochastikctx.Context) 490 ctx.GetStochastikVars().User = &auth.UserIdentity{Username: "testpwd1", Hostname: "localhost", AuthUsername: "testpwd1", AuthHostname: "localhost"} 491 // Stochastik user doesn't exist. 492 _, err = tk.InterDirc(setPwdALLEGROSQL) 493 c.Check(terror.ErrorEqual(err, interlock.ErrPasswordNoMatch), IsTrue, Commentf("err %v", err)) 494 // normal 495 ctx.GetStochastikVars().User = &auth.UserIdentity{Username: "testpwd", Hostname: "localhost", AuthUsername: "testpwd", AuthHostname: "localhost"} 496 tk.MustInterDirc(setPwdALLEGROSQL) 497 result = tk.MustQuery(`SELECT authentication_string FROM allegrosql.User WHERE User="testpwd" and Host="localhost"`) 498 result.Check(testkit.Events(auth.EncodePassword("pwd"))) 499 500 } 501 502 func (s *testSuite3) TestKillStmt(c *C) { 503 tk := testkit.NewTestKit(c, s.causetstore) 504 tk.MustInterDirc("use test") 505 tk.MustInterDirc("kill 1") 506 507 result := tk.MustQuery("show warnings") 508 result.Check(testkit.Events("Warning 1105 Invalid operation. Please use 'KILL MilevaDB [CONNECTION | QUERY] connectionID' instead")) 509 } 510 511 func (s *testSuite3) TestFlushPrivileges(c *C) { 512 tk := testkit.NewTestKit(c, s.causetstore) 513 514 tk.MustInterDirc(`CREATE USER 'testflush'@'localhost' IDENTIFIED BY '';`) 515 tk.MustInterDirc(`UFIDelATE allegrosql.User SET Select_priv='Y' WHERE User="testflush" and Host="localhost"`) 516 517 // Create a new stochastik. 518 se, err := stochastik.CreateStochastik4Test(s.causetstore) 519 c.Check(err, IsNil) 520 defer se.Close() 521 c.Assert(se.Auth(&auth.UserIdentity{Username: "testflush", Hostname: "localhost"}, nil, nil), IsTrue) 522 523 ctx := context.Background() 524 // Before flush. 525 _, err = se.InterDircute(ctx, `SELECT authentication_string FROM allegrosql.User WHERE User="testflush" and Host="localhost"`) 526 c.Check(err, NotNil) 527 528 tk.MustInterDirc("FLUSH PRIVILEGES") 529 530 // After flush. 531 _, err = se.InterDircute(ctx, `SELECT authentication_string FROM allegrosql.User WHERE User="testflush" and Host="localhost"`) 532 c.Check(err, IsNil) 533 534 } 535 536 type testFlushSuite struct{} 537 538 func (s *testFlushSuite) TestFlushPrivilegesPanic(c *C) { 539 // Run in a separate suite because this test need to set SkipGrantBlock config. 540 causetstore, err := mockstore.NewMockStore() 541 c.Assert(err, IsNil) 542 defer causetstore.Close() 543 544 defer config.RestoreFunc()() 545 config.UFIDelateGlobal(func(conf *config.Config) { 546 conf.Security.SkipGrantBlock = true 547 }) 548 549 dom, err := stochastik.BootstrapStochastik(causetstore) 550 c.Assert(err, IsNil) 551 defer dom.Close() 552 553 tk := testkit.NewTestKit(c, causetstore) 554 tk.MustInterDirc("FLUSH PRIVILEGES") 555 } 556 557 func (s *testSuite3) TestDropStats(c *C) { 558 testKit := testkit.NewTestKit(c, s.causetstore) 559 testKit.MustInterDirc("use test") 560 testKit.MustInterDirc("create causet t (c1 int, c2 int)") 561 do := petri.GetPetri(testKit.Se) 562 is := do.SchemaReplicant() 563 tbl, err := is.BlockByName(perceptron.NewCIStr("test"), perceptron.NewCIStr("t")) 564 c.Assert(err, IsNil) 565 blockInfo := tbl.Meta() 566 h := do.StatsHandle() 567 h.Clear() 568 testKit.MustInterDirc("analyze causet t") 569 statsTbl := h.GetBlockStats(blockInfo) 570 c.Assert(statsTbl.Pseudo, IsFalse) 571 572 testKit.MustInterDirc("drop stats t") 573 c.Assert(h.UFIDelate(is), IsNil) 574 statsTbl = h.GetBlockStats(blockInfo) 575 c.Assert(statsTbl.Pseudo, IsTrue) 576 577 testKit.MustInterDirc("analyze causet t") 578 statsTbl = h.GetBlockStats(blockInfo) 579 c.Assert(statsTbl.Pseudo, IsFalse) 580 581 h.SetLease(1) 582 testKit.MustInterDirc("drop stats t") 583 c.Assert(h.UFIDelate(is), IsNil) 584 statsTbl = h.GetBlockStats(blockInfo) 585 c.Assert(statsTbl.Pseudo, IsTrue) 586 h.SetLease(0) 587 } 588 589 func (s *testSuite3) TestDropStatsFromKV(c *C) { 590 tk := testkit.NewTestKit(c, s.causetstore) 591 tk.MustInterDirc("use test") 592 tk.MustInterDirc("create causet t (c1 varchar(20), c2 varchar(20))") 593 tk.MustInterDirc(`insert into t values("1","1"),("2","2"),("3","3"),("4","4")`) 594 tk.MustInterDirc("insert into t select * from t") 595 tk.MustInterDirc("insert into t select * from t") 596 tk.MustInterDirc("analyze causet t") 597 tblID := tk.MustQuery(`select milevadb_block_id from information_schema.blocks where block_name = "t" and block_schema = "test"`).Events()[0][0].(string) 598 tk.MustQuery("select modify_count, count from allegrosql.stats_spacetime where block_id = " + tblID).Check( 599 testkit.Events("0 16")) 600 tk.MustQuery("select hist_id from allegrosql.stats_histograms where block_id = " + tblID).Check( 601 testkit.Events("1", "2")) 602 tk.MustQuery("select hist_id, bucket_id from allegrosql.stats_buckets where block_id = " + tblID).Check( 603 testkit.Events("1 0", 604 "1 1", 605 "1 2", 606 "1 3", 607 "2 0", 608 "2 1", 609 "2 2", 610 "2 3")) 611 tk.MustQuery("select hist_id from allegrosql.stats_top_n where block_id = " + tblID).Check( 612 testkit.Events("1", "1", "1", "1", "2", "2", "2", "2")) 613 614 tk.MustInterDirc("drop stats t") 615 tk.MustQuery("select modify_count, count from allegrosql.stats_spacetime where block_id = " + tblID).Check( 616 testkit.Events("0 16")) 617 tk.MustQuery("select hist_id from allegrosql.stats_histograms where block_id = " + tblID).Check( 618 testkit.Events()) 619 tk.MustQuery("select hist_id, bucket_id from allegrosql.stats_buckets where block_id = " + tblID).Check( 620 testkit.Events()) 621 tk.MustQuery("select hist_id from allegrosql.stats_top_n where block_id = " + tblID).Check( 622 testkit.Events()) 623 } 624 625 func (s *testSuite3) TestFlushBlocks(c *C) { 626 tk := testkit.NewTestKit(c, s.causetstore) 627 628 _, err := tk.InterDirc("FLUSH TABLES") 629 c.Check(err, IsNil) 630 631 _, err = tk.InterDirc("FLUSH TABLES WITH READ LOCK") 632 c.Check(err, NotNil) 633 634 } 635 636 func (s *testSuite3) TestUseDB(c *C) { 637 tk := testkit.NewTestKit(c, s.causetstore) 638 _, err := tk.InterDirc("USE test") 639 c.Check(err, IsNil) 640 641 _, err = tk.InterDirc("USE ``") 642 c.Assert(terror.ErrorEqual(embedded.ErrNoDB, err), IsTrue, Commentf("err %v", err)) 643 } 644 645 func (s *testSuite3) TestStmtAutoNewTxn(c *C) { 646 // Some memexs are like DBS, they commit the previous txn automically. 647 tk := testkit.NewTestKit(c, s.causetstore) 648 tk.MustInterDirc("use test") 649 650 // Fix issue https://github.com/whtcorpsinc/milevadb/issues/10705 651 tk.MustInterDirc("begin") 652 tk.MustInterDirc("create user 'xxx'@'%';") 653 tk.MustInterDirc("grant all privileges on *.* to 'xxx'@'%';") 654 655 tk.MustInterDirc("create causet auto_new (id int)") 656 tk.MustInterDirc("begin") 657 tk.MustInterDirc("insert into auto_new values (1)") 658 tk.MustInterDirc("revoke all privileges on *.* from 'xxx'@'%'") 659 tk.MustInterDirc("rollback") // insert memex has already committed 660 tk.MustQuery("select * from auto_new").Check(testkit.Events("1")) 661 662 // Test the behavior when autocommit is false. 663 tk.MustInterDirc("set autocommit = 0") 664 tk.MustInterDirc("insert into auto_new values (2)") 665 tk.MustInterDirc("create user 'yyy'@'%'") 666 tk.MustInterDirc("rollback") 667 tk.MustQuery("select * from auto_new").Check(testkit.Events("1", "2")) 668 669 tk.MustInterDirc("drop user 'yyy'@'%'") 670 tk.MustInterDirc("insert into auto_new values (3)") 671 tk.MustInterDirc("rollback") 672 tk.MustQuery("select * from auto_new").Check(testkit.Events("1", "2")) 673 } 674 675 func (s *testSuite3) TestIssue9111(c *C) { 676 // CREATE USER / DROP USER fails if admin doesn't have insert privilege on `allegrosql.user` causet. 677 tk := testkit.NewTestKit(c, s.causetstore) 678 tk.MustInterDirc("create user 'user_admin'@'localhost';") 679 tk.MustInterDirc("grant create user on *.* to 'user_admin'@'localhost';") 680 681 // Create a new stochastik. 682 se, err := stochastik.CreateStochastik4Test(s.causetstore) 683 c.Check(err, IsNil) 684 defer se.Close() 685 c.Assert(se.Auth(&auth.UserIdentity{Username: "user_admin", Hostname: "localhost"}, nil, nil), IsTrue) 686 687 ctx := context.Background() 688 _, err = se.InterDircute(ctx, `create user test_create_user`) 689 c.Check(err, IsNil) 690 _, err = se.InterDircute(ctx, `drop user test_create_user`) 691 c.Check(err, IsNil) 692 693 tk.MustInterDirc("revoke create user on *.* from 'user_admin'@'localhost';") 694 tk.MustInterDirc("grant insert, delete on allegrosql.user to 'user_admin'@'localhost';") 695 696 _, err = se.InterDircute(ctx, `create user test_create_user`) 697 c.Check(err, IsNil) 698 _, err = se.InterDircute(ctx, `drop user test_create_user`) 699 c.Check(err, IsNil) 700 701 _, err = se.InterDircute(ctx, `create role test_create_user`) 702 c.Check(err, IsNil) 703 _, err = se.InterDircute(ctx, `drop role test_create_user`) 704 c.Check(err, IsNil) 705 706 tk.MustInterDirc("drop user 'user_admin'@'localhost';") 707 } 708 709 func (s *testSuite3) TestRoleAtomic(c *C) { 710 tk := testkit.NewTestKit(c, s.causetstore) 711 712 tk.MustInterDirc("create role r2;") 713 _, err := tk.InterDirc("create role r1, r2, r3") 714 c.Check(err, NotNil) 715 // Check atomic create role. 716 result := tk.MustQuery(`SELECT user FROM allegrosql.User WHERE user in ('r1', 'r2', 'r3')`) 717 result.Check(testkit.Events("r2")) 718 // Check atomic drop role. 719 _, err = tk.InterDirc("drop role r1, r2, r3") 720 c.Check(err, NotNil) 721 result = tk.MustQuery(`SELECT user FROM allegrosql.User WHERE user in ('r1', 'r2', 'r3')`) 722 result.Check(testkit.Events("r2")) 723 tk.MustInterDirc("drop role r2;") 724 } 725 726 func (s *testSuite3) TestExtendedStatsPrivileges(c *C) { 727 tk := testkit.NewTestKit(c, s.causetstore) 728 tk.MustInterDirc("use test") 729 tk.MustInterDirc("drop causet if exists t") 730 tk.MustInterDirc("create causet t(a int, b int)") 731 tk.MustInterDirc("create user 'u1'@'%'") 732 se, err := stochastik.CreateStochastik4Test(s.causetstore) 733 c.Check(err, IsNil) 734 defer se.Close() 735 c.Assert(se.Auth(&auth.UserIdentity{Username: "u1", Hostname: "%"}, nil, nil), IsTrue) 736 ctx := context.Background() 737 _, err = se.InterDircute(ctx, "create statistics s1(correlation) on test.t(a,b)") 738 c.Assert(err, NotNil) 739 c.Assert(err.Error(), Equals, "[causet:1142]CREATE STATISTICS command denied to user 'u1'@'%' for causet 't'") 740 tk.MustInterDirc("grant select on test.* to 'u1'@'%'") 741 _, err = se.InterDircute(ctx, "create statistics s1(correlation) on test.t(a,b)") 742 c.Assert(err, NotNil) 743 c.Assert(err.Error(), Equals, "[causet:1142]CREATE STATISTICS command denied to user 'u1'@'%' for causet 'stats_extended'") 744 tk.MustInterDirc("grant insert on allegrosql.stats_extended to 'u1'@'%'") 745 _, err = se.InterDircute(ctx, "create statistics s1(correlation) on test.t(a,b)") 746 c.Assert(err, IsNil) 747 748 _, err = se.InterDircute(ctx, "use test") 749 c.Assert(err, IsNil) 750 _, err = se.InterDircute(ctx, "drop statistics s1") 751 c.Assert(err, NotNil) 752 c.Assert(err.Error(), Equals, "[causet:1142]DROP STATISTICS command denied to user 'u1'@'%' for causet 'stats_extended'") 753 tk.MustInterDirc("grant uFIDelate on allegrosql.stats_extended to 'u1'@'%'") 754 _, err = se.InterDircute(ctx, "drop statistics s1") 755 c.Assert(err, IsNil) 756 tk.MustInterDirc("drop user 'u1'@'%'") 757 } 758 759 func (s *testSuite3) TestIssue17247(c *C) { 760 tk := testkit.NewTestKit(c, s.causetstore) 761 tk.MustInterDirc("create user 'issue17247'") 762 tk.MustInterDirc("grant CREATE USER on *.* to 'issue17247'") 763 764 tk1 := testkit.NewTestKit(c, s.causetstore) 765 tk1.MustInterDirc("use test") 766 c.Assert(tk1.Se.Auth(&auth.UserIdentity{Username: "issue17247", Hostname: "%"}, nil, nil), IsTrue) 767 tk1.MustInterDirc("ALTER USER USER() IDENTIFIED BY 'xxx'") 768 tk1.MustInterDirc("ALTER USER CURRENT_USER() IDENTIFIED BY 'yyy'") 769 tk1.MustInterDirc("ALTER USER CURRENT_USER IDENTIFIED BY 'zzz'") 770 tk.MustInterDirc("ALTER USER 'issue17247'@'%' IDENTIFIED BY 'kkk'") 771 tk.MustInterDirc("ALTER USER 'issue17247'@'%' IDENTIFIED BY PASSWORD '*B50FBDB37F1256824274912F2A1CE648082C3F1F'") 772 // Wrong grammar 773 _, err := tk1.InterDirc("ALTER USER USER() IDENTIFIED BY PASSWORD '*B50FBDB37F1256824274912F2A1CE648082C3F1F'") 774 c.Assert(err, NotNil) 775 }