github.com/MerlinKodo/quic-go@v0.39.2/logging/multiplex_test.go (about)

     1  package logging_test
     2  
     3  import (
     4  	"errors"
     5  	"net"
     6  	"time"
     7  
     8  	mocklogging "github.com/MerlinKodo/quic-go/internal/mocks/logging"
     9  	"github.com/MerlinKodo/quic-go/internal/protocol"
    10  	"github.com/MerlinKodo/quic-go/internal/wire"
    11  	. "github.com/MerlinKodo/quic-go/logging"
    12  
    13  	. "github.com/onsi/ginkgo/v2"
    14  	. "github.com/onsi/gomega"
    15  )
    16  
    17  var _ = Describe("Tracing", func() {
    18  	Context("Tracer", func() {
    19  		It("returns a nil tracer if no tracers are passed in", func() {
    20  			Expect(NewMultiplexedTracer()).To(BeNil())
    21  		})
    22  
    23  		It("returns the raw tracer if only one tracer is passed in", func() {
    24  			tr := &Tracer{}
    25  			tracer := NewMultiplexedTracer(tr)
    26  			Expect(tracer).To(Equal(tr))
    27  		})
    28  
    29  		Context("tracing events", func() {
    30  			var (
    31  				tracer   *Tracer
    32  				tr1, tr2 *mocklogging.MockTracer
    33  			)
    34  
    35  			BeforeEach(func() {
    36  				var t1, t2 *Tracer
    37  				t1, tr1 = mocklogging.NewMockTracer(mockCtrl)
    38  				t2, tr2 = mocklogging.NewMockTracer(mockCtrl)
    39  				tracer = NewMultiplexedTracer(t1, t2, &Tracer{})
    40  			})
    41  
    42  			It("traces the PacketSent event", func() {
    43  				remote := &net.UDPAddr{IP: net.IPv4(4, 3, 2, 1)}
    44  				hdr := &Header{DestConnectionID: protocol.ParseConnectionID([]byte{1, 2, 3})}
    45  				f := &MaxDataFrame{MaximumData: 1337}
    46  				tr1.EXPECT().SentPacket(remote, hdr, ByteCount(1024), []Frame{f})
    47  				tr2.EXPECT().SentPacket(remote, hdr, ByteCount(1024), []Frame{f})
    48  				tracer.SentPacket(remote, hdr, 1024, []Frame{f})
    49  			})
    50  
    51  			It("traces the PacketSent event", func() {
    52  				remote := &net.UDPAddr{IP: net.IPv4(4, 3, 2, 1)}
    53  				src := ArbitraryLenConnectionID{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13}
    54  				dest := ArbitraryLenConnectionID{1, 2, 3, 4}
    55  				versions := []VersionNumber{1, 2, 3}
    56  				tr1.EXPECT().SentVersionNegotiationPacket(remote, dest, src, versions)
    57  				tr2.EXPECT().SentVersionNegotiationPacket(remote, dest, src, versions)
    58  				tracer.SentVersionNegotiationPacket(remote, dest, src, versions)
    59  			})
    60  
    61  			It("traces the PacketDropped event", func() {
    62  				remote := &net.UDPAddr{IP: net.IPv4(4, 3, 2, 1)}
    63  				tr1.EXPECT().DroppedPacket(remote, PacketTypeRetry, ByteCount(1024), PacketDropDuplicate)
    64  				tr2.EXPECT().DroppedPacket(remote, PacketTypeRetry, ByteCount(1024), PacketDropDuplicate)
    65  				tracer.DroppedPacket(remote, PacketTypeRetry, 1024, PacketDropDuplicate)
    66  			})
    67  		})
    68  	})
    69  
    70  	Context("Connection Tracer", func() {
    71  		var (
    72  			tracer *ConnectionTracer
    73  			tr1    *mocklogging.MockConnectionTracer
    74  			tr2    *mocklogging.MockConnectionTracer
    75  		)
    76  
    77  		BeforeEach(func() {
    78  			var t1, t2 *ConnectionTracer
    79  			t1, tr1 = mocklogging.NewMockConnectionTracer(mockCtrl)
    80  			t2, tr2 = mocklogging.NewMockConnectionTracer(mockCtrl)
    81  			tracer = NewMultiplexedConnectionTracer(t1, t2)
    82  		})
    83  
    84  		It("traces the StartedConnection event", func() {
    85  			local := &net.UDPAddr{IP: net.IPv4(1, 2, 3, 4)}
    86  			remote := &net.UDPAddr{IP: net.IPv4(4, 3, 2, 1)}
    87  			dest := protocol.ParseConnectionID([]byte{1, 2, 3, 4})
    88  			src := protocol.ParseConnectionID([]byte{4, 3, 2, 1})
    89  			tr1.EXPECT().StartedConnection(local, remote, src, dest)
    90  			tr2.EXPECT().StartedConnection(local, remote, src, dest)
    91  			tracer.StartedConnection(local, remote, src, dest)
    92  		})
    93  
    94  		It("traces the NegotiatedVersion event", func() {
    95  			chosen := protocol.Version2
    96  			client := []protocol.VersionNumber{protocol.Version1}
    97  			server := []protocol.VersionNumber{13, 37}
    98  			tr1.EXPECT().NegotiatedVersion(chosen, client, server)
    99  			tr2.EXPECT().NegotiatedVersion(chosen, client, server)
   100  			tracer.NegotiatedVersion(chosen, client, server)
   101  		})
   102  
   103  		It("traces the ClosedConnection event", func() {
   104  			e := errors.New("test err")
   105  			tr1.EXPECT().ClosedConnection(e)
   106  			tr2.EXPECT().ClosedConnection(e)
   107  			tracer.ClosedConnection(e)
   108  		})
   109  
   110  		It("traces the SentTransportParameters event", func() {
   111  			tp := &wire.TransportParameters{InitialMaxData: 1337}
   112  			tr1.EXPECT().SentTransportParameters(tp)
   113  			tr2.EXPECT().SentTransportParameters(tp)
   114  			tracer.SentTransportParameters(tp)
   115  		})
   116  
   117  		It("traces the ReceivedTransportParameters event", func() {
   118  			tp := &wire.TransportParameters{InitialMaxData: 1337}
   119  			tr1.EXPECT().ReceivedTransportParameters(tp)
   120  			tr2.EXPECT().ReceivedTransportParameters(tp)
   121  			tracer.ReceivedTransportParameters(tp)
   122  		})
   123  
   124  		It("traces the RestoredTransportParameters event", func() {
   125  			tp := &wire.TransportParameters{InitialMaxData: 1337}
   126  			tr1.EXPECT().RestoredTransportParameters(tp)
   127  			tr2.EXPECT().RestoredTransportParameters(tp)
   128  			tracer.RestoredTransportParameters(tp)
   129  		})
   130  
   131  		It("traces the SentLongHeaderPacket event", func() {
   132  			hdr := &ExtendedHeader{Header: Header{DestConnectionID: protocol.ParseConnectionID([]byte{1, 2, 3})}}
   133  			ack := &AckFrame{AckRanges: []AckRange{{Smallest: 1, Largest: 10}}}
   134  			ping := &PingFrame{}
   135  			tr1.EXPECT().SentLongHeaderPacket(hdr, ByteCount(1337), ECTNot, ack, []Frame{ping})
   136  			tr2.EXPECT().SentLongHeaderPacket(hdr, ByteCount(1337), ECTNot, ack, []Frame{ping})
   137  			tracer.SentLongHeaderPacket(hdr, 1337, ECTNot, ack, []Frame{ping})
   138  		})
   139  
   140  		It("traces the SentShortHeaderPacket event", func() {
   141  			hdr := &ShortHeader{DestConnectionID: protocol.ParseConnectionID([]byte{1, 2, 3})}
   142  			ack := &AckFrame{AckRanges: []AckRange{{Smallest: 1, Largest: 10}}}
   143  			ping := &PingFrame{}
   144  			tr1.EXPECT().SentShortHeaderPacket(hdr, ByteCount(1337), ECNCE, ack, []Frame{ping})
   145  			tr2.EXPECT().SentShortHeaderPacket(hdr, ByteCount(1337), ECNCE, ack, []Frame{ping})
   146  			tracer.SentShortHeaderPacket(hdr, 1337, ECNCE, ack, []Frame{ping})
   147  		})
   148  
   149  		It("traces the ReceivedVersionNegotiationPacket event", func() {
   150  			src := ArbitraryLenConnectionID{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13}
   151  			dest := ArbitraryLenConnectionID{1, 2, 3, 4}
   152  			tr1.EXPECT().ReceivedVersionNegotiationPacket(dest, src, []VersionNumber{1337})
   153  			tr2.EXPECT().ReceivedVersionNegotiationPacket(dest, src, []VersionNumber{1337})
   154  			tracer.ReceivedVersionNegotiationPacket(dest, src, []VersionNumber{1337})
   155  		})
   156  
   157  		It("traces the ReceivedRetry event", func() {
   158  			hdr := &Header{DestConnectionID: protocol.ParseConnectionID([]byte{1, 2, 3})}
   159  			tr1.EXPECT().ReceivedRetry(hdr)
   160  			tr2.EXPECT().ReceivedRetry(hdr)
   161  			tracer.ReceivedRetry(hdr)
   162  		})
   163  
   164  		It("traces the ReceivedLongHeaderPacket event", func() {
   165  			hdr := &ExtendedHeader{Header: Header{DestConnectionID: protocol.ParseConnectionID([]byte{1, 2, 3})}}
   166  			ping := &PingFrame{}
   167  			tr1.EXPECT().ReceivedLongHeaderPacket(hdr, ByteCount(1337), ECT1, []Frame{ping})
   168  			tr2.EXPECT().ReceivedLongHeaderPacket(hdr, ByteCount(1337), ECT1, []Frame{ping})
   169  			tracer.ReceivedLongHeaderPacket(hdr, 1337, ECT1, []Frame{ping})
   170  		})
   171  
   172  		It("traces the ReceivedShortHeaderPacket event", func() {
   173  			hdr := &ShortHeader{DestConnectionID: protocol.ParseConnectionID([]byte{1, 2, 3})}
   174  			ping := &PingFrame{}
   175  			tr1.EXPECT().ReceivedShortHeaderPacket(hdr, ByteCount(1337), ECT0, []Frame{ping})
   176  			tr2.EXPECT().ReceivedShortHeaderPacket(hdr, ByteCount(1337), ECT0, []Frame{ping})
   177  			tracer.ReceivedShortHeaderPacket(hdr, 1337, ECT0, []Frame{ping})
   178  		})
   179  
   180  		It("traces the BufferedPacket event", func() {
   181  			tr1.EXPECT().BufferedPacket(PacketTypeHandshake, ByteCount(1337))
   182  			tr2.EXPECT().BufferedPacket(PacketTypeHandshake, ByteCount(1337))
   183  			tracer.BufferedPacket(PacketTypeHandshake, 1337)
   184  		})
   185  
   186  		It("traces the DroppedPacket event", func() {
   187  			tr1.EXPECT().DroppedPacket(PacketTypeInitial, ByteCount(1337), PacketDropHeaderParseError)
   188  			tr2.EXPECT().DroppedPacket(PacketTypeInitial, ByteCount(1337), PacketDropHeaderParseError)
   189  			tracer.DroppedPacket(PacketTypeInitial, 1337, PacketDropHeaderParseError)
   190  		})
   191  
   192  		It("traces the UpdatedCongestionState event", func() {
   193  			tr1.EXPECT().UpdatedCongestionState(CongestionStateRecovery)
   194  			tr2.EXPECT().UpdatedCongestionState(CongestionStateRecovery)
   195  			tracer.UpdatedCongestionState(CongestionStateRecovery)
   196  		})
   197  
   198  		It("traces the UpdatedMetrics event", func() {
   199  			rttStats := &RTTStats{}
   200  			rttStats.UpdateRTT(time.Second, 0, time.Now())
   201  			tr1.EXPECT().UpdatedMetrics(rttStats, ByteCount(1337), ByteCount(42), 13)
   202  			tr2.EXPECT().UpdatedMetrics(rttStats, ByteCount(1337), ByteCount(42), 13)
   203  			tracer.UpdatedMetrics(rttStats, 1337, 42, 13)
   204  		})
   205  
   206  		It("traces the AcknowledgedPacket event", func() {
   207  			tr1.EXPECT().AcknowledgedPacket(EncryptionHandshake, PacketNumber(42))
   208  			tr2.EXPECT().AcknowledgedPacket(EncryptionHandshake, PacketNumber(42))
   209  			tracer.AcknowledgedPacket(EncryptionHandshake, 42)
   210  		})
   211  
   212  		It("traces the LostPacket event", func() {
   213  			tr1.EXPECT().LostPacket(EncryptionHandshake, PacketNumber(42), PacketLossReorderingThreshold)
   214  			tr2.EXPECT().LostPacket(EncryptionHandshake, PacketNumber(42), PacketLossReorderingThreshold)
   215  			tracer.LostPacket(EncryptionHandshake, 42, PacketLossReorderingThreshold)
   216  		})
   217  
   218  		It("traces the UpdatedPTOCount event", func() {
   219  			tr1.EXPECT().UpdatedPTOCount(uint32(88))
   220  			tr2.EXPECT().UpdatedPTOCount(uint32(88))
   221  			tracer.UpdatedPTOCount(88)
   222  		})
   223  
   224  		It("traces the UpdatedKeyFromTLS event", func() {
   225  			tr1.EXPECT().UpdatedKeyFromTLS(EncryptionHandshake, PerspectiveClient)
   226  			tr2.EXPECT().UpdatedKeyFromTLS(EncryptionHandshake, PerspectiveClient)
   227  			tracer.UpdatedKeyFromTLS(EncryptionHandshake, PerspectiveClient)
   228  		})
   229  
   230  		It("traces the UpdatedKey event", func() {
   231  			tr1.EXPECT().UpdatedKey(KeyPhase(42), true)
   232  			tr2.EXPECT().UpdatedKey(KeyPhase(42), true)
   233  			tracer.UpdatedKey(KeyPhase(42), true)
   234  		})
   235  
   236  		It("traces the DroppedEncryptionLevel event", func() {
   237  			tr1.EXPECT().DroppedEncryptionLevel(EncryptionHandshake)
   238  			tr2.EXPECT().DroppedEncryptionLevel(EncryptionHandshake)
   239  			tracer.DroppedEncryptionLevel(EncryptionHandshake)
   240  		})
   241  
   242  		It("traces the DroppedKey event", func() {
   243  			tr1.EXPECT().DroppedKey(KeyPhase(123))
   244  			tr2.EXPECT().DroppedKey(KeyPhase(123))
   245  			tracer.DroppedKey(123)
   246  		})
   247  
   248  		It("traces the SetLossTimer event", func() {
   249  			now := time.Now()
   250  			tr1.EXPECT().SetLossTimer(TimerTypePTO, EncryptionHandshake, now)
   251  			tr2.EXPECT().SetLossTimer(TimerTypePTO, EncryptionHandshake, now)
   252  			tracer.SetLossTimer(TimerTypePTO, EncryptionHandshake, now)
   253  		})
   254  
   255  		It("traces the LossTimerExpired event", func() {
   256  			tr1.EXPECT().LossTimerExpired(TimerTypePTO, EncryptionHandshake)
   257  			tr2.EXPECT().LossTimerExpired(TimerTypePTO, EncryptionHandshake)
   258  			tracer.LossTimerExpired(TimerTypePTO, EncryptionHandshake)
   259  		})
   260  
   261  		It("traces the LossTimerCanceled event", func() {
   262  			tr1.EXPECT().LossTimerCanceled()
   263  			tr2.EXPECT().LossTimerCanceled()
   264  			tracer.LossTimerCanceled()
   265  		})
   266  
   267  		It("traces the Close event", func() {
   268  			tr1.EXPECT().Close()
   269  			tr2.EXPECT().Close()
   270  			tracer.Close()
   271  		})
   272  	})
   273  })