Skip to content

Commit 3dba896

Browse files
committed
rust/api/bitcoin: unit test address_simple()
We couldn't unit test it before as we couldn't call C code from Rust unit tests. Now that we can, we should properly unit test it so that porting the remaining C code won't introduce regressions.
1 parent e7751d4 commit 3dba896

File tree

1 file changed

+186
-0
lines changed

1 file changed

+186
-0
lines changed

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

Lines changed: 186 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,3 +201,189 @@ pub async fn process_api(request: &Request) -> Option<Result<pb::btc_response::R
201201
_ => None,
202202
}
203203
}
204+
205+
#[cfg(test)]
206+
mod tests {
207+
use super::*;
208+
209+
use crate::bb02_async::block_on;
210+
use alloc::boxed::Box;
211+
use alloc::vec::Vec;
212+
use bitbox02::testing::{mock, mock_unlocked, Data, MUTEX};
213+
use util::bip32::HARDENED;
214+
215+
#[test]
216+
pub fn test_address_simple() {
217+
let _guard = MUTEX.lock().unwrap();
218+
219+
struct Test<'a> {
220+
coin: BtcCoin,
221+
keypath: &'a [u32],
222+
simple_type: SimpleType,
223+
expected_address: &'a str,
224+
expected_display_title: &'a str,
225+
}
226+
227+
for test in vec![
228+
// BTC P2WPKH-P2SH
229+
Test {
230+
coin: BtcCoin::Btc,
231+
keypath: &[49 + HARDENED, 0 + HARDENED, 0 + HARDENED, 0, 0],
232+
simple_type: SimpleType::P2wpkhP2sh,
233+
expected_address: "3BaL6XecvLAidPToUDhXo1zxD99ZUrErpd",
234+
expected_display_title: "Bitcoin",
235+
},
236+
Test {
237+
coin: BtcCoin::Btc,
238+
keypath: &[49 + HARDENED, 0 + HARDENED, 0 + HARDENED, 0, 1],
239+
simple_type: SimpleType::P2wpkhP2sh,
240+
expected_address: "3QRfCGEJVzvR1HN13kxB7xkuUtdEvG2orZ",
241+
expected_display_title: "Bitcoin",
242+
},
243+
Test {
244+
coin: BtcCoin::Btc,
245+
keypath: &[49 + HARDENED, 0 + HARDENED, 1 + HARDENED, 1, 100],
246+
simple_type: SimpleType::P2wpkhP2sh,
247+
expected_address: "39r7CFVo1wpb3mxQfkG6yYyxMAfqAmZMhA",
248+
expected_display_title: "Bitcoin",
249+
},
250+
// BTC P2WPKH
251+
Test {
252+
coin: BtcCoin::Btc,
253+
keypath: &[84 + HARDENED, 0 + HARDENED, 0 + HARDENED, 0, 0],
254+
simple_type: SimpleType::P2wpkh,
255+
expected_address: "bc1qk5f9em9qc8yfpks8ngfg3h8h02n2e3yeqdyhpt",
256+
expected_display_title: "Bitcoin",
257+
},
258+
Test {
259+
coin: BtcCoin::Btc,
260+
keypath: &[84 + HARDENED, 0 + HARDENED, 0 + HARDENED, 0, 1],
261+
simple_type: SimpleType::P2wpkh,
262+
expected_address: "bc1qtn7feuj7juxkzf48zfxtngrcyqyns9f4ska7hg",
263+
expected_display_title: "Bitcoin",
264+
},
265+
Test {
266+
coin: BtcCoin::Btc,
267+
keypath: &[84 + HARDENED, 0 + HARDENED, 1 + HARDENED, 1, 100],
268+
simple_type: SimpleType::P2wpkh,
269+
expected_address: "bc1qarhxx6daqetewkjwz9p6y78a28ygxm2vndhdas",
270+
expected_display_title: "Bitcoin",
271+
},
272+
// TBTC P2WPKH-P2SH
273+
Test {
274+
coin: BtcCoin::Tbtc,
275+
keypath: &[49 + HARDENED, 1 + HARDENED, 0 + HARDENED, 0, 0],
276+
simple_type: SimpleType::P2wpkhP2sh,
277+
expected_address: "2N5Tjwx5Htk7gLbv7nWqXUgpg5K2Uf4TacQ",
278+
expected_display_title: "BTC Testnet",
279+
},
280+
// TBTC P2WPKH
281+
Test {
282+
coin: BtcCoin::Tbtc,
283+
keypath: &[84 + HARDENED, 1 + HARDENED, 0 + HARDENED, 0, 0],
284+
simple_type: SimpleType::P2wpkh,
285+
expected_address: "tb1qnlyrq9pshg0v0lsuudjgga4nvmjxhcvketqwdg",
286+
expected_display_title: "BTC Testnet",
287+
},
288+
// LTC P2WPKH-P2SH
289+
Test {
290+
coin: BtcCoin::Ltc,
291+
keypath: &[49 + HARDENED, 2 + HARDENED, 0 + HARDENED, 0, 0],
292+
simple_type: SimpleType::P2wpkhP2sh,
293+
expected_address: "MMmYgSH7fbTPnfdi1vTejMJyY7rKY4j9qv",
294+
expected_display_title: "Litecoin",
295+
},
296+
Test {
297+
coin: BtcCoin::Ltc,
298+
keypath: &[49 + HARDENED, 2 + HARDENED, 0 + HARDENED, 0, 1],
299+
simple_type: SimpleType::P2wpkhP2sh,
300+
expected_address: "M7wA8gBLL4SBiwQ1muQeKcG6naYqWcaUHg",
301+
expected_display_title: "Litecoin",
302+
},
303+
Test {
304+
coin: BtcCoin::Ltc,
305+
keypath: &[49 + HARDENED, 2 + HARDENED, 1 + HARDENED, 1, 100],
306+
simple_type: SimpleType::P2wpkhP2sh,
307+
expected_address: "MPBnihMP2JYjPtBnLxGydqvaALBsc5ALTG",
308+
expected_display_title: "Litecoin",
309+
},
310+
// LTC P2WPKH
311+
Test {
312+
coin: BtcCoin::Ltc,
313+
keypath: &[84 + HARDENED, 2 + HARDENED, 0 + HARDENED, 0, 0],
314+
simple_type: SimpleType::P2wpkh,
315+
expected_address: "ltc1q7598y6mzud5fka043vs4vkx7zktvppxffsf7e3",
316+
expected_display_title: "Litecoin",
317+
},
318+
Test {
319+
coin: BtcCoin::Ltc,
320+
keypath: &[84 + HARDENED, 2 + HARDENED, 0 + HARDENED, 0, 1],
321+
simple_type: SimpleType::P2wpkh,
322+
expected_address: "ltc1qtgjfu2ltg4slmksv27awmh6h2pccvsth4mw2w9",
323+
expected_display_title: "Litecoin",
324+
},
325+
Test {
326+
coin: BtcCoin::Ltc,
327+
keypath: &[84 + HARDENED, 2 + HARDENED, 1 + HARDENED, 1, 100],
328+
simple_type: SimpleType::P2wpkh,
329+
expected_address: "ltc1qwsz89auhpezjfllq9y9qegpfgdwpw5vesppsz0",
330+
expected_display_title: "Litecoin",
331+
},
332+
// TLTC P2WPKH-P2SH
333+
Test {
334+
coin: BtcCoin::Tltc,
335+
keypath: &[49 + HARDENED, 1 + HARDENED, 0 + HARDENED, 0, 0],
336+
simple_type: SimpleType::P2wpkhP2sh,
337+
expected_address: "2N5Tjwx5Htk7gLbv7nWqXUgpg5K2Uf4TacQ",
338+
expected_display_title: "LTC Testnet",
339+
},
340+
// TLTC P2WPKH
341+
Test {
342+
coin: BtcCoin::Tltc,
343+
keypath: &[84 + HARDENED, 1 + HARDENED, 0 + HARDENED, 0, 0],
344+
simple_type: SimpleType::P2wpkh,
345+
expected_address: "tltc1qnlyrq9pshg0v0lsuudjgga4nvmjxhcvkqrzsap",
346+
expected_display_title: "LTC Testnet",
347+
},
348+
] {
349+
let mut req = pb::BtcPubRequest {
350+
coin: test.coin as _,
351+
keypath: test.keypath.to_vec(),
352+
display: false,
353+
output: Some(Output::ScriptConfig(BtcScriptConfig {
354+
config: Some(Config::SimpleType(test.simple_type as _)),
355+
})),
356+
};
357+
358+
// Without display.
359+
mock_unlocked();
360+
assert_eq!(
361+
block_on(process_pub(&req)),
362+
Some(Ok(Response::Pub(pb::PubResponse {
363+
r#pub: test.expected_address.into(),
364+
}))),
365+
);
366+
367+
// With display.
368+
req.display = true;
369+
let expected_display_title = test.expected_display_title.clone();
370+
let expected_address = test.expected_address.clone();
371+
mock(Data {
372+
ui_confirm_create: Some(Box::new(move |params| {
373+
assert_eq!(params.title, expected_display_title);
374+
assert_eq!(params.body, expected_address);
375+
assert!(params.scrollable);
376+
true
377+
})),
378+
..Default::default()
379+
});
380+
mock_unlocked();
381+
assert_eq!(
382+
block_on(process_pub(&req)),
383+
Some(Ok(Response::Pub(pb::PubResponse {
384+
r#pub: test.expected_address.into()
385+
}))),
386+
);
387+
}
388+
}
389+
}

0 commit comments

Comments
 (0)