From 8d843dc84c221074ecb76fd3a4236693b002ec01 Mon Sep 17 00:00:00 2001 From: 13009 Date: Tue, 20 Feb 2024 17:31:38 +0800 Subject: [PATCH] =?UTF-8?q?jacoco=20=E5=8D=95=E5=85=83=E6=B5=8B=E8=AF=95?= =?UTF-8?q?=E8=A6=86=E7=9B=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 68 ++++---- scanner.sh | 2 +- .../cipher/LocalCertificateProviderTest.java | 12 +- .../client/ApacheHttpclientProxyTest.java | 8 +- .../http/client/ApacheHttpclientTestv2.java | 157 ----------------- .../http/client/ApacheHttpclientTestv3.java | 158 ------------------ .../http/client/ApacheHttpclientV2Test.java | 7 +- .../http/client/ApacheHttpclientV3Test.java | 7 +- .../czcb/scfs/api/core/util/PemFileTest.java | 81 ++++----- 9 files changed, 76 insertions(+), 424 deletions(-) delete mode 100644 scfs-api-core/src/test/java/com/czcb/scfs/api/core/http/client/ApacheHttpclientTestv2.java delete mode 100644 scfs-api-core/src/test/java/com/czcb/scfs/api/core/http/client/ApacheHttpclientTestv3.java diff --git a/pom.xml b/pom.xml index a357bd1..745896b 100644 --- a/pom.xml +++ b/pom.xml @@ -101,39 +101,36 @@ sonar-maven-plugin 3.10.0.2594 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + org.jacoco + jacoco-maven-plugin + 0.8.11 + + ${project.build.directory}/jacoco.exec + ${project.build.directory}/jacoco.exec + + + + + prepare-agent + + + + report + prepare-package + + report + + + + post-unit-test + test + + report + + + + org.apache.maven.plugins maven-jar-plugin @@ -154,6 +151,11 @@ false + + org.apache.maven.plugins + maven-surefire-plugin + 3.2.3 + diff --git a/scanner.sh b/scanner.sh index a54ca98..188ed48 100644 --- a/scanner.sh +++ b/scanner.sh @@ -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 \ diff --git a/scfs-api-core/src/test/java/com/czcb/scfs/api/core/cipher/LocalCertificateProviderTest.java b/scfs-api-core/src/test/java/com/czcb/scfs/api/core/cipher/LocalCertificateProviderTest.java index 27eb833..907fe64 100644 --- a/scfs-api-core/src/test/java/com/czcb/scfs/api/core/cipher/LocalCertificateProviderTest.java +++ b/scfs-api-core/src/test/java/com/czcb/scfs/api/core/cipher/LocalCertificateProviderTest.java @@ -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 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 list = new ArrayList<>(); list.add(certificate); diff --git a/scfs-api-core/src/test/java/com/czcb/scfs/api/core/http/client/ApacheHttpclientProxyTest.java b/scfs-api-core/src/test/java/com/czcb/scfs/api/core/http/client/ApacheHttpclientProxyTest.java index dd108d3..389f755 100644 --- a/scfs-api-core/src/test/java/com/czcb/scfs/api/core/http/client/ApacheHttpclientProxyTest.java +++ b/scfs-api-core/src/test/java/com/czcb/scfs/api/core/http/client/ApacheHttpclientProxyTest.java @@ -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 list = new ArrayList<>(); list.add(certificate); CertificateProvider certificateProvider = new LocalCertificateProvider(list); diff --git a/scfs-api-core/src/test/java/com/czcb/scfs/api/core/http/client/ApacheHttpclientTestv2.java b/scfs-api-core/src/test/java/com/czcb/scfs/api/core/http/client/ApacheHttpclientTestv2.java deleted file mode 100644 index 87fa379..0000000 --- a/scfs-api-core/src/test/java/com/czcb/scfs/api/core/http/client/ApacheHttpclientTestv2.java +++ /dev/null @@ -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 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 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; - } - } -} \ No newline at end of file diff --git a/scfs-api-core/src/test/java/com/czcb/scfs/api/core/http/client/ApacheHttpclientTestv3.java b/scfs-api-core/src/test/java/com/czcb/scfs/api/core/http/client/ApacheHttpclientTestv3.java deleted file mode 100644 index 12ba895..0000000 --- a/scfs-api-core/src/test/java/com/czcb/scfs/api/core/http/client/ApacheHttpclientTestv3.java +++ /dev/null @@ -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 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; - } - } -} \ No newline at end of file diff --git a/scfs-api-core/src/test/java/com/czcb/scfs/api/core/http/client/ApacheHttpclientV2Test.java b/scfs-api-core/src/test/java/com/czcb/scfs/api/core/http/client/ApacheHttpclientV2Test.java index 7292a4e..5164559 100644 --- a/scfs-api-core/src/test/java/com/czcb/scfs/api/core/http/client/ApacheHttpclientV2Test.java +++ b/scfs-api-core/src/test/java/com/czcb/scfs/api/core/http/client/ApacheHttpclientV2Test.java @@ -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 list = new ArrayList<>(); list.add(certificate); CertificateProvider certificateProvider = new LocalCertificateProvider(list); diff --git a/scfs-api-core/src/test/java/com/czcb/scfs/api/core/http/client/ApacheHttpclientV3Test.java b/scfs-api-core/src/test/java/com/czcb/scfs/api/core/http/client/ApacheHttpclientV3Test.java index 0da1d98..b1b2063 100644 --- a/scfs-api-core/src/test/java/com/czcb/scfs/api/core/http/client/ApacheHttpclientV3Test.java +++ b/scfs-api-core/src/test/java/com/czcb/scfs/api/core/http/client/ApacheHttpclientV3Test.java @@ -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 list = new ArrayList<>(); list.add(certificate); CertificateProvider certificateProvider = new LocalCertificateProvider(list); diff --git a/scfs-api-core/src/test/java/com/czcb/scfs/api/core/util/PemFileTest.java b/scfs-api-core/src/test/java/com/czcb/scfs/api/core/util/PemFileTest.java index 37bf2be..9998b6c 100644 --- a/scfs-api-core/src/test/java/com/czcb/scfs/api/core/util/PemFileTest.java +++ b/scfs-api-core/src/test/java/com/czcb/scfs/api/core/util/PemFileTest.java @@ -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(); + } } } \ No newline at end of file