blocks_test.go 4.1 KB

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