13c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller/* 23c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller * Copyright 2013 Twitter, Inc. 33c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller * 43c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller * Licensed under the Apache License, Version 2.0 (the "License"); 53c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller * you may not use this file except in compliance with the License. 63c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller * You may obtain a copy of the License at 73c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller * 83c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller * http://www.apache.org/licenses/LICENSE-2.0 93c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller * 103c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller * Unless required by applicable law or agreed to in writing, software 113c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller * distributed under the License is distributed on an "AS IS" BASIS, 123c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 133c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller * See the License for the specific language governing permissions and 143c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller * limitations under the License. 153c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller */ 163c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fullerpackage com.squareup.okhttp.internal.spdy; 173c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller 183c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fullerimport java.io.ByteArrayOutputStream; 193c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fullerimport java.io.DataOutputStream; 203c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fullerimport java.io.IOException; 213c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fullerimport java.util.Arrays; 223c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fullerimport java.util.Random; 233c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fullerimport org.junit.Test; 243c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller 253c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fullerimport static org.junit.Assert.assertEquals; 263c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fullerimport static org.junit.Assert.assertTrue; 273c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller 283c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller/** 293c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller * Original version of this class was lifted from {@code com.twitter.hpack.HuffmanTest}. 303c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller */ 313c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fullerpublic class HuffmanTest { 323c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller 333c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller @Test public void roundTripForRequestAndResponse() throws IOException { 343c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller String s = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; 353c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller for (int i = 0; i < s.length(); i++) { 363c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller assertRoundTrip(s.substring(0, i).getBytes()); 373c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller } 383c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller 393c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller Random random = new Random(123456789L); 403c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller byte[] buf = new byte[4096]; 413c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller random.nextBytes(buf); 423c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller assertRoundTrip(buf); 433c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller } 443c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller 453c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller private void assertRoundTrip(byte[] buf) throws IOException { 463c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller ByteArrayOutputStream baos = new ByteArrayOutputStream(); 473c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller DataOutputStream dos = new DataOutputStream(baos); 483c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller 49e78f117bcbd6b57d783737107f445ef75ecb474aNeil Fuller Huffman.get().encode(buf, dos); 50e78f117bcbd6b57d783737107f445ef75ecb474aNeil Fuller assertEquals(baos.size(), Huffman.get().encodedLength(buf)); 513c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller 52e78f117bcbd6b57d783737107f445ef75ecb474aNeil Fuller byte[] decodedBytes = Huffman.get().decode(baos.toByteArray()); 533c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller assertTrue(Arrays.equals(buf, decodedBytes)); 543c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller } 553c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller} 56