@@ -296,6 +296,49 @@ private void testResponseCaching(TransferKind transferKind) throws IOException {
296
296
assertThat (response2 .handshake ().localPrincipal ()).isEqualTo (localPrincipal );
297
297
}
298
298
299
+ @ Test public void secureResponseCachingWithCorruption () throws IOException {
300
+ server .useHttps (handshakeCertificates .sslSocketFactory ());
301
+ server .enqueue (new MockResponse .Builder ()
302
+ .addHeader ("Last-Modified: " + formatDate (-1 , TimeUnit .HOURS ))
303
+ .addHeader ("Expires: " + formatDate (1 , TimeUnit .HOURS ))
304
+ .body ("ABC" )
305
+ .build ());
306
+ server .enqueue (new MockResponse .Builder ()
307
+ .addHeader ("Last-Modified: " + formatDate (-5 , TimeUnit .MINUTES ))
308
+ .addHeader ("Expires: " + formatDate (2 , TimeUnit .HOURS ))
309
+ .body ("DEF" )
310
+ .build ());
311
+
312
+ client = client .newBuilder ()
313
+ .sslSocketFactory (
314
+ handshakeCertificates .sslSocketFactory (), handshakeCertificates .trustManager ())
315
+ .hostnameVerifier (NULL_HOSTNAME_VERIFIER )
316
+ .build ();
317
+
318
+ Request request = new Request .Builder ().url (server .url ("/" )).build ();
319
+ Response response1 = client .newCall (request ).execute ();
320
+ assertThat (response1 .body ().string ()).isEqualTo ("ABC" );
321
+
322
+ Path cacheEntry = fileSystem .allPaths ().stream ()
323
+ .filter ((e ) -> e .name ().endsWith (".0" ))
324
+ .findFirst ()
325
+ .orElseThrow ();
326
+ corruptCertificate (cacheEntry );
327
+
328
+ Response response2 = client .newCall (request ).execute (); // Not Cached!
329
+ assertThat (response2 .body ().string ()).isEqualTo ("DEF" );
330
+
331
+ assertThat (cache .requestCount ()).isEqualTo (2 );
332
+ assertThat (cache .networkCount ()).isEqualTo (2 );
333
+ assertThat (cache .hitCount ()).isEqualTo (0 );
334
+ }
335
+
336
+ private void corruptCertificate (Path cacheEntry ) throws IOException {
337
+ String content = Okio .buffer (fileSystem .source (cacheEntry )).readUtf8 ();
338
+ content = content .replace ("MII" , "!!!" );
339
+ Okio .buffer (fileSystem .sink (cacheEntry )).writeUtf8 (content ).close ();
340
+ }
341
+
299
342
@ Test public void responseCachingAndRedirects () throws Exception {
300
343
server .enqueue (new MockResponse ()
301
344
.addHeader ("Last-Modified: " + formatDate (-1 , TimeUnit .HOURS ))
0 commit comments