From 4c5d3b628365cd39d1a7fd7f92dec737bc8efcf1 Mon Sep 17 00:00:00 2001 From: Elizabeth Engelman <4752801+elizabethengelman@users.noreply.github.com> Date: Thu, 30 May 2024 17:58:52 -0400 Subject: [PATCH] Update emulator_tests to test with different ledger models --- .../stellar-ledger/src/emulator_tests.rs | 123 ++++++++++++------ .../stellar-ledger/tests/test/speculos.rs | 24 +++- 2 files changed, 105 insertions(+), 42 deletions(-) diff --git a/cmd/crates/stellar-ledger/src/emulator_tests.rs b/cmd/crates/stellar-ledger/src/emulator_tests.rs index 5a9dbbfe9b..d7ae6d3276 100644 --- a/cmd/crates/stellar-ledger/src/emulator_tests.rs +++ b/cmd/crates/stellar-ledger/src/emulator_tests.rs @@ -27,15 +27,29 @@ mod test_helpers { } } -use test_helpers::test::{emulator_http_transport::EmulatorHttpTransport, speculos::Speculos}; +use test_helpers::test::{ + emulator_http_transport::EmulatorHttpTransport, + speculos::{Args, Speculos}, +}; #[tokio::test] -async fn test_get_public_key() { +async fn test_get_public_key_for_different_ledger_devices() { + let device_models = ["nanos", "nanox", "nanosp"]; + for device_model in device_models.iter() { + let args = Args { + ledger_device_model: device_model.to_string(), + }; + println!("Running test_get_public_key for {device_model}"); + test_get_public_key(args).await; + } +} + +async fn test_get_public_key(image_args: Args) { let docker = clients::Cli::default(); - let node = docker.run(Speculos::new()); + let args = image_args.clone(); + let node = docker.run((Speculos::new(), args)); let host_port = node.get_host_port_ipv4(9998); let ui_host_port: u16 = node.get_host_port_ipv4(5000); - wait_for_emulator_start_text(ui_host_port).await; let ledger = ledger(host_port); @@ -59,12 +73,22 @@ async fn test_get_public_key() { } #[tokio::test] -async fn test_get_app_configuration() { +async fn test_get_app_configuration_for_different_ledger_devices() { + let device_models = ["nanos", "nanox", "nanosp"]; + for device_model in device_models.iter() { + let args = Args { + ledger_device_model: device_model.to_string(), + }; + println!("Running test_get_app_configuration for {device_model}"); + test_get_app_configuration(args).await; + } +} + +async fn test_get_app_configuration(image_args: Args) { let docker = clients::Cli::default(); - let node = docker.run(Speculos::new()); + let node = docker.run((Speculos::new(), image_args.clone())); let host_port = node.get_host_port_ipv4(9998); let ui_host_port: u16 = node.get_host_port_ipv4(5000); - wait_for_emulator_start_text(ui_host_port).await; let ledger = ledger(host_port); @@ -84,12 +108,22 @@ async fn test_get_app_configuration() { } #[tokio::test] -async fn test_sign_tx() { +async fn test_sign_tx_for_different_ledger_devices() { + let device_models = ["nanos", "nanox", "nanosp"]; + for device_model in device_models.iter() { + let args = Args { + ledger_device_model: device_model.to_string(), + }; + println!("Running test_sign_tx for {device_model}"); + test_sign_tx(args).await; + } +} + +async fn test_sign_tx(image_args: Args) { let docker = clients::Cli::default(); - let node = docker.run(Speculos::new()); + let node = docker.run((Speculos::new(), image_args.clone())); let host_port = node.get_host_port_ipv4(9998); let ui_host_port: u16 = node.get_host_port_ipv4(5000); - wait_for_emulator_start_text(ui_host_port).await; let ledger = Arc::new(ledger(host_port)); @@ -150,7 +184,8 @@ async fn test_sign_tx() { let ledger = Arc::clone(&ledger); async move { ledger.sign_transaction(path, tx, test_network_hash()).await } }); - let approve = tokio::task::spawn(approve_tx_signature(ui_host_port)); + let args = image_args.clone(); + let approve = tokio::task::spawn(approve_tx_signature(ui_host_port, args.ledger_device_model)); let result = sign.await.unwrap(); let _ = approve.await.unwrap(); @@ -170,12 +205,22 @@ async fn test_sign_tx() { } #[tokio::test] -async fn test_sign_tx_hash_when_hash_signing_is_not_enabled() { +async fn test_sign_tx_hash_when_hash_signing_is_not_enabled_for_different_ledger_devices() { + let device_models = ["nanos", "nanox", "nanosp"]; + for device_model in device_models.iter() { + let args = Args { + ledger_device_model: device_model.to_string(), + }; + println!("Running test_sign_tx_hash_when_hash_signing_is_not_enabled for {device_model}"); + test_sign_tx_hash_when_hash_signing_is_not_enabled(args).await; + } +} + +async fn test_sign_tx_hash_when_hash_signing_is_not_enabled(image_args: Args) { let docker = clients::Cli::default(); - let node = docker.run(Speculos::new()); + let node = docker.run((Speculos::new(), image_args.clone())); let host_port = node.get_host_port_ipv4(9998); let ui_host_port: u16 = node.get_host_port_ipv4(5000); - wait_for_emulator_start_text(ui_host_port).await; let ledger = ledger(host_port); @@ -196,9 +241,20 @@ async fn test_sign_tx_hash_when_hash_signing_is_not_enabled() { } #[tokio::test] -async fn test_sign_tx_hash_when_hash_signing_is_enabled() { +async fn test_sign_tx_hash_when_hash_signing_is_enabled_for_different_ledger_devices() { + let device_models = ["nanos", "nanosp", "nanox"]; + for device_model in device_models.iter() { + let args = Args { + ledger_device_model: device_model.to_string(), + }; + println!("Running test_sign_tx_hash_when_hash_signing_is_enabled for {device_model}"); + test_sign_tx_hash_when_hash_signing_is_enabled(args).await; + } +} + +async fn test_sign_tx_hash_when_hash_signing_is_enabled(image_args: Args) { let docker = clients::Cli::default(); - let node = docker.run(Speculos::new()); + let node = docker.run((Speculos::new(), image_args.clone())); let host_port = node.get_host_port_ipv4(9998); let ui_host_port: u16 = node.get_host_port_ipv4(5000); @@ -225,7 +281,11 @@ async fn test_sign_tx_hash_when_hash_signing_is_enabled() { let ledger = Arc::clone(&ledger); async move { ledger.sign_transaction_hash(path, &test_hash).await } }); - let approve = tokio::task::spawn(approve_tx_hash_signature(ui_host_port)); + let args = image_args.clone(); + let approve = tokio::task::spawn(approve_tx_hash_signature( + ui_host_port, + args.ledger_device_model, + )); let response = sign.await.unwrap(); let _ = approve.await.unwrap(); @@ -328,32 +388,19 @@ async fn get_emulator_events(ui_host_port: u16) -> Vec { resp.events } -async fn approve_tx_hash_signature(ui_host_port: u16) { - for _ in 0..10 { +async fn approve_tx_hash_signature(ui_host_port: u16, device_model: String) { + let number_of_right_clicks = if device_model == "nanos" { 10 } else { 6 }; + for _ in 0..number_of_right_clicks { click(ui_host_port, "button/right").await; } click(ui_host_port, "button/both").await; } -async fn approve_tx_signature(ui_host_port: u16) { - let mut map = HashMap::new(); - map.insert("action", "press-and-release"); - - let client = reqwest::Client::new(); - for _ in 0..17 { - client - .post(format!("http://localhost:{ui_host_port}/button/right")) - .json(&map) - .send() - .await - .unwrap(); +async fn approve_tx_signature(ui_host_port: u16, device_model: String) { + let number_of_right_clicks = if device_model == "nanos" { 17 } else { 11 }; + for _ in 0..number_of_right_clicks { + click(ui_host_port, "button/right").await; } - - client - .post(format!("http://localhost:{ui_host_port}/button/both")) - .json(&map) - .send() - .await - .unwrap(); + click(ui_host_port, "button/both").await; } diff --git a/cmd/crates/stellar-ledger/tests/test/speculos.rs b/cmd/crates/stellar-ledger/tests/test/speculos.rs index 52dc506851..caccdfc389 100644 --- a/cmd/crates/stellar-ledger/tests/test/speculos.rs +++ b/cmd/crates/stellar-ledger/tests/test/speculos.rs @@ -43,13 +43,29 @@ impl Speculos { } } -#[derive(Debug, Default, Clone)] -pub struct Args; +#[derive(Debug, Clone)] +pub struct Args { + pub ledger_device_model: String, +} + +impl Default for Args { + fn default() -> Self { + Self { + ledger_device_model: "nanos".to_string(), + } + } +} 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 {TEST_SEED_PHRASE} -m nanos {container_elf_path}"); + let device_model = self.ledger_device_model.clone(); + let container_elf_path = match device_model.as_str() { + "nanos" => format!("{DEFAULT_APP_PATH}/stellarNanoSApp.elf"), + "nanosp" => format!("{DEFAULT_APP_PATH}/stellarNanoSPApp.elf"), + "nanox" => format!("{DEFAULT_APP_PATH}/stellarNanoXApp.elf"), + _ => panic!("Unsupported device model"), + }; + let command_string = format!("/home/zondax/speculos/speculos.py --log-level speculos:DEBUG --color JADE_GREEN --display headless -s {TEST_SEED_PHRASE} -m {device_model} {container_elf_path}"); Box::new(vec![command_string].into_iter()) } }