Skip to content

Commit

Permalink
expand tests, add benches for basic auth
Browse files Browse the repository at this point in the history
  • Loading branch information
aumetra committed Dec 17, 2024
1 parent f8ea1a6 commit 188140a
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 42 deletions.
3 changes: 3 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions lib/komainu/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ url.workspace = true

[dev-dependencies]
divan.workspace = true
headers.workspace = true
mimalloc.workspace = true
rstest.workspace = true
serde_test.workspace = true

[lints]
Expand Down
56 changes: 42 additions & 14 deletions lib/komainu/benches/basic_auth.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,47 @@
use divan::{black_box, black_box_drop};
use komainu::extractor::BasicAuth;
#[global_allocator]
static GLOBAL: divan::AllocProfiler<mimalloc::MiMalloc> =
divan::AllocProfiler::new(mimalloc::MiMalloc);

#[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::<Authorization<Basic>>().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() {
Expand Down
4 changes: 4 additions & 0 deletions lib/komainu/benches/pkce.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ use divan::black_box;
use komainu::flow::{PkceMethod, PkcePayload};
use std::borrow::Cow;

#[global_allocator]
static GLOBAL: divan::AllocProfiler<mimalloc::MiMalloc> =
divan::AllocProfiler::new(mimalloc::MiMalloc);

#[divan::bench]
fn s256() -> komainu::Result<()> {
let verifier_base64 = "dBjftJeZ4CVP-mB92K27uhbUJU1p1r_wW1gFWFOEjXk";
Expand Down
2 changes: 1 addition & 1 deletion lib/komainu/src/extractor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
45 changes: 18 additions & 27 deletions lib/komainu/tests/basic_auth.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use komainu::extractor::BasicAuth;
use rstest::rstest;

#[test]
fn parse_basic_auth_rfc() {
Expand All @@ -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}");

Expand All @@ -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}");

Expand All @@ -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);
}

0 comments on commit 188140a

Please sign in to comment.