You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

secret_test.go 933B

12345678910111213141516171819202122232425262728293031
  1. // Copyright 2019 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package secret
  4. import (
  5. "testing"
  6. "github.com/stretchr/testify/assert"
  7. )
  8. func TestEncryptDecrypt(t *testing.T) {
  9. hex, err := EncryptSecret("foo", "baz")
  10. assert.NoError(t, err)
  11. str, _ := DecryptSecret("foo", hex)
  12. assert.Equal(t, "baz", str)
  13. hex, err = EncryptSecret("bar", "baz")
  14. assert.NoError(t, err)
  15. str, _ = DecryptSecret("foo", hex)
  16. assert.NotEqual(t, "baz", str)
  17. _, err = DecryptSecret("a", "b")
  18. assert.ErrorContains(t, err, "invalid hex string")
  19. _, err = DecryptSecret("a", "bb")
  20. assert.ErrorContains(t, err, "the key (maybe SECRET_KEY?) might be incorrect: AesDecrypt ciphertext too short")
  21. _, err = DecryptSecret("a", "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef")
  22. assert.ErrorContains(t, err, "the key (maybe SECRET_KEY?) might be incorrect: AesDecrypt invalid decrypted base64 string")
  23. }