MCPcopy
hub / github.com/ory/kratos / TestCipher

Function TestCipher

cipher/cipher_test.go:25–84  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

23var goodSecret = []string{"secret-thirty-two-character-long"}
24
25func TestCipher(t *testing.T) {
26 ctx := context.Background()
27 _, reg := pkg.NewFastRegistryWithMocks(t, configx.WithValue(config.ViperKeySecretsDefault, goodSecret))
28
29 ciphers := []cipher.Cipher{
30 cipher.NewCryptAES(reg.Config()),
31 cipher.NewCryptChaCha20(reg.Config()),
32 }
33
34 for _, c := range ciphers {
35 t.Run(fmt.Sprintf("cipher=%T", c), func(t *testing.T) {
36 t.Parallel()
37
38 t.Run("case=all_work", func(t *testing.T) {
39 t.Parallel()
40
41 testAllWork(ctx, t, c)
42 })
43
44 t.Run("case=encryption_failed", func(t *testing.T) {
45 t.Parallel()
46
47 ctx := contextx.WithConfigValue(ctx, config.ViperKeySecretsCipher, []string{""})
48
49 // secret have to be set
50 _, err := c.Encrypt(ctx, []byte("not-empty"))
51 require.Error(t, err)
52 var hErr *herodot.DefaultError
53 require.ErrorAs(t, err, &hErr)
54 assert.Equal(t, "Unable to encrypt message because no cipher secrets were configured.", hErr.Reason())
55
56 ctx = contextx.WithConfigValue(ctx, config.ViperKeySecretsCipher, []string{"bad-length"})
57
58 // bad secret length
59 _, err = c.Encrypt(ctx, []byte("not-empty"))
60 require.ErrorAs(t, err, &hErr)
61 assert.Equal(t, "Unable to encrypt message because no cipher secrets were configured.", hErr.Reason())
62 })
63
64 t.Run("case=decryption_failed", func(t *testing.T) {
65 t.Parallel()
66
67 _, err := c.Decrypt(ctx, hex.EncodeToString([]byte("bad-data")))
68 require.Error(t, err)
69
70 _, err = c.Decrypt(ctx, "not-empty")
71 require.Error(t, err)
72
73 _, err = c.Decrypt(contextx.WithConfigValue(ctx, config.ViperKeySecretsCipher, []string{""}), "not-empty")
74 require.Error(t, err)
75 })
76 })
77 }
78
79 c := cipher.NewNoop()
80 t.Run(fmt.Sprintf("cipher=%T", c), func(t *testing.T) {
81 t.Parallel()
82 testAllWork(ctx, t, c)

Callers

nothing calls this directly

Calls 11

EncryptMethod · 0.95
DecryptMethod · 0.95
NewFastRegistryWithMocksFunction · 0.92
NewCryptAESFunction · 0.92
NewCryptChaCha20Function · 0.92
NewNoopFunction · 0.92
testAllWorkFunction · 0.85
ConfigMethod · 0.65
RunMethod · 0.65
ReasonMethod · 0.65
ErrorMethod · 0.45

Tested by

no test coverage detected