@@ -648,15 +648,15 @@ func TestSerialize(t *testing.T) {
648648 case test .pass == "" && test .file == "" :
649649 p , err = Serialize (in )
650650 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 ))
652652 case test .pass != "" && test .pkcs8 :
653653 p , err = Serialize (in , WithPKCS8 (true ), WithPasswordPrompt ("Please enter the password to encrypt the key" , func (prompt string ) ([]byte , error ) {
654654 return []byte (test .pass ), nil
655655 }))
656656 case test .pass != "" :
657657 p , err = Serialize (in , WithPassword ([]byte (test .pass )))
658658 default :
659- p , err = Serialize (in , ToFile (test .file , 0600 ))
659+ p , err = Serialize (in , ToFile (test .file , 0o600 ))
660660 }
661661
662662 if err != nil {
@@ -722,7 +722,7 @@ func TestSerialize(t *testing.T) {
722722 var fileInfo os.FileInfo
723723 fileInfo , err = os .Stat (test .file )
724724 require .NoError (t , err )
725- assert .Equal (t , fileInfo .Mode (), os .FileMode (0600 ))
725+ assert .Equal (t , fileInfo .Mode (), os .FileMode (0o600 ))
726726 // Verify that key written to file is correct
727727 var keyFileBytes []byte
728728 keyFileBytes , err = os .ReadFile (test .file )
@@ -1024,6 +1024,7 @@ func TestRead_options(t *testing.T) {
10241024 {"withPasswordPromptError" , args {"testdata/openssl.p256.enc.pem" , []Options {WithPasswordPrompt ("Enter the password" , func (s string ) ([]byte , error ) {
10251025 return nil , errors .New ("an error" )
10261026 })}}, nil , true },
1027+ {"withPasswordFile" , args {"testdata/openssl.p256.enc.pem" , []Options {WithPasswordFile ("testdata/password.txt" )}}, p256Key , false },
10271028 }
10281029 for _ , tt := range tests {
10291030 t .Run (tt .name , func (t * testing.T ) {
@@ -1039,6 +1040,80 @@ func TestRead_options(t *testing.T) {
10391040 }
10401041}
10411042
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+
10421117func TestRead_promptPassword (t * testing.T ) {
10431118 mustKey := func (filename string ) interface {} {
10441119 b , err := os .ReadFile (filename )
0 commit comments