From 7e8702a15027de16e641fc11c5832a264327db1d Mon Sep 17 00:00:00 2001 From: ryan Date: Tue, 12 Sep 2023 01:36:05 -0500 Subject: [PATCH] more misc tests --- Cargo.lock | 1 + keyfork-derive-util/src/tests.rs | 13 +++++++++ keyfork-seed/Cargo.toml | 1 + keyfork-seed/src/tests.rs | 48 ++++++++++++++++++++++++++++++-- 4 files changed, 61 insertions(+), 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 0adfe47..fd60b2d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -570,6 +570,7 @@ version = "0.1.0" dependencies = [ "bincode", "clap", + "ed25519-dalek", "hex-literal", "keyfork-derive-util", "keyfork-frame", diff --git a/keyfork-derive-util/src/tests.rs b/keyfork-derive-util/src/tests.rs index eb529fd..69ea610 100644 --- a/keyfork-derive-util/src/tests.rs +++ b/keyfork-derive-util/src/tests.rs @@ -111,3 +111,16 @@ fn ed25519() { assert_eq!(response.data, private_key); } } + +#[cfg(feature = "ed25519")] +#[test] +#[should_panic] +fn panics_at_depth() { + use ed25519_dalek::SigningKey; + + let seed = hex!("000102030405060708090a0b0c0d0e0f"); + let mut xkey = ExtendedPrivateKey::::new(seed).unwrap(); + for i in 0..u32::from(u8::MAX) + 1 { + xkey = xkey.derive_child(&DerivationIndex::new(i, true).unwrap()).unwrap(); + } +} diff --git a/keyfork-seed/Cargo.toml b/keyfork-seed/Cargo.toml index 8d0df7a..88e67a7 100644 --- a/keyfork-seed/Cargo.toml +++ b/keyfork-seed/Cargo.toml @@ -13,6 +13,7 @@ keyfork-frame = { version = "0.1.0", path = "../keyfork-frame", default-features thiserror = "1.0.48" [dev-dependencies] +ed25519-dalek = "2.0.0" hex-literal = "0.4.1" keyforkd = { path = "../keyforkd", default-features = false } tempdir = "0.3.7" diff --git a/keyfork-seed/src/tests.rs b/keyfork-seed/src/tests.rs index 90bdd17..42d98ad 100644 --- a/keyfork-seed/src/tests.rs +++ b/keyfork-seed/src/tests.rs @@ -1,10 +1,10 @@ use crate::client::Client; use hex_literal::hex; -use keyfork_derive_util::{request::*, DerivationPath}; +use keyfork_derive_util::{request::*, DerivationPath, DerivationIndex, ExtendedPrivateKey}; +use std::sync::mpsc::channel; use std::{os::unix::net::UnixStream, str::FromStr}; use tempdir::TempDir; use tokio::runtime::Builder; -use std::sync::mpsc::channel; #[test] fn it_works() { @@ -45,3 +45,47 @@ fn it_works() { ); handle.abort(); } + +#[test] +fn misc_multi_requests() { + // Setup + let entropy = &hex!("000102030405060708090a0b0c0d0e0f")[..]; + let mnemonic = keyforkd::Mnemonic::from_entropy(entropy, Default::default()).unwrap(); + let rt = Builder::new_multi_thread().enable_io().build().unwrap(); + let tempdir = TempDir::new("keyfork-seed").unwrap(); + let socket_path = tempdir.path().join("keyforkd.sock"); + let (tx, rx) = channel(); + + let handle = rt.spawn({ + let socket_path = socket_path.clone(); + async move { + let mut server = keyforkd::UnixServer::bind(&socket_path).unwrap(); + // Connections can be pending for a few seconds, so signal to the main + // test we're ready to start accepting. + tx.send(()).unwrap(); + let service = keyforkd::ServiceBuilder::new() + .layer(keyforkd::middleware::BincodeLayer::new()) + .service(keyforkd::Keyforkd::new(mnemonic)); + server.run(service).await.unwrap(); + } + }); + + rx.recv().unwrap(); + let socket = UnixStream::connect(&socket_path).unwrap(); + let mut client = Client::new(socket); + let req = DerivationRequest::new( + DerivationAlgorithm::Ed25519, + DerivationPath::from_str("m/7366512'/0'").unwrap(), + ); + let response = client.request(&req).unwrap(); + let key = ExtendedPrivateKey::::new_from_parts( + &response.data, + response.depth, + response.chain_code, + ).unwrap(); + + for i in 0..255 { + key.derive_child(&DerivationIndex::new(i, true).unwrap()).unwrap(); + } + handle.abort(); +}