@@ -179,3 +179,55 @@ pub async fn unlock(hal: &mut impl crate::hal::Hal) -> Result<(), ()> {
179179 unlock_bip39 ( hal, & bitbox02:: keystore:: copy_seed ( ) ?) . await ;
180180 Ok ( ( ) )
181181}
182+
183+ #[ cfg( test) ]
184+ mod tests {
185+ use super :: * ;
186+
187+ use crate :: hal:: testing:: TestingHal ;
188+ use crate :: workflow:: testing:: Screen ;
189+ use alloc:: boxed:: Box ;
190+ use bitbox02:: testing:: { mock_memory, mock_unlocked, mock_unlocked_using_mnemonic} ;
191+ use util:: bb02_async:: block_on;
192+
193+ #[ test]
194+ fn test_unlock_success ( ) {
195+ mock_memory ( ) ;
196+
197+ // Set up an initialized wallet with password
198+ bitbox02:: keystore:: encrypt_and_store_seed (
199+ hex:: decode ( "c7940c13479b8d9a6498f4e50d5a42e0d617bc8e8ac9f2b8cecf97e94c2b035c" )
200+ . unwrap ( )
201+ . as_slice ( ) ,
202+ "password" ,
203+ )
204+ . unwrap ( ) ;
205+
206+ bitbox02:: memory:: set_initialized ( ) . unwrap ( ) ;
207+
208+ // Lock the keystore to simulate the normal locked state
209+ bitbox02:: keystore:: lock ( ) ;
210+
211+ let mut password_entered = false ;
212+
213+ let mut mock_hal = TestingHal :: new ( ) ;
214+ mock_hal. ui . set_enter_string ( Box :: new ( |_params| {
215+ password_entered = true ;
216+ Ok ( "password" . into ( ) )
217+ } ) ) ;
218+ bitbox02:: securechip:: fake_event_counter_reset ( ) ;
219+ assert_eq ! ( block_on( unlock( & mut mock_hal) ) , Ok ( ( ) ) ) ;
220+ assert_eq ! ( bitbox02:: securechip:: fake_event_counter( ) , 8 ) ;
221+
222+ assert ! ( !bitbox02:: keystore:: is_locked( ) ) ;
223+
224+ assert_eq ! (
225+ bitbox02:: keystore:: copy_bip39_seed( ) . unwrap( ) . as_slice( ) ,
226+ hex:: decode( "cff4b263e5b0eb299e5fd35fcd09988f6b14e5b464f8d18fb84b152f889dd2a30550f4c2b346cae825ffedd4a87fc63fc12a9433de5125b6c7fdbc5eab0c590b" )
227+ . unwrap( ) ,
228+ ) ;
229+
230+ drop ( mock_hal) ; // to remove mutable borrow of `password_entered`
231+ assert ! ( password_entered) ;
232+ }
233+ }
0 commit comments