@@ -1026,10 +1026,91 @@ impl<T> fmt::Debug for EcKey<T> {
1026
1026
#[ cfg( test) ]
1027
1027
mod test {
1028
1028
use hex:: FromHex ;
1029
+ use std:: str:: from_utf8;
1029
1030
1030
1031
use super :: * ;
1031
1032
use crate :: bn:: { BigNum , BigNumContext } ;
1032
1033
use crate :: nid:: Nid ;
1034
+ use crate :: symm:: Cipher ;
1035
+
1036
+ #[ test]
1037
+ fn test_private_key_from_pem ( ) {
1038
+ EcKey :: private_key_from_pem ( include_bytes ! ( "../test/ec.pem" ) ) . unwrap ( ) ;
1039
+ }
1040
+
1041
+ #[ test]
1042
+ fn test_private_key_from_pem_trad ( ) {
1043
+ EcKey :: private_key_from_pem ( include_bytes ! ( "../test/ec.trad.pem" ) ) . unwrap ( ) ;
1044
+ }
1045
+
1046
+ #[ test]
1047
+ fn test_private_key_from_pem_password ( ) {
1048
+ let key = include_bytes ! ( "../test/ec-encrypted.pem" ) ;
1049
+ EcKey :: private_key_from_pem_passphrase ( key, b"mypass" ) . unwrap ( ) ;
1050
+ }
1051
+
1052
+ #[ test]
1053
+ fn test_private_key_from_pem_callback ( ) {
1054
+ let mut password_queried = false ;
1055
+ let key = include_bytes ! ( "../test/ec-encrypted.pem" ) ;
1056
+ EcKey :: private_key_from_pem_callback ( key, |password| {
1057
+ password_queried = true ;
1058
+ password[ ..6 ] . copy_from_slice ( b"mypass" ) ;
1059
+ Ok ( 6 )
1060
+ } )
1061
+ . unwrap ( ) ;
1062
+
1063
+ assert ! ( password_queried) ;
1064
+ }
1065
+
1066
+ #[ test]
1067
+ fn test_private_key_from_der ( ) {
1068
+ EcKey :: private_key_from_der ( include_bytes ! ( "../test/ec.der" ) ) . unwrap ( ) ;
1069
+ }
1070
+
1071
+ #[ test]
1072
+ fn test_private_key_to_pem ( ) {
1073
+ let key = EcKey :: private_key_from_pem ( include_bytes ! ( "../test/ec.pem" ) ) . unwrap ( ) ;
1074
+ let pem = key. private_key_to_pem ( ) . unwrap ( ) ;
1075
+ assert_eq ! (
1076
+ from_utf8( & pem) . unwrap( ) ,
1077
+ include_str!( "../test/ec.trad.pem" ) . replace( "\r \n " , "\n " )
1078
+ ) ;
1079
+ }
1080
+
1081
+ #[ test]
1082
+ fn test_private_key_to_pem_password ( ) {
1083
+ let key = EcKey :: private_key_from_pem ( include_bytes ! ( "../test/ec.pem" ) ) . unwrap ( ) ;
1084
+ let pem = key
1085
+ . private_key_to_pem_passphrase ( Cipher :: aes_128_cbc ( ) , b"foobar" )
1086
+ . unwrap ( ) ;
1087
+ EcKey :: private_key_from_pem_passphrase ( & pem, b"foobar" ) . unwrap ( ) ;
1088
+ assert ! ( EcKey :: private_key_from_pem_passphrase( & pem, b"fizzbuzz" ) . is_err( ) ) ;
1089
+ }
1090
+
1091
+ #[ test]
1092
+ fn test_private_key_to_der ( ) {
1093
+ let key = EcKey :: private_key_from_pem ( include_bytes ! ( "../test/ec.pem" ) ) . unwrap ( ) ;
1094
+ let der = key. private_key_to_der ( ) . unwrap ( ) ;
1095
+ assert_eq ! ( der, include_bytes!( "../test/ec.der" ) ) ;
1096
+ }
1097
+
1098
+ #[ test]
1099
+ fn test_public_key_to_pem ( ) {
1100
+ let keypair = EcKey :: private_key_from_pem ( include_bytes ! ( "../test/ec.pem" ) ) . unwrap ( ) ;
1101
+ let pubkey_pem = keypair. public_key_to_pem ( ) . unwrap ( ) ;
1102
+ assert_eq ! (
1103
+ from_utf8( & pubkey_pem) . unwrap( ) ,
1104
+ include_str!( "../test/ec.pub.pem" ) . replace( "\r \n " , "\n " )
1105
+ ) ;
1106
+ }
1107
+
1108
+ #[ test]
1109
+ fn test_public_key_to_der ( ) {
1110
+ let keypair = EcKey :: private_key_from_pem ( include_bytes ! ( "../test/ec.pem" ) ) . unwrap ( ) ;
1111
+ let pubkey_der = keypair. public_key_to_der ( ) . unwrap ( ) ;
1112
+ assert_eq ! ( pubkey_der, include_bytes!( "../test/ec.pub.der" ) ) ;
1113
+ }
1033
1114
1034
1115
#[ test]
1035
1116
fn key_new_by_curve_name ( ) {
0 commit comments