Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[4.x] Fix bad merge #8053

Merged
merged 3 commits into from
Oct 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -119,4 +119,7 @@ class InMemoryFileSystem : FileSystem, TestRule {
}

override fun toString() = "InMemoryFileSystem"
fun allPaths(): MutableSet<File> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MutableSet –> Set

return files.keys
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

files.keys is a live view; files.keys.toSet() is a necessary defensive copy, if that’s what we want

}
}
33 changes: 18 additions & 15 deletions okhttp/src/test/java/okhttp3/CacheTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -296,18 +296,16 @@ private void testResponseCaching(TransferKind transferKind) throws IOException {
assertThat(response2.handshake().localPrincipal()).isEqualTo(localPrincipal);
}

@Test public void secureResponseCachingWithCorruption() throws IOException {
server.useHttps(handshakeCertificates.sslSocketFactory());
server.enqueue(new MockResponse.Builder()
@Test public void secureResponseCachingWithCorruption() throws Exception {
server.useHttps(handshakeCertificates.sslSocketFactory(), false);
server.enqueue(new MockResponse()
.addHeader("Last-Modified: " + formatDate(-1, TimeUnit.HOURS))
.addHeader("Expires: " + formatDate(1, TimeUnit.HOURS))
.body("ABC")
.build());
server.enqueue(new MockResponse.Builder()
.setBody("ABC"));
server.enqueue(new MockResponse()
.addHeader("Last-Modified: " + formatDate(-5, TimeUnit.MINUTES))
.addHeader("Expires: " + formatDate(2, TimeUnit.HOURS))
.body("DEF")
.build());
.setBody("DEF"));

client = client.newBuilder()
.sslSocketFactory(
Expand All @@ -319,10 +317,10 @@ private void testResponseCaching(TransferKind transferKind) throws IOException {
Response response1 = client.newCall(request).execute();
assertThat(response1.body().string()).isEqualTo("ABC");

Path cacheEntry = fileSystem.allPaths().stream()
.filter((e) -> e.name().endsWith(".0"))
File cacheEntry = fileSystem.allPaths().stream()
.filter((e) -> e.getName().endsWith(".0"))
.findFirst()
.orElseThrow();
.orElseThrow(Exception::new);
corruptCertificate(cacheEntry);

Response response2 = client.newCall(request).execute(); // Not Cached!
Expand All @@ -333,10 +331,15 @@ private void testResponseCaching(TransferKind transferKind) throws IOException {
assertThat(cache.hitCount()).isEqualTo(0);
}

private void corruptCertificate(Path cacheEntry) throws IOException {
String content = Okio.buffer(fileSystem.source(cacheEntry)).readUtf8();
content = content.replace("MII", "!!!");
Okio.buffer(fileSystem.sink(cacheEntry)).writeUtf8(content).close();
private void corruptCertificate(File cacheEntry) throws IOException {
BufferedSource source = Okio.buffer(fileSystem.source(cacheEntry));
try {
String content = source.readUtf8();
content = content.replace("MII", "!!!");
Okio.buffer(fileSystem.sink(cacheEntry)).writeUtf8(content).close();
} finally {
source.close();
}
}

@Test public void responseCachingAndRedirects() throws Exception {
Expand Down