|
36 | 36 | import java.util.concurrent.CountDownLatch; |
37 | 37 | import java.util.concurrent.LinkedBlockingQueue; |
38 | 38 |
|
| 39 | +import java.io.ByteArrayInputStream; |
| 40 | +import java.io.ByteArrayOutputStream; |
| 41 | +import java.io.IOException; |
| 42 | +import java.io.ObjectInputStream; |
| 43 | +import java.io.ObjectOutputStream; |
| 44 | + |
39 | 45 | import java.security.Security; |
40 | 46 | import java.security.Provider; |
41 | 47 | import java.security.SecureRandom; |
@@ -345,5 +351,92 @@ public void testGenerateSeedWithZeroArgument() |
345 | 351 | assertNotNull(seed); |
346 | 352 | assertEquals(0, seed.length); |
347 | 353 | } |
| 354 | + |
| 355 | + @Test |
| 356 | + public void testSerializationDeserialization() |
| 357 | + throws NoSuchProviderException, NoSuchAlgorithmException, |
| 358 | + IOException, ClassNotFoundException { |
| 359 | + |
| 360 | + /* Create original SecureRandom instance */ |
| 361 | + SecureRandom orig = SecureRandom.getInstance("HashDRBG", "wolfJCE"); |
| 362 | + assertNotNull(orig); |
| 363 | + |
| 364 | + /* Serialize the SecureRandom instance */ |
| 365 | + byte[] serialized = serialize(orig); |
| 366 | + assertNotNull(serialized); |
| 367 | + assertTrue(serialized.length > 0); |
| 368 | + |
| 369 | + /* Deserialize the SecureRandom instance */ |
| 370 | + SecureRandom copy = deserialize(serialized); |
| 371 | + assertNotNull(copy); |
| 372 | + |
| 373 | + /* Test that both original and deserialized instances can generate |
| 374 | + * random numbers without throwing NullPointerException. |
| 375 | + * This test would fail without the lazy loading fix in |
| 376 | + * WolfCryptRandom.engineNextBytes() and engineGenerateSeed() */ |
| 377 | + byte[] origBytes = new byte[32]; |
| 378 | + byte[] copyBytes = new byte[32]; |
| 379 | + |
| 380 | + /* This should not throw NullPointerException */ |
| 381 | + orig.nextBytes(origBytes); |
| 382 | + copy.nextBytes(copyBytes); |
| 383 | + |
| 384 | + /* Also test generateSeed() to ensure it works after deserialization */ |
| 385 | + byte[] origSeed = orig.generateSeed(16); |
| 386 | + byte[] copySeed = copy.generateSeed(16); |
| 387 | + |
| 388 | + assertNotNull(origSeed); |
| 389 | + assertNotNull(copySeed); |
| 390 | + assertEquals(16, origSeed.length); |
| 391 | + assertEquals(16, copySeed.length); |
| 392 | + |
| 393 | + /* Verify we got actual random data */ |
| 394 | + assertNotNull(origBytes); |
| 395 | + assertNotNull(copyBytes); |
| 396 | + assertEquals(32, origBytes.length); |
| 397 | + assertEquals(32, copyBytes.length); |
| 398 | + |
| 399 | + /* Random bytes should not be all zeros */ |
| 400 | + boolean origHasNonZero = false; |
| 401 | + boolean copyHasNonZero = false; |
| 402 | + for (int i = 0; i < 32; i++) { |
| 403 | + if (origBytes[i] != 0) { |
| 404 | + origHasNonZero = true; |
| 405 | + } |
| 406 | + if (copyBytes[i] != 0) { |
| 407 | + copyHasNonZero = true; |
| 408 | + } |
| 409 | + } |
| 410 | + assertTrue("Original random bytes should not be all zeros", |
| 411 | + origHasNonZero); |
| 412 | + assertTrue("Deserialized random bytes should not be all zeros", |
| 413 | + copyHasNonZero); |
| 414 | + |
| 415 | + /* The two instances should generate different random sequences */ |
| 416 | + assertFalse("Original and deserialized instances should generate " + |
| 417 | + "different random sequences", Arrays.equals(origBytes, copyBytes)); |
| 418 | + } |
| 419 | + |
| 420 | + /* |
| 421 | + * Serialize a SecureRandom object to byte array |
| 422 | + */ |
| 423 | + private byte[] serialize(SecureRandom sr) throws IOException { |
| 424 | + try (ByteArrayOutputStream bos = new ByteArrayOutputStream(); |
| 425 | + ObjectOutputStream oos = new ObjectOutputStream(bos)) { |
| 426 | + oos.writeObject(sr); |
| 427 | + return bos.toByteArray(); |
| 428 | + } |
| 429 | + } |
| 430 | + |
| 431 | + /* |
| 432 | + * Deserialize a SecureRandom object from byte array |
| 433 | + */ |
| 434 | + private SecureRandom deserialize(byte[] serialized) |
| 435 | + throws IOException, ClassNotFoundException { |
| 436 | + try (ByteArrayInputStream bis = new ByteArrayInputStream(serialized); |
| 437 | + ObjectInputStream ois = new ObjectInputStream(bis)) { |
| 438 | + return (SecureRandom) ois.readObject(); |
| 439 | + } |
| 440 | + } |
348 | 441 | } |
349 | 442 |
|
0 commit comments