From d7fdbd40753550b31b30514854dd7d2c0ff681cd Mon Sep 17 00:00:00 2001 From: Elizabeth Engelman <4752801+elizabethengelman@users.noreply.github.com> Date: Tue, 4 Jun 2024 15:09:49 -0400 Subject: [PATCH] Fix/ledger tests org update (#1343) --- cmd/crates/stellar-ledger/src/hd_path.rs | 2 +- cmd/crates/stellar-ledger/src/lib.rs | 19 ++++++++------- .../{src => tests/test}/emulator_tests.rs | 22 ++++++++++++++---- cmd/crates/stellar-ledger/tests/test/main.rs | 2 ++ .../test_fixtures}/apps/stellarNanosApp.elf | Bin .../utils}/emulator_http_transport.rs | 0 cmd/crates/stellar-ledger/tests/utils/mod.rs | 2 ++ .../{src => tests/utils}/speculos.rs | 9 +++++-- 8 files changed, 39 insertions(+), 17 deletions(-) rename cmd/crates/stellar-ledger/{src => tests/test}/emulator_tests.rs (95%) create mode 100644 cmd/crates/stellar-ledger/tests/test/main.rs rename cmd/crates/stellar-ledger/{ => tests/test_fixtures}/apps/stellarNanosApp.elf (100%) rename cmd/crates/stellar-ledger/{src => tests/utils}/emulator_http_transport.rs (100%) create mode 100644 cmd/crates/stellar-ledger/tests/utils/mod.rs rename cmd/crates/stellar-ledger/{src => tests/utils}/speculos.rs (89%) diff --git a/cmd/crates/stellar-ledger/src/hd_path.rs b/cmd/crates/stellar-ledger/src/hd_path.rs index fd2a227f5..07ed133f1 100644 --- a/cmd/crates/stellar-ledger/src/hd_path.rs +++ b/cmd/crates/stellar-ledger/src/hd_path.rs @@ -1,7 +1,7 @@ use crate::Error; #[derive(Clone, Copy)] -pub struct HdPath(pub(crate) u32); +pub struct HdPath(pub u32); impl HdPath { pub fn depth(&self) -> u8 { diff --git a/cmd/crates/stellar-ledger/src/lib.rs b/cmd/crates/stellar-ledger/src/lib.rs index 9dd5bdee5..c74365af2 100644 --- a/cmd/crates/stellar-ledger/src/lib.rs +++ b/cmd/crates/stellar-ledger/src/lib.rs @@ -14,14 +14,8 @@ use stellar_xdr::curr::{ }; pub use crate::signer::Blob; - -mod emulator_http_transport; -mod signer; -mod speculos; - -#[cfg(all(test, feature = "emulator-tests"))] -mod emulator_tests; pub mod hd_path; +mod signer; // this is from https://github.com/LedgerHQ/ledger-live/blob/36cfbf3fa3300fd99bcee2ab72e1fd8f280e6280/libs/ledgerjs/packages/hw-app-str/src/Str.ts#L181 const APDU_MAX_SIZE: u8 = 150; @@ -300,16 +294,23 @@ fn get_transport() -> Result { pub const TEST_NETWORK_PASSPHRASE: &[u8] = b"Test SDF Network ; September 2015"; #[cfg(test)] -fn test_network_hash() -> Hash { +pub fn test_network_hash() -> Hash { use sha2::Digest; Hash(sha2::Sha256::digest(TEST_NETWORK_PASSPHRASE).into()) } + #[cfg(test)] mod test { + mod test_helpers { + pub mod test { + include!("../tests/utils/mod.rs"); + } + } use httpmock::prelude::*; use serde_json::json; - use crate::{emulator_http_transport::EmulatorHttpTransport, Blob}; + use crate::Blob; + use test_helpers::test::emulator_http_transport::EmulatorHttpTransport; use soroban_env_host::xdr::Transaction; use std::vec; diff --git a/cmd/crates/stellar-ledger/src/emulator_tests.rs b/cmd/crates/stellar-ledger/tests/test/emulator_tests.rs similarity index 95% rename from cmd/crates/stellar-ledger/src/emulator_tests.rs rename to cmd/crates/stellar-ledger/tests/test/emulator_tests.rs index c6c90dfb1..eca77fc2b 100644 --- a/cmd/crates/stellar-ledger/src/emulator_tests.rs +++ b/cmd/crates/stellar-ledger/tests/test/emulator_tests.rs @@ -1,13 +1,11 @@ use ledger_transport::Exchange; use serde::Deserialize; -use soroban_env_host::xdr::Transaction; use soroban_env_host::xdr::{self, Operation, OperationBody, Uint256}; +use soroban_env_host::xdr::{Hash, Transaction}; use std::vec; -use crate::emulator_http_transport::EmulatorHttpTransport; -use crate::hd_path::HdPath; -use crate::speculos::Speculos; -use crate::{test_network_hash, Blob, Error, LedgerSigner}; +use stellar_ledger::hd_path::HdPath; +use stellar_ledger::{Blob, Error, LedgerSigner}; use std::sync::Arc; use std::{collections::HashMap, time::Duration}; @@ -19,10 +17,24 @@ use stellar_xdr::curr::{ use testcontainers::clients; use tokio::time::sleep; +pub const TEST_NETWORK_PASSPHRASE: &[u8] = b"Test SDF Network ; September 2015"; +pub fn test_network_hash() -> Hash { + use sha2::Digest; + Hash(sha2::Sha256::digest(TEST_NETWORK_PASSPHRASE).into()) +} + fn ledger(host_port: u16) -> LedgerSigner { LedgerSigner::new(get_http_transport("127.0.0.1", host_port).unwrap()) } +mod test_helpers { + pub mod test { + include!("../utils/mod.rs"); + } +} + +use test_helpers::test::{emulator_http_transport::EmulatorHttpTransport, speculos::Speculos}; + #[tokio::test] async fn test_get_public_key() { let docker = clients::Cli::default(); diff --git a/cmd/crates/stellar-ledger/tests/test/main.rs b/cmd/crates/stellar-ledger/tests/test/main.rs new file mode 100644 index 000000000..c370240b5 --- /dev/null +++ b/cmd/crates/stellar-ledger/tests/test/main.rs @@ -0,0 +1,2 @@ +#[cfg(all(test, feature = "emulator-tests"))] +mod emulator_tests; diff --git a/cmd/crates/stellar-ledger/apps/stellarNanosApp.elf b/cmd/crates/stellar-ledger/tests/test_fixtures/apps/stellarNanosApp.elf similarity index 100% rename from cmd/crates/stellar-ledger/apps/stellarNanosApp.elf rename to cmd/crates/stellar-ledger/tests/test_fixtures/apps/stellarNanosApp.elf diff --git a/cmd/crates/stellar-ledger/src/emulator_http_transport.rs b/cmd/crates/stellar-ledger/tests/utils/emulator_http_transport.rs similarity index 100% rename from cmd/crates/stellar-ledger/src/emulator_http_transport.rs rename to cmd/crates/stellar-ledger/tests/utils/emulator_http_transport.rs diff --git a/cmd/crates/stellar-ledger/tests/utils/mod.rs b/cmd/crates/stellar-ledger/tests/utils/mod.rs new file mode 100644 index 000000000..5b63732bc --- /dev/null +++ b/cmd/crates/stellar-ledger/tests/utils/mod.rs @@ -0,0 +1,2 @@ +pub(crate) mod emulator_http_transport; +pub(crate) mod speculos; diff --git a/cmd/crates/stellar-ledger/src/speculos.rs b/cmd/crates/stellar-ledger/tests/utils/speculos.rs similarity index 89% rename from cmd/crates/stellar-ledger/src/speculos.rs rename to cmd/crates/stellar-ledger/tests/utils/speculos.rs index 13d154211..52dc50685 100644 --- a/cmd/crates/stellar-ledger/src/speculos.rs +++ b/cmd/crates/stellar-ledger/tests/utils/speculos.rs @@ -3,6 +3,8 @@ use testcontainers::{core::WaitFor, Image, ImageArgs}; const NAME: &str = "docker.io/zondax/builder-zemu"; const TAG: &str = "speculos-3a3439f6b45eca7f56395673caaf434c202e7005"; +const TEST_SEED_PHRASE: &str = + "\"other base behind follow wet put glad muscle unlock sell income october\""; #[allow(dead_code)] static ENV: &Map = &Map(phf::phf_map! { @@ -28,7 +30,10 @@ impl Speculos { #[allow(dead_code)] pub fn new() -> Self { #[allow(unused_mut)] - let apps_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("apps"); + let apps_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR")) + .join("tests") + .join("test_fixtures") + .join("apps"); let mut volumes = HashMap::new(); volumes.insert( apps_dir.to_str().unwrap().to_string(), @@ -44,7 +49,7 @@ pub struct Args; impl ImageArgs for Args { fn into_iterator(self) -> Box> { let container_elf_path = format!("{DEFAULT_APP_PATH}/stellarNanosApp.elf"); - let command_string = format!("/home/zondax/speculos/speculos.py --log-level speculos:DEBUG --color JADE_GREEN --display headless -s \"other base behind follow wet put glad muscle unlock sell income october\" -m nanos {container_elf_path}"); + let command_string = format!("/home/zondax/speculos/speculos.py --log-level speculos:DEBUG --color JADE_GREEN --display headless -s {TEST_SEED_PHRASE} -m nanos {container_elf_path}"); Box::new(vec![command_string].into_iter()) } }