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.

171 lines
4.0 KiB

  1. package p2p
  2. import (
  3. "math/rand"
  4. "strings"
  5. "testing"
  6. . "github.com/tendermint/tendermint/common"
  7. "github.com/tendermint/tendermint/types"
  8. )
  9. // Returns an empty dummy peer
  10. func randPeer() *Peer {
  11. return &Peer{
  12. Key: RandStr(12),
  13. NodeInfo: &types.NodeInfo{
  14. Host: Fmt("%v.%v.%v.%v", rand.Int()%256, rand.Int()%256, rand.Int()%256, rand.Int()%256),
  15. },
  16. }
  17. }
  18. func TestAddRemoveOne(t *testing.T) {
  19. peerSet := NewPeerSet()
  20. peer := randPeer()
  21. err := peerSet.Add(peer)
  22. if err != nil {
  23. t.Errorf("Failed to add new peer")
  24. }
  25. if peerSet.Size() != 1 {
  26. t.Errorf("Failed to add new peer and increment size")
  27. }
  28. peerSet.Remove(peer)
  29. if peerSet.Has(peer.Key) {
  30. t.Errorf("Failed to remove peer")
  31. }
  32. if peerSet.Size() != 0 {
  33. t.Errorf("Failed to remove peer and decrement size")
  34. }
  35. }
  36. func TestAddRemoveMany(t *testing.T) {
  37. peerSet := NewPeerSet()
  38. peers := []*Peer{}
  39. N := 100
  40. maxPeersPerIPRange = [4]int{N, N, N, N}
  41. for i := 0; i < N; i++ {
  42. peer := randPeer()
  43. if err := peerSet.Add(peer); err != nil {
  44. t.Errorf("Failed to add new peer")
  45. }
  46. if peerSet.Size() != i+1 {
  47. t.Errorf("Failed to add new peer and increment size")
  48. }
  49. peers = append(peers, peer)
  50. }
  51. for i, peer := range peers {
  52. peerSet.Remove(peer)
  53. if peerSet.Has(peer.Key) {
  54. t.Errorf("Failed to remove peer")
  55. }
  56. if peerSet.Size() != len(peers)-i-1 {
  57. t.Errorf("Failed to remove peer and decrement size")
  58. }
  59. }
  60. }
  61. func newPeerInIPRange(ipBytes ...string) *Peer {
  62. ips := make([]string, 4)
  63. for i, ipByte := range ipBytes {
  64. ips[i] = ipByte
  65. }
  66. for i := len(ipBytes); i < 4; i++ {
  67. ips[i] = Fmt("%v", rand.Int()%256)
  68. }
  69. ipS := strings.Join(ips, ".")
  70. return &Peer{
  71. Key: RandStr(12),
  72. NodeInfo: &types.NodeInfo{
  73. Host: ipS,
  74. },
  75. }
  76. }
  77. func TestIPRanges(t *testing.T) {
  78. peerSet := NewPeerSet()
  79. // test /8
  80. maxPeersPerIPRange = [4]int{2, 2, 2, 2}
  81. peer := newPeerInIPRange("54")
  82. if err := peerSet.Add(peer); err != nil {
  83. t.Errorf("Failed to add new peer")
  84. }
  85. peer = newPeerInIPRange("54")
  86. if err := peerSet.Add(peer); err != nil {
  87. t.Errorf("Failed to add new peer")
  88. }
  89. peer = newPeerInIPRange("54")
  90. if err := peerSet.Add(peer); err == nil {
  91. t.Errorf("Added peer when we shouldn't have")
  92. }
  93. peer = newPeerInIPRange("55")
  94. if err := peerSet.Add(peer); err != nil {
  95. t.Errorf("Failed to add new peer")
  96. }
  97. // test /16
  98. peerSet = NewPeerSet()
  99. maxPeersPerIPRange = [4]int{3, 2, 1, 1}
  100. peer = newPeerInIPRange("54", "112")
  101. if err := peerSet.Add(peer); err != nil {
  102. t.Errorf("Failed to add new peer")
  103. }
  104. peer = newPeerInIPRange("54", "112")
  105. if err := peerSet.Add(peer); err != nil {
  106. t.Errorf("Failed to add new peer")
  107. }
  108. peer = newPeerInIPRange("54", "112")
  109. if err := peerSet.Add(peer); err == nil {
  110. t.Errorf("Added peer when we shouldn't have")
  111. }
  112. peer = newPeerInIPRange("54", "113")
  113. if err := peerSet.Add(peer); err != nil {
  114. t.Errorf("Failed to add new peer")
  115. }
  116. // test /24
  117. peerSet = NewPeerSet()
  118. maxPeersPerIPRange = [4]int{5, 3, 2, 1}
  119. peer = newPeerInIPRange("54", "112", "11")
  120. if err := peerSet.Add(peer); err != nil {
  121. t.Errorf("Failed to add new peer")
  122. }
  123. peer = newPeerInIPRange("54", "112", "11")
  124. if err := peerSet.Add(peer); err != nil {
  125. t.Errorf("Failed to add new peer")
  126. }
  127. peer = newPeerInIPRange("54", "112", "11")
  128. if err := peerSet.Add(peer); err == nil {
  129. t.Errorf("Added peer when we shouldn't have")
  130. }
  131. peer = newPeerInIPRange("54", "112", "12")
  132. if err := peerSet.Add(peer); err != nil {
  133. t.Errorf("Failed to add new peer")
  134. }
  135. // test /32
  136. peerSet = NewPeerSet()
  137. maxPeersPerIPRange = [4]int{11, 7, 5, 2}
  138. peer = newPeerInIPRange("54", "112", "11", "10")
  139. if err := peerSet.Add(peer); err != nil {
  140. t.Errorf("Failed to add new peer")
  141. }
  142. peer = newPeerInIPRange("54", "112", "11", "10")
  143. if err := peerSet.Add(peer); err != nil {
  144. t.Errorf("Failed to add new peer")
  145. }
  146. peer = newPeerInIPRange("54", "112", "11", "10")
  147. if err := peerSet.Add(peer); err == nil {
  148. t.Errorf("Added peer when we shouldn't have")
  149. }
  150. peer = newPeerInIPRange("54", "112", "11", "11")
  151. if err := peerSet.Add(peer); err != nil {
  152. t.Errorf("Failed to add new peer")
  153. }
  154. }