aboutsummaryrefslogtreecommitdiffstats
path: root/components/shared/net/tests
diff options
context:
space:
mode:
authorMartin Robinson <mrobinson@igalia.com>2023-10-05 19:47:39 +0200
committerMartin Robinson <mrobinson@igalia.com>2023-11-03 15:38:18 +0000
commitf4d3af296c05260dfbb3deea4f8fa400cb6887d3 (patch)
tree169db2cc68e01a755b30500dd525f1a2ec2da861 /components/shared/net/tests
parent863529d9622c68f0a9535225237eb5e5c5b8c757 (diff)
downloadservo-f4d3af296c05260dfbb3deea4f8fa400cb6887d3.tar.gz
servo-f4d3af296c05260dfbb3deea4f8fa400cb6887d3.zip
Move `*_traits` and other shared types to `shared`
This is the start of the organization of types that are in their own crates in order to break dependency cycles between other crates. The idea here is that putting these packages into their own directory is the first step toward cleaning them up. They have grown organically and it is difficult to explain to new folks where to put new shared types. Many of these crates contain more than traits or don't contain traits at all. Notably, `script_traits` isn't touched because it is vendored from Gecko. Eventually this will move to `third_party`.
Diffstat (limited to 'components/shared/net/tests')
-rw-r--r--components/shared/net/tests/image.rs28
-rw-r--r--components/shared/net/tests/lib.rs212
-rw-r--r--components/shared/net/tests/pub_domains.rs122
-rw-r--r--components/shared/net/tests/whitespace.rs25
4 files changed, 387 insertions, 0 deletions
diff --git a/components/shared/net/tests/image.rs b/components/shared/net/tests/image.rs
new file mode 100644
index 00000000000..a4963702b57
--- /dev/null
+++ b/components/shared/net/tests/image.rs
@@ -0,0 +1,28 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
+
+use net_traits::image::base::detect_image_format;
+
+#[test]
+fn test_supported_images() {
+ let gif1 = [b'G', b'I', b'F', b'8', b'7', b'a'];
+ let gif2 = [b'G', b'I', b'F', b'8', b'9', b'a'];
+ let jpeg = [0xff, 0xd8, 0xff];
+ let png = [0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A];
+ let webp = [
+ b'R', b'I', b'F', b'F', 0x01, 0x02, 0x03, 0x04, b'W', b'E', b'B', b'P', b'V', b'P',
+ ];
+ let bmp = [0x42, 0x4D];
+ let ico = [0x00, 0x00, 0x01, 0x00];
+ let junk_format = [0x01, 0x02, 0x03, 0x04, 0x05];
+
+ assert!(detect_image_format(&gif1).is_ok());
+ assert!(detect_image_format(&gif2).is_ok());
+ assert!(detect_image_format(&jpeg).is_ok());
+ assert!(detect_image_format(&png).is_ok());
+ assert!(detect_image_format(&webp).is_ok());
+ assert!(detect_image_format(&bmp).is_ok());
+ assert!(detect_image_format(&ico).is_ok());
+ assert!(detect_image_format(&junk_format).is_err());
+}
diff --git a/components/shared/net/tests/lib.rs b/components/shared/net/tests/lib.rs
new file mode 100644
index 00000000000..290ca902935
--- /dev/null
+++ b/components/shared/net/tests/lib.rs
@@ -0,0 +1,212 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
+
+use net_traits::{ResourceAttribute, ResourceFetchTiming, ResourceTimeValue, ResourceTimingType};
+
+#[test]
+fn test_set_start_time_to_fetch_start_if_nonzero_tao() {
+ let mut resource_timing: ResourceFetchTiming =
+ ResourceFetchTiming::new(ResourceTimingType::Resource);
+ resource_timing.fetch_start = 1;
+ assert_eq!(resource_timing.start_time, 0, "`start_time` should be zero");
+ assert!(
+ resource_timing.fetch_start > 0,
+ "`fetch_start` should have a positive value"
+ );
+
+ // verify that setting `start_time` to `fetch_start` succeeds
+ resource_timing.set_attribute(ResourceAttribute::StartTime(ResourceTimeValue::FetchStart));
+ assert_eq!(
+ resource_timing.start_time, resource_timing.fetch_start,
+ "`start_time` should equal `fetch_start`"
+ );
+}
+
+#[test]
+fn test_set_start_time_to_fetch_start_if_zero_tao() {
+ let mut resource_timing: ResourceFetchTiming =
+ ResourceFetchTiming::new(ResourceTimingType::Resource);
+ resource_timing.start_time = 1;
+ assert!(
+ resource_timing.start_time > 0,
+ "`start_time` should have a positive value"
+ );
+ assert_eq!(
+ resource_timing.fetch_start, 0,
+ "`fetch_start` should be zero"
+ );
+
+ // verify that setting `start_time` to `fetch_start` succeeds even when `fetch_start` == zero
+ resource_timing.set_attribute(ResourceAttribute::StartTime(ResourceTimeValue::FetchStart));
+ assert_eq!(
+ resource_timing.start_time, resource_timing.fetch_start,
+ "`start_time` should equal `fetch_start`"
+ );
+}
+
+#[test]
+fn test_set_start_time_to_fetch_start_if_nonzero_no_tao() {
+ let mut resource_timing: ResourceFetchTiming =
+ ResourceFetchTiming::new(ResourceTimingType::Resource);
+ resource_timing.mark_timing_check_failed();
+ resource_timing.fetch_start = 1;
+ assert_eq!(resource_timing.start_time, 0, "`start_time` should be zero");
+ assert!(
+ resource_timing.fetch_start > 0,
+ "`fetch_start` should have a positive value"
+ );
+
+ // verify that setting `start_time` to `fetch_start` succeeds even when TAO check failed
+ resource_timing.set_attribute(ResourceAttribute::StartTime(ResourceTimeValue::FetchStart));
+ assert_eq!(
+ resource_timing.start_time, resource_timing.fetch_start,
+ "`start_time` should equal `fetch_start`"
+ );
+}
+
+#[test]
+fn test_set_start_time_to_fetch_start_if_zero_no_tao() {
+ let mut resource_timing: ResourceFetchTiming =
+ ResourceFetchTiming::new(ResourceTimingType::Resource);
+ resource_timing.mark_timing_check_failed();
+ resource_timing.start_time = 1;
+ assert!(
+ resource_timing.start_time > 0,
+ "`start_time` should have a positive value"
+ );
+ assert_eq!(
+ resource_timing.fetch_start, 0,
+ "`fetch_start` should be zero"
+ );
+
+ // verify that setting `start_time` to `fetch_start` succeeds even when `fetch_start`==0 and no TAO
+ resource_timing.set_attribute(ResourceAttribute::StartTime(ResourceTimeValue::FetchStart));
+ assert_eq!(
+ resource_timing.start_time, resource_timing.fetch_start,
+ "`start_time` should equal `fetch_start`"
+ );
+}
+
+#[test]
+fn test_set_start_time_to_redirect_start_if_nonzero_tao() {
+ let mut resource_timing: ResourceFetchTiming =
+ ResourceFetchTiming::new(ResourceTimingType::Resource);
+ resource_timing.redirect_start = 1;
+ assert_eq!(resource_timing.start_time, 0, "`start_time` should be zero");
+ assert!(
+ resource_timing.redirect_start > 0,
+ "`redirect_start` should have a positive value"
+ );
+
+ // verify that setting `start_time` to `redirect_start` succeeds for nonzero `redirect_start`, TAO pass
+ resource_timing.set_attribute(ResourceAttribute::StartTime(
+ ResourceTimeValue::RedirectStart,
+ ));
+ assert_eq!(
+ resource_timing.start_time, resource_timing.redirect_start,
+ "`start_time` should equal `redirect_start`"
+ );
+}
+
+#[test]
+fn test_not_set_start_time_to_redirect_start_if_zero_tao() {
+ let mut resource_timing: ResourceFetchTiming =
+ ResourceFetchTiming::new(ResourceTimingType::Resource);
+ resource_timing.start_time = 1;
+ assert!(
+ resource_timing.start_time > 0,
+ "`start_time` should have a positive value"
+ );
+ assert_eq!(
+ resource_timing.redirect_start, 0,
+ "`redirect_start` should be zero"
+ );
+
+ // verify that setting `start_time` to `redirect_start` fails if `redirect_start` == 0
+ resource_timing.set_attribute(ResourceAttribute::StartTime(
+ ResourceTimeValue::RedirectStart,
+ ));
+ assert_ne!(
+ resource_timing.start_time, resource_timing.redirect_start,
+ "`start_time` should *not* equal `redirect_start`"
+ );
+}
+
+#[test]
+fn test_not_set_start_time_to_redirect_start_if_nonzero_no_tao() {
+ let mut resource_timing: ResourceFetchTiming =
+ ResourceFetchTiming::new(ResourceTimingType::Resource);
+ resource_timing.mark_timing_check_failed();
+ // Note: properly-behaved redirect_start should never be nonzero once TAO check has failed
+ resource_timing.redirect_start = 1;
+ assert_eq!(resource_timing.start_time, 0, "`start_time` should be zero");
+ assert!(
+ resource_timing.redirect_start > 0,
+ "`redirect_start` should have a positive value"
+ );
+
+ // verify that setting `start_time` to `redirect_start` fails if TAO check fails
+ resource_timing.set_attribute(ResourceAttribute::StartTime(
+ ResourceTimeValue::RedirectStart,
+ ));
+ assert_ne!(
+ resource_timing.start_time, resource_timing.redirect_start,
+ "`start_time` should *not* equal `redirect_start`"
+ );
+}
+
+#[test]
+fn test_not_set_start_time_to_redirect_start_if_zero_no_tao() {
+ let mut resource_timing: ResourceFetchTiming =
+ ResourceFetchTiming::new(ResourceTimingType::Resource);
+ resource_timing.mark_timing_check_failed();
+ resource_timing.start_time = 1;
+ assert!(
+ resource_timing.start_time > 0,
+ "`start_time` should have a positive value"
+ );
+ assert_eq!(
+ resource_timing.redirect_start, 0,
+ "`redirect_start` should be zero"
+ );
+
+ // verify that setting `start_time` to `redirect_start` fails if `redirect_start`==0 and no TAO
+ resource_timing.set_attribute(ResourceAttribute::StartTime(
+ ResourceTimeValue::RedirectStart,
+ ));
+ assert_ne!(
+ resource_timing.start_time, resource_timing.redirect_start,
+ "`start_time` should *not* equal `redirect_start`"
+ );
+}
+
+#[test]
+fn test_set_start_time() {
+ let mut resource_timing: ResourceFetchTiming =
+ ResourceFetchTiming::new(ResourceTimingType::Resource);
+ assert_eq!(resource_timing.start_time, 0, "`start_time` should be zero");
+
+ // verify setting `start_time` to current time succeeds
+ resource_timing.set_attribute(ResourceAttribute::StartTime(ResourceTimeValue::Now));
+ assert!(resource_timing.start_time > 0, "failed to set `start_time`");
+}
+#[test]
+fn test_reset_start_time() {
+ let mut resource_timing: ResourceFetchTiming =
+ ResourceFetchTiming::new(ResourceTimingType::Resource);
+ assert_eq!(resource_timing.start_time, 0, "`start_time` should be zero");
+
+ resource_timing.start_time = 1;
+ assert!(
+ resource_timing.start_time > 0,
+ "`start_time` should have a positive value"
+ );
+
+ // verify resetting `start_time` (to zero) succeeds
+ resource_timing.set_attribute(ResourceAttribute::StartTime(ResourceTimeValue::Zero));
+ assert_eq!(
+ resource_timing.start_time, 0,
+ "failed to reset `start_time`"
+ );
+}
diff --git a/components/shared/net/tests/pub_domains.rs b/components/shared/net/tests/pub_domains.rs
new file mode 100644
index 00000000000..ea58e7650e3
--- /dev/null
+++ b/components/shared/net/tests/pub_domains.rs
@@ -0,0 +1,122 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
+
+use net_traits::pub_domains::{is_pub_domain, is_reg_domain, pub_suffix, reg_suffix};
+
+// These tests may need to be updated if the PSL changes.
+
+#[test]
+fn test_is_pub_domain_plain() {
+ assert!(is_pub_domain("com"));
+ assert!(is_pub_domain(".org"));
+ assert!(is_pub_domain("za.org"));
+ assert!(is_pub_domain("xn--od0alg.hk"));
+ assert!(is_pub_domain("xn--krdsherad-m8a.no"));
+}
+
+#[test]
+fn test_is_pub_domain_wildcard() {
+ assert!(is_pub_domain("hello.bd"));
+ assert!(is_pub_domain("world.jm"));
+ assert!(is_pub_domain("toto.kobe.jp"));
+}
+
+#[test]
+fn test_is_pub_domain_exception() {
+ assert_eq!(is_pub_domain("www.ck"), false);
+ assert_eq!(is_pub_domain("city.kawasaki.jp"), false);
+ assert_eq!(is_pub_domain("city.nagoya.jp"), false);
+ assert_eq!(is_pub_domain("teledata.mz"), false);
+}
+
+#[test]
+fn test_is_pub_domain_not() {
+ assert_eq!(is_pub_domain(""), false);
+ assert_eq!(is_pub_domain("."), false);
+ assert_eq!(is_pub_domain("..."), false);
+ assert_eq!(is_pub_domain(".servo.org"), false);
+ assert_eq!(is_pub_domain("www.mozilla.org"), false);
+ assert_eq!(is_pub_domain("publicsuffix.org"), false);
+ assert_eq!(is_pub_domain("hello.world.jm"), false);
+ assert_eq!(is_pub_domain("toto.toto.kobe.jp"), false);
+}
+
+#[test]
+fn test_is_pub_domain() {
+ assert!(!is_pub_domain("city.yokohama.jp"));
+ assert!(!is_pub_domain("foo.bar.baz.yokohama.jp"));
+ assert!(!is_pub_domain("foo.bar.city.yokohama.jp"));
+ assert!(!is_pub_domain("foo.bar.com"));
+ assert!(!is_pub_domain("foo.bar.tokyo.jp"));
+ assert!(!is_pub_domain("foo.bar.yokohama.jp"));
+ assert!(!is_pub_domain("foo.city.yokohama.jp"));
+ assert!(!is_pub_domain("foo.com"));
+ assert!(!is_pub_domain("foo.tokyo.jp"));
+ assert!(!is_pub_domain("yokohama.jp"));
+ assert!(is_pub_domain("com"));
+ assert!(is_pub_domain("foo.yokohama.jp"));
+ assert!(is_pub_domain("jp"));
+ assert!(is_pub_domain("tokyo.jp"));
+}
+
+#[test]
+fn test_is_reg_domain() {
+ assert!(!is_reg_domain("com"));
+ assert!(!is_reg_domain("foo.bar.baz.yokohama.jp"));
+ assert!(!is_reg_domain("foo.bar.com"));
+ assert!(!is_reg_domain("foo.bar.tokyo.jp"));
+ assert!(!is_reg_domain("foo.city.yokohama.jp"));
+ assert!(!is_reg_domain("foo.yokohama.jp"));
+ assert!(!is_reg_domain("jp"));
+ assert!(!is_reg_domain("tokyo.jp"));
+ assert!(is_reg_domain("city.yokohama.jp"));
+ assert!(is_reg_domain("foo.bar.yokohama.jp"));
+ assert!(is_reg_domain("foo.com"));
+ assert!(is_reg_domain("foo.tokyo.jp"));
+ assert!(is_reg_domain("yokohama.jp"));
+}
+
+#[test]
+fn test_pub_suffix() {
+ assert_eq!(pub_suffix("city.yokohama.jp"), "yokohama.jp");
+ assert_eq!(pub_suffix("com"), "com");
+ assert_eq!(pub_suffix("foo.bar.baz.yokohama.jp"), "baz.yokohama.jp");
+ assert_eq!(pub_suffix("foo.bar.com"), "com");
+ assert_eq!(pub_suffix("foo.bar.tokyo.jp"), "tokyo.jp");
+ assert_eq!(pub_suffix("foo.bar.yokohama.jp"), "bar.yokohama.jp");
+ assert_eq!(pub_suffix("foo.city.yokohama.jp"), "yokohama.jp");
+ assert_eq!(pub_suffix("foo.com"), "com");
+ assert_eq!(pub_suffix("foo.tokyo.jp"), "tokyo.jp");
+ assert_eq!(pub_suffix("foo.yokohama.jp"), "foo.yokohama.jp");
+ assert_eq!(pub_suffix("jp"), "jp");
+ assert_eq!(pub_suffix("tokyo.jp"), "tokyo.jp");
+ assert_eq!(pub_suffix("yokohama.jp"), "jp");
+}
+
+#[test]
+fn test_reg_suffix() {
+ assert_eq!(reg_suffix("city.yokohama.jp"), "city.yokohama.jp");
+ assert_eq!(reg_suffix("com"), "com");
+ assert_eq!(reg_suffix("foo.bar.baz.yokohama.jp"), "bar.baz.yokohama.jp");
+ assert_eq!(reg_suffix("foo.bar.com"), "bar.com");
+ assert_eq!(reg_suffix("foo.bar.tokyo.jp"), "bar.tokyo.jp");
+ assert_eq!(reg_suffix("foo.bar.yokohama.jp"), "foo.bar.yokohama.jp");
+ assert_eq!(reg_suffix("foo.city.yokohama.jp"), "city.yokohama.jp");
+ assert_eq!(reg_suffix("foo.com"), "foo.com");
+ assert_eq!(reg_suffix("foo.tokyo.jp"), "foo.tokyo.jp");
+ assert_eq!(reg_suffix("foo.yokohama.jp"), "foo.yokohama.jp");
+ assert_eq!(reg_suffix("jp"), "jp");
+ assert_eq!(reg_suffix("tokyo.jp"), "tokyo.jp");
+ assert_eq!(reg_suffix("yokohama.jp"), "yokohama.jp");
+}
+
+#[test]
+fn test_weirdness() {
+ // These are weird results, but AFAICT they are spec-compliant.
+ assert_ne!(
+ pub_suffix("city.yokohama.jp"),
+ pub_suffix(pub_suffix("city.yokohama.jp"))
+ );
+ assert!(!is_pub_domain(pub_suffix("city.yokohama.jp")));
+}
diff --git a/components/shared/net/tests/whitespace.rs b/components/shared/net/tests/whitespace.rs
new file mode 100644
index 00000000000..d1e6b7a2ac8
--- /dev/null
+++ b/components/shared/net/tests/whitespace.rs
@@ -0,0 +1,25 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
+
+#[test]
+fn test_trim_http_whitespace() {
+ fn test_trim(in_: &[u8], out: &[u8]) {
+ let b = net_traits::trim_http_whitespace(in_);
+ assert_eq!(b, out);
+ }
+
+ test_trim(b"", b"");
+
+ test_trim(b" ", b"");
+ test_trim(b"a", b"a");
+ test_trim(b" a", b"a");
+ test_trim(b"a ", b"a");
+ test_trim(b" a ", b"a");
+
+ test_trim(b"\t", b"");
+ test_trim(b"a", b"a");
+ test_trim(b"\ta", b"a");
+ test_trim(b"a\t", b"a");
+ test_trim(b"\ta\t", b"a");
+}