blocks_test.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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. "crypto/sha256"
  12. "fmt"
  13. origAdler32 "hash/adler32"
  14. mrand "math/rand"
  15. "testing"
  16. "testing/quick"
  17. rollingAdler32 "github.com/chmduquesne/rollinghash/adler32"
  18. "github.com/syncthing/syncthing/lib/protocol"
  19. )
  20. var blocksTestData = []struct {
  21. data []byte
  22. blocksize int
  23. hash []string
  24. weakhash []uint32
  25. }{
  26. {
  27. []byte(""), 1024,
  28. []string{
  29. "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
  30. },
  31. []uint32{0},
  32. },
  33. {
  34. []byte("contents"), 1024,
  35. []string{
  36. "d1b2a59fbea7e20077af9f91b27e95e865061b270be03ff539ab3b73587882e8",
  37. },
  38. []uint32{0x0f3a036f},
  39. },
  40. {
  41. []byte("contents"), 9,
  42. []string{
  43. "d1b2a59fbea7e20077af9f91b27e95e865061b270be03ff539ab3b73587882e8",
  44. },
  45. []uint32{0x0f3a036f},
  46. },
  47. {
  48. []byte("contents"), 8,
  49. []string{
  50. "d1b2a59fbea7e20077af9f91b27e95e865061b270be03ff539ab3b73587882e8",
  51. },
  52. []uint32{0x0f3a036f},
  53. },
  54. {
  55. []byte("contents"), 7,
  56. []string{
  57. "ed7002b439e9ac845f22357d822bac1444730fbdb6016d3ec9432297b9ec9f73",
  58. "043a718774c572bd8a25adbeb1bfcd5c0256ae11cecf9f9c3f925d0e52beaf89",
  59. },
  60. []uint32{0x0bcb02fc, 0x00740074},
  61. },
  62. {
  63. []byte("contents"), 3,
  64. []string{
  65. "1143da2bc54c495c4be31d3868785d39ffdfd56df5668f0645d8f14d47647952",
  66. "e4432baa90819aaef51d2a7f8e148bf7e679610f3173752fabb4dcb2d0f418d3",
  67. "44ad63f60af0f6db6fdde6d5186ef78176367df261fa06be3079b6c80c8adba4",
  68. },
  69. []uint32{0x02780141, 0x02970148, 0x015d00e8},
  70. },
  71. {
  72. []byte("conconts"), 3,
  73. []string{
  74. "1143da2bc54c495c4be31d3868785d39ffdfd56df5668f0645d8f14d47647952",
  75. "1143da2bc54c495c4be31d3868785d39ffdfd56df5668f0645d8f14d47647952",
  76. "44ad63f60af0f6db6fdde6d5186ef78176367df261fa06be3079b6c80c8adba4",
  77. },
  78. []uint32{0x02780141, 0x02780141, 0x015d00e8},
  79. },
  80. {
  81. []byte("contenten"), 3,
  82. []string{
  83. "1143da2bc54c495c4be31d3868785d39ffdfd56df5668f0645d8f14d47647952",
  84. "e4432baa90819aaef51d2a7f8e148bf7e679610f3173752fabb4dcb2d0f418d3",
  85. "e4432baa90819aaef51d2a7f8e148bf7e679610f3173752fabb4dcb2d0f418d3",
  86. },
  87. []uint32{0x02780141, 0x02970148, 0x02970148},
  88. },
  89. }
  90. func TestBlocks(t *testing.T) {
  91. for testNo, test := range blocksTestData {
  92. buf := bytes.NewBuffer(test.data)
  93. blocks, err := Blocks(context.TODO(), buf, test.blocksize, -1, nil, true)
  94. if err != nil {
  95. t.Fatal(err)
  96. }
  97. if l := len(blocks); l != len(test.hash) {
  98. t.Fatalf("%d: Incorrect number of blocks %d != %d", testNo, l, len(test.hash))
  99. } else {
  100. i := 0
  101. for off := int64(0); off < int64(len(test.data)); off += int64(test.blocksize) {
  102. if blocks[i].Offset != off {
  103. t.Errorf("%d/%d: Incorrect offset %d != %d", testNo, i, blocks[i].Offset, off)
  104. }
  105. bs := test.blocksize
  106. if rem := len(test.data) - int(off); bs > rem {
  107. bs = rem
  108. }
  109. if int(blocks[i].Size) != bs {
  110. t.Errorf("%d/%d: Incorrect length %d != %d", testNo, i, blocks[i].Size, bs)
  111. }
  112. if h := fmt.Sprintf("%x", blocks[i].Hash); h != test.hash[i] {
  113. t.Errorf("%d/%d: Incorrect block hash %q != %q", testNo, i, h, test.hash[i])
  114. }
  115. if h := blocks[i].WeakHash; h != test.weakhash[i] {
  116. t.Errorf("%d/%d: Incorrect block weakhash 0x%08x != 0x%08x", testNo, i, h, test.weakhash[i])
  117. }
  118. i++
  119. }
  120. }
  121. }
  122. }
  123. func TestAdler32Variants(t *testing.T) {
  124. // Verify that the two adler32 functions give matching results for a few
  125. // different blocks of data.
  126. hf1 := origAdler32.New()
  127. hf2 := rollingAdler32.New()
  128. checkFn := func(data []byte) bool {
  129. hf1.Write(data)
  130. sum1 := hf1.Sum32()
  131. hf2.Write(data)
  132. sum2 := hf2.Sum32()
  133. hf1.Reset()
  134. hf2.Reset()
  135. // Make sure whatever we use in Validate matches too resp. this
  136. // tests gets adjusted if we ever switch the weak hash algo.
  137. return sum1 == sum2 && Validate(data, nil, sum1)
  138. }
  139. // protocol block sized data
  140. data := make([]byte, protocol.MinBlockSize)
  141. for i := 0; i < 5; i++ {
  142. rand.Read(data)
  143. if !checkFn(data) {
  144. t.Errorf("Hash mismatch on block sized data")
  145. }
  146. }
  147. // random small blocks
  148. if err := quick.Check(checkFn, nil); err != nil {
  149. t.Error(err)
  150. }
  151. // rolling should have the same result as the individual blocks
  152. // themselves.
  153. windowSize := 128
  154. hf3 := rollingAdler32.New()
  155. hf3.Write(data[:windowSize])
  156. for i := windowSize; i < len(data); i++ {
  157. if i%windowSize == 0 {
  158. // let the reference function catch up
  159. window := data[i-windowSize : i]
  160. hf1.Reset()
  161. hf1.Write(window)
  162. hf2.Reset()
  163. hf2.Write(window)
  164. // verify that they are in sync with the rolling function
  165. sum1 := hf1.Sum32()
  166. sum2 := hf2.Sum32()
  167. sum3 := hf3.Sum32()
  168. t.Logf("At i=%d, sum2=%08x, sum3=%08x", i, sum2, sum3)
  169. if sum2 != sum3 {
  170. t.Errorf("Mismatch after roll; i=%d, sum2=%08x, sum3=%08x", i, sum2, sum3)
  171. break
  172. }
  173. if sum1 != sum3 {
  174. t.Errorf("Mismatch after roll; i=%d, sum1=%08x, sum3=%08x", i, sum1, sum3)
  175. break
  176. }
  177. if !Validate(window, nil, sum1) {
  178. t.Errorf("Validation failure after roll; i=%d", i)
  179. }
  180. }
  181. hf3.Roll(data[i])
  182. }
  183. }
  184. func BenchmarkValidate(b *testing.B) {
  185. type block struct {
  186. data []byte
  187. hash [sha256.Size]byte
  188. weakhash uint32
  189. }
  190. var blocks []block
  191. const blocksPerType = 100
  192. r := mrand.New(mrand.NewSource(0x136bea689e851))
  193. // Valid blocks.
  194. for i := 0; i < blocksPerType; i++ {
  195. var b block
  196. b.data = make([]byte, 128<<10)
  197. r.Read(b.data)
  198. b.hash = sha256.Sum256(b.data)
  199. b.weakhash = origAdler32.Checksum(b.data)
  200. blocks = append(blocks, b)
  201. }
  202. // Blocks where the hash matches, but the weakhash doesn't.
  203. for i := 0; i < blocksPerType; i++ {
  204. var b block
  205. b.data = make([]byte, 128<<10)
  206. r.Read(b.data)
  207. b.hash = sha256.Sum256(b.data)
  208. b.weakhash = 1 // Zeros causes Validate to skip the weakhash.
  209. blocks = append(blocks, b)
  210. }
  211. b.ReportAllocs()
  212. b.ResetTimer()
  213. for i := 0; i < b.N; i++ {
  214. for _, b := range blocks {
  215. Validate(b.data, b.hash[:], b.weakhash)
  216. }
  217. }
  218. }