blocks_test.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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 http://mozilla.org/MPL/2.0/.
  6. package scanner
  7. import (
  8. "bytes"
  9. "fmt"
  10. "testing"
  11. "github.com/syncthing/protocol"
  12. )
  13. var blocksTestData = []struct {
  14. data []byte
  15. blocksize int
  16. hash []string
  17. }{
  18. {[]byte(""), 1024, []string{
  19. "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"}},
  20. {[]byte("contents"), 1024, []string{
  21. "d1b2a59fbea7e20077af9f91b27e95e865061b270be03ff539ab3b73587882e8"}},
  22. {[]byte("contents"), 9, []string{
  23. "d1b2a59fbea7e20077af9f91b27e95e865061b270be03ff539ab3b73587882e8"}},
  24. {[]byte("contents"), 8, []string{
  25. "d1b2a59fbea7e20077af9f91b27e95e865061b270be03ff539ab3b73587882e8"}},
  26. {[]byte("contents"), 7, []string{
  27. "ed7002b439e9ac845f22357d822bac1444730fbdb6016d3ec9432297b9ec9f73",
  28. "043a718774c572bd8a25adbeb1bfcd5c0256ae11cecf9f9c3f925d0e52beaf89"},
  29. },
  30. {[]byte("contents"), 3, []string{
  31. "1143da2bc54c495c4be31d3868785d39ffdfd56df5668f0645d8f14d47647952",
  32. "e4432baa90819aaef51d2a7f8e148bf7e679610f3173752fabb4dcb2d0f418d3",
  33. "44ad63f60af0f6db6fdde6d5186ef78176367df261fa06be3079b6c80c8adba4"},
  34. },
  35. {[]byte("conconts"), 3, []string{
  36. "1143da2bc54c495c4be31d3868785d39ffdfd56df5668f0645d8f14d47647952",
  37. "1143da2bc54c495c4be31d3868785d39ffdfd56df5668f0645d8f14d47647952",
  38. "44ad63f60af0f6db6fdde6d5186ef78176367df261fa06be3079b6c80c8adba4"},
  39. },
  40. {[]byte("contenten"), 3, []string{
  41. "1143da2bc54c495c4be31d3868785d39ffdfd56df5668f0645d8f14d47647952",
  42. "e4432baa90819aaef51d2a7f8e148bf7e679610f3173752fabb4dcb2d0f418d3",
  43. "e4432baa90819aaef51d2a7f8e148bf7e679610f3173752fabb4dcb2d0f418d3"},
  44. },
  45. }
  46. func TestBlocks(t *testing.T) {
  47. for _, test := range blocksTestData {
  48. buf := bytes.NewBuffer(test.data)
  49. blocks, err := Blocks(buf, test.blocksize, 0)
  50. if err != nil {
  51. t.Fatal(err)
  52. }
  53. if l := len(blocks); l != len(test.hash) {
  54. t.Fatalf("Incorrect number of blocks %d != %d", l, len(test.hash))
  55. } else {
  56. i := 0
  57. for off := int64(0); off < int64(len(test.data)); off += int64(test.blocksize) {
  58. if blocks[i].Offset != off {
  59. t.Errorf("Incorrect offset for block %d: %d != %d", i, blocks[i].Offset, off)
  60. }
  61. bs := test.blocksize
  62. if rem := len(test.data) - int(off); bs > rem {
  63. bs = rem
  64. }
  65. if int(blocks[i].Size) != bs {
  66. t.Errorf("Incorrect length for block %d: %d != %d", i, blocks[i].Size, bs)
  67. }
  68. if h := fmt.Sprintf("%x", blocks[i].Hash); h != test.hash[i] {
  69. t.Errorf("Incorrect block hash %q != %q", h, test.hash[i])
  70. }
  71. i++
  72. }
  73. }
  74. }
  75. }
  76. var diffTestData = []struct {
  77. a string
  78. b string
  79. s int
  80. d []protocol.BlockInfo
  81. }{
  82. {"contents", "contents", 1024, []protocol.BlockInfo{}},
  83. {"", "", 1024, []protocol.BlockInfo{}},
  84. {"contents", "contents", 3, []protocol.BlockInfo{}},
  85. {"contents", "cantents", 3, []protocol.BlockInfo{{0, 3, nil}}},
  86. {"contents", "contants", 3, []protocol.BlockInfo{{3, 3, nil}}},
  87. {"contents", "cantants", 3, []protocol.BlockInfo{{0, 3, nil}, {3, 3, nil}}},
  88. {"contents", "", 3, []protocol.BlockInfo{{0, 0, nil}}},
  89. {"", "contents", 3, []protocol.BlockInfo{{0, 3, nil}, {3, 3, nil}, {6, 2, nil}}},
  90. {"con", "contents", 3, []protocol.BlockInfo{{3, 3, nil}, {6, 2, nil}}},
  91. {"contents", "con", 3, nil},
  92. {"contents", "cont", 3, []protocol.BlockInfo{{3, 1, nil}}},
  93. {"cont", "contents", 3, []protocol.BlockInfo{{3, 3, nil}, {6, 2, nil}}},
  94. }
  95. func TestDiff(t *testing.T) {
  96. for i, test := range diffTestData {
  97. a, _ := Blocks(bytes.NewBufferString(test.a), test.s, 0)
  98. b, _ := Blocks(bytes.NewBufferString(test.b), test.s, 0)
  99. _, d := BlockDiff(a, b)
  100. if len(d) != len(test.d) {
  101. t.Fatalf("Incorrect length for diff %d; %d != %d", i, len(d), len(test.d))
  102. } else {
  103. for j := range test.d {
  104. if d[j].Offset != test.d[j].Offset {
  105. t.Errorf("Incorrect offset for diff %d block %d; %d != %d", i, j, d[j].Offset, test.d[j].Offset)
  106. }
  107. if d[j].Size != test.d[j].Size {
  108. t.Errorf("Incorrect length for diff %d block %d; %d != %d", i, j, d[j].Size, test.d[j].Size)
  109. }
  110. }
  111. }
  112. }
  113. }