0
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2025-03-04 01:44:26 -05:00

simplify check_net test

This commit is contained in:
Ryan Dahl 2019-06-24 19:30:11 -04:00
parent d1482c6b8a
commit 046cbef4f0

View file

@ -552,7 +552,7 @@ mod tests {
} }
#[test] #[test]
fn check_net() { fn test_check_net() {
let perms = DenoPermissions::from_flags(&DenoFlags { let perms = DenoPermissions::from_flags(&DenoFlags {
net_whitelist: svec![ net_whitelist: svec![
"localhost", "localhost",
@ -565,195 +565,73 @@ mod tests {
..Default::default() ..Default::default()
}); });
// Any protocol + port for localhost should be ok, since we don't specify let domain_tests = vec![
assert!( ("localhost:1234", true),
perms ("deno.land", true),
.check_net_url(url::Url::parse("http://localhost").unwrap()) ("deno.land:3000", true),
.is_ok() ("deno.lands", false),
); ("deno.lands:3000", false),
assert!( ("github.com:3000", true),
perms ("github.com", false),
.check_net_url(url::Url::parse("http://localhost:8080").unwrap()) ("github.com:2000", false),
.is_ok() ("github.net:3000", false),
); ("127.0.0.1", true),
assert!( ("127.0.0.1:3000", true),
perms ("127.0.0.2", false),
.check_net_url(url::Url::parse("https://localhost").unwrap()) ("127.0.0.2:3000", false),
.is_ok() ("172.16.0.2:8000", true),
); ("172.16.0.2", false),
assert!( ("172.16.0.2:6000", false),
perms ("172.16.0.1:8000", false),
.check_net_url(url::Url::parse("https://localhost:4443").unwrap()) // Just some random hosts that should err
.is_ok() ("somedomain", false),
); ("192.168.0.1", false),
assert!( ];
perms
.check_net_url(url::Url::parse("tcp://localhost:5000").unwrap())
.is_ok()
);
assert!(
perms
.check_net_url(url::Url::parse("udp://localhost:6000").unwrap())
.is_ok()
);
assert!(perms.check_net("localhost:1234").is_ok());
// Correct domain + any port and protocol should be ok incorrect shouldn't let url_tests = vec![
assert!(perms.check_net("deno.land").is_ok()); // Any protocol + port for localhost should be ok, since we don't specify
assert!( ("http://localhost", true),
perms ("https://localhost", true),
.check_net_url( ("https://localhost:4443", true),
url::Url::parse("https://deno.land/std/example/welcome.ts").unwrap() ("tcp://localhost:5000", true),
).is_ok() ("udp://localhost:6000", true),
); // Correct domain + any port and protocol should be ok incorrect shouldn't
assert!(perms.check_net("deno.land:3000").is_ok()); ("https://deno.land/std/example/welcome.ts", true),
assert!( ("https://deno.land:3000/std/example/welcome.ts", true),
perms ("https://deno.lands/std/example/welcome.ts", false),
.check_net_url( ("https://deno.lands:3000/std/example/welcome.ts", false),
url::Url::parse("https://deno.land:3000/std/example/welcome.ts") // Correct domain + port should be ok all other combinations should err
.unwrap() ("https://github.com:3000/denoland/deno", true),
).is_ok() ("https://github.com/denoland/deno", false),
); ("https://github.com:2000/denoland/deno", false),
assert!(perms.check_net("deno.lands").is_err()); ("https://github.net:3000/denoland/deno", false),
assert!( // Correct ipv4 address + any port should be ok others should err
perms ("tcp://127.0.0.1", true),
.check_net_url( ("https://127.0.0.1", true),
url::Url::parse("https://deno.lands/std/example/welcome.ts").unwrap() ("tcp://127.0.0.1:3000", true),
).is_err() ("https://127.0.0.1:3000", true),
); ("tcp://127.0.0.2", false),
assert!(perms.check_net("deno.lands:3000").is_err()); ("https://127.0.0.2", false),
assert!( ("tcp://127.0.0.2:3000", false),
perms ("https://127.0.0.2:3000", false),
.check_net_url( // Correct address + port should be ok all other combinations should err
url::Url::parse("https://deno.lands:3000/std/example/welcome.ts") ("tcp://172.16.0.2:8000", true),
.unwrap() ("https://172.16.0.2:8000", true),
).is_err() ("tcp://172.16.0.2", false),
); ("https://172.16.0.2", false),
("tcp://172.16.0.2:6000", false),
("https://172.16.0.2:6000", false),
("tcp://172.16.0.1:8000", false),
("https://172.16.0.1:8000", false),
];
// Correct domain + port should be ok all other combinations should err for (url_str, is_ok) in url_tests.iter() {
assert!(perms.check_net("github.com:3000").is_ok()); let u = url::Url::parse(url_str).unwrap();
assert!( assert_eq!(*is_ok, perms.check_net_url(u).is_ok());
perms }
.check_net_url(
url::Url::parse("https://github.com:3000/denoland/deno").unwrap()
).is_ok()
);
assert!(perms.check_net("github.com").is_err());
assert!(
perms
.check_net_url(
url::Url::parse("https://github.com/denoland/deno").unwrap()
).is_err()
);
assert!(perms.check_net("github.com:2000").is_err());
assert!(
perms
.check_net_url(
url::Url::parse("https://github.com:2000/denoland/deno").unwrap()
).is_err()
);
assert!(perms.check_net("github.net:3000").is_err());
assert!(
perms
.check_net_url(
url::Url::parse("https://github.net:3000/denoland/deno").unwrap()
).is_err()
);
// Correct ipv4 address + any port should be ok others should err for (domain, is_ok) in domain_tests.iter() {
assert!(perms.check_net("127.0.0.1").is_ok()); assert_eq!(*is_ok, perms.check_net(domain).is_ok());
assert!( }
perms
.check_net_url(url::Url::parse("tcp://127.0.0.1").unwrap())
.is_ok()
);
assert!(
perms
.check_net_url(url::Url::parse("https://127.0.0.1").unwrap())
.is_ok()
);
assert!(perms.check_net("127.0.0.1:3000").is_ok());
assert!(
perms
.check_net_url(url::Url::parse("tcp://127.0.0.1:3000").unwrap())
.is_ok()
);
assert!(
perms
.check_net_url(url::Url::parse("https://127.0.0.1:3000").unwrap())
.is_ok()
);
assert!(perms.check_net("127.0.0.2").is_err());
assert!(
perms
.check_net_url(url::Url::parse("tcp://127.0.0.2").unwrap())
.is_err()
);
assert!(
perms
.check_net_url(url::Url::parse("https://127.0.0.2").unwrap())
.is_err()
);
assert!(perms.check_net("127.0.0.2:3000").is_err());
assert!(
perms
.check_net_url(url::Url::parse("tcp://127.0.0.2:3000").unwrap())
.is_err()
);
assert!(
perms
.check_net_url(url::Url::parse("https://127.0.0.2:3000").unwrap())
.is_err()
);
// Correct address + port should be ok all other combinations should err
assert!(perms.check_net("172.16.0.2:8000").is_ok());
assert!(
perms
.check_net_url(url::Url::parse("tcp://172.16.0.2:8000").unwrap())
.is_ok()
);
assert!(
perms
.check_net_url(url::Url::parse("https://172.16.0.2:8000").unwrap())
.is_ok()
);
assert!(perms.check_net("172.16.0.2").is_err());
assert!(
perms
.check_net_url(url::Url::parse("tcp://172.16.0.2").unwrap())
.is_err()
);
assert!(
perms
.check_net_url(url::Url::parse("https://172.16.0.2").unwrap())
.is_err()
);
assert!(perms.check_net("172.16.0.2:6000").is_err());
assert!(
perms
.check_net_url(url::Url::parse("tcp://172.16.0.2:6000").unwrap())
.is_err()
);
assert!(
perms
.check_net_url(url::Url::parse("https://172.16.0.2:6000").unwrap())
.is_err()
);
assert!(perms.check_net("172.16.0.1:8000").is_err());
assert!(
perms
.check_net_url(url::Url::parse("tcp://172.16.0.1:8000").unwrap())
.is_err()
);
assert!(
perms
.check_net_url(url::Url::parse("https://172.16.0.1:8000").unwrap())
.is_err()
);
// Just some random hosts that should err
assert!(perms.check_net("somedomain").is_err());
assert!(perms.check_net("192.168.0.1").is_err());
} }
} }