You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

118 lines
3.1 KiB

  1. package kademlia
  2. import (
  3. "fmt"
  4. "testing"
  5. "github.com/stretchr/testify/assert"
  6. )
  7. var debug = false
  8. func TestCountZeros(t *testing.T) {
  9. zeroes := []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
  10. assert.Equal(t, 0, kBucketByDistance(zeroes))
  11. b := []byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}
  12. assert.Equal(t, 20, len(b))
  13. assert.Equal(t, 159, kBucketByDistance(b))
  14. b[19] = 0x00
  15. assert.Equal(t, 159, kBucketByDistance(b))
  16. b[0] = 0x0f
  17. assert.Equal(t, 159-4, kBucketByDistance(b))
  18. b[0] = 0x0c
  19. assert.Equal(t, 159-4, kBucketByDistance(b))
  20. b[0] = 0x00
  21. b[1] = 0x00
  22. b[2] = 0x0f
  23. assert.Equal(t, 159-20, kBucketByDistance(b))
  24. b[2] = 0x07
  25. assert.Equal(t, 159-21, kBucketByDistance(b))
  26. b[2] = 0x03
  27. assert.Equal(t, 159-22, kBucketByDistance(b))
  28. }
  29. func TestKBucket(t *testing.T) {
  30. idA, err := IDFromString("0fd85ddddf15aeec2d5d8b01b013dbca030a18d7")
  31. assert.Nil(t, err)
  32. kademlia := NewKademliaTable(idA)
  33. d := kademlia.KBucket(kademlia.ID)
  34. assert.Equal(t, 0, d) // same node should have distance 0
  35. idB, err := IDFromString("c48d8b53dbefb609ed4e94d386dd5b22efcb2c5b")
  36. assert.Nil(t, err)
  37. d = kademlia.KBucket(idB)
  38. assert.Equal(t, 159, d)
  39. }
  40. func prepareTestListedNodes() []ListedNode {
  41. lnIDs := []string{
  42. "c48d8b53dbefb609ed4e94d386dd5b22efcb2c5b",
  43. "420bfebd44fc62615253224328f40f29c9b225fa",
  44. "6272bb67b1661abfa07d1d32cd9b810e531d42cd",
  45. "07db608db033384895e48098a1bbe25266387463",
  46. "c19c3285ab9ada4b420050ae1a204640b2bed508",
  47. "f8971d5da24517c8cc5a316fb0658de8906c4155",
  48. "04122a5f2dec42284147b1847ec1bc41ecd78626",
  49. "407a90870d7b482a271446c85fda940ce78a4c7a",
  50. "5ebe4539e7a33721a8623f7ebfab62600aa503e7",
  51. "fc44a42879ef3a74d6bd8303bc3e4e205a92acf9",
  52. "fc44a42879ef3a74d6bd8303bc3e4e205a92acf9",
  53. "10c86f96ebaa1685a46a6417e6faa2ef34a68976",
  54. "243c81515196a5b0e2d4675e73f0da3eead12793",
  55. "0fd85ddddf15aeec2d5d8b01b013dbca030a18d7",
  56. "0fd85ddddf15aeec2d5d8b01b013dbca030a18d5",
  57. "0fd85ddddf15aeec2d5d8b01b013dbca030a18d0",
  58. "0fd85ddddf15aeec2d5d8b01b013dbca030a1800",
  59. "0750931c40a52a2facd220a02851f7d34f95e1fa",
  60. "ebfba615ac50bcd0f5c2420741d032e885abf576",
  61. }
  62. var lns []ListedNode
  63. for i := 0; i < len(lnIDs); i++ {
  64. idI, err := IDFromString(lnIDs[i])
  65. if err != nil {
  66. panic(err)
  67. }
  68. lnI := ListedNode{
  69. ID: idI,
  70. Addr: "",
  71. }
  72. lns = append(lns, lnI)
  73. }
  74. return lns
  75. }
  76. func TestMoveToBottom(t *testing.T) {
  77. lns := prepareTestListedNodes()
  78. movedElem := lns[3]
  79. assert.NotEqual(t, movedElem, lns[len(lns)-1])
  80. lns = moveToBottom(lns, 3)
  81. assert.Equal(t, movedElem, lns[len(lns)-1])
  82. }
  83. func TestUpdate(t *testing.T) {
  84. idA, err := IDFromString("0fd85ddddf15aeec2d5d8b01b013dbca030a18d7")
  85. assert.Nil(t, err)
  86. kademlia := NewKademliaTable(idA)
  87. lns := prepareTestListedNodes()
  88. for _, lnI := range lns {
  89. kademlia.Update(lnI)
  90. }
  91. if debug {
  92. fmt.Println(kademlia)
  93. }
  94. assert.Equal(t, len(kademlia.KBuckets[0]), 1)
  95. assert.Equal(t, len(kademlia.KBuckets[1]), 1)
  96. assert.Equal(t, len(kademlia.KBuckets[158]), 4)
  97. assert.Equal(t, len(kademlia.KBuckets[159]), 5)
  98. }