github.com/apernet/quic-go@v0.43.1-0.20240515053213-5e9e635fd9f0/internal/wire/log_test.go (about) 1 package wire 2 3 import ( 4 "bytes" 5 "log" 6 "os" 7 "time" 8 9 "github.com/apernet/quic-go/internal/protocol" 10 "github.com/apernet/quic-go/internal/utils" 11 12 . "github.com/onsi/ginkgo/v2" 13 . "github.com/onsi/gomega" 14 ) 15 16 var _ = Describe("Frame logging", func() { 17 var ( 18 buf *bytes.Buffer 19 logger utils.Logger 20 ) 21 22 BeforeEach(func() { 23 buf = &bytes.Buffer{} 24 logger = utils.DefaultLogger 25 logger.SetLogLevel(utils.LogLevelDebug) 26 log.SetOutput(buf) 27 }) 28 29 AfterEach(func() { 30 log.SetOutput(os.Stdout) 31 }) 32 33 It("doesn't log when debug is disabled", func() { 34 logger.SetLogLevel(utils.LogLevelInfo) 35 LogFrame(logger, &ResetStreamFrame{}, true) 36 Expect(buf.Len()).To(BeZero()) 37 }) 38 39 It("logs sent frames", func() { 40 LogFrame(logger, &ResetStreamFrame{}, true) 41 Expect(buf.String()).To(ContainSubstring("\t-> &wire.ResetStreamFrame{StreamID: 0, ErrorCode: 0x0, FinalSize: 0}\n")) 42 }) 43 44 It("logs received frames", func() { 45 LogFrame(logger, &ResetStreamFrame{}, false) 46 Expect(buf.String()).To(ContainSubstring("\t<- &wire.ResetStreamFrame{StreamID: 0, ErrorCode: 0x0, FinalSize: 0}\n")) 47 }) 48 49 It("logs CRYPTO frames", func() { 50 frame := &CryptoFrame{ 51 Offset: 42, 52 Data: make([]byte, 123), 53 } 54 LogFrame(logger, frame, false) 55 Expect(buf.String()).To(ContainSubstring("\t<- &wire.CryptoFrame{Offset: 42, Data length: 123, Offset + Data length: 165}\n")) 56 }) 57 58 It("logs STREAM frames", func() { 59 frame := &StreamFrame{ 60 StreamID: 42, 61 Offset: 1337, 62 Data: bytes.Repeat([]byte{'f'}, 100), 63 } 64 LogFrame(logger, frame, false) 65 Expect(buf.String()).To(ContainSubstring("\t<- &wire.StreamFrame{StreamID: 42, Fin: false, Offset: 1337, Data length: 100, Offset + Data length: 1437}\n")) 66 }) 67 68 It("logs ACK frames without missing packets", func() { 69 frame := &AckFrame{ 70 AckRanges: []AckRange{{Smallest: 42, Largest: 1337}}, 71 DelayTime: 1 * time.Millisecond, 72 } 73 LogFrame(logger, frame, false) 74 Expect(buf.String()).To(ContainSubstring("\t<- &wire.AckFrame{LargestAcked: 1337, LowestAcked: 42, DelayTime: 1ms}\n")) 75 }) 76 77 It("logs ACK frames with ECN", func() { 78 frame := &AckFrame{ 79 AckRanges: []AckRange{{Smallest: 42, Largest: 1337}}, 80 DelayTime: 1 * time.Millisecond, 81 ECT0: 5, 82 ECT1: 66, 83 ECNCE: 777, 84 } 85 LogFrame(logger, frame, false) 86 Expect(buf.String()).To(ContainSubstring("\t<- &wire.AckFrame{LargestAcked: 1337, LowestAcked: 42, DelayTime: 1ms, ECT0: 5, ECT1: 66, CE: 777}\n")) 87 }) 88 89 It("logs ACK frames with missing packets", func() { 90 frame := &AckFrame{ 91 AckRanges: []AckRange{ 92 {Smallest: 5, Largest: 8}, 93 {Smallest: 2, Largest: 3}, 94 }, 95 DelayTime: 12 * time.Millisecond, 96 } 97 LogFrame(logger, frame, false) 98 Expect(buf.String()).To(ContainSubstring("\t<- &wire.AckFrame{LargestAcked: 8, LowestAcked: 2, AckRanges: {{Largest: 8, Smallest: 5}, {Largest: 3, Smallest: 2}}, DelayTime: 12ms}\n")) 99 }) 100 101 It("logs MAX_STREAMS frames", func() { 102 frame := &MaxStreamsFrame{ 103 Type: protocol.StreamTypeBidi, 104 MaxStreamNum: 42, 105 } 106 LogFrame(logger, frame, false) 107 Expect(buf.String()).To(ContainSubstring("\t<- &wire.MaxStreamsFrame{Type: bidi, MaxStreamNum: 42}\n")) 108 }) 109 110 It("logs MAX_DATA frames", func() { 111 frame := &MaxDataFrame{ 112 MaximumData: 42, 113 } 114 LogFrame(logger, frame, false) 115 Expect(buf.String()).To(ContainSubstring("\t<- &wire.MaxDataFrame{MaximumData: 42}\n")) 116 }) 117 118 It("logs MAX_STREAM_DATA frames", func() { 119 frame := &MaxStreamDataFrame{ 120 StreamID: 10, 121 MaximumStreamData: 42, 122 } 123 LogFrame(logger, frame, false) 124 Expect(buf.String()).To(ContainSubstring("\t<- &wire.MaxStreamDataFrame{StreamID: 10, MaximumStreamData: 42}\n")) 125 }) 126 127 It("logs DATA_BLOCKED frames", func() { 128 frame := &DataBlockedFrame{ 129 MaximumData: 1000, 130 } 131 LogFrame(logger, frame, false) 132 Expect(buf.String()).To(ContainSubstring("\t<- &wire.DataBlockedFrame{MaximumData: 1000}\n")) 133 }) 134 135 It("logs STREAM_DATA_BLOCKED frames", func() { 136 frame := &StreamDataBlockedFrame{ 137 StreamID: 42, 138 MaximumStreamData: 1000, 139 } 140 LogFrame(logger, frame, false) 141 Expect(buf.String()).To(ContainSubstring("\t<- &wire.StreamDataBlockedFrame{StreamID: 42, MaximumStreamData: 1000}\n")) 142 }) 143 144 It("logs STREAMS_BLOCKED frames", func() { 145 frame := &StreamsBlockedFrame{ 146 Type: protocol.StreamTypeBidi, 147 StreamLimit: 42, 148 } 149 LogFrame(logger, frame, false) 150 Expect(buf.String()).To(ContainSubstring("\t<- &wire.StreamsBlockedFrame{Type: bidi, MaxStreams: 42}\n")) 151 }) 152 153 It("logs NEW_CONNECTION_ID frames", func() { 154 LogFrame(logger, &NewConnectionIDFrame{ 155 SequenceNumber: 42, 156 RetirePriorTo: 24, 157 ConnectionID: protocol.ParseConnectionID([]byte{0xde, 0xad, 0xbe, 0xef}), 158 StatelessResetToken: protocol.StatelessResetToken{0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf, 0x10}, 159 }, false) 160 Expect(buf.String()).To(ContainSubstring("\t<- &wire.NewConnectionIDFrame{SequenceNumber: 42, RetirePriorTo: 24, ConnectionID: deadbeef, StatelessResetToken: 0x0102030405060708090a0b0c0d0e0f10}")) 161 }) 162 163 It("logs RETIRE_CONNECTION_ID frames", func() { 164 LogFrame(logger, &RetireConnectionIDFrame{SequenceNumber: 42}, false) 165 Expect(buf.String()).To(ContainSubstring("\t<- &wire.RetireConnectionIDFrame{SequenceNumber: 42}")) 166 }) 167 168 It("logs NEW_TOKEN frames", func() { 169 LogFrame(logger, &NewTokenFrame{ 170 Token: []byte{0xde, 0xad, 0xbe, 0xef}, 171 }, true) 172 Expect(buf.String()).To(ContainSubstring("\t-> &wire.NewTokenFrame{Token: 0xdeadbeef")) 173 }) 174 })