|
| 1 | +package vault |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/hex" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/stretchr/testify/require" |
| 8 | + |
| 9 | + vaultcommon "github.com/smartcontractkit/chainlink-common/pkg/capabilities/actions/vault" |
| 10 | + pkgconfig "github.com/smartcontractkit/chainlink-common/pkg/config" |
| 11 | + "github.com/smartcontractkit/chainlink-common/pkg/settings/limits" |
| 12 | +) |
| 13 | + |
| 14 | +func TestRequestValidator_CiphertextSizeLimit(t *testing.T) { |
| 15 | + validator := NewRequestValidator( |
| 16 | + limits.NewUpperBoundLimiter(10), |
| 17 | + limits.NewUpperBoundLimiter[pkgconfig.Size](10*pkgconfig.Byte), |
| 18 | + ) |
| 19 | + |
| 20 | + id := &vaultcommon.SecretIdentifier{ |
| 21 | + Key: "key", |
| 22 | + Namespace: "namespace", |
| 23 | + Owner: "0x1111111111111111111111111111111111111111", |
| 24 | + } |
| 25 | + |
| 26 | + tests := []struct { |
| 27 | + name string |
| 28 | + call func(*testing.T, *RequestValidator, string) error |
| 29 | + value string |
| 30 | + errSubstr string |
| 31 | + }{ |
| 32 | + { |
| 33 | + name: "create accepts ciphertext at the limit", |
| 34 | + call: func(t *testing.T, validator *RequestValidator, value string) error { |
| 35 | + return validator.ValidateCreateSecretsRequest(nil, &vaultcommon.CreateSecretsRequest{ |
| 36 | + RequestId: "request-id", |
| 37 | + EncryptedSecrets: []*vaultcommon.EncryptedSecret{ |
| 38 | + {Id: id, EncryptedValue: value}, |
| 39 | + }, |
| 40 | + }) |
| 41 | + }, |
| 42 | + value: hex.EncodeToString(make([]byte, 10)), |
| 43 | + }, |
| 44 | + { |
| 45 | + name: "create rejects ciphertext above the limit", |
| 46 | + call: func(t *testing.T, validator *RequestValidator, value string) error { |
| 47 | + return validator.ValidateCreateSecretsRequest(nil, &vaultcommon.CreateSecretsRequest{ |
| 48 | + RequestId: "request-id", |
| 49 | + EncryptedSecrets: []*vaultcommon.EncryptedSecret{ |
| 50 | + {Id: id, EncryptedValue: value}, |
| 51 | + }, |
| 52 | + }) |
| 53 | + }, |
| 54 | + value: hex.EncodeToString(make([]byte, 11)), |
| 55 | + errSubstr: "ciphertext size exceeds maximum allowed size", |
| 56 | + }, |
| 57 | + { |
| 58 | + name: "update accepts ciphertext at the limit", |
| 59 | + call: func(t *testing.T, validator *RequestValidator, value string) error { |
| 60 | + return validator.ValidateUpdateSecretsRequest(nil, &vaultcommon.UpdateSecretsRequest{ |
| 61 | + RequestId: "request-id", |
| 62 | + EncryptedSecrets: []*vaultcommon.EncryptedSecret{ |
| 63 | + {Id: id, EncryptedValue: value}, |
| 64 | + }, |
| 65 | + }) |
| 66 | + }, |
| 67 | + value: hex.EncodeToString(make([]byte, 10)), |
| 68 | + }, |
| 69 | + { |
| 70 | + name: "update rejects ciphertext above the limit", |
| 71 | + call: func(t *testing.T, validator *RequestValidator, value string) error { |
| 72 | + return validator.ValidateUpdateSecretsRequest(nil, &vaultcommon.UpdateSecretsRequest{ |
| 73 | + RequestId: "request-id", |
| 74 | + EncryptedSecrets: []*vaultcommon.EncryptedSecret{ |
| 75 | + {Id: id, EncryptedValue: value}, |
| 76 | + }, |
| 77 | + }) |
| 78 | + }, |
| 79 | + value: hex.EncodeToString(make([]byte, 11)), |
| 80 | + errSubstr: "ciphertext size exceeds maximum allowed size", |
| 81 | + }, |
| 82 | + } |
| 83 | + |
| 84 | + for _, tt := range tests { |
| 85 | + t.Run(tt.name, func(t *testing.T) { |
| 86 | + err := tt.call(t, validator, tt.value) |
| 87 | + if tt.errSubstr == "" { |
| 88 | + require.NoError(t, err) |
| 89 | + return |
| 90 | + } |
| 91 | + |
| 92 | + require.Error(t, err) |
| 93 | + require.ErrorContains(t, err, tt.errSubstr) |
| 94 | + }) |
| 95 | + } |
| 96 | +} |
0 commit comments