Misc cleanup

This commit is contained in:
Oracle 2026-04-29 16:59:33 +02:00
parent 9b5fa56215
commit 6ed2ecd2e6
Signed by: Oracle
SSH key fingerprint: SHA256:x4/RtnjUyuHkdvmwNDsWSfcfF1V5PNr3OpriZqOvCX8
13 changed files with 70 additions and 565 deletions

View file

@ -58,8 +58,8 @@ class CloseTest {
SecureCompletionClient client = new SecureCompletionClient();
client.generateKeys(false);
assertDoesNotThrow(() -> client.close(), "First close should not throw");
assertDoesNotThrow(() -> client.close(), "Second close should not throw");
assertDoesNotThrow(() -> client.close(), "Third close should not throw");
assertDoesNotThrow(client::close, "First close should not throw");
assertDoesNotThrow(client::close, "Second close should not throw");
assertDoesNotThrow(client::close, "Third close should not throw");
}
}

View file

@ -18,6 +18,9 @@ import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.security.spec.X509EncodedKeySpec;
import javax.crypto.spec.OAEPParameterSpec;
import javax.crypto.spec.PSource;
import java.security.spec.MGF1ParameterSpec;
import static org.junit.jupiter.api.Assertions.*;
@ -57,7 +60,8 @@ class DecryptResponseTest {
PublicKey serverPublicKey = KeyFactory.getInstance("RSA").generatePublic(keySpec);
Cipher rsaCipher = Cipher.getInstance("RSA/ECB/OAEPWithSHA-256AndMGF1Padding");
rsaCipher.init(Cipher.ENCRYPT_MODE, serverPublicKey);
OAEPParameterSpec oaepParams = new OAEPParameterSpec("SHA-256", "MGF1", new MGF1ParameterSpec("SHA-256"), PSource.PSpecified.DEFAULT);
rsaCipher.init(Cipher.ENCRYPT_MODE, serverPublicKey, oaepParams);
byte[] encryptedAESKey = rsaCipher.doFinal(aesKey.getEncoded());
JsonObject packageJson = new JsonObject();
@ -121,7 +125,8 @@ class DecryptResponseTest {
PublicKey serverPublicKey = KeyFactory.getInstance("RSA").generatePublic(new X509EncodedKeySpec(pubKeyBytes));
Cipher rsaCipher = Cipher.getInstance("RSA/ECB/OAEPWithSHA-256AndMGF1Padding");
rsaCipher.init(Cipher.ENCRYPT_MODE, serverPublicKey);
OAEPParameterSpec oaepParams = new OAEPParameterSpec("SHA-256", "MGF1", new MGF1ParameterSpec("SHA-256"), PSource.PSpecified.DEFAULT);
rsaCipher.init(Cipher.ENCRYPT_MODE, serverPublicKey, oaepParams);
byte[] encryptedAESKey = rsaCipher.doFinal(aesKey.getEncoded());
JsonObject packageJson = new JsonObject();
@ -155,8 +160,7 @@ class DecryptResponseTest {
CompletableFuture<Map<String, Object>> future = client.decryptResponse(new byte[0], "test-id");
ExecutionException error = assertThrows(ExecutionException.class, future::get);
assertTrue(error.getCause() instanceof SecureCompletionClient.ValueError,
"Should throw ValueError for empty response");
assertInstanceOf(SecureCompletionClient.ValueError.class, error.getCause(), "Should throw ValueError for empty response");
assertTrue(error.getCause().getMessage().contains("Empty"),
"Error message should mention empty");
}
@ -171,8 +175,7 @@ class DecryptResponseTest {
CompletableFuture<Map<String, Object>> future = client.decryptResponse(null, "test-id");
ExecutionException error = assertThrows(ExecutionException.class, future::get);
assertTrue(error.getCause() instanceof SecureCompletionClient.ValueError,
"Should throw ValueError for null response");
assertInstanceOf(SecureCompletionClient.ValueError.class, error.getCause(), "Should throw ValueError for null response");
}
@Test
@ -186,8 +189,7 @@ class DecryptResponseTest {
CompletableFuture<Map<String, Object>> future = client.decryptResponse(invalidJson, "test-id");
ExecutionException error = assertThrows(ExecutionException.class, future::get);
assertTrue(error.getCause() instanceof SecureCompletionClient.ValueError,
"Should throw ValueError for malformed JSON");
assertInstanceOf(SecureCompletionClient.ValueError.class, error.getCause(), "Should throw ValueError for malformed JSON");
assertTrue(error.getCause().getMessage().contains("malformed JSON") || error.getCause().getMessage().contains("JSON"),
"Error message should mention JSON");
}
@ -207,8 +209,7 @@ class DecryptResponseTest {
CompletableFuture<Map<String, Object>> future = client.decryptResponse(encryptedResponse, "test-id");
ExecutionException error = assertThrows(ExecutionException.class, future::get);
assertTrue(error.getCause() instanceof SecureCompletionClient.ValueError,
"Should throw ValueError for missing fields");
assertInstanceOf(SecureCompletionClient.ValueError.class, error.getCause(), "Should throw ValueError for missing fields");
assertTrue(error.getCause().getMessage().contains("Missing required fields"),
"Error message should mention missing fields");
}
@ -234,8 +235,7 @@ class DecryptResponseTest {
CompletableFuture<Map<String, Object>> future = client.decryptResponse(encryptedResponse, "test-id");
ExecutionException error = assertThrows(ExecutionException.class, future::get);
assertTrue(error.getCause() instanceof SecureCompletionClient.ValueError,
"Should throw ValueError for wrong version");
assertInstanceOf(SecureCompletionClient.ValueError.class, error.getCause(), "Should throw ValueError for wrong version");
assertTrue(error.getCause().getMessage().contains("Unsupported protocol version"),
"Error message should mention unsupported version");
}
@ -261,8 +261,7 @@ class DecryptResponseTest {
CompletableFuture<Map<String, Object>> future = client.decryptResponse(encryptedResponse, "test-id");
ExecutionException error = assertThrows(ExecutionException.class, future::get);
assertTrue(error.getCause() instanceof SecureCompletionClient.ValueError,
"Should throw ValueError for wrong algorithm");
assertInstanceOf(SecureCompletionClient.ValueError.class, error.getCause(), "Should throw ValueError for wrong algorithm");
assertTrue(error.getCause().getMessage().contains("Unsupported encryption algorithm"),
"Error message should mention unsupported algorithm");
}
@ -270,7 +269,7 @@ class DecryptResponseTest {
@Test
@Execution(ExecutionMode.SAME_THREAD)
@DisplayName("decryptResponse should throw SecurityError when private key not initialized")
void decryptResponse_noPrivateKey_shouldThrowSecurityError() throws Exception {
void decryptResponse_noPrivateKey_shouldThrowSecurityError() {
SecureCompletionClient client = new SecureCompletionClient();
JsonObject packageJson = new JsonObject();
@ -287,8 +286,7 @@ class DecryptResponseTest {
CompletableFuture<Map<String, Object>> future = client.decryptResponse(encryptedResponse, "test-id");
ExecutionException error = assertThrows(ExecutionException.class, future::get);
assertTrue(error.getCause() instanceof SecurityError,
"Should throw SecurityError when no private key");
assertInstanceOf(SecurityError.class, error.getCause(), "Should throw SecurityError when no private key");
assertTrue(error.getCause().getMessage().contains("Private key not initialized"),
"Error message should mention private key not initialized");
}
@ -343,7 +341,6 @@ class DecryptResponseTest {
CompletableFuture<Map<String, Object>> future = client2.decryptResponse(encryptedResponse, "test-id");
ExecutionException error = assertThrows(ExecutionException.class, future::get);
assertTrue(error.getCause() instanceof SecurityError,
"Should throw SecurityError for wrong private key");
assertInstanceOf(SecurityError.class, error.getCause(), "Should throw SecurityError for wrong private key");
}
}

View file

@ -138,7 +138,7 @@ class SecureChatCompletionTest {
} catch (RuntimeException e) {
Throwable cause = e.getCause();
if (cause instanceof ExecutionException) {
throw (ExecutionException) cause;
throw cause;
}
throw new ExecutionException(cause);
}
@ -158,7 +158,7 @@ class SecureChatCompletionTest {
} catch (RuntimeException e) {
Throwable cause = e.getCause();
if (cause instanceof ExecutionException) {
throw (ExecutionException) cause;
throw cause;
}
throw new ExecutionException(cause);
}
@ -176,7 +176,7 @@ class SecureChatCompletionTest {
} catch (RuntimeException e) {
Throwable cause = e.getCause();
if (cause instanceof ExecutionException) {
throw (ExecutionException) cause;
throw cause;
}
throw new ExecutionException(cause);
}
@ -203,7 +203,7 @@ class SecureChatCompletionTest {
} catch (RuntimeException e) {
Throwable cause = e.getCause();
if (cause instanceof ExecutionException) {
throw (ExecutionException) cause;
throw cause;
}
throw new ExecutionException(cause);
}
@ -229,8 +229,8 @@ class SecureChatCompletionTest {
void chatCompletion_close_multipleCalls_shouldNotThrow() {
SecureChatCompletion chat = new SecureChatCompletion();
assertDoesNotThrow(() -> chat.close(), "First close should not throw");
assertDoesNotThrow(() -> chat.close(), "Second close should not throw");
assertDoesNotThrow(chat::close, "First close should not throw");
assertDoesNotThrow(chat::close, "Second close should not throw");
}
@Test

View file

@ -108,7 +108,7 @@ class SecureMemoryTest {
buffer.zero();
assertDoesNotThrow(() -> buffer.zero(), "Zeroing should not throw");
assertDoesNotThrow(buffer::zero, "Zeroing should not throw");
}
@Test
@ -129,8 +129,8 @@ class SecureMemoryTest {
byte[] data = new byte[]{1, 2, 3};
SecureMemory.SecureBuffer buffer = SecureMemory.secureByteArray(data);
assertDoesNotThrow(() -> buffer.close(), "First close should not throw");
assertDoesNotThrow(() -> buffer.close(), "Second close should not throw");
assertDoesNotThrow(buffer::close, "First close should not throw");
assertDoesNotThrow(buffer::close, "Second close should not throw");
}
@Test

View file

@ -36,7 +36,7 @@ class SplitterTest {
List<String> result = Splitter.fixedLengthString(10, "1234567890");
assertEquals(1, result.size(), "Should have 1 part");
assertEquals("1234567890", result.get(0), "Single part should be the full string");
assertEquals("1234567890", result.getFirst(), "Single part should be the full string");
}
@Test
@ -45,7 +45,7 @@ class SplitterTest {
List<String> result = Splitter.fixedLengthString(100, "hello");
assertEquals(1, result.size(), "Should have 1 part");
assertEquals("hello", result.get(0), "Single part should be 'hello'");
assertEquals("hello", result.getFirst(), "Single part should be 'hello'");
}
@Test
@ -81,9 +81,9 @@ class SplitterTest {
@Test
@DisplayName("fixedLengthString should handle unicode characters")
void fixedLengthString_unicode_shouldSplitCorrectly() {
List<String> result = Splitter.fixedLengthString(2, "ab\u00e9\u00fc");
List<String> result = Splitter.fixedLengthString(2, "abéü");
assertEquals(2, result.size(), "Should have 2 parts");
assertEquals("ab", result.get(0));
assertEquals("ab", result.getFirst());
}
}