Debian Codegraph @

third_party/rust/bhttp/tests/test.rs

raw
1 // Rather than grapple with #[cfg(...)] for every variable and import.
2 #![cfg(all(feature = "http", feature = "bhttp"))]
3
4 use bhttp::{Error, Message, Mode};
5 use std::{io::Cursor, mem::drop};
6
7 const CHUNKED_HTTP: &[u8] = b"HTTP/1.1 200 OK\r\n\
8 Transfer-Encoding: camel, chunked\r\n\
9 \r\n\
10 4\r\n\
11 This\r\n\
12 6\r\n \
13 conte\r\n\
14 13;chunk-extension=foo\r\n\
15 nt contains CRLF.\r\n\
16 \r\n\
17 0\r\n\
18 Trailer: text\r\n\
19 \r\n";
20 const TRANSFER_ENCODING: &[u8] = b"transfer-encoding";
21 const CHUNKED_KNOWN: &[u8] = &[
22 0x01, 0x40, 0xc8, 0x00, 0x1d, 0x54, 0x68, 0x69, 0x73, 0x20, 0x63, 0x6f, 0x6e, 0x74, 0x65, 0x6e,
23 0x74, 0x20, 0x63, 0x6f, 0x6e, 0x74, 0x61, 0x69, 0x6e, 0x73, 0x20, 0x43, 0x52, 0x4c, 0x46, 0x2e,
24 0x0d, 0x0a, 0x0d, 0x07, 0x74, 0x72, 0x61, 0x69, 0x6c, 0x65, 0x72, 0x04, 0x74, 0x65, 0x78, 0x74,
25 ];
26 const CHUNKED_INDEFINITE: &[u8] = &[
27 0x03, 0x40, 0xc8, 0x00, 0x1d, 0x54, 0x68, 0x69, 0x73, 0x20, 0x63, 0x6f, 0x6e, 0x74, 0x65, 0x6e,
28 0x74, 0x20, 0x63, 0x6f, 0x6e, 0x74, 0x61, 0x69, 0x6e, 0x73, 0x20, 0x43, 0x52, 0x4c, 0x46, 0x2e,
29 0x0d, 0x0a, 0x00, 0x07, 0x74, 0x72, 0x61, 0x69, 0x6c, 0x65, 0x72, 0x04, 0x74, 0x65, 0x78, 0x74,
30 0x00,
31 ];
32
33 const REQUEST: &[u8] = b"GET /hello.txt HTTP/1.1\r\n\
34 user-agent: curl/7.16.3 libcurl/7.16.3 OpenSSL/0.9.7l zlib/1.2.3\r\n\
35 host: www.example.com\r\n\
36 accept-language: en, mi\r\n\
37 \r\n";
38 const REQUEST_KNOWN: &[u8] = &[
39 0x00, 0x03, 0x47, 0x45, 0x54, 0x05, 0x68, 0x74, 0x74, 0x70, 0x73, 0x00, 0x0a, 0x2f, 0x68, 0x65,
40 0x6c, 0x6c, 0x6f, 0x2e, 0x74, 0x78, 0x74, 0x40, 0x6c, 0x0a, 0x75, 0x73, 0x65, 0x72, 0x2d, 0x61,
41 0x67, 0x65, 0x6e, 0x74, 0x34, 0x63, 0x75, 0x72, 0x6c, 0x2f, 0x37, 0x2e, 0x31, 0x36, 0x2e, 0x33,
42 0x20, 0x6c, 0x69, 0x62, 0x63, 0x75, 0x72, 0x6c, 0x2f, 0x37, 0x2e, 0x31, 0x36, 0x2e, 0x33, 0x20,
43 0x4f, 0x70, 0x65, 0x6e, 0x53, 0x53, 0x4c, 0x2f, 0x30, 0x2e, 0x39, 0x2e, 0x37, 0x6c, 0x20, 0x7a,
44 0x6c, 0x69, 0x62, 0x2f, 0x31, 0x2e, 0x32, 0x2e, 0x33, 0x04, 0x68, 0x6f, 0x73, 0x74, 0x0f, 0x77,
45 0x77, 0x77, 0x2e, 0x65, 0x78, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x2e, 0x63, 0x6f, 0x6d, 0x0f, 0x61,
46 0x63, 0x63, 0x65, 0x70, 0x74, 0x2d, 0x6c, 0x61, 0x6e, 0x67, 0x75, 0x61, 0x67, 0x65, 0x06, 0x65,
47 0x6e, 0x2c, 0x20, 0x6d, 0x69, 0x00, 0x00,
48 ];
49
50 #[test]
51 fn chunked_read() {
52 drop(Message::read_http(&mut Cursor::new(CHUNKED_HTTP)).unwrap());
53 }
54
55 #[test]
56 fn chunked_read_known() {
57 drop(Message::read_bhttp(&mut Cursor::new(CHUNKED_KNOWN)).unwrap());
58 }
59
60 #[test]
61 fn chunked_read_indefinite() {
62 drop(Message::read_bhttp(&mut Cursor::new(CHUNKED_INDEFINITE)).unwrap());
63 }
64
65 #[test]
66 fn chunked_to_known() {
67 let m = Message::read_http(&mut Cursor::new(CHUNKED_HTTP)).unwrap();
68 assert!(m.header().get(TRANSFER_ENCODING).is_none());
69
70 let mut buf = Vec::new();
71 m.write_bhttp(Mode::KnownLength, &mut buf).unwrap();
72 println!("result: {}", hex::encode(&buf));
73 assert_eq!(&buf[..], CHUNKED_KNOWN);
74 }
75
76 #[test]
77 fn chunked_to_indefinite() {
78 let m = Message::read_http(&mut Cursor::new(CHUNKED_HTTP)).unwrap();
79 assert!(m.header().get(TRANSFER_ENCODING).is_none());
80
81 let mut buf = Vec::new();
82 m.write_bhttp(Mode::IndefiniteLength, &mut buf).unwrap();
83 println!("result: {}", hex::encode(&buf));
84 assert_eq!(&buf[..], CHUNKED_INDEFINITE);
85 }
86
87 #[test]
88 fn convert_request() {
89 let m = Message::read_http(&mut Cursor::new(REQUEST)).unwrap();
90 let mut buf = Vec::new();
91 m.write_bhttp(Mode::KnownLength, &mut buf).unwrap();
92 println!("result: {}", hex::encode(&buf));
93 assert_eq!(&buf[..], REQUEST_KNOWN);
94 }
95
96 #[test]
97 fn padded_to_http() {
98 let mut padded = Vec::from(REQUEST_KNOWN);
99 padded.resize(padded.len() + 100, 0);
100 let m = Message::read_bhttp(&mut Cursor::new(&padded[..])).unwrap();
101 let mut buf = Vec::new();
102 m.write_http(&mut buf).unwrap();
103 assert_eq!(&buf[..], REQUEST);
104 }
105
106 #[test]
107 fn truncated_to_http() {
108 let mut padded = Vec::from(REQUEST_KNOWN);
109 assert_eq!(2, padded.iter().rev().take_while(|&x| *x == 0).count());
110 padded.truncate(padded.len() - 2);
111
112 let m = Message::read_bhttp(&mut Cursor::new(&padded[..])).unwrap();
113 let mut buf = Vec::new();
114 m.write_http(&mut buf).unwrap();
115 assert_eq!(&buf[..], REQUEST);
116 }
117
118 #[test]
119 fn tiny_request() {
120 const REQUEST: &[u8] = &[
121 0x00, 0x03, 0x47, 0x45, 0x54, 0x05, 0x68, 0x74, 0x74, 0x70, 0x73, 0x0b, 0x65, 0x78, 0x61,
122 0x6d, 0x70, 0x6c, 0x65, 0x2e, 0x63, 0x6f, 0x6d, 0x01, 0x2f,
123 ];
124 let m = Message::read_bhttp(&mut Cursor::new(REQUEST)).unwrap();
125 assert_eq!(m.control().method().unwrap(), b"GET");
126 assert_eq!(m.control().scheme().unwrap(), b"https");
127 assert_eq!(m.control().authority().unwrap(), b"example.com");
128 assert_eq!(m.control().path().unwrap(), b"/");
129 assert!(m.control().status().is_none());
130 assert!(m.header().is_empty());
131 assert!(m.content().is_empty());
132 assert!(m.trailer().is_empty());
133 }
134
135 #[test]
136 fn tiny_response() {
137 const RESPONSE: &[u8] = &[0x01, 0x40, 0xc8];
138 let m = Message::read_bhttp(&mut Cursor::new(RESPONSE)).unwrap();
139 assert!(m.informational().is_empty());
140 assert_eq!(m.control().status().unwrap(), 200);
141 assert!(m.control().method().is_none());
142 assert!(m.control().scheme().is_none());
143 assert!(m.control().authority().is_none());
144 assert!(m.control().path().is_none());
145 assert!(m.header().is_empty());
146 assert!(m.content().is_empty());
147 assert!(m.trailer().is_empty());
148 }
149
150 #[test]
151 fn connect_request() {
152 const REQUEST: &[u8] = b"CONNECT test.example HTTP/1.1\r\n\
153 Host: example.com\r\n\
154 \r\n";
155 let err = Message::read_http(&mut Cursor::new(REQUEST)).unwrap_err();
156 assert!(matches!(err, Error::ConnectUnsupported));
157 }
158
159 /// Verify that Connection and Proxy-Connection are stripped out properly.
160 #[test]
161 fn connection_header() {
162 const REQUEST: &[u8] = b"POST test.example HTTP/1.1\r\n\
163 Host: example.com\r\n\
164 other: test\r\n\
165 Connection: sample\r\n\
166 Connection: other, garbage\r\n\
167 sample: test2\r\n\
168 px: test3\r\n\
169 proXy-connection: px\r\n\
170 \r\n";
171
172 let m = Message::read_http(&mut Cursor::new(REQUEST)).unwrap();
173 assert!(m.header().get(b"other").is_none());
174 assert!(m.header().get(b"sample").is_none());
175 assert!(m.header().get(b"garbage").is_none());
176 assert!(m.header().get(b"connection").is_none());
177 assert!(m.header().get(b"proxy-connection").is_none());
178 assert!(m.header().get(b"px").is_none());
179 }
180
181 /// Verify that hop-by-hop headers (other than transfer-encoding) are stripped out properly.
182 #[test]
183 fn hop_by_hop() {
184 const REQUEST: &[u8] = b"POST test.example HTTP/1.1\r\n\
185 Host: example.com\r\n\
186 keep-alive: 1\r\n\
187 te: trailers\r\n\
188 trailer: te\r\n\
189 upgrade: h2c\r\n\
190 \r\n";
191
192 let m = Message::read_http(&mut Cursor::new(REQUEST)).unwrap();
193 assert!(m.header().get(b"keep-alive").is_none());
194 assert!(m.header().get(b"te").is_none());
195 assert!(m.header().get(b"trailer").is_none());
196 assert!(m.header().get(b"transfer-encoding").is_none());
197 assert!(m.header().get(b"upgrade").is_none());
198 }
199
200 /// Verify that very bad chunked encoding produces a result.
201 #[test]
202 fn bad_chunked() {
203 const REQUEST: &[u8] = b"POST test.example HTTP/1.1\r\n\
204 Transfer-Encoding: chunked\r\n\
205 \r\n";
206
207 let e = Message::read_http(&mut Cursor::new(REQUEST)).unwrap_err();
208 assert!(matches!(e, Error::Truncated));
209 }
210
211 /// If a length field overruns the buffer, stop.
212 #[test]
213 fn oversized() {
214 const REQUEST: &[u8] = &[0x00, 0x01];
215 let e = Message::read_bhttp(&mut Cursor::new(REQUEST)).unwrap_err();
216 assert!(matches!(e, Error::Truncated));
217 }
218
219 /// If a length field overruns the buffer, stop before over-allocating.
220 #[test]
221 fn oversized_max() {
222 const REQUEST: &[u8] = &[0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff];
223 let e = Message::read_bhttp(&mut Cursor::new(REQUEST)).unwrap_err();
224 assert!(matches!(e, Error::Truncated));
225 }