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.

216 lines
5.0 KiB

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