mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
Encypt tool and Decrypt
This commit is contained in:
@@ -1,54 +0,0 @@
|
||||
/*
|
||||
* 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.config;
|
||||
|
||||
import java.sql.Connection;
|
||||
|
||||
import com.alibaba.druid.mock.MockDriver;
|
||||
import com.alibaba.druid.pool.DruidDataSource;
|
||||
import com.alibaba.druid.util.JdbcUtils;
|
||||
|
||||
import junit.framework.Assert;
|
||||
import junit.framework.TestCase;
|
||||
|
||||
public class ConfigLoadFilterTest extends TestCase {
|
||||
|
||||
private DruidDataSource dataSource;
|
||||
|
||||
protected void setUp() throws Exception {
|
||||
dataSource = new DruidDataSource();
|
||||
dataSource.setUrl("druid-configFile=bvt/config/config-0.properties");
|
||||
}
|
||||
|
||||
protected void tearDown() throws Exception {
|
||||
JdbcUtils.close(dataSource);
|
||||
}
|
||||
|
||||
public void test_config() throws Exception {
|
||||
Assert.assertEquals(true, dataSource.isTestOnBorrow()); // default
|
||||
Assert.assertEquals(8, dataSource.getMaxActive());
|
||||
|
||||
dataSource.init();
|
||||
|
||||
Assert.assertEquals(false, dataSource.isTestOnBorrow());
|
||||
Assert.assertEquals(10, dataSource.getMaxActive());
|
||||
Assert.assertEquals("jdbc:mock:config-0", dataSource.getUrl());
|
||||
Assert.assertEquals(MockDriver.instance, dataSource.getDriver());
|
||||
|
||||
Connection conn = dataSource.getConnection();
|
||||
conn.close();
|
||||
}
|
||||
}
|
||||
@@ -1,62 +0,0 @@
|
||||
/*
|
||||
* 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.config;
|
||||
|
||||
import java.sql.Connection;
|
||||
|
||||
import junit.framework.Assert;
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import com.alibaba.druid.filter.config.ConfigFilter;
|
||||
import com.alibaba.druid.mock.MockDriver;
|
||||
import com.alibaba.druid.pool.DruidDataSource;
|
||||
import com.alibaba.druid.util.JdbcUtils;
|
||||
|
||||
public class ConfigLoadFilterTest1 extends TestCase {
|
||||
|
||||
private DruidDataSource dataSource;
|
||||
private ConfigFilter configFilter;
|
||||
|
||||
protected void setUp() throws Exception {
|
||||
configFilter = new ConfigFilter();
|
||||
configFilter.setFile("bvt/config/config-1.properties");
|
||||
|
||||
dataSource = new DruidDataSource();
|
||||
dataSource.getProxyFilters().add(configFilter);
|
||||
}
|
||||
|
||||
protected void tearDown() throws Exception {
|
||||
JdbcUtils.close(dataSource);
|
||||
}
|
||||
|
||||
public void test_config() throws Exception {
|
||||
Assert.assertEquals(true, dataSource.isTestOnBorrow()); // default
|
||||
Assert.assertEquals(8, dataSource.getMaxActive());
|
||||
|
||||
Assert.assertEquals(1, dataSource.getProxyFilters().size());
|
||||
|
||||
dataSource.init();
|
||||
|
||||
Assert.assertEquals(false, dataSource.isTestOnBorrow());
|
||||
Assert.assertEquals(11, dataSource.getMaxActive());
|
||||
Assert.assertEquals("jdbc:mock:config-1", dataSource.getUrl());
|
||||
Assert.assertEquals(MockDriver.instance, dataSource.getDriver());
|
||||
Assert.assertEquals(3, dataSource.getProxyFilters().size());
|
||||
|
||||
Connection conn = dataSource.getConnection();
|
||||
conn.close();
|
||||
}
|
||||
}
|
||||
@@ -1,61 +0,0 @@
|
||||
/*
|
||||
* 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.config;
|
||||
|
||||
import java.sql.Connection;
|
||||
|
||||
import junit.framework.Assert;
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import com.alibaba.druid.filter.config.ConfigFilter;
|
||||
import com.alibaba.druid.mock.MockDriver;
|
||||
import com.alibaba.druid.pool.DruidDataSource;
|
||||
import com.alibaba.druid.util.JdbcUtils;
|
||||
|
||||
public class ConfigLoadFilterTest2 extends TestCase {
|
||||
|
||||
private DruidDataSource dataSource;
|
||||
private ConfigFilter configFilter;
|
||||
|
||||
protected void setUp() throws Exception {
|
||||
configFilter = new ConfigFilter();
|
||||
configFilter.setFile("bvt/config/config-2.properties");
|
||||
configFilter.setKey("MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAMmVkLy+Cy/3rQ1OyH7nz1tT8Bu2KEU+z5LbeZ1yue/RN4KlcOEalj/K9Ev05Lpfu9cOdYnYM8ka1MiQdHLyJP0CAwEAAQ==");
|
||||
|
||||
dataSource = new DruidDataSource();
|
||||
dataSource.getProxyFilters().add(configFilter);
|
||||
}
|
||||
|
||||
protected void tearDown() throws Exception {
|
||||
JdbcUtils.close(dataSource);
|
||||
}
|
||||
|
||||
public void test_config() throws Exception {
|
||||
Assert.assertEquals(true, dataSource.isTestOnBorrow()); // default
|
||||
Assert.assertEquals(8, dataSource.getMaxActive());
|
||||
|
||||
dataSource.init();
|
||||
|
||||
Assert.assertEquals(false, dataSource.isTestOnBorrow());
|
||||
Assert.assertEquals(12, dataSource.getMaxActive());
|
||||
Assert.assertEquals("jdbc:mock:config-2", dataSource.getUrl());
|
||||
Assert.assertEquals(MockDriver.instance, dataSource.getDriver());
|
||||
Assert.assertEquals("abc", dataSource.getPassword());
|
||||
|
||||
Connection conn = dataSource.getConnection();
|
||||
conn.close();
|
||||
}
|
||||
}
|
||||
@@ -1,59 +0,0 @@
|
||||
/*
|
||||
* 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.config;
|
||||
|
||||
import java.sql.Connection;
|
||||
|
||||
import com.alibaba.druid.filter.config.ConfigFilter;
|
||||
import com.alibaba.druid.mock.MockDriver;
|
||||
import com.alibaba.druid.pool.DruidDataSource;
|
||||
import com.alibaba.druid.util.JdbcUtils;
|
||||
|
||||
import junit.framework.Assert;
|
||||
import junit.framework.TestCase;
|
||||
|
||||
public class ConfigLoadFilterTest3 extends TestCase {
|
||||
private DruidDataSource dataSource;
|
||||
private ConfigFilter configFilter;
|
||||
|
||||
protected void setUp() throws Exception {
|
||||
configFilter = new ConfigFilter();
|
||||
configFilter.setEncryptedPassword("TFxB6eJcgxE1hxOgiwOC/L7zWR/9vnSIfpggI2PTfcvvRhSnCGCPwI9n03fiJiLmRdnDU2/KaVTJYwz8zzkBqg==");
|
||||
configFilter.setKey("MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAMmVkLy+Cy/3rQ1OyH7nz1tT8Bu2KEU+z5LbeZ1yue/RN4KlcOEalj/K9Ev05Lpfu9cOdYnYM8ka1MiQdHLyJP0CAwEAAQ==");
|
||||
|
||||
dataSource = new DruidDataSource();
|
||||
dataSource.setUrl("jdbc:mock:config-3");
|
||||
dataSource.setMaxActive(13);
|
||||
dataSource.getProxyFilters().add(configFilter);
|
||||
dataSource.setTestOnBorrow(false);
|
||||
}
|
||||
|
||||
protected void tearDown() throws Exception {
|
||||
JdbcUtils.close(dataSource);
|
||||
}
|
||||
|
||||
public void test_config() throws Exception {
|
||||
dataSource.init();
|
||||
|
||||
Assert.assertEquals(13, dataSource.getMaxActive());
|
||||
Assert.assertEquals("jdbc:mock:config-3", dataSource.getUrl());
|
||||
Assert.assertEquals(MockDriver.instance, dataSource.getDriver());
|
||||
Assert.assertEquals("abc", dataSource.getPassword());
|
||||
|
||||
Connection conn = dataSource.getConnection();
|
||||
conn.close();
|
||||
}
|
||||
}
|
||||
@@ -1,62 +0,0 @@
|
||||
/*
|
||||
* 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.config;
|
||||
|
||||
import java.sql.Connection;
|
||||
|
||||
import com.alibaba.druid.filter.config.ConfigFilter;
|
||||
import com.alibaba.druid.mock.MockDriver;
|
||||
import com.alibaba.druid.pool.DruidDataSource;
|
||||
import com.alibaba.druid.util.JdbcUtils;
|
||||
|
||||
import junit.framework.Assert;
|
||||
import junit.framework.TestCase;
|
||||
|
||||
public class ConfigLoadFilterTest4 extends TestCase {
|
||||
|
||||
private DruidDataSource dataSource;
|
||||
private ConfigFilter configFilter;
|
||||
|
||||
protected void setUp() throws Exception {
|
||||
System.setProperty(ConfigFilter.SYS_PROP_CONFIG_KEY,
|
||||
"MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAMmVkLy+Cy/3rQ1OyH7nz1tT8Bu2KEU+z5LbeZ1yue/RN4KlcOEalj/K9Ev05Lpfu9cOdYnYM8ka1MiQdHLyJP0CAwEAAQ==");
|
||||
|
||||
configFilter = new ConfigFilter();
|
||||
configFilter.setEncryptedPassword("TFxB6eJcgxE1hxOgiwOC/L7zWR/9vnSIfpggI2PTfcvvRhSnCGCPwI9n03fiJiLmRdnDU2/KaVTJYwz8zzkBqg==");
|
||||
dataSource = new DruidDataSource();
|
||||
dataSource.setUrl("jdbc:mock:config-3");
|
||||
dataSource.setMaxActive(13);
|
||||
dataSource.getProxyFilters().add(configFilter);
|
||||
dataSource.setTestOnBorrow(false);
|
||||
}
|
||||
|
||||
protected void tearDown() throws Exception {
|
||||
JdbcUtils.close(dataSource);
|
||||
System.clearProperty(ConfigFilter.SYS_PROP_CONFIG_KEY);
|
||||
}
|
||||
|
||||
public void test_config() throws Exception {
|
||||
dataSource.init();
|
||||
|
||||
Assert.assertEquals(13, dataSource.getMaxActive());
|
||||
Assert.assertEquals("jdbc:mock:config-3", dataSource.getUrl());
|
||||
Assert.assertEquals(MockDriver.instance, dataSource.getDriver());
|
||||
Assert.assertEquals("abc", dataSource.getPassword());
|
||||
|
||||
Connection conn = dataSource.getConnection();
|
||||
conn.close();
|
||||
}
|
||||
}
|
||||
@@ -1,62 +0,0 @@
|
||||
/*
|
||||
* 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.config;
|
||||
|
||||
import java.sql.Connection;
|
||||
|
||||
import com.alibaba.druid.filter.config.ConfigFilter;
|
||||
import com.alibaba.druid.mock.MockDriver;
|
||||
import com.alibaba.druid.pool.DruidDataSource;
|
||||
import com.alibaba.druid.util.JdbcUtils;
|
||||
|
||||
import junit.framework.Assert;
|
||||
import junit.framework.TestCase;
|
||||
|
||||
public class ConfigLoadFilterTest5 extends TestCase {
|
||||
|
||||
private DruidDataSource dataSource;
|
||||
|
||||
protected void setUp() throws Exception {
|
||||
System.setProperty(ConfigFilter.SYS_PROP_CONFIG_KEY,
|
||||
"MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAMmVkLy+Cy/3rQ1OyH7nz1tT8Bu2KEU+z5LbeZ1yue/RN4KlcOEalj/K9Ev05Lpfu9cOdYnYM8ka1MiQdHLyJP0CAwEAAQ==");
|
||||
|
||||
System.setProperty(ConfigFilter.SYS_PROP_CONFIG_ENCRYPTED_PASSWORD,
|
||||
"TFxB6eJcgxE1hxOgiwOC/L7zWR/9vnSIfpggI2PTfcvvRhSnCGCPwI9n03fiJiLmRdnDU2/KaVTJYwz8zzkBqg==");
|
||||
dataSource = new DruidDataSource();
|
||||
dataSource.setUrl("jdbc:mock:config-3");
|
||||
dataSource.setMaxActive(13);
|
||||
dataSource.setFilters("config");
|
||||
dataSource.setTestOnBorrow(false);
|
||||
}
|
||||
|
||||
protected void tearDown() throws Exception {
|
||||
JdbcUtils.close(dataSource);
|
||||
System.clearProperty(ConfigFilter.SYS_PROP_CONFIG_KEY);
|
||||
System.clearProperty(ConfigFilter.SYS_PROP_CONFIG_ENCRYPTED_PASSWORD);
|
||||
}
|
||||
|
||||
public void test_config() throws Exception {
|
||||
dataSource.init();
|
||||
|
||||
Assert.assertEquals(13, dataSource.getMaxActive());
|
||||
Assert.assertEquals("jdbc:mock:config-3", dataSource.getUrl());
|
||||
Assert.assertEquals(MockDriver.instance, dataSource.getDriver());
|
||||
Assert.assertEquals("abc", dataSource.getPassword());
|
||||
|
||||
Connection conn = dataSource.getConnection();
|
||||
conn.close();
|
||||
}
|
||||
}
|
||||
@@ -1,54 +0,0 @@
|
||||
/*
|
||||
* 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.config;
|
||||
|
||||
import java.sql.Connection;
|
||||
|
||||
import com.alibaba.druid.mock.MockDriver;
|
||||
import com.alibaba.druid.pool.DruidDataSource;
|
||||
import com.alibaba.druid.util.JdbcUtils;
|
||||
|
||||
import junit.framework.Assert;
|
||||
import junit.framework.TestCase;
|
||||
|
||||
public class ConfigLoadFilterTest6 extends TestCase {
|
||||
|
||||
private DruidDataSource dataSource;
|
||||
|
||||
protected void setUp() throws Exception {
|
||||
dataSource = new DruidDataSource();
|
||||
dataSource.setUrl("druid-configFile=https://raw.github.com/AlibabaTech/druid/master/src/test/resources/bvt/config/config-0.properties");
|
||||
}
|
||||
|
||||
protected void tearDown() throws Exception {
|
||||
JdbcUtils.close(dataSource);
|
||||
}
|
||||
|
||||
public void test_http_config() throws Exception {
|
||||
Assert.assertEquals(true, dataSource.isTestOnBorrow()); // default
|
||||
Assert.assertEquals(8, dataSource.getMaxActive());
|
||||
|
||||
dataSource.init();
|
||||
|
||||
Assert.assertEquals(false, dataSource.isTestOnBorrow());
|
||||
Assert.assertEquals(10, dataSource.getMaxActive());
|
||||
Assert.assertEquals("jdbc:mock:config-0", dataSource.getUrl());
|
||||
Assert.assertEquals(MockDriver.instance, dataSource.getDriver());
|
||||
|
||||
Connection conn = dataSource.getConnection();
|
||||
conn.close();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package com.alibaba.druid.bvt.filter.config;
|
||||
|
||||
import com.alibaba.druid.filter.config.ConfigFilterTest;
|
||||
import com.alibaba.druid.filter.config.ConfigLoaderFactoryTest;
|
||||
import com.alibaba.druid.filter.config.impl.FileConfigLoaderTest;
|
||||
import com.alibaba.druid.filter.config.impl.FileConfigLoaderTest1;
|
||||
import com.alibaba.druid.filter.config.impl.HttpConfigLoaderTest;
|
||||
import com.alibaba.druid.filter.config.impl.HttpConfigLoaderTest1;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.runners.Suite;
|
||||
|
||||
/**
|
||||
* @author Jonas Yang
|
||||
*/
|
||||
|
||||
@RunWith(Suite.class)
|
||||
@Suite.SuiteClasses({
|
||||
ConfigLoaderFactoryTest.class,
|
||||
FileConfigLoaderTest.class,
|
||||
FileConfigLoaderTest1.class,
|
||||
HttpConfigLoaderTest.class,
|
||||
HttpConfigLoaderTest1.class,
|
||||
ConfigFilterTest.class
|
||||
})
|
||||
public class ConfigTestSuite {
|
||||
}
|
||||
@@ -1,44 +0,0 @@
|
||||
/*
|
||||
* 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.config;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import junit.framework.Assert;
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import com.alibaba.druid.filter.config.ConfigTool;
|
||||
|
||||
public class ConfigToolTest extends TestCase {
|
||||
|
||||
public void test_configTool() throws Exception {
|
||||
Map<String, String> result = ConfigTool.gen(1024);
|
||||
|
||||
String publicKey = result.get("publicKey");
|
||||
String privateKey = result.get("privateKey");
|
||||
|
||||
System.out.println("publicKey : " + publicKey);
|
||||
System.out.println("privateKey : " + privateKey);
|
||||
|
||||
String cipher = ConfigTool.encrypt(privateKey, "abcdef").get("result");
|
||||
System.out.println("cipher : " + cipher);
|
||||
|
||||
String plain = ConfigTool.decrypt(publicKey, cipher).get("result");
|
||||
System.out.println("plain : " + plain);
|
||||
|
||||
Assert.assertEquals("abcdef", plain);
|
||||
}
|
||||
}
|
||||
@@ -1,14 +1,15 @@
|
||||
package com.alibaba.druid.bvt.pool;
|
||||
|
||||
import com.alibaba.druid.pool.DecryptException;
|
||||
import com.alibaba.druid.pool.Decrypter;
|
||||
import com.alibaba.druid.support.security.decryptor.DecryptException;
|
||||
import com.alibaba.druid.support.security.decryptor.Decrypter;
|
||||
import com.alibaba.druid.pool.DruidDataSource;
|
||||
import com.alibaba.druid.pool.SensitiveParameters;
|
||||
import com.alibaba.druid.support.security.decryptor.SensitiveParameters;
|
||||
import com.alibaba.druid.util.JdbcUtils;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.sql.SQLException;
|
||||
import java.util.Properties;
|
||||
|
||||
/**
|
||||
* @author Jonas Yang
|
||||
@@ -20,23 +21,13 @@ public class DruidDataSourceDecryptTest {
|
||||
DruidDataSource dataSource = new DruidDataSource();
|
||||
dataSource.setUrl("jdbc:oracle:thin:@");
|
||||
dataSource.setUsername("xiaoyu");
|
||||
dataSource.setPassword("druid");
|
||||
|
||||
dataSource.setDecrypter(new Decrypter() {
|
||||
@Override
|
||||
public SensitiveParameters decrypt(SensitiveParameters parameters) throws DecryptException {
|
||||
Assert.assertEquals("xiaoyu", parameters.getUsername());
|
||||
throw new DecryptException("Test");
|
||||
}
|
||||
});
|
||||
dataSource.setPassword("OJfUm6WCHi7EuXqE6aEc+Po2xFrAGBeSNy8O2jWhV2FTG8/5kbRRr2rjNKhptlevm/03Y0048P7h88gdUOXAYg==");
|
||||
dataSource.setFilters("config");
|
||||
dataSource.setConnectionProperties("config.decrypt=RSA");
|
||||
|
||||
try {
|
||||
dataSource.init();
|
||||
dataSource.createPhysicalConnection();
|
||||
Assert.assertTrue("Got Here.", false);
|
||||
} catch (SQLException e) {
|
||||
Assert.assertEquals("The cause clas is " + e.getCause().getClass(), DecryptException.class, e.getCause().getClass());
|
||||
Assert.assertEquals("The excepiton message is " + e.getCause().getMessage(), "Test", e.getCause().getMessage());
|
||||
Assert.assertEquals("The password is " + dataSource.getPassword(), "xiaoyu", dataSource.getPassword());
|
||||
} finally {
|
||||
JdbcUtils.close(dataSource);
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package com.alibaba.druid.bvt.support.security;
|
||||
|
||||
import com.alibaba.druid.support.security.decryptor.AesDecrypterTest;
|
||||
import com.alibaba.druid.support.security.decryptor.DecrypterFactoryTest;
|
||||
import com.alibaba.druid.support.security.decryptor.RsaDecrypterTest;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.runners.Suite;
|
||||
@@ -11,7 +12,8 @@ import org.junit.runners.Suite;
|
||||
@RunWith(Suite.class)
|
||||
@Suite.SuiteClasses({
|
||||
AesDecrypterTest.class,
|
||||
RsaDecrypterTest.class
|
||||
RsaDecrypterTest.class,
|
||||
DecrypterFactoryTest.class
|
||||
})
|
||||
public class DecrypterTestSuite {
|
||||
}
|
||||
|
||||
@@ -0,0 +1,55 @@
|
||||
package com.alibaba.druid.filter.config;
|
||||
|
||||
import com.alibaba.druid.pool.DruidDataSourceFactory;
|
||||
import junit.framework.Assert;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.io.PrintWriter;
|
||||
|
||||
/**
|
||||
* @author Jonas Yang
|
||||
*/
|
||||
public class ConfigFileGenerator {
|
||||
|
||||
protected String filePath;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
PrintWriter out = null;
|
||||
|
||||
try {
|
||||
File file = File.createTempFile("MyTest", Long.toString(System.nanoTime()));
|
||||
filePath = file.getAbsolutePath();
|
||||
|
||||
out = new PrintWriter(new FileWriter(file));
|
||||
out.println(DruidDataSourceFactory.PROP_USERNAME + "=test1");
|
||||
out.println(DruidDataSourceFactory.PROP_PASSWORD + "=OJfUm6WCHi7EuXqE6aEc+Po2xFrAGBeSNy8O2jWhV2FTG8/5kbRRr2rjNKhptlevm/03Y0048P7h88gdUOXAYg==");
|
||||
out.println(DruidDataSourceFactory.PROP_URL + "=jdbc:oracle:thin:@");
|
||||
} catch (IOException e) {
|
||||
Assert.assertNull("Failed to init resource.", e);
|
||||
} finally {
|
||||
if (out != null) {
|
||||
try {
|
||||
out.close();
|
||||
} catch(Exception e){}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() {
|
||||
if (this.filePath == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
File file = new File(this.filePath);
|
||||
if(file.exists()) {
|
||||
file.delete();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,131 @@
|
||||
package com.alibaba.druid.filter.config;
|
||||
|
||||
import com.alibaba.druid.filter.config.impl.FileConfigLoader;
|
||||
import com.alibaba.druid.mock.MockDriver;
|
||||
import com.alibaba.druid.pool.DruidDataSource;
|
||||
import com.alibaba.druid.util.JdbcUtils;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.sql.SQLException;
|
||||
|
||||
/**
|
||||
* @author Jonas Yang
|
||||
*/
|
||||
public class ConfigFilterTest extends ConfigFileGenerator {
|
||||
|
||||
String encryptedString = "OJfUm6WCHi7EuXqE6aEc+Po2xFrAGBeSNy8O2jWhV2FTG8/5kbRRr2rjNKhptlevm/03Y0048P7h88gdUOXAYg==";
|
||||
|
||||
@Test
|
||||
public void testInitRemoteConfigFile() throws SQLException {
|
||||
DruidDataSource dataSource = new DruidDataSource();
|
||||
dataSource.setFilters("config");
|
||||
dataSource.setConnectionProperties("config.file=" + FileConfigLoader.PROTOCOL_PREFIX + this.filePath);
|
||||
try {
|
||||
dataSource.init();
|
||||
|
||||
Assert.assertEquals("The username is " + dataSource.getUsername(), "test1", dataSource.getUsername());
|
||||
} finally {
|
||||
JdbcUtils.close(dataSource);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testInitInvalidRemoteConfigFile() throws SQLException {
|
||||
DruidDataSource dataSource = new DruidDataSource();
|
||||
dataSource.setFilters("config");
|
||||
dataSource.setConnectionProperties("config.file=abcdef");
|
||||
try {
|
||||
dataSource.init();
|
||||
Assert.assertTrue("It is here", false);
|
||||
} catch (IllegalArgumentException e) {
|
||||
e.printStackTrace();
|
||||
Assert.assertTrue(true);
|
||||
} finally {
|
||||
JdbcUtils.close(dataSource);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInitDecrypt() throws SQLException {
|
||||
DruidDataSource dataSource = new DruidDataSource();
|
||||
dataSource.setDriver(MockDriver.instance);
|
||||
dataSource.setUrl("");
|
||||
dataSource.setUsername("test");
|
||||
dataSource.setPassword(encryptedString);
|
||||
dataSource.setFilters("config");
|
||||
dataSource.setConnectionProperties("config.decrypt=RSA");
|
||||
|
||||
try {
|
||||
dataSource.init();
|
||||
Assert.assertEquals("The password is " + dataSource.getPassword() + ", is not xiaoyu", "xiaoyu", dataSource.getPassword());
|
||||
} finally {
|
||||
JdbcUtils.close(dataSource);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInitInvalidDecrypt() throws SQLException {
|
||||
DruidDataSource dataSource = new DruidDataSource();
|
||||
dataSource.setDriver(MockDriver.instance);
|
||||
dataSource.setUrl("");
|
||||
dataSource.setUsername("test");
|
||||
dataSource.setPassword(encryptedString);
|
||||
dataSource.setFilters("config");
|
||||
dataSource.setConnectionProperties("config.decrypt=ABCDEF");
|
||||
|
||||
try {
|
||||
dataSource.init();
|
||||
Assert.assertTrue("It is here", false);
|
||||
} catch (IllegalArgumentException e) {
|
||||
e.printStackTrace();
|
||||
Assert.assertTrue(true);
|
||||
} finally {
|
||||
JdbcUtils.close(dataSource);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInitRemoteConfigAndDecrypt() throws SQLException {
|
||||
DruidDataSource dataSource = new DruidDataSource();
|
||||
dataSource.setFilters("config");
|
||||
dataSource.setConnectionProperties("config.decrypt=RSA;config.file=" + FileConfigLoader.PROTOCOL_PREFIX + this.filePath);
|
||||
try {
|
||||
dataSource.init();
|
||||
|
||||
Assert.assertEquals("The password is " + dataSource.getPassword(), "xiaoyu", dataSource.getPassword());
|
||||
} finally {
|
||||
JdbcUtils.close(dataSource);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNormalInit() throws SQLException {
|
||||
DruidDataSource dataSource = new DruidDataSource();
|
||||
dataSource.setUrl("jdbc:oracle:thin:@");
|
||||
|
||||
try {
|
||||
dataSource.init();
|
||||
} finally {
|
||||
JdbcUtils.close(dataSource);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInvalidInit() throws SQLException {
|
||||
DruidDataSource dataSource = new DruidDataSource();
|
||||
dataSource.setDriver(MockDriver.instance);
|
||||
dataSource.setFilters("config");
|
||||
dataSource.setConnectionProperties("config.file=abcdefeg");
|
||||
|
||||
try {
|
||||
dataSource.init();
|
||||
Assert.assertTrue("Get here.", false);
|
||||
} catch (IllegalArgumentException e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
JdbcUtils.close(dataSource);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
package com.alibaba.druid.filter.config;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.Properties;
|
||||
|
||||
/**
|
||||
* @author Jonas Yang
|
||||
*/
|
||||
public class ConfigLoaderFactoryTest {
|
||||
|
||||
@Test
|
||||
public void testInitConfigLoader() {
|
||||
ConfigLoader[] configLoaders = ConfigLoaderFactory.getConfigLoaders();
|
||||
Assert.assertTrue("The size is " + configLoaders.length + " , not 2", configLoaders.length == 2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetConfigLoader() {
|
||||
ConfigLoader configLoader = ConfigLoaderFactory.getConfigLoader("abcde");
|
||||
Assert.assertEquals("The config is " + configLoader, null, configLoader);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAddConfigLoader() {
|
||||
int size = ConfigLoaderFactory.getConfigLoaders().length;
|
||||
|
||||
ConfigLoaderFactory.addConfigLoader(new ConfigLoader() {
|
||||
@Override
|
||||
public String getId() {
|
||||
return "My Test"; //To change body of implemented methods use File | Settings | File Templates.
|
||||
}
|
||||
|
||||
@Override
|
||||
public Properties loadConfig(String protocol) {
|
||||
return null; //To change body of implemented methods use File | Settings | File Templates.
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isSupported(String protocol) {
|
||||
if (protocol.startsWith("abc")) {
|
||||
return true;
|
||||
}
|
||||
return false; //To change body of implemented methods use File | Settings | File Templates.
|
||||
}
|
||||
});
|
||||
|
||||
Assert.assertEquals("The size is " + ConfigLoaderFactory.getConfigLoaders().length, ConfigLoaderFactory.getConfigLoaders().length, size + 1);
|
||||
|
||||
ConfigLoader configLoader = ConfigLoaderFactory.getConfigLoader("abc");
|
||||
Assert.assertNotNull("The config loader is null", configLoader);
|
||||
Assert.assertEquals("The config loader's id is " + configLoader.getId(), "My Test", configLoader.getId());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
package com.alibaba.druid.filter.config.impl;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* @author Jonas Yang
|
||||
*/
|
||||
public class FileConfigLoaderTest {
|
||||
|
||||
@Test
|
||||
public void testIsSupported() {
|
||||
FileConfigLoader configLoader = new FileConfigLoader();
|
||||
boolean isSupported = configLoader.isSupported(FileConfigLoader.PROTOCOL_PREFIX + "/opt/mytest");
|
||||
Assert.assertTrue("The value is " + isSupported + ", not true", isSupported);
|
||||
|
||||
isSupported = configLoader.isSupported("jdbc:/opt/mytest");
|
||||
Assert.assertFalse("The value is " + isSupported + ", not false", isSupported);
|
||||
|
||||
System.setProperty(FileConfigLoader.SYS_PROP_CONFIG_FILE, "/opt/mytest");
|
||||
isSupported = configLoader.isSupported(null);
|
||||
Assert.assertTrue("The value is " + isSupported + ", not true", isSupported);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetFilePath() {
|
||||
FileConfigLoader configLoader = new FileConfigLoader();
|
||||
String filePath = configLoader.getFilePath(FileConfigLoader.PROTOCOL_PREFIX + "/opt/test/test.properties");
|
||||
Assert.assertEquals("The path is " + filePath, filePath, "/opt/test/test.properties");
|
||||
|
||||
System.setProperty(FileConfigLoader.SYS_PROP_CONFIG_FILE, "/opt/mytest");
|
||||
filePath = configLoader.getFilePath(null);
|
||||
Assert.assertEquals("The path is " + filePath, filePath, "/opt/mytest");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
package com.alibaba.druid.filter.config.impl;
|
||||
|
||||
import com.alibaba.druid.filter.config.ConfigFileGenerator;
|
||||
import junit.framework.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.Properties;
|
||||
|
||||
/**
|
||||
* @author Jonas Yang
|
||||
*/
|
||||
public class FileConfigLoaderTest1 extends ConfigFileGenerator {
|
||||
|
||||
@Test
|
||||
public void testLoadConfigByFile() {
|
||||
FileConfigLoader configLoader = new FileConfigLoader();
|
||||
Properties p = configLoader.loadConfig(FileConfigLoader.PROTOCOL_PREFIX + this.filePath);
|
||||
|
||||
Assert.assertNotNull("The properties is null", p);
|
||||
Assert.assertEquals("The value is " + p.getProperty("username") + ", not test1", "test1", p.getProperty("username"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLoadConfigBySystemProperty() {
|
||||
System.setProperty(FileConfigLoader.SYS_PROP_CONFIG_FILE, this.filePath);
|
||||
FileConfigLoader configLoader = new FileConfigLoader();
|
||||
Properties p = configLoader.loadConfig(null);
|
||||
|
||||
Assert.assertNotNull("The properties is null", p);
|
||||
Assert.assertEquals("The value is " + p.getProperty("username") + ", not test1", "test1", p.getProperty("username"));
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLoadConfigByNotExistFile() {
|
||||
FileConfigLoader configLoader = new FileConfigLoader();
|
||||
Properties p = configLoader.loadConfig(FileConfigLoader.PROTOCOL_PREFIX + "/test/test/test");
|
||||
|
||||
Assert.assertNull("The properties is not null", p);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package com.alibaba.druid.filter.config.impl;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
|
||||
/**
|
||||
* @author Jonas Yang
|
||||
*/
|
||||
public class HttpConfigLoaderTest {
|
||||
|
||||
@Test
|
||||
public void testIsSupported() {
|
||||
HttpConfigLoader configLoader = new HttpConfigLoader();
|
||||
boolean isSupported = configLoader.isSupported(HttpConfigLoader.PROTOCOL_PREFIX + "www.5-nb.com");
|
||||
Assert.assertTrue("The value is " + isSupported + ", not true", isSupported);
|
||||
|
||||
isSupported = configLoader.isSupported("jdbc:/opt/mytest");
|
||||
Assert.assertFalse("The value is " + isSupported + ", not false", isSupported);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetUrl() throws MalformedURLException {
|
||||
String protocol = HttpConfigLoader.PROTOCOL_PREFIX + "www.5-nb.com";
|
||||
HttpConfigLoader configLoader = new HttpConfigLoader();
|
||||
URL url = configLoader.getUrl(protocol);
|
||||
Assert.assertEquals("The url is " + url, "http://www.5-nb.com", url.toString());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,99 @@
|
||||
package com.alibaba.druid.filter.config.impl;
|
||||
|
||||
import junit.framework.Assert;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStreamWriter;
|
||||
import java.io.PrintWriter;
|
||||
import java.net.ServerSocket;
|
||||
import java.net.Socket;
|
||||
import java.util.Properties;
|
||||
|
||||
/**
|
||||
* @author Jonas Yang
|
||||
*/
|
||||
public class HttpConfigLoaderTest1 {
|
||||
protected String protocol = "localhost:19999/test.properties";
|
||||
|
||||
protected ServerSocket server;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
try {
|
||||
this.server = new ServerSocket(19999);
|
||||
this.server.setReuseAddress(true);
|
||||
Thread t = new Thread("Http Server Thread") {
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
Socket socket = HttpConfigLoaderTest1.this.server.accept();
|
||||
// InputStream in = socket.getInputStream();
|
||||
// int b;
|
||||
// while ((b = in.read()) != -1) {
|
||||
// if (b == '\r') {
|
||||
// b = in.read();
|
||||
// if (b == '\n') {
|
||||
// b = in.read();
|
||||
// if (b == '\r') {
|
||||
// b = in.read();
|
||||
// if (b == '\n') {
|
||||
// break;
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// if (b == -1) {
|
||||
// break;
|
||||
// }
|
||||
// }
|
||||
|
||||
PrintWriter out = new PrintWriter(new OutputStreamWriter(socket.getOutputStream()));
|
||||
out.println("a1=test1");
|
||||
out.println("b2=test2");
|
||||
out.flush();
|
||||
socket.close();
|
||||
} catch (IOException e) {
|
||||
if (e.getMessage().indexOf("Socket closed") == -1
|
||||
&& e.getMessage().indexOf("Socket is closed") == -1)
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
};
|
||||
t.start();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() {
|
||||
if (this.server != null) {
|
||||
try {
|
||||
this.server.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLoadConfig() {
|
||||
HttpConfigLoader configLoader = new HttpConfigLoader();
|
||||
Properties p = configLoader.loadConfig(HttpConfigLoader.PROTOCOL_PREFIX + this.protocol);
|
||||
|
||||
Assert.assertNotNull("The properties is null", p);
|
||||
Assert.assertEquals("The value is " + p.getProperty("a1") + ", not test1", "test1", p.getProperty("a1"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLoadConfigByNotExistUrl() {
|
||||
HttpConfigLoader configLoader = new HttpConfigLoader();
|
||||
Properties p = configLoader.loadConfig(HttpConfigLoader.PROTOCOL_PREFIX + "localhost/jonas");
|
||||
|
||||
Assert.assertNull("The properties is not null", p);
|
||||
}
|
||||
}
|
||||
@@ -1,10 +1,10 @@
|
||||
package com.alibaba.druid.support.security.decryptor;
|
||||
|
||||
import com.alibaba.druid.pool.DecryptException;
|
||||
import com.alibaba.druid.pool.SensitiveParameters;
|
||||
import junit.framework.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.Properties;
|
||||
|
||||
/**
|
||||
* @author Jonas Yang
|
||||
*/
|
||||
@@ -13,8 +13,9 @@ public class AesDecrypterTest {
|
||||
@Test
|
||||
public void testDecrypt() throws DecryptException {
|
||||
AesDecrypter decrypter = new AesDecrypter();
|
||||
decrypter.setKey("123456");
|
||||
SensitiveParameters parameters = decrypter.decrypt(new SensitiveParameters("", "", "0UNXTyp/r/wdm+b5FAY1Jw=="));
|
||||
Properties info = new Properties();
|
||||
info.setProperty(AesDecrypter.KEY, "123456");
|
||||
SensitiveParameters parameters = decrypter.decrypt(new SensitiveParameters("", "", "0UNXTyp/r/wdm+b5FAY1Jw=="), info);
|
||||
|
||||
Assert.assertEquals("It is not same.", "xiaoyu", parameters.getPassword());
|
||||
}
|
||||
@@ -22,7 +23,7 @@ public class AesDecrypterTest {
|
||||
@Test
|
||||
public void testDecryptWithEmptyKey() throws DecryptException {
|
||||
AesDecrypter decrypter = new AesDecrypter();
|
||||
SensitiveParameters parameters = decrypter.decrypt(new SensitiveParameters("", "", "AzgdbVU3SIK6A8+0qg+Btg=="));
|
||||
SensitiveParameters parameters = decrypter.decrypt(new SensitiveParameters("", "", "AzgdbVU3SIK6A8+0qg+Btg=="), null);
|
||||
|
||||
Assert.assertEquals("It is not same.", "xiaoyu", parameters.getPassword());
|
||||
}
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
package com.alibaba.druid.support.security.decryptor;
|
||||
|
||||
import junit.framework.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* @author Jonas Yang
|
||||
*/
|
||||
public class DecrypterFactoryTest {
|
||||
|
||||
@Test
|
||||
public void testGetDecrypter() throws DecryptException {
|
||||
Assert.assertNotNull(DecrypterFactory.getDecrypter("RSA"));
|
||||
Assert.assertEquals(DecrypterFactory.getDecrypter("RSA").getClass(), RsaDecrypter.class);
|
||||
|
||||
Assert.assertNotNull(DecrypterFactory.getDecrypter("AES"));
|
||||
Assert.assertEquals(DecrypterFactory.getDecrypter("AES").getClass(), AesDecrypter.class);
|
||||
|
||||
Assert.assertNull(DecrypterFactory.getDecrypter("ajfdjksfklds;"));
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,5 @@
|
||||
package com.alibaba.druid.support.security.decryptor;
|
||||
|
||||
import com.alibaba.druid.pool.DecryptException;
|
||||
import com.alibaba.druid.pool.SensitiveParameters;
|
||||
import com.alibaba.druid.util.Base64;
|
||||
import junit.framework.Assert;
|
||||
import org.junit.After;
|
||||
@@ -9,6 +7,8 @@ import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.*;
|
||||
import java.security.PublicKey;
|
||||
import java.util.Properties;
|
||||
|
||||
/**
|
||||
* @author Jonas Yang
|
||||
@@ -52,11 +52,71 @@ public class RsaDecrypterTest {
|
||||
this.keyFilePath = null;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetPublicKeyByString() {
|
||||
RsaDecrypter rsaDecrypter = new RsaDecrypter();
|
||||
PublicKey key = rsaDecrypter.getPublicKeyByString(this.publicKeyContent);
|
||||
Assert.assertNotNull("It is null", key);
|
||||
|
||||
key = rsaDecrypter.getPublicKeyByString(null);
|
||||
Assert.assertNotNull("It is null", key);
|
||||
Assert.assertEquals(RsaDecrypter.DEFAULT_PUBLIC_KEY_STRING, Base64.byteArrayToBase64(key.getEncoded()));
|
||||
|
||||
try {
|
||||
key = rsaDecrypter.getPublicKeyByString("abcdef");
|
||||
Assert.assertTrue("It is here", false);
|
||||
} catch (Exception e) {
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetPublicKeyByPublicKeyFile() {
|
||||
RsaDecrypter rsaDecrypter = new RsaDecrypter();
|
||||
PublicKey key = rsaDecrypter.getPublicKeyByPublicKeyFile(this.keyFilePath);
|
||||
Assert.assertNotNull("It is null", key);
|
||||
|
||||
key = rsaDecrypter.getPublicKeyByPublicKeyFile(null);
|
||||
Assert.assertNotNull("It is null", key);
|
||||
Assert.assertEquals(RsaDecrypter.DEFAULT_PUBLIC_KEY_STRING, Base64.byteArrayToBase64(key.getEncoded()));
|
||||
|
||||
try {
|
||||
key = rsaDecrypter.getPublicKeyByPublicKeyFile("abcdef");
|
||||
Assert.assertTrue("It is here", false);
|
||||
} catch (Exception e) {
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetPublicKeyByX509File() {
|
||||
//No test
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetPublicKey() {
|
||||
RsaDecrypter rsaDecrypter = new RsaDecrypter();
|
||||
PublicKey key = rsaDecrypter.getPublicKey(null);
|
||||
Assert.assertNotNull("It is null", key);
|
||||
Assert.assertEquals(RsaDecrypter.DEFAULT_PUBLIC_KEY_STRING, Base64.byteArrayToBase64(key.getEncoded()));
|
||||
|
||||
Properties info = new Properties();
|
||||
info.put(RsaDecrypter.KEY, this.publicKeyContent);
|
||||
key = rsaDecrypter.getPublicKey(info);
|
||||
Assert.assertNotNull("It is null", key);
|
||||
Assert.assertEquals(this.publicKeyContent, Base64.byteArrayToBase64(key.getEncoded()));
|
||||
|
||||
info = new Properties();
|
||||
info.put(RsaDecrypter.KEY_FILE, this.keyFilePath);
|
||||
key = rsaDecrypter.getPublicKey(info);
|
||||
Assert.assertNotNull("It is null", key);
|
||||
Assert.assertEquals(this.publicKeyContent, Base64.byteArrayToBase64(key.getEncoded()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDecrypt() throws DecryptException {
|
||||
RsaDecrypter decrypter = new RsaDecrypter();
|
||||
decrypter.setPublicKeyFile(this.keyFilePath);
|
||||
SensitiveParameters parameters = decrypter.decrypt(new SensitiveParameters("", "", encryptedString));
|
||||
Properties info = new Properties();
|
||||
info.put(RsaDecrypter.KEY_FILE, this.keyFilePath);
|
||||
SensitiveParameters parameters = decrypter.decrypt(new SensitiveParameters("", "", encryptedString), info);
|
||||
|
||||
Assert.assertEquals("It is not same.", "xiaoyu", parameters.getPassword());
|
||||
}
|
||||
@@ -64,7 +124,7 @@ public class RsaDecrypterTest {
|
||||
@Test
|
||||
public void testDecryptWithEmptyKey() throws DecryptException {
|
||||
RsaDecrypter decrypter = new RsaDecrypter();
|
||||
SensitiveParameters parameters = decrypter.decrypt(new SensitiveParameters("", "", "OJfUm6WCHi7EuXqE6aEc+Po2xFrAGBeSNy8O2jWhV2FTG8/5kbRRr2rjNKhptlevm/03Y0048P7h88gdUOXAYg=="));
|
||||
SensitiveParameters parameters = decrypter.decrypt(new SensitiveParameters("", "", "OJfUm6WCHi7EuXqE6aEc+Po2xFrAGBeSNy8O2jWhV2FTG8/5kbRRr2rjNKhptlevm/03Y0048P7h88gdUOXAYg=="), null);
|
||||
|
||||
Assert.assertEquals("It is not same.", "xiaoyu", parameters.getPassword());
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user