@@ -77,3 +77,83 @@ impl nom::error::ParseError<&[u8]> for Error {
77
77
}
78
78
79
79
// TODO: conver DecodingError to quic error
80
+
81
+ #[ cfg( test) ]
82
+ mod tests {
83
+ use nom:: error:: ParseError ;
84
+
85
+ use super :: * ;
86
+ use crate :: packet:: r#type:: {
87
+ long:: { Type :: V1 , Ver1 } ,
88
+ Type ,
89
+ } ;
90
+
91
+ #[ test]
92
+ fn test_error_conversion_to_transport_error ( ) {
93
+ let cases = vec ! [
94
+ ( Error :: NoFrames , TransportErrorKind :: ProtocolViolation ) ,
95
+ (
96
+ Error :: IncompleteType ( "test" . to_string( ) ) ,
97
+ TransportErrorKind :: FrameEncoding ,
98
+ ) ,
99
+ (
100
+ Error :: InvalidType ( VarInt :: from_u32( 0x1f ) ) ,
101
+ TransportErrorKind :: FrameEncoding ,
102
+ ) ,
103
+ (
104
+ Error :: WrongType ( FrameType :: Ping , Type :: Long ( V1 ( Ver1 :: INITIAL ) ) ) ,
105
+ TransportErrorKind :: FrameEncoding ,
106
+ ) ,
107
+ (
108
+ Error :: IncompleteFrame ( FrameType :: Ping , "incomplete" . to_string( ) ) ,
109
+ TransportErrorKind :: FrameEncoding ,
110
+ ) ,
111
+ (
112
+ Error :: ParseError ( FrameType :: Ping , "parse error" . to_string( ) ) ,
113
+ TransportErrorKind :: FrameEncoding ,
114
+ ) ,
115
+ ] ;
116
+
117
+ for ( error, expected_kind) in cases {
118
+ let transport_error: TransportError = error. into ( ) ;
119
+ assert_eq ! ( transport_error. kind( ) , expected_kind) ;
120
+ }
121
+ }
122
+
123
+ #[ test]
124
+ fn test_nom_error_conversion ( ) {
125
+ let error = Error :: NoFrames ;
126
+ let nom_error = nom:: Err :: Error ( error. clone ( ) ) ;
127
+ let converted: Error = nom_error. into ( ) ;
128
+ assert_eq ! ( converted, error) ;
129
+
130
+ let nom_failure = nom:: Err :: Failure ( error. clone ( ) ) ;
131
+ let converted: Error = nom_failure. into ( ) ;
132
+ assert_eq ! ( converted, error) ;
133
+ }
134
+
135
+ #[ test]
136
+ fn test_parse_error_impl ( ) {
137
+ let error = Error :: ParseError ( FrameType :: Ping , "test error" . to_string ( ) ) ;
138
+ let appended = Error :: append ( & [ ] , NomErrorKind :: ManyTill , error. clone ( ) ) ;
139
+ assert_eq ! ( appended, error) ;
140
+ }
141
+
142
+ #[ test]
143
+ #[ should_panic( expected = "QUIC frame parser must always consume" ) ]
144
+ fn test_parse_error_unreachable ( ) {
145
+ Error :: from_error_kind ( & [ ] , NomErrorKind :: ManyTill ) ;
146
+ }
147
+
148
+ #[ test]
149
+ fn test_error_display ( ) {
150
+ let error = Error :: NoFrames ;
151
+ assert_eq ! ( error. to_string( ) , "A packet containing no frames" ) ;
152
+
153
+ let error = Error :: IncompleteType ( "test" . to_string ( ) ) ;
154
+ assert_eq ! ( error. to_string( ) , "Incomplete frame type: test" ) ;
155
+
156
+ let error = Error :: InvalidType ( VarInt :: from_u32 ( 0x1f ) ) ;
157
+ assert_eq ! ( error. to_string( ) , "Invalid frame type from 31" ) ;
158
+ }
159
+ }
0 commit comments