Skip to content

Commit

Permalink
generate unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bridiver committed Feb 28, 2025
1 parent b96a1d3 commit 726ccfc
Show file tree
Hide file tree
Showing 5 changed files with 184 additions and 13 deletions.
1 change: 1 addition & 0 deletions test/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
30 changes: 22 additions & 8 deletions third_party/rust/url/v2/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
]
Expand Down
9 changes: 7 additions & 2 deletions third_party/rust/url/v2/crate/parse.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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
7 changes: 5 additions & 2 deletions third_party/rust/url/v2/crate/parse.h
Original file line number Diff line number Diff line change
Expand Up @@ -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_
150 changes: 149 additions & 1 deletion third_party/rust/url/v2/crate/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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;
}
Expand Down Expand Up @@ -264,3 +265,150 @@ impl AsRef<str> 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::<Url>().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::<Url>()
.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
);
}
}

0 comments on commit 726ccfc

Please sign in to comment.