@@ -648,15 +648,15 @@ func TestSerialize(t *testing.T) {
648
648
case test .pass == "" && test .file == "" :
649
649
p , err = Serialize (in )
650
650
case test .pass != "" && test .file != "" :
651
- p , err = Serialize (in , WithPassword ([]byte (test .pass )), ToFile (test .file , 0600 ))
651
+ p , err = Serialize (in , WithPassword ([]byte (test .pass )), ToFile (test .file , 0o600 ))
652
652
case test .pass != "" && test .pkcs8 :
653
653
p , err = Serialize (in , WithPKCS8 (true ), WithPasswordPrompt ("Please enter the password to encrypt the key" , func (prompt string ) ([]byte , error ) {
654
654
return []byte (test .pass ), nil
655
655
}))
656
656
case test .pass != "" :
657
657
p , err = Serialize (in , WithPassword ([]byte (test .pass )))
658
658
default :
659
- p , err = Serialize (in , ToFile (test .file , 0600 ))
659
+ p , err = Serialize (in , ToFile (test .file , 0o600 ))
660
660
}
661
661
662
662
if err != nil {
@@ -722,7 +722,7 @@ func TestSerialize(t *testing.T) {
722
722
var fileInfo os.FileInfo
723
723
fileInfo , err = os .Stat (test .file )
724
724
require .NoError (t , err )
725
- assert .Equal (t , fileInfo .Mode (), os .FileMode (0600 ))
725
+ assert .Equal (t , fileInfo .Mode (), os .FileMode (0o600 ))
726
726
// Verify that key written to file is correct
727
727
var keyFileBytes []byte
728
728
keyFileBytes , err = os .ReadFile (test .file )
@@ -1024,6 +1024,7 @@ func TestRead_options(t *testing.T) {
1024
1024
{"withPasswordPromptError" , args {"testdata/openssl.p256.enc.pem" , []Options {WithPasswordPrompt ("Enter the password" , func (s string ) ([]byte , error ) {
1025
1025
return nil , errors .New ("an error" )
1026
1026
})}}, nil , true },
1027
+ {"withPasswordFile" , args {"testdata/openssl.p256.enc.pem" , []Options {WithPasswordFile ("testdata/password.txt" )}}, p256Key , false },
1027
1028
}
1028
1029
for _ , tt := range tests {
1029
1030
t .Run (tt .name , func (t * testing.T ) {
@@ -1039,6 +1040,80 @@ func TestRead_options(t *testing.T) {
1039
1040
}
1040
1041
}
1041
1042
1043
+ func TestWithMinLengthPasswordFile (t * testing.T ) {
1044
+ tests := []struct {
1045
+ name string
1046
+ length int
1047
+ file string
1048
+ want []byte
1049
+ wantErr bool
1050
+ }{
1051
+ {
1052
+ name : "negative" ,
1053
+ length : - 5 ,
1054
+ file : "testdata/password.txt" ,
1055
+ wantErr : false ,
1056
+ want : []byte ("mypassword" ),
1057
+ },
1058
+ {
1059
+ name : "zero" ,
1060
+ length : 0 ,
1061
+ file : "testdata/password.txt" ,
1062
+ wantErr : false ,
1063
+ want : []byte ("mypassword" ),
1064
+ },
1065
+ {
1066
+ name : "greater-than-min-length" ,
1067
+ length : 9 ,
1068
+ file : "testdata/password.txt" ,
1069
+ wantErr : false ,
1070
+ want : []byte ("mypassword" ),
1071
+ },
1072
+ {
1073
+ name : "equal-min-length" ,
1074
+ length : 10 ,
1075
+ file : "testdata/password.txt" ,
1076
+ wantErr : false ,
1077
+ want : []byte ("mypassword" ),
1078
+ },
1079
+ {
1080
+ name : "less-than-min-length" ,
1081
+ length : 11 ,
1082
+ file : "testdata/password.txt" ,
1083
+ wantErr : true ,
1084
+ },
1085
+ {
1086
+ name : "ignore-pre-post-whitespace-characters" ,
1087
+ length : 7 ,
1088
+ file : "testdata/password2.txt" ,
1089
+ wantErr : true ,
1090
+ },
1091
+ {
1092
+ name : "ignore-pre-post-whitespace-characters-ok" ,
1093
+ length : 6 ,
1094
+ file : "testdata/password2.txt" ,
1095
+ wantErr : false ,
1096
+ want : []byte (" pass" ),
1097
+ },
1098
+ }
1099
+ for _ , tt := range tests {
1100
+ t .Run (tt .name , func (t * testing.T ) {
1101
+ ctx := newContext (tt .name )
1102
+ gotErr := WithMinLengthPasswordFile (tt .file , tt .length )(ctx ) != nil
1103
+ if gotErr != tt .wantErr {
1104
+ t .Errorf ("WithMinLengthPasswordFile(%v, %v) = %v, want %v" , tt .file , tt .length , gotErr , tt .wantErr )
1105
+ return
1106
+ }
1107
+ if gotErr {
1108
+ return
1109
+ }
1110
+ if ! bytes .Equal (ctx .password , tt .want ) {
1111
+ t .Errorf ("Expected %v, but got %v" , tt .want , ctx .password )
1112
+ }
1113
+ })
1114
+ }
1115
+ }
1116
+
1042
1117
func TestRead_promptPassword (t * testing.T ) {
1043
1118
mustKey := func (filename string ) interface {} {
1044
1119
b , err := os .ReadFile (filename )
0 commit comments