From b64a3e948a98a1e7a5783842ba7a4f3734cf66ab Mon Sep 17 00:00:00 2001 From: Felix Ableitner Date: Thu, 5 Dec 2024 22:36:38 +0100 Subject: [PATCH] Speed up tests by using a single static keypair --- src/backend/database/user.rs | 11 ++++------- src/backend/mod.rs | 4 ++-- src/backend/utils.rs | 18 +++++++++++++++++- 3 files changed, 23 insertions(+), 10 deletions(-) diff --git a/src/backend/database/user.rs b/src/backend/database/user.rs index b41cb00..6c2ab61 100644 --- a/src/backend/database/user.rs +++ b/src/backend/database/user.rs @@ -5,6 +5,7 @@ use crate::{ IbisData, }, error::MyResult, + utils::generate_keypair, }, common::{ newtypes::PersonId, @@ -15,11 +16,7 @@ use crate::{ LocalUserView, }, }; -use activitypub_federation::{ - config::Data, - fetch::object_id::ObjectId, - http_signatures::generate_actor_keypair, -}; +use activitypub_federation::{config::Data, fetch::object_id::ObjectId}; use bcrypt::{hash, DEFAULT_COST}; use chrono::{DateTime, Utc}; use diesel::{ @@ -83,7 +80,7 @@ impl DbPerson { http_protocol_str() ))?; let inbox_url = format!("{}://{domain}/inbox", http_protocol_str()); - let keypair = generate_actor_keypair()?; + let keypair = generate_keypair()?; let person_form = DbPersonForm { username, ap_id, @@ -185,7 +182,7 @@ impl DbPerson { http_protocol_str() ))?; let inbox_url = format!("{}://{domain}/inbox", http_protocol_str()); - let keypair = generate_actor_keypair()?; + let keypair = generate_keypair()?; let person_form = DbPersonForm { username: username.to_string(), ap_id, diff --git a/src/backend/mod.rs b/src/backend/mod.rs index 386f253..9ff71c2 100644 --- a/src/backend/mod.rs +++ b/src/backend/mod.rs @@ -21,7 +21,6 @@ use crate::{ use activitypub_federation::{ config::{Data, FederationConfig, FederationMiddleware}, fetch::object_id::ObjectId, - http_signatures::generate_actor_keypair, }; use api::api_routes; use assets::file_and_error_handler; @@ -54,6 +53,7 @@ use std::net::SocketAddr; use tokio::{net::TcpListener, sync::oneshot}; use tower_http::cors::CorsLayer; use tower_layer::Layer; +use utils::generate_keypair; pub mod api; mod assets; @@ -156,7 +156,7 @@ async fn setup(data: &Data) -> Result<(), Error> { let domain = &data.config.federation.domain; let ap_id = ObjectId::parse(&format!("{}://{domain}", http_protocol_str()))?; let inbox_url = format!("{}://{domain}/inbox", http_protocol_str()); - let keypair = generate_actor_keypair()?; + let keypair = generate_keypair()?; let form = DbInstanceForm { domain: domain.to_string(), ap_id, diff --git a/src/backend/utils.rs b/src/backend/utils.rs index ba5be3f..5cca78a 100644 --- a/src/backend/utils.rs +++ b/src/backend/utils.rs @@ -2,10 +2,14 @@ use crate::{ backend::{database::IbisData, error::MyResult}, common::{utils, DbEdit, EditVersion}, }; -use activitypub_federation::config::Data; +use activitypub_federation::{ + config::Data, + http_signatures::{generate_actor_keypair, Keypair}, +}; use anyhow::anyhow; use diffy::{apply, Patch}; use rand::{distributions::Alphanumeric, thread_rng, Rng}; +use std::sync::LazyLock; use url::{ParseError, Url}; pub fn generate_activity_id(data: &Data) -> Result { @@ -101,3 +105,15 @@ mod test { Ok(()) } } + +/// Use a single static keypair during testing which is signficantly faster than +/// generating dozens of keys from scratch. +pub fn generate_keypair() -> MyResult { + if cfg!(debug_assertions) { + static KEYPAIR: LazyLock = + LazyLock::new(|| generate_actor_keypair().expect("generate keypair")); + Ok(KEYPAIR.clone()) + } else { + Ok(generate_actor_keypair()?) + } +}