|
| 1 | +// Copyright 2024 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.24 && goexperiment.synctest |
| 6 | + |
| 7 | +package http3 |
| 8 | + |
| 9 | +import ( |
| 10 | + "testing" |
| 11 | + "testing/synctest" |
| 12 | +) |
| 13 | + |
| 14 | +// Tests which apply to both client and server connections. |
| 15 | + |
| 16 | +func TestConnCreatesControlStream(t *testing.T) { |
| 17 | + runConnTest(t, func(t testing.TB, tc *testQUICConn) { |
| 18 | + controlStream := tc.wantStream(streamTypeControl) |
| 19 | + controlStream.wantFrameHeader( |
| 20 | + "server sends SETTINGS frame on control stream", |
| 21 | + frameTypeSettings) |
| 22 | + controlStream.discardFrame() |
| 23 | + }) |
| 24 | +} |
| 25 | + |
| 26 | +func TestConnUnknownUnidirectionalStream(t *testing.T) { |
| 27 | + // "Recipients of unknown stream types MUST either abort reading of the stream |
| 28 | + // or discard incoming data without further processing." |
| 29 | + // https://www.rfc-editor.org/rfc/rfc9114.html#section-6.2-7 |
| 30 | + runConnTest(t, func(t testing.TB, tc *testQUICConn) { |
| 31 | + st := tc.newStream(0x21) // reserved stream type |
| 32 | + |
| 33 | + // The endpoint should send a STOP_SENDING for this stream, |
| 34 | + // but it should not close the connection. |
| 35 | + synctest.Wait() |
| 36 | + if _, err := st.Write([]byte("hello")); err == nil { |
| 37 | + t.Fatalf("write to send-only stream with an unknown type succeeded; want error") |
| 38 | + } |
| 39 | + tc.wantNotClosed("after receiving unknown unidirectional stream type") |
| 40 | + }) |
| 41 | +} |
| 42 | + |
| 43 | +func TestConnUnknownSettings(t *testing.T) { |
| 44 | + // "An implementation MUST ignore any [settings] parameter with |
| 45 | + // an identifier it does not understand." |
| 46 | + // https://www.rfc-editor.org/rfc/rfc9114.html#section-7.2.4-9 |
| 47 | + runConnTest(t, func(t testing.TB, tc *testQUICConn) { |
| 48 | + controlStream := tc.newStream(streamTypeControl) |
| 49 | + controlStream.writeSettings(0x1f+0x21, 0) // reserved settings type |
| 50 | + controlStream.Flush() |
| 51 | + tc.wantNotClosed("after receiving unknown settings") |
| 52 | + }) |
| 53 | +} |
| 54 | + |
| 55 | +func TestConnInvalidSettings(t *testing.T) { |
| 56 | + // "These reserved settings MUST NOT be sent, and their receipt MUST |
| 57 | + // be treated as a connection error of type H3_SETTINGS_ERROR." |
| 58 | + // https://www.rfc-editor.org/rfc/rfc9114.html#section-7.2.4.1-5 |
| 59 | + runConnTest(t, func(t testing.TB, tc *testQUICConn) { |
| 60 | + controlStream := tc.newStream(streamTypeControl) |
| 61 | + controlStream.writeSettings(0x02, 0) // HTTP/2 SETTINGS_ENABLE_PUSH |
| 62 | + controlStream.Flush() |
| 63 | + tc.wantClosed("invalid setting", errH3SettingsError) |
| 64 | + }) |
| 65 | +} |
| 66 | + |
| 67 | +func TestConnDuplicateStream(t *testing.T) { |
| 68 | + for _, stype := range []streamType{ |
| 69 | + streamTypeControl, |
| 70 | + streamTypeEncoder, |
| 71 | + streamTypeDecoder, |
| 72 | + } { |
| 73 | + t.Run(stype.String(), func(t *testing.T) { |
| 74 | + runConnTest(t, func(t testing.TB, tc *testQUICConn) { |
| 75 | + _ = tc.newStream(stype) |
| 76 | + tc.wantNotClosed("after creating one " + stype.String() + " stream") |
| 77 | + |
| 78 | + // Opening a second control, encoder, or decoder stream |
| 79 | + // is a protocol violation. |
| 80 | + _ = tc.newStream(stype) |
| 81 | + tc.wantClosed("duplicate stream", errH3StreamCreationError) |
| 82 | + }) |
| 83 | + }) |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +func TestConnUnknownFrames(t *testing.T) { |
| 88 | + for _, stype := range []streamType{ |
| 89 | + streamTypeControl, |
| 90 | + } { |
| 91 | + t.Run(stype.String(), func(t *testing.T) { |
| 92 | + runConnTest(t, func(t testing.TB, tc *testQUICConn) { |
| 93 | + st := tc.newStream(stype) |
| 94 | + |
| 95 | + if stype == streamTypeControl { |
| 96 | + // First frame on the control stream must be settings. |
| 97 | + st.writeVarint(int64(frameTypeSettings)) |
| 98 | + st.writeVarint(0) // size |
| 99 | + } |
| 100 | + |
| 101 | + data := "frame content" |
| 102 | + st.writeVarint(0x1f + 0x21) // reserved frame type |
| 103 | + st.writeVarint(int64(len(data))) // size |
| 104 | + st.Write([]byte(data)) |
| 105 | + st.Flush() |
| 106 | + |
| 107 | + tc.wantNotClosed("after writing unknown frame") |
| 108 | + }) |
| 109 | + }) |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | +func TestConnInvalidFrames(t *testing.T) { |
| 114 | + runConnTest(t, func(t testing.TB, tc *testQUICConn) { |
| 115 | + control := tc.newStream(streamTypeControl) |
| 116 | + |
| 117 | + // SETTINGS frame. |
| 118 | + control.writeVarint(int64(frameTypeSettings)) |
| 119 | + control.writeVarint(0) // size |
| 120 | + |
| 121 | + // DATA frame (invalid on the control stream). |
| 122 | + control.writeVarint(int64(frameTypeData)) |
| 123 | + control.writeVarint(0) // size |
| 124 | + control.Flush() |
| 125 | + tc.wantClosed("after writing DATA frame to control stream", errH3FrameUnexpected) |
| 126 | + }) |
| 127 | +} |
| 128 | + |
| 129 | +func TestConnPeerCreatesBadUnidirectionalStream(t *testing.T) { |
| 130 | + runConnTest(t, func(t testing.TB, tc *testQUICConn) { |
| 131 | + // Create and close a stream without sending the unidirectional stream header. |
| 132 | + qs, err := tc.qconn.NewSendOnlyStream(canceledCtx) |
| 133 | + if err != nil { |
| 134 | + t.Fatal(err) |
| 135 | + } |
| 136 | + st := newTestQUICStream(tc.t, newStream(qs)) |
| 137 | + st.stream.stream.Close() |
| 138 | + |
| 139 | + tc.wantClosed("after peer creates and closes uni stream", errH3StreamCreationError) |
| 140 | + }) |
| 141 | +} |
| 142 | + |
| 143 | +func runConnTest(t *testing.T, f func(testing.TB, *testQUICConn)) { |
| 144 | + t.Helper() |
| 145 | + runSynctestSubtest(t, "client", func(t testing.TB) { |
| 146 | + tc := newTestClientConn(t) |
| 147 | + f(t, tc.testQUICConn) |
| 148 | + }) |
| 149 | +} |
0 commit comments