aboutsummaryrefslogtreecommitdiffstats
path: root/components/net_traits
diff options
context:
space:
mode:
authorAnthony Ramine <n.oxyde@gmail.com>2018-01-19 13:18:56 +0100
committerAnthony Ramine <n.oxyde@gmail.com>2018-01-20 15:36:24 +0100
commitea924e398faafc29fafa83312e87410a3575f723 (patch)
tree1e619d3e640c9b56cf4753fe5683e27bd97811a0 /components/net_traits
parent697b9e2b87147a0f3767226c52097549d65b96da (diff)
downloadservo-ea924e398faafc29fafa83312e87410a3575f723.tar.gz
servo-ea924e398faafc29fafa83312e87410a3575f723.zip
Merge net_traits and net_traits_tests
Diffstat (limited to 'components/net_traits')
-rw-r--r--components/net_traits/Cargo.toml2
-rw-r--r--components/net_traits/tests/image.rs26
-rw-r--r--components/net_traits/tests/pub_domains.rs121
-rw-r--r--components/net_traits/tests/whitespace.rs27
4 files changed, 176 insertions, 0 deletions
diff --git a/components/net_traits/Cargo.toml b/components/net_traits/Cargo.toml
index 82ca26c6b9c..eb1774c6106 100644
--- a/components/net_traits/Cargo.toml
+++ b/components/net_traits/Cargo.toml
@@ -8,6 +8,8 @@ publish = false
[lib]
name = "net_traits"
path = "lib.rs"
+test = false
+doctest = false
[dependencies]
cookie = "0.10"
diff --git a/components/net_traits/tests/image.rs b/components/net_traits/tests/image.rs
new file mode 100644
index 00000000000..238fae97050
--- /dev/null
+++ b/components/net_traits/tests/image.rs
@@ -0,0 +1,26 @@
+/* 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 http://mozilla.org/MPL/2.0/. */
+
+extern crate net_traits;
+
+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 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(&bmp).is_ok());
+ assert!(detect_image_format(&ico).is_ok());
+ assert!(detect_image_format(&junk_format).is_err());
+}
diff --git a/components/net_traits/tests/pub_domains.rs b/components/net_traits/tests/pub_domains.rs
new file mode 100644
index 00000000000..36e70cd39a8
--- /dev/null
+++ b/components/net_traits/tests/pub_domains.rs
@@ -0,0 +1,121 @@
+/* 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 http://mozilla.org/MPL/2.0/. */
+
+extern crate net_traits;
+
+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!(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/net_traits/tests/whitespace.rs b/components/net_traits/tests/whitespace.rs
new file mode 100644
index 00000000000..4954e0f67f4
--- /dev/null
+++ b/components/net_traits/tests/whitespace.rs
@@ -0,0 +1,27 @@
+/* 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 http://mozilla.org/MPL/2.0/. */
+
+extern crate net_traits;
+
+#[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");
+}