github.com/XiaoMi/Gaea@v1.2.5/parser/ast/misc_test.go (about)

     1  // Copyright 2016 PingCAP, 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 ast_test
    15  
    16  import (
    17  	. "github.com/pingcap/check"
    18  
    19  	"github.com/XiaoMi/Gaea/parser"
    20  	. "github.com/XiaoMi/Gaea/parser/ast"
    21  	"github.com/XiaoMi/Gaea/parser/auth"
    22  )
    23  
    24  var _ = Suite(&testMiscSuite{})
    25  
    26  type testMiscSuite struct {
    27  }
    28  
    29  type visitor struct{}
    30  
    31  func (v visitor) Enter(in Node) (Node, bool) {
    32  	return in, false
    33  }
    34  
    35  func (v visitor) Leave(in Node) (Node, bool) {
    36  	return in, true
    37  }
    38  
    39  type visitor1 struct {
    40  	visitor
    41  }
    42  
    43  func (visitor1) Enter(in Node) (Node, bool) {
    44  	return in, true
    45  }
    46  
    47  func (ts *testMiscSuite) TestMiscVisitorCover(c *C) {
    48  	valueExpr := NewValueExpr(42)
    49  	stmts := []Node{
    50  		&AdminStmt{},
    51  		&AlterUserStmt{},
    52  		&BeginStmt{},
    53  		&BinlogStmt{},
    54  		&CommitStmt{},
    55  		&CreateUserStmt{},
    56  		&DeallocateStmt{},
    57  		&DoStmt{},
    58  		&ExecuteStmt{UsingVars: []ExprNode{valueExpr}},
    59  		&ExplainStmt{Stmt: &ShowStmt{}},
    60  		&GrantStmt{},
    61  		&PrepareStmt{SQLVar: &VariableExpr{Value: valueExpr}},
    62  		&RollbackStmt{},
    63  		&SetPwdStmt{},
    64  		&SetStmt{Variables: []*VariableAssignment{
    65  			{
    66  				Value: valueExpr,
    67  			},
    68  		}},
    69  		&UseStmt{},
    70  		&AnalyzeTableStmt{
    71  			TableNames: []*TableName{
    72  				{},
    73  			},
    74  		},
    75  		&FlushStmt{},
    76  		&PrivElem{},
    77  		&VariableAssignment{Value: valueExpr},
    78  		&KillStmt{},
    79  		&DropStatsStmt{Table: &TableName{}},
    80  	}
    81  
    82  	for _, v := range stmts {
    83  		v.Accept(visitor{})
    84  		v.Accept(visitor1{})
    85  	}
    86  }
    87  
    88  func (ts *testMiscSuite) TestDDLVisitorCover(c *C) {
    89  	sql := `
    90  create table t (c1 smallint unsigned, c2 int unsigned);
    91  alter table t add column a smallint unsigned after b;
    92  create index t_i on t (id);
    93  create database test character set utf8;
    94  drop database test;
    95  drop index t_i on t;
    96  drop table t;
    97  truncate t;
    98  create table t (
    99  jobAbbr char(4) not null,
   100  constraint foreign key (jobabbr) references ffxi_jobtype (jobabbr) on delete cascade on update cascade
   101  );
   102  `
   103  	parse := parser.New()
   104  	stmts, _, err := parse.Parse(sql, "", "")
   105  	c.Assert(err, IsNil)
   106  	for _, stmt := range stmts {
   107  		stmt.Accept(visitor{})
   108  		stmt.Accept(visitor1{})
   109  	}
   110  }
   111  
   112  func (ts *testMiscSuite) TestDMLVistorCover(c *C) {
   113  	sql := `delete from somelog where user = 'jcole' order by timestamp_column limit 1;
   114  delete t1, t2 from t1 inner join t2 inner join t3 where t1.id=t2.id and t2.id=t3.id;
   115  select * from t where exists(select * from t k where t.c = k.c having sum(c) = 1);
   116  insert into t_copy select * from t where t.x > 5;
   117  (select /*+ TIDB_INLJ(t1) */ a from t1 where a=10 and b=1) union (select /*+ TIDB_SMJ(t2) */ a from t2 where a=11 and b=2) order by a limit 10;
   118  update t1 set col1 = col1 + 1, col2 = col1;
   119  show create table t;
   120  load data infile '/tmp/t.csv' into table t fields terminated by 'ab' enclosed by 'b';`
   121  
   122  	p := parser.New()
   123  	stmts, _, err := p.Parse(sql, "", "")
   124  	c.Assert(err, IsNil)
   125  	for _, stmt := range stmts {
   126  		stmt.Accept(visitor{})
   127  		stmt.Accept(visitor1{})
   128  	}
   129  }
   130  
   131  func (ts *testMiscSuite) TestSensitiveStatement(c *C) {
   132  	positive := []StmtNode{
   133  		&SetPwdStmt{},
   134  		&CreateUserStmt{},
   135  		&AlterUserStmt{},
   136  		&GrantStmt{},
   137  	}
   138  	for i, stmt := range positive {
   139  		_, ok := stmt.(SensitiveStmtNode)
   140  		c.Assert(ok, IsTrue, Commentf("%d, %#v fail", i, stmt))
   141  	}
   142  
   143  	negative := []StmtNode{
   144  		&DropUserStmt{},
   145  		&RevokeStmt{},
   146  		&AlterTableStmt{},
   147  		&CreateDatabaseStmt{},
   148  		&CreateIndexStmt{},
   149  		&CreateTableStmt{},
   150  		&DropDatabaseStmt{},
   151  		&DropIndexStmt{},
   152  		&DropTableStmt{},
   153  		&RenameTableStmt{},
   154  		&TruncateTableStmt{},
   155  	}
   156  	for _, stmt := range negative {
   157  		_, ok := stmt.(SensitiveStmtNode)
   158  		c.Assert(ok, IsFalse)
   159  	}
   160  }
   161  
   162  func (ts *testMiscSuite) TestUserSpec(c *C) {
   163  	hashString := "*3D56A309CD04FA2EEF181462E59011F075C89548"
   164  	u := UserSpec{
   165  		User: &auth.UserIdentity{
   166  			Username: "test",
   167  		},
   168  		AuthOpt: &AuthOption{
   169  			ByAuthString: false,
   170  			AuthString:   "xxx",
   171  			HashString:   hashString,
   172  		},
   173  	}
   174  	pwd, ok := u.EncodedPassword()
   175  	c.Assert(ok, IsTrue)
   176  	c.Assert(pwd, Equals, u.AuthOpt.HashString)
   177  
   178  	u.AuthOpt.HashString = "not-good-password-format"
   179  	pwd, ok = u.EncodedPassword()
   180  	c.Assert(ok, IsFalse)
   181  
   182  	u.AuthOpt.ByAuthString = true
   183  	pwd, ok = u.EncodedPassword()
   184  	c.Assert(ok, IsTrue)
   185  	c.Assert(pwd, Equals, hashString)
   186  
   187  	u.AuthOpt.AuthString = ""
   188  	pwd, ok = u.EncodedPassword()
   189  	c.Assert(ok, IsTrue)
   190  	c.Assert(pwd, Equals, "")
   191  }
   192  
   193  func (ts *testMiscSuite) TestTableOptimizerHintRestore(c *C) {
   194  	testCases := []NodeRestoreTestCase{
   195  		{"TIDB_SMJ(`t1`)", "TIDB_SMJ(`t1`)"},
   196  		{"TIDB_SMJ(t1)", "TIDB_SMJ(`t1`)"},
   197  		{"TIDB_SMJ(t1,t2)", "TIDB_SMJ(`t1`, `t2`)"},
   198  		{"TIDB_INLJ(t1,t2)", "TIDB_INLJ(`t1`, `t2`)"},
   199  		{"TIDB_HJ(t1,t2)", "TIDB_HJ(`t1`, `t2`)"},
   200  		{"MAX_EXECUTION_TIME(3000)", "MAX_EXECUTION_TIME(3000)"},
   201  	}
   202  	extractNodeFunc := func(node Node) Node {
   203  		return node.(*SelectStmt).TableHints[0]
   204  	}
   205  	RunNodeRestoreTest(c, testCases, "select /*+ %s */ * from t1 join t2", extractNodeFunc)
   206  }