|
| 1 | +// Copyright 2023 The Go Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style |
| 3 | +// license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +//go:build go1.21 |
| 6 | + |
| 7 | +package quic |
| 8 | + |
| 9 | +import "testing" |
| 10 | + |
| 11 | +func TestConnInflowReturnOnRead(t *testing.T) { |
| 12 | + ctx := canceledContext() |
| 13 | + tc, s := newTestConnAndRemoteStream(t, serverSide, uniStream, func(c *Config) { |
| 14 | + c.MaxConnReadBufferSize = 64 |
| 15 | + }) |
| 16 | + tc.writeFrames(packetType1RTT, debugFrameStream{ |
| 17 | + id: s.id, |
| 18 | + data: make([]byte, 64), |
| 19 | + }) |
| 20 | + const readSize = 8 |
| 21 | + if n, err := s.ReadContext(ctx, make([]byte, readSize)); n != readSize || err != nil { |
| 22 | + t.Fatalf("s.Read() = %v, %v; want %v, nil", n, err, readSize) |
| 23 | + } |
| 24 | + tc.wantFrame("available window increases, send a MAX_DATA", |
| 25 | + packetType1RTT, debugFrameMaxData{ |
| 26 | + max: 64 + readSize, |
| 27 | + }) |
| 28 | + if n, err := s.ReadContext(ctx, make([]byte, 64)); n != 64-readSize || err != nil { |
| 29 | + t.Fatalf("s.Read() = %v, %v; want %v, nil", n, err, 64-readSize) |
| 30 | + } |
| 31 | + tc.wantFrame("available window increases, send a MAX_DATA", |
| 32 | + packetType1RTT, debugFrameMaxData{ |
| 33 | + max: 128, |
| 34 | + }) |
| 35 | +} |
| 36 | + |
| 37 | +func TestConnInflowReturnOnClose(t *testing.T) { |
| 38 | + tc, s := newTestConnAndRemoteStream(t, serverSide, uniStream, func(c *Config) { |
| 39 | + c.MaxConnReadBufferSize = 64 |
| 40 | + }) |
| 41 | + tc.ignoreFrame(frameTypeStopSending) |
| 42 | + tc.writeFrames(packetType1RTT, debugFrameStream{ |
| 43 | + id: s.id, |
| 44 | + data: make([]byte, 64), |
| 45 | + }) |
| 46 | + s.CloseRead() |
| 47 | + tc.wantFrame("closing stream updates connection-level flow control", |
| 48 | + packetType1RTT, debugFrameMaxData{ |
| 49 | + max: 128, |
| 50 | + }) |
| 51 | +} |
| 52 | + |
| 53 | +func TestConnInflowReturnOnReset(t *testing.T) { |
| 54 | + tc, s := newTestConnAndRemoteStream(t, serverSide, uniStream, func(c *Config) { |
| 55 | + c.MaxConnReadBufferSize = 64 |
| 56 | + }) |
| 57 | + tc.ignoreFrame(frameTypeStopSending) |
| 58 | + tc.writeFrames(packetType1RTT, debugFrameStream{ |
| 59 | + id: s.id, |
| 60 | + data: make([]byte, 32), |
| 61 | + }) |
| 62 | + tc.writeFrames(packetType1RTT, debugFrameResetStream{ |
| 63 | + id: s.id, |
| 64 | + finalSize: 64, |
| 65 | + }) |
| 66 | + s.CloseRead() |
| 67 | + tc.wantFrame("receiving stream reseet updates connection-level flow control", |
| 68 | + packetType1RTT, debugFrameMaxData{ |
| 69 | + max: 128, |
| 70 | + }) |
| 71 | +} |
| 72 | + |
| 73 | +func TestConnInflowStreamViolation(t *testing.T) { |
| 74 | + tc := newTestConn(t, serverSide, func(c *Config) { |
| 75 | + c.MaxConnReadBufferSize = 100 |
| 76 | + }) |
| 77 | + tc.handshake() |
| 78 | + tc.ignoreFrame(frameTypeAck) |
| 79 | + // Total MAX_DATA consumed: 50 |
| 80 | + tc.writeFrames(packetType1RTT, debugFrameStream{ |
| 81 | + id: newStreamID(clientSide, bidiStream, 0), |
| 82 | + data: make([]byte, 50), |
| 83 | + }) |
| 84 | + // Total MAX_DATA consumed: 80 |
| 85 | + tc.writeFrames(packetType1RTT, debugFrameStream{ |
| 86 | + id: newStreamID(clientSide, uniStream, 0), |
| 87 | + off: 20, |
| 88 | + data: make([]byte, 10), |
| 89 | + }) |
| 90 | + // Total MAX_DATA consumed: 100 |
| 91 | + tc.writeFrames(packetType1RTT, debugFrameStream{ |
| 92 | + id: newStreamID(clientSide, bidiStream, 0), |
| 93 | + off: 70, |
| 94 | + fin: true, |
| 95 | + }) |
| 96 | + // This stream has already consumed quota for these bytes. |
| 97 | + // Total MAX_DATA consumed: 100 |
| 98 | + tc.writeFrames(packetType1RTT, debugFrameStream{ |
| 99 | + id: newStreamID(clientSide, uniStream, 0), |
| 100 | + data: make([]byte, 20), |
| 101 | + }) |
| 102 | + tc.wantIdle("peer has consumed all MAX_DATA quota") |
| 103 | + |
| 104 | + // Total MAX_DATA consumed: 101 |
| 105 | + tc.writeFrames(packetType1RTT, debugFrameStream{ |
| 106 | + id: newStreamID(clientSide, bidiStream, 2), |
| 107 | + data: make([]byte, 1), |
| 108 | + }) |
| 109 | + tc.wantFrame("peer violates MAX_DATA limit", |
| 110 | + packetType1RTT, debugFrameConnectionCloseTransport{ |
| 111 | + code: errFlowControl, |
| 112 | + }) |
| 113 | +} |
| 114 | + |
| 115 | +func TestConnInflowResetViolation(t *testing.T) { |
| 116 | + tc := newTestConn(t, serverSide, func(c *Config) { |
| 117 | + c.MaxConnReadBufferSize = 100 |
| 118 | + }) |
| 119 | + tc.handshake() |
| 120 | + tc.ignoreFrame(frameTypeAck) |
| 121 | + tc.writeFrames(packetType1RTT, debugFrameStream{ |
| 122 | + id: newStreamID(clientSide, bidiStream, 0), |
| 123 | + data: make([]byte, 100), |
| 124 | + }) |
| 125 | + tc.wantIdle("peer has consumed all MAX_DATA quota") |
| 126 | + |
| 127 | + tc.writeFrames(packetType1RTT, debugFrameResetStream{ |
| 128 | + id: newStreamID(clientSide, uniStream, 0), |
| 129 | + finalSize: 0, |
| 130 | + }) |
| 131 | + tc.wantIdle("stream reset does not consume MAX_DATA quota, no error") |
| 132 | + |
| 133 | + tc.writeFrames(packetType1RTT, debugFrameResetStream{ |
| 134 | + id: newStreamID(clientSide, uniStream, 1), |
| 135 | + finalSize: 1, |
| 136 | + }) |
| 137 | + tc.wantFrame("RESET_STREAM final size violates MAX_DATA limit", |
| 138 | + packetType1RTT, debugFrameConnectionCloseTransport{ |
| 139 | + code: errFlowControl, |
| 140 | + }) |
| 141 | +} |
| 142 | + |
| 143 | +func TestConnInflowMultipleStreams(t *testing.T) { |
| 144 | + ctx := canceledContext() |
| 145 | + tc := newTestConn(t, serverSide, func(c *Config) { |
| 146 | + c.MaxConnReadBufferSize = 128 |
| 147 | + }) |
| 148 | + tc.handshake() |
| 149 | + tc.ignoreFrame(frameTypeAck) |
| 150 | + |
| 151 | + var streams []*Stream |
| 152 | + for _, id := range []streamID{ |
| 153 | + newStreamID(clientSide, uniStream, 0), |
| 154 | + newStreamID(clientSide, uniStream, 1), |
| 155 | + newStreamID(clientSide, bidiStream, 0), |
| 156 | + newStreamID(clientSide, bidiStream, 1), |
| 157 | + } { |
| 158 | + tc.writeFrames(packetType1RTT, debugFrameStream{ |
| 159 | + id: id, |
| 160 | + data: make([]byte, 32), |
| 161 | + }) |
| 162 | + s, err := tc.conn.AcceptStream(ctx) |
| 163 | + if err != nil { |
| 164 | + t.Fatalf("AcceptStream() = %v", err) |
| 165 | + } |
| 166 | + streams = append(streams, s) |
| 167 | + if n, err := s.ReadContext(ctx, make([]byte, 1)); err != nil || n != 1 { |
| 168 | + t.Fatalf("s.Read() = %v, %v; want 1, nil", n, err) |
| 169 | + } |
| 170 | + } |
| 171 | + tc.wantIdle("streams have read data, but not enough to update MAX_DATA") |
| 172 | + |
| 173 | + if n, err := streams[0].ReadContext(ctx, make([]byte, 32)); err != nil || n != 31 { |
| 174 | + t.Fatalf("s.Read() = %v, %v; want 31, nil", n, err) |
| 175 | + } |
| 176 | + tc.wantFrame("read enough data to trigger a MAX_DATA update", |
| 177 | + packetType1RTT, debugFrameMaxData{ |
| 178 | + max: 128 + 32 + 1 + 1 + 1, |
| 179 | + }) |
| 180 | + |
| 181 | + streams[2].CloseRead() |
| 182 | + tc.wantFrame("closed stream triggers another MAX_DATA update", |
| 183 | + packetType1RTT, debugFrameMaxData{ |
| 184 | + max: 128 + 32 + 1 + 32 + 1, |
| 185 | + }) |
| 186 | +} |
0 commit comments