1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
use bytes::Bytes; use ser::deserialize; use common::{Port, IpAddress, Services}; #[derive(Debug, Default, PartialEq, Clone, Serializable, Deserializable)] pub struct NetAddress { pub services: Services, pub address: IpAddress, pub port: Port, } impl From<&'static str> for NetAddress { fn from(s: &'static str) -> Self { let bytes: Bytes = s.into(); deserialize(bytes.as_ref()).unwrap() } } #[cfg(test)] mod tests { use ser::{serialize, deserialize}; use common::Services; use super::NetAddress; #[test] fn test_net_address_serialize() { let expected = vec![ 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0x0a, 0x00, 0x00, 0x01, 0x20, 0x8d ].into(); let address = NetAddress { services: Services::default().with_network(true), address: "::ffff:a00:1".into(), port: 8333.into(), }; assert_eq!(serialize(&address), expected); } #[test] fn test_net_address_deserialize() { let bytes = vec![ 0x01u8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0x0a, 0x00, 0x00, 0x01, 0x20, 0x8d ]; let expected = NetAddress { services: Services::default().with_network(true), address: "::ffff:a00:1".into(), port: 8333.into(), }; assert_eq!(expected, deserialize(&bytes as &[u8]).unwrap()); } #[test] fn test_net_address_from_static_str() { let expected = NetAddress { services: Services::default().with_network(true), address: "::ffff:a00:1".into(), port: 8333.into(), }; let s = "010000000000000000000000000000000000ffff0a000001208d"; assert_eq!(expected, s.into()); } }