encryption_test.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. Offset: 0,
  80. Size: 45,
  81. Hash: []byte{1, 2, 3},
  82. },
  83. {
  84. Offset: 45,
  85. Size: 45,
  86. Hash: []byte{1, 2, 3},
  87. },
  88. },
  89. }
  90. enc := encryptFileInfo(fi, &key)
  91. if bytes.Equal(enc.Blocks[0].Hash, enc.Blocks[1].Hash) {
  92. t.Error("block hashes should not repeat when on different offsets")
  93. }
  94. again := encryptFileInfo(fi, &key)
  95. if !bytes.Equal(enc.Blocks[0].Hash, again.Blocks[0].Hash) {
  96. t.Error("block hashes should remain stable (0)")
  97. }
  98. if !bytes.Equal(enc.Blocks[1].Hash, again.Blocks[1].Hash) {
  99. t.Error("block hashes should remain stable (1)")
  100. }
  101. dec, err := DecryptFileInfo(enc, &key)
  102. if err != nil {
  103. t.Error(err)
  104. }
  105. if !reflect.DeepEqual(fi, dec) {
  106. t.Error("mismatch after decryption")
  107. }
  108. }
  109. func TestIsEncryptedParent(t *testing.T) {
  110. comp := rand.String(maxPathComponent)
  111. cases := []struct {
  112. path string
  113. is bool
  114. }{
  115. {"", false},
  116. {".", false},
  117. {"/", false},
  118. {"12" + encryptedDirExtension, false},
  119. {"1" + encryptedDirExtension, true},
  120. {"1" + encryptedDirExtension + "/b", false},
  121. {"1" + encryptedDirExtension + "/bc", true},
  122. {"1" + encryptedDirExtension + "/bcd", false},
  123. {"1" + encryptedDirExtension + "/bc/foo", false},
  124. {"1.12/22", false},
  125. {"1" + encryptedDirExtension + "/bc/" + comp, true},
  126. {"1" + encryptedDirExtension + "/bc/" + comp + "/" + comp, true},
  127. {"1" + encryptedDirExtension + "/bc/" + comp + "a", false},
  128. {"1" + encryptedDirExtension + "/bc/" + comp + "/a/" + comp, false},
  129. }
  130. for _, tc := range cases {
  131. if res := IsEncryptedParent(tc.path); res != tc.is {
  132. t.Errorf("%v: got %v, expected %v", tc.path, res, tc.is)
  133. }
  134. }
  135. }