@@ -99,15 +99,15 @@ pub async fn create(
9999
100100 let is_initialized = bitbox02:: memory:: is_initialized ( ) ;
101101
102- if is_initialized {
103- unlock:: unlock_keystore ( hal, "Unlock device" , unlock:: CanCancel :: Yes ) . await ?;
104- }
105-
106- let seed = bitbox02 :: keystore :: copy_seed ( ) ? ;
107-
108- // Yield now to give executor a chance to process USB/BLE communication, as copy_seed() causes
109- // some delay.
110- futures_lite :: future :: yield_now ( ) . await ;
102+ let seed = if is_initialized {
103+ unlock:: unlock_keystore ( hal, "Unlock device" , unlock:: CanCancel :: Yes ) . await ?
104+ } else {
105+ let seed = bitbox02 :: keystore :: copy_seed ( ) ? ;
106+ // Yield now to give executor a chance to process USB/BLE communication, as copy_seed() causes
107+ // some delay.
108+ futures_lite :: future :: yield_now ( ) . await ;
109+ seed
110+ } ;
111111
112112 let seed_birthdate = if !is_initialized {
113113 if bitbox02:: memory:: set_seed_birthdate ( timestamp) . is_err ( ) {
@@ -186,6 +186,7 @@ mod tests {
186186
187187 let mut mock_hal = TestingHal :: new ( ) ;
188188 mock_hal. sd . inserted = Some ( true ) ;
189+ bitbox02:: securechip:: fake_event_counter_reset ( ) ;
189190 assert_eq ! (
190191 block_on( create(
191192 & mut mock_hal,
@@ -196,6 +197,7 @@ mod tests {
196197 ) ) ,
197198 Ok ( Response :: Success ( pb:: Success { } ) )
198199 ) ;
200+ assert_eq ! ( bitbox02:: securechip:: fake_event_counter( ) , 1 ) ;
199201 assert_eq ! ( EXPECTED_TIMESTMAP , bitbox02:: memory:: get_seed_birthdate( ) ) ;
200202 assert_eq ! (
201203 mock_hal. ui. screens,
@@ -223,6 +225,68 @@ mod tests {
223225 ) ;
224226 }
225227
228+ /// Test backup creation on a initialized keystore. The sdcard does not contain the backup yet.
229+ #[ test]
230+ pub fn test_create_initialized_new ( ) {
231+ const TIMESTMAP : u32 = 1601281809 ;
232+
233+ mock_memory ( ) ;
234+
235+ let seed = hex:: decode ( "cb33c20cea62a5c277527e2002da82e6e2b37450a755143a540a54cea8da9044" )
236+ . unwrap ( ) ;
237+ bitbox02:: keystore:: encrypt_and_store_seed ( & seed, "password" ) . unwrap ( ) ;
238+ bitbox02:: memory:: set_initialized ( ) . unwrap ( ) ;
239+
240+ let mut password_entered: bool = false ;
241+
242+ let mut mock_hal = TestingHal :: new ( ) ;
243+ mock_hal. sd . inserted = Some ( true ) ;
244+ mock_hal. ui . set_enter_string ( Box :: new ( |_params| {
245+ password_entered = true ;
246+ Ok ( "password" . into ( ) )
247+ } ) ) ;
248+ bitbox02:: securechip:: fake_event_counter_reset ( ) ;
249+ assert_eq ! (
250+ block_on( create(
251+ & mut mock_hal,
252+ & pb:: CreateBackupRequest {
253+ timestamp: TIMESTMAP ,
254+ timezone_offset: 18000 ,
255+ }
256+ ) ) ,
257+ Ok ( Response :: Success ( pb:: Success { } ) )
258+ ) ;
259+ assert_eq ! ( bitbox02:: securechip:: fake_event_counter( ) , 5 ) ;
260+ assert_eq ! (
261+ mock_hal. ui. screens,
262+ vec![
263+ Screen :: Confirm {
264+ title: "Is today?" . into( ) ,
265+ body: "Mon 2020-09-28" . into( ) ,
266+ longtouch: false
267+ } ,
268+ Screen :: Status {
269+ title: "Backup created" . into( ) ,
270+ success: true
271+ }
272+ ]
273+ ) ;
274+
275+ mock_hal. ui . remove_enter_string ( ) ; // no more password entry needed
276+ assert_eq ! (
277+ block_on( check(
278+ & mut mock_hal,
279+ & pb:: CheckBackupRequest { silent: true }
280+ ) ) ,
281+ Ok ( Response :: CheckBackup ( pb:: CheckBackupResponse {
282+ id: backup:: id( & seed) ,
283+ } ) )
284+ ) ;
285+
286+ drop ( mock_hal) ; // to remove mutable borrow of `password_entered`
287+ assert ! ( password_entered) ;
288+ }
289+
226290 /// Use backup file fixtures generated using firmware v9.12.0 and perform tests on it. This
227291 /// should catch regressions when changing backup loading/verification in the firmware code.
228292 #[ test]
0 commit comments