mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
improve wall filter
This commit is contained in:
@@ -21,6 +21,7 @@ import org.junit.Assert;
|
||||
|
||||
import com.alibaba.druid.wall.WallConfig;
|
||||
import com.alibaba.druid.wall.WallUtils;
|
||||
import com.alibaba.druid.wall.spi.MySqlWallProvider;
|
||||
|
||||
/**
|
||||
* 这个场景测试访问Oracle系统对象
|
||||
@@ -30,16 +31,16 @@ import com.alibaba.druid.wall.WallUtils;
|
||||
public class MySqlWallPermitVariantTest extends TestCase {
|
||||
|
||||
public void test_allow() throws Exception {
|
||||
WallConfig config = new WallConfig();
|
||||
config.setVariantCheck(false);
|
||||
MySqlWallProvider provider = new MySqlWallProvider();
|
||||
provider.getConfig().setVariantCheck(false);
|
||||
|
||||
Assert.assertTrue(WallUtils.isValidateMySql("select @@version_compile_os FROM X", config));
|
||||
Assert.assertTrue(provider.checkValid("select @@version_compile_os FROM X"));
|
||||
}
|
||||
|
||||
public void test_not_allow() throws Exception {
|
||||
WallConfig config = new WallConfig();
|
||||
config.setVariantCheck(true);
|
||||
MySqlWallProvider provider = new MySqlWallProvider();
|
||||
provider.getConfig().setVariantCheck(true);
|
||||
|
||||
Assert.assertFalse(WallUtils.isValidateMySql("select @@version_compile_os FROM X", config));
|
||||
Assert.assertFalse(provider.checkValid("select @@version_compile_os FROM X"));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -37,7 +37,7 @@ public class MySqlWallTest72 extends TestCase {
|
||||
provider.getConfig().setCommentAllow(true);
|
||||
|
||||
Assert.assertFalse(provider.checkValid(//
|
||||
"select * from t /**/ where 1=1"));
|
||||
"select * from t /**/ where fid = 123 AND 1=1"));
|
||||
|
||||
Assert.assertEquals(1, provider.getTableStats().size());
|
||||
}
|
||||
|
||||
@@ -17,9 +17,10 @@ package com.alibaba.druid.bvt.pool;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
|
||||
import junit.framework.Assert;
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.junit.Assert;
|
||||
|
||||
import com.alibaba.druid.pool.DruidDataSource;
|
||||
import com.alibaba.druid.support.logging.Log;
|
||||
|
||||
@@ -50,6 +51,6 @@ public class ConfigErrorTest3 extends TestCase {
|
||||
|
||||
Assert.assertEquals(0, LOG.getErrorCount());
|
||||
dataSource.init();
|
||||
Assert.assertEquals(1, LOG.getErrorCount());
|
||||
Assert.assertEquals(0, LOG.getErrorCount());
|
||||
}
|
||||
}
|
||||
|
||||
+105
@@ -0,0 +1,105 @@
|
||||
package com.alibaba.druid.bvt.pool.exception;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Statement;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.junit.Assert;
|
||||
|
||||
import com.alibaba.druid.mock.MockConnection;
|
||||
import com.alibaba.druid.pool.DruidDataSource;
|
||||
import com.alibaba.druid.pool.vendor.OracleExceptionSorter;
|
||||
import com.alibaba.druid.stat.DruidDataSourceStatManager;
|
||||
import com.alibaba.druid.stat.JdbcStatManager;
|
||||
import com.alibaba.druid.test.util.OracleMockDriver;
|
||||
import com.alibaba.druid.util.JdbcUtils;
|
||||
|
||||
public class OracleExceptionSorterTest_concurrent extends TestCase {
|
||||
|
||||
private DruidDataSource dataSource;
|
||||
|
||||
protected void setUp() throws Exception {
|
||||
Assert.assertEquals(0, JdbcStatManager.getInstance().getSqlList().size());
|
||||
|
||||
dataSource = new DruidDataSource();
|
||||
|
||||
dataSource.setExceptionSorter(new OracleExceptionSorter());
|
||||
|
||||
dataSource.setDriver(new OracleMockDriver());
|
||||
dataSource.setUrl("jdbc:mock:xxx");
|
||||
dataSource.setPoolPreparedStatements(true);
|
||||
dataSource.setMaxOpenPreparedStatements(100);
|
||||
dataSource.setMaxActive(1);
|
||||
dataSource.setMaxWait(1000 * 100);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void tearDown() throws Exception {
|
||||
JdbcUtils.close(dataSource);
|
||||
Assert.assertEquals(0, DruidDataSourceStatManager.getInstance().getDataSourceList().size());
|
||||
}
|
||||
|
||||
public void test_connect() throws Exception {
|
||||
final CountDownLatch latch_0 = new CountDownLatch(1);
|
||||
|
||||
Thread errorThread = new Thread() {
|
||||
|
||||
public void run() {
|
||||
try {
|
||||
Connection conn = dataSource.getConnection();
|
||||
|
||||
latch_0.countDown();
|
||||
|
||||
MockConnection mockConn = conn.unwrap(MockConnection.class);
|
||||
Assert.assertNotNull(mockConn);
|
||||
|
||||
SQLException exception = new SQLException("xx", "xxx", 28);
|
||||
mockConn.setError(exception);
|
||||
|
||||
try {
|
||||
conn.createStatement();
|
||||
} catch (SQLException ex) {
|
||||
// ex.printStackTrace();
|
||||
}
|
||||
conn.close();
|
||||
} catch (Exception error) {
|
||||
error.printStackTrace();
|
||||
}
|
||||
}
|
||||
};
|
||||
errorThread.start();
|
||||
|
||||
final CountDownLatch workLatch = new CountDownLatch(2);
|
||||
final CountDownLatch workCompleteLatch = new CountDownLatch(2);
|
||||
for (int i = 0; i < 2; ++i) {
|
||||
Thread thread = new Thread() {
|
||||
|
||||
public void run() {
|
||||
try {
|
||||
for (int i = 0; i < 1000; ++i) {
|
||||
workLatch.countDown();
|
||||
Connection conn = dataSource.getConnection();
|
||||
conn.close();
|
||||
}
|
||||
} catch (Exception ex) {
|
||||
ex.printStackTrace();
|
||||
} finally {
|
||||
workCompleteLatch.countDown();
|
||||
}
|
||||
}
|
||||
};
|
||||
thread.start();
|
||||
}
|
||||
workLatch.await();
|
||||
|
||||
latch_0.countDown();
|
||||
|
||||
workCompleteLatch.await();
|
||||
|
||||
Assert.assertEquals(2001, dataSource.getConnectCount());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,87 @@
|
||||
package com.alibaba.druid.pool.oracle;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.Statement;
|
||||
|
||||
import junit.framework.Assert;
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import com.alibaba.druid.pool.DruidDataSource;
|
||||
import com.alibaba.druid.pool.vendor.OracleValidConnectionChecker;
|
||||
|
||||
/**
|
||||
* 这个场景测试minIdle > maxActive
|
||||
*
|
||||
* @author wenshao<szujobs@hotmail.com>
|
||||
*/
|
||||
public class DruidDataSourceTest_oracle3 extends TestCase {
|
||||
|
||||
private DruidDataSource dataSource;
|
||||
|
||||
protected void setUp() throws Exception {
|
||||
dataSource = new DruidDataSource();
|
||||
dataSource.setUrl("jdbc:oracle:thin:@192.168.122.184:1521:ORCL");
|
||||
dataSource.setUsername("system");
|
||||
dataSource.setPassword("druid");
|
||||
dataSource.setInitialSize(1);
|
||||
dataSource.setMaxActive(14);
|
||||
dataSource.setMinIdle(1);
|
||||
dataSource.setMinEvictableIdleTimeMillis(300 * 1000); // 300 / 10
|
||||
dataSource.setTimeBetweenEvictionRunsMillis(180 * 1000); // 180 / 10
|
||||
dataSource.setTestWhileIdle(false);
|
||||
dataSource.setTestOnBorrow(false);
|
||||
dataSource.setValidationQuery("SELECT 1 FROM DUAL");
|
||||
dataSource.setFilters("stat,log4j");
|
||||
}
|
||||
|
||||
protected void tearDown() throws Exception {
|
||||
dataSource.close();
|
||||
}
|
||||
|
||||
public void test_error() throws Exception {
|
||||
dataSource.init();
|
||||
Assert.assertTrue(dataSource.isOracle());
|
||||
Assert.assertTrue(dataSource.getValidConnectionChecker() instanceof OracleValidConnectionChecker);
|
||||
|
||||
try {
|
||||
Connection conn = dataSource.getConnection();
|
||||
Statement stmt = conn.createStatement();
|
||||
ResultSet rs = stmt.executeQuery("SELECT 1 FROM DUAL");
|
||||
while (rs.next()) {
|
||||
|
||||
}
|
||||
rs.close();
|
||||
stmt.close();
|
||||
conn.close();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
try {
|
||||
Connection conn = dataSource.getConnection();
|
||||
Statement stmt = conn.createStatement();
|
||||
ResultSet rs = stmt.executeQuery("SELECT 1 FROM DUAL");
|
||||
while (rs.next()) {
|
||||
|
||||
}
|
||||
rs.close();
|
||||
stmt.close();
|
||||
conn.close();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
try {
|
||||
Connection conn = dataSource.getConnection();
|
||||
Statement stmt = conn.createStatement();
|
||||
ResultSet rs = stmt.executeQuery("SELECT 1 FROM DUAL");
|
||||
while (rs.next()) {
|
||||
|
||||
}
|
||||
rs.close();
|
||||
stmt.close();
|
||||
conn.close();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user