Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions cmd/crates/stellar-ledger/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ once_cell = "1.19.0"
pretty_assertions = "1.2.1"
serial_test = "3.0.0"
httpmock = "0.7.0-rc.1"
test-case = "*"


[features]
Expand Down
98 changes: 60 additions & 38 deletions cmd/crates/stellar-ledger/tests/test/emulator_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,24 @@ mod test_helpers {
}
}

use test_helpers::test::{emulator_http_transport::EmulatorHttpTransport, speculos::Speculos};
use test_case::test_case;
use test_helpers::test::{
emulator_http_transport::EmulatorHttpTransport,
speculos::{Args, Speculos},
};

#[test_case("nanos".to_string() ; "when the device is NanoS")]
#[test_case("nanox".to_string() ; "when the device is NanoX")]
#[test_case("nanosp".to_string() ; "when the device is NanoS Plus")]
#[tokio::test]
async fn test_get_public_key() {
async fn test_get_public_key(ledger_device_model: String) {
let args = Args {
ledger_device_model,
};
let docker = clients::Cli::default();
let node = docker.run(Speculos::new());
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);
Expand All @@ -64,13 +73,18 @@ async fn test_get_public_key() {
node.stop();
}

#[test_case("nanos".to_string() ; "when the device is NanoS")]
#[test_case("nanox".to_string() ; "when the device is NanoX")]
#[test_case("nanosp".to_string() ; "when the device is NanoS Plus")]
#[tokio::test]
async fn test_get_app_configuration() {
async fn test_get_app_configuration(ledger_device_model: String) {
let args = Args {
ledger_device_model,
};
let docker = clients::Cli::default();
let node = docker.run(Speculos::new());
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);
Expand All @@ -89,13 +103,18 @@ async fn test_get_app_configuration() {
node.stop();
}

#[test_case("nanos".to_string() ; "when the device is NanoS")]
#[test_case("nanox".to_string() ; "when the device is NanoX")]
#[test_case("nanosp".to_string() ; "when the device is NanoS Plus")]
#[tokio::test]
async fn test_sign_tx() {
async fn test_sign_tx(ledger_device_model: String) {
let args = Args {
ledger_device_model,
};
let docker = clients::Cli::default();
let node = docker.run(Speculos::new());
let node = docker.run((Speculos::new(), 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));
Expand Down Expand Up @@ -156,7 +175,7 @@ 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 approve = tokio::task::spawn(approve_tx_signature(ui_host_port, args.ledger_device_model));

let result = sign.await.unwrap();
let _ = approve.await.unwrap();
Expand All @@ -175,13 +194,19 @@ async fn test_sign_tx() {
node.stop();
}

#[test_case("nanos".to_string() ; "when the device is NanoS")]
#[test_case("nanox".to_string() ; "when the device is NanoX")]
#[test_case("nanosp".to_string() ; "when the device is NanoS Plus")]
#[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(ledger_device_model: String) {
let args = Args {
ledger_device_model,
};

let docker = clients::Cli::default();
let node = docker.run(Speculos::new());
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);
Expand All @@ -201,10 +226,16 @@ async fn test_sign_tx_hash_when_hash_signing_is_not_enabled() {
node.stop();
}

#[test_case("nanos".to_string() ; "when the device is NanoS")]
#[test_case("nanox".to_string() ; "when the device is NanoX")]
#[test_case("nanosp".to_string() ; "when the device is NanoS Plus")]
#[tokio::test]
async fn test_sign_tx_hash_when_hash_signing_is_enabled() {
async fn test_sign_tx_hash_when_hash_signing_is_enabled(ledger_device_model: String) {
let args = Args {
ledger_device_model,
};
let docker = clients::Cli::default();
let node = docker.run(Speculos::new());
let node = docker.run((Speculos::new(), args.clone()));
let host_port = node.get_host_port_ipv4(9998);
let ui_host_port: u16 = node.get_host_port_ipv4(5000);

Expand All @@ -231,7 +262,10 @@ 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 approve = tokio::task::spawn(approve_tx_hash_signature(
ui_host_port,
args.ledger_device_model,
));

let response = sign.await.unwrap();
let _ = approve.await.unwrap();
Expand Down Expand Up @@ -318,6 +352,7 @@ async fn wait_for_emulator_start_text(ui_host_port: u16) {
if events.iter().any(|event| event.text == "is ready") {
ready = true;
}
sleep(Duration::from_secs(1)).await;
}
}

Expand All @@ -334,32 +369,19 @@ async fn get_emulator_events(ui_host_port: u16) -> Vec<EmulatorEvent> {
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;
}
File renamed without changes.
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ impl Exchange for EmulatorHttpTransport {
let resp: Response = HttpClient::new()
.post(&self.url)
.headers(headers)
.timeout(Duration::from_secs(20))
.timeout(Duration::from_secs(25))
.json(&request)
.send()
.await
Expand Down
24 changes: 20 additions & 4 deletions cmd/crates/stellar-ledger/tests/utils/speculos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<dyn Iterator<Item = String>> {
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())
}
}
Expand Down