@@ -79,7 +79,7 @@ pub fn decode_tag(d: &mut Decoder, from: &str) -> Result<Tag, decode::Error> {
7979/// # Errors
8080///
8181/// Error if the decoding fails.
82- pub fn decode_any ( d : & mut Decoder , from : & str ) -> Result < Vec < u8 > , decode:: Error > {
82+ pub fn decode_any < ' d > ( d : & mut Decoder < ' d > , from : & str ) -> Result < & ' d [ u8 ] , decode:: Error > {
8383 let start = d. position ( ) ;
8484 d. skip ( ) ?;
8585 let end = d. position ( ) ;
@@ -88,93 +88,91 @@ pub fn decode_any(d: &mut Decoder, from: &str) -> Result<Vec<u8>, decode::Error>
8888 . get ( start..end)
8989 . ok_or ( decode:: Error :: message ( format ! (
9090 "Failed to get any CBOR bytes in {from}. Invalid CBOR bytes."
91- ) ) ) ?
92- . to_vec ( ) ;
91+ ) ) ) ?;
9392 Ok ( bytes)
9493}
9594
9695#[ cfg( test) ]
9796mod tests {
98-
9997 use minicbor:: Encoder ;
98+ use test_strategy:: proptest;
10099
101100 use super :: * ;
102101
103- #[ test ]
104- fn test_decode_any_bytes ( ) {
102+ #[ proptest ]
103+ fn test_decode_any_bytes ( random_bytes : Vec < u8 > ) {
105104 let mut buf = Vec :: new ( ) ;
106105 let mut e = Encoder :: new ( & mut buf) ;
107- e. bytes ( & [ 1 , 2 , 3 , 4 ] ) . expect ( "Error encoding bytes" ) ;
106+ e. bytes ( & random_bytes ) . expect ( "Error encoding bytes" ) ;
108107
109108 let mut d = Decoder :: new ( & buf) ;
110- let result = decode_any ( & mut d, "test" ) . expect ( "Error decoding bytes" ) ;
111- assert_eq ! ( result, vec![ 1 , 2 , 3 , 4 ] ) ;
109+ let cbor_bytes = decode_any ( & mut d, "test" ) . expect ( "Error decoding bytes" ) ;
110+
111+ let result = decode_bytes ( & mut Decoder :: new ( cbor_bytes) , "test" ) . unwrap ( ) ;
112+ assert_eq ! ( result, random_bytes) ;
112113 }
113114
114- #[ test ]
115- fn test_decode_any_string ( ) {
115+ #[ proptest ]
116+ fn test_decode_any_string ( random_string : String ) {
116117 let mut buf = Vec :: new ( ) ;
117118 let mut e = Encoder :: new ( & mut buf) ;
118- e. str ( "hello" ) . expect ( "Error encoding string" ) ;
119+ e. str ( & random_string ) . expect ( "Error encoding string" ) ;
119120
120121 let mut d = Decoder :: new ( & buf) ;
121- let result = decode_any ( & mut d, "test" ) . expect ( "Error decoding string" ) ;
122- assert_eq ! ( result, b"hello" . to_vec( ) ) ;
122+ let cbor_bytes = decode_any ( & mut d, "test" ) . expect ( "Error decoding string" ) ;
123+
124+ let result =
125+ decode_helper :: < String , _ > ( & mut Decoder :: new ( cbor_bytes) , "test" , & mut ( ) ) . unwrap ( ) ;
126+ assert_eq ! ( result, random_string) ;
123127 }
124128
125- #[ test ]
126- fn test_decode_any_array ( ) {
129+ #[ proptest ]
130+ fn test_decode_any_array ( random_array : Vec < u8 > ) {
127131 // The array should contain a supported type
128132 let mut buf = Vec :: new ( ) ;
129133 let mut e = Encoder :: new ( & mut buf) ;
130- e. array ( 2 ) . expect ( "Error encoding array" ) ;
131- e. u8 ( 1 ) . expect ( "Error encoding u8" ) ;
132- e. u8 ( 2 ) . expect ( "Error encoding u8" ) ;
134+ e. array ( random_array. len ( ) as u64 )
135+ . expect ( "Error encoding array" ) ;
136+ for el in & random_array {
137+ e. u8 ( * el) . expect ( "Error encoding u8" ) ;
138+ }
133139 let mut d = Decoder :: new ( & buf) ;
134- let result = decode_any ( & mut d, "test" ) . expect ( "Error decoding array" ) ;
140+ let cbor_bytes = decode_any ( & mut d, "test" ) . expect ( "Error decoding array" ) ;
135141 // The decode of array is just a length of the array
136- assert_eq ! (
137- u64 :: from_be_bytes( result. try_into( ) . expect( "Error converting bytes to u64" ) ) ,
138- 2
139- ) ;
142+ let result = decode_array_len ( & mut Decoder :: new ( cbor_bytes) , "test" ) . unwrap ( ) ;
143+ assert_eq ! ( result, random_array. len( ) as u64 ) ;
140144 }
141145
142- #[ test ]
143- fn test_decode_any_u32 ( ) {
146+ #[ proptest ]
147+ fn test_decode_any_u32 ( random_u32 : u32 ) {
144148 let mut buf = Vec :: new ( ) ;
145149 let mut e = Encoder :: new ( & mut buf) ;
146- let num: u32 = 123_456_789 ;
147- e. u32 ( num) . expect ( "Error encoding u32" ) ;
150+ e. u32 ( random_u32) . expect ( "Error encoding u32" ) ;
148151
149152 let mut d = Decoder :: new ( & buf) ;
150- let result = decode_any ( & mut d, "test" ) . expect ( "Error decoding u32" ) ;
151- assert_eq ! (
152- u32 :: from_be_bytes ( result. try_into ( ) . expect ( "Error converting bytes to u32" ) ) ,
153- num
154- ) ;
153+ let cbor_bytes = decode_any ( & mut d, "test" ) . expect ( "Error decoding u32" ) ;
154+
155+ let result =
156+ decode_helper :: < u32 , _ > ( & mut Decoder :: new ( cbor_bytes ) , "test" , & mut ( ) ) . unwrap ( ) ;
157+ assert_eq ! ( result , random_u32 ) ;
155158 }
156159
157- #[ test ]
158- fn test_decode_any_i32 ( ) {
160+ #[ proptest ]
161+ fn test_decode_any_i32 ( random_i32 : i32 ) {
159162 let mut buf = Vec :: new ( ) ;
160163 let mut e = Encoder :: new ( & mut buf) ;
161- let num: i32 = -123_456_789 ;
162- e. i32 ( num) . expect ( "Error encoding i32" ) ;
164+ e. i32 ( random_i32) . expect ( "Error encoding i32" ) ;
163165 let mut d = Decoder :: new ( & buf) ;
164- let result = decode_any ( & mut d, "test" ) . expect ( "Error decoding i32" ) ;
165- assert_eq ! (
166- i32 :: from_be_bytes ( result. try_into ( ) . expect ( "Error converting bytes to i32" ) ) ,
167- num
168- ) ;
166+ let cbor_bytes = decode_any ( & mut d, "test" ) . expect ( "Error decoding i32" ) ;
167+
168+ let result =
169+ decode_helper :: < i32 , _ > ( & mut Decoder :: new ( cbor_bytes ) , "test" , & mut ( ) ) . unwrap ( ) ;
170+ assert_eq ! ( result , random_i32 ) ;
169171 }
170172
171173 #[ test]
172- fn test_decode_any_unsupported_type ( ) {
173- let mut buf = Vec :: new ( ) ;
174- let mut e = Encoder :: new ( & mut buf) ;
175- e. null ( ) . expect ( "Error encoding null" ) ; // Encode a null type which is unsupported
176-
177- let mut d = Decoder :: new ( & buf) ;
174+ fn test_decode_any_not_cbor ( ) {
175+ let mut d = Decoder :: new ( & [ ] ) ;
178176 let result = decode_any ( & mut d, "test" ) ;
179177 // Should print out the error message with the location of the error
180178 assert ! ( result. is_err( ) ) ;
0 commit comments