forked from bytecodealliance/wasmtime
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
wasi-sockets: Simplify ip name lookup interface (bytecodealliance#7483)
* Refactor ip_name_lookup test * Update ip-name-lookup::resolve-addresses - Remove the non-essential parameters - Lift the restriction against parsing IP addresses. Implementations would still have to parse IP addresses to decide whether or not to return an error * Deduplicate to_canonical
- Loading branch information
1 parent
42f3c93
commit 21e8025
Showing
8 changed files
with
187 additions
and
139 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,70 @@ | ||
use test_programs::wasi::clocks::*; | ||
use test_programs::wasi::io::*; | ||
use test_programs::wasi::sockets::network::{ErrorCode, IpAddress}; | ||
use test_programs::wasi::sockets::*; | ||
|
||
fn main() { | ||
let network = instance_network::instance_network(); | ||
// Valid domains | ||
resolve("localhost").unwrap(); | ||
resolve("example.com").unwrap(); | ||
resolve("münchen.de").unwrap(); | ||
|
||
// Valid IP addresses | ||
assert_eq!(resolve_one("0.0.0.0").unwrap(), IpAddress::IPV4_UNSPECIFIED); | ||
assert_eq!(resolve_one("127.0.0.1").unwrap(), IpAddress::IPV4_LOOPBACK); | ||
assert_eq!( | ||
resolve_one("192.0.2.0").unwrap(), | ||
IpAddress::Ipv4((192, 0, 2, 0)) | ||
); | ||
assert_eq!(resolve_one("::").unwrap(), IpAddress::IPV6_UNSPECIFIED); | ||
assert_eq!(resolve_one("::1").unwrap(), IpAddress::IPV6_LOOPBACK); | ||
assert_eq!(resolve_one("[::]").unwrap(), IpAddress::IPV6_UNSPECIFIED); | ||
assert_eq!( | ||
resolve_one("2001:0db8:0:0:0:0:0:0").unwrap(), | ||
IpAddress::Ipv6((0x2001, 0x0db8, 0, 0, 0, 0, 0, 0)) | ||
); | ||
assert_eq!( | ||
resolve_one("dead:beef::").unwrap(), | ||
IpAddress::Ipv6((0xdead, 0xbeef, 0, 0, 0, 0, 0, 0)) | ||
); | ||
assert_eq!( | ||
resolve_one("dead:beef::0").unwrap(), | ||
IpAddress::Ipv6((0xdead, 0xbeef, 0, 0, 0, 0, 0, 0)) | ||
); | ||
assert_eq!( | ||
resolve_one("DEAD:BEEF::0").unwrap(), | ||
IpAddress::Ipv6((0xdead, 0xbeef, 0, 0, 0, 0, 0, 0)) | ||
); | ||
|
||
let addresses = | ||
ip_name_lookup::resolve_addresses(&network, "example.com", None, false).unwrap(); | ||
let pollable = addresses.subscribe(); | ||
pollable.block(); | ||
assert!(addresses.resolve_next_address().is_ok()); | ||
// Invalid inputs | ||
assert_eq!(resolve("").unwrap_err(), ErrorCode::InvalidArgument); | ||
assert_eq!(resolve(" ").unwrap_err(), ErrorCode::InvalidArgument); | ||
assert_eq!(resolve("a.b<&>").unwrap_err(), ErrorCode::InvalidArgument); | ||
assert_eq!( | ||
resolve("127.0.0.1:80").unwrap_err(), | ||
ErrorCode::InvalidArgument | ||
); | ||
assert_eq!(resolve("[::]:80").unwrap_err(), ErrorCode::InvalidArgument); | ||
assert_eq!( | ||
resolve("http://example.com/").unwrap_err(), | ||
ErrorCode::InvalidArgument | ||
); | ||
} | ||
|
||
let result = ip_name_lookup::resolve_addresses(&network, "a.b<&>", None, false); | ||
assert!(matches!(result, Err(network::ErrorCode::InvalidArgument))); | ||
fn resolve(name: &str) -> Result<Vec<IpAddress>, ErrorCode> { | ||
let network = instance_network::instance_network(); | ||
|
||
// Try resolving a valid address and ensure that it eventually terminates. | ||
// To help prevent this test from being flaky this additionally times out | ||
// the resolution and allows errors. | ||
let addresses = ip_name_lookup::resolve_addresses(&network, "github.com", None, false).unwrap(); | ||
let lookup = addresses.subscribe(); | ||
let timeout = monotonic_clock::subscribe_duration(1_000_000_000); | ||
let ready = poll::poll(&[&lookup, &timeout]); | ||
assert!(ready.len() > 0); | ||
match ready[0] { | ||
0 => loop { | ||
match addresses.resolve_next_address() { | ||
Ok(Some(_)) => {} | ||
Ok(None) => break, | ||
Err(_) => break, | ||
} | ||
}, | ||
1 => {} | ||
_ => unreachable!(), | ||
match network.blocking_resolve_addresses(name) { | ||
// The following error codes signal that the input passed validation | ||
// and a lookup was actually attempted, but failed. Ignore these to | ||
// make the CI tests less flaky: | ||
Err( | ||
ErrorCode::NameUnresolvable | ||
| ErrorCode::TemporaryResolverFailure | ||
| ErrorCode::PermanentResolverFailure, | ||
) => Ok(vec![]), | ||
r => r, | ||
} | ||
} | ||
|
||
fn resolve_one(name: &str) -> Result<IpAddress, ErrorCode> { | ||
Ok(resolve(name)?.first().unwrap().to_owned()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.