MCPcopy Index your code
hub / github.com/coder/coder / TestCipherAES256

Function TestCipherAES256

enterprise/dbcrypt/cipher_internal_test.go:13–67  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

11)
12
13func TestCipherAES256(t *testing.T) {
14 t.Parallel()
15
16 t.Run("ValidInput", func(t *testing.T) {
17 t.Parallel()
18 key := bytes.Repeat([]byte{'a'}, 32)
19 cipher, err := cipherAES256(key)
20 require.NoError(t, err)
21
22 output, err := cipher.Encrypt([]byte("hello world"))
23 require.NoError(t, err)
24
25 response, err := cipher.Decrypt(output)
26 require.NoError(t, err)
27 require.Equal(t, "hello world", string(response))
28 })
29
30 t.Run("InvalidInput", func(t *testing.T) {
31 t.Parallel()
32 key := bytes.Repeat([]byte{'a'}, 32)
33 cipher, err := cipherAES256(key)
34 require.NoError(t, err)
35 _, err = cipher.Decrypt(bytes.Repeat([]byte{'a'}, 100))
36 var decryptErr *DecryptFailedError
37 require.ErrorAs(t, err, &decryptErr)
38 })
39
40 t.Run("InvalidKeySize", func(t *testing.T) {
41 t.Parallel()
42
43 _, err := cipherAES256(bytes.Repeat([]byte{'a'}, 31))
44 require.ErrorContains(t, err, "key must be 32 bytes")
45 })
46
47 t.Run("TestNonce", func(t *testing.T) {
48 t.Parallel()
49 key := bytes.Repeat([]byte{'a'}, 32)
50 cipher, err := cipherAES256(key)
51 require.NoError(t, err)
52 require.Equal(t, "864f702", cipher.HexDigest())
53
54 encrypted1, err := cipher.Encrypt([]byte("hello world"))
55 require.NoError(t, err)
56 encrypted2, err := cipher.Encrypt([]byte("hello world"))
57 require.NoError(t, err)
58 require.NotEqual(t, encrypted1, encrypted2, "nonce should be different for each encryption")
59
60 munged := make([]byte, len(encrypted1))
61 copy(munged, encrypted1)
62 munged[0] ^= 0xff
63 _, err = cipher.Decrypt(munged)
64 var decryptErr *DecryptFailedError
65 require.ErrorAs(t, err, &decryptErr, "munging the first byte of the encrypted data should cause decryption to fail")
66 })
67}
68
69// This test ensures backwards compatibility. If it breaks, something is very wrong.
70func TestCiphersBackwardCompatibility(t *testing.T) {

Callers

nothing calls this directly

Calls 6

cipherAES256Function · 0.85
RunMethod · 0.65
EncryptMethod · 0.65
DecryptMethod · 0.65
HexDigestMethod · 0.65
EqualMethod · 0.45

Tested by

no test coverage detected