2024-07-05 00:21:45 +03:00
use sequoia_openpgp as openpgp ;
use openpgp ::KeyHandle ;
use openpgp ::KeyID ;
use openpgp ::Result ;
use openpgp ::packet ::Key ;
2024-08-15 14:38:43 +03:00
use super ::common ::power_set ;
use super ::common ::Sq ;
2024-07-05 00:21:45 +03:00
2024-08-15 15:05:24 +03:00
#[ test ]
fn sq_key_subkey_delete ( ) -> Result < ( ) >
{
let sq = Sq ::new ( ) ;
2024-07-05 00:21:45 +03:00
2024-08-15 15:05:24 +03:00
// Generate a key in a file.
let ( cert , cert_file , _rev_file ) = sq . key_generate ( & [ ] , & [ " alice " ] ) ;
assert! ( cert . is_tsk ( ) ) ;
// Delete each non-empty subset of keys.
eprintln! ( " Certificate: " ) ;
for k in cert . keys ( ) {
eprintln! ( " {} " , k . fingerprint ( ) ) ;
}
2024-07-05 00:21:45 +03:00
2024-08-15 15:05:24 +03:00
let keys : Vec < Key < _ , _ > > = cert . keys ( )
. map ( | k | {
k . key ( ) . clone ( )
} )
. collect ( ) ;
2024-07-05 00:21:45 +03:00
2024-08-15 15:05:24 +03:00
let key_ids = keys . iter ( ) . map ( | k | k . fingerprint ( ) ) . collect ::< Vec < _ > > ( ) ;
2024-07-05 00:21:45 +03:00
2024-08-15 15:05:24 +03:00
for ( ( ( i , to_delete ) , keystore ) , by_fpr ) in power_set ( & key_ids ) . into_iter ( )
. enumerate ( )
. flat_map ( | x | [ ( x . clone ( ) , false ) , ( x . clone ( ) , true ) ] )
. flat_map ( | x | [ ( x . clone ( ) , false ) , ( x . clone ( ) , true ) ] )
{
eprintln! ( " Test # {} , by {} , from {} : " ,
i + 1 ,
if by_fpr { " fingerprint " } else { " key ID " } ,
if keystore {
" the key store " . to_string ( )
} else {
cert_file . display ( ) . to_string ( )
} ) ;
eprintln! ( " Deleting: " ) ;
for k in to_delete . iter ( ) {
eprintln! ( " {} " , k ) ;
2024-07-05 00:21:45 +03:00
}
2024-08-15 15:05:24 +03:00
let to_delete_kh : Vec < KeyHandle > = if by_fpr {
to_delete . iter ( )
. map ( | fpr | KeyHandle ::from ( fpr ) )
. collect ( )
} else {
to_delete . iter ( )
. map ( | fpr | KeyHandle ::from ( KeyID ::from ( fpr ) ) )
. collect ( )
} ;
// Delete the selection.
let got = if keystore {
// Import it into the key store.
sq . key_import ( & cert_file ) ;
sq . key_subkey_delete (
cert . key_handle ( ) , & to_delete_kh , None )
} else {
sq . key_subkey_delete (
& cert_file , & to_delete_kh , None )
} ;
// Make sure we got exactly what we asked for; no
// more, no less.
eprintln! ( " Got: " ) ;
let mut deletions = 0 ;
for got in got . keys ( ) {
eprintln! ( " {} {} secret key material " ,
got . fingerprint ( ) ,
if got . has_secret ( ) {
" has "
2024-07-05 00:21:45 +03:00
} else {
2024-08-15 15:05:24 +03:00
" doesn't have "
2024-07-05 00:21:45 +03:00
} ) ;
2024-08-15 15:05:24 +03:00
let should_have_deleted
= to_delete . contains ( & got . fingerprint ( ) ) ;
if should_have_deleted {
assert! (
! got . has_secret ( ) ,
" got secret key material \
for a key we deleted ( { } ) " ,
got . fingerprint ( ) ) ;
deletions + = 1 ;
2024-07-05 00:21:45 +03:00
} else {
2024-08-15 15:05:24 +03:00
assert! (
got . has_secret ( ) ,
" didn't get secret key material \
for a key we didn ' t delete ( { } ) " ,
got . fingerprint ( ) ) ;
2024-07-05 00:21:45 +03:00
}
}
2024-08-15 15:05:24 +03:00
assert_eq! ( deletions , to_delete . len ( ) ) ;
2024-07-05 00:21:45 +03:00
}
2024-08-15 15:05:24 +03:00
Ok ( ( ) )
2024-07-05 00:21:45 +03:00
}