From fb885f9175219637084dec06b8409e1e8480da2d Mon Sep 17 00:00:00 2001 From: Aumetra Weisman Date: Tue, 17 Dec 2024 13:10:36 +0100 Subject: [PATCH] expand tests, add benches for basic auth --- Cargo.lock | 2 ++ lib/komainu/Cargo.toml | 2 ++ lib/komainu/benches/basic_auth.rs | 55 +++++++++++++++++++++++-------- lib/komainu/benches/pkce.rs | 3 ++ lib/komainu/src/extractor.rs | 2 +- lib/komainu/tests/basic_auth.rs | 45 ++++++++++--------------- 6 files changed, 67 insertions(+), 42 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 6eec3db7e..06651e1d3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3996,8 +3996,10 @@ dependencies = [ "base64-simd", "bytes", "divan", + "headers", "http", "memchr", + "rstest", "serde", "serde_test", "serde_urlencoded", diff --git a/lib/komainu/Cargo.toml b/lib/komainu/Cargo.toml index 0990e3824..8f856749c 100644 --- a/lib/komainu/Cargo.toml +++ b/lib/komainu/Cargo.toml @@ -31,6 +31,8 @@ url.workspace = true [dev-dependencies] divan.workspace = true +headers.workspace = true +rstest.workspace = true serde_test.workspace = true [lints] diff --git a/lib/komainu/benches/basic_auth.rs b/lib/komainu/benches/basic_auth.rs index 90971a19e..0d622566b 100644 --- a/lib/komainu/benches/basic_auth.rs +++ b/lib/komainu/benches/basic_auth.rs @@ -1,19 +1,46 @@ -use divan::{black_box, black_box_drop}; -use komainu::extractor::BasicAuth; +#[global_allocator] +static GLOBAL: divan::AllocProfiler = divan::AllocProfiler::system(); -#[divan::bench] -fn rfc_value(b: divan::Bencher<'_, '_>) { - let mut map = http::HeaderMap::new(); - map.insert( - http::header::AUTHORIZATION, - http::HeaderValue::from_static("Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ=="), - ); +#[divan::bench_group] +mod headers { + use divan::{black_box, black_box_drop}; + use headers::{authorization::Basic, Authorization, HeaderMapExt}; - b.bench(|| { - let auth = BasicAuth::extract(black_box(&map)).unwrap(); - black_box_drop((auth.username(), auth.password())); - auth - }); + #[divan::bench] + fn rfc_value(b: divan::Bencher<'_, '_>) { + let mut map = http::HeaderMap::new(); + map.insert( + http::header::AUTHORIZATION, + http::HeaderValue::from_static("Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ=="), + ); + + b.bench(|| { + let auth = black_box(&map).typed_get::>().unwrap(); + black_box_drop((auth.username(), auth.password())); + auth + }); + } +} + +#[divan::bench_group] +mod ours { + use divan::{black_box, black_box_drop}; + use komainu::extractor::BasicAuth; + + #[divan::bench] + fn rfc_value(b: divan::Bencher<'_, '_>) { + let mut map = http::HeaderMap::new(); + map.insert( + http::header::AUTHORIZATION, + http::HeaderValue::from_static("Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ=="), + ); + + b.bench(|| { + let auth = BasicAuth::extract(black_box(&map)).unwrap(); + black_box_drop((auth.username(), auth.password())); + auth + }); + } } fn main() { diff --git a/lib/komainu/benches/pkce.rs b/lib/komainu/benches/pkce.rs index 3a424bc56..06bba6a51 100644 --- a/lib/komainu/benches/pkce.rs +++ b/lib/komainu/benches/pkce.rs @@ -2,6 +2,9 @@ use divan::black_box; use komainu::flow::{PkceMethod, PkcePayload}; use std::borrow::Cow; +#[global_allocator] +static GLOBAL: divan::AllocProfiler = divan::AllocProfiler::system(); + #[divan::bench] fn s256() -> komainu::Result<()> { let verifier_base64 = "dBjftJeZ4CVP-mB92K27uhbUJU1p1r_wW1gFWFOEjXk"; diff --git a/lib/komainu/src/extractor.rs b/lib/komainu/src/extractor.rs index 54f3bf5b8..b6da1d188 100644 --- a/lib/komainu/src/extractor.rs +++ b/lib/komainu/src/extractor.rs @@ -109,7 +109,7 @@ impl BasicAuth { String::from_utf8_unchecked(buffer) }; - let delimiter_pos = buffer.find(':')?; + let delimiter_pos = memchr(b':', buffer.as_bytes())?; Some(Self { buffer, diff --git a/lib/komainu/tests/basic_auth.rs b/lib/komainu/tests/basic_auth.rs index fee3cf578..459d85cf6 100644 --- a/lib/komainu/tests/basic_auth.rs +++ b/lib/komainu/tests/basic_auth.rs @@ -1,4 +1,5 @@ use komainu::extractor::BasicAuth; +use rstest::rstest; #[test] fn parse_basic_auth_rfc() { @@ -13,9 +14,12 @@ fn parse_basic_auth_rfc() { assert_eq!(auth.password(), "open sesame"); } -#[test] -fn empty_creds() { - let creds = ":"; +#[rstest] +#[case("", "")] +#[case("OwO", "")] +#[case("", "UwU")] +fn emptiness(#[case] username: &str, #[case] password: &str) { + let creds = format!("{username}:{password}"); let encoded = base64_simd::STANDARD.encode_to_string(creds); let val = format!("Basic {encoded}"); @@ -26,30 +30,17 @@ fn empty_creds() { ); let auth = BasicAuth::extract(&map).unwrap(); - assert_eq!(auth.username(), ""); - assert_eq!(auth.password(), ""); + assert_eq!(auth.username(), username); + assert_eq!(auth.password(), password); } -#[test] -fn empty_username() { - let creds = ":UwU"; - let encoded = base64_simd::STANDARD.encode_to_string(creds); - let val = format!("Basic {encoded}"); - - let mut map = http::HeaderMap::new(); - map.insert( - http::header::AUTHORIZATION, - http::HeaderValue::from_str(val.as_str()).unwrap(), - ); - - let auth = BasicAuth::extract(&map).unwrap(); - assert_eq!(auth.username(), ""); - assert_eq!(auth.password(), "UwU"); -} - -#[test] -fn empty_password() { - let creds = "OwO:"; +#[rstest] +#[case("pwease?? πŸ₯Ί", "enter?? πŸ‘‰πŸ‘ˆ")] +#[case("hΓ€llΓΆ πŸ’€", "tschüß πŸ˜”")] +#[case("μ•ˆλ…•ν•˜μ„Έμš”", "μ•ˆλ…•")] +#[case("πŸ³οΈβ€βš§οΈ", "πŸ³β€πŸŒˆ")] +fn unicode_works(#[case] username: &str, #[case] password: &str) { + let creds = format!("{username}:{password}"); let encoded = base64_simd::STANDARD.encode_to_string(creds); let val = format!("Basic {encoded}"); @@ -60,6 +51,6 @@ fn empty_password() { ); let auth = BasicAuth::extract(&map).unwrap(); - assert_eq!(auth.username(), "OwO"); - assert_eq!(auth.password(), ""); + assert_eq!(auth.username(), username); + assert_eq!(auth.password(), password); }