File: addr.rs

package info (click to toggle)
rust-async-std 1.13.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,992 kB
  • sloc: sh: 13; makefile: 8
file content (86 lines) | stat: -rw-r--r-- 2,615 bytes parent folder | download | duplicates (3)
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#![cfg(not(target_os = "unknown"))]

use std::net::{Ipv4Addr, Ipv6Addr, SocketAddr, SocketAddrV4, SocketAddrV6};

use async_std::net::ToSocketAddrs;
use async_std::task;

fn blocking_resolve<A>(a: A) -> Result<Vec<SocketAddr>, String>
where
    A: ToSocketAddrs,
    A::Iter: Send,
{
    let socket_addrs = task::block_on(a.to_socket_addrs());
    match socket_addrs {
        Ok(a) => Ok(a.collect()),
        Err(e) => Err(e.to_string()),
    }
}

#[test]
fn to_socket_addr_ipaddr_u16() {
    let a = Ipv4Addr::new(77, 88, 21, 11);
    let p = 12345;
    let e = SocketAddr::V4(SocketAddrV4::new(a, p));
    assert_eq!(Ok(vec![e]), blocking_resolve((a, p)));
}

#[test]
fn to_socket_addr_str_u16() {
    let a = SocketAddr::V4(SocketAddrV4::new(Ipv4Addr::new(77, 88, 21, 11), 24352));
    assert_eq!(Ok(vec![a]), blocking_resolve(("77.88.21.11", 24352)));

    let a = SocketAddr::V6(SocketAddrV6::new(
        Ipv6Addr::new(0x2a02, 0x6b8, 0, 1, 0, 0, 0, 1),
        53,
        0,
        0,
    ));
    assert_eq!(Ok(vec![a]), blocking_resolve(("2a02:6b8:0:1::1", 53)));

    let a = SocketAddr::V4(SocketAddrV4::new(Ipv4Addr::new(127, 0, 0, 1), 23924));
    #[cfg(not(target_env = "sgx"))]
    assert!(blocking_resolve(("localhost", 23924)).unwrap().contains(&a));
    #[cfg(target_env = "sgx")]
    let _ = a;
}

#[test]
fn to_socket_addr_str() {
    let a = SocketAddr::V4(SocketAddrV4::new(Ipv4Addr::new(77, 88, 21, 11), 24352));
    assert_eq!(Ok(vec![a]), blocking_resolve("77.88.21.11:24352"));

    let a = SocketAddr::V6(SocketAddrV6::new(
        Ipv6Addr::new(0x2a02, 0x6b8, 0, 1, 0, 0, 0, 1),
        53,
        0,
        0,
    ));
    assert_eq!(Ok(vec![a]), blocking_resolve("[2a02:6b8:0:1::1]:53"));

    let a = SocketAddr::V4(SocketAddrV4::new(Ipv4Addr::new(127, 0, 0, 1), 23924));
    #[cfg(not(target_env = "sgx"))]
    assert!(blocking_resolve("localhost:23924").unwrap().contains(&a));
    #[cfg(target_env = "sgx")]
    let _ = a;
}

#[test]
fn to_socket_addr_string() {
    let a = SocketAddr::V4(SocketAddrV4::new(Ipv4Addr::new(77, 88, 21, 11), 24352));
    let s: &str = "77.88.21.11:24352";
    assert_eq!(Ok(vec![a]), blocking_resolve(s));

    let s: &String = &"77.88.21.11:24352".to_string();
    assert_eq!(Ok(vec![a]), blocking_resolve(s));

    let s: String = "77.88.21.11:24352".to_string();
    assert_eq!(Ok(vec![a]), blocking_resolve(s));
}

// FIXME: figure out why this fails on openbsd and fix it
#[test]
#[cfg(not(any(windows, target_os = "openbsd")))]
fn to_socket_addr_str_bad() {
    assert!(blocking_resolve("1200::AB00:1234::2552:7777:1313:34300").is_err());
}