单元测试覆盖
parent
3c5152b871
commit
1deae436d5
|
|
@ -0,0 +1,67 @@
|
|||
package com.czcb.scfs.api.core.cipher;
|
||||
|
||||
import com.czcb.scfs.api.core.KeyText;
|
||||
import com.czcb.scfs.api.core.http.client.TestPrivacyDecryptor;
|
||||
import com.czcb.scfs.api.core.http.client.TestPrivacyEncryptor;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.security.PrivateKey;
|
||||
import java.security.Provider;
|
||||
import java.security.cert.X509Certificate;
|
||||
|
||||
class AbstractPrivacyDecryptorTest {
|
||||
|
||||
@Test
|
||||
void decrypt() {
|
||||
PrivateKey privateKey = KeyText.loadTestPrivateKeyRSA();
|
||||
PrivacyDecryptor decryptor = new TestPrivacyDecryptor(privateKey);
|
||||
|
||||
X509Certificate certificate = KeyText.loadTestRSA();
|
||||
PrivacyEncryptor encryptor = new TestPrivacyEncryptor(certificate.getPublicKey());
|
||||
|
||||
String text = "123456";
|
||||
String ecn = encryptor.encrypt(text);
|
||||
Assertions.assertEquals(text, decryptor.decrypt(ecn));
|
||||
|
||||
TestPrivacyDecryptorV2 testPrivacyDecryptorV2 = new TestPrivacyDecryptorV2(privateKey, null);
|
||||
Assertions.assertEquals(text, testPrivacyDecryptorV2.decrypt(ecn));
|
||||
}
|
||||
|
||||
@Test
|
||||
void decryptError() {
|
||||
PrivateKey privateKey = KeyText.loadTestPrivateKeyRSA2();
|
||||
PrivacyDecryptor decryptor = new TestPrivacyDecryptor(privateKey);
|
||||
|
||||
X509Certificate certificate = KeyText.loadTestRSA();
|
||||
PrivacyEncryptor encryptor = new TestPrivacyEncryptor(certificate.getPublicKey());
|
||||
|
||||
String text = "123456";
|
||||
String ecn = encryptor.encrypt(text);
|
||||
Assertions.assertThrows(IllegalArgumentException.class, () -> decryptor.decrypt(ecn));
|
||||
|
||||
PrivacyDecryptor decryptorv1 = new TestPrivacyDecryptorV2(privateKey);
|
||||
Assertions.assertThrows(IllegalArgumentException.class, () -> decryptorv1.decrypt(ecn));
|
||||
|
||||
PrivacyDecryptor decryptorv2 = new TestPrivacyDecryptorV2(privateKey, new TestProvider());
|
||||
Assertions.assertThrows(IllegalArgumentException.class, () -> decryptorv2.decrypt(ecn));
|
||||
}
|
||||
|
||||
@Test
|
||||
void getPrivateKey() {
|
||||
PrivateKey privateKey = KeyText.loadTestPrivateKeyRSA();
|
||||
PrivacyDecryptor decryptor = new TestPrivacyDecryptor(privateKey);
|
||||
Assertions.assertNotNull(decryptor.getPrivateKey());
|
||||
}
|
||||
|
||||
private static class TestPrivacyDecryptorV2 extends AbstractPrivacyDecryptor {
|
||||
public TestPrivacyDecryptorV2(PrivateKey privateKey) {
|
||||
super("RSA/ECB/", privateKey, null);
|
||||
}
|
||||
|
||||
public TestPrivacyDecryptorV2(PrivateKey privateKey, Provider provider) {
|
||||
super("RSA/ECB/OAEPWithSHA-1AndMGF1Padding", privateKey, provider);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,49 @@
|
|||
package com.czcb.scfs.api.core.cipher;
|
||||
|
||||
import com.czcb.scfs.api.core.KeyText;
|
||||
import com.czcb.scfs.api.core.http.client.TestPrivacyDecryptor;
|
||||
import com.czcb.scfs.api.core.http.client.TestPrivacyEncryptor;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.security.PrivateKey;
|
||||
import java.security.Provider;
|
||||
import java.security.PublicKey;
|
||||
import java.security.cert.X509Certificate;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
class AbstractPrivacyEncryptorTest {
|
||||
|
||||
@Test
|
||||
void encrypt() {
|
||||
PrivateKey privateKey = KeyText.loadTestPrivateKeyRSA();
|
||||
PrivacyDecryptor decryptor = new TestPrivacyDecryptor(privateKey);
|
||||
|
||||
X509Certificate certificate = KeyText.loadTestRSA();
|
||||
PrivacyEncryptor encryptor = new TestPrivacyEncryptor(certificate.getPublicKey());
|
||||
String text = "123456";
|
||||
String ecn = encryptor.encrypt(text);
|
||||
Assertions.assertEquals(text, decryptor.decrypt(ecn));
|
||||
}
|
||||
|
||||
@Test
|
||||
void encryptError() {
|
||||
X509Certificate certificate = KeyText.loadTestRSA();
|
||||
PrivacyEncryptor encryptor = new TestPrivacyEncryptorv2(certificate.getPublicKey(), new TestProvider());
|
||||
assertThrows(IllegalArgumentException.class, () -> encryptor.encrypt("123456"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void getPublicKey() {
|
||||
X509Certificate certificate = KeyText.loadTestRSA();
|
||||
PrivacyEncryptor encryptor = new TestPrivacyEncryptor(certificate.getPublicKey());
|
||||
Assertions.assertNotNull(encryptor.getPublicKey());
|
||||
}
|
||||
|
||||
private static class TestPrivacyEncryptorv2 extends AbstractPrivacyEncryptor {
|
||||
public TestPrivacyEncryptorv2(PublicKey publicKey, Provider provider) {
|
||||
super("RSA/ECB/OAEPWithSHA-1AndMGF1Padding", publicKey, provider);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,52 @@
|
|||
package com.czcb.scfs.api.core.cipher;
|
||||
|
||||
import com.czcb.scfs.api.core.KeyText;
|
||||
import com.czcb.scfs.api.core.http.client.TestVerifier;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.security.PrivateKey;
|
||||
import java.security.Provider;
|
||||
import java.security.cert.X509Certificate;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
class AbstractSignerTest {
|
||||
|
||||
@Test
|
||||
void sign() {
|
||||
TestSigner signer = new TestSigner(KeyText.loadTestPrivateKeyRSA(), null);
|
||||
String message = "1234567890";
|
||||
String sg = signer.sign(message).getResult();
|
||||
Assertions.assertNotNull(sg);
|
||||
Assertions.assertEquals("SHA256withRSA", signer.getAlgorithm());
|
||||
|
||||
List<X509Certificate> list = new ArrayList<>();
|
||||
list.add(KeyText.loadTestRSA());
|
||||
CertificateProvider certificateProvider = new LocalCertificateProvider(list);
|
||||
TestVerifier verifier = new TestVerifier(certificateProvider);
|
||||
Assertions.assertTrue(verifier.verify("", message, sg));
|
||||
}
|
||||
|
||||
@Test
|
||||
void signError() {
|
||||
TestSigner signer = new TestSigner(KeyText.loadTestPrivateKeyRSA());
|
||||
String message = "1234567890";
|
||||
Assertions.assertThrows(UnsupportedOperationException.class, () -> signer.sign(message));
|
||||
}
|
||||
|
||||
private static class TestSigner extends AbstractSigner {
|
||||
|
||||
/**
|
||||
* @param privateKey API私钥
|
||||
*/
|
||||
public TestSigner(PrivateKey privateKey, Provider provider) {
|
||||
super("SHA256withRSA", "SHA256withRSA", privateKey, provider);
|
||||
}
|
||||
|
||||
public TestSigner(PrivateKey privateKey) {
|
||||
// 错误的签名算法
|
||||
super("SHA256withRSA1", "SHA256withRSA1", privateKey, null);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,24 @@
|
|||
package com.czcb.scfs.api.core.cipher;
|
||||
|
||||
import com.czcb.scfs.api.core.KeyText;
|
||||
import com.czcb.scfs.api.core.http.client.TestVerifier;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.security.cert.X509Certificate;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
class AbstractVerifierTest {
|
||||
|
||||
@Test
|
||||
void verify() {
|
||||
List<X509Certificate> list = new ArrayList<>();
|
||||
list.add(KeyText.loadTestRSA());
|
||||
CertificateProvider certificateProvider = new LocalCertificateProvider(list);
|
||||
TestVerifier verifier = new TestVerifier(certificateProvider);
|
||||
|
||||
Assertions.assertFalse(verifier.verify("", "", ""));
|
||||
Assertions.assertFalse(verifier.verify(KeyText.loadTestRSA(), "", ""));
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,13 @@
|
|||
package com.czcb.scfs.api.core.cipher;
|
||||
|
||||
import java.security.Provider;
|
||||
|
||||
/**
|
||||
* @author wangwei
|
||||
* @date 2024/2/20
|
||||
*/
|
||||
public class TestProvider extends Provider {
|
||||
public TestProvider() {
|
||||
super("TestProvider", 1.0, "TestProvider");
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,66 @@
|
|||
package com.czcb.scfs.api.core.util;
|
||||
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
class StopWatchTest {
|
||||
|
||||
@Test
|
||||
void create() {
|
||||
StopWatch stopWatch = new StopWatch();
|
||||
Assertions.assertEquals("", stopWatch.getId());
|
||||
Assertions.assertEquals("1234567", StopWatch.create("1234567").getId());
|
||||
}
|
||||
|
||||
@Test
|
||||
void start() throws InterruptedException {
|
||||
StopWatch stopWatch = new StopWatch().start("test");
|
||||
stopWatch.setKeepTaskList(true);
|
||||
Assertions.assertEquals("test", stopWatch.currentTaskName());
|
||||
Assertions.assertTrue(stopWatch.isRunning());
|
||||
Thread.sleep(10);
|
||||
stopWatch.stop();
|
||||
|
||||
Assertions.assertTrue(stopWatch.getLastTaskTimeMillis() > 0);
|
||||
Assertions.assertTrue(stopWatch.getLastTaskTimeNanos() > 0);
|
||||
Assertions.assertTrue(stopWatch.getTotalTimeSeconds() > 0);
|
||||
Assertions.assertTrue(stopWatch.getTotalTimeMillis() > 0);
|
||||
Assertions.assertNotEquals(0, stopWatch.getTotalTimeNanos());
|
||||
Assertions.assertTrue(stopWatch.getTotal(TimeUnit.MILLISECONDS) > 0);
|
||||
|
||||
Assertions.assertTrue(stopWatch.getTaskCount() > 0);
|
||||
|
||||
System.out.println();
|
||||
System.out.println();
|
||||
Assertions.assertEquals("test", stopWatch.getLastTaskName());
|
||||
Assertions.assertNotNull(stopWatch.getLastTaskInfo());
|
||||
}
|
||||
|
||||
@Test
|
||||
void start2() {
|
||||
StopWatch stopWatch = new StopWatch().start();
|
||||
stopWatch.setKeepTaskList(false);
|
||||
Assertions.assertEquals("", stopWatch.currentTaskName());
|
||||
stopWatch.stop();
|
||||
}
|
||||
|
||||
@Test
|
||||
void start3() {
|
||||
StopWatch stopWatch = new StopWatch("11", false).start();
|
||||
stopWatch.setKeepTaskList(true);
|
||||
Assertions.assertEquals("", stopWatch.currentTaskName());
|
||||
Assertions.assertEquals("11", stopWatch.getId());
|
||||
try {
|
||||
Thread.sleep(10);
|
||||
} catch (InterruptedException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
stopWatch.stop();
|
||||
|
||||
Assertions.assertNotNull(stopWatch.getTaskInfo());
|
||||
Assertions.assertEquals(1, stopWatch.getTaskInfo().length);
|
||||
Assertions.assertNotEquals(0, stopWatch.getTaskInfo()[0].getTimeSeconds());
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,99 @@
|
|||
package com.czcb.scfs.spring.boot.starter;
|
||||
|
||||
import com.czcb.scfs.api.core.cipher.StoreType;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import static net.javacrumbs.jsonunit.assertj.JsonAssertions.assertThatJson;
|
||||
|
||||
class ScfsApiGatewayPropertiesTest {
|
||||
|
||||
@Test
|
||||
void getOnline() {
|
||||
ScfsApiGatewayProperties properties = new ScfsApiGatewayProperties();
|
||||
properties.setOnline(false);
|
||||
Assertions.assertFalse(properties.getOnline());
|
||||
}
|
||||
|
||||
@Test
|
||||
void getHost() {
|
||||
ScfsApiGatewayProperties properties = new ScfsApiGatewayProperties();
|
||||
properties.setOnline(true);
|
||||
Assertions.assertTrue(properties.getOnline());
|
||||
|
||||
properties.setHost("http://10.129.135.192:7100/");
|
||||
Assertions.assertEquals("http://10.129.135.192:7100/", properties.getHost());
|
||||
}
|
||||
|
||||
@Test
|
||||
void setChannel() {
|
||||
ScfsApiGatewayProperties.Channel channel = new ScfsApiGatewayProperties.Channel();
|
||||
channel.setChannelNo("000000");
|
||||
channel.setAppNo("111111");
|
||||
ScfsApiGatewayProperties properties = new ScfsApiGatewayProperties();
|
||||
properties.setChannel(channel);
|
||||
|
||||
Assertions.assertNotNull(properties.getChannel());
|
||||
assertThatJson("{\"channelNo\":\"000000\",\"appNo\":\"111111\"}").isEqualTo(properties.getChannel());
|
||||
}
|
||||
|
||||
@Test
|
||||
void getHttpclient() {
|
||||
ScfsApiGatewayProperties.Httpclient httpclient = new ScfsApiGatewayProperties.Httpclient();
|
||||
Map<String, String> headers = new HashMap<>();
|
||||
headers.put("aa", "123456");
|
||||
httpclient.setHeaders(headers);
|
||||
|
||||
ScfsApiGatewayProperties.Httpclient.Proxy proxy = new ScfsApiGatewayProperties.Httpclient.Proxy();
|
||||
proxy.setEnabled(true);
|
||||
proxy.setHost("128.0.0.1");
|
||||
proxy.setPort("1000");
|
||||
proxy.setUsername("name");
|
||||
proxy.setPassword("123456");
|
||||
proxy.setScheme("https");
|
||||
httpclient.setProxy(proxy);
|
||||
|
||||
ScfsApiGatewayProperties.Httpclient.ConnPool connPool = new ScfsApiGatewayProperties.Httpclient.ConnPool();
|
||||
connPool.setMaxRequests(100);
|
||||
connPool.setMaxRequestPerHost(100);
|
||||
connPool.setConnectTimeout(100);
|
||||
connPool.setSocketTimeout(100);
|
||||
httpclient.setConnPool(connPool);
|
||||
|
||||
httpclient.setCompressionEnabled(false);
|
||||
httpclient.setLogEnabled(false);
|
||||
ScfsApiGatewayProperties properties = new ScfsApiGatewayProperties();
|
||||
properties.setHttpclient(httpclient);
|
||||
|
||||
Assertions.assertNotNull(properties.getHttpclient());
|
||||
Assertions.assertFalse(properties.getHttpclient().getCompressionEnabled());
|
||||
Assertions.assertFalse(properties.getHttpclient().getLogEnabled());
|
||||
|
||||
assertThatJson("{\"logEnabled\":false,\"compressionEnabled\":false," +
|
||||
"\"connPool\":{\"maxRequests\":100,\"maxRequestPerHost\":100,\"connectTimeout\":100,\"socketTimeout\":100}," +
|
||||
"\"proxy\":{\"enabled\":true,\"host\":\"128.0.0.1\",\"port\":\"1000\",\"username\":\"name\",\"password\":\"123456\",\"scheme\":\"https\"}," +
|
||||
"\"headers\":{\"aa\":\"123456\"}}")
|
||||
.isEqualTo(properties.getHttpclient());
|
||||
}
|
||||
|
||||
@Test
|
||||
void getCipher() {
|
||||
ScfsApiGatewayProperties.Cipher cipher = new ScfsApiGatewayProperties.Cipher();
|
||||
cipher.setStoreType(StoreType.RESOURCES);
|
||||
cipher.setChannelPrivateKey("/home/key.pem");
|
||||
cipher.setChannelCertificate("/home/c1.pem");
|
||||
cipher.setCertificate("/home/c2.pem");
|
||||
|
||||
ScfsApiGatewayProperties properties = new ScfsApiGatewayProperties();
|
||||
properties.setCipher(cipher);
|
||||
|
||||
Assertions.assertNotNull(properties.getCipher());
|
||||
|
||||
assertThatJson("{\"storeType\":\"RESOURCES\",\"channelPrivateKey\":\"/home/key.pem\",\"channelCertificate\":\"/home/c1.pem\",\"certificate\":\"/home/c2.pem\"}")
|
||||
.isEqualTo(properties.getCipher());
|
||||
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,85 @@
|
|||
package com.czcb.scfs.spring.boot.starter;
|
||||
|
||||
import com.czcb.scfs.api.core.cipher.StoreType;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.Mockito;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
class SmConfigurationTest {
|
||||
|
||||
@Mock
|
||||
private SmConfiguration smConfiguration;
|
||||
|
||||
private ScfsApiGatewayProperties properties() {
|
||||
ScfsApiGatewayProperties properties = new ScfsApiGatewayProperties();
|
||||
|
||||
ScfsApiGatewayProperties.Httpclient httpclient = new ScfsApiGatewayProperties.Httpclient();
|
||||
Map<String, String> headers = new HashMap<>();
|
||||
headers.put("aa", "123456");
|
||||
httpclient.setHeaders(headers);
|
||||
|
||||
ScfsApiGatewayProperties.Httpclient.Proxy proxy = new ScfsApiGatewayProperties.Httpclient.Proxy();
|
||||
proxy.setEnabled(true);
|
||||
proxy.setHost("128.0.0.1");
|
||||
proxy.setPort("1000");
|
||||
proxy.setUsername("name");
|
||||
proxy.setPassword("123456");
|
||||
proxy.setScheme("https");
|
||||
httpclient.setProxy(proxy);
|
||||
|
||||
ScfsApiGatewayProperties.Httpclient.ConnPool connPool = new ScfsApiGatewayProperties.Httpclient.ConnPool();
|
||||
connPool.setMaxRequests(100);
|
||||
connPool.setMaxRequestPerHost(100);
|
||||
connPool.setConnectTimeout(100);
|
||||
connPool.setSocketTimeout(100);
|
||||
httpclient.setConnPool(connPool);
|
||||
httpclient.setCompressionEnabled(false);
|
||||
httpclient.setLogEnabled(false);
|
||||
properties.setHttpclient(httpclient);
|
||||
|
||||
ScfsApiGatewayProperties.Channel channel = new ScfsApiGatewayProperties.Channel();
|
||||
channel.setChannelNo("000000");
|
||||
channel.setAppNo("111111");
|
||||
properties.setChannel(channel);
|
||||
|
||||
ScfsApiGatewayProperties.Cipher cipher = new ScfsApiGatewayProperties.Cipher();
|
||||
cipher.setStoreType(StoreType.RESOURCES);
|
||||
cipher.setChannelPrivateKey("/home/key.pem");
|
||||
cipher.setChannelCertificate("/home/c1.pem");
|
||||
cipher.setCertificate("/home/c2.pem");
|
||||
properties.setCipher(cipher);
|
||||
|
||||
properties.setOnline(true);
|
||||
properties.setHost("http://127.0.0.1");
|
||||
return properties;
|
||||
}
|
||||
|
||||
@Test
|
||||
void getProperties() {
|
||||
Mockito.when(smConfiguration.getProperties()).thenReturn(properties());
|
||||
ScfsApiGatewayProperties properties = smConfiguration.getProperties();
|
||||
Assertions.assertNotNull(properties);
|
||||
}
|
||||
|
||||
// @Test
|
||||
// void smProfile() {
|
||||
// Mockito.when(smConfiguration.getProperties()).thenReturn(properties());
|
||||
// Mockito.when(smConfiguration.smProfile()).thenReturn(new SmConfiguration().smProfile());
|
||||
// Mockito.when(smConfiguration.getChannel()).thenReturn(new SmConfiguration().getChannel());
|
||||
// SmProfile smProfile = smConfiguration.smProfile();
|
||||
// Assertions.assertNotNull(smProfile);
|
||||
// assertThatJson("{\"channelNo\":\"000000\",\"appNo\":\"111111\"}").isEqualTo(smProfile.getChannel());
|
||||
// assertThatJson("{\"logEnabled\":false,\"compressionEnabled\":false," +
|
||||
// "\"connPool\":{\"maxRequests\":100,\"maxRequestPerHost\":100,\"connectTimeout\":100,\"socketTimeout\":100}," +
|
||||
// "\"proxy\":{\"enabled\":true,\"host\":\"128.0.0.1\",\"port\":\"1000\",\"username\":\"name\",\"password\":\"123456\",\"scheme\":\"https\"}," +
|
||||
// "\"headers\":{\"aa\":\"123456\"}}")
|
||||
// .isEqualTo(smProfile.getHttpProfile());
|
||||
// }
|
||||
}
|
||||
Loading…
Reference in New Issue