more misc tests

This commit is contained in:
Ryan Heywood 2023-09-12 01:36:05 -05:00
parent 90d2f46cda
commit 7e8702a150
Signed by: ryan
GPG Key ID: 8E401478A3FBEF72
4 changed files with 61 additions and 2 deletions

1
Cargo.lock generated
View File

@ -570,6 +570,7 @@ version = "0.1.0"
dependencies = [
"bincode",
"clap",
"ed25519-dalek",
"hex-literal",
"keyfork-derive-util",
"keyfork-frame",

View File

@ -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::<SigningKey>::new(seed).unwrap();
for i in 0..u32::from(u8::MAX) + 1 {
xkey = xkey.derive_child(&DerivationIndex::new(i, true).unwrap()).unwrap();
}
}

View File

@ -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"

View File

@ -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::<ed25519_dalek::SigningKey>::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();
}