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.

231 lines
5.4 KiB

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