Skip to content

Commit

Permalink
refactor gix-url tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Byron committed Dec 7, 2024
1 parent 530257f commit 01f66eb
Show file tree
Hide file tree
Showing 12 changed files with 26 additions and 11 deletions.
32 changes: 21 additions & 11 deletions gix-url/src/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,19 +114,26 @@ pub(crate) fn url(input: &BStr, protocol_end: usize) -> Result<crate::Url, Error
Ok(crate::Url {
serialize_alternative_form: false,
scheme,
user: url_user(&url),
password: url.password().map(percent_decoded_utf8),
user: url_user(&url, UrlKind::Url)?,
password: url
.password()
.map(|s| percent_decoded_utf8(s, UrlKind::Url))
.transpose()?,
host: url.host_str().map(Into::into),
port: url.port(),
path: url.path().into(),
})
}

fn percent_decoded_utf8(s: &str) -> String {
percent_decode_str(s)
fn percent_decoded_utf8(s: &str, kind: UrlKind) -> Result<String, Error> {
Ok(percent_decode_str(s)
.decode_utf8()
.expect("it's not possible to sneak illegal UTF8 into a URL")
.into_owned()
.map_err(|err| Error::Utf8 {
url: s.into(),
kind,
source: err,
})?
.into_owned())
}

pub(crate) fn scp(input: &BStr, colon: usize) -> Result<crate::Url, Error> {
Expand Down Expand Up @@ -157,19 +164,22 @@ pub(crate) fn scp(input: &BStr, colon: usize) -> Result<crate::Url, Error> {
Ok(crate::Url {
serialize_alternative_form: true,
scheme: url.scheme().into(),
user: url_user(&url),
password: url.password().map(percent_decoded_utf8),
user: url_user(&url, UrlKind::Scp)?,
password: url
.password()
.map(|s| percent_decoded_utf8(s, UrlKind::Scp))
.transpose()?,
host: url.host_str().map(Into::into),
port: url.port(),
path: path.into(),
})
}

fn url_user(url: &url::Url) -> Option<String> {
fn url_user(url: &url::Url, kind: UrlKind) -> Result<Option<String>, Error> {
if url.username().is_empty() && url.password().is_none() {
None
Ok(None)
} else {
Some(percent_decoded_utf8(url.username()))
Ok(Some(percent_decoded_utf8(url.username(), kind)?))
}
}

Expand Down
1 change: 1 addition & 0 deletions gix-url/tests/fixtures/fuzzed/illegal-utf8.url
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
BBi%BBBBB,@}:m\
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ fn assert_urls_equal(expected: &baseline::GitDiagUrl<'_>, actual: &gix_url::Url)
assert_eq!(actual.path, expected.path.unwrap_or_default());
}

#[allow(clippy::module_inception)]
mod baseline {
use bstr::{BStr, BString, ByteSlice};
use gix_testtools::once_cell::sync::Lazy;
Expand Down
File renamed without changes.
1 change: 1 addition & 0 deletions gix-url/tests/fuzzed.rs → gix-url/tests/url/fuzzed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use bstr::ByteSlice;
#[test]
fn fuzzed() {
for name in [
"illegal-utf8",
"short-panic",
"very-long-abort2",
"very-long-abort",
Expand Down
2 changes: 2 additions & 0 deletions gix-url/tests/url.rs → gix-url/tests/url/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,7 @@ pub type Error = Box<dyn std::error::Error>;
pub type Result = std::result::Result<(), Error>;

mod access;
mod baseline;
mod expand_path;
mod fuzzed;
mod parse;
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 comments on commit 01f66eb

Please sign in to comment.