(t *testing.T)
| 23 | var goodSecret = []string{"secret-thirty-two-character-long"} |
| 24 | |
| 25 | func 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) |
nothing calls this directly
no test coverage detected