Skip to content

Commit d1c3efd

Browse files
committed
refactor(rust): refactor background auto-approving thread
Also, remove tracing from unit-tests and enable `test_ecdh_shared_secret`. [no changelog]
1 parent 173e644 commit d1c3efd

1 file changed

Lines changed: 42 additions & 46 deletions

File tree

rust/trezor-client/src/lib.rs

Lines changed: 42 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -169,10 +169,11 @@ mod tests {
169169
emulator
170170
}
171171

172-
/// Spawn a thread that continuously sends a "YES" decision to the debug link.
173-
fn spawn_debug_yes_loop<'scope, 'env>(
174-
scope: &'scope thread::Scope<'scope, 'env>,
175-
) -> Arc<AtomicBool> {
172+
/// Spawn a background thread that continuously sends a "YES" decision to the debug link.
173+
fn with_auto_approve<F, T>(f: F) -> T
174+
where
175+
F: FnOnce() -> T,
176+
{
176177
let mut debuglink = find_devices(true)
177178
.into_iter()
178179
.find(|t| t.model == Model::TrezorEmulator)
@@ -186,17 +187,20 @@ mod tests {
186187
let mut req = DebugLinkDecision::new();
187188
req.set_button(DebugButton::YES);
188189

189-
scope.spawn(move || {
190-
while !stop_clone.load(Ordering::Relaxed) {
191-
// DebugLinkDecision has no response; send fire-and-forget then
192-
// poll state so the firmware has time to process the decision.
193-
let _ = debuglink.send_message(req.clone());
194-
let _ = debuglink
195-
.call(DebugLinkGetState::new(), Box::new(|_, m: DebugLinkState| Ok(m)));
196-
}
197-
});
198-
199-
stop
190+
thread::scope(|scope| {
191+
scope.spawn(move || {
192+
while !stop_clone.load(Ordering::Relaxed) {
193+
// DebugLinkDecision has no response; send fire-and-forget then poll
194+
// state (which returns when the firmware has processed the decision).
195+
let _ = debuglink.send_message(req.clone());
196+
let _ = debuglink
197+
.call(DebugLinkGetState::new(), Box::new(|_, m: DebugLinkState| Ok(m)));
198+
}
199+
});
200+
let res = f();
201+
stop.store(true, Ordering::Relaxed);
202+
res
203+
})
200204
}
201205

202206
#[test]
@@ -233,12 +237,9 @@ mod tests {
233237
assert_eq!(address.ok().unwrap().to_string(), "mvbu1Gdy8SUjTenqerxUaZyYjmveZvt33q");
234238
}
235239

236-
#[ignore]
237240
#[test]
238241
#[serial]
239242
fn test_ecdh_shared_secret() {
240-
tracing_subscriber::fmt().with_max_level(tracing::Level::TRACE).init();
241-
242243
let mut emulator = init_emulator();
243244
assert_eq!(emulator.features().expect("Failed to get features").label(), "SLIP-0014");
244245

@@ -252,11 +253,14 @@ mod tests {
252253

253254
let peer_public_key = Vec::from_hex("0407f2c6e5becf3213c1d07df0cfbe8e39f70a8c643df7575e5c56859ec52c45ca950499c019719dae0fda04248d851e52cf9d66eeb211d89a77be40de22b6c89d").unwrap();
254255
let curve_name = "secp256k1".to_owned();
255-
let response = handle_interaction(
256-
emulator
257-
.get_ecdh_session_key(ident, peer_public_key, curve_name)
258-
.expect("Failed to get ECDH shared secret"),
259-
)
256+
257+
let response = with_auto_approve(|| {
258+
handle_interaction(
259+
emulator
260+
.get_ecdh_session_key(ident, peer_public_key, curve_name)
261+
.expect("Failed to get ECDH shared secret"),
262+
)
263+
})
260264
.unwrap();
261265

262266
let expected_session_key = Vec::from_hex("048125883b086746244b0d2c548860ecc723346e14c87e51dc7ba32791bc780d132dbd814fbee77134f318afac6ad6db3c5334efe6a8798628a1038195b96e82e2").unwrap();
@@ -271,32 +275,24 @@ mod tests {
271275
#[test]
272276
#[serial]
273277
fn test_ethereum_sign_tx_large() {
274-
tracing_subscriber::fmt().with_max_level(tracing::Level::TRACE).init();
275-
276278
let mut emulator = init_emulator();
277279
assert_eq!(emulator.features().expect("Failed to get features").label(), "SLIP-0014");
278280

279-
let signature = thread::scope(|s| {
280-
let stop = spawn_debug_yes_loop(s);
281-
282-
let signature = emulator
283-
.ethereum_sign_tx(
284-
// default ETH path
285-
vec![44 + (1 << 31), 60 + (1 << 31), 0 + (1 << 31), 0, 0],
286-
vec![],
287-
vec![],
288-
vec![],
289-
"".to_string(),
290-
vec![],
291-
// 10kB of empty data
292-
vec![0; 10 * 1024],
293-
Some(1u64),
294-
)
295-
.unwrap();
296-
297-
stop.store(true, Ordering::Relaxed);
298-
signature
299-
});
281+
let signature = with_auto_approve(|| {
282+
emulator.ethereum_sign_tx(
283+
// default ETH path
284+
vec![44 + (1 << 31), 60 + (1 << 31), 0 + (1 << 31), 0, 0],
285+
vec![],
286+
vec![],
287+
vec![],
288+
"".to_string(),
289+
vec![],
290+
// 10kB of empty data
291+
vec![0; 10 * 1024],
292+
Some(1u64),
293+
)
294+
})
295+
.unwrap();
300296

301297
assert_eq!(signature.r.len(), 32);
302298
assert_eq!(signature.s.len(), 32);

0 commit comments

Comments
 (0)