From 779733f6c9514d6aee766d4d3bee7a94704c8599 Mon Sep 17 00:00:00 2001 From: Johanna Larsson Date: Fri, 25 Sep 2026 12:50:31 +0100 Subject: [PATCH] test: lower bcrypt cost in tests The default is 12 but we can go down to 4 to waste less time on this. --- crates/tranquil-api/Cargo.toml | 1 + crates/tranquil-api/src/common.rs | 12 +++++++++--- justfile | 6 +++--- 3 files changed, 13 insertions(+), 6 deletions(-) diff --git a/crates/tranquil-api/Cargo.toml b/crates/tranquil-api/Cargo.toml index 9f7c180..22f8bd9 100644 --- a/crates/tranquil-api/Cargo.toml +++ b/crates/tranquil-api/Cargo.toml @@ -51,3 +51,4 @@ webauthn-rs = { workspace = true } [features] bsky = ["bsky-support"] bsky-support = [] +low-bcrypt-cost = [] diff --git a/crates/tranquil-api/src/common.rs b/crates/tranquil-api/src/common.rs index 66a943f..9b8ac98 100644 --- a/crates/tranquil-api/src/common.rs +++ b/crates/tranquil-api/src/common.rs @@ -1,4 +1,4 @@ -use bcrypt::{DEFAULT_COST, hash}; +use bcrypt::hash; use chrono::{DateTime, Utc}; use std::collections::HashMap; use tracing::{error, warn}; @@ -8,6 +8,12 @@ use tranquil_pds::api::error::DbResultExt; use tranquil_pds::types::{AtIdentifier, Did, Handle, PasswordHash}; use tranquil_types::{DiscordUsername, SignalUsername, TelegramUsername}; +#[cfg(not(feature = "low-bcrypt-cost"))] +const PASSWORD_HASH_COST: u32 = bcrypt::DEFAULT_COST; +// Use a lower bcrypt cost in tests. +#[cfg(feature = "low-bcrypt-cost")] +const PASSWORD_HASH_COST: u32 = 4; + pub struct ResolvedRepo { pub user_id: uuid::Uuid, pub did: Did, @@ -278,7 +284,7 @@ pub async fn verify_credential( } pub fn hash_or_internal_error(value: &str) -> Result { - bcrypt::hash(value, DEFAULT_COST) + bcrypt::hash(value, PASSWORD_HASH_COST) .map(PasswordHash::new) .map_err(|e| { error!("Bcrypt hash error: {:?}", e); @@ -288,7 +294,7 @@ pub fn hash_or_internal_error(value: &str) -> Result { pub async fn hash_password_async(password: &str) -> Result { let password = password.to_string(); - tokio::task::spawn_blocking(move || hash(password, DEFAULT_COST)) + tokio::task::spawn_blocking(move || hash(password, PASSWORD_HASH_COST)) .await .map_err(|e| { error!("Failed to spawn blocking task: {:?}", e); diff --git a/justfile b/justfile index 3e31576..16b8b02 100644 --- a/justfile +++ b/justfile @@ -98,7 +98,7 @@ test-store-asan: test-unit: SQLX_OFFLINE=true cargo test --test it -- dpop_unit:: validation_edge_cases:: scope_edge_cases:: -store_run := "SQLX_OFFLINE=true TRANQUIL_TEST_BACKEND=store TRANQUIL_PDS_ALLOW_INSECURE_SECRETS=1 DISABLE_RATE_LIMITING=1 TRANQUIL_LEXICON_OFFLINE=1 SKIP_IMPORT_VERIFICATION=true cargo nextest run --workspace --exclude tranquil-store --features tranquil-store/skip-fsync" +store_run := "SQLX_OFFLINE=true TRANQUIL_TEST_BACKEND=store TRANQUIL_PDS_ALLOW_INSECURE_SECRETS=1 DISABLE_RATE_LIMITING=1 TRANQUIL_LEXICON_OFFLINE=1 SKIP_IMPORT_VERIFICATION=true cargo nextest run --workspace --exclude tranquil-store --features tranquil-store/skip-fsync,tranquil-api/low-bcrypt-cost" store_test := store_run + " -E 'not (binary(it) and test(/^store_parity::/)) and not (package(tranquil-signal) and test(/^tests::/))'" test-auth: @@ -136,10 +136,10 @@ test-one name: test-full *args: @just services-up - eval "$(tranquil-dev-services env)" && SQLX_OFFLINE=true cargo nextest run --workspace --exclude tranquil-store --features tranquil-pds/s3 {{args}} + eval "$(tranquil-dev-services env)" && SQLX_OFFLINE=true cargo nextest run --workspace --exclude tranquil-store --features tranquil-pds/s3,tranquil-api/low-bcrypt-cost {{args}} test-pg *args: - SQLX_OFFLINE=true ./scripts/run-tests.sh {{args}} + SQLX_OFFLINE=true ./scripts/run-tests.sh --features tranquil-api/low-bcrypt-cost {{args}} services-up: tranquil-dev-services up