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.

214 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. fname := createTempFileName("addrbook_test")
  91. randAddrs := randNetAddressPairs(t, 100)
  92. book := NewAddrBook(fname, true)
  93. book.SetLogger(log.TestingLogger())
  94. for _, addrSrc := range randAddrs {
  95. book.AddAddress(addrSrc.addr, addrSrc.src)
  96. }
  97. // Attempt all addresses.
  98. for _, addrSrc := range randAddrs {
  99. book.MarkAttempt(addrSrc.addr)
  100. }
  101. // Promote half of them
  102. for i, addrSrc := range randAddrs {
  103. if i%2 == 0 {
  104. book.MarkGood(addrSrc.addr)
  105. }
  106. }
  107. // TODO: do more testing :)
  108. selection := book.GetSelection()
  109. t.Logf("selection: %v", selection)
  110. if len(selection) > book.Size() {
  111. t.Errorf("selection could not be bigger than the book")
  112. }
  113. if book.Size() != 100 {
  114. t.Errorf("Size is not 100. Got %v", book.Size())
  115. }
  116. }
  117. func TestAddrBookHandlesDuplicates(t *testing.T) {
  118. fname := createTempFileName("addrbook_test")
  119. book := NewAddrBook(fname, true)
  120. book.SetLogger(log.TestingLogger())
  121. randAddrs := randNetAddressPairs(t, 100)
  122. differentSrc := randIPv4Address(t)
  123. for _, addrSrc := range randAddrs {
  124. book.AddAddress(addrSrc.addr, addrSrc.src)
  125. book.AddAddress(addrSrc.addr, addrSrc.src) // duplicate
  126. book.AddAddress(addrSrc.addr, differentSrc) // different src
  127. }
  128. assert.Equal(t, 100, book.Size())
  129. }
  130. type netAddressPair struct {
  131. addr *NetAddress
  132. src *NetAddress
  133. }
  134. func randNetAddressPairs(t *testing.T, n int) []netAddressPair {
  135. randAddrs := make([]netAddressPair, n)
  136. for i := 0; i < n; i++ {
  137. randAddrs[i] = netAddressPair{addr: randIPv4Address(t), src: randIPv4Address(t)}
  138. }
  139. return randAddrs
  140. }
  141. func randIPv4Address(t *testing.T) *NetAddress {
  142. for {
  143. ip := fmt.Sprintf("%v.%v.%v.%v",
  144. rand.Intn(254)+1,
  145. rand.Intn(255),
  146. rand.Intn(255),
  147. rand.Intn(255),
  148. )
  149. port := rand.Intn(65535-1) + 1
  150. addr, err := NewNetAddressString(fmt.Sprintf("%v:%v", ip, port))
  151. assert.Nil(t, err, "error generating rand network address")
  152. if addr.Routable() {
  153. return addr
  154. }
  155. }
  156. }
  157. func TestAddrBookRemoveAddress(t *testing.T) {
  158. fname := createTempFileName("addrbook_test")
  159. book := NewAddrBook(fname, true)
  160. book.SetLogger(log.TestingLogger())
  161. addr := randIPv4Address(t)
  162. book.AddAddress(addr, addr)
  163. assert.Equal(t, 1, book.Size())
  164. book.RemoveAddress(addr)
  165. assert.Equal(t, 0, book.Size())
  166. nonExistingAddr := randIPv4Address(t)
  167. book.RemoveAddress(nonExistingAddr)
  168. assert.Equal(t, 0, book.Size())
  169. }