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.

213 lines
4.9 KiB

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