From a2b0698e9f6b45780167380b9f71057195a04a79 Mon Sep 17 00:00:00 2001 From: Adrian Brink Date: Tue, 12 Feb 2019 10:03:18 +0100 Subject: [PATCH] Make access to the validator app sequential Previously the tests passed individually, but not when all run together. This was due to every test connecting to the Ledger application. This commit makes the Ledger application a global variable that is protected by a mutex so that it can be shared between tests. --- Cargo.toml | 1 + src/lib.rs | 56 +++++++++++++++++++++++++++++++----------------------- 2 files changed, 33 insertions(+), 24 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 628ec78..e603fb7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -19,6 +19,7 @@ ledger = "0.0.9" quick-error = "1.2.2" byteorder = "1.2.4" matches = "0.1.8" +lazy_static = "1.2.0" [dev-dependencies] ed25519-dalek = "0.8.1" diff --git a/src/lib.rs b/src/lib.rs index afcc330..0c42112 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -218,8 +218,19 @@ extern crate sha2; #[cfg(test)] extern crate ed25519_dalek; +#[cfg(test)] +#[macro_use] +extern crate lazy_static; + #[cfg(test)] mod tests { + use CosmosValidatorApp; + use std::sync::Mutex; + + lazy_static! { + static ref APP: Mutex = Mutex::new(CosmosValidatorApp::connect().unwrap()); + } + #[test] fn derivation_path() { use to_bip32array; @@ -264,27 +275,30 @@ mod tests { #[test] fn version() { - use CosmosValidatorApp; - - let app = CosmosValidatorApp::connect().unwrap(); + let app = APP.lock().unwrap(); - let version = app.version().unwrap(); + let resp = app.version(); - println!("mode {}", version.mode); - println!("major {}", version.major); - println!("minor {}", version.minor); - println!("patch {}", version.patch); - - assert_eq!(version.mode, 0xFF); - assert_eq!(version.major, 0x00); - assert!(version.minor >= 0x04); + match resp { + Ok(version) => { + println!("mode {}", version.mode); + println!("major {}", version.major); + println!("minor {}", version.minor); + println!("patch {}", version.patch); + + assert_eq!(version.mode, 0xFF); + assert_eq!(version.major, 0x00); + assert!(version.minor >= 0x04); + } + Err(err) => { + eprintln!("Error: {:?}", err); + } + } } #[test] fn public_key() { - use CosmosValidatorApp; - - let app = CosmosValidatorApp::connect().unwrap(); + let app = APP.lock().unwrap(); let resp = app.public_key(); match resp { @@ -300,10 +314,9 @@ mod tests { #[test] fn sign_empty() { - use CosmosValidatorApp; use Error; - let app = CosmosValidatorApp::connect().unwrap(); + let app = APP.lock().unwrap(); let some_message0 = b""; @@ -314,9 +327,7 @@ mod tests { #[test] fn sign_verify() { - use CosmosValidatorApp; - - let app = CosmosValidatorApp::connect().unwrap(); + let app = APP.lock().unwrap(); let some_message1 = [ 0x8, // (field_number << 3) | wire_type @@ -348,7 +359,6 @@ mod tests { match app.sign(&some_message2) { Ok(sig) => { use sha2::Sha512; - use sha2::Digest; use ed25519_dalek::PublicKey; use ed25519_dalek::Signature; @@ -368,9 +378,7 @@ mod tests { #[test] fn sign_many() { - use CosmosValidatorApp; - - let app = CosmosValidatorApp::connect().unwrap(); + let app = APP.lock().unwrap(); // First, get public key let resp = app.public_key();