blocks_test.go 3.8 KB

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