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 java.util.ArrayList; 19 import java.util.Arrays; 20 import java.util.List; 21 import org.junit.Test; 22 23 import static com.squareup.okhttp.internal.spdy.Http2.FLAG_ACK; 24 import static com.squareup.okhttp.internal.spdy.Http2.FLAG_END_HEADERS; 25 import static com.squareup.okhttp.internal.spdy.Http2.FLAG_END_STREAM; 26 import static com.squareup.okhttp.internal.spdy.Http2.FLAG_NONE; 27 import static com.squareup.okhttp.internal.spdy.Http2.FrameLogger.formatFlags; 28 import static com.squareup.okhttp.internal.spdy.Http2.FrameLogger.formatHeader; 29 import static com.squareup.okhttp.internal.spdy.Http2.TYPE_CONTINUATION; 30 import static com.squareup.okhttp.internal.spdy.Http2.TYPE_DATA; 31 import static com.squareup.okhttp.internal.spdy.Http2.TYPE_GOAWAY; 32 import static com.squareup.okhttp.internal.spdy.Http2.TYPE_HEADERS; 33 import static com.squareup.okhttp.internal.spdy.Http2.TYPE_PING; 34 import static com.squareup.okhttp.internal.spdy.Http2.TYPE_PUSH_PROMISE; 35 import static com.squareup.okhttp.internal.spdy.Http2.TYPE_SETTINGS; 36 import static org.junit.Assert.assertEquals; 37 38 public class Http2FrameLoggerTest { 39 40 /** Real stream traffic applied to the log format. */ exampleStream()41 @Test public void exampleStream() { 42 assertEquals(">> 0x00000000 5 SETTINGS ", 43 formatHeader(false, 0, 5, TYPE_SETTINGS, FLAG_NONE)); 44 assertEquals(">> 0x00000003 100 HEADERS END_HEADERS", 45 formatHeader(false, 3, 100, TYPE_HEADERS, FLAG_END_HEADERS)); 46 assertEquals(">> 0x00000003 0 DATA END_STREAM", 47 formatHeader(false, 3, 0, TYPE_DATA, FLAG_END_STREAM)); 48 assertEquals("<< 0x00000000 15 SETTINGS ", 49 formatHeader(true, 0, 15, TYPE_SETTINGS, FLAG_NONE)); 50 assertEquals(">> 0x00000000 0 SETTINGS ACK", 51 formatHeader(false, 0, 0, TYPE_SETTINGS, FLAG_ACK)); 52 assertEquals("<< 0x00000000 0 SETTINGS ACK", 53 formatHeader(true, 0, 0, TYPE_SETTINGS, FLAG_ACK)); 54 assertEquals("<< 0x00000003 22 HEADERS END_HEADERS", 55 formatHeader(true, 3, 22, TYPE_HEADERS, FLAG_END_HEADERS)); 56 assertEquals("<< 0x00000003 226 DATA END_STREAM", 57 formatHeader(true, 3, 226, TYPE_DATA, FLAG_END_STREAM)); 58 assertEquals(">> 0x00000000 8 GOAWAY ", 59 formatHeader(false, 0, 8, TYPE_GOAWAY, FLAG_NONE)); 60 } 61 flagOverlapOn0x1()62 @Test public void flagOverlapOn0x1() { 63 assertEquals("<< 0x00000000 0 SETTINGS ACK", 64 formatHeader(true, 0, 0, TYPE_SETTINGS, (byte) 0x1)); 65 assertEquals("<< 0x00000000 8 PING ACK", 66 formatHeader(true, 0, 8, TYPE_PING, (byte) 0x1)); 67 assertEquals("<< 0x00000003 0 HEADERS END_STREAM", 68 formatHeader(true, 3, 0, TYPE_HEADERS, (byte) 0x1)); 69 assertEquals("<< 0x00000003 0 DATA END_STREAM", 70 formatHeader(true, 3, 0, TYPE_DATA, (byte) 0x1)); 71 } 72 flagOverlapOn0x4()73 @Test public void flagOverlapOn0x4() { 74 assertEquals("<< 0x00000003 10000 HEADERS END_HEADERS", 75 formatHeader(true, 3, 10000, TYPE_HEADERS, (byte) 0x4)); 76 assertEquals("<< 0x00000003 10000 CONTINUATION END_HEADERS", 77 formatHeader(true, 3, 10000, TYPE_CONTINUATION, (byte) 0x4)); 78 assertEquals("<< 0x00000004 10000 PUSH_PROMISE END_PUSH_PROMISE", 79 formatHeader(true, 4, 10000, TYPE_PUSH_PROMISE, (byte) 0x4)); 80 } 81 flagOverlapOn0x20()82 @Test public void flagOverlapOn0x20() { 83 assertEquals("<< 0x00000003 10000 HEADERS PRIORITY", 84 formatHeader(true, 3, 10000, TYPE_HEADERS, (byte) 0x20)); 85 assertEquals("<< 0x00000003 10000 DATA COMPRESSED", 86 formatHeader(true, 3, 10000, TYPE_DATA, (byte) 0x20)); 87 } 88 89 /** 90 * Ensures that valid flag combinations appear visually correct, and invalid show in hex. This 91 * also demonstrates how sparse the lookup table is. 92 */ allFormattedFlagsWithValidBits()93 @Test public void allFormattedFlagsWithValidBits() { 94 List<String> formattedFlags = new ArrayList<>(0x40); // Highest valid flag is 0x20. 95 for (byte i = 0; i < 0x40; i++) formattedFlags.add(formatFlags(TYPE_HEADERS, i)); 96 97 assertEquals(Arrays.asList( 98 "", 99 "END_STREAM", 100 "00000010", 101 "00000011", 102 "END_HEADERS", 103 "END_STREAM|END_HEADERS", 104 "00000110", 105 "00000111", 106 "PADDED", 107 "END_STREAM|PADDED", 108 "00001010", 109 "00001011", 110 "00001100", 111 "END_STREAM|END_HEADERS|PADDED", 112 "00001110", 113 "00001111", 114 "00010000", 115 "00010001", 116 "00010010", 117 "00010011", 118 "00010100", 119 "00010101", 120 "00010110", 121 "00010111", 122 "00011000", 123 "00011001", 124 "00011010", 125 "00011011", 126 "00011100", 127 "00011101", 128 "00011110", 129 "00011111", 130 "PRIORITY", 131 "END_STREAM|PRIORITY", 132 "00100010", 133 "00100011", 134 "END_HEADERS|PRIORITY", 135 "END_STREAM|END_HEADERS|PRIORITY", 136 "00100110", 137 "00100111", 138 "00101000", 139 "END_STREAM|PRIORITY|PADDED", 140 "00101010", 141 "00101011", 142 "00101100", 143 "END_STREAM|END_HEADERS|PRIORITY|PADDED", 144 "00101110", 145 "00101111", 146 "00110000", 147 "00110001", 148 "00110010", 149 "00110011", 150 "00110100", 151 "00110101", 152 "00110110", 153 "00110111", 154 "00111000", 155 "00111001", 156 "00111010", 157 "00111011", 158 "00111100", 159 "00111101", 160 "00111110", 161 "00111111" 162 ), formattedFlags); 163 } 164 } 165