forked from netty/netty
/
DefaultHttp2HeadersDecoderTest.java
203 lines (183 loc) · 8.29 KB
/
DefaultHttp2HeadersDecoderTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/*
* Copyright 2014 The Netty Project
*
* The Netty Project licenses this file to you under the Apache License, version 2.0 (the
* "License"); you may not use this file except in compliance with the License. You may obtain a
* copy of the License at:
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*/
package io.netty.handler.codec.http2;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.handler.codec.http.HttpHeaderNames;
import io.netty.handler.codec.http.HttpHeaderValues;
import io.netty.util.AsciiString;
import io.netty.util.ReferenceCountUtil;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.function.Executable;
import static io.netty.handler.codec.http2.Http2CodecUtil.MAX_HEADER_LIST_SIZE;
import static io.netty.handler.codec.http2.Http2CodecUtil.MIN_HEADER_LIST_SIZE;
import static io.netty.handler.codec.http2.Http2HeadersEncoder.NEVER_SENSITIVE;
import static io.netty.handler.codec.http2.Http2TestUtil.newTestEncoder;
import static io.netty.handler.codec.http2.Http2TestUtil.randomBytes;
import static io.netty.util.CharsetUtil.UTF_8;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
/**
* Tests for {@link DefaultHttp2HeadersDecoder}.
*/
public class DefaultHttp2HeadersDecoderTest {
private DefaultHttp2HeadersDecoder decoder;
@BeforeEach
public void setup() {
decoder = new DefaultHttp2HeadersDecoder(false);
}
@Test
public void decodeShouldSucceed() throws Exception {
ByteBuf buf = encode(b(":method"), b("GET"), b("akey"), b("avalue"), randomBytes(), randomBytes());
try {
Http2Headers headers = decoder.decodeHeaders(0, buf);
assertEquals(3, headers.size());
assertEquals("GET", headers.method().toString());
assertEquals("avalue", headers.get(new AsciiString("akey")).toString());
} finally {
buf.release();
}
}
@Test
public void testExceedHeaderSize() throws Exception {
final int maxListSize = 100;
decoder.configuration().maxHeaderListSize(maxListSize, maxListSize);
final ByteBuf buf = encode(randomBytes(maxListSize), randomBytes(1));
try {
assertThrows(Http2Exception.class, new Executable() {
@Override
public void execute() throws Throwable {
decoder.decodeHeaders(0, buf);
}
});
} finally {
buf.release();
}
}
@Test
public void decodeLargerThanHeaderListSizeButLessThanGoAway() throws Exception {
decoder.maxHeaderListSize(MIN_HEADER_LIST_SIZE, MAX_HEADER_LIST_SIZE);
final ByteBuf buf = encode(b(":method"), b("GET"));
final int streamId = 1;
Http2Exception.HeaderListSizeException e =
assertThrows(Http2Exception.HeaderListSizeException.class, new Executable() {
@Override
public void execute() throws Throwable {
decoder.decodeHeaders(streamId, buf);
}
});
assertEquals(streamId, e.streamId());
buf.release();
}
@Test
public void decodeLargerThanHeaderListSizeButLessThanGoAwayWithInitialDecoderSettings() throws Exception {
final ByteBuf buf = encode(b(":method"), b("GET"), b("test_header"),
b(String.format("%09000d", 0).replace('0', 'A')));
final int streamId = 1;
try {
Http2Exception.HeaderListSizeException e = assertThrows(Http2Exception.HeaderListSizeException.class,
new Executable() {
@Override
public void execute() throws Throwable {
decoder.decodeHeaders(streamId, buf);
}
});
assertEquals(streamId, e.streamId());
} finally {
buf.release();
}
}
@Test
public void decodeLargerThanHeaderListSizeGoAway() throws Exception {
decoder.maxHeaderListSize(MIN_HEADER_LIST_SIZE, MIN_HEADER_LIST_SIZE);
final ByteBuf buf = encode(b(":method"), b("GET"));
final int streamId = 1;
try {
Http2Exception e = assertThrows(Http2Exception.class, new Executable() {
@Override
public void execute() throws Throwable {
decoder.decodeHeaders(streamId, buf);
}
});
assertEquals(Http2Error.PROTOCOL_ERROR, e.error());
} finally {
buf.release();
}
}
@Test
public void duplicatePseudoHeadersMustFailValidation() throws Exception {
final DefaultHttp2HeadersDecoder decoder = new DefaultHttp2HeadersDecoder(true);
verifyValidationFails(decoder, encode(b(":authority"), b("abc"), b(":authority"), b("def")));
}
@Test
void decodingTrailersTeHeaderMustNotFailValidation() throws Exception {
// The TE header is expressly allowed to have the value "trailers".
ByteBuf buf = null;
try {
buf = encode(b(":method"), b("GET"), b("te"), b("trailers"));
Http2Headers headers = decoder.decodeHeaders(1, buf); // This must not throw.
assertThat(headers.get(HttpHeaderNames.TE)).isEqualToIgnoringCase(HttpHeaderValues.TRAILERS);
} finally {
ReferenceCountUtil.release(buf);
}
}
@Test
public void decodingConnectionRelatedHeadersMustFailValidation() throws Exception {
final DefaultHttp2HeadersDecoder decoder = new DefaultHttp2HeadersDecoder(true);
// Standard connection related headers
verifyValidationFails(decoder, encode(b(":method"), b("GET"), b("keep-alive"), b("timeout=5")));
verifyValidationFails(decoder, encode(b(":method"), b("GET"),
b("connection"), b("keep-alive"), b("keep-alive"), b("timeout=5")));
verifyValidationFails(decoder, encode(b(":method"), b("GET"), b("transfer-encoding"), b("chunked")));
verifyValidationFails(decoder, encode(b(":method"), b("GET"),
b("connection"), b("transfer-encoding"), b("transfer-encoding"), b("chunked")));
verifyValidationFails(decoder, encode(b(":method"), b("GET"), b("upgrade"), b("foo/2")));
verifyValidationFails(decoder, encode(b(":method"), b("GET"),
b("connection"), b("upgrade"), b("upgrade"), b("foo/2")));
verifyValidationFails(decoder, encode(b(":method"), b("GET"), b("connection"), b("close")));
// Non-standard connection related headers:
verifyValidationFails(decoder, encode(b(":method"), b("GET"), b("proxy-connection"), b("keep-alive")));
// Only "trailers" is allowed for the TE header:
verifyValidationFails(decoder, encode(b(":method"), b("GET"), b("te"), b("compress")));
}
private static void verifyValidationFails(final DefaultHttp2HeadersDecoder decoder, final ByteBuf buf) {
try {
Http2Exception e = assertThrows(Http2Exception.class, new Executable() {
@Override
public void execute() throws Throwable {
decoder.decodeHeaders(1, buf);
}
});
assertEquals(Http2Error.PROTOCOL_ERROR, e.error());
} finally {
buf.release();
}
}
private static byte[] b(String string) {
return string.getBytes(UTF_8);
}
private static ByteBuf encode(byte[]... entries) throws Exception {
HpackEncoder hpackEncoder = newTestEncoder();
ByteBuf out = Unpooled.buffer();
Http2Headers http2Headers = new DefaultHttp2Headers(false);
for (int ix = 0; ix < entries.length;) {
http2Headers.add(new AsciiString(entries[ix++], false), new AsciiString(entries[ix++], false));
}
hpackEncoder.encodeHeaders(3 /* randomly chosen */, out, http2Headers, NEVER_SENSITIVE);
return out;
}
}