2023-02-21 14:43:43 +03:00
use std ::time ;
use sequoia_openpgp as openpgp ;
use openpgp ::Result ;
2024-08-14 11:30:24 +03:00
2024-08-15 14:38:43 +03:00
use super ::common ;
2024-11-13 11:20:18 +03:00
use super ::common ::UserIDArg ;
use super ::common ::NO_USERIDS ;
2023-02-21 14:43:43 +03:00
2024-08-15 15:05:24 +03:00
#[ test ]
fn sq_key_generate_creation_time ( ) -> Result < ( ) >
{
let sq = common ::Sq ::new ( ) ;
// $ date +'%Y%m%dT%H%M%S%z'; date +'%s'
let iso8601 = " 20220120T163236+0100 " ;
let t = 1642692756 ;
let ( result , _ , _ ) = sq . key_generate ( & [
" --time " , iso8601 ,
" --expiration " , " never " ,
2024-11-13 11:20:18 +03:00
] , NO_USERIDS ) ;
2024-08-15 15:05:24 +03:00
let vc = result . with_policy ( common ::STANDARD_POLICY , None ) ? ;
assert_eq! ( vc . primary_key ( ) . creation_time ( ) ,
time ::UNIX_EPOCH + time ::Duration ::new ( t , 0 ) ) ;
assert! ( vc . primary_key ( ) . key_expiration_time ( ) . is_none ( ) ) ;
Ok ( ( ) )
}
#[ test ]
fn sq_key_generate_name_email ( ) -> Result < ( ) > {
let sq = common ::Sq ::new ( ) ;
2024-11-13 11:20:18 +03:00
let ( cert , _ , _ ) = sq . key_generate (
& [ ] ,
& [
UserIDArg ::Name ( " Joan Clarke " ) ,
UserIDArg ::Name ( " Joan Clarke Murray " ) ,
UserIDArg ::Email ( " joan@hut8.bletchley.park " ) ,
] ) ;
2024-08-15 15:05:24 +03:00
assert_eq! ( cert . userids ( ) . count ( ) , 3 ) ;
assert! ( cert . userids ( ) . any ( | u | u . value ( ) = = b " Joan Clarke " ) ) ;
assert! ( cert . userids ( ) . any ( | u | u . value ( ) = = b " Joan Clarke Murray " ) ) ;
assert! (
cert . userids ( ) . any ( | u | u . value ( ) = = b " <joan@hut8.bletchley.park> " ) ) ;
Ok ( ( ) )
2023-02-21 14:43:43 +03:00
}
2024-09-02 11:03:50 +03:00
#[ test ]
fn sq_key_generate_with_password ( ) -> Result < ( ) > {
let sq = common ::Sq ::new ( ) ;
let password = " hunter2 " ;
let path = sq . base ( ) . join ( " password " ) ;
std ::fs ::write ( & path , password ) ? ;
let ( cert , _ , _ ) = sq . key_generate ( & [
" --new-password-file " , & path . display ( ) . to_string ( ) ,
2024-11-13 11:20:18 +03:00
] , NO_USERIDS ) ;
2024-09-02 11:03:50 +03:00
assert! ( cert . is_tsk ( ) ) ;
let password = password . into ( ) ;
for key in cert . keys ( ) {
let secret = key . optional_secret ( ) . unwrap ( ) ;
assert! ( secret . is_encrypted ( ) ) ;
assert! ( secret . clone ( ) . decrypt ( key . pk_algo ( ) , & password ) . is_ok ( ) ) ;
}
Ok ( ( ) )
}