Search in sources :

Example 1 with WallUpdateCheckHandler

use of com.alibaba.druid.wall.WallUpdateCheckHandler in project druid by alibaba.

the class WallUpdateCheckTest method test_update_check_handler.

public void test_update_check_handler() throws Exception {
    {
        WallCheckResult result = wallProvider.check("update t_orders set status = 3 where id = 3 and status = 4");
        assertTrue(result.getViolations().size() == 0);
    }
    wallProvider.getConfig().setUpdateCheckHandler(new WallUpdateCheckHandler() {

        @Override
        public boolean check(String table, String column, Object setValue, List<Object> filterValues) {
            return false;
        }
    });
    {
        WallCheckResult result = wallProvider.check("update t_orders set status = 3 where id = 3 and status = 4");
        assertTrue(result.getViolations().size() > 0);
    }
    wallProvider.getConfig().setUpdateCheckHandler(new WallUpdateCheckHandler() {

        @Override
        public boolean check(String table, String column, Object setValue, List<Object> filterValues) {
            return true;
        }
    });
    {
        WallCheckResult result = wallProvider.check("update t_orders set status = 3 where id = 3 and status = 4");
        assertTrue(result.getViolations().size() == 0);
    }
    assertEquals(0, wallProvider.getWhiteListHitCount());
    assertEquals(0, wallProvider.getBlackListHitCount());
    wallProvider.getConfig().setUpdateCheckHandler(new WallUpdateCheckHandler() {

        @Override
        public boolean check(String table, String column, Object setValue, List<Object> filterValues) {
            // 增加对in语句的支持, status in (1, 2) 应该返回的filterValue为1和2
            assertTrue(filterValues.size() == 2);
            return true;
        }
    });
    {
        WallCheckResult result = wallProvider.check("update t_orders set status = 3 where id = 3 and status in (1, 2)");
        assertTrue(result.getViolations().size() == 0);
    }
    {
        WallCheckResult result = wallProvider.check("update t_orders set status = 3 where id = 3 and status = 3 and status in (3, 4)");
        assertTrue(result.getViolations().size() == 0);
    }
}
Also used : WallUpdateCheckHandler(com.alibaba.druid.wall.WallUpdateCheckHandler) WallCheckResult(com.alibaba.druid.wall.WallCheckResult)

Aggregations

WallCheckResult (com.alibaba.druid.wall.WallCheckResult)1 WallUpdateCheckHandler (com.alibaba.druid.wall.WallUpdateCheckHandler)1