mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
Encrypt and Decrypt tool, and load remote config file
This commit is contained in:
@@ -1,11 +1,11 @@
|
||||
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 com.alibaba.druid.filter.config.loader.ConfigLoaderFactoryTest;
|
||||
import com.alibaba.druid.filter.config.loader.impl.FileConfigLoaderTest;
|
||||
import com.alibaba.druid.filter.config.loader.impl.FileConfigLoaderTest1;
|
||||
import com.alibaba.druid.filter.config.loader.impl.HttpConfigLoaderTest;
|
||||
import com.alibaba.druid.filter.config.loader.impl.HttpConfigLoaderTest1;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.runners.Suite;
|
||||
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
package com.alibaba.druid.bvt.filter.config;
|
||||
|
||||
import com.alibaba.druid.filter.config.security.decrypter.DecrypterFactoryTest;
|
||||
import com.alibaba.druid.filter.config.security.decrypter.RsaDecrypterTest;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.runners.Suite;
|
||||
|
||||
/**
|
||||
* @author Jonas Yang
|
||||
*/
|
||||
@RunWith(Suite.class)
|
||||
@Suite.SuiteClasses({
|
||||
RsaDecrypterTest.class,
|
||||
DecrypterFactoryTest.class
|
||||
})
|
||||
public class DecrypterTestSuite {
|
||||
}
|
||||
@@ -1,15 +1,11 @@
|
||||
package com.alibaba.druid.bvt.pool;
|
||||
|
||||
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.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
|
||||
|
||||
@@ -1,19 +0,0 @@
|
||||
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;
|
||||
|
||||
/**
|
||||
* @author Jonas Yang
|
||||
*/
|
||||
@RunWith(Suite.class)
|
||||
@Suite.SuiteClasses({
|
||||
AesDecrypterTest.class,
|
||||
RsaDecrypterTest.class,
|
||||
DecrypterFactoryTest.class
|
||||
})
|
||||
public class DecrypterTestSuite {
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
package com.alibaba.druid.filter.config;
|
||||
|
||||
import com.alibaba.druid.filter.config.impl.FileConfigLoader;
|
||||
import com.alibaba.druid.filter.config.loader.impl.FileConfigLoader;
|
||||
import com.alibaba.druid.mock.MockDriver;
|
||||
import com.alibaba.druid.pool.DruidDataSource;
|
||||
import com.alibaba.druid.util.JdbcUtils;
|
||||
|
||||
@@ -1,99 +0,0 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
+3
-1
@@ -1,5 +1,7 @@
|
||||
package com.alibaba.druid.filter.config;
|
||||
package com.alibaba.druid.filter.config.loader;
|
||||
|
||||
import com.alibaba.druid.filter.config.loader.ConfigLoader;
|
||||
import com.alibaba.druid.filter.config.loader.ConfigLoaderFactory;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package com.alibaba.druid.filter.config.impl;
|
||||
package com.alibaba.druid.filter.config.loader.impl;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package com.alibaba.druid.filter.config.impl;
|
||||
package com.alibaba.druid.filter.config.loader.impl;
|
||||
|
||||
import com.alibaba.druid.filter.config.ConfigFileGenerator;
|
||||
import junit.framework.Assert;
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package com.alibaba.druid.filter.config.impl;
|
||||
package com.alibaba.druid.filter.config.loader.impl;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
@@ -0,0 +1,101 @@
|
||||
package com.alibaba.druid.filter.config.loader.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() {
|
||||
while (true) {
|
||||
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
-4
@@ -1,4 +1,4 @@
|
||||
package com.alibaba.druid.support.security.decryptor;
|
||||
package com.alibaba.druid.filter.config.security.decrypter;
|
||||
|
||||
import junit.framework.Assert;
|
||||
import org.junit.Test;
|
||||
@@ -13,9 +13,6 @@ public class DecrypterFactoryTest {
|
||||
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;"));
|
||||
}
|
||||
}
|
||||
+2
-2
@@ -1,4 +1,4 @@
|
||||
package com.alibaba.druid.support.security.decryptor;
|
||||
package com.alibaba.druid.filter.config.security.decrypter;
|
||||
|
||||
import com.alibaba.druid.util.Base64;
|
||||
import junit.framework.Assert;
|
||||
@@ -124,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=="), null);
|
||||
SensitiveParameters parameters = decrypter.decrypt(new SensitiveParameters("", "", "OJfUm6WCHi7EuXqE6aEc+Po2xFrAGBeSNy8O2jWhV2FTG8/5kbRRr2rjNKhptlevm/03Y0048P7h88gdUOXAYg=="), (Properties) null);
|
||||
|
||||
Assert.assertEquals("It is not same.", "xiaoyu", parameters.getPassword());
|
||||
}
|
||||
@@ -1,30 +0,0 @@
|
||||
package com.alibaba.druid.support.security.decryptor;
|
||||
|
||||
import junit.framework.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.Properties;
|
||||
|
||||
/**
|
||||
* @author Jonas Yang
|
||||
*/
|
||||
public class AesDecrypterTest {
|
||||
|
||||
@Test
|
||||
public void testDecrypt() throws DecryptException {
|
||||
AesDecrypter decrypter = new AesDecrypter();
|
||||
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());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDecryptWithEmptyKey() throws DecryptException {
|
||||
AesDecrypter decrypter = new AesDecrypter();
|
||||
SensitiveParameters parameters = decrypter.decrypt(new SensitiveParameters("", "", "AzgdbVU3SIK6A8+0qg+Btg=="), null);
|
||||
|
||||
Assert.assertEquals("It is not same.", "xiaoyu", parameters.getPassword());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user