Skip to content

Instantly share code, notes, and snippets.

@swankjesse
Created March 4, 2020 02:45
Show Gist options
  • Save swankjesse/b504eb456474e4c5f47fbbeba1ef0244 to your computer and use it in GitHub Desktop.
Save swankjesse/b504eb456474e4c5f47fbbeba1ef0244 to your computer and use it in GitHub Desktop.
/*
* Copyright (C) 2020 Square, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package okhttp3.internal.ws
import okio.Buffer
import okio.DeflaterSink
import okio.InflaterSource
import org.assertj.core.api.Assertions.assertThat
import org.junit.Test
import java.util.zip.Deflater
import java.util.zip.Inflater
class ResetTest {
private val deflatedBuffer = Buffer()
private val deflater = Deflater()
private val deflaterSink = DeflaterSink(deflatedBuffer, deflater)
private val inflaterBuffer = Buffer()
private val inflater = Inflater()
private val inflaterSource = InflaterSource(inflaterBuffer, inflater)
@Test
fun inflaterResets() {
val deflatedMessages = mutableListOf<Buffer>()
deflatedMessages += deflateMessage("abc".repeat(100))
deflater.reset()
deflatedMessages += deflateMessage("def".repeat(100))
deflater.reset()
assertThat(inflateMessage(deflatedMessages.removeAt(0)))
.isEqualTo("abc".repeat(100))
inflater.reset()
assertThat(inflateMessage(deflatedMessages.removeAt(0)))
.isEqualTo("def".repeat(100))
inflater.reset()
}
@Test
fun inflaterDoesNotReset() {
val deflatedMessages = mutableListOf<Buffer>()
deflatedMessages += deflateMessage("abc".repeat(100))
deflater.reset()
deflatedMessages += deflateMessage("def".repeat(100))
deflater.reset()
assertThat(inflateMessage(deflatedMessages.removeAt(0)))
.isEqualTo("abc".repeat(100))
assertThat(inflateMessage(deflatedMessages.removeAt(0)))
.isEqualTo("def".repeat(100))
}
private fun deflateMessage(message: String): Buffer {
val contentBuffer = Buffer().writeUtf8(message)
deflaterSink.write(contentBuffer, contentBuffer.size)
deflaterSink.flush()
return Buffer().also { it.writeAll(deflatedBuffer) }
}
private fun inflateMessage(message: Buffer): String {
val targetSize = inflater.bytesRead + message.size
inflaterBuffer.writeAll(message)
val result = Buffer()
while (inflater.bytesRead < targetSize) {
check(inflaterSource.read(result, Long.MAX_VALUE) != -1L)
}
return result.readUtf8()
}
}
@swankjesse
Copy link
Author

swankjesse commented Mar 4, 2020

The inflaterResets() test passes.

The inflaterDoesNotReset() test fails like this:

java.io.IOException: java.util.zip.DataFormatException: invalid stored block lengths
	at okio.InflaterSource.read(InflaterSource.kt:77)
	at okhttp3.internal.ws.ResetTest.inflateMessage(ResetTest.kt:69)
	at okhttp3.internal.ws.ResetTest.inflaterDoesNotReset(ResetTest.kt:52)
Caused by: java.util.zip.DataFormatException: invalid stored block lengths
	at java.base/java.util.zip.Inflater.inflateBytesBytes(Native Method)
	at java.base/java.util.zip.Inflater.inflate(Inflater.java:385)
	at okio.InflaterSource.read(InflaterSource.kt:60)
	... 27 more

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment