1
0

encryption_test.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. // Copyright (C) 2019 The Syncthing Authors.
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this file,
  5. // You can obtain one at https://mozilla.org/MPL/2.0/.
  6. package protocol
  7. import (
  8. "bytes"
  9. "reflect"
  10. "strings"
  11. "testing"
  12. "github.com/syncthing/syncthing/lib/rand"
  13. )
  14. func TestEnDecryptName(t *testing.T) {
  15. var key [32]byte
  16. cases := []string{
  17. "",
  18. "foo",
  19. "a longer name/with/slashes and spaces",
  20. }
  21. for _, tc := range cases {
  22. var prev string
  23. for i := 0; i < 5; i++ {
  24. enc := encryptName(tc, &key)
  25. if prev != "" && prev != enc {
  26. t.Error("name should always encrypt the same")
  27. }
  28. prev = enc
  29. if tc != "" && strings.Contains(enc, tc) {
  30. t.Error("shouldn't contain plaintext")
  31. }
  32. dec, err := decryptName(enc, &key)
  33. if err != nil {
  34. t.Error(err)
  35. }
  36. if dec != tc {
  37. t.Error("mismatch after decryption")
  38. }
  39. t.Log(enc)
  40. }
  41. }
  42. }
  43. func TestEnDecryptBytes(t *testing.T) {
  44. var key [32]byte
  45. cases := [][]byte{
  46. {},
  47. {1, 2, 3, 4, 5},
  48. }
  49. for _, tc := range cases {
  50. var prev []byte
  51. for i := 0; i < 5; i++ {
  52. enc := encryptBytes(tc, &key)
  53. if bytes.Equal(enc, prev) {
  54. t.Error("encryption should not repeat")
  55. }
  56. prev = enc
  57. if len(tc) > 0 && bytes.Contains(enc, tc) {
  58. t.Error("shouldn't contain plaintext")
  59. }
  60. dec, err := DecryptBytes(enc, &key)
  61. if err != nil {
  62. t.Error(err)
  63. }
  64. if !bytes.Equal(dec, tc) {
  65. t.Error("mismatch after decryption")
  66. }
  67. }
  68. }
  69. }
  70. func TestEnDecryptFileInfo(t *testing.T) {
  71. var key [32]byte
  72. fi := FileInfo{
  73. Name: "hello",
  74. Size: 45,
  75. Permissions: 0755,
  76. ModifiedS: 8080,
  77. Blocks: []BlockInfo{
  78. {
  79. Size: 45,
  80. Hash: []byte{1, 2, 3},
  81. },
  82. },
  83. }
  84. enc := encryptFileInfo(fi, &key)
  85. dec, err := DecryptFileInfo(enc, &key)
  86. if err != nil {
  87. t.Error(err)
  88. }
  89. if !reflect.DeepEqual(fi, dec) {
  90. t.Error("mismatch after decryption")
  91. }
  92. }
  93. func TestIsEncryptedParent(t *testing.T) {
  94. comp := rand.String(maxPathComponent)
  95. cases := []struct {
  96. path string
  97. is bool
  98. }{
  99. {"", false},
  100. {".", false},
  101. {"/", false},
  102. {"12" + encryptedDirExtension, false},
  103. {"1" + encryptedDirExtension, true},
  104. {"1" + encryptedDirExtension + "/b", false},
  105. {"1" + encryptedDirExtension + "/bc", true},
  106. {"1" + encryptedDirExtension + "/bcd", false},
  107. {"1" + encryptedDirExtension + "/bc/foo", false},
  108. {"1.12/22", false},
  109. {"1" + encryptedDirExtension + "/bc/" + comp, true},
  110. {"1" + encryptedDirExtension + "/bc/" + comp + "/" + comp, true},
  111. {"1" + encryptedDirExtension + "/bc/" + comp + "a", false},
  112. {"1" + encryptedDirExtension + "/bc/" + comp + "/a/" + comp, false},
  113. }
  114. for _, tc := range cases {
  115. if res := IsEncryptedParent(tc.path); res != tc.is {
  116. t.Errorf("%v: got %v, expected %v", tc.path, res, tc.is)
  117. }
  118. }
  119. }