Skip to content

Commit 945617e

Browse files
committed
tests cases
1 parent 524bea2 commit 945617e

File tree

1 file changed

+58
-7
lines changed

1 file changed

+58
-7
lines changed

src/protocol/address.rs

Lines changed: 58 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -172,10 +172,12 @@ impl AsyncStreamOperation for Address {
172172
let atyp = stream.read_u8().await?;
173173
match AddressType::try_from(atyp)? {
174174
AddressType::IPv4 => {
175-
let mut buf = [0; 6];
175+
let mut addr_bytes = [0; 4];
176+
stream.read_exact(&mut addr_bytes).await?;
177+
let mut buf = [0; 2];
176178
stream.read_exact(&mut buf).await?;
177-
let addr = Ipv4Addr::new(buf[0], buf[1], buf[2], buf[3]);
178-
let port = u16::from_be_bytes([buf[4], buf[5]]);
179+
let addr = Ipv4Addr::from(addr_bytes);
180+
let port = u16::from_be_bytes(buf);
179181
Ok(Self::SocketAddress(SocketAddr::from((addr, port))))
180182
}
181183
AddressType::Domain => {
@@ -196,11 +198,11 @@ impl AsyncStreamOperation for Address {
196198
Ok(Self::DomainAddress(addr, port))
197199
}
198200
AddressType::IPv6 => {
199-
let mut buf = [0; 18];
200-
stream.read_exact(&mut buf).await?;
201-
let port = u16::from_be_bytes([buf[16], buf[17]]);
202201
let mut addr_bytes = [0; 16];
203-
addr_bytes.copy_from_slice(&buf[..16]);
202+
stream.read_exact(&mut addr_bytes).await?;
203+
let mut buf = [0; 2];
204+
stream.read_exact(&mut buf).await?;
205+
let port = u16::from_be_bytes(buf);
204206
Ok(Self::SocketAddress(SocketAddr::from((Ipv6Addr::from(addr_bytes), port))))
205207
}
206208
}
@@ -346,3 +348,52 @@ impl TryFrom<&str> for Address {
346348
}
347349
}
348350
}
351+
352+
#[test]
353+
fn test_address() {
354+
let addr = Address::from((Ipv4Addr::new(127, 0, 0, 1), 8080));
355+
let mut buf = Vec::new();
356+
addr.write_to_buf(&mut buf);
357+
assert_eq!(buf, vec![0x01, 127, 0, 0, 1, 0x1f, 0x90]);
358+
let addr2 = Address::retrieve_from_stream(&mut Cursor::new(&buf)).unwrap();
359+
assert_eq!(addr, addr2);
360+
361+
let addr = Address::from((Ipv6Addr::new(0x45, 0xff89, 0, 0, 0, 0, 0, 1), 8080));
362+
let mut buf = Vec::new();
363+
addr.write_to_buf(&mut buf);
364+
assert_eq!(buf, vec![0x04, 0, 0x45, 0xff, 0x89, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0x1f, 0x90]);
365+
let addr2 = Address::retrieve_from_stream(&mut Cursor::new(&buf)).unwrap();
366+
assert_eq!(addr, addr2);
367+
368+
let addr = Address::from(("sex.com".to_owned(), 8080));
369+
let mut buf = Vec::new();
370+
addr.write_to_buf(&mut buf);
371+
assert_eq!(buf, vec![0x03, 0x07, b's', b'e', b'x', b'.', b'c', b'o', b'm', 0x1f, 0x90]);
372+
let addr2 = Address::retrieve_from_stream(&mut Cursor::new(&buf)).unwrap();
373+
assert_eq!(addr, addr2);
374+
}
375+
376+
#[cfg(feature = "tokio")]
377+
#[tokio::test]
378+
async fn test_address_async() {
379+
let addr = Address::from((Ipv4Addr::new(127, 0, 0, 1), 8080));
380+
let mut buf = Vec::new();
381+
addr.write_to_async_stream(&mut buf).await.unwrap();
382+
assert_eq!(buf, vec![0x01, 127, 0, 0, 1, 0x1f, 0x90]);
383+
let addr2 = Address::retrieve_from_async_stream(&mut Cursor::new(&buf)).await.unwrap();
384+
assert_eq!(addr, addr2);
385+
386+
let addr = Address::from((Ipv6Addr::new(0x45, 0xff89, 0, 0, 0, 0, 0, 1), 8080));
387+
let mut buf = Vec::new();
388+
addr.write_to_async_stream(&mut buf).await.unwrap();
389+
assert_eq!(buf, vec![0x04, 0, 0x45, 0xff, 0x89, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0x1f, 0x90]);
390+
let addr2 = Address::retrieve_from_async_stream(&mut Cursor::new(&buf)).await.unwrap();
391+
assert_eq!(addr, addr2);
392+
393+
let addr = Address::from(("sex.com".to_owned(), 8080));
394+
let mut buf = Vec::new();
395+
addr.write_to_async_stream(&mut buf).await.unwrap();
396+
assert_eq!(buf, vec![0x03, 0x07, b's', b'e', b'x', b'.', b'c', b'o', b'm', 0x1f, 0x90]);
397+
let addr2 = Address::retrieve_from_async_stream(&mut Cursor::new(&buf)).await.unwrap();
398+
assert_eq!(addr, addr2);
399+
}

0 commit comments

Comments
 (0)