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.

218 lines
5.1 KiB

9 years ago
9 years ago
8 years ago
9 years ago
9 years ago
8 years ago
9 years ago
8 years ago
9 years ago
9 years ago
9 years ago
9 years ago
8 years ago
9 years ago
9 years ago
9 years ago
8 years ago
9 years ago
9 years ago
9 years ago
8 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
8 years ago
9 years ago
8 years ago
8 years ago
8 years ago
  1. package pex
  2. import (
  3. "encoding/hex"
  4. "fmt"
  5. "io/ioutil"
  6. "math/rand"
  7. "testing"
  8. "github.com/stretchr/testify/assert"
  9. "github.com/tendermint/tendermint/p2p"
  10. cmn "github.com/tendermint/tmlibs/common"
  11. "github.com/tendermint/tmlibs/log"
  12. )
  13. func createTempFileName(prefix string) string {
  14. f, err := ioutil.TempFile("", prefix)
  15. if err != nil {
  16. panic(err)
  17. }
  18. fname := f.Name()
  19. err = f.Close()
  20. if err != nil {
  21. panic(err)
  22. }
  23. return fname
  24. }
  25. func TestAddrBookPickAddress(t *testing.T) {
  26. assert := assert.New(t)
  27. fname := createTempFileName("addrbook_test")
  28. // 0 addresses
  29. book := NewAddrBook(fname, true)
  30. book.SetLogger(log.TestingLogger())
  31. assert.Zero(book.Size())
  32. addr := book.PickAddress(50)
  33. assert.Nil(addr, "expected no address")
  34. randAddrs := randNetAddressPairs(t, 1)
  35. addrSrc := randAddrs[0]
  36. book.AddAddress(addrSrc.addr, addrSrc.src)
  37. // pick an address when we only have new address
  38. addr = book.PickAddress(0)
  39. assert.NotNil(addr, "expected an address")
  40. addr = book.PickAddress(50)
  41. assert.NotNil(addr, "expected an address")
  42. addr = book.PickAddress(100)
  43. assert.NotNil(addr, "expected an address")
  44. // pick an address when we only have old address
  45. book.MarkGood(addrSrc.addr)
  46. addr = book.PickAddress(0)
  47. assert.NotNil(addr, "expected an address")
  48. addr = book.PickAddress(50)
  49. assert.NotNil(addr, "expected an address")
  50. // in this case, nNew==0 but we biased 100% to new, so we return nil
  51. addr = book.PickAddress(100)
  52. assert.Nil(addr, "did not expected an address")
  53. }
  54. func TestAddrBookSaveLoad(t *testing.T) {
  55. fname := createTempFileName("addrbook_test")
  56. // 0 addresses
  57. book := NewAddrBook(fname, true)
  58. book.SetLogger(log.TestingLogger())
  59. book.saveToFile(fname)
  60. book = NewAddrBook(fname, true)
  61. book.SetLogger(log.TestingLogger())
  62. book.loadFromFile(fname)
  63. assert.Zero(t, book.Size())
  64. // 100 addresses
  65. randAddrs := randNetAddressPairs(t, 100)
  66. for _, addrSrc := range randAddrs {
  67. book.AddAddress(addrSrc.addr, addrSrc.src)
  68. }
  69. assert.Equal(t, 100, book.Size())
  70. book.saveToFile(fname)
  71. book = NewAddrBook(fname, true)
  72. book.SetLogger(log.TestingLogger())
  73. book.loadFromFile(fname)
  74. assert.Equal(t, 100, book.Size())
  75. }
  76. func TestAddrBookLookup(t *testing.T) {
  77. fname := createTempFileName("addrbook_test")
  78. randAddrs := randNetAddressPairs(t, 100)
  79. book := NewAddrBook(fname, true)
  80. book.SetLogger(log.TestingLogger())
  81. for _, addrSrc := range randAddrs {
  82. addr := addrSrc.addr
  83. src := addrSrc.src
  84. book.AddAddress(addr, src)
  85. ka := book.addrLookup[addr.ID]
  86. assert.NotNil(t, ka, "Expected to find KnownAddress %v but wasn't there.", addr)
  87. if !(ka.Addr.Equals(addr) && ka.Src.Equals(src)) {
  88. t.Fatalf("KnownAddress doesn't match addr & src")
  89. }
  90. }
  91. }
  92. func TestAddrBookPromoteToOld(t *testing.T) {
  93. assert := assert.New(t)
  94. fname := createTempFileName("addrbook_test")
  95. randAddrs := randNetAddressPairs(t, 100)
  96. book := NewAddrBook(fname, true)
  97. book.SetLogger(log.TestingLogger())
  98. for _, addrSrc := range randAddrs {
  99. book.AddAddress(addrSrc.addr, addrSrc.src)
  100. }
  101. // Attempt all addresses.
  102. for _, addrSrc := range randAddrs {
  103. book.MarkAttempt(addrSrc.addr)
  104. }
  105. // Promote half of them
  106. for i, addrSrc := range randAddrs {
  107. if i%2 == 0 {
  108. book.MarkGood(addrSrc.addr)
  109. }
  110. }
  111. // TODO: do more testing :)
  112. selection := book.GetSelection()
  113. t.Logf("selection: %v", selection)
  114. if len(selection) > book.Size() {
  115. t.Errorf("selection could not be bigger than the book")
  116. }
  117. assert.Equal(book.Size(), 100, "expecting book size to be 100")
  118. }
  119. func TestAddrBookHandlesDuplicates(t *testing.T) {
  120. fname := createTempFileName("addrbook_test")
  121. book := NewAddrBook(fname, true)
  122. book.SetLogger(log.TestingLogger())
  123. randAddrs := randNetAddressPairs(t, 100)
  124. differentSrc := randIPv4Address(t)
  125. for _, addrSrc := range randAddrs {
  126. book.AddAddress(addrSrc.addr, addrSrc.src)
  127. book.AddAddress(addrSrc.addr, addrSrc.src) // duplicate
  128. book.AddAddress(addrSrc.addr, differentSrc) // different src
  129. }
  130. assert.Equal(t, 100, book.Size())
  131. }
  132. type netAddressPair struct {
  133. addr *p2p.NetAddress
  134. src *p2p.NetAddress
  135. }
  136. func randNetAddressPairs(t *testing.T, n int) []netAddressPair {
  137. randAddrs := make([]netAddressPair, n)
  138. for i := 0; i < n; i++ {
  139. randAddrs[i] = netAddressPair{addr: randIPv4Address(t), src: randIPv4Address(t)}
  140. }
  141. return randAddrs
  142. }
  143. func randIPv4Address(t *testing.T) *p2p.NetAddress {
  144. for {
  145. ip := fmt.Sprintf("%v.%v.%v.%v",
  146. rand.Intn(254)+1,
  147. rand.Intn(255),
  148. rand.Intn(255),
  149. rand.Intn(255),
  150. )
  151. port := rand.Intn(65535-1) + 1
  152. id := p2p.ID(hex.EncodeToString(cmn.RandBytes(p2p.IDByteLength)))
  153. idAddr := p2p.IDAddressString(id, fmt.Sprintf("%v:%v", ip, port))
  154. addr, err := p2p.NewNetAddressString(idAddr)
  155. assert.Nil(t, err, "error generating rand network address")
  156. if addr.Routable() {
  157. return addr
  158. }
  159. }
  160. }
  161. func TestAddrBookRemoveAddress(t *testing.T) {
  162. fname := createTempFileName("addrbook_test")
  163. book := NewAddrBook(fname, true)
  164. book.SetLogger(log.TestingLogger())
  165. addr := randIPv4Address(t)
  166. book.AddAddress(addr, addr)
  167. assert.Equal(t, 1, book.Size())
  168. book.RemoveAddress(addr)
  169. assert.Equal(t, 0, book.Size())
  170. nonExistingAddr := randIPv4Address(t)
  171. book.RemoveAddress(nonExistingAddr)
  172. assert.Equal(t, 0, book.Size())
  173. }