File: TestRfc9218.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 (219 lines) | stat: -rw-r--r-- 7,744 bytes parent folder | download | duplicates (9)
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
/*
 *  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.io.IOException;
import java.nio.charset.StandardCharsets;

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

public class TestRfc9218 extends Http2TestBase {

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

        /*
         * This test uses small window updates and data frames that will trigger the excessive overhead protection so
         * disable it.
         */
        http2Protocol.setOverheadWindowUpdateThreshold(0);
        http2Protocol.setOverheadDataThreshold(0);

        // Default connection window size is 64k - 1. Initial request will have used 8k (56k -1). Increase it to 57k.
        sendWindowUpdate(0, 1 + 1024);

        // Consume 56k of the connection window
        for (int i = 3; i < 17; i += 2) {
            sendSimpleGetRequest(i);
            readSimpleGetResponse();
        }

        String trace = output.getTrace();
        System.out.println(trace);
        output.clearTrace();

        // At this point the connection window should be 1k

        // Process a request on stream 17. This should consume the connection window.
        sendSimpleGetRequest(17);
        // 17-headers, 17-1k-body
        parser.readFrame();
        parser.readFrame();
        trace = output.getTrace();
        System.out.println(trace);
        output.clearTrace();

        // Send additional requests. Connection window is empty so only headers will be returned.
        sendSimpleGetRequest(19);
        sendSimpleGetRequest(21);

        // 19-headers, 21-headers
        parser.readFrame();
        parser.readFrame();
        trace = output.getTrace();
        System.out.println(trace);
        output.clearTrace();

        // At this point 17, 19 and 21 are all blocked because the connection window is zero.
        // 17 - 7k body left
        // 19 - 8k body left
        // 21 - 8k body left

        // Add 1k to the connection window. Should be used for stream 17.
        sendWindowUpdate(0, 1024);
        parser.readFrame();
        Assert.assertEquals("17-Body-1024\n", output.getTrace());
        output.clearTrace();

        // 17 - 6k body left
        // 19 - 8k body left
        // 21 - 8k body left

        // Re-order the priorities
        sendPriorityUpdate(19, 2, false);
        sendPriorityUpdate(21, 1, false);

        // Add 1k to the connection window. Should be used for stream 21.
        sendWindowUpdate(0, 1024);
        parser.readFrame();

        Assert.assertEquals("21-Body-1024\n", output.getTrace());
        output.clearTrace();

        // 17 - 6k body left
        // 19 - 8k body left
        // 21 - 7k body left

        // Re-order the priorities
        sendPriorityUpdate(17, 3, true);
        sendPriorityUpdate(19, 3, true);
        sendPriorityUpdate(21, 3, true);

        // Add 3k to the connection window. Should be split between 17, 19 and 21.
        sendWindowUpdate(0, 1024 * 3);
        parser.readFrame();
        parser.readFrame();
        parser.readFrame();

        trace = output.getTrace();
        Assert.assertTrue(trace.contains("17-Body-877\n"));
        trace = trace.replace("17-Body-877\n", "");
        Assert.assertTrue(trace.contains("19-Body-1170\n"));
        trace = trace.replace("19-Body-1170\n", "");
        Assert.assertTrue(trace.contains("21-Body-1024\n"));
        trace = trace.replace("21-Body-1024\n", "");
        Assert.assertEquals(0, trace.length());
        output.clearTrace();

        // 1 byte unallocated in connection window
        // 17 - 5267 body left
        // 19 - 7022 body left
        // 21 - 6144 body left

        // Add 1 byte to the connection window. Due to rounding up, each stream should get 1 byte.
        sendWindowUpdate(0, 1);
        parser.readFrame();
        parser.readFrame();
        parser.readFrame();

        trace = output.getTrace();
        Assert.assertTrue(trace.contains("17-Body-1\n"));
        trace = trace.replace("17-Body-1\n", "");
        Assert.assertTrue(trace.contains("19-Body-1\n"));
        trace = trace.replace("19-Body-1\n", "");
        Assert.assertTrue(trace.contains("21-Body-1\n"));
        trace = trace.replace("21-Body-1\n", "");
        Assert.assertEquals(0, trace.length());
        output.clearTrace();

        // 1 byte over allocated in connection window
        // 17 - 5266 body left
        // 19 - 7021 body left
        // 21 - 6143 body left

        // BZ 69614 - invalid priority update frames should be ignored
        sendInvalidPriorityUpdate(17);

        // Re-order the priorities
        sendPriorityUpdate(17, 2, true);

        /*
         * Add 8k to the connection window. Should clear the connection window over allocation and fully allocate 17
         * with the remainder split proportionally between 19 and 21.
         */
        sendWindowUpdate(0, 1024 * 8);
        // Use try/catch as third read has been failing on some tests runs
        try {
            parser.readFrame();
            parser.readFrame();
            parser.readFrame();
        } catch (IOException ioe) {
            // Dump for debugging purposes
            ioe.printStackTrace();
            // Continue - we'll get trace dumped to stdout below
        }

        trace = output.getTrace();
        System.out.println(trace);
        Assert.assertTrue(trace.contains("17-Body-5266\n"));
        trace = trace.replace("17-Body-5266\n", "");
        Assert.assertTrue(trace.contains("17-EndOfStream\n"));
        trace = trace.replace("17-EndOfStream\n", "");
        Assert.assertTrue(trace.contains("19-Body-1560\n"));
        trace = trace.replace("19-Body-1560\n", "");
        Assert.assertTrue(trace.contains("21-Body-1365\n"));
        trace = trace.replace("21-Body-1365\n", "");
        Assert.assertEquals(0, trace.length());

        // 19 - 5641 body left
        // 21 - 4778 body left

        // Add 16k to the connection window. Should fully allocate 19 and 21.
        sendWindowUpdate(0, 1024 * 16);

        try {
            parser.readFrame();
            parser.readFrame();
        } catch (IOException ioe) {
            // Dump for debugging purposes
            ioe.printStackTrace();
        }
    }


    private void sendInvalidPriorityUpdate(int streamId) throws IOException {
        byte[] payload = "u=1:i".getBytes(StandardCharsets.US_ASCII);

        byte[] priorityUpdateFrame = new byte[13 + payload.length];

        // length
        ByteUtil.setThreeBytes(priorityUpdateFrame, 0, 4 + payload.length);
        // type
        priorityUpdateFrame[3] = FrameType.PRIORITY_UPDATE.getIdByte();
        // Stream ID
        ByteUtil.set31Bits(priorityUpdateFrame, 5, 0);

        // Payload
        ByteUtil.set31Bits(priorityUpdateFrame, 9, streamId);
        System.arraycopy(payload, 0, priorityUpdateFrame, 13, payload.length);

        os.write(priorityUpdateFrame);
        os.flush();
    }
}