blob_crypto_test.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. package blobcrypto
  2. import (
  3. "strings"
  4. "testing"
  5. "github.com/pkg/errors"
  6. "github.com/stretchr/testify/require"
  7. "github.com/kopia/kopia/internal/gather"
  8. "github.com/kopia/kopia/repo/encryption"
  9. "github.com/kopia/kopia/repo/format"
  10. "github.com/kopia/kopia/repo/hashing"
  11. )
  12. func TestBlobCrypto(t *testing.T) {
  13. f := &format.ContentFormat{
  14. Hash: hashing.DefaultAlgorithm,
  15. Encryption: encryption.DefaultAlgorithm,
  16. }
  17. hf, err := hashing.CreateHashFunc(f)
  18. require.NoError(t, err)
  19. enc, err := encryption.CreateEncryptor(f)
  20. require.NoError(t, err)
  21. cr := StaticCrypter{hf, enc}
  22. var tmp, tmp2, tmp3 gather.WriteBuffer
  23. defer tmp.Close()
  24. defer tmp2.Close()
  25. defer tmp3.Close()
  26. id, err := Encrypt(cr, gather.FromSlice([]byte{1, 2, 3}), "n", "mysessionid", &tmp)
  27. require.NoError(t, err)
  28. id2, err := Encrypt(cr, gather.FromSlice([]byte{1, 2, 4}), "n", "mysessionid", &tmp2)
  29. require.NoError(t, err)
  30. require.NotEqual(t, id, id2)
  31. require.NoError(t, Decrypt(cr, tmp.Bytes(), id, &tmp3))
  32. require.Equal(t, []byte{1, 2, 3}, tmp3.ToByteSlice())
  33. require.NoError(t, Decrypt(cr, tmp2.Bytes(), id2, &tmp3))
  34. require.Equal(t, []byte{1, 2, 4}, tmp3.ToByteSlice())
  35. // decrypting using invalid ID fails
  36. require.Error(t, Decrypt(cr, tmp.Bytes(), id2, &tmp3))
  37. require.Error(t, Decrypt(cr, tmp2.Bytes(), id, &tmp3))
  38. require.True(t, strings.HasPrefix(string(id), "n"))
  39. require.True(t, strings.HasSuffix(string(id), "-mysessionid"), id)
  40. // negative cases
  41. require.Error(t, Decrypt(cr, tmp.Bytes(), "invalid-blob-id", &tmp3))
  42. require.Error(t, Decrypt(cr, tmp.Bytes(), "zzz0123456789abcdef0123456789abcde-suffix", &tmp3))
  43. require.Error(t, Decrypt(cr, tmp.Bytes(), id2, &tmp3))
  44. require.Error(t, Decrypt(cr, gather.FromSlice([]byte{2, 3, 4}), id, &tmp2))
  45. }
  46. type badEncryptor struct{}
  47. func (badEncryptor) Encrypt(input gather.Bytes, contentID []byte, output *gather.WriteBuffer) error {
  48. return errors.New("some error")
  49. }
  50. func (badEncryptor) Decrypt(input gather.Bytes, contentID []byte, output *gather.WriteBuffer) error {
  51. return errors.New("some error")
  52. }
  53. func (badEncryptor) Overhead() int { return 0 }
  54. func TestBlobCrypto_Invalid(t *testing.T) {
  55. cr := StaticCrypter{
  56. func(output []byte, data gather.Bytes) []byte {
  57. // invalid hash
  58. return append(output, 9, 9, 9, 9)
  59. },
  60. badEncryptor{},
  61. }
  62. var tmp, tmp2, tmp3 gather.WriteBuffer
  63. defer tmp.Close()
  64. defer tmp2.Close()
  65. defer tmp3.Close()
  66. _, err := Encrypt(cr, gather.FromSlice([]byte{1, 2, 3}), "n", "mysessionid", &tmp)
  67. require.Error(t, err)
  68. f := &format.ContentFormat{
  69. Hash: hashing.DefaultAlgorithm,
  70. Encryption: encryption.DefaultAlgorithm,
  71. }
  72. // now fix HashFunction but encryption still fails.
  73. hf, err := hashing.CreateHashFunc(f)
  74. require.NoError(t, err)
  75. cr.Hash = hf
  76. _, err = Encrypt(cr, gather.FromSlice([]byte{1, 2, 3}), "n", "mysessionid", &tmp)
  77. require.Error(t, err)
  78. }