blocks_test.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. // Copyright (C) 2014 The Syncthing Authors.
  2. //
  3. // This program is free software: you can redistribute it and/or modify it
  4. // under the terms of the GNU General Public License as published by the Free
  5. // Software Foundation, either version 3 of the License, or (at your option)
  6. // any later version.
  7. //
  8. // This program is distributed in the hope that it will be useful, but WITHOUT
  9. // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  11. // more details.
  12. //
  13. // You should have received a copy of the GNU General Public License along
  14. // with this program. If not, see <http://www.gnu.org/licenses/>.
  15. package scanner
  16. import (
  17. "bytes"
  18. "fmt"
  19. "testing"
  20. "github.com/syncthing/syncthing/internal/protocol"
  21. )
  22. var blocksTestData = []struct {
  23. data []byte
  24. blocksize int
  25. hash []string
  26. }{
  27. {[]byte(""), 1024, []string{
  28. "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"}},
  29. {[]byte("contents"), 1024, []string{
  30. "d1b2a59fbea7e20077af9f91b27e95e865061b270be03ff539ab3b73587882e8"}},
  31. {[]byte("contents"), 9, []string{
  32. "d1b2a59fbea7e20077af9f91b27e95e865061b270be03ff539ab3b73587882e8"}},
  33. {[]byte("contents"), 8, []string{
  34. "d1b2a59fbea7e20077af9f91b27e95e865061b270be03ff539ab3b73587882e8"}},
  35. {[]byte("contents"), 7, []string{
  36. "ed7002b439e9ac845f22357d822bac1444730fbdb6016d3ec9432297b9ec9f73",
  37. "043a718774c572bd8a25adbeb1bfcd5c0256ae11cecf9f9c3f925d0e52beaf89"},
  38. },
  39. {[]byte("contents"), 3, []string{
  40. "1143da2bc54c495c4be31d3868785d39ffdfd56df5668f0645d8f14d47647952",
  41. "e4432baa90819aaef51d2a7f8e148bf7e679610f3173752fabb4dcb2d0f418d3",
  42. "44ad63f60af0f6db6fdde6d5186ef78176367df261fa06be3079b6c80c8adba4"},
  43. },
  44. {[]byte("conconts"), 3, []string{
  45. "1143da2bc54c495c4be31d3868785d39ffdfd56df5668f0645d8f14d47647952",
  46. "1143da2bc54c495c4be31d3868785d39ffdfd56df5668f0645d8f14d47647952",
  47. "44ad63f60af0f6db6fdde6d5186ef78176367df261fa06be3079b6c80c8adba4"},
  48. },
  49. {[]byte("contenten"), 3, []string{
  50. "1143da2bc54c495c4be31d3868785d39ffdfd56df5668f0645d8f14d47647952",
  51. "e4432baa90819aaef51d2a7f8e148bf7e679610f3173752fabb4dcb2d0f418d3",
  52. "e4432baa90819aaef51d2a7f8e148bf7e679610f3173752fabb4dcb2d0f418d3"},
  53. },
  54. }
  55. func TestBlocks(t *testing.T) {
  56. for _, test := range blocksTestData {
  57. buf := bytes.NewBuffer(test.data)
  58. blocks, err := Blocks(buf, test.blocksize, 0)
  59. if err != nil {
  60. t.Fatal(err)
  61. }
  62. if l := len(blocks); l != len(test.hash) {
  63. t.Fatalf("Incorrect number of blocks %d != %d", l, len(test.hash))
  64. } else {
  65. i := 0
  66. for off := int64(0); off < int64(len(test.data)); off += int64(test.blocksize) {
  67. if blocks[i].Offset != off {
  68. t.Errorf("Incorrect offset for block %d: %d != %d", i, blocks[i].Offset, off)
  69. }
  70. bs := test.blocksize
  71. if rem := len(test.data) - int(off); bs > rem {
  72. bs = rem
  73. }
  74. if int(blocks[i].Size) != bs {
  75. t.Errorf("Incorrect length for block %d: %d != %d", i, blocks[i].Size, bs)
  76. }
  77. if h := fmt.Sprintf("%x", blocks[i].Hash); h != test.hash[i] {
  78. t.Errorf("Incorrect block hash %q != %q", h, test.hash[i])
  79. }
  80. i++
  81. }
  82. }
  83. }
  84. }
  85. var diffTestData = []struct {
  86. a string
  87. b string
  88. s int
  89. d []protocol.BlockInfo
  90. }{
  91. {"contents", "contents", 1024, []protocol.BlockInfo{}},
  92. {"", "", 1024, []protocol.BlockInfo{}},
  93. {"contents", "contents", 3, []protocol.BlockInfo{}},
  94. {"contents", "cantents", 3, []protocol.BlockInfo{{0, 3, nil}}},
  95. {"contents", "contants", 3, []protocol.BlockInfo{{3, 3, nil}}},
  96. {"contents", "cantants", 3, []protocol.BlockInfo{{0, 3, nil}, {3, 3, nil}}},
  97. {"contents", "", 3, []protocol.BlockInfo{{0, 0, nil}}},
  98. {"", "contents", 3, []protocol.BlockInfo{{0, 3, nil}, {3, 3, nil}, {6, 2, nil}}},
  99. {"con", "contents", 3, []protocol.BlockInfo{{3, 3, nil}, {6, 2, nil}}},
  100. {"contents", "con", 3, nil},
  101. {"contents", "cont", 3, []protocol.BlockInfo{{3, 1, nil}}},
  102. {"cont", "contents", 3, []protocol.BlockInfo{{3, 3, nil}, {6, 2, nil}}},
  103. }
  104. func TestDiff(t *testing.T) {
  105. for i, test := range diffTestData {
  106. a, _ := Blocks(bytes.NewBufferString(test.a), test.s, 0)
  107. b, _ := Blocks(bytes.NewBufferString(test.b), test.s, 0)
  108. _, d := BlockDiff(a, b)
  109. if len(d) != len(test.d) {
  110. t.Fatalf("Incorrect length for diff %d; %d != %d", i, len(d), len(test.d))
  111. } else {
  112. for j := range test.d {
  113. if d[j].Offset != test.d[j].Offset {
  114. t.Errorf("Incorrect offset for diff %d block %d; %d != %d", i, j, d[j].Offset, test.d[j].Offset)
  115. }
  116. if d[j].Size != test.d[j].Size {
  117. t.Errorf("Incorrect length for diff %d block %d; %d != %d", i, j, d[j].Size, test.d[j].Size)
  118. }
  119. }
  120. }
  121. }
  122. }