github.com/TugasAkhir-QUIC/quic-go@v0.0.2-0.20240215011318-d20e25a9054c/qlog/types_test.go (about) 1 package qlog 2 3 import ( 4 "go/ast" 5 "go/parser" 6 gotoken "go/token" 7 "path" 8 "runtime" 9 "strconv" 10 11 "github.com/TugasAkhir-QUIC/quic-go/internal/protocol" 12 "github.com/TugasAkhir-QUIC/quic-go/internal/qerr" 13 "github.com/TugasAkhir-QUIC/quic-go/logging" 14 15 . "github.com/onsi/ginkgo/v2" 16 . "github.com/onsi/gomega" 17 ) 18 19 var _ = Describe("Types", func() { 20 It("has a string representation for the owner", func() { 21 Expect(ownerLocal.String()).To(Equal("local")) 22 Expect(ownerRemote.String()).To(Equal("remote")) 23 }) 24 25 It("has a string representation for the category", func() { 26 Expect(categoryConnectivity.String()).To(Equal("connectivity")) 27 Expect(categoryTransport.String()).To(Equal("transport")) 28 Expect(categoryRecovery.String()).To(Equal("recovery")) 29 Expect(categorySecurity.String()).To(Equal("security")) 30 }) 31 32 It("has a string representation for the packet type", func() { 33 Expect(packetType(logging.PacketTypeInitial).String()).To(Equal("initial")) 34 Expect(packetType(logging.PacketTypeHandshake).String()).To(Equal("handshake")) 35 Expect(packetType(logging.PacketType0RTT).String()).To(Equal("0RTT")) 36 Expect(packetType(logging.PacketType1RTT).String()).To(Equal("1RTT")) 37 Expect(packetType(logging.PacketTypeStatelessReset).String()).To(Equal("stateless_reset")) 38 Expect(packetType(logging.PacketTypeRetry).String()).To(Equal("retry")) 39 Expect(packetType(logging.PacketTypeVersionNegotiation).String()).To(Equal("version_negotiation")) 40 Expect(packetType(logging.PacketTypeNotDetermined).String()).To(BeEmpty()) 41 }) 42 43 It("has a string representation for the packet drop reason", func() { 44 Expect(packetDropReason(logging.PacketDropKeyUnavailable).String()).To(Equal("key_unavailable")) 45 Expect(packetDropReason(logging.PacketDropUnknownConnectionID).String()).To(Equal("unknown_connection_id")) 46 Expect(packetDropReason(logging.PacketDropHeaderParseError).String()).To(Equal("header_parse_error")) 47 Expect(packetDropReason(logging.PacketDropPayloadDecryptError).String()).To(Equal("payload_decrypt_error")) 48 Expect(packetDropReason(logging.PacketDropProtocolViolation).String()).To(Equal("protocol_violation")) 49 Expect(packetDropReason(logging.PacketDropDOSPrevention).String()).To(Equal("dos_prevention")) 50 Expect(packetDropReason(logging.PacketDropUnsupportedVersion).String()).To(Equal("unsupported_version")) 51 Expect(packetDropReason(logging.PacketDropUnexpectedPacket).String()).To(Equal("unexpected_packet")) 52 Expect(packetDropReason(logging.PacketDropUnexpectedSourceConnectionID).String()).To(Equal("unexpected_source_connection_id")) 53 Expect(packetDropReason(logging.PacketDropUnexpectedVersion).String()).To(Equal("unexpected_version")) 54 }) 55 56 It("has a string representation for the timer type", func() { 57 Expect(timerType(logging.TimerTypeACK).String()).To(Equal("ack")) 58 Expect(timerType(logging.TimerTypePTO).String()).To(Equal("pto")) 59 }) 60 61 It("has a string representation for the key type", func() { 62 Expect(encLevelToKeyType(protocol.EncryptionInitial, protocol.PerspectiveClient).String()).To(Equal("client_initial_secret")) 63 Expect(encLevelToKeyType(protocol.EncryptionInitial, protocol.PerspectiveServer).String()).To(Equal("server_initial_secret")) 64 Expect(encLevelToKeyType(protocol.EncryptionHandshake, protocol.PerspectiveClient).String()).To(Equal("client_handshake_secret")) 65 Expect(encLevelToKeyType(protocol.EncryptionHandshake, protocol.PerspectiveServer).String()).To(Equal("server_handshake_secret")) 66 Expect(encLevelToKeyType(protocol.Encryption0RTT, protocol.PerspectiveClient).String()).To(Equal("client_0rtt_secret")) 67 Expect(encLevelToKeyType(protocol.Encryption0RTT, protocol.PerspectiveServer).String()).To(Equal("server_0rtt_secret")) 68 Expect(encLevelToKeyType(protocol.Encryption1RTT, protocol.PerspectiveClient).String()).To(Equal("client_1rtt_secret")) 69 Expect(encLevelToKeyType(protocol.Encryption1RTT, protocol.PerspectiveServer).String()).To(Equal("server_1rtt_secret")) 70 }) 71 72 It("has a string representation for the key update trigger", func() { 73 Expect(keyUpdateTLS.String()).To(Equal("tls")) 74 Expect(keyUpdateRemote.String()).To(Equal("remote_update")) 75 Expect(keyUpdateLocal.String()).To(Equal("local_update")) 76 }) 77 78 It("tells the packet number space from the encryption level", func() { 79 Expect(encLevelToPacketNumberSpace(protocol.EncryptionInitial)).To(Equal("initial")) 80 Expect(encLevelToPacketNumberSpace(protocol.EncryptionHandshake)).To(Equal("handshake")) 81 Expect(encLevelToPacketNumberSpace(protocol.Encryption0RTT)).To(Equal("application_data")) 82 Expect(encLevelToPacketNumberSpace(protocol.Encryption1RTT)).To(Equal("application_data")) 83 }) 84 85 Context("transport errors", func() { 86 It("has a string representation for every error code", func() { 87 // We parse the error code file, extract all constants, and verify that 88 // each of them has a string version. Go FTW! 89 _, thisfile, _, ok := runtime.Caller(0) 90 if !ok { 91 panic("Failed to get current frame") 92 } 93 filename := path.Join(path.Dir(thisfile), "../internal/qerr/error_codes.go") 94 fileAst, err := parser.ParseFile(gotoken.NewFileSet(), filename, nil, 0) 95 Expect(err).NotTo(HaveOccurred()) 96 constSpecs := fileAst.Decls[2].(*ast.GenDecl).Specs 97 Expect(len(constSpecs)).To(BeNumerically(">", 4)) // at time of writing 98 for _, c := range constSpecs { 99 valString := c.(*ast.ValueSpec).Values[0].(*ast.BasicLit).Value 100 val, err := strconv.ParseInt(valString, 0, 64) 101 Expect(err).NotTo(HaveOccurred()) 102 Expect(transportError(val).String()).ToNot(BeEmpty()) 103 } 104 }) 105 106 It("has a string representation for transport errors", func() { 107 Expect(transportError(qerr.NoError).String()).To(Equal("no_error")) 108 Expect(transportError(qerr.InternalError).String()).To(Equal("internal_error")) 109 Expect(transportError(qerr.ConnectionRefused).String()).To(Equal("connection_refused")) 110 Expect(transportError(qerr.FlowControlError).String()).To(Equal("flow_control_error")) 111 Expect(transportError(qerr.StreamLimitError).String()).To(Equal("stream_limit_error")) 112 Expect(transportError(qerr.StreamStateError).String()).To(Equal("stream_state_error")) 113 Expect(transportError(qerr.FrameEncodingError).String()).To(Equal("frame_encoding_error")) 114 Expect(transportError(qerr.ConnectionIDLimitError).String()).To(Equal("connection_id_limit_error")) 115 Expect(transportError(qerr.ProtocolViolation).String()).To(Equal("protocol_violation")) 116 Expect(transportError(qerr.InvalidToken).String()).To(Equal("invalid_token")) 117 Expect(transportError(qerr.ApplicationErrorErrorCode).String()).To(Equal("application_error")) 118 Expect(transportError(qerr.CryptoBufferExceeded).String()).To(Equal("crypto_buffer_exceeded")) 119 Expect(transportError(qerr.NoViablePathError).String()).To(Equal("no_viable_path")) 120 Expect(transportError(1337).String()).To(BeEmpty()) 121 }) 122 }) 123 124 It("has a string representation for congestion state updates", func() { 125 Expect(congestionState(logging.CongestionStateSlowStart).String()).To(Equal("slow_start")) 126 Expect(congestionState(logging.CongestionStateCongestionAvoidance).String()).To(Equal("congestion_avoidance")) 127 Expect(congestionState(logging.CongestionStateApplicationLimited).String()).To(Equal("application_limited")) 128 Expect(congestionState(logging.CongestionStateRecovery).String()).To(Equal("recovery")) 129 }) 130 131 It("has a string representation for the ECN bits", func() { 132 Expect(ecn(logging.ECT0).String()).To(Equal("ECT(0)")) 133 Expect(ecn(logging.ECT1).String()).To(Equal("ECT(1)")) 134 Expect(ecn(logging.ECNCE).String()).To(Equal("CE")) 135 Expect(ecn(logging.ECTNot).String()).To(Equal("Not-ECT")) 136 Expect(ecn(42).String()).To(Equal("unknown ECN")) 137 }) 138 139 It("has a string representation for the ECN state", func() { 140 Expect(ecnState(logging.ECNStateTesting).String()).To(Equal("testing")) 141 Expect(ecnState(logging.ECNStateUnknown).String()).To(Equal("unknown")) 142 Expect(ecnState(logging.ECNStateFailed).String()).To(Equal("failed")) 143 Expect(ecnState(logging.ECNStateCapable).String()).To(Equal("capable")) 144 Expect(ecnState(42).String()).To(Equal("unknown ECN state")) 145 }) 146 147 It("has a string representation for the ECN state trigger", func() { 148 Expect(ecnStateTrigger(logging.ECNTriggerNoTrigger).String()).To(Equal("")) 149 Expect(ecnStateTrigger(logging.ECNFailedNoECNCounts).String()).To(Equal("ACK doesn't contain ECN marks")) 150 Expect(ecnStateTrigger(logging.ECNFailedDecreasedECNCounts).String()).To(Equal("ACK decreases ECN counts")) 151 Expect(ecnStateTrigger(logging.ECNFailedLostAllTestingPackets).String()).To(Equal("all ECN testing packets declared lost")) 152 Expect(ecnStateTrigger(logging.ECNFailedMoreECNCountsThanSent).String()).To(Equal("ACK contains more ECN counts than ECN-marked packets sent")) 153 Expect(ecnStateTrigger(logging.ECNFailedTooFewECNCounts).String()).To(Equal("ACK contains fewer new ECN counts than acknowledged ECN-marked packets")) 154 Expect(ecnStateTrigger(logging.ECNFailedManglingDetected).String()).To(Equal("ECN mangling detected")) 155 Expect(ecnStateTrigger(42).String()).To(Equal("unknown ECN state trigger")) 156 }) 157 })