From 726ccfc7910b354c7d033938e680f5509741450e Mon Sep 17 00:00:00 2001 From: bridiver <34129+bridiver@users.noreply.github.com> Date: Fri, 28 Feb 2025 08:38:52 -0700 Subject: [PATCH] generate unit tests --- test/BUILD.gn | 1 + third_party/rust/url/v2/BUILD.gn | 30 +++-- third_party/rust/url/v2/crate/parse.cc | 9 +- third_party/rust/url/v2/crate/parse.h | 7 +- third_party/rust/url/v2/crate/src/lib.rs | 150 ++++++++++++++++++++++- 5 files changed, 184 insertions(+), 13 deletions(-) diff --git a/test/BUILD.gn b/test/BUILD.gn index 7c4afaf9503b..a3226e3050a1 100644 --- a/test/BUILD.gn +++ b/test/BUILD.gn @@ -277,6 +277,7 @@ test("brave_unit_tests") { "//brave/mojo/brave_ast_patcher:unit_tests", "//brave/net:unit_tests", "//brave/third_party/blink/renderer:renderer", + "//brave/third_party/rust/url/v2:lib_internal_url_v2_unittests", "//brave/vendor/brave_base", "//chrome:dependencies", "//chrome:resources", diff --git a/third_party/rust/url/v2/BUILD.gn b/third_party/rust/url/v2/BUILD.gn index 1e804c58df64..b2abd0f873c2 100644 --- a/third_party/rust/url/v2/BUILD.gn +++ b/third_party/rust/url/v2/BUILD.gn @@ -11,33 +11,43 @@ import("//build/rust/cargo_crate.gni") group("lib") { public_deps = [ ":lib_internal", - ":parser", + ":parse", ] } -static_library("parser") { +static_library("parse") { + visibility = [ ":*" ] sources = [ "crate/parse.cc", "crate/parse.h", ] deps = [ - ":lib_internal", + ":cxx_generated", + "//base:i18n", + "//build/rust:cxx_cppdeps", "//url", ] } +# cargo_crate target has to depend on `parse` for unit tests so we can't +# use the auto-generated target from `cargo_crate` or there would be a circular +# dependency +rust_cxx("cxx_generated") { + visibility = [ ":*" ] + sources = [ "crate/src/lib.rs" ] + export_symbols = is_component_build +} + cargo_crate("lib_internal") { + visibility = [ ":*" ] crate_name = "url" epoch = "2" crate_type = "rlib" crate_root = "crate/src/lib.rs" sources = [ "crate/src/lib.rs" ] inputs = [] - - cxx_bindings = [ "crate/src/lib.rs" ] - - build_native_rust_unit_tests = false + build_native_rust_unit_tests = true edition = "2018" cargo_pkg_version = "2.3.1" cargo_pkg_authors = "The rust-url developers" @@ -50,7 +60,11 @@ cargo_crate("lib_internal") { executable_configs += [ "//build/config/compiler:no_chromium_code" ] proc_macro_configs -= [ "//build/config/compiler:chromium_code" ] proc_macro_configs += [ "//build/config/compiler:no_chromium_code" ] - deps = [] + deps = [ + ":cxx_generated", + ":parse", + "//build/rust:cxx_rustdeps", + ] rustflags = [ "--cap-lints=allow", # Suppress all warnings in crates.io crates ] diff --git a/third_party/rust/url/v2/crate/parse.cc b/third_party/rust/url/v2/crate/parse.cc index 1af4454563d5..a36d60bc9537 100644 --- a/third_party/rust/url/v2/crate/parse.cc +++ b/third_party/rust/url/v2/crate/parse.cc @@ -9,8 +9,13 @@ #include "brave/third_party/rust/url/v2/crate/src/lib.rs.h" #include "url/gurl.h" +#include "base/i18n/icu_util.h" -namespace cxx_url { +namespace parse { + +void InitializeICUForTesting() { + CHECK(base::i18n::InitializeICU()); +} ParseResult ParseURL(rust::Str url_string) { std::string str(url_string); @@ -42,4 +47,4 @@ ParseResult Resolve(const ParseResult& base, rust::Str path) { return ParseURL(GURL(base_url).Resolve(relative_path).possibly_invalid_spec()); } -} // namespace url +} // namespace parse diff --git a/third_party/rust/url/v2/crate/parse.h b/third_party/rust/url/v2/crate/parse.h index 36ea8850142b..d72ef4c5778e 100644 --- a/third_party/rust/url/v2/crate/parse.h +++ b/third_party/rust/url/v2/crate/parse.h @@ -8,10 +8,13 @@ #include "third_party/rust/cxx/v1/cxx.h" -namespace cxx_url { +namespace parse { + struct ParseResult; +void InitializeICUForTesting(); ParseResult ParseURL(rust::Str host); ParseResult Resolve(const ParseResult& base, rust::Str relative); -} // namespace url + +} // namespace parse #endif // THIRD_PARTY_RUST_URL_V2_CRATE_PARSE_H_ diff --git a/third_party/rust/url/v2/crate/src/lib.rs b/third_party/rust/url/v2/crate/src/lib.rs index 8d1d0e722789..054e0035f509 100644 --- a/third_party/rust/url/v2/crate/src/lib.rs +++ b/third_party/rust/url/v2/crate/src/lib.rs @@ -62,7 +62,7 @@ simple_enum_error! { Overflow => "URLs more than 4 GB are not supported", } -#[cxx::bridge(namespace = "cxx_url")] +#[cxx::bridge(namespace = "parse")] mod ffi { #[derive(Clone)] struct ParseResult { @@ -84,6 +84,7 @@ mod ffi { unsafe extern "C++" { include!("brave/third_party/rust/url/v2/crate/parse.h"); + fn InitializeICUForTesting(); fn ParseURL(url: &str) -> ParseResult; fn Resolve(base: &ParseResult, relative: &str) -> ParseResult; } @@ -264,3 +265,150 @@ impl AsRef for Url { &self.parsed_result.serialization } } + +#[test] +fn test_relative() { + let base: Url = "sc://%C3%B1".parse().unwrap(); + let url = base.join("/resources/testharness.js").unwrap(); + assert_eq!(url.as_str(), "sc://%C3%B1/resources/testharness.js"); +} + +#[test] +fn test_relative_empty() { + let base: Url = "sc://%C3%B1".parse().unwrap(); + let url = base.join("").unwrap(); + assert_eq!(url.as_str(), "sc://%C3%B1"); +} + +#[test] +fn test_idna() { + ffi::InitializeICUForTesting(); + assert!("http://goșu.ro".parse::().is_ok()); + assert_eq!( + Url::parse("http://☃.net/").unwrap().host_str(), + Some("xn--n3h.net") + ); + assert!("https://r2---sn-huoa-cvhl.googlevideo.com/crossdomain.xml" + .parse::() + .is_ok()); +} + +#[test] +fn test_make_relative() { + let tests = [ + ( + "http://127.0.0.1:8080/test", + "http://127.0.0.1:8080/test", + "", + ), + ( + "http://127.0.0.1:8080/test", + "http://127.0.0.1:8080/test/", + "test/", + ), + ( + "http://127.0.0.1:8080/test/", + "http://127.0.0.1:8080/test", + "../test", + ), + ( + "http://127.0.0.1:8080/", + "http://127.0.0.1:8080/?foo=bar#123", + "?foo=bar#123", + ), + ( + "http://127.0.0.1:8080/", + "http://127.0.0.1:8080/test/video", + "test/video", + ), + ( + "http://127.0.0.1:8080/test", + "http://127.0.0.1:8080/test/video", + "test/video", + ), + ( + "http://127.0.0.1:8080/test/", + "http://127.0.0.1:8080/test/video", + "video", + ), + ( + "http://127.0.0.1:8080/test", + "http://127.0.0.1:8080/test2/video", + "test2/video", + ), + ( + "http://127.0.0.1:8080/test/", + "http://127.0.0.1:8080/test2/video", + "../test2/video", + ), + ( + "http://127.0.0.1:8080/test/bla", + "http://127.0.0.1:8080/test2/video", + "../test2/video", + ), + ( + "http://127.0.0.1:8080/test/bla/", + "http://127.0.0.1:8080/test2/video", + "../../test2/video", + ), + ( + "http://127.0.0.1:8080/test/?foo=bar#123", + "http://127.0.0.1:8080/test/video", + "video", + ), + ( + "http://127.0.0.1:8080/test/", + "http://127.0.0.1:8080/test/video?baz=meh#456", + "video?baz=meh#456", + ), + ( + "http://127.0.0.1:8080/test", + "http://127.0.0.1:8080/test?baz=meh#456", + "?baz=meh#456", + ), + ( + "http://127.0.0.1:8080/test/", + "http://127.0.0.1:8080/test?baz=meh#456", + "../test?baz=meh#456", + ), + ( + "http://127.0.0.1:8080/test/", + "http://127.0.0.1:8080/test/?baz=meh#456", + "?baz=meh#456", + ), + ( + "http://127.0.0.1:8080/test/?foo=bar#123", + "http://127.0.0.1:8080/test/video?baz=meh#456", + "video?baz=meh#456", + ), + ( + "http://127.0.0.1:8080/file.txt", + "http://127.0.0.1:8080/test/file.txt", + "test/file.txt", + ), + ( + "http://127.0.0.1:8080/not_equal.txt", + "http://127.0.0.1:8080/test/file.txt", + "test/file.txt", + ), + ]; + + for (base, uri, relative) in &tests { + let base_uri = url::Url::parse(base).unwrap(); + let relative_uri = url::Url::parse(uri).unwrap(); + // let make_relative = base_uri.make_relative(&relative_uri).unwrap(); + // assert_eq!( + // make_relative, *relative, + // "base: {}, uri: {}, relative: {}", + // base, uri, relative + // ); + assert_eq!( + base_uri.join(relative).unwrap().as_str(), + *uri, + "base: {}, uri: {}, relative: {}", + base, + uri, + relative + ); + } +}