improve wall filter

This commit is contained in:
wenshao
2013-04-05 12:49:25 +08:00
parent d268227242
commit 9ea3434bc4
12 changed files with 324 additions and 17 deletions
@@ -0,0 +1,45 @@
/*
* Copyright 1999-2011 Alibaba Group Holding Ltd.
*
* 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,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.druid.bvt.filter.wall;
import junit.framework.TestCase;
import org.junit.Assert;
import com.alibaba.druid.support.json.JSONUtils;
import com.alibaba.druid.wall.WallProvider;
import com.alibaba.druid.wall.spi.MySqlWallProvider;
/**
* SQLServerWallTest
*
* @author RaymondXiu
* @version 1.0, 2012-3-18
* @see
*/
public class MySqlWallTest35 extends TestCase {
public void test_true() throws Exception {
WallProvider provider = new MySqlWallProvider();
Assert.assertTrue(provider.checkValid(//
"REPLACE INTO `test2tb` VALUES " + //
"('51', '6714105741', '亦美珊不掉正品隐形文胸安全硅胶文胸 小胸聚拢比基尼<span class=H>游泳</span>衣泳装', 'http://img03.taobaocdn.com/bao/uploaded/i3/16011019585534199/T1cnlaXA8bXXXXXXXX_!!0-item_pic.jpg', 'liqiang198163', 'c', '25.00', '20.00', '1000.00', '76', '529', '4.86', '4.91', '4.90', '0.00', '4.80', '2878', '77', '12', '15');")); //
Assert.assertEquals(1, provider.getTableStat("test2tb").getReplaceCount());
System.out.println(JSONUtils.toJSONString(provider.getStatsMap()));
}
}
@@ -0,0 +1,51 @@
package com.alibaba.druid.bvt.filter.wall;
import java.util.Map;
import junit.framework.TestCase;
import org.junit.Assert;
import com.alibaba.druid.stat.DruidStatManagerFacade;
import com.alibaba.druid.support.json.JSONUtils;
import com.alibaba.druid.wall.WallContext;
import com.alibaba.druid.wall.WallProvider;
import com.alibaba.druid.wall.spi.MySqlWallProvider;
public class WallStatTest_statMap extends TestCase {
protected void setUp() throws Exception {
WallContext.clearContext();
}
protected void tearDown() throws Exception {
WallContext.clearContext();
}
public void testMySql() throws Exception {
WallProvider providerA = new MySqlWallProvider();
{
String sql = "select * from t where len(fname) = 1";
Assert.assertTrue(providerA.checkValid(sql));
}
WallProvider providerB = new MySqlWallProvider();
{
String sql = "select * from t where len(fname) = 2";
Assert.assertTrue(providerB.checkValid(sql));
}
Map<String, Object> statMapA = providerA.getStatsMap();
Map<String, Object> statMapB = providerB.getStatsMap();
System.out.println(JSONUtils.toJSONString(statMapA));
System.out.println(JSONUtils.toJSONString(statMapB));
Map<String, Object> statMapMerged = DruidStatManagerFacade.mergWallStat(statMapA, statMapB);
System.out.println(JSONUtils.toJSONString(statMapMerged));
Assert.assertEquals(2L, statMapMerged.get("checkCount"));
}
}
@@ -0,0 +1,69 @@
/*
* Copyright 1999-2011 Alibaba Group Holding Ltd.
*
* 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,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.druid.bvt.sql.mysql;
import java.util.List;
import org.junit.Assert;
import com.alibaba.druid.sql.MysqlTest;
import com.alibaba.druid.sql.ast.SQLStatement;
import com.alibaba.druid.sql.ast.statement.SQLCreateTableStatement;
import com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser;
import com.alibaba.druid.sql.dialect.mysql.visitor.MySqlSchemaStatVisitor;
import com.alibaba.druid.stat.TableStat;
import com.alibaba.druid.stat.TableStat.Column;
public class MySqlCreateTableTest10 extends MysqlTest {
public void test_0() throws Exception {
String sql = "create table TACCOUNT (" + //
" ID varchar(36) not null," + //
" ACCOUNT varchar(100) not null," + //
" account_money double precision," + //
" NAME varchar(100) not null," + //
" TYPE integer," + //
" primary key (ID)" + //
" )";
MySqlStatementParser parser = new MySqlStatementParser(sql);
List<SQLStatement> statementList = parser.parseStatementList();
SQLCreateTableStatement stmt = (SQLCreateTableStatement) statementList.get(0);
print(statementList);
Assert.assertEquals(1, statementList.size());
MySqlSchemaStatVisitor visitor = new MySqlSchemaStatVisitor();
stmt.accept(visitor);
System.out.println("Tables : " + visitor.getTables());
System.out.println("fields : " + visitor.getColumns());
System.out.println("coditions : " + visitor.getConditions());
System.out.println("orderBy : " + visitor.getOrderByColumns());
Assert.assertEquals(1, visitor.getTables().size());
Assert.assertEquals(5, visitor.getColumns().size());
Assert.assertEquals(0, visitor.getConditions().size());
Assert.assertTrue(visitor.getTables().containsKey(new TableStat.Name("TACCOUNT")));
Assert.assertTrue(visitor.getColumns().contains(new Column("TACCOUNT", "ID")));
Assert.assertTrue(visitor.getColumns().contains(new Column("TACCOUNT", "ACCOUNT")));
Assert.assertTrue(visitor.getColumns().contains(new Column("TACCOUNT", "account_money")));
Assert.assertTrue(visitor.getColumns().contains(new Column("TACCOUNT", "NAME")));
Assert.assertTrue(visitor.getColumns().contains(new Column("TACCOUNT", "TYPE")));
}
}
@@ -0,0 +1,57 @@
/*
* Copyright 1999-2011 Alibaba Group Holding Ltd.
*
* 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,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.druid.bvt.sql.mysql;
import java.util.List;
import junit.framework.Assert;
import com.alibaba.druid.sql.MysqlTest;
import com.alibaba.druid.sql.ast.SQLStatement;
import com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser;
import com.alibaba.druid.sql.dialect.mysql.visitor.MySqlSchemaStatVisitor;
import com.alibaba.druid.stat.TableStat;
import com.alibaba.druid.stat.TableStat.Column;
public class MySqlShowFieldsTest extends MysqlTest {
public void test_0() throws Exception {
String sql = "SHOW FIELDS FROM `schema_migrations`";
MySqlStatementParser parser = new MySqlStatementParser(sql);
List<SQLStatement> statementList = parser.parseStatementList();
SQLStatement statemen = statementList.get(0);
print(statementList);
Assert.assertEquals(1, statementList.size());
MySqlSchemaStatVisitor visitor = new MySqlSchemaStatVisitor();
statemen.accept(visitor);
System.out.println("Tables : " + visitor.getTables());
System.out.println("fields : " + visitor.getColumns());
System.out.println("coditions : " + visitor.getConditions());
System.out.println("orderBy : " + visitor.getOrderByColumns());
Assert.assertEquals(0, visitor.getTables().size());
Assert.assertEquals(0, visitor.getColumns().size());
Assert.assertEquals(0, visitor.getConditions().size());
// Assert.assertTrue(visitor.getTables().containsKey(new TableStat.Name("t_price")));
// Assert.assertTrue(visitor.getTables().containsKey(new TableStat.Name("t_basic_store")));
}
}
@@ -0,0 +1,18 @@
package com.alibaba.druid.bvt.support.json;
import java.util.LinkedHashMap;
import java.util.Map;
import junit.framework.TestCase;
import com.alibaba.druid.support.json.JSONUtils;
import com.alibaba.druid.util.IOUtils;
public class JSONParserTest3 extends TestCase {
public void test_parse() throws Exception {
String text = IOUtils.readFromResource("bvt/sonar-sql.json");
Map<String, Object> result = (Map<String, Object>)JSONUtils.parse(text);
LinkedHashMap wallStats = (LinkedHashMap) result.get("Content");
System.out.println(wallStats.get("blackList"));
}
}