use of com.alibaba.druid.wall.spi.MySqlWallProvider in project druid by alibaba.
the class WallProviderTest method test_getSqlStat.
public void test_getSqlStat() throws Exception {
String whiteSql_1 = "select * from t1 where fid = 1";
String whiteSql_2 = "select * from t1 where fid = 2";
String blackSql_1 = "select * from t2 where len(fid) = 1 OR 1 = 1";
String blackSql_2 = "select * from t2 where len(fid) = 2 OR 1 = 1";
MySqlWallProvider provider = new MySqlWallProvider();
Assert.assertTrue(provider.checkValid(whiteSql_1));
Assert.assertTrue(provider.checkValid(whiteSql_2));
Assert.assertEquals(1, provider.getSqlList().size());
Assert.assertFalse(provider.checkValid(blackSql_1));
Assert.assertFalse(provider.checkValid(blackSql_2));
Assert.assertEquals(2, provider.getSqlList().size());
Assert.assertNotNull(provider.getSqlStat(whiteSql_1));
Assert.assertNotNull(provider.getSqlStat(blackSql_1));
Assert.assertSame(provider.getSqlStat(blackSql_1), provider.getSqlStat(blackSql_2));
WallSqlStat whiteStat_1 = provider.getSqlStat(whiteSql_1);
WallSqlStat whiteStat_2 = provider.getSqlStat(whiteSql_2);
Assert.assertSame(whiteStat_1, whiteStat_2);
provider.addFetchRowCount(whiteStat_1, 3);
WallTableStat wallTableStat = provider.getTableStat("t1");
Assert.assertNotNull(wallTableStat);
Assert.assertEquals(3, wallTableStat.getFetchRowCount());
Assert.assertTrue(provider.checkValid(whiteSql_1));
Assert.assertTrue(provider.checkValid(whiteSql_2));
Assert.assertFalse(provider.checkValid(blackSql_1));
Assert.assertFalse(provider.checkValid(blackSql_2));
for (int i = 1000; i < 1000 * 2; ++i) {
String sql_x = "select * from t1 where fid = " + i;
Assert.assertTrue(provider.checkValid(sql_x));
Assert.assertSame(whiteStat_1, provider.getSqlStat(sql_x));
}
Assert.assertEquals(1, provider.getWhiteList().size());
Assert.assertEquals(2, provider.getSqlList().size());
Assert.assertFalse(provider.checkValid("slelc"));
{
WallProviderStatValue statValue = provider.getStatValue(true);
Assert.assertNotNull(statValue);
Assert.assertEquals(2, statValue.getTables().size());
Assert.assertEquals(1, statValue.getFunctions().size());
Assert.assertEquals(1009, statValue.getCheckCount());
Assert.assertEquals(2, statValue.getBlackListHitCount());
Assert.assertEquals(4, statValue.getHardCheckCount());
Assert.assertEquals(1, statValue.getSyntaxErrorCount());
Assert.assertEquals(5, statValue.getViolationCount());
Assert.assertEquals(1003, statValue.getWhiteListHitCount());
Assert.assertEquals(1, statValue.getWhiteList().size());
Assert.assertEquals(2, statValue.getBlackList().size());
}
{
WallProviderStatValue statValue = provider.getStatValue(true);
Assert.assertNotNull(statValue);
Assert.assertEquals(0, statValue.getTables().size());
Assert.assertEquals(0, statValue.getFunctions().size());
Assert.assertEquals(0, statValue.getCheckCount());
Assert.assertEquals(0, statValue.getBlackListHitCount());
Assert.assertEquals(0, statValue.getHardCheckCount());
Assert.assertEquals(0, statValue.getSyntaxErrorCount());
Assert.assertEquals(0, statValue.getViolationCount());
Assert.assertEquals(0, statValue.getWhiteListHitCount());
Assert.assertEquals(0, statValue.getWhiteList().size());
Assert.assertEquals(0, statValue.getBlackList().size());
}
}
use of com.alibaba.druid.wall.spi.MySqlWallProvider in project druid by alibaba.
the class WallReadOnlyTest2 method testMySql.
public void testMySql() throws Exception {
MySqlWallProvider provider = new MySqlWallProvider(config);
Assert.assertTrue(provider.checkValid(sql));
Assert.assertFalse(provider.checkValid(insert_sql));
Assert.assertFalse(provider.checkValid(update_sql));
Assert.assertFalse(provider.checkValid(delete_sql));
}
use of com.alibaba.druid.wall.spi.MySqlWallProvider in project druid by alibaba.
the class WallStatTest_WhiteList method testMySql.
public void testMySql() throws Exception {
WallProvider provider = new MySqlWallProvider();
for (int i = 0; i < 3001; ++i) {
String sql = "select * from t where id = " + i;
Assert.assertTrue(provider.checkValid(sql));
}
WallTableStat tableStat = provider.getTableStat("t");
Assert.assertEquals(3001, tableStat.getSelectCount());
Assert.assertEquals(0, provider.getBlackListHitCount());
Assert.assertEquals(3000, provider.getWhiteListHitCount());
Assert.assertEquals(1, provider.getWhiteList().size());
Assert.assertEquals(3001, provider.getCheckCount());
}
use of com.alibaba.druid.wall.spi.MySqlWallProvider in project druid by alibaba.
the class WallStatTest_select_into method testMySql.
public void testMySql() throws Exception {
WallProvider provider = new MySqlWallProvider();
Assert.assertTrue(provider.checkValid(sql));
{
WallTableStat tableStat = provider.getTableStat("t");
Assert.assertEquals(1, tableStat.getSelectCount());
Assert.assertEquals(0, tableStat.getSelectIntoCount());
}
{
WallTableStat tableStat = provider.getTableStat("x");
Assert.assertEquals(0, tableStat.getSelectCount());
Assert.assertEquals(1, tableStat.getSelectIntoCount());
}
}
use of com.alibaba.druid.wall.spi.MySqlWallProvider in project druid by alibaba.
the class WallStatTest_statMap method testMySql.
@SuppressWarnings("unchecked")
public void testMySql() throws Exception {
WallProvider providerA = new MySqlWallProvider();
{
String sql = "select * from t where len(fname1) = 1 OR 1 = 1";
Assert.assertFalse(providerA.checkValid(sql));
providerA.addViolationEffectRowCount(10);
}
WallProvider providerB = new MySqlWallProvider();
{
String sql = "select * from t where len(fname2) = 2 OR 1 = 1";
Assert.assertFalse(providerB.checkValid(sql));
providerB.addViolationEffectRowCount(11);
}
WallProvider providerC = new MySqlWallProvider();
{
String sql = "select * from t where len(fname2) = 2 OR 1 = 1";
Assert.assertFalse(providerC.checkValid(sql));
providerC.addViolationEffectRowCount(12);
}
Map<String, Object> statMapA = providerA.getStatsMap();
Map<String, Object> statMapB = providerB.getStatsMap();
Map<String, Object> statMapC = providerC.getStatsMap();
System.out.println(JSONUtils.toJSONString(statMapA));
System.out.println(JSONUtils.toJSONString(statMapB));
System.out.println(JSONUtils.toJSONString(statMapC));
Map<String, Object> statMapMerged = DruidStatManagerFacade.mergWallStat(statMapA, statMapB);
System.out.println(JSONUtils.toJSONString(statMapMerged));
Assert.assertEquals(2L, statMapMerged.get("checkCount"));
Assert.assertEquals(21L, statMapMerged.get("violationEffectRowCount"));
Assert.assertEquals(2, ((Collection<Map<String, Object>>) statMapMerged.get("blackList")).size());
statMapMerged = DruidStatManagerFacade.mergWallStat(statMapMerged, statMapC);
System.out.println(JSONUtils.toJSONString(statMapMerged));
Assert.assertEquals(2, ((Collection<Map<String, Object>>) statMapMerged.get("blackList")).size());
Assert.assertEquals(33L, statMapMerged.get("violationEffectRowCount"));
}
Aggregations