1 /* 2 * Copyright 2013 Twitter, 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 com.squareup.okhttp.internal.spdy; 17 18 import java.io.ByteArrayOutputStream; 19 import java.io.DataOutputStream; 20 import java.io.IOException; 21 import java.util.Arrays; 22 import java.util.Random; 23 import org.junit.Test; 24 25 import static org.junit.Assert.assertEquals; 26 import static org.junit.Assert.assertTrue; 27 28 /** 29 * Original version of this class was lifted from {@code com.twitter.hpack.HuffmanTest}. 30 */ 31 public class HuffmanTest { 32 33 @Test public void roundTripForRequestAndResponse() throws IOException { 34 String s = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; 35 for (int i = 0; i < s.length(); i++) { 36 assertRoundTrip(s.substring(0, i).getBytes()); 37 } 38 39 Random random = new Random(123456789L); 40 byte[] buf = new byte[4096]; 41 random.nextBytes(buf); 42 assertRoundTrip(buf); 43 } 44 45 private void assertRoundTrip(byte[] buf) throws IOException { 46 assertRoundTrip(Huffman.Codec.REQUEST, buf); 47 assertRoundTrip(Huffman.Codec.RESPONSE, buf); 48 } 49 50 private static void assertRoundTrip(Huffman.Codec codec, byte[] buf) throws IOException { 51 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 52 DataOutputStream dos = new DataOutputStream(baos); 53 54 codec.encode(buf, dos); 55 assertEquals(baos.size(), codec.encodedLength(buf)); 56 57 byte[] decodedBytes = codec.decode(baos.toByteArray()); 58 assertTrue(Arrays.equals(buf, decodedBytes)); 59 } 60 } 61