Mercurial > crates > systemd-socket
comparison src/lib.rs @ 2:cabc4aafdd85
Added length check and a few tests
Systemd socket names must not exceed 255 chars, so this change satity
checks the limit.
author | Martin Habovstiak <martin.habovstiak@gmail.com> |
---|---|
date | Fri, 27 Nov 2020 10:21:07 +0100 |
parents | a65053246c29 |
children | 0edcde404b02 |
comparison
equal
deleted
inserted
replaced
1:ef8bf41097ac | 2:cabc4aafdd85 |
---|---|
161 | 161 |
162 // We can't impl<T: Deref<Target=str> + Into<String>> TryFrom<T> for SocketAddr because of orphan | 162 // We can't impl<T: Deref<Target=str> + Into<String>> TryFrom<T> for SocketAddr because of orphan |
163 // rules. | 163 // rules. |
164 fn try_from_generic<'a, T>(string: T) -> Result<Self, ParseError> where T: 'a + std::ops::Deref<Target=str> + Into<String> { | 164 fn try_from_generic<'a, T>(string: T) -> Result<Self, ParseError> where T: 'a + std::ops::Deref<Target=str> + Into<String> { |
165 if string.starts_with(SYSTEMD_PREFIX) { | 165 if string.starts_with(SYSTEMD_PREFIX) { |
166 let name_len = string.len() - SYSTEMD_PREFIX.len(); | |
166 match string[SYSTEMD_PREFIX.len()..].chars().enumerate().find(|(_, c)| !c.is_ascii() || *c < ' ' || *c == ':') { | 167 match string[SYSTEMD_PREFIX.len()..].chars().enumerate().find(|(_, c)| !c.is_ascii() || *c < ' ' || *c == ':') { |
167 None => Ok(SocketAddr(SocketAddrInner::Systemd(string.into()))), | 168 None if name_len <= 255 => Ok(SocketAddr(SocketAddrInner::Systemd(string.into()))), |
169 None => Err(ParseErrorInner::LongSocketName { string: string.into(), len: name_len }.into()), | |
168 Some((pos, c)) => Err(ParseErrorInner::InvalidCharacter { string: string.into(), c, pos, }.into()), | 170 Some((pos, c)) => Err(ParseErrorInner::InvalidCharacter { string: string.into(), c, pos, }.into()), |
169 } | 171 } |
170 } else { | 172 } else { |
171 Ok(string.parse().map(SocketAddrInner::Ordinary).map(SocketAddr).map_err(ParseErrorInner::SocketAddr)?) | 173 Ok(string.parse().map(SocketAddrInner::Ordinary).map(SocketAddr).map_err(ParseErrorInner::SocketAddr)?) |
172 } | 174 } |
305 | 307 |
306 #[test] | 308 #[test] |
307 fn parse_systemd() { | 309 fn parse_systemd() { |
308 assert_eq!("systemd://foo".parse::<SocketAddr>().unwrap().0, SocketAddrInner::Systemd("systemd://foo".to_owned())); | 310 assert_eq!("systemd://foo".parse::<SocketAddr>().unwrap().0, SocketAddrInner::Systemd("systemd://foo".to_owned())); |
309 } | 311 } |
310 } | 312 |
313 #[test] | |
314 #[should_panic] | |
315 fn parse_systemd_fail_control() { | |
316 "systemd://foo\n".parse::<SocketAddr>().unwrap(); | |
317 } | |
318 | |
319 #[test] | |
320 #[should_panic] | |
321 fn parse_systemd_fail_colon() { | |
322 "systemd://foo:".parse::<SocketAddr>().unwrap(); | |
323 } | |
324 | |
325 #[test] | |
326 #[should_panic] | |
327 fn parse_systemd_fail_non_ascii() { | |
328 "systemd://fooĆ”".parse::<SocketAddr>().unwrap(); | |
329 } | |
330 | |
331 #[test] | |
332 #[should_panic] | |
333 fn parse_systemd_fail_too_long() { | |
334 "systemd://xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx".parse::<SocketAddr>().unwrap(); | |
335 } | |
336 } |