|
23 | 23 |
|
24 | 24 | import static org.junit.Assert.*; |
25 | 25 |
|
| 26 | +import org.junit.After; |
26 | 27 | import org.junit.Assume; |
27 | 28 | import org.junit.BeforeClass; |
28 | 29 | import org.junit.Test; |
@@ -68,6 +69,15 @@ public class WolfCryptPKIXRevocationCheckerTest { |
68 | 69 | @Rule(order = Integer.MIN_VALUE) |
69 | 70 | public TestRule testWatcher = TimedTestWatcher.create(); |
70 | 71 |
|
| 72 | + /** |
| 73 | + * Clean up wolfjce.ioTimeout system property after each |
| 74 | + * test to avoid affecting other tests. |
| 75 | + */ |
| 76 | + @After |
| 77 | + public void clearIOTimeoutProperty() { |
| 78 | + System.clearProperty("wolfjce.ioTimeout"); |
| 79 | + } |
| 80 | + |
71 | 81 | /** |
72 | 82 | * Test if this environment is Android. |
73 | 83 | * @return true if Android, otherwise false |
@@ -1023,5 +1033,65 @@ public void testRevocationCheckerInitClearsExceptions() throws Exception { |
1023 | 1033 |
|
1024 | 1034 | cm.free(); |
1025 | 1035 | } |
| 1036 | + |
| 1037 | + @Test |
| 1038 | + public void testRevocationCheckerIOTimeoutLowValue() |
| 1039 | + throws Exception { |
| 1040 | + |
| 1041 | + if (!WolfCrypt.OcspEnabled()) { |
| 1042 | + /* Skip test if OCSP not compiled in */ |
| 1043 | + return; |
| 1044 | + } |
| 1045 | + |
| 1046 | + if (!WolfCrypt.IoTimeoutEnabled()) { |
| 1047 | + /* Skip test if HAVE_IO_TIMEOUT not compiled in */ |
| 1048 | + return; |
| 1049 | + } |
| 1050 | + |
| 1051 | + CertPathValidator cpv = CertPathValidator.getInstance("PKIX", provider); |
| 1052 | + WolfCryptPKIXRevocationChecker checker = |
| 1053 | + (WolfCryptPKIXRevocationChecker)cpv.getRevocationChecker(); |
| 1054 | + |
| 1055 | + /* Load certs */ |
| 1056 | + FileInputStream fis = new FileInputStream(caCertDer); |
| 1057 | + CertificateFactory cf = CertificateFactory.getInstance("X.509"); |
| 1058 | + X509Certificate caCert = (X509Certificate)cf.generateCertificate(fis); |
| 1059 | + fis.close(); |
| 1060 | + |
| 1061 | + fis = new FileInputStream(serverCertDer); |
| 1062 | + X509Certificate serverCert = |
| 1063 | + (X509Certificate)cf.generateCertificate(fis); |
| 1064 | + fis.close(); |
| 1065 | + |
| 1066 | + /* Set 1 second I/O timeout via system property */ |
| 1067 | + System.setProperty("wolfjce.ioTimeout", "1"); |
| 1068 | + |
| 1069 | + /* Set SOFT_FAIL and override OCSP URL to non-routable address. |
| 1070 | + * 198.51.100.1 (TEST-NET-2, RFC 5737) is not routable, so TCP connect |
| 1071 | + * will hang until timeout kicks in, rather than getting an immediate |
| 1072 | + * connection refused like localhost would. */ |
| 1073 | + Set<Option> options = EnumSet.of(Option.SOFT_FAIL); |
| 1074 | + checker.setOptions(options); |
| 1075 | + checker.setOcspResponder(new URI("http://198.51.100.1:12345")); |
| 1076 | + |
| 1077 | + /* Create CertManager, load CA, and init */ |
| 1078 | + WolfSSLCertManager cm = new WolfSSLCertManager(); |
| 1079 | + cm.CertManagerLoadCA(caCert); |
| 1080 | + checker.setCertManager(cm); |
| 1081 | + checker.init(false); |
| 1082 | + |
| 1083 | + /* Time the check() call. With 1 second timeout and a non-routable OCSP |
| 1084 | + * URL, should complete quickly. */ |
| 1085 | + long startMs = System.currentTimeMillis(); |
| 1086 | + checker.check(serverCert, null); |
| 1087 | + long elapsedMs = System.currentTimeMillis() - startMs; |
| 1088 | + |
| 1089 | + /* Verify check completed within reasonable time. |
| 1090 | + * Allow 10 sec margin for system overhead/etc. */ |
| 1091 | + assertTrue("OCSP check with 1s timeout took " + elapsedMs + |
| 1092 | + "ms, expected < 10000ms", elapsedMs < 10000); |
| 1093 | + |
| 1094 | + cm.free(); |
| 1095 | + } |
1026 | 1096 | } |
1027 | 1097 |
|
0 commit comments