From b8610dc097cc964f4a26ead6644fdb170b58aa91 Mon Sep 17 00:00:00 2001 From: Nordine Bittich Date: Sat, 7 Dec 2024 19:46:14 +0100 Subject: [PATCH] move iri test into a separate module --- src/iri.rs | 390 +----------------------------------------- src/tests/iri_test.rs | 377 ++++++++++++++++++++++++++++++++++++++++ src/tests/mod.rs | 1 + 3 files changed, 381 insertions(+), 387 deletions(-) create mode 100644 src/tests/iri_test.rs diff --git a/src/iri.rs b/src/iri.rs index 5f31317..60b5dd6 100644 --- a/src/iri.rs +++ b/src/iri.rs @@ -118,7 +118,7 @@ impl IRI<'_> { } } } -mod ip { +pub(crate) mod ip { use nom::{ bytes::complete::take_while_m_n, combinator::{success, verify}, @@ -132,7 +132,7 @@ mod ip { Compressed, IpV4(Vec), } - pub(super) fn parse_ip_v6(s: &str) -> ParserResult> { + pub(crate) fn parse_ip_v6(s: &str) -> ParserResult> { fn hex_to_u16(input: &str) -> Result { u16::from_str_radix(input, 16) } @@ -187,7 +187,7 @@ mod ip { Ok((rest, ipv6)) } - pub(super) fn parse_ip_v4(s: &str) -> ParserResult> { + pub(crate) fn parse_ip_v4(s: &str) -> ParserResult> { verify( separated_list1( tag("."), @@ -487,387 +487,3 @@ mod parser { u32::from_str_radix(hex, 16).ok().and_then(char::from_u32) } } - -#[cfg(test)] -mod test { - - use crate::iri::{ - ip::{parse_ip_v4, parse_ip_v6}, - Authority, Host, IHierPart, IPath, RelativePart, RelativeRef, - }; - - use super::IRI; - - #[test] - fn test_hex_st_to_char() { - assert_eq!(':', u8::from_str_radix("3A", 16).unwrap() as char); - } - #[test] - fn parse_ip_v4_test() { - assert_eq!( - parse_ip_v4("192.168.0.1").unwrap(), - ("", [192, 168, 0, 1].to_vec()) - ); - assert_eq!( - parse_ip_v4("127.0.0.1").unwrap(), - ("", [127, 0, 0, 1].to_vec()) - ); - assert_eq!(parse_ip_v4("8.8.8.8").unwrap(), ("", [8, 8, 8, 8].to_vec())); - assert_eq!( - parse_ip_v4("255.255.255.255").unwrap(), - ("", [255, 255, 255, 255].to_vec()) - ); - assert!(parse_ip_v4("256.1.1.1").is_err()); - assert!(parse_ip_v4("192.168.0").is_err()); - assert!(parse_ip_v4("192.168..1").is_err()); - } - - #[test] - fn parse_ip_v6_test() { - assert_eq!( - parse_ip_v6("2001:0db8:85a3:0000:0000:8a2e:0370:7334").unwrap(), - ( - "", - [0x2001, 0x0db8, 0x85a3, 0, 0, 0x8a2e, 0x370, 0x7334].into() - ) - ); - assert_eq!( - parse_ip_v6("2001:0db8:0000:0000:0000:0000:0000:0001").unwrap(), - ("", [0x2001, 0x0db8, 0, 0, 0, 0, 0, 1].into()) - ); - - assert_eq!( - parse_ip_v6("2001:0db8:0000:0000:0000:ff00:0042:8329").unwrap(), - ("", [0x2001, 0x0db8, 0, 0, 0, 0xff00, 0x42, 0x8329].into()) - ); - - assert_eq!( - parse_ip_v6("2001:db8:0:0:0:ff00:42:8329").unwrap(), - ("", [0x2001, 0x0db8, 0, 0, 0, 0xff00, 0x42, 0x8329].into()) - ); - - assert!(parse_ip_v6("2001:db8::::ff00:42:8329").is_err()); - assert_eq!( - parse_ip_v6("::ffff:192.0.2.128").unwrap(), - ("", [0, 0, 0, 0, 0, 0xffff, 0xc000, 0x280].into()) - ); - let test_cases = [ - ( - "2001:0db8:85a3:0000:0000:8a2e:0370:7334", - vec![ - 0x2001, 0x0db8, 0x85a3, 0x0000, 0x0000, 0x8a2e, 0x0370, 0x7334, - ], - ), - ( - "2001:db8:85a3::8a2e:370:7334", - vec![ - 0x2001, 0xdb8, 0x85a3, 0x0000, 0x0000, 0x8a2e, 0x0370, 0x7334, - ], - ), - ( - "2001:db8:85a3:0:0:8a2e:0370:7334", - vec![ - 0x2001, 0xdb8, 0x85a3, 0x0000, 0x0000, 0x8a2e, 0x0370, 0x7334, - ], - ), - ( - "2001:db8::370:7334", - vec![0x2001, 0xdb8, 0x0, 0x0, 0x0, 0x0, 0x370, 0x7334], - ), - ( - "2001:0db8:0000:0000:0000:ff00:0042:8329", - vec![0x2001, 0x0db8, 0x0, 0x0, 0x0, 0xff00, 0x42, 0x8329], - ), - ( - "fe80::1ff:fe23:4567:890a", - vec![0xfe80, 0x0, 0x0, 0x0, 0x1ff, 0xfe23, 0x4567, 0x890a], - ), - ( - "0:0:0:0:0:0:0:0", - vec![0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0], - ), - ( - "0:0:0:0:0:0:0:1", - vec![0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1], - ), - ( - "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff", - vec![ - 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, - ], - ), - ]; - - for (addr, expected) in test_cases.into_iter() { - let result = parse_ip_v6(addr).unwrap(); - assert_eq!(result, ("", expected),); - } - - assert!(parse_ip_v6("192.168.1.1:0::ffff").is_err()); - } - - #[test] - - fn test_iris() { - let iri = IRI::try_from("http://example.com/").unwrap(); - assert_eq!( - iri, - IRI::IRI { - scheme: "http", - hier_part: IHierPart::AbEmpty { - authority: Authority { - user_info: None, - host: Host::RegName(Some("example.com",),), - port: None, - }, - ipath: IPath::AbEmpty(vec!["/",],), - }, - query: "", - fragment: "", - } - ); - - let iri = IRI::try_from("https://example.com/page").unwrap(); - assert_eq!( - iri, - IRI::IRI { - scheme: "https", - hier_part: IHierPart::AbEmpty { - authority: Authority { - user_info: None, - host: Host::RegName(Some("example.com",),), - port: None, - }, - ipath: IPath::AbEmpty(vec!["/page",],), - }, - query: "", - fragment: "", - } - ); - - let iri = IRI::try_from("ftp://ftp.example.org/file.txt").unwrap(); - assert_eq!( - iri, - IRI::IRI { - scheme: "ftp", - hier_part: IHierPart::AbEmpty { - authority: Authority { - user_info: None, - host: Host::RegName(Some("ftp.example.org",),), - port: None, - }, - ipath: IPath::AbEmpty(vec!["/file.txt",],), - }, - query: "", - fragment: "", - } - ); - - let iri = IRI::try_from("http://example.com/a/b%20c").unwrap(); - assert_eq!( - iri, - IRI::IRI { - scheme: "http", - hier_part: IHierPart::AbEmpty { - authority: Authority { - user_info: None, - host: Host::RegName(Some("example.com",),), - port: None, - }, - ipath: IPath::AbEmpty(vec!["/a", "/b%20c"],), - }, - query: "", - fragment: "", - } - ); - - let iri = IRI::try_from("http://example.com/a/こんにちは").unwrap(); - assert_eq!( - iri, - IRI::IRI { - scheme: "http", - hier_part: IHierPart::AbEmpty { - authority: Authority { - user_info: None, - host: Host::RegName(Some("example.com",),), - port: None, - }, - ipath: IPath::AbEmpty(vec!["/a", "/こんにちは"],), - }, - query: "", - fragment: "", - } - ); - - let iri = IRI::try_from("mailto:user@example.com").unwrap(); - assert_eq!( - iri, - IRI::IRI { - scheme: "mailto", - hier_part: IHierPart::Rootless(IPath::Rootless { - snz: "user@example.com", - segments: vec![] - }), - query: "", - fragment: "", - } - ); - - let iri = IRI::try_from("http://example.com/?q=foo%3Dbar").unwrap(); - assert_eq!( - iri, - IRI::IRI { - scheme: "http", - hier_part: IHierPart::AbEmpty { - authority: Authority { - user_info: None, - host: Host::RegName(Some("example.com",),), - port: None, - }, - ipath: IPath::AbEmpty(vec!["/"],), - }, - query: "q=foo%3Dbar", - fragment: "", - } - ); - let iri = IRI::try_from("/a/b/c").unwrap(); - assert_eq!( - iri, - IRI::Reference(RelativeRef { - relative_part: RelativePart::Absolute(IPath::Absolute { - snz: "", - segments: vec!["/a", "/b", "/c",], - },), - query: "", - fragment: "", - },) - ); - - let iri = IRI::try_from("./c").unwrap(); - assert_eq!( - iri, - IRI::Reference(RelativeRef { - relative_part: RelativePart::NoScheme(IPath::NoScheme { - snz_nc: ".", - segments: vec!["/c",], - },), - query: "", - fragment: "", - },) - ); - let iri = IRI::try_from("../b/c").unwrap(); - assert_eq!( - iri, - IRI::Reference(RelativeRef { - relative_part: RelativePart::NoScheme(IPath::NoScheme { - snz_nc: "..", - segments: vec!["/b", "/c",], - },), - query: "", - fragment: "", - },) - ); - let iri = IRI::try_from("http://xn--fsq.com").unwrap(); - assert_eq!( - iri, - IRI::IRI { - scheme: "http", - hier_part: IHierPart::AbEmpty { - authority: Authority { - user_info: None, - host: Host::RegName(Some("xn--fsq.com",),), - port: None, - }, - ipath: IPath::AbEmpty(vec![],), - }, - query: "", - fragment: "", - } - ); - let iri = IRI::try_from("http://[2001:db8::1]/path").unwrap(); - assert_eq!( - iri, - IRI::IRI { - scheme: "http", - hier_part: IHierPart::AbEmpty { - authority: Authority { - user_info: None, - host: Host::IPV6(vec![0x2001, 0xdb8, 0, 0, 0, 0, 0, 1]), - port: None, - }, - ipath: IPath::AbEmpty(vec!["/path"],), - }, - query: "", - fragment: "", - } - ); - let iri = IRI::try_from("ftp://example.com/path?query=1¶m=2#fragment").unwrap(); - assert_eq!( - iri, - IRI::IRI { - scheme: "ftp", - hier_part: IHierPart::AbEmpty { - authority: Authority { - user_info: None, - host: Host::RegName(Some("example.com",),), - port: None, - }, - ipath: IPath::AbEmpty(vec!["/path"],), - }, - query: "query=1¶m=2", - fragment: "fragment", - } - ); - let iri = IRI::try_from("http://user:pass@example.com:8080/path?q#frag").unwrap(); - assert_eq!( - iri, - IRI::IRI { - scheme: "http", - hier_part: IHierPart::AbEmpty { - authority: Authority { - user_info: Some("user:pass"), - host: Host::RegName(Some("example.com")), - port: Some("8080"), - }, - ipath: IPath::AbEmpty(vec!["/path"],), - }, - query: "q", - fragment: "frag", - } - ); - assert!(IRI::try_from("://example.com").is_err()); - assert!(IRI::try_from("`http://example.com/").is_err()); - assert!(IRI::try_from("http://example.com/\n/path").is_err()); - let iri = IRI::try_from("path/").unwrap(); - assert_eq!( - iri, - IRI::Reference(RelativeRef { - relative_part: RelativePart::NoScheme(IPath::NoScheme { - snz_nc: "path", - segments: vec!["/",], - },), - query: "", - fragment: "", - },) - ); - assert_eq!( - IRI::try_from("about").unwrap(), - IRI::Reference(RelativeRef { - relative_part: RelativePart::NoScheme(IPath::NoScheme { - snz_nc: "about", - segments: vec![], - },), - query: "", - fragment: "", - },) - ); - assert_eq!( - IRI::try_from("").unwrap(), - IRI::Reference(RelativeRef { - relative_part: RelativePart::Empty(IPath::Empty), - query: "", - fragment: "", - },) - ); - } -} diff --git a/src/tests/iri_test.rs b/src/tests/iri_test.rs new file mode 100644 index 0000000..b3b9c7c --- /dev/null +++ b/src/tests/iri_test.rs @@ -0,0 +1,377 @@ +use crate::iri::{ + ip::{parse_ip_v4, parse_ip_v6}, + Authority, Host, IHierPart, IPath, RelativePart, RelativeRef, IRI, +}; + +#[test] +fn test_hex_st_to_char() { + assert_eq!(':', u8::from_str_radix("3A", 16).unwrap() as char); +} +#[test] +fn parse_ip_v4_test() { + assert_eq!( + parse_ip_v4("192.168.0.1").unwrap(), + ("", [192, 168, 0, 1].to_vec()) + ); + assert_eq!( + parse_ip_v4("127.0.0.1").unwrap(), + ("", [127, 0, 0, 1].to_vec()) + ); + assert_eq!(parse_ip_v4("8.8.8.8").unwrap(), ("", [8, 8, 8, 8].to_vec())); + assert_eq!( + parse_ip_v4("255.255.255.255").unwrap(), + ("", [255, 255, 255, 255].to_vec()) + ); + assert!(parse_ip_v4("256.1.1.1").is_err()); + assert!(parse_ip_v4("192.168.0").is_err()); + assert!(parse_ip_v4("192.168..1").is_err()); +} + +#[test] +fn parse_ip_v6_test() { + assert_eq!( + parse_ip_v6("2001:0db8:85a3:0000:0000:8a2e:0370:7334").unwrap(), + ( + "", + [0x2001, 0x0db8, 0x85a3, 0, 0, 0x8a2e, 0x370, 0x7334].into() + ) + ); + assert_eq!( + parse_ip_v6("2001:0db8:0000:0000:0000:0000:0000:0001").unwrap(), + ("", [0x2001, 0x0db8, 0, 0, 0, 0, 0, 1].into()) + ); + + assert_eq!( + parse_ip_v6("2001:0db8:0000:0000:0000:ff00:0042:8329").unwrap(), + ("", [0x2001, 0x0db8, 0, 0, 0, 0xff00, 0x42, 0x8329].into()) + ); + + assert_eq!( + parse_ip_v6("2001:db8:0:0:0:ff00:42:8329").unwrap(), + ("", [0x2001, 0x0db8, 0, 0, 0, 0xff00, 0x42, 0x8329].into()) + ); + + assert!(parse_ip_v6("2001:db8::::ff00:42:8329").is_err()); + assert_eq!( + parse_ip_v6("::ffff:192.0.2.128").unwrap(), + ("", [0, 0, 0, 0, 0, 0xffff, 0xc000, 0x280].into()) + ); + let test_cases = [ + ( + "2001:0db8:85a3:0000:0000:8a2e:0370:7334", + vec![ + 0x2001, 0x0db8, 0x85a3, 0x0000, 0x0000, 0x8a2e, 0x0370, 0x7334, + ], + ), + ( + "2001:db8:85a3::8a2e:370:7334", + vec![ + 0x2001, 0xdb8, 0x85a3, 0x0000, 0x0000, 0x8a2e, 0x0370, 0x7334, + ], + ), + ( + "2001:db8:85a3:0:0:8a2e:0370:7334", + vec![ + 0x2001, 0xdb8, 0x85a3, 0x0000, 0x0000, 0x8a2e, 0x0370, 0x7334, + ], + ), + ( + "2001:db8::370:7334", + vec![0x2001, 0xdb8, 0x0, 0x0, 0x0, 0x0, 0x370, 0x7334], + ), + ( + "2001:0db8:0000:0000:0000:ff00:0042:8329", + vec![0x2001, 0x0db8, 0x0, 0x0, 0x0, 0xff00, 0x42, 0x8329], + ), + ( + "fe80::1ff:fe23:4567:890a", + vec![0xfe80, 0x0, 0x0, 0x0, 0x1ff, 0xfe23, 0x4567, 0x890a], + ), + ( + "0:0:0:0:0:0:0:0", + vec![0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0], + ), + ( + "0:0:0:0:0:0:0:1", + vec![0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1], + ), + ( + "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff", + vec![ + 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, + ], + ), + ]; + + for (addr, expected) in test_cases.into_iter() { + let result = parse_ip_v6(addr).unwrap(); + assert_eq!(result, ("", expected),); + } + + assert!(parse_ip_v6("192.168.1.1:0::ffff").is_err()); +} + +#[test] + +fn test_iris() { + let iri = IRI::try_from("http://example.com/").unwrap(); + assert_eq!( + iri, + IRI::IRI { + scheme: "http", + hier_part: IHierPart::AbEmpty { + authority: Authority { + user_info: None, + host: Host::RegName(Some("example.com",),), + port: None, + }, + ipath: IPath::AbEmpty(vec!["/",],), + }, + query: "", + fragment: "", + } + ); + + let iri = IRI::try_from("https://example.com/page").unwrap(); + assert_eq!( + iri, + IRI::IRI { + scheme: "https", + hier_part: IHierPart::AbEmpty { + authority: Authority { + user_info: None, + host: Host::RegName(Some("example.com",),), + port: None, + }, + ipath: IPath::AbEmpty(vec!["/page",],), + }, + query: "", + fragment: "", + } + ); + + let iri = IRI::try_from("ftp://ftp.example.org/file.txt").unwrap(); + assert_eq!( + iri, + IRI::IRI { + scheme: "ftp", + hier_part: IHierPart::AbEmpty { + authority: Authority { + user_info: None, + host: Host::RegName(Some("ftp.example.org",),), + port: None, + }, + ipath: IPath::AbEmpty(vec!["/file.txt",],), + }, + query: "", + fragment: "", + } + ); + + let iri = IRI::try_from("http://example.com/a/b%20c").unwrap(); + assert_eq!( + iri, + IRI::IRI { + scheme: "http", + hier_part: IHierPart::AbEmpty { + authority: Authority { + user_info: None, + host: Host::RegName(Some("example.com",),), + port: None, + }, + ipath: IPath::AbEmpty(vec!["/a", "/b%20c"],), + }, + query: "", + fragment: "", + } + ); + + let iri = IRI::try_from("http://example.com/a/こんにちは").unwrap(); + assert_eq!( + iri, + IRI::IRI { + scheme: "http", + hier_part: IHierPart::AbEmpty { + authority: Authority { + user_info: None, + host: Host::RegName(Some("example.com",),), + port: None, + }, + ipath: IPath::AbEmpty(vec!["/a", "/こんにちは"],), + }, + query: "", + fragment: "", + } + ); + + let iri = IRI::try_from("mailto:user@example.com").unwrap(); + assert_eq!( + iri, + IRI::IRI { + scheme: "mailto", + hier_part: IHierPart::Rootless(IPath::Rootless { + snz: "user@example.com", + segments: vec![] + }), + query: "", + fragment: "", + } + ); + + let iri = IRI::try_from("http://example.com/?q=foo%3Dbar").unwrap(); + assert_eq!( + iri, + IRI::IRI { + scheme: "http", + hier_part: IHierPart::AbEmpty { + authority: Authority { + user_info: None, + host: Host::RegName(Some("example.com",),), + port: None, + }, + ipath: IPath::AbEmpty(vec!["/"],), + }, + query: "q=foo%3Dbar", + fragment: "", + } + ); + let iri = IRI::try_from("/a/b/c").unwrap(); + assert_eq!( + iri, + IRI::Reference(RelativeRef { + relative_part: RelativePart::Absolute(IPath::Absolute { + snz: "", + segments: vec!["/a", "/b", "/c",], + },), + query: "", + fragment: "", + },) + ); + + let iri = IRI::try_from("./c").unwrap(); + assert_eq!( + iri, + IRI::Reference(RelativeRef { + relative_part: RelativePart::NoScheme(IPath::NoScheme { + snz_nc: ".", + segments: vec!["/c",], + },), + query: "", + fragment: "", + },) + ); + let iri = IRI::try_from("../b/c").unwrap(); + assert_eq!( + iri, + IRI::Reference(RelativeRef { + relative_part: RelativePart::NoScheme(IPath::NoScheme { + snz_nc: "..", + segments: vec!["/b", "/c",], + },), + query: "", + fragment: "", + },) + ); + let iri = IRI::try_from("http://xn--fsq.com").unwrap(); + assert_eq!( + iri, + IRI::IRI { + scheme: "http", + hier_part: IHierPart::AbEmpty { + authority: Authority { + user_info: None, + host: Host::RegName(Some("xn--fsq.com",),), + port: None, + }, + ipath: IPath::AbEmpty(vec![],), + }, + query: "", + fragment: "", + } + ); + let iri = IRI::try_from("http://[2001:db8::1]/path").unwrap(); + assert_eq!( + iri, + IRI::IRI { + scheme: "http", + hier_part: IHierPart::AbEmpty { + authority: Authority { + user_info: None, + host: Host::IPV6(vec![0x2001, 0xdb8, 0, 0, 0, 0, 0, 1]), + port: None, + }, + ipath: IPath::AbEmpty(vec!["/path"],), + }, + query: "", + fragment: "", + } + ); + let iri = IRI::try_from("ftp://example.com/path?query=1¶m=2#fragment").unwrap(); + assert_eq!( + iri, + IRI::IRI { + scheme: "ftp", + hier_part: IHierPart::AbEmpty { + authority: Authority { + user_info: None, + host: Host::RegName(Some("example.com",),), + port: None, + }, + ipath: IPath::AbEmpty(vec!["/path"],), + }, + query: "query=1¶m=2", + fragment: "fragment", + } + ); + let iri = IRI::try_from("http://user:pass@example.com:8080/path?q#frag").unwrap(); + assert_eq!( + iri, + IRI::IRI { + scheme: "http", + hier_part: IHierPart::AbEmpty { + authority: Authority { + user_info: Some("user:pass"), + host: Host::RegName(Some("example.com")), + port: Some("8080"), + }, + ipath: IPath::AbEmpty(vec!["/path"],), + }, + query: "q", + fragment: "frag", + } + ); + assert!(IRI::try_from("://example.com").is_err()); + assert!(IRI::try_from("`http://example.com/").is_err()); + assert!(IRI::try_from("http://example.com/\n/path").is_err()); + let iri = IRI::try_from("path/").unwrap(); + assert_eq!( + iri, + IRI::Reference(RelativeRef { + relative_part: RelativePart::NoScheme(IPath::NoScheme { + snz_nc: "path", + segments: vec!["/",], + },), + query: "", + fragment: "", + },) + ); + assert_eq!( + IRI::try_from("about").unwrap(), + IRI::Reference(RelativeRef { + relative_part: RelativePart::NoScheme(IPath::NoScheme { + snz_nc: "about", + segments: vec![], + },), + query: "", + fragment: "", + },) + ); + assert_eq!( + IRI::try_from("").unwrap(), + IRI::Reference(RelativeRef { + relative_part: RelativePart::Empty(IPath::Empty), + query: "", + fragment: "", + },) + ); +} diff --git a/src/tests/mod.rs b/src/tests/mod.rs index a83d2b2..b396a4b 100644 --- a/src/tests/mod.rs +++ b/src/tests/mod.rs @@ -90,6 +90,7 @@ fn cmp_input_file( assert_eq!(input.len(), output.len()); } } +mod iri_test; mod triple_common_parser_test_misc; mod turtle_doc_test; mod turtle_doc_test_misc;