File: TestHttp2Section_6_9.java

package info (click to toggle)
tomcat9 9.0.115-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 48,140 kB
  • sloc: java: 383,595; xml: 71,225; jsp: 4,682; sh: 1,228; perl: 324; makefile: 18; ansic: 14
file content (273 lines) | stat: -rw-r--r-- 9,567 bytes parent folder | download | duplicates (11)
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
/*
 *  Licensed to the Apache Software Foundation (ASF) under one or more
 *  contributor license agreements.  See the NOTICE file distributed with
 *  this work for additional information regarding copyright ownership.
 *  The ASF 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
 *
 *      http://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 org.apache.coyote.http2;

import java.nio.ByteBuffer;

import org.junit.Assert;
import org.junit.Test;

/**
 * Unit tests for Section 6.9 of <a href="https://tools.ietf.org/html/rfc7540">RFC 7540</a>. <br>
 * The order of tests in this class is aligned with the order of the requirements in the RFC.
 */
public class TestHttp2Section_6_9 extends Http2TestBase {

    @Test
    public void testZeroWindowUpdateConnection() throws Exception {
        http2Connect();

        sendWindowUpdate(0, 0);

        handleGoAwayResponse(1);
    }


    @Test
    public void testZeroWindowUpdateStream() throws Exception {
        http2Connect();

        sendSimplePostRequest(3, null, false);
        sendWindowUpdate(3, 0);

        parser.readFrame();

        Assert.assertEquals("3-RST-[" + Http2Error.PROTOCOL_ERROR.getCode() + "]\n", output.getTrace());
    }


    @Test
    public void testWindowUpdateOnClosedStream() throws Exception {
        http2Connect();

        // Should not be an error so should be nothing to read
        sendWindowUpdate(1, 200);

        // So the next request should process normally
        sendSimpleGetRequest(3);
        readSimpleGetResponse();
        Assert.assertEquals(getSimpleResponseTrace(3), output.getTrace());
    }


    // TODO: Test always accounting for changes in flow control windows even if
    // the frame is in error.


    @Test
    public void testWindowUpdateWrongLength() throws Exception {
        http2Connect();

        byte[] zeroLengthWindowFrame = new byte[9];
        // Length zero
        setOneBytes(zeroLengthWindowFrame, 3, FrameType.WINDOW_UPDATE.getIdByte());
        // No flags
        // Stream 1
        ByteUtil.set31Bits(zeroLengthWindowFrame, 5, 1);

        os.write(zeroLengthWindowFrame);
        os.flush();

        handleGoAwayResponse(1, Http2Error.FRAME_SIZE_ERROR);
    }


    @Test
    public void testEmptyDataFrameWithNoAvailableFlowControl() throws Exception {
        http2Connect();

        // Default connection window size is 64k - 1. Initial request will have
        // used 8k (56k -1).

        // Use up the remaining connection window. These requests require 48k.
        for (int i = 3; i < 15; i += 2) {
            sendSimpleGetRequest(i);
            readSimpleGetResponse();
            Assert.assertEquals(getSimpleResponseTrace(i), output.getTrace());
            output.clearTrace();
        }
        // This request requires 8k but there is only 8k-1 available
        sendSimpleGetRequest(15);
        readSimpleGetResponse();
        String expected = getResponseBodyFrameTrace(15, 200, "application/octet-stream", null, "8191", "8192");
        // No end of stream
        expected = expected.substring(0, expected.length() - "15-EndOfStream\n".length());
        Assert.assertEquals(expected, output.getTrace());
        output.clearTrace();

        // It should be possible to send a request that generates an empty
        // response at this point
        sendEmptyGetRequest(17);
        // Headers
        parser.readFrame();
        // Body
        parser.readFrame();

        // Release Stream 15 which is waiting for a single byte.
        sendWindowUpdate(0, 1024);

        Assert.assertEquals(getEmptyResponseTrace(17), output.getTrace());
    }


    @Test
    public void testWindowSizeTooLargeStream() throws Exception {
        http2Connect();

        // Set up stream 3
        sendSimplePostRequest(3, null, false);

        // Super size the flow control window.
        sendWindowUpdate(3, (1 << 31) - 1);

        parser.readFrame();

        Assert.assertEquals("3-RST-[" + Http2Error.FLOW_CONTROL_ERROR.getCode() + "]\n", output.getTrace());
    }


    @Test
    public void testWindowSizeTooLargeConnection() throws Exception {
        http2Connect();

        // Super size the flow control window.
        sendWindowUpdate(0, (1 << 31) - 1);

        handleGoAwayResponse(1, Http2Error.FLOW_CONTROL_ERROR);
    }


    @Test
    public void testWindowSizeAndSettingsFrame() throws Exception {
        http2Connect();

        // Disable overhead protection for window update as it breaks the test
        http2Protocol.setOverheadWindowUpdateThreshold(0);

        // Set up a POST request that echoes the body back
        byte[] headersFrameHeader = new byte[9];
        ByteBuffer headersPayload = ByteBuffer.allocate(128);
        byte[] dataFrameHeader = new byte[9];
        ByteBuffer dataPayload = ByteBuffer.allocate(8 * 1024);

        buildPostRequest(headersFrameHeader, headersPayload, false, dataFrameHeader, dataPayload, null, 3);

        // Write the headers
        writeFrame(headersFrameHeader, headersPayload);

        // Now use a settings frame to reduce the size of the flow control
        // window.
        sendSettings(0, false, new SettingValue(4, 4 * 1024));
        // Ack
        parser.readFrame();
        Assert.assertEquals("0-Settings-Ack\n", output.getTrace());
        output.clearTrace();

        // Write the body
        writeFrame(dataFrameHeader, dataPayload);

        // Window size updates after reading POST body
        parser.readFrame();
        parser.readFrame();
        Assert.assertEquals("0-WindowSize-[8192]\n" + "3-WindowSize-[8192]\n", output.getTrace());
        output.clearTrace();

        // Read stream 3 headers and first part of body
        parser.readFrame();
        parser.readFrame();
        Assert.assertEquals(
                "3-HeadersStart\n" + "3-Header-[:status]-[200]\n" + "3-Header-[content-length]-[8192]\n" +
                        "3-Header-[date]-[" + DEFAULT_DATE + "]\n" + "3-HeadersEnd\n" + "3-Body-4096\n",
                output.getTrace());
        output.clearTrace();

        // Now use a settings frame to further reduce the size of the flow
        // control window. This should make the stream 3 window negative
        sendSettings(0, false, new SettingValue(4, 2 * 1024));
        // Ack
        parser.readFrame();
        Assert.assertEquals("0-Settings-Ack\n", output.getTrace());
        output.clearTrace();

        // Now use a settings frame to increase the size of the flow control
        // window. The stream 3 window should still be negative
        sendSettings(0, false, new SettingValue(4, 3 * 1024));
        // Ack
        parser.readFrame();
        Assert.assertEquals("0-Settings-Ack\n", output.getTrace());
        output.clearTrace();

        // Do a POST that won't be affected by the above limit
        sendSimplePostRequest(5, null);
        // Window size updates after reading POST body
        parser.readFrame();
        parser.readFrame();
        Assert.assertEquals("0-WindowSize-[128]\n" + "5-WindowSize-[128]\n", output.getTrace());
        output.clearTrace();
        // Headers + body
        parser.readFrame();
        parser.readFrame();
        Assert.assertEquals("5-HeadersStart\n" + "5-Header-[:status]-[200]\n" + "5-Header-[content-length]-[128]\n" +
                "5-Header-[date]-[Wed, 11 Nov 2015 19:18:42 GMT]\n" + "5-HeadersEnd\n" + "5-Body-128\n" +
                "5-EndOfStream\n", output.getTrace());
        output.clearTrace();

        // Now use a settings frame to restore the size of the flow control
        // window.
        sendSettings(0, false, new SettingValue(4, 64 * 1024 - 1));

        // Settings ack and stream 3 body are written from different threads.
        // Order depends on server side timing. Handle both possibilities.
        parser.readFrame();
        String trace = output.getTrace();
        String settingsAck = "0-Settings-Ack\n";
        String endOfStreamThree = "3-Body-4096\n3-EndOfStream\n";

        if (settingsAck.equals(trace)) {
            // Ack the end of stream 3
            output.clearTrace();
            parser.readFrame();
            Assert.assertEquals(endOfStreamThree, output.getTrace());
        } else {
            // End of stream 3 thenack
            Assert.assertEquals(endOfStreamThree, output.getTrace());
            output.clearTrace();
            parser.readFrame();
            Assert.assertEquals(settingsAck, output.getTrace());
        }
        output.clearTrace();
    }


    @Test
    public void testWindowSizeTooLargeViaSettings() throws Exception {
        http2Connect();

        // Set up stream 3
        sendSimplePostRequest(3, null, false);

        // Increase the flow control window but keep it under the limit
        sendWindowUpdate(3, 1 << 30);

        // Now increase beyond the limit via a settings frame
        sendSettings(0, false, new SettingValue(4, 1 << 30));
        // Ack
        parser.readFrame();
        Assert.assertEquals("3-RST-[" + Http2Error.FLOW_CONTROL_ERROR.getCode() + "]\n", output.getTrace());

    }
}