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