blocks_test.go 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. // Copyright (C) 2014 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 scanner
  7. import (
  8. "bytes"
  9. "context"
  10. "crypto/rand"
  11. "fmt"
  12. origAdler32 "hash/adler32"
  13. "testing"
  14. "testing/quick"
  15. rollingAdler32 "github.com/chmduquesne/rollinghash/adler32"
  16. "github.com/syncthing/syncthing/lib/protocol"
  17. )
  18. var blocksTestData = []struct {
  19. data []byte
  20. blocksize int
  21. hash []string
  22. weakhash []uint32
  23. }{
  24. {[]byte(""), 1024, []string{
  25. "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"},
  26. []uint32{0},
  27. },
  28. {[]byte("contents"), 1024, []string{
  29. "d1b2a59fbea7e20077af9f91b27e95e865061b270be03ff539ab3b73587882e8"},
  30. []uint32{0x0f3a036f},
  31. },
  32. {[]byte("contents"), 9, []string{
  33. "d1b2a59fbea7e20077af9f91b27e95e865061b270be03ff539ab3b73587882e8"},
  34. []uint32{0x0f3a036f},
  35. },
  36. {[]byte("contents"), 8, []string{
  37. "d1b2a59fbea7e20077af9f91b27e95e865061b270be03ff539ab3b73587882e8"},
  38. []uint32{0x0f3a036f},
  39. },
  40. {[]byte("contents"), 7, []string{
  41. "ed7002b439e9ac845f22357d822bac1444730fbdb6016d3ec9432297b9ec9f73",
  42. "043a718774c572bd8a25adbeb1bfcd5c0256ae11cecf9f9c3f925d0e52beaf89"},
  43. []uint32{0x0bcb02fc, 0x00740074},
  44. },
  45. {[]byte("contents"), 3, []string{
  46. "1143da2bc54c495c4be31d3868785d39ffdfd56df5668f0645d8f14d47647952",
  47. "e4432baa90819aaef51d2a7f8e148bf7e679610f3173752fabb4dcb2d0f418d3",
  48. "44ad63f60af0f6db6fdde6d5186ef78176367df261fa06be3079b6c80c8adba4"},
  49. []uint32{0x02780141, 0x02970148, 0x015d00e8},
  50. },
  51. {[]byte("conconts"), 3, []string{
  52. "1143da2bc54c495c4be31d3868785d39ffdfd56df5668f0645d8f14d47647952",
  53. "1143da2bc54c495c4be31d3868785d39ffdfd56df5668f0645d8f14d47647952",
  54. "44ad63f60af0f6db6fdde6d5186ef78176367df261fa06be3079b6c80c8adba4"},
  55. []uint32{0x02780141, 0x02780141, 0x015d00e8},
  56. },
  57. {[]byte("contenten"), 3, []string{
  58. "1143da2bc54c495c4be31d3868785d39ffdfd56df5668f0645d8f14d47647952",
  59. "e4432baa90819aaef51d2a7f8e148bf7e679610f3173752fabb4dcb2d0f418d3",
  60. "e4432baa90819aaef51d2a7f8e148bf7e679610f3173752fabb4dcb2d0f418d3"},
  61. []uint32{0x02780141, 0x02970148, 0x02970148},
  62. },
  63. }
  64. func TestBlocks(t *testing.T) {
  65. for testNo, test := range blocksTestData {
  66. buf := bytes.NewBuffer(test.data)
  67. blocks, err := Blocks(context.TODO(), buf, test.blocksize, -1, nil, true)
  68. if err != nil {
  69. t.Fatal(err)
  70. }
  71. if l := len(blocks); l != len(test.hash) {
  72. t.Fatalf("%d: Incorrect number of blocks %d != %d", testNo, l, len(test.hash))
  73. } else {
  74. i := 0
  75. for off := int64(0); off < int64(len(test.data)); off += int64(test.blocksize) {
  76. if blocks[i].Offset != off {
  77. t.Errorf("%d/%d: Incorrect offset %d != %d", testNo, i, blocks[i].Offset, off)
  78. }
  79. bs := test.blocksize
  80. if rem := len(test.data) - int(off); bs > rem {
  81. bs = rem
  82. }
  83. if int(blocks[i].Size) != bs {
  84. t.Errorf("%d/%d: Incorrect length %d != %d", testNo, i, blocks[i].Size, bs)
  85. }
  86. if h := fmt.Sprintf("%x", blocks[i].Hash); h != test.hash[i] {
  87. t.Errorf("%d/%d: Incorrect block hash %q != %q", testNo, i, h, test.hash[i])
  88. }
  89. if h := blocks[i].WeakHash; h != test.weakhash[i] {
  90. t.Errorf("%d/%d: Incorrect block weakhash 0x%08x != 0x%08x", testNo, i, h, test.weakhash[i])
  91. }
  92. i++
  93. }
  94. }
  95. }
  96. }
  97. var diffTestData = []struct {
  98. a string
  99. b string
  100. s int
  101. d []protocol.BlockInfo
  102. }{
  103. {"contents", "contents", 1024, []protocol.BlockInfo{}},
  104. {"", "", 1024, []protocol.BlockInfo{}},
  105. {"contents", "contents", 3, []protocol.BlockInfo{}},
  106. {"contents", "cantents", 3, []protocol.BlockInfo{{Offset: 0, Size: 3}}},
  107. {"contents", "contants", 3, []protocol.BlockInfo{{Offset: 3, Size: 3}}},
  108. {"contents", "cantants", 3, []protocol.BlockInfo{{Offset: 0, Size: 3}, {Offset: 3, Size: 3}}},
  109. {"contents", "", 3, []protocol.BlockInfo{{Offset: 0, Size: 0}}},
  110. {"", "contents", 3, []protocol.BlockInfo{{Offset: 0, Size: 3}, {Offset: 3, Size: 3}, {Offset: 6, Size: 2}}},
  111. {"con", "contents", 3, []protocol.BlockInfo{{Offset: 3, Size: 3}, {Offset: 6, Size: 2}}},
  112. {"contents", "con", 3, nil},
  113. {"contents", "cont", 3, []protocol.BlockInfo{{Offset: 3, Size: 1}}},
  114. {"cont", "contents", 3, []protocol.BlockInfo{{Offset: 3, Size: 3}, {Offset: 6, Size: 2}}},
  115. }
  116. func TestDiff(t *testing.T) {
  117. for i, test := range diffTestData {
  118. a, _ := Blocks(context.TODO(), bytes.NewBufferString(test.a), test.s, -1, nil, false)
  119. b, _ := Blocks(context.TODO(), bytes.NewBufferString(test.b), test.s, -1, nil, false)
  120. _, d := BlockDiff(a, b)
  121. if len(d) != len(test.d) {
  122. t.Fatalf("Incorrect length for diff %d; %d != %d", i, len(d), len(test.d))
  123. } else {
  124. for j := range test.d {
  125. if d[j].Offset != test.d[j].Offset {
  126. t.Errorf("Incorrect offset for diff %d block %d; %d != %d", i, j, d[j].Offset, test.d[j].Offset)
  127. }
  128. if d[j].Size != test.d[j].Size {
  129. t.Errorf("Incorrect length for diff %d block %d; %d != %d", i, j, d[j].Size, test.d[j].Size)
  130. }
  131. }
  132. }
  133. }
  134. }
  135. func TestDiffEmpty(t *testing.T) {
  136. emptyCases := []struct {
  137. a []protocol.BlockInfo
  138. b []protocol.BlockInfo
  139. need int
  140. have int
  141. }{
  142. {nil, nil, 0, 0},
  143. {[]protocol.BlockInfo{{Offset: 3, Size: 1}}, nil, 0, 0},
  144. {nil, []protocol.BlockInfo{{Offset: 3, Size: 1}}, 1, 0},
  145. }
  146. for _, emptyCase := range emptyCases {
  147. h, n := BlockDiff(emptyCase.a, emptyCase.b)
  148. if len(h) != emptyCase.have {
  149. t.Errorf("incorrect have: %d != %d", len(h), emptyCase.have)
  150. }
  151. if len(n) != emptyCase.need {
  152. t.Errorf("incorrect have: %d != %d", len(h), emptyCase.have)
  153. }
  154. }
  155. }
  156. func TestAdler32Variants(t *testing.T) {
  157. // Verify that the two adler32 functions give matching results for a few
  158. // different blocks of data.
  159. hf1 := origAdler32.New()
  160. hf2 := rollingAdler32.New()
  161. checkFn := func(data []byte) bool {
  162. hf1.Write(data)
  163. sum1 := hf1.Sum32()
  164. hf2.Write(data)
  165. sum2 := hf2.Sum32()
  166. hf1.Reset()
  167. hf2.Reset()
  168. return sum1 == sum2
  169. }
  170. // protocol block sized data
  171. data := make([]byte, protocol.BlockSize)
  172. for i := 0; i < 5; i++ {
  173. rand.Read(data)
  174. if !checkFn(data) {
  175. t.Errorf("Hash mismatch on block sized data")
  176. }
  177. }
  178. // random small blocks
  179. if err := quick.Check(checkFn, nil); err != nil {
  180. t.Error(err)
  181. }
  182. // rolling should have the same result as the individual blocks
  183. // themselves. Which is not the same as the original non-rollind adler32
  184. // blocks.
  185. windowSize := 128
  186. hf2.Reset()
  187. hf3 := rollingAdler32.New()
  188. hf3.Write(data[:windowSize])
  189. for i := windowSize; i < len(data); i++ {
  190. if i%windowSize == 0 {
  191. // let the reference function catch up
  192. hf2.Write(data[i-windowSize : i])
  193. // verify that they are in sync with the rolling function
  194. sum2 := hf2.Sum32()
  195. sum3 := hf3.Sum32()
  196. t.Logf("At i=%d, sum2=%08x, sum3=%08x", i, sum2, sum3)
  197. if sum2 != sum3 {
  198. t.Errorf("Mismatch after roll; i=%d, sum2=%08x, sum3=%08x", i, sum2, sum3)
  199. break
  200. }
  201. }
  202. hf3.Roll(data[i])
  203. }
  204. }