mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
Merge remote-tracking branch 'upstream/master'
This commit is contained in:
@@ -23,7 +23,7 @@ public class ConfigFilterTest2 extends TestCase {
|
||||
public void test_decrypt() throws Exception {
|
||||
dataSource.addConnectionProperty(ConfigFilter.CONFIG_FILE, "bvt/config/config-0.properties");
|
||||
Assert.assertEquals(1, dataSource.getProxyFilters().size());
|
||||
|
||||
|
||||
dataSource.init();
|
||||
|
||||
Assert.assertEquals("jdbc:mock:config-0", dataSource.getUrl());
|
||||
@@ -42,7 +42,7 @@ public class ConfigFilterTest2 extends TestCase {
|
||||
Assert.assertEquals(11, dataSource.getMaxActive());
|
||||
Assert.assertEquals(3, dataSource.getProxyFilters().size());
|
||||
}
|
||||
|
||||
|
||||
public void test_decrypt2() throws Exception {
|
||||
dataSource.addConnectionProperty(ConfigFilter.CONFIG_FILE, "bvt/config/config-2.properties");
|
||||
dataSource.addConnectionProperty(ConfigFilter.CONFIG_DECRYPT, "true");
|
||||
@@ -55,16 +55,49 @@ public class ConfigFilterTest2 extends TestCase {
|
||||
Assert.assertEquals(12, dataSource.getMaxActive());
|
||||
Assert.assertEquals("abcdefg1234567890", dataSource.getPassword());
|
||||
}
|
||||
|
||||
|
||||
public void test_decrypt3() throws Exception {
|
||||
dataSource.addConnectionProperty(ConfigFilter.CONFIG_FILE, "bvt/config/config-3.properties");
|
||||
Assert.assertEquals(1, dataSource.getProxyFilters().size());
|
||||
|
||||
|
||||
dataSource.init();
|
||||
|
||||
|
||||
Assert.assertEquals("jdbc:mock:config-3", dataSource.getUrl());
|
||||
Assert.assertEquals(false, dataSource.isTestOnBorrow());
|
||||
Assert.assertEquals(13, dataSource.getMaxActive());
|
||||
Assert.assertEquals("abcdefg1234567890", dataSource.getPassword());
|
||||
}
|
||||
|
||||
public void test_decrypt4() throws Exception {
|
||||
String file = Thread.currentThread().getContextClassLoader().getResource("bvt/config/config-3.properties").getFile();
|
||||
dataSource.addConnectionProperty(ConfigFilter.CONFIG_FILE, "file://" + file);
|
||||
Assert.assertEquals(1, dataSource.getProxyFilters().size());
|
||||
|
||||
dataSource.init();
|
||||
|
||||
Assert.assertEquals("jdbc:mock:config-3", dataSource.getUrl());
|
||||
Assert.assertEquals(false, dataSource.isTestOnBorrow());
|
||||
Assert.assertEquals(13, dataSource.getMaxActive());
|
||||
Assert.assertEquals("abcdefg1234567890", dataSource.getPassword());
|
||||
}
|
||||
|
||||
public void test_decrypt5() throws Exception {
|
||||
Assert.assertEquals(1, dataSource.getProxyFilters().size());
|
||||
|
||||
try {
|
||||
String file = Thread.currentThread().getContextClassLoader().getResource("bvt/config/config-2.properties").getFile();
|
||||
System.setProperty(ConfigFilter.SYS_PROP_CONFIG_FILE, "file://" + file);
|
||||
System.setProperty(ConfigFilter.SYS_PROP_CONFIG_DECRYPT, "true");
|
||||
|
||||
dataSource.init();
|
||||
|
||||
Assert.assertEquals("jdbc:mock:config-2", dataSource.getUrl());
|
||||
Assert.assertEquals(false, dataSource.isTestOnBorrow());
|
||||
Assert.assertEquals(12, dataSource.getMaxActive());
|
||||
Assert.assertEquals("abcdefg1234567890", dataSource.getPassword());
|
||||
} finally {
|
||||
System.clearProperty(ConfigFilter.SYS_PROP_CONFIG_FILE);
|
||||
System.clearProperty(ConfigFilter.SYS_PROP_CONFIG_DECRYPT);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,77 @@
|
||||
package com.alibaba.druid.bvt.filter.config;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.util.Properties;
|
||||
|
||||
import junit.framework.Assert;
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import com.alibaba.druid.filter.config.ConfigFilter;
|
||||
import com.alibaba.druid.filter.config.ConfigTools;
|
||||
import com.alibaba.druid.pool.DruidDataSource;
|
||||
import com.alibaba.druid.pool.DruidDataSourceFactory;
|
||||
import com.alibaba.druid.util.JdbcUtils;
|
||||
|
||||
public class ConfigFilterTest3 extends TestCase {
|
||||
|
||||
private DruidDataSource dataSource;
|
||||
|
||||
protected void setUp() throws Exception {
|
||||
dataSource = new DruidDataSource();
|
||||
dataSource.setFilters("config");
|
||||
}
|
||||
|
||||
protected void tearDown() throws Exception {
|
||||
JdbcUtils.close(dataSource);
|
||||
}
|
||||
|
||||
public void test_0() throws Exception {
|
||||
String password = "abcdefg1234";
|
||||
String[] keys = ConfigTools.genKeyPair(1024);
|
||||
|
||||
File file = File.createTempFile("MyTest", Long.toString(System.nanoTime()));
|
||||
Properties properties = new Properties();
|
||||
properties.put(DruidDataSourceFactory.PROP_URL, "jdbc:mock:xx0");
|
||||
properties.put(DruidDataSourceFactory.PROP_USERNAME, "sa");
|
||||
properties.put(DruidDataSourceFactory.PROP_PASSWORD, ConfigTools.encrypt(keys[0], password));
|
||||
properties.put(ConfigFilter.CONFIG_DECRYPT, "true");
|
||||
properties.store(new FileOutputStream(file), "");
|
||||
|
||||
dataSource.getConnectProperties().put(ConfigFilter.CONFIG_KEY, keys[1]);
|
||||
dataSource.getConnectProperties().put(ConfigFilter.CONFIG_FILE, "file://" + file.getAbsolutePath());
|
||||
|
||||
dataSource.init();
|
||||
|
||||
Assert.assertEquals("jdbc:mock:xx0", dataSource.getUrl());
|
||||
Assert.assertEquals("sa", dataSource.getUsername());
|
||||
Assert.assertEquals(password, dataSource.getPassword());
|
||||
}
|
||||
|
||||
public void test_sys_property() throws Exception {
|
||||
String password = "abcdefg1234";
|
||||
String[] keys = ConfigTools.genKeyPair(1024);
|
||||
|
||||
File file = File.createTempFile("MyTest", Long.toString(System.nanoTime()));
|
||||
Properties properties = new Properties();
|
||||
properties.put(DruidDataSourceFactory.PROP_URL, "jdbc:mock:xx0");
|
||||
properties.put(DruidDataSourceFactory.PROP_USERNAME, "sa");
|
||||
properties.put(DruidDataSourceFactory.PROP_PASSWORD, ConfigTools.encrypt(keys[0], password));
|
||||
properties.put(ConfigFilter.CONFIG_DECRYPT, "true");
|
||||
properties.store(new FileOutputStream(file), "");
|
||||
|
||||
System.getProperties().put(ConfigFilter.SYS_PROP_CONFIG_KEY, keys[1]);
|
||||
System.getProperties().put(ConfigFilter.SYS_PROP_CONFIG_FILE, "file://" + file.getAbsolutePath());
|
||||
|
||||
try {
|
||||
dataSource.init();
|
||||
|
||||
Assert.assertEquals("jdbc:mock:xx0", dataSource.getUrl());
|
||||
Assert.assertEquals("sa", dataSource.getUsername());
|
||||
Assert.assertEquals(password, dataSource.getPassword());
|
||||
} finally {
|
||||
System.clearProperty(ConfigFilter.SYS_PROP_CONFIG_KEY);
|
||||
System.clearProperty(ConfigFilter.SYS_PROP_CONFIG_FILE);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
package com.alibaba.druid.bvt.filter.config;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.util.Properties;
|
||||
|
||||
import junit.framework.Assert;
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import com.alibaba.druid.filter.config.ConfigFilter;
|
||||
import com.alibaba.druid.filter.config.ConfigTools;
|
||||
import com.alibaba.druid.pool.DruidDataSource;
|
||||
import com.alibaba.druid.pool.DruidDataSourceFactory;
|
||||
import com.alibaba.druid.util.JdbcUtils;
|
||||
|
||||
public class ConfigFilterTest4 extends TestCase {
|
||||
|
||||
private DruidDataSource dataSource;
|
||||
|
||||
protected void setUp() throws Exception {
|
||||
dataSource = new DruidDataSource();
|
||||
dataSource.setFilters("config");
|
||||
}
|
||||
|
||||
protected void tearDown() throws Exception {
|
||||
JdbcUtils.close(dataSource);
|
||||
}
|
||||
|
||||
public void test_0() throws Exception {
|
||||
String password = "abcdefg1234";
|
||||
String[] keys = ConfigTools.genKeyPair(1024);
|
||||
|
||||
File file = File.createTempFile("MyTest", Long.toString(System.nanoTime()));
|
||||
Properties properties = new Properties();
|
||||
properties.put(DruidDataSourceFactory.PROP_URL, "jdbc:mock:xx0");
|
||||
properties.put(DruidDataSourceFactory.PROP_USERNAME, "sa");
|
||||
properties.put(DruidDataSourceFactory.PROP_PASSWORD, ConfigTools.encrypt(keys[0], password));
|
||||
properties.put(ConfigFilter.CONFIG_DECRYPT, "true");
|
||||
properties.store(new FileOutputStream(file), "");
|
||||
|
||||
dataSource.getConnectProperties().put(ConfigFilter.CONFIG_KEY, keys[1]);
|
||||
dataSource.getConnectProperties().put(ConfigFilter.CONFIG_FILE, "file://" + file.getAbsolutePath());
|
||||
|
||||
dataSource.init();
|
||||
|
||||
Assert.assertEquals("jdbc:mock:xx0", dataSource.getUrl());
|
||||
Assert.assertEquals("sa", dataSource.getUsername());
|
||||
Assert.assertEquals(password, dataSource.getPassword());
|
||||
}
|
||||
|
||||
public void test_sys_property() throws Exception {
|
||||
String password = "abcdefg1234";
|
||||
String[] keys = ConfigTools.genKeyPair(1024);
|
||||
|
||||
File file = File.createTempFile("MyTest", Long.toString(System.nanoTime()));
|
||||
Properties properties = new Properties();
|
||||
properties.put(DruidDataSourceFactory.PROP_URL, "jdbc:mock:xx0");
|
||||
properties.put(DruidDataSourceFactory.PROP_USERNAME, "sa");
|
||||
properties.put(DruidDataSourceFactory.PROP_PASSWORD, ConfigTools.encrypt(keys[0], password));
|
||||
properties.put(ConfigFilter.CONFIG_DECRYPT, "true");
|
||||
properties.store(new FileOutputStream(file), "");
|
||||
|
||||
System.getProperties().put(ConfigFilter.SYS_PROP_CONFIG_KEY, keys[1]);
|
||||
System.getProperties().put(ConfigFilter.SYS_PROP_CONFIG_FILE, "file://" + file.getAbsolutePath());
|
||||
|
||||
try {
|
||||
dataSource.init();
|
||||
|
||||
Assert.assertEquals("jdbc:mock:xx0", dataSource.getUrl());
|
||||
Assert.assertEquals("sa", dataSource.getUsername());
|
||||
Assert.assertEquals(password, dataSource.getPassword());
|
||||
} finally {
|
||||
System.clearProperty(ConfigFilter.SYS_PROP_CONFIG_KEY);
|
||||
System.clearProperty(ConfigFilter.SYS_PROP_CONFIG_FILE);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,7 @@
|
||||
package com.alibaba.druid.bvt.filter.config;
|
||||
|
||||
import java.util.Random;
|
||||
|
||||
import junit.framework.Assert;
|
||||
import junit.framework.TestCase;
|
||||
|
||||
@@ -11,7 +13,23 @@ public class ConfigToolsTest extends TestCase {
|
||||
String plainText = "abcdef";
|
||||
String cipherText = ConfigTools.encrypt(plainText);
|
||||
String decipherText = ConfigTools.decrypt(cipherText);
|
||||
|
||||
|
||||
Assert.assertEquals(plainText, decipherText);
|
||||
}
|
||||
|
||||
public void test_genKeys() throws Exception {
|
||||
String plainText = Long.toString(new Random().nextLong());
|
||||
String[] keys = ConfigTools.genKeyPair(512);
|
||||
|
||||
String cipherText = ConfigTools.encrypt(keys[0], plainText);
|
||||
Assert.assertEquals(plainText, ConfigTools.decrypt(keys[1], cipherText));
|
||||
}
|
||||
|
||||
public void test_genKeys1024() throws Exception {
|
||||
String plainText = Long.toString(new Random().nextLong());
|
||||
String[] keys = ConfigTools.genKeyPair(1024);
|
||||
|
||||
String cipherText = ConfigTools.encrypt(keys[0], plainText);
|
||||
Assert.assertEquals(plainText, ConfigTools.decrypt(keys[1], cipherText));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,7 +19,7 @@ public class DruidDataSourceDecryptTest {
|
||||
dataSource.setUsername("xiaoyu");
|
||||
dataSource.setPassword("OJfUm6WCHi7EuXqE6aEc+Po2xFrAGBeSNy8O2jWhV2FTG8/5kbRRr2rjNKhptlevm/03Y0048P7h88gdUOXAYg==");
|
||||
dataSource.setFilters("config");
|
||||
dataSource.setConnectionProperties("config.decrypt=RSA");
|
||||
dataSource.setConnectionProperties("config.decrypt=true");
|
||||
|
||||
try {
|
||||
dataSource.init();
|
||||
|
||||
Reference in New Issue
Block a user