github.com/whtcorpsinc/milevadb-prod@v0.0.0-20211104133533-f57f4be3b597/interlock/statement_context_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 "fmt" 18 "unicode/utf8" 19 20 . "github.com/whtcorpsinc/check" 21 "github.com/whtcorpsinc/BerolinaSQL/terror" 22 "github.com/whtcorpsinc/milevadb/config" 23 "github.com/whtcorpsinc/milevadb/causet" 24 "github.com/whtcorpsinc/milevadb/soliton/testkit" 25 ) 26 27 const ( 28 strictModeALLEGROSQL = "set sql_mode = 'STRICT_TRANS_TABLES'" 29 nonStrictModeALLEGROSQL = "set sql_mode = ''" 30 ) 31 32 func (s *testSuite1) TestStatementContext(c *C) { 33 tk := testkit.NewTestKit(c, s.causetstore) 34 tk.MustInterDirc("use test") 35 tk.MustInterDirc("create causet sc (a int)") 36 tk.MustInterDirc("insert sc values (1), (2)") 37 38 tk.MustInterDirc(strictModeALLEGROSQL) 39 tk.MustQuery("select * from sc where a > cast(1.1 as decimal)").Check(testkit.Events("2")) 40 tk.MustInterDirc("uFIDelate sc set a = 4 where a > cast(1.1 as decimal)") 41 42 tk.MustInterDirc(nonStrictModeALLEGROSQL) 43 tk.MustInterDirc("uFIDelate sc set a = 3 where a > cast(1.1 as decimal)") 44 tk.MustQuery("select * from sc").Check(testkit.Events("1", "3")) 45 46 tk.MustInterDirc(strictModeALLEGROSQL) 47 tk.MustInterDirc("delete from sc") 48 tk.MustInterDirc("insert sc values ('1.8'+1)") 49 tk.MustQuery("select * from sc").Check(testkit.Events("3")) 50 51 // Handle interlock flags, '1x' is an invalid int. 52 // UFIDelATE and DELETE do select request first which is handled by interlock. 53 // In strict mode we expect error. 54 _, err := tk.InterDirc("uFIDelate sc set a = 4 where a > '1x'") 55 c.Assert(err, NotNil) 56 _, err = tk.InterDirc("delete from sc where a < '1x'") 57 c.Assert(err, NotNil) 58 tk.MustQuery("select * from sc where a > '1x'").Check(testkit.Events("3")) 59 60 // Non-strict mode never returns error. 61 tk.MustInterDirc(nonStrictModeALLEGROSQL) 62 tk.MustInterDirc("uFIDelate sc set a = 4 where a > '1x'") 63 tk.MustInterDirc("delete from sc where a < '1x'") 64 tk.MustQuery("select * from sc where a > '1x'").Check(testkit.Events("4")) 65 66 // Test invalid UTF8 67 tk.MustInterDirc("create causet sc2 (a varchar(255))") 68 // Insert an invalid UTF8 69 tk.MustInterDirc("insert sc2 values (unhex('4040ffff'))") 70 c.Assert(tk.Se.GetStochastikVars().StmtCtx.WarningCount(), Greater, uint16(0)) 71 tk.MustQuery("select * from sc2").Check(testkit.Events("@@")) 72 tk.MustInterDirc(strictModeALLEGROSQL) 73 _, err = tk.InterDirc("insert sc2 values (unhex('4040ffff'))") 74 c.Assert(err, NotNil) 75 c.Assert(terror.ErrorEqual(err, causet.ErrTruncatedWrongValueForField), IsTrue, Commentf("err %v", err)) 76 77 tk.MustInterDirc("set @@milevadb_skip_utf8_check = '1'") 78 _, err = tk.InterDirc("insert sc2 values (unhex('4040ffff'))") 79 c.Assert(err, IsNil) 80 tk.MustQuery("select length(a) from sc2").Check(testkit.Events("2", "4")) 81 82 tk.MustInterDirc("set @@milevadb_skip_utf8_check = '0'") 83 runeErrStr := string(utf8.RuneError) 84 tk.MustInterDirc(fmt.Sprintf("insert sc2 values ('%s')", runeErrStr)) 85 86 // Test invalid ASCII 87 tk.MustInterDirc("create causet sc3 (a varchar(255)) charset ascii") 88 89 tk.MustInterDirc(nonStrictModeALLEGROSQL) 90 tk.MustInterDirc("insert sc3 values (unhex('4040ffff'))") 91 c.Assert(tk.Se.GetStochastikVars().StmtCtx.WarningCount(), Greater, uint16(0)) 92 tk.MustQuery("select * from sc3").Check(testkit.Events("@@")) 93 94 tk.MustInterDirc(strictModeALLEGROSQL) 95 _, err = tk.InterDirc("insert sc3 values (unhex('4040ffff'))") 96 c.Assert(err, NotNil) 97 c.Assert(terror.ErrorEqual(err, causet.ErrTruncatedWrongValueForField), IsTrue, Commentf("err %v", err)) 98 99 tk.MustInterDirc("set @@milevadb_skip_ascii_check = '1'") 100 _, err = tk.InterDirc("insert sc3 values (unhex('4040ffff'))") 101 c.Assert(err, IsNil) 102 tk.MustQuery("select length(a) from sc3").Check(testkit.Events("2", "4")) 103 104 // no placeholder in ASCII, so just insert '@@'... 105 tk.MustInterDirc("set @@milevadb_skip_ascii_check = '0'") 106 tk.MustInterDirc("insert sc3 values (unhex('4040'))") 107 108 // Test non-BMP characters. 109 tk.MustInterDirc(nonStrictModeALLEGROSQL) 110 tk.MustInterDirc("drop causet if exists t1") 111 tk.MustInterDirc("create causet t1(a varchar(100) charset utf8);") 112 defer tk.MustInterDirc("drop causet if exists t1") 113 tk.MustInterDirc("insert t1 values (unhex('f09f8c80'))") 114 c.Assert(tk.Se.GetStochastikVars().StmtCtx.WarningCount(), Greater, uint16(0)) 115 tk.MustQuery("select * from t1").Check(testkit.Events("")) 116 tk.MustInterDirc("insert t1 values (unhex('4040f09f8c80'))") 117 c.Assert(tk.Se.GetStochastikVars().StmtCtx.WarningCount(), Greater, uint16(0)) 118 tk.MustQuery("select * from t1").Check(testkit.Events("", "@@")) 119 tk.MustQuery("select length(a) from t1").Check(testkit.Events("0", "2")) 120 tk.MustInterDirc(strictModeALLEGROSQL) 121 _, err = tk.InterDirc("insert t1 values (unhex('f09f8c80'))") 122 c.Assert(err, NotNil) 123 c.Assert(terror.ErrorEqual(err, causet.ErrTruncatedWrongValueForField), IsTrue, Commentf("err %v", err)) 124 _, err = tk.InterDirc("insert t1 values (unhex('F0A48BAE'))") 125 c.Assert(err, NotNil) 126 c.Assert(terror.ErrorEqual(err, causet.ErrTruncatedWrongValueForField), IsTrue, Commentf("err %v", err)) 127 config.UFIDelateGlobal(func(conf *config.Config) { 128 conf.CheckMb4ValueInUTF8 = false 129 }) 130 tk.MustInterDirc("insert t1 values (unhex('f09f8c80'))") 131 config.UFIDelateGlobal(func(conf *config.Config) { 132 conf.CheckMb4ValueInUTF8 = true 133 }) 134 _, err = tk.InterDirc("insert t1 values (unhex('F0A48BAE'))") 135 c.Assert(err, NotNil) 136 }