Home | History | Annotate | Download | only in okio
      1 /*
      2  * Copyright (C) 2014 Square, Inc.
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 package okio;
     17 
     18 import java.io.EOFException;
     19 import java.io.IOException;
     20 import java.util.Arrays;
     21 import java.util.Random;
     22 import java.util.zip.DeflaterOutputStream;
     23 import java.util.zip.Inflater;
     24 import org.junit.Test;
     25 
     26 import static org.junit.Assert.assertEquals;
     27 import static org.junit.Assert.fail;
     28 
     29 public final class InflaterSourceTest {
     30   @Test public void inflate() throws Exception {
     31     OkBuffer deflated = decodeBase64("eJxzz09RyEjNKVAoLdZRKE9VL0pVyMxTKMlIVchIzEspVshPU0jNS8/MS00tK"
     32         + "tYDAF6CD5s=");
     33     OkBuffer inflated = inflate(deflated);
     34     assertEquals("God help us, we're in the hands of engineers.", readUtf8(inflated));
     35   }
     36 
     37   @Test public void inflateTruncated() throws Exception {
     38     OkBuffer deflated = decodeBase64("eJxzz09RyEjNKVAoLdZRKE9VL0pVyMxTKMlIVchIzEspVshPU0jNS8/MS00tK"
     39         + "tYDAF6CDw==");
     40     try {
     41       inflate(deflated);
     42       fail();
     43     } catch (EOFException expected) {
     44     }
     45   }
     46 
     47   @Test public void inflateWellCompressed() throws Exception {
     48     OkBuffer deflated = decodeBase64("eJztwTEBAAAAwqCs61/CEL5AAQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
     49         + "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
     50         + "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
     51         + "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
     52         + "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
     53         + "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
     54         + "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
     55         + "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
     56         + "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
     57         + "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
     58         + "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
     59         + "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
     60         + "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
     61         + "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
     62         + "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
     63         + "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAB8B"
     64         + "tFeWvE=\n");
     65     String original = repeat('a', 1024 * 1024);
     66     OkBuffer inflated = inflate(deflated);
     67     assertEquals(original, readUtf8(inflated));
     68   }
     69 
     70   @Test public void inflatePoorlyCompressed() throws Exception {
     71     ByteString original = randomBytes(1024 * 1024);
     72     OkBuffer deflated = deflate(original);
     73     OkBuffer inflated = inflate(deflated);
     74     assertEquals(original, inflated.readByteString(inflated.size()));
     75   }
     76 
     77   private OkBuffer decodeBase64(String s) {
     78     return new OkBuffer().write(ByteString.decodeBase64(s));
     79   }
     80 
     81   private String readUtf8(OkBuffer buffer) {
     82     return buffer.readUtf8(buffer.size());
     83   }
     84 
     85   /** Use DeflaterOutputStream to deflate source. */
     86   private OkBuffer deflate(ByteString source) throws IOException {
     87     OkBuffer result = new OkBuffer();
     88     Sink sink = Okio.sink(new DeflaterOutputStream(result.outputStream()));
     89     sink.write(new OkBuffer().write(source), source.size());
     90     sink.close();
     91     return result;
     92   }
     93 
     94   /** Returns a new buffer containing the inflated contents of {@code deflated}. */
     95   private OkBuffer inflate(OkBuffer deflated) throws IOException {
     96     OkBuffer result = new OkBuffer();
     97     InflaterSource source = new InflaterSource(deflated, new Inflater());
     98     while (source.read(result, Integer.MAX_VALUE) != -1) {
     99     }
    100     return result;
    101   }
    102 
    103   private ByteString randomBytes(int length) {
    104     Random random = new Random(0);
    105     byte[] randomBytes = new byte[length];
    106     random.nextBytes(randomBytes);
    107     return ByteString.of(randomBytes);
    108   }
    109 
    110   private String repeat(char c, int count) {
    111     char[] array = new char[count];
    112     Arrays.fill(array, c);
    113     return new String(array);
    114   }
    115 }
    116