Debian Codegraph @

third_party/rust/rmp/tests/func/decode/string.rs

raw
1 use super::Cursor;
2
3 use rmp::decode::*;
4 use rmp::Marker;
5
6 #[test]
7 fn from_fixstr_min_read_str_len() {
8 let buf: &[u8] = &[0xa0];
9 let mut cur = Cursor::new(buf);
10
11 assert_eq!(0, read_str_len(&mut cur).unwrap());
12 assert_eq!(1, cur.position());
13 }
14
15 #[test]
16 fn from_fixstr_rnd_read_str_len() {
17 let buf: &[u8] = &[0xaa];
18 let mut cur = Cursor::new(buf);
19
20 assert_eq!(10, read_str_len(&mut cur).unwrap());
21 assert_eq!(1, cur.position());
22 }
23
24 #[test]
25 fn from_fixstr_max_read_str_len() {
26 let buf: &[u8] = &[0xbf];
27 let mut cur = Cursor::new(buf);
28
29 assert_eq!(31, read_str_len(&mut cur).unwrap());
30 assert_eq!(1, cur.position());
31 }
32
33 #[test]
34 fn from_str8_min_read_str_len() {
35 let buf: &[u8] = &[0xd9, 0x00];
36 let mut cur = Cursor::new(buf);
37
38 assert_eq!(0, read_str_len(&mut cur).unwrap());
39 assert_eq!(2, cur.position());
40 }
41
42 #[test]
43 fn from_str8_rnd_read_str_len() {
44 let buf: &[u8] = &[0xd9, 0x0a];
45 let mut cur = Cursor::new(buf);
46
47 assert_eq!(10, read_str_len(&mut cur).unwrap());
48 assert_eq!(2, cur.position());
49 }
50
51 #[test]
52 fn from_str8_read_str_len_eof() {
53 let buf: &[u8] = &[0xd9];
54 let mut cur = Cursor::new(buf);
55
56 read_str_len(&mut cur).err().unwrap();
57 assert_eq!(1, cur.position());
58 }
59
60 #[test]
61 fn from_str8_max_read_str_len() {
62 let buf: &[u8] = &[0xd9, 0xff];
63 let mut cur = Cursor::new(buf);
64
65 assert_eq!(255, read_str_len(&mut cur).unwrap());
66 assert_eq!(2, cur.position());
67 }
68
69 #[test]
70 fn from_str16_min_read_str_len() {
71 let buf: &[u8] = &[0xda, 0x00, 0x00];
72 let mut cur = Cursor::new(buf);
73
74 assert_eq!(0, read_str_len(&mut cur).unwrap());
75 assert_eq!(3, cur.position());
76 }
77
78 #[test]
79 fn from_str16_max_read_str_len() {
80 let buf: &[u8] = &[0xda, 0xff, 0xff];
81 let mut cur = Cursor::new(buf);
82
83 assert_eq!(65535, read_str_len(&mut cur).unwrap());
84 assert_eq!(3, cur.position());
85 }
86
87 #[test]
88 fn from_str16_read_str_len_eof() {
89 let buf: &[u8] = &[0xda, 0x00];
90 let mut cur = Cursor::new(buf);
91
92 read_str_len(&mut cur).err().unwrap();
93 assert!(cur.position() >= 1);
94 }
95
96 #[test]
97 fn from_str32_min_read_str_len() {
98 let buf: &[u8] = &[0xdb, 0x00, 0x00, 0x00, 0x00];
99 let mut cur = Cursor::new(buf);
100
101 assert_eq!(0, read_str_len(&mut cur).unwrap());
102 assert_eq!(5, cur.position());
103 }
104
105 #[test]
106 fn from_str32_max_read_str_len() {
107 let buf: &[u8] = &[0xdb, 0xff, 0xff, 0xff, 0xff];
108 let mut cur = Cursor::new(buf);
109
110 assert_eq!(4294967295, read_str_len(&mut cur).unwrap());
111 assert_eq!(5, cur.position());
112 }
113
114 #[test]
115 fn from_str32_read_str_len_eof() {
116 let buf: &[u8] = &[0xdb, 0x00, 0x00, 0x00];
117 let mut cur = Cursor::new(buf);
118
119 read_str_len(&mut cur).err().unwrap();
120 assert!(cur.position() >= 1);
121 }
122
123 #[test]
124 fn from_null_read_str_len() {
125 let buf: &[u8] = &[0xc0];
126 let mut cur = Cursor::new(buf);
127
128 match read_str_len(&mut cur) {
129 Err(ValueReadError::TypeMismatch(Marker::Null)) => (),
130 other => panic!("unexpected result: {other:?}"),
131 }
132 assert_eq!(1, cur.position());
133 }
134
135 #[test]
136 fn from_str_strfix() {
137 let buf: &[u8] = &[0xaa, 0x6c, 0x65, 0x20, 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65];
138 let mut cur = Cursor::new(buf);
139
140 let out: &mut [u8] = &mut [0u8; 16];
141
142 assert_eq!("le message", read_str(&mut cur, out).unwrap());
143 assert_eq!(11, cur.position());
144 }
145
146 #[test]
147 fn from_str_strfix_extra_data() {
148 let buf: &[u8] = &[0xaa, 0x6c, 0x65, 0x20, 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x00];
149 let mut cur = Cursor::new(buf);
150
151 let out: &mut [u8] = &mut [0u8; 16];
152
153 assert_eq!("le message", read_str(&mut cur, out).unwrap());
154 assert_eq!(11, cur.position());
155 }
156
157 #[test]
158 fn from_str_strfix_exact_buffer() {
159 let buf: &[u8] = &[0xaa, 0x6c, 0x65, 0x20, 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65];
160 let mut cur = Cursor::new(buf);
161
162 let out: &mut [u8] = &mut [0u8; 10];
163
164 assert_eq!("le message", read_str(&mut cur, out).unwrap());
165 assert_eq!(11, cur.position());
166 }
167
168 #[test]
169 fn from_str_strfix_invalid_utf8() {
170 // Invalid 2 Octet Sequence.
171 let buf: &[u8] = &[0xa2, 0xc3, 0x28];
172 let mut cur = Cursor::new(buf);
173
174 let out: &mut [u8] = &mut [0u8; 16];
175
176 match read_str(&mut cur, out) {
177 Err(DecodeStringError::InvalidUtf8(raw, _)) => {
178 assert_eq!(&[0xc3, 0x28], raw);
179 }
180 other => panic!("unexpected result: {other:?}"),
181 }
182
183 assert_eq!(3, cur.position());
184 }
185
186 #[test]
187 fn from_str_strfix_buffer_too_small() {
188 let buf: &[u8] = &[0xaa, 0x6c, 0x65, 0x20, 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65];
189 let mut cur = Cursor::new(buf);
190
191 let out: &mut [u8] = &mut [0u8; 9];
192
193 match read_str(&mut cur, out) {
194 Err(DecodeStringError::BufferSizeTooSmall(10)) => (),
195 other => panic!("unexpected result: {other:?}"),
196 }
197 assert_eq!(1, cur.position());
198 }
199
200 #[test]
201 fn from_str_strfix_decode_from_slice() {
202 // Wrap an incomplete buffer into the Cursor to see how many bytes were consumed.
203 let mut buf = vec![0xaa, 0x6c, 0x65, 0x20, 0x6d, 0x65, 0x73];
204 assert!(read_str_from_slice(&buf).is_err());
205
206 // ... complete the buffer and try to parse again.
207 buf.append(&mut vec![0x73, 0x61, 0x67, 0x65]);
208 assert_eq!(("le message", &[][..]), read_str_from_slice(&buf).unwrap());
209 }
210
211 #[test]
212 fn from_str_strfix_decode_from_slice_with_trailing_bytes() {
213 let buf = vec![
214 0xaa, 0x6c, 0x65, 0x20, 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x01, 0x02, 0x03,
215 ];
216
217 assert_eq!(("le message", &[0x01, 0x02, 0x03][..]), read_str_from_slice(&buf).unwrap());
218 }
219
220 #[test]
221 fn example_process_sequence_of_strings() {
222 // Encoded: 'Unpacking', 'multiple', 'strings'.
223 let vec = [
224 0xa9, 0x55, 0x6e, 0x70, 0x61, 0x63, 0x6b, 0x69, 0x6e, 0x67,
225 0xa8, 0x6d, 0x75, 0x6c, 0x74, 0x69, 0x70, 0x6c, 0x65, 0xa7,
226 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x73
227 ];
228
229 let mut chunks = Vec::new();
230 let mut unparsed = &vec[..];
231 loop {
232 match read_str_from_slice(unparsed) {
233 Ok((chunk, tail)) => {
234 chunks.push(chunk);
235 unparsed = tail;
236 }
237 Err(..) => break,
238 }
239 }
240
241 assert_eq!(["Unpacking", "multiple", "strings"], chunks[..]);
242 }