@@ -375,4 +375,73 @@ mod test_tr_from_tpm_public {
375375 assert_eq ! ( expected_name, actual_name) ;
376376 assert_eq ! ( expected_data, actual_data) ;
377377 }
378+
379+ #[ cfg( has_esys_tr_get_tpm_handle) ]
380+ #[ test]
381+ fn test_tr_get_tpm_handle ( ) {
382+ use tss_esapi:: handles:: TpmHandle ;
383+
384+ let nv_index_tpm_handle = NvIndexTpmHandle :: new ( 0x01500024 ) . unwrap ( ) ;
385+ remove_nv_index_handle_from_tpm ( nv_index_tpm_handle, Provision :: Owner ) ;
386+
387+ let mut context = create_ctx_without_session ( ) ;
388+
389+ // closure for cleaning up if a call fails.
390+ let cleanup = |context : & mut Context ,
391+ e : tss_esapi:: Error ,
392+ handle : NvIndexHandle ,
393+ fn_name : & str |
394+ -> tss_esapi:: Error {
395+ // Set password authorization
396+ context. set_sessions ( ( Some ( AuthSession :: Password ) , None , None ) ) ;
397+ context
398+ . nv_undefine_space ( Provision :: Owner , handle)
399+ . expect ( "Failed to call nv_undefine_space" ) ;
400+ panic ! ( "{} failed: {}" , fn_name, e) ;
401+ } ;
402+
403+ // Create nv public.
404+ let nv_index_attributes = NvIndexAttributesBuilder :: new ( )
405+ . with_owner_write ( true )
406+ . with_owner_read ( true )
407+ . build ( )
408+ . expect ( "Failed to create owner nv index attributes" ) ;
409+
410+ let nv_public = NvPublicBuilder :: new ( )
411+ . with_nv_index ( nv_index_tpm_handle)
412+ . with_index_name_algorithm ( HashingAlgorithm :: Sha256 )
413+ . with_index_attributes ( nv_index_attributes)
414+ . with_data_area_size ( 32 )
415+ . build ( )
416+ . unwrap ( ) ;
417+ ///////////////////////////////////////////////////////////////
418+ // Define space
419+ //
420+ // Set password authorization when creating the space.
421+ context. set_sessions ( ( Some ( AuthSession :: Password ) , None , None ) ) ;
422+ let nv_index_handle = context
423+ . nv_define_space ( Provision :: Owner , None , nv_public)
424+ . expect ( "Failed to call nv_define_space" ) ;
425+ ///////////////////////////////////////////////////////////////
426+ // Get the TPM handle from the NV index handle object handle.
427+ //
428+ // Set password authorization
429+ let actual = context
430+ . tr_get_tpm_handle ( nv_index_handle. into ( ) )
431+ . map_err ( |e| cleanup ( & mut context, e, nv_index_handle, "tr_get" ) )
432+ . expect ( "Failed to get TPM handle" ) ;
433+ ///////////////////////////////////////////////////////////////
434+ // Remove undefine the space
435+ //
436+ // Set password authorization
437+ context. set_sessions ( ( Some ( AuthSession :: Password ) , None , None ) ) ;
438+ context
439+ . nv_undefine_space ( Provision :: Owner , nv_index_handle)
440+ . expect ( "Failed to call nv_undefine_space" ) ;
441+ ///////////////////////////////////////////////////////////////
442+ // Check that we got the correct handle
443+ //
444+ let expected = TpmHandle :: NvIndex ( nv_index_tpm_handle) ;
445+ assert_eq ! ( expected, actual) ;
446+ }
378447}
0 commit comments