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.

172 lines
4.1 KiB

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