forked from pingcap/tidb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmisc_test.go
157 lines (141 loc) · 3.81 KB
/
misc_test.go
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
// Copyright 2016 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// See the License for the specific language governing permissions and
// limitations under the License.
package ast_test
import (
. "github.com/pingcap/check"
. "github.com/pingcap/tidb/ast"
"github.com/pingcap/tidb/parser"
)
var _ = Suite(&testMiscSuite{})
type testMiscSuite struct {
}
type visitor struct{}
func (v visitor) Enter(in Node) (Node, bool) {
return in, false
}
func (v visitor) Leave(in Node) (Node, bool) {
return in, true
}
type visitor1 struct {
visitor
}
func (visitor1) Enter(in Node) (Node, bool) {
return in, true
}
func (ts *testMiscSuite) TestMiscVisitorCover(c *C) {
stmts := []Node{
(&AdminStmt{}),
(&AlterUserStmt{}),
(&BeginStmt{}),
(&BinlogStmt{}),
(&CommitStmt{}),
(&CreateUserStmt{}),
(&DeallocateStmt{}),
(&DoStmt{}),
(&ExecuteStmt{UsingVars: []ExprNode{&ValueExpr{}}}),
(&ExplainStmt{Stmt: &ShowStmt{}}),
(&GrantStmt{}),
(&PrepareStmt{SQLVar: &VariableExpr{Value: &ValueExpr{}}}),
(&RollbackStmt{}),
(&SetPwdStmt{}),
(&SetStmt{Variables: []*VariableAssignment{
{
Value: &ValueExpr{},
},
}}),
(&UseStmt{}),
(&AnalyzeTableStmt{
TableNames: []*TableName{
{},
},
}),
(&FlushStmt{}),
(&PrivElem{}),
(&VariableAssignment{Value: &ValueExpr{}}),
(&KillStmt{}),
(&DropStatsStmt{Table: &TableName{}}),
}
for _, v := range stmts {
v.Accept(visitor{})
v.Accept(visitor1{})
}
}
func (ts *testMiscSuite) TestDDLVisitorCover(c *C) {
sql := `
create table t (c1 smallint unsigned, c2 int unsigned);
alter table t add column a smallint unsigned after b;
create index t_i on t (id);
create database test character set utf8;
drop database test;
drop index t_i on t;
drop table t;
truncate t;
create table t (
jobAbbr char(4) not null,
constraint foreign key (jobabbr) references ffxi_jobtype (jobabbr) on delete cascade on update cascade
);
`
parser := parser.New()
stmts, err := parser.Parse(sql, "", "")
c.Assert(err, IsNil)
for _, stmt := range stmts {
stmt.Accept(visitor{})
stmt.Accept(visitor1{})
}
}
func (ts *testMiscSuite) TestDMLVistorCover(c *C) {
sql := `delete from somelog where user = 'jcole' order by timestamp_column limit 1;
delete t1, t2 from t1 inner join t2 inner join t3 where t1.id=t2.id and t2.id=t3.id;
select * from t where exists(select * from t k where t.c = k.c having sum(c) = 1);
insert into t_copy select * from t where t.x > 5;
(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;
update t1 set col1 = col1 + 1, col2 = col1;
show create table t;
load data infile '/tmp/t.csv' into table t fields terminated by 'ab' enclosed by 'b';`
p := parser.New()
stmts, err := p.Parse(sql, "", "")
c.Assert(err, IsNil)
for _, stmt := range stmts {
stmt.Accept(visitor{})
stmt.Accept(visitor1{})
}
}
func (ts *testMiscSuite) TestSensitiveStatement(c *C) {
positive := []StmtNode{
&SetPwdStmt{},
&CreateUserStmt{},
&AlterUserStmt{},
&GrantStmt{},
}
for i, stmt := range positive {
_, ok := stmt.(SensitiveStmtNode)
c.Assert(ok, IsTrue, Commentf("%d, %#v fail", i, stmt))
}
negative := []StmtNode{
&DropUserStmt{},
&RevokeStmt{},
&AlterTableStmt{},
&CreateDatabaseStmt{},
&CreateIndexStmt{},
&CreateTableStmt{},
&DropDatabaseStmt{},
&DropIndexStmt{},
&DropTableStmt{},
&RenameTableStmt{},
&TruncateTableStmt{},
}
for _, stmt := range negative {
_, ok := stmt.(SensitiveStmtNode)
c.Assert(ok, IsFalse)
}
}