@@ -114,6 +114,112 @@ func rsaEqual(priv *rsa.PrivateKey, x crypto.PrivateKey) bool {
114114 return true
115115}
116116
117+ func TestEncrypt (t * testing.T ) {
118+ jwk := fixJWK (mustGenerateJWK (t , "EC" , "P-256" , "ES256" , "" , "" , 0 ))
119+ data , err := json .Marshal (jwk )
120+ if err != nil {
121+ t .Fatal (err )
122+ }
123+
124+ type args struct {
125+ data []byte
126+ opts []Option
127+ }
128+ tests := []struct {
129+ name string
130+ args args
131+ wantFn func (t * testing.T ) * JSONWebEncryption
132+ wantErr bool
133+ }{
134+ {"ok" , args {data , []Option {WithPassword ([]byte ("password" )), WithContentType ("jwk+json" )}},
135+ func (t * testing.T ) * JSONWebEncryption {
136+ reader := mustTeeReader (t )
137+ jwe := mustEncryptJWK (t , jwk , []byte ("password" ))
138+ rand .Reader = reader
139+ jose .RandReader = reader
140+ return jwe
141+ }, false },
142+ {"ok WithPasswordPrompter" , args {data , []Option {
143+ WithContentType ("jwk+json" ),
144+ WithPasswordPrompter ("Enter the password" , func (s string ) ([]byte , error ) {
145+ return []byte ("password" ), nil
146+ })}},
147+ func (t * testing.T ) * JSONWebEncryption {
148+ reader := mustTeeReader (t )
149+ jwe := mustEncryptJWK (t , jwk , []byte ("password" ))
150+ rand .Reader = reader
151+ jose .RandReader = reader
152+ return jwe
153+ }, false },
154+ {"ok with PromptPassword" , args {data , []Option {WithContentType ("jwk+json" )}},
155+ func (t * testing.T ) * JSONWebEncryption {
156+ tmp := PromptPassword
157+ t .Cleanup (func () { PromptPassword = tmp })
158+ PromptPassword = func (s string ) ([]byte , error ) {
159+ return []byte ("password" ), nil
160+ }
161+ reader := mustTeeReader (t )
162+ jwe := mustEncryptJWK (t , jwk , []byte ("password" ))
163+ rand .Reader = reader
164+ jose .RandReader = reader
165+ return jwe
166+ }, false },
167+ {"fail apply" , args {data , []Option {WithPasswordFile ("testdata/missing.txt" )}},
168+ func (t * testing.T ) * JSONWebEncryption {
169+ return nil
170+ }, true },
171+ {"fail WithPasswordPrompter" , args {data , []Option {
172+ WithContentType ("jwk+json" ),
173+ WithPasswordPrompter ("Enter the password" , func (s string ) ([]byte , error ) {
174+ return nil , errors .New ("test error" )
175+ })}},
176+ func (t * testing.T ) * JSONWebEncryption {
177+ return nil
178+ }, true },
179+ {"fail with PromptPassword" , args {data , []Option {WithContentType ("jwk+json" )}},
180+ func (t * testing.T ) * JSONWebEncryption {
181+ tmp := PromptPassword
182+ t .Cleanup (func () { PromptPassword = tmp })
183+ PromptPassword = func (s string ) ([]byte , error ) {
184+ return nil , errors .New ("test error" )
185+ }
186+ return nil
187+ }, true },
188+ {"fail no passowrd" , args {data , nil },
189+ func (t * testing.T ) * JSONWebEncryption {
190+ return nil
191+ }, true },
192+ {"fail encrypt" , args {data , []Option {WithPassword ([]byte ("password" ))}},
193+ func (t * testing.T ) * JSONWebEncryption {
194+ reader := mustTeeReader (t )
195+ _ , _ = randutil .Salt (PBKDF2SaltSize )
196+ rand .Reader = reader
197+ jose .RandReader = reader
198+ return nil
199+ }, true },
200+ {"fail salt" , args {data , []Option {WithPassword ([]byte ("password" ))}},
201+ func (t * testing.T ) * JSONWebEncryption {
202+ reader := mustTeeReader (t )
203+ rand .Reader = reader
204+ jose .RandReader = reader
205+ return nil
206+ }, true },
207+ }
208+ for _ , tt := range tests {
209+ t .Run (tt .name , func (t * testing.T ) {
210+ want := tt .wantFn (t )
211+ got , err := Encrypt (tt .args .data , tt .args .opts ... )
212+ if (err != nil ) != tt .wantErr {
213+ t .Errorf ("Encrypt() error = %v, wantErr %v" , err , tt .wantErr )
214+ return
215+ }
216+ if ! reflect .DeepEqual (got , want ) {
217+ t .Errorf ("Encrypt() = %v, want %v" , got , want )
218+ }
219+ })
220+ }
221+ }
222+
117223func TestEncryptJWK (t * testing.T ) {
118224 jwk := fixJWK (mustGenerateJWK (t , "EC" , "P-256" , "ES256" , "" , "" , 0 ))
119225
@@ -266,35 +372,47 @@ func TestDecrypt(t *testing.T) {
266372 want []byte
267373 wantErr bool
268374 }{
269- {"okNotEncrypted " , args {[]byte ("foobar" ), nil , nil }, []byte ("foobar" ), false },
270- {"okWithPassword " , args {encryptedData , []Option {WithPassword (testPassword )}, nil }, data , false },
271- {"okWithPasswordFile " , args {encryptedData , []Option {WithPasswordFile ("testdata/passphrase.txt" )}, nil }, data , false },
272- {"okWithPasswordPrompter " , args {encryptedData , []Option {WithPasswordPrompter ("What's the password?" , func (s string ) ([]byte , error ) {
375+ {"ok not encrypted " , args {[]byte ("foobar" ), nil , nil }, []byte ("foobar" ), false },
376+ {"ok WithPassword " , args {encryptedData , []Option {WithPassword (testPassword )}, nil }, data , false },
377+ {"ok WithPasswordFile " , args {encryptedData , []Option {WithPasswordFile ("testdata/passphrase.txt" )}, nil }, data , false },
378+ {"ok WithPasswordPrompter " , args {encryptedData , []Option {WithPasswordPrompter ("What's the password?" , func (s string ) ([]byte , error ) {
273379 return testPassword , nil
274380 })}, nil }, data , false },
275- {"okGlobalPasswordPrompter" , args {encryptedData , []Option {}, func (s string ) ([]byte , error ) {
381+ {"ok PasswordPrompter" , args {encryptedData , []Option {}, func (s string ) ([]byte , error ) {
382+ return testPassword , nil
383+ }}, data , false },
384+ {"ok WithFilename and PasswordPrompter" , args {encryptedData , []Option {WithFilename ("test.jwk" )}, func (s string ) ([]byte , error ) {
276385 return testPassword , nil
277386 }}, data , false },
278- {"failBadData " , args {badEncryptedData , []Option {WithPassword (testPassword )}, nil }, nil , true },
279- {"failWithPassword " , args {encryptedData , []Option {WithPassword ([]byte ("bad-password" ))}, nil }, nil , true },
280- {"failWithPasswordFile " , args {encryptedData , []Option {WithPasswordFile ("testdata/oct.txt" )}, nil }, nil , true },
281- {"failWithPasswordPrompter " , args {encryptedData , []Option {WithPasswordPrompter ("What's the password?" , func (s string ) ([]byte , error ) {
387+ {"fail bad data " , args {badEncryptedData , []Option {WithPassword (testPassword )}, nil }, nil , true },
388+ {"fail WithPassword " , args {encryptedData , []Option {WithPassword ([]byte ("bad-password" ))}, nil }, nil , true },
389+ {"fail WithPasswordFile " , args {encryptedData , []Option {WithPasswordFile ("testdata/oct.txt" )}, nil }, nil , true },
390+ {"fail WithPasswordPrompter " , args {encryptedData , []Option {WithPasswordPrompter ("What's the password?" , func (s string ) ([]byte , error ) {
282391 return []byte ("bad-password" ), nil
283392 })}, nil }, nil , true },
284- {"failGlobalPasswordPrompter " , args {encryptedData , []Option {}, func (s string ) ([]byte , error ) {
393+ {"fail PasswordPrompter " , args {encryptedData , []Option {}, func (s string ) ([]byte , error ) {
285394 return []byte ("bad-password" ), nil
286395 }}, nil , true },
287- {"failApplyWithPassword " , args {encryptedData , []Option {WithPasswordFile ("testdata/missing.txt" )}, nil }, nil , true },
288- {"failApplyWithPasswordPrompter " , args {encryptedData , []Option {WithPasswordPrompter ("What's the password?" , func (s string ) ([]byte , error ) {
396+ {"fail apply WithPassword " , args {encryptedData , []Option {WithPasswordFile ("testdata/missing.txt" )}, nil }, nil , true },
397+ {"fail apply WithPasswordPrompter " , args {encryptedData , []Option {WithPasswordPrompter ("What's the password?" , func (s string ) ([]byte , error ) {
289398 return nil , errors .New ("unexpected error" )
290399 })}, nil }, nil , true },
291- {"failGlobalPasswordPrompterError" , args {encryptedData , []Option {}, func (s string ) ([]byte , error ) {
400+ {"fail PasswordPrompter" , args {encryptedData , []Option {}, func (s string ) ([]byte , error ) {
401+ return nil , errors .New ("unexpected error" )
402+ }}, nil , true },
403+ {"fail WithFilename and PasswordPrompter" , args {encryptedData , []Option {WithFilename ("test.jwk" )}, func (s string ) ([]byte , error ) {
292404 return nil , errors .New ("unexpected error" )
293405 }}, nil , true },
294406 }
295407 for _ , tt := range tests {
296408 t .Run (tt .name , func (t * testing.T ) {
409+ if tt .name == "okGlobalPasswordPrompter" {
410+ t .Log ("foo" )
411+ }
412+ tmp := PromptPassword
413+ t .Cleanup (func () { PromptPassword = tmp })
297414 PromptPassword = tt .args .passwordPrompter
415+
298416 got , err := Decrypt (tt .args .data , tt .args .opts ... )
299417 if (err != nil ) != tt .wantErr {
300418 t .Errorf ("Decrypt() error = %v, wantErr %v" , err , tt .wantErr )
0 commit comments