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

Function TestEncryptDecryptField

enterprise/dbcrypt/dbcrypt_internal_test.go:741–825  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

739}
740
741func TestEncryptDecryptField(t *testing.T) {
742 t.Parallel()
743 t.Run("OK", func(t *testing.T) {
744 t.Parallel()
745 _, cryptDB, ciphers := setup(t)
746 field := "coder"
747 digest := sql.NullString{}
748 require.NoError(t, cryptDB.encryptField(&field, &digest))
749 require.Equal(t, ciphers[0].HexDigest(), digest.String)
750 requireEncryptedEquals(t, ciphers[0], field, "coder")
751 require.NoError(t, cryptDB.decryptField(&field, digest))
752 require.Equal(t, "coder", field)
753 })
754
755 t.Run("NoKeys", func(t *testing.T) {
756 t.Parallel()
757 // With no keys, encryption and decryption are both no-ops.
758 _, cryptDB := setupNoCiphers(t)
759 field := "coder"
760 digest := sql.NullString{}
761 require.NoError(t, cryptDB.encryptField(&field, &digest))
762 require.Empty(t, digest.String)
763 require.Equal(t, "coder", field)
764 require.NoError(t, cryptDB.decryptField(&field, digest))
765 require.Equal(t, "coder", field)
766 })
767
768 t.Run("MissingKey", func(t *testing.T) {
769 t.Parallel()
770 _, cryptDB, ciphers := setup(t)
771 field := "coder"
772 digest := sql.NullString{}
773 err := cryptDB.encryptField(&field, &digest)
774 require.NoError(t, err)
775 requireEncryptedEquals(t, ciphers[0], field, "coder")
776 require.Equal(t, ciphers[0].HexDigest(), digest.String)
777 require.True(t, digest.Valid)
778
779 digest = sql.NullString{String: "missing", Valid: true}
780 var derr *DecryptFailedError
781 err = cryptDB.decryptField(&field, digest)
782 require.Error(t, err)
783 require.ErrorAs(t, err, &derr)
784 })
785
786 t.Run("CantEncryptOrDecryptNil", func(t *testing.T) {
787 t.Parallel()
788 _, cryptDB, _ := setup(t)
789 require.ErrorContains(t, cryptDB.encryptField(nil, nil), "developer error")
790 require.ErrorContains(t, cryptDB.decryptField(nil, sql.NullString{}), "developer error")
791 })
792
793 t.Run("EncryptEmptyString", func(t *testing.T) {
794 t.Parallel()
795 _, cryptDB, ciphers := setup(t)
796 field := ""
797 digest := sql.NullString{}
798 require.NoError(t, cryptDB.encryptField(&field, &digest))

Callers

nothing calls this directly

Calls 10

setupNoCiphersFunction · 0.85
encryptFieldMethod · 0.80
decryptFieldMethod · 0.80
setupFunction · 0.70
requireEncryptedEqualsFunction · 0.70
RunMethod · 0.65
HexDigestMethod · 0.65
EqualMethod · 0.45
EmptyMethod · 0.45
ErrorMethod · 0.45

Tested by

no test coverage detected