jacoco 单元测试覆盖
parent
eb3060a379
commit
8d843dc84c
68
pom.xml
68
pom.xml
|
|
@ -101,39 +101,36 @@
|
|||
<artifactId>sonar-maven-plugin</artifactId>
|
||||
<version>3.10.0.2594</version>
|
||||
</plugin>
|
||||
<!-- <plugin>-->
|
||||
<!-- <groupId>org.jacoco</groupId>-->
|
||||
<!-- <artifactId>jacoco-maven-plugin</artifactId>-->
|
||||
<!-- <version>0.8.11</version>-->
|
||||
<!-- <configuration>-->
|
||||
<!-- <destFile>${project.build.directory}/jacoco.exec</destFile>-->
|
||||
<!-- <dataFile>${project.build.directory}/jacoco.exec</dataFile>-->
|
||||
<!-- </configuration>-->
|
||||
<!-- <executions>-->
|
||||
<!-- <execution>-->
|
||||
<!-- <goals>-->
|
||||
<!-- <goal>prepare-agent</goal>-->
|
||||
<!-- </goals>-->
|
||||
<!-- </execution>-->
|
||||
<!-- <execution>-->
|
||||
<!-- <id>report</id>-->
|
||||
<!-- <phase>prepare-package</phase>-->
|
||||
<!-- <goals>-->
|
||||
<!-- <goal>report</goal>-->
|
||||
<!-- </goals>-->
|
||||
<!-- </execution>-->
|
||||
<!-- <execution>-->
|
||||
<!-- <id>post-unit-test</id>-->
|
||||
<!-- <phase>test</phase>-->
|
||||
<!-- <goals>-->
|
||||
<!-- <goal>report</goal>-->
|
||||
<!-- </goals>-->
|
||||
<!-- <configuration>-->
|
||||
<!-- <dataFile>target/jacoco.exec</dataFile>-->
|
||||
<!-- </configuration>-->
|
||||
<!-- </execution>-->
|
||||
<!-- </executions>-->
|
||||
<!-- </plugin>-->
|
||||
<plugin>
|
||||
<groupId>org.jacoco</groupId>
|
||||
<artifactId>jacoco-maven-plugin</artifactId>
|
||||
<version>0.8.11</version>
|
||||
<configuration>
|
||||
<destFile>${project.build.directory}/jacoco.exec</destFile>
|
||||
<dataFile>${project.build.directory}/jacoco.exec</dataFile>
|
||||
</configuration>
|
||||
<executions>
|
||||
<execution>
|
||||
<goals>
|
||||
<goal>prepare-agent</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
<execution>
|
||||
<id>report</id>
|
||||
<phase>prepare-package</phase>
|
||||
<goals>
|
||||
<goal>report</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
<execution>
|
||||
<id>post-unit-test</id>
|
||||
<phase>test</phase>
|
||||
<goals>
|
||||
<goal>report</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-jar-plugin</artifactId>
|
||||
|
|
@ -154,6 +151,11 @@
|
|||
<generateBackupPoms>false</generateBackupPoms>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
<version>3.2.3</version>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
sh mvn clean package verify sonar:sonar \
|
||||
mvn clean package verify sonar:sonar \
|
||||
-Dsonar.projectKey=scfs-api-sdk \
|
||||
-Dsonar.projectName=scfs-api-sdk \
|
||||
-Dsonar.host.url=http://10.129.135.192:7100 \
|
||||
|
|
|
|||
|
|
@ -1,10 +1,9 @@
|
|||
package com.czcb.scfs.api.core.cipher;
|
||||
|
||||
import com.czcb.scfs.api.core.util.PemFile;
|
||||
import com.czcb.scfs.api.core.KeyText;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.net.URL;
|
||||
import java.security.cert.X509Certificate;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
|
@ -14,10 +13,7 @@ class LocalCertificateProviderTest {
|
|||
@Test
|
||||
void getCertificate() {
|
||||
String xx = "38844645436081632637265568516991627875287655057";
|
||||
|
||||
URL url = getClass().getClassLoader().getResource("rsa_channel_certificate.pem");
|
||||
Assertions.assertNotNull(url);
|
||||
X509Certificate certificate = PemFile.loadX509FromAbsolutePath(url.getPath());
|
||||
X509Certificate certificate = KeyText.loadTestRSA();
|
||||
|
||||
List<X509Certificate> list = new ArrayList<>();
|
||||
list.add(certificate);
|
||||
|
|
@ -27,9 +23,7 @@ class LocalCertificateProviderTest {
|
|||
|
||||
@Test
|
||||
void getAvailableCertificate() {
|
||||
URL url = getClass().getClassLoader().getResource("rsa_channel_certificate.pem");
|
||||
Assertions.assertNotNull(url);
|
||||
X509Certificate certificate = PemFile.loadX509FromAbsolutePath(url.getPath());
|
||||
X509Certificate certificate = KeyText.loadTestRSA();
|
||||
|
||||
List<X509Certificate> list = new ArrayList<>();
|
||||
list.add(certificate);
|
||||
|
|
|
|||
|
|
@ -6,7 +6,6 @@ import com.czcb.scfs.api.core.exception.TimestampException;
|
|||
import com.czcb.scfs.api.core.http.*;
|
||||
import com.czcb.scfs.api.core.util.DateTimes;
|
||||
import com.czcb.scfs.api.core.util.Nonce;
|
||||
import com.czcb.scfs.api.core.util.PemFile;
|
||||
import com.czcb.scfs.api.core.util.Strings;
|
||||
import com.google.gson.Gson;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
|
|
@ -39,11 +38,8 @@ class ApacheHttpclientProxyTest {
|
|||
}
|
||||
|
||||
private Profile profile() {
|
||||
URL url = ClassLoader.getSystemResource("rsa_channel_private_key.pem");
|
||||
PrivateKey privateKey = PemFile.loadPrivateKeyFromAbsolutePath(url.getPath());
|
||||
|
||||
URL certificatesurl = ClassLoader.getSystemResource("rsa_channel_certificate.pem");
|
||||
X509Certificate certificate = PemFile.loadX509FromAbsolutePath(certificatesurl.getPath());
|
||||
PrivateKey privateKey = KeyText.loadTestPrivateKeyRSA();
|
||||
X509Certificate certificate = KeyText.loadTestRSA();
|
||||
List<X509Certificate> list = new ArrayList<>();
|
||||
list.add(certificate);
|
||||
CertificateProvider certificateProvider = new LocalCertificateProvider(list);
|
||||
|
|
|
|||
|
|
@ -1,157 +0,0 @@
|
|||
package com.czcb.scfs.api.core.http.client;
|
||||
|
||||
import com.czcb.scfs.api.core.*;
|
||||
import com.czcb.scfs.api.core.cipher.*;
|
||||
import com.czcb.scfs.api.core.exception.DecryptException;
|
||||
import com.czcb.scfs.api.core.http.*;
|
||||
import com.czcb.scfs.api.core.util.DateTimes;
|
||||
import com.czcb.scfs.api.core.util.Nonce;
|
||||
import com.czcb.scfs.api.core.util.Strings;
|
||||
import com.google.gson.Gson;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.mockserver.client.MockServerClient;
|
||||
import org.mockserver.junit.jupiter.MockServerExtension;
|
||||
import org.mockserver.junit.jupiter.MockServerSettings;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.security.PrivateKey;
|
||||
import java.security.cert.X509Certificate;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import static com.czcb.scfs.api.core.Constants.*;
|
||||
import static net.javacrumbs.jsonunit.assertj.JsonAssertions.assertThatJson;
|
||||
import static org.mockserver.model.HttpRequest.request;
|
||||
import static org.mockserver.model.HttpResponse.response;
|
||||
|
||||
@ExtendWith(MockServerExtension.class)
|
||||
@MockServerSettings(ports = {8888})
|
||||
class ApacheHttpclientTestv2 {
|
||||
private MockServerClient client;
|
||||
|
||||
@BeforeEach
|
||||
public void beforeEachLifecyleMethod(MockServerClient client) {
|
||||
this.client = client;
|
||||
}
|
||||
|
||||
private Profile profile() {
|
||||
PrivateKey privateKey = KeyText.loadTestPrivateKeyRSA();
|
||||
X509Certificate certificate = KeyText.loadTestRSA();
|
||||
List<X509Certificate> list = new ArrayList<>();
|
||||
list.add(certificate);
|
||||
CertificateProvider certificateProvider = new LocalCertificateProvider(list);
|
||||
Privacy privacy = new TestPrivacy(privateKey, certificateProvider);
|
||||
Signature signature = new DefaultSignature(certificateProvider, new TestSigner(privateKey), new TestVerifier(certificateProvider));
|
||||
|
||||
return new TestProfile(
|
||||
privacy, signature, new DefaultChannel.Builder()
|
||||
.channelNo("0000")
|
||||
.appNo("100000")
|
||||
.build(), new DefaultHttpProfile.Builder()
|
||||
.online(false)
|
||||
.logEnabled(true)
|
||||
.compressionEnabled(false)
|
||||
.host("http://127.0.0.1:8888")
|
||||
.build()
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
void doRemoteExecuteResponseBodyIsNull() {
|
||||
ApiClient apiClient = new DefaultApiClientBuilder()
|
||||
.profile(profile())
|
||||
.build();
|
||||
|
||||
// 对称密钥
|
||||
byte[] secret = apiClient.getProfile().getPrivacy().getSecretCipher().getSecretKey();
|
||||
// 加密响应报文
|
||||
String responseBody = null;
|
||||
|
||||
// 加密对称密钥
|
||||
String secretKey = apiClient.getProfile().getPrivacy().getEncryptor().encrypt(Strings.toStr(secret));
|
||||
|
||||
org.mockserver.model.HttpResponse mock = response()
|
||||
.withBody(responseBody)
|
||||
.withHeader(NONCE, Nonce.ofNonce())
|
||||
.withHeader(SECRET_KEY, secretKey)
|
||||
.withHeader(REQUEST_ID, Nonce.ofNonce())
|
||||
.withHeader(TIMESTAMP, DateTimes.ofTimestamp());
|
||||
|
||||
String buildAuth = NONCE + "=" + mock.getHeader(NONCE).get(0) + "," +
|
||||
TIMESTAMP + "=" + mock.getHeader(TIMESTAMP).get(0) + "," +
|
||||
SECRET_KEY + "=" + secretKey;
|
||||
|
||||
String message = buildAuth + "\n" + "\n";
|
||||
mock.withHeader(SIGNATURE, apiClient.getProfile().getSignature().getSigner().sign(message).getResult());
|
||||
|
||||
client.when(request()
|
||||
.withMethod(HttpMethod.POST.getUpperName())
|
||||
.withPath("/mock/responseBodyIsNull")
|
||||
).respond(mock);
|
||||
|
||||
TestRequest request = new TestRequest();
|
||||
RequestBody requestBody = new JsonRequestBody.Builder()
|
||||
.body(new Gson().toJson(request))
|
||||
.build();
|
||||
|
||||
Assertions.assertThrows(DecryptException.class, () -> apiClient.post("/mock/responseBodyIsNull", new HttpHeaders(), requestBody, TestResponse.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
void doRemoteExecuteGet() {
|
||||
TestResponse mockResponse = new TestResponse();
|
||||
mockResponse.setName("123456");
|
||||
ApiClient apiClient = new DefaultApiClientBuilder()
|
||||
.profile(profile())
|
||||
.build();
|
||||
|
||||
// 对称密钥
|
||||
byte[] secret = apiClient.getProfile().getPrivacy().getSecretCipher().getSecretKey();
|
||||
// 加密响应报文
|
||||
String responseBody = apiClient.getProfile().getPrivacy().getSecretCipher().encrypt(secret, mockResponse.toJsonResponse().getBody().getBytes(StandardCharsets.UTF_8));
|
||||
|
||||
// 加密对称密钥
|
||||
String secretKey = apiClient.getProfile().getPrivacy().getEncryptor().encrypt(Strings.toStr(secret));
|
||||
|
||||
org.mockserver.model.HttpResponse mock = response()
|
||||
.withBody(responseBody)
|
||||
.withHeader(NONCE, Nonce.ofNonce())
|
||||
.withHeader(SECRET_KEY, secretKey)
|
||||
.withHeader(REQUEST_ID, Nonce.ofNonce())
|
||||
.withHeader(TIMESTAMP, DateTimes.ofTimestamp());
|
||||
|
||||
String buildAuth = NONCE + "=" + mock.getHeader(NONCE).get(0) + "," +
|
||||
TIMESTAMP + "=" + mock.getHeader(TIMESTAMP).get(0) + "," +
|
||||
SECRET_KEY + "=" + secretKey;
|
||||
|
||||
String message = buildAuth + "\n" + responseBody + "\n";
|
||||
mock.withHeader(SIGNATURE, apiClient.getProfile().getSignature().getSigner().sign(message).getResult());
|
||||
|
||||
client.when(request()
|
||||
.withMethod(HttpMethod.GET.getUpperName())
|
||||
.withPath("/mock/get")
|
||||
).respond(mock);
|
||||
|
||||
HttpResponse<TestResponse> response = apiClient.get("/mock/get", null, TestResponse.class);
|
||||
|
||||
assertThatJson("{\"name\":\"123456\"}").isEqualTo(response.getServiceResponse().toJsonResponse().getBody());
|
||||
}
|
||||
|
||||
public static class TestRequest implements ApiRequest {
|
||||
}
|
||||
|
||||
public static class TestResponse implements ApiResponse {
|
||||
private String name;
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,158 +0,0 @@
|
|||
package com.czcb.scfs.api.core.http.client;
|
||||
|
||||
import com.czcb.scfs.api.core.*;
|
||||
import com.czcb.scfs.api.core.cipher.*;
|
||||
import com.czcb.scfs.api.core.exception.ApiClientException;
|
||||
import com.czcb.scfs.api.core.exception.TimestampException;
|
||||
import com.czcb.scfs.api.core.http.*;
|
||||
import com.czcb.scfs.api.core.util.DateTimes;
|
||||
import com.czcb.scfs.api.core.util.Nonce;
|
||||
import com.czcb.scfs.api.core.util.Strings;
|
||||
import com.google.gson.Gson;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.mockserver.client.MockServerClient;
|
||||
import org.mockserver.junit.jupiter.MockServerExtension;
|
||||
import org.mockserver.junit.jupiter.MockServerSettings;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.security.PrivateKey;
|
||||
import java.security.cert.X509Certificate;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import static com.czcb.scfs.api.core.Constants.*;
|
||||
import static org.mockserver.model.HttpRequest.request;
|
||||
import static org.mockserver.model.HttpResponse.response;
|
||||
|
||||
@ExtendWith(MockServerExtension.class)
|
||||
@MockServerSettings(ports = {8888})
|
||||
class ApacheHttpclientTestv3 {
|
||||
private MockServerClient client;
|
||||
|
||||
@BeforeEach
|
||||
public void beforeEachLifecyleMethod(MockServerClient client) {
|
||||
this.client = client;
|
||||
}
|
||||
|
||||
private Profile profile() {
|
||||
PrivateKey privateKey = KeyText.loadTestPrivateKeyRSA();
|
||||
X509Certificate certificate = KeyText.loadTestRSA();
|
||||
List<X509Certificate> list = new ArrayList<>();
|
||||
list.add(certificate);
|
||||
CertificateProvider certificateProvider = new LocalCertificateProvider(list);
|
||||
Privacy privacy = new TestPrivacy(privateKey, certificateProvider);
|
||||
Signature signature = new DefaultSignature(certificateProvider, new TestSigner(privateKey), new TestVerifier(certificateProvider));
|
||||
|
||||
return new TestProfile(
|
||||
privacy, signature, new DefaultChannel.Builder()
|
||||
.channelNo("0000")
|
||||
.appNo("100000")
|
||||
.build(), new DefaultHttpProfile.Builder()
|
||||
.online(false)
|
||||
.logEnabled(true)
|
||||
.compressionEnabled(false)
|
||||
.host("http://127.0.0.1:8888")
|
||||
.build()
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
void doRemoteExecuteTimestamp() {
|
||||
ApiClient apiClient = new DefaultApiClientBuilder()
|
||||
.profile(profile())
|
||||
.build();
|
||||
|
||||
// 对称密钥
|
||||
byte[] secret = apiClient.getProfile().getPrivacy().getSecretCipher().getSecretKey();
|
||||
// 加密响应报文
|
||||
String responseBody = apiClient.getProfile().getPrivacy().getSecretCipher().encrypt(secret, "{\"a\":\"123\"}".getBytes(StandardCharsets.UTF_8));
|
||||
|
||||
// 加密对称密钥
|
||||
String secretKey = apiClient.getProfile().getPrivacy().getEncryptor().encrypt(Strings.toStr(secret));
|
||||
|
||||
org.mockserver.model.HttpResponse mock = response()
|
||||
.withBody(responseBody)
|
||||
.withHeader(NONCE, Nonce.ofNonce())
|
||||
.withHeader(SECRET_KEY, secretKey)
|
||||
.withHeader(REQUEST_ID, Nonce.ofNonce())
|
||||
.withHeader(TIMESTAMP, DateTimes.ofTimestamp() + 800);
|
||||
|
||||
String buildAuth = NONCE + "=" + mock.getHeader(NONCE).get(0) + "," +
|
||||
TIMESTAMP + "=" + mock.getHeader(TIMESTAMP).get(0) + "," +
|
||||
SECRET_KEY + "=" + secretKey;
|
||||
|
||||
String message = buildAuth + "\n" + "\n";
|
||||
mock.withHeader(SIGNATURE, apiClient.getProfile().getSignature().getSigner().sign(message).getResult());
|
||||
|
||||
client.when(request()
|
||||
.withMethod(HttpMethod.POST.getUpperName())
|
||||
.withPath("/mock/timestamp")
|
||||
).respond(mock);
|
||||
|
||||
TestRequest request = new TestRequest();
|
||||
RequestBody requestBody = new JsonRequestBody.Builder()
|
||||
.body(new Gson().toJson(request))
|
||||
.build();
|
||||
|
||||
Assertions.assertThrows(TimestampException.class, () -> apiClient.post("/mock/timestamp", new HttpHeaders(), requestBody, TestResponse.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
void doRemoteExecuteJsonException() {
|
||||
ApiClient apiClient = new DefaultApiClientBuilder()
|
||||
.profile(profile())
|
||||
.build();
|
||||
|
||||
// 对称密钥
|
||||
byte[] secret = apiClient.getProfile().getPrivacy().getSecretCipher().getSecretKey();
|
||||
// 加密响应报文
|
||||
String responseBody = apiClient.getProfile().getPrivacy().getSecretCipher().encrypt(secret, "{\"aa".getBytes(StandardCharsets.UTF_8));
|
||||
|
||||
// 加密对称密钥
|
||||
String secretKey = apiClient.getProfile().getPrivacy().getEncryptor().encrypt(Strings.toStr(secret));
|
||||
|
||||
org.mockserver.model.HttpResponse mock = response()
|
||||
.withBody(responseBody)
|
||||
.withHeader(NONCE, Nonce.ofNonce())
|
||||
.withHeader(SECRET_KEY, secretKey)
|
||||
.withHeader(REQUEST_ID, Nonce.ofNonce())
|
||||
.withHeader(TIMESTAMP, DateTimes.ofTimestamp());
|
||||
|
||||
String buildAuth = NONCE + "=" + mock.getHeader(NONCE).get(0) + "," +
|
||||
TIMESTAMP + "=" + mock.getHeader(TIMESTAMP).get(0) + "," +
|
||||
SECRET_KEY + "=" + secretKey;
|
||||
|
||||
String message = buildAuth + "\n" + responseBody + "\n";
|
||||
mock.withHeader(SIGNATURE, apiClient.getProfile().getSignature().getSigner().sign(message).getResult());
|
||||
|
||||
client.when(request()
|
||||
.withMethod(HttpMethod.POST.getUpperName())
|
||||
.withPath("/mock/json")
|
||||
).respond(mock);
|
||||
|
||||
TestRequest request = new TestRequest();
|
||||
RequestBody requestBody = new JsonRequestBody.Builder()
|
||||
.body(new Gson().toJson(request))
|
||||
.build();
|
||||
|
||||
Assertions.assertThrows(ApiClientException.class, () -> apiClient.post("/mock/json", new HttpHeaders(), requestBody, TestResponse.class));
|
||||
}
|
||||
|
||||
public static class TestRequest implements ApiRequest {
|
||||
}
|
||||
|
||||
public static class TestResponse implements ApiResponse {
|
||||
private String name;
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -40,11 +40,8 @@ class ApacheHttpclientV2Test {
|
|||
}
|
||||
|
||||
private Profile profile() {
|
||||
URL url = ClassLoader.getSystemResource("rsa_channel_private_key.pem");
|
||||
PrivateKey privateKey = PemFile.loadPrivateKeyFromAbsolutePath(url.getPath());
|
||||
|
||||
URL certificatesurl = ClassLoader.getSystemResource("rsa_channel_certificate.pem");
|
||||
X509Certificate certificate = PemFile.loadX509FromAbsolutePath(certificatesurl.getPath());
|
||||
PrivateKey privateKey = KeyText.loadTestPrivateKeyRSA();
|
||||
X509Certificate certificate = KeyText.loadTestRSA();
|
||||
List<X509Certificate> list = new ArrayList<>();
|
||||
list.add(certificate);
|
||||
CertificateProvider certificateProvider = new LocalCertificateProvider(list);
|
||||
|
|
|
|||
|
|
@ -40,11 +40,8 @@ class ApacheHttpclientV3Test {
|
|||
}
|
||||
|
||||
private Profile profile() {
|
||||
URL url = ClassLoader.getSystemResource("rsa_channel_private_key.pem");
|
||||
PrivateKey privateKey = PemFile.loadPrivateKeyFromAbsolutePath(url.getPath());
|
||||
|
||||
URL certificatesurl = ClassLoader.getSystemResource("rsa_channel_certificate.pem");
|
||||
X509Certificate certificate = PemFile.loadX509FromAbsolutePath(certificatesurl.getPath());
|
||||
PrivateKey privateKey = KeyText.loadTestPrivateKeyRSA();
|
||||
X509Certificate certificate = KeyText.loadTestRSA();
|
||||
List<X509Certificate> list = new ArrayList<>();
|
||||
list.add(certificate);
|
||||
CertificateProvider certificateProvider = new LocalCertificateProvider(list);
|
||||
|
|
|
|||
|
|
@ -1,77 +1,58 @@
|
|||
package com.czcb.scfs.api.core.util;
|
||||
|
||||
import com.czcb.scfs.api.core.KeyText;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.net.URL;
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.security.PrivateKey;
|
||||
|
||||
class PemFileTest {
|
||||
|
||||
private final static String PRIVATE_KEY = "-----BEGIN PRIVATE KEY-----\n" +
|
||||
"MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQDDwbOmleN4X6YD\n" +
|
||||
"iTUk5tuFhhqJYGIdSBb7x14E5OCd/2UIxx8l/tF162rAlPeYquHM34mi1Ha0Ana9\n" +
|
||||
"6ttMNMDMyq3TBUxq7n8H0pLU8fboCorU83owRj4Eqrfk7tG0cMgeUmrYbaTXPD5d\n" +
|
||||
"lRvKdpFxN1UNRav+mYUb1OPxoHuJRxoeRGAm/RWC1nTj9UYeAQQKehb5FhWD3UBL\n" +
|
||||
"Cxk0iXgPrfykkZDeLeUI9fA+dzEDnrHDmz+jt9fQnup3RVku7/n1DQQyCOUDOn24\n" +
|
||||
"NGfBXciDeivJ3/bZ1SJy5t/4Hu6GyqyJKqP3Oe9UAbkbZWOnIu186R6e7NBk5n/+\n" +
|
||||
"HgDf17CLAgMBAAECggEAOyoOoZEYxHdoJhZwlPwfKe57k7ypOr9gju1VObZxOt+H\n" +
|
||||
"Sk1gWSv4Y6Loy1batG4lO7XuE+QSElrSR5k1eYVSJjM1sRsfWwxFIXifvzN4U+5n\n" +
|
||||
"wm/qJK/+K4vrc4pIoDgcC6oON+Z/lTvUf5VC5oDKeYTnNFEz6Am/RMelsxA1diob\n" +
|
||||
"SNSwPTPijqy0b63IHSWAbB+zmurRZPOSzy5lZ2F+xdYhnF+j9XprP4iJiHydvrgH\n" +
|
||||
"RJbxwsiphjgPeqPSVSs5MTQ0zDumRkbMBANk/Y5rCGybBSGpW1BwXZjTp3jtY9Tr\n" +
|
||||
"DmXDbzsiZWx/D1wFd0F+BhS6vg3dUkX63mSDOaTvKQKBgQDpLcwdAkhvWbRs03ev\n" +
|
||||
"5/BgmgHO2iwJkAL6yyX9wox4NOYe1FInBht5Nc/ZC0mXy5E4Wmd1SGAoYt8R2vxo\n" +
|
||||
"LnIyJnRE4iUKWxL87okKCDW4b3AaMTMZXyKmkMgMPDvpk6Odag5Ey2xpojb8TP7q\n" +
|
||||
"QtZu5DWTkU2lR+2Z4qH8+DFC2QKBgQDW6k4guiR0ZP7Po1QLQWXwRfQU4WUlxaKp\n" +
|
||||
"MEp6hsdKRN2MPbbPqtg4PoBpc1Hpvx2SX3YGAaRIwJtLGuoOq4TnVJ+9SKI5VDqT\n" +
|
||||
"YcjE4UZm1DDmm1XYaCJKYeN2Swr2xl2VA6LJpHHZDLXC7YVshZ/53lJhPTbnBpIE\n" +
|
||||
"0T+MBCIoAwKBgAtSS+zEpZuycU4vXkpOKp+mGpxef3DfayWeI3QUtBlKCDpK+Moo\n" +
|
||||
"Q8E42aSiJaqXGa/ww8EwCGstkcMeLM8qIiVJNK/7guzjyyI4urKf/2u7DWhYJzD+\n" +
|
||||
"K4KG2Q+vBQGsR29nLNYuQk9GrwDFBEN/h0XPEsLddTWnLmQgWtkbe9jRAoGBAIAj\n" +
|
||||
"dW+37HJloTPl+v82//ORkeU9OlSaEND1lQKXB431n261gSEyiPaH9YJqGqXWqGgU\n" +
|
||||
"eMc+BVjHVhAivPkANR7EavWtr+Okn8QuIkmY06QN0hY8+aMs+30D/l4Sgjf+r99g\n" +
|
||||
"Kpg1bxftpxXRDqNUzLxrXQzTNb/rP/aVWGFAOalHAoGAPiW3cKvfTOLIS0PFxGOF\n" +
|
||||
"EKQzr/TDGnKJNKeGJ25VOKPnq8RDX/Ik2+hNjdThOWApIgDQo4JtUMCxCDM7lRcu\n" +
|
||||
"ne9B/UShSzd8uuZgHZVSFD+pfPJvyVpZ/ORHvrQqUAtveQgk7Cpp2tuvsixbBhdQ\n" +
|
||||
"E6ZWsef5uyYdyjLp2C4cl2A=\n" +
|
||||
"-----END PRIVATE KEY-----\n";
|
||||
|
||||
@Test
|
||||
void loadPrivateKeyFromString() {
|
||||
PrivateKey privateKey = PemFile.loadPrivateKeyFromString(PRIVATE_KEY);
|
||||
PrivateKey privateKey = PemFile.loadPrivateKeyFromString(KeyText.PRIVATE_TEXT_RSA);
|
||||
Assertions.assertNotNull(privateKey);
|
||||
Assertions.assertEquals("RSA", privateKey.getAlgorithm());
|
||||
}
|
||||
|
||||
@Test
|
||||
void loadPrivateKeyFromAbsolutePath() {
|
||||
URL url = getClass().getClassLoader().getResource("rsa_channel_private_key.pem");
|
||||
Assertions.assertNotNull(url);
|
||||
PrivateKey privateKey = PemFile.loadPrivateKeyFromAbsolutePath(url.getPath());
|
||||
Assertions.assertNotNull(privateKey);
|
||||
Assertions.assertEquals("RSA", privateKey.getAlgorithm());
|
||||
void loadPrivateKeyFromAbsolutePath() throws IOException {
|
||||
File file = File.createTempFile("Test.", "rsa_channel_private_key.pem");
|
||||
try (FileOutputStream outputStream = new FileOutputStream(file)) {
|
||||
outputStream.write(KeyText.PRIVATE_TEXT_RSA.getBytes(StandardCharsets.UTF_8));
|
||||
outputStream.flush();
|
||||
|
||||
String text = PemFile.readPrivateKeyStringFromPath(file.getPath());
|
||||
Assertions.assertNotNull(text);
|
||||
Assertions.assertEquals("RSA", PemFile.loadPrivateKeyFromString(text).getAlgorithm());
|
||||
} finally {
|
||||
file.delete();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void loadPrivateKeyFromStream() {
|
||||
InputStream stream = getClass().getClassLoader().getResourceAsStream("rsa_channel_private_key.pem");
|
||||
PrivateKey privateKey = PemFile.loadPrivateKeyFromStream(stream);
|
||||
PrivateKey privateKey = PemFile.loadPrivateKeyFromStream(new ByteArrayInputStream(KeyText.PRIVATE_TEXT_RSA.getBytes(StandardCharsets.UTF_8)));
|
||||
Assertions.assertNotNull(privateKey);
|
||||
Assertions.assertEquals("RSA", privateKey.getAlgorithm());
|
||||
|
||||
// Assertions.assertThrows(FileReadException.class, () -> {
|
||||
// ByteArrayInputStream arrayInputStream = new ByteArrayInputStream(PRIVATE_KEY.getBytes(StandardCharsets.UTF_8));
|
||||
// PemFile.loadPrivateKeyFromStream(arrayInputStream);
|
||||
// });
|
||||
}
|
||||
|
||||
@Test
|
||||
void readPrivateKeyStringFromPath() {
|
||||
URL url = getClass().getClassLoader().getResource("rsa_channel_private_key.pem");
|
||||
Assertions.assertNotNull(url);
|
||||
String privateKey = PemFile.readPrivateKeyStringFromPath(url.getPath());
|
||||
Assertions.assertNotNull(privateKey);
|
||||
void readPrivateKeyStringFromPath() throws IOException {
|
||||
File file = File.createTempFile("Test.", "rsa_channel_private_key.pem");
|
||||
try (FileOutputStream outputStream = new FileOutputStream(file)) {
|
||||
outputStream.write(KeyText.PRIVATE_TEXT_RSA.getBytes(StandardCharsets.UTF_8));
|
||||
outputStream.flush();
|
||||
|
||||
String privateKey = PemFile.readPrivateKeyStringFromPath(file.getPath());
|
||||
Assertions.assertNotNull(PemFile.loadPrivateKeyFromString(privateKey));
|
||||
} finally {
|
||||
file.delete();
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue