|
67 | 67 | import javax.crypto.SecretKey; |
68 | 68 |
|
69 | 69 | import com.wolfssl.provider.jce.WolfCryptProvider; |
| 70 | +import com.wolfssl.provider.jce.WolfSSLKeyStore; |
70 | 71 | import com.wolfssl.wolfcrypt.test.TimedTestWatcher; |
71 | 72 |
|
72 | 73 | public class WolfSSLKeyStoreTest { |
@@ -2371,5 +2372,81 @@ public void run() { |
2371 | 2372 | assertNotNull(chain); |
2372 | 2373 | assertTrue(chain.length > 0); |
2373 | 2374 | } |
| 2375 | + |
| 2376 | + /** |
| 2377 | + * Test that engineProbe() correctly identifies WKS format by checking |
| 2378 | + * the magic number at the beginning of the stream. |
| 2379 | + */ |
| 2380 | + @Test |
| 2381 | + public void testEngineProbeIdentifiesWKS() |
| 2382 | + throws KeyStoreException, IOException, NoSuchProviderException, |
| 2383 | + NoSuchAlgorithmException, CertificateException { |
| 2384 | + |
| 2385 | + /* Create a WKS keystore and store it to a byte array */ |
| 2386 | + KeyStore store = KeyStore.getInstance(storeType, storeProvider); |
| 2387 | + store.load(null, storePass.toCharArray()); |
| 2388 | + |
| 2389 | + ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
| 2390 | + store.store(baos, storePass.toCharArray()); |
| 2391 | + byte[] wksBytes = baos.toByteArray(); |
| 2392 | + |
| 2393 | + /* Get the WolfSSLKeyStore SPI instance via reflection to call |
| 2394 | + * engineProbe directly (since KeyStore doesn't expose it) */ |
| 2395 | + try { |
| 2396 | + WolfSSLKeyStore wksSpi = new WolfSSLKeyStore(); |
| 2397 | + |
| 2398 | + /* Test that valid WKS data returns true */ |
| 2399 | + ByteArrayInputStream bais = new ByteArrayInputStream(wksBytes); |
| 2400 | + boolean result = wksSpi.engineProbe(bais); |
| 2401 | + assertTrue("engineProbe should return true for valid WKS", result); |
| 2402 | + |
| 2403 | + /* Verify stream position is preserved after engineProbe(). |
| 2404 | + * Per KeyStoreSpi spec, probe should leave stream at original |
| 2405 | + * position so other implementations can try. */ |
| 2406 | + assertEquals("Stream should be at beginning after engineProbe()", |
| 2407 | + wksBytes[0] & 0xFF, bais.read()); |
| 2408 | + |
| 2409 | + /* Test that invalid data (non-WKS) returns false */ |
| 2410 | + byte[] invalidData = new byte[] { |
| 2411 | + 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 |
| 2412 | + }; |
| 2413 | + bais = new ByteArrayInputStream(invalidData); |
| 2414 | + result = wksSpi.engineProbe(bais); |
| 2415 | + assertFalse("engineProbe should return false for non-WKS", result); |
| 2416 | + |
| 2417 | + /* Test that JKS magic number returns false |
| 2418 | + * JKS magic is 0xFEEDFEED */ |
| 2419 | + byte[] jksMagic = new byte[] { |
| 2420 | + (byte)0xFE, (byte)0xED, (byte)0xFE, (byte)0xED |
| 2421 | + }; |
| 2422 | + bais = new ByteArrayInputStream(jksMagic); |
| 2423 | + result = wksSpi.engineProbe(bais); |
| 2424 | + assertFalse("engineProbe should return false for JKS", result); |
| 2425 | + |
| 2426 | + /* Test that empty stream returns false */ |
| 2427 | + bais = new ByteArrayInputStream(new byte[0]); |
| 2428 | + result = wksSpi.engineProbe(bais); |
| 2429 | + assertFalse("engineProbe should return false for empty stream", |
| 2430 | + result); |
| 2431 | + |
| 2432 | + /* Test that stream shorter than 4 bytes returns false */ |
| 2433 | + bais = new ByteArrayInputStream(new byte[] {0x00, 0x00, 0x00}); |
| 2434 | + result = wksSpi.engineProbe(bais); |
| 2435 | + assertFalse("engineProbe should return false for short stream", |
| 2436 | + result); |
| 2437 | + |
| 2438 | + } catch (Exception e) { |
| 2439 | + fail("engineProbe test threw exception: " + e.getMessage()); |
| 2440 | + } |
| 2441 | + } |
| 2442 | + |
| 2443 | + /** |
| 2444 | + * Test that engineProbe() throws NullPointerException for null stream. |
| 2445 | + */ |
| 2446 | + @Test(expected = NullPointerException.class) |
| 2447 | + public void testEngineProbeNullStream() throws IOException { |
| 2448 | + WolfSSLKeyStore wksSpi = new WolfSSLKeyStore(); |
| 2449 | + wksSpi.engineProbe(null); |
| 2450 | + } |
2374 | 2451 | } |
2375 | 2452 |
|
0 commit comments