Skip to content

Commit e7751d4

Browse files
committed
rust: don't mock C btc_address_simple
Since a while we can call C code directly also in unit tests.
1 parent ce49531 commit e7751d4

File tree

3 files changed

+9
-33
lines changed

3 files changed

+9
-33
lines changed

src/rust/bitbox02-rust/src/hww/api/bitcoin/signmsg.rs

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -135,9 +135,9 @@ mod tests {
135135
use std::boxed::Box;
136136
use util::bip32::HARDENED;
137137

138-
const ADDRESS: &str = "<address>";
139138
const KEYPATH: &[u32] = &[84 + HARDENED, 0 + HARDENED, 0 + HARDENED, 0, 0];
140139
const MESSAGE: &[u8] = b"message";
140+
const EXPECTED_ADDRESS: &str = "bc1qk5f9em9qc8yfpks8ngfg3h8h02n2e3yeqdyhpt";
141141
const EXPECTED_SIGNATURE: &[u8] = b"\x0f\x1d\x54\x2a\x9e\x2f\x37\x4e\xfe\xd4\x57\x8c\xaa\x84\x72\xd1\xc3\x12\x68\xfb\x89\x2d\x39\xa6\x15\x44\x59\x18\x5b\x2d\x35\x4d\x3b\x2b\xff\xf0\xe1\x61\x5c\x77\x25\x73\x4f\x43\x13\x4a\xb4\x51\x6b\x7e\x7c\xb3\x9d\x2d\xba\xaa\x5f\x4e\x8b\x8a\xff\x9f\x97\xd0\x00";
142142

143143
#[test]
@@ -159,10 +159,6 @@ mod tests {
159159
static mut CONFIRM_COUNTER: u32 = 0;
160160

161161
mock(Data {
162-
btc_address_simple: Some(Box::new(|coin, _, _| {
163-
assert_eq!(coin, BtcCoin::Btc as _);
164-
Ok(ADDRESS.into())
165-
})),
166162
ui_confirm_create: Some(Box::new(|params| {
167163
match unsafe {
168164
CONFIRM_COUNTER += 1;
@@ -175,7 +171,7 @@ mod tests {
175171
}
176172
2 => {
177173
assert_eq!(params.title, "Address");
178-
assert_eq!(params.body, ADDRESS);
174+
assert_eq!(params.body, EXPECTED_ADDRESS);
179175
true
180176
}
181177
3 => {
@@ -217,7 +213,6 @@ mod tests {
217213

218214
// Basic info dialog aborted.
219215
mock(Data {
220-
btc_address_simple: Some(Box::new(|_, _, _| Ok(ADDRESS.into()))),
221216
ui_confirm_create: Some(Box::new(|params| {
222217
match unsafe {
223218
CONFIRM_COUNTER += 1;
@@ -233,14 +228,14 @@ mod tests {
233228
})),
234229
..Default::default()
235230
});
231+
mock_unlocked();
236232
assert_eq!(block_on(process(&request)), Err(Error::UserAbort));
237233

238234
// Address verification aborted.
239235
unsafe {
240236
CONFIRM_COUNTER = 0;
241237
}
242238
mock(Data {
243-
btc_address_simple: Some(Box::new(|_, _, _| Ok(ADDRESS.into()))),
244239
ui_confirm_create: Some(Box::new(|params| {
245240
match unsafe {
246241
CONFIRM_COUNTER += 1;
@@ -249,22 +244,22 @@ mod tests {
249244
1 => true,
250245
2 => {
251246
assert_eq!(params.title, "Address");
252-
assert_eq!(params.body, ADDRESS);
247+
assert_eq!(params.body, EXPECTED_ADDRESS);
253248
false
254249
}
255250
_ => panic!("too many user confirmations"),
256251
}
257252
})),
258253
..Default::default()
259254
});
255+
mock_unlocked();
260256
assert_eq!(block_on(process(&request)), Err(Error::UserAbort));
261257

262258
// Message verification aborted.
263259
unsafe {
264260
CONFIRM_COUNTER = 0;
265261
}
266262
mock(Data {
267-
btc_address_simple: Some(Box::new(|_, _, _| Ok(ADDRESS.into()))),
268263
ui_confirm_create: Some(Box::new(|params| {
269264
match unsafe {
270265
CONFIRM_COUNTER += 1;
@@ -281,6 +276,7 @@ mod tests {
281276
})),
282277
..Default::default()
283278
});
279+
mock_unlocked();
284280
assert_eq!(block_on(process(&request)), Err(Error::UserAbort));
285281
}
286282

@@ -354,19 +350,19 @@ mod tests {
354350
Err(Error::InvalidInput)
355351
);
356352

357-
// Address could not be generated
353+
// Invalid keypath
358354
mock(Data {
359-
btc_address_simple: Some(Box::new(|_, _, _| Err(()))),
360355
..Default::default()
361356
});
357+
mock_unlocked();
362358
assert_eq!(
363359
block_on(process(&pb::BtcSignMessageRequest {
364360
coin: BtcCoin::Btc as _,
365361
script_config: Some(pb::BtcScriptConfigWithKeypath {
366362
script_config: Some(pb::BtcScriptConfig {
367363
config: Some(Config::SimpleType(SimpleType::P2wpkh as _))
368364
}),
369-
keypath: KEYPATH.to_vec(),
365+
keypath: [0].to_vec(),
370366
}),
371367
msg: MESSAGE.to_vec(),
372368
host_nonce_commitment: None,

src/rust/bitbox02/src/app_btc.rs

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ use alloc::string::String;
1717

1818
pub use bitbox02_sys::{BTCCoin, BTCScriptConfig_SimpleType};
1919

20-
#[cfg(not(feature = "testing"))]
2120
pub fn address_simple(
2221
coin: BTCCoin,
2322
script_type: BTCScriptConfig_SimpleType,
@@ -40,13 +39,3 @@ pub fn address_simple(
4039
false => Err(()),
4140
}
4241
}
43-
44-
#[cfg(feature = "testing")]
45-
pub fn address_simple(
46-
coin: BTCCoin,
47-
script_type: BTCScriptConfig_SimpleType,
48-
keypath: &[u32],
49-
) -> Result<String, ()> {
50-
let data = crate::testing::DATA.0.borrow();
51-
data.btc_address_simple.as_ref().unwrap()(coin, script_type, keypath)
52-
}

src/rust/bitbox02/src/testing.rs

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,6 @@ pub struct Data {
3838
pub backup_create: Option<Box<dyn Fn(u32, u32) -> Result<(), super::backup::Error>>>,
3939
pub keystore_encode_xpub_at_keypath:
4040
Option<Box<dyn Fn(&[u32], super::keystore::xpub_type_t) -> Result<String, ()>>>,
41-
pub btc_address_simple: Option<
42-
Box<
43-
dyn Fn(
44-
bitbox02_sys::BTCCoin,
45-
bitbox02_sys::BTCScriptConfig_SimpleType,
46-
&[u32],
47-
) -> Result<String, ()>,
48-
>,
49-
>,
5041
pub ui_transaction_address_create: Option<Box<dyn Fn(&str, &str) -> bool>>,
5142
pub ui_transaction_fee_create: Option<Box<dyn Fn(&str, &str) -> bool>>,
5243
}

0 commit comments

Comments
 (0)