/* * Copyright (C) 2014 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 okio; import java.io.EOFException; import java.io.IOException; import java.util.Arrays; import java.util.Random; import java.util.zip.DeflaterOutputStream; import java.util.zip.Inflater; import org.junit.Test; import static org.junit.Assert.assertEquals; import static org.junit.Assert.fail; public final class InflaterSourceTest { @Test public void inflate() throws Exception { OkBuffer deflated = decodeBase64("eJxzz09RyEjNKVAoLdZRKE9VL0pVyMxTKMlIVchIzEspVshPU0jNS8/MS00tK" + "tYDAF6CD5s="); OkBuffer inflated = inflate(deflated); assertEquals("God help us, we're in the hands of engineers.", readUtf8(inflated)); } @Test public void inflateTruncated() throws Exception { OkBuffer deflated = decodeBase64("eJxzz09RyEjNKVAoLdZRKE9VL0pVyMxTKMlIVchIzEspVshPU0jNS8/MS00tK" + "tYDAF6CDw=="); try { inflate(deflated); fail(); } catch (EOFException expected) { } } @Test public void inflateWellCompressed() throws Exception { OkBuffer deflated = decodeBase64("eJztwTEBAAAAwqCs61/CEL5AAQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" + "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" + "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" + "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" + "AAAAAAAAAAAAAAAAAAAAAAAAAA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AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAB8B" + "tFeWvE=\n"); String original = repeat('a', 1024 * 1024); OkBuffer inflated = inflate(deflated); assertEquals(original, readUtf8(inflated)); } @Test public void inflatePoorlyCompressed() throws Exception { ByteString original = randomBytes(1024 * 1024); OkBuffer deflated = deflate(original); OkBuffer inflated = inflate(deflated); assertEquals(original, inflated.readByteString(inflated.size())); } private OkBuffer decodeBase64(String s) { return new OkBuffer().write(ByteString.decodeBase64(s)); } private String readUtf8(OkBuffer buffer) { return buffer.readUtf8(buffer.size()); } /** Use DeflaterOutputStream to deflate source. */ private OkBuffer deflate(ByteString source) throws IOException { OkBuffer result = new OkBuffer(); Sink sink = Okio.sink(new DeflaterOutputStream(result.outputStream())); sink.write(new OkBuffer().write(source), source.size()); sink.close(); return result; } /** Returns a new buffer containing the inflated contents of {@code deflated}. */ private OkBuffer inflate(OkBuffer deflated) throws IOException { OkBuffer result = new OkBuffer(); InflaterSource source = new InflaterSource(deflated, new Inflater()); while (source.read(result, Integer.MAX_VALUE) != -1) { } return result; } private ByteString randomBytes(int length) { Random random = new Random(0); byte[] randomBytes = new byte[length]; random.nextBytes(randomBytes); return ByteString.of(randomBytes); } private String repeat(char c, int count) { char[] array = new char[count]; Arrays.fill(array, c); return new String(array); } }