leveldb_test.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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 db
  7. import (
  8. "bytes"
  9. "testing"
  10. "github.com/syncthing/syncthing/lib/protocol"
  11. )
  12. func TestDeviceKey(t *testing.T) {
  13. fld := []byte("folder6789012345678901234567890123456789012345678901234567890123")
  14. dev := []byte("device67890123456789012345678901")
  15. name := []byte("name")
  16. db := OpenMemory()
  17. db.folderIdx.ID(fld)
  18. db.deviceIdx.ID(dev)
  19. key := db.deviceKey(fld, dev, name)
  20. fld2 := db.deviceKeyFolder(key)
  21. if !bytes.Equal(fld2, fld) {
  22. t.Errorf("wrong folder %q != %q", fld2, fld)
  23. }
  24. dev2 := db.deviceKeyDevice(key)
  25. if !bytes.Equal(dev2, dev) {
  26. t.Errorf("wrong device %q != %q", dev2, dev)
  27. }
  28. name2 := db.deviceKeyName(key)
  29. if !bytes.Equal(name2, name) {
  30. t.Errorf("wrong name %q != %q", name2, name)
  31. }
  32. }
  33. func TestGlobalKey(t *testing.T) {
  34. fld := []byte("folder6789012345678901234567890123456789012345678901234567890123")
  35. name := []byte("name")
  36. db := OpenMemory()
  37. db.folderIdx.ID(fld)
  38. key := db.globalKey(fld, name)
  39. fld2, ok := db.globalKeyFolder(key)
  40. if !ok {
  41. t.Error("should have been found")
  42. }
  43. if !bytes.Equal(fld2, fld) {
  44. t.Errorf("wrong folder %q != %q", fld2, fld)
  45. }
  46. name2 := db.globalKeyName(key)
  47. if !bytes.Equal(name2, name) {
  48. t.Errorf("wrong name %q != %q", name2, name)
  49. }
  50. _, ok = db.globalKeyFolder([]byte{1, 2, 3, 4, 5})
  51. if ok {
  52. t.Error("should not have been found")
  53. }
  54. }
  55. func TestDropIndexIDs(t *testing.T) {
  56. db := OpenMemory()
  57. d1 := []byte("device67890123456789012345678901")
  58. d2 := []byte("device12345678901234567890123456")
  59. // Set some index IDs
  60. db.setIndexID(protocol.LocalDeviceID[:], []byte("foo"), 1)
  61. db.setIndexID(protocol.LocalDeviceID[:], []byte("bar"), 2)
  62. db.setIndexID(d1, []byte("foo"), 3)
  63. db.setIndexID(d1, []byte("bar"), 4)
  64. db.setIndexID(d2, []byte("foo"), 5)
  65. db.setIndexID(d2, []byte("bar"), 6)
  66. // Verify them
  67. if db.getIndexID(protocol.LocalDeviceID[:], []byte("foo")) != 1 {
  68. t.Fatal("fail local 1")
  69. }
  70. if db.getIndexID(protocol.LocalDeviceID[:], []byte("bar")) != 2 {
  71. t.Fatal("fail local 2")
  72. }
  73. if db.getIndexID(d1, []byte("foo")) != 3 {
  74. t.Fatal("fail remote 1")
  75. }
  76. if db.getIndexID(d1, []byte("bar")) != 4 {
  77. t.Fatal("fail remote 2")
  78. }
  79. if db.getIndexID(d2, []byte("foo")) != 5 {
  80. t.Fatal("fail remote 3")
  81. }
  82. if db.getIndexID(d2, []byte("bar")) != 6 {
  83. t.Fatal("fail remote 4")
  84. }
  85. // Drop the local ones, verify only they got dropped
  86. db.DropLocalDeltaIndexIDs()
  87. if db.getIndexID(protocol.LocalDeviceID[:], []byte("foo")) != 0 {
  88. t.Fatal("fail local 1")
  89. }
  90. if db.getIndexID(protocol.LocalDeviceID[:], []byte("bar")) != 0 {
  91. t.Fatal("fail local 2")
  92. }
  93. if db.getIndexID(d1, []byte("foo")) != 3 {
  94. t.Fatal("fail remote 1")
  95. }
  96. if db.getIndexID(d1, []byte("bar")) != 4 {
  97. t.Fatal("fail remote 2")
  98. }
  99. if db.getIndexID(d2, []byte("foo")) != 5 {
  100. t.Fatal("fail remote 3")
  101. }
  102. if db.getIndexID(d2, []byte("bar")) != 6 {
  103. t.Fatal("fail remote 4")
  104. }
  105. // Set local ones again
  106. db.setIndexID(protocol.LocalDeviceID[:], []byte("foo"), 1)
  107. db.setIndexID(protocol.LocalDeviceID[:], []byte("bar"), 2)
  108. // Drop the remote ones, verify only they got dropped
  109. db.DropRemoteDeltaIndexIDs()
  110. if db.getIndexID(protocol.LocalDeviceID[:], []byte("foo")) != 1 {
  111. t.Fatal("fail local 1")
  112. }
  113. if db.getIndexID(protocol.LocalDeviceID[:], []byte("bar")) != 2 {
  114. t.Fatal("fail local 2")
  115. }
  116. if db.getIndexID(d1, []byte("foo")) != 0 {
  117. t.Fatal("fail remote 1")
  118. }
  119. if db.getIndexID(d1, []byte("bar")) != 0 {
  120. t.Fatal("fail remote 2")
  121. }
  122. if db.getIndexID(d2, []byte("foo")) != 0 {
  123. t.Fatal("fail remote 3")
  124. }
  125. if db.getIndexID(d2, []byte("bar")) != 0 {
  126. t.Fatal("fail remote 4")
  127. }
  128. }