• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 com.squareup.okhttp.internal.framed;
17 
18 import com.squareup.okhttp.internal.framed.hpackjson.Case;
19 import com.squareup.okhttp.internal.framed.hpackjson.HpackJsonUtil;
20 import com.squareup.okhttp.internal.framed.hpackjson.Story;
21 import java.util.ArrayList;
22 import java.util.Collection;
23 import java.util.LinkedHashSet;
24 import java.util.List;
25 import okio.Buffer;
26 
27 import static org.junit.Assert.assertEquals;
28 import static org.junit.Assert.fail;
29 
30 /**
31  * Tests Hpack implementation using https://github.com/http2jp/hpack-test-case/
32  */
33 public class HpackDecodeTestBase {
34 
35   /**
36    * Reads all stories in the folders provided, asserts if no story found.
37    */
createStories(String[] interopTests)38   protected static Collection<Story[]> createStories(String[] interopTests)
39       throws Exception {
40     List<Story[]> result = new ArrayList<>();
41     for (String interopTestName : interopTests) {
42       List<Story> stories = HpackJsonUtil.readStories(interopTestName);
43       if (stories.isEmpty()) {
44         fail("No stories for: " + interopTestName);
45       }
46       for (Story story : stories) {
47         result.add(new Story[] { story });
48       }
49     }
50     return result;
51   }
52 
53   private final Buffer bytesIn = new Buffer();
54   private final Hpack.Reader hpackReader = new Hpack.Reader(4096, bytesIn);
55 
56   private final Story story;
57 
HpackDecodeTestBase(Story story)58   public HpackDecodeTestBase(Story story) {
59     this.story = story;
60   }
61 
62   /**
63    * Expects wire to be set for all cases, and compares the decoder's output to
64    * expected headers.
65    */
testDecoder()66   protected void testDecoder() throws Exception {
67     testDecoder(story);
68   }
69 
testDecoder(Story story)70   protected void testDecoder(Story story) throws Exception {
71     for (Case caze : story.getCases()) {
72       bytesIn.write(caze.getWire());
73       hpackReader.readHeaders();
74       assertSetEquals(String.format("seqno=%d", caze.getSeqno()), caze.getHeaders(),
75           hpackReader.getAndResetHeaderList());
76     }
77   }
78   /**
79    * Checks if {@code expected} and {@code observed} are equal when viewed as a
80    * set and headers are deduped.
81    *
82    * TODO: See if duped headers should be preserved on decode and verify.
83    */
assertSetEquals( String message, List<Header> expected, List<Header> observed)84   private static void assertSetEquals(
85       String message, List<Header> expected, List<Header> observed) {
86     assertEquals(message, new LinkedHashSet<>(expected), new LinkedHashSet<>(observed));
87   }
88 
getStory()89   protected Story getStory() {
90     return story;
91   }
92 }
93