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.

174 lines
3.8 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 TestAddrBookSaveLoad(t *testing.T) {
  23. fname := createTempFileName("addrbook_test")
  24. // 0 addresses
  25. book := NewAddrBook(fname, true)
  26. book.SetLogger(log.TestingLogger())
  27. book.saveToFile(fname)
  28. book = NewAddrBook(fname, true)
  29. book.SetLogger(log.TestingLogger())
  30. book.loadFromFile(fname)
  31. assert.Zero(t, book.Size())
  32. // 100 addresses
  33. randAddrs := randNetAddressPairs(t, 100)
  34. for _, addrSrc := range randAddrs {
  35. book.AddAddress(addrSrc.addr, addrSrc.src)
  36. }
  37. assert.Equal(t, 100, book.Size())
  38. book.saveToFile(fname)
  39. book = NewAddrBook(fname, true)
  40. book.SetLogger(log.TestingLogger())
  41. book.loadFromFile(fname)
  42. assert.Equal(t, 100, book.Size())
  43. }
  44. func TestAddrBookLookup(t *testing.T) {
  45. fname := createTempFileName("addrbook_test")
  46. randAddrs := randNetAddressPairs(t, 100)
  47. book := NewAddrBook(fname, true)
  48. book.SetLogger(log.TestingLogger())
  49. for _, addrSrc := range randAddrs {
  50. addr := addrSrc.addr
  51. src := addrSrc.src
  52. book.AddAddress(addr, src)
  53. ka := book.addrLookup[addr.String()]
  54. assert.NotNil(t, ka, "Expected to find KnownAddress %v but wasn't there.", addr)
  55. if !(ka.Addr.Equals(addr) && ka.Src.Equals(src)) {
  56. t.Fatalf("KnownAddress doesn't match addr & src")
  57. }
  58. }
  59. }
  60. func TestAddrBookPromoteToOld(t *testing.T) {
  61. fname := createTempFileName("addrbook_test")
  62. randAddrs := randNetAddressPairs(t, 100)
  63. book := NewAddrBook(fname, true)
  64. book.SetLogger(log.TestingLogger())
  65. for _, addrSrc := range randAddrs {
  66. book.AddAddress(addrSrc.addr, addrSrc.src)
  67. }
  68. // Attempt all addresses.
  69. for _, addrSrc := range randAddrs {
  70. book.MarkAttempt(addrSrc.addr)
  71. }
  72. // Promote half of them
  73. for i, addrSrc := range randAddrs {
  74. if i%2 == 0 {
  75. book.MarkGood(addrSrc.addr)
  76. }
  77. }
  78. // TODO: do more testing :)
  79. selection := book.GetSelection()
  80. t.Logf("selection: %v", selection)
  81. if len(selection) > book.Size() {
  82. t.Errorf("selection could not be bigger than the book")
  83. }
  84. }
  85. func TestAddrBookHandlesDuplicates(t *testing.T) {
  86. fname := createTempFileName("addrbook_test")
  87. book := NewAddrBook(fname, true)
  88. book.SetLogger(log.TestingLogger())
  89. randAddrs := randNetAddressPairs(t, 100)
  90. differentSrc := randIPv4Address(t)
  91. for _, addrSrc := range randAddrs {
  92. book.AddAddress(addrSrc.addr, addrSrc.src)
  93. book.AddAddress(addrSrc.addr, addrSrc.src) // duplicate
  94. book.AddAddress(addrSrc.addr, differentSrc) // different src
  95. }
  96. assert.Equal(t, 100, book.Size())
  97. }
  98. type netAddressPair struct {
  99. addr *NetAddress
  100. src *NetAddress
  101. }
  102. func randNetAddressPairs(t *testing.T, n int) []netAddressPair {
  103. randAddrs := make([]netAddressPair, n)
  104. for i := 0; i < n; i++ {
  105. randAddrs[i] = netAddressPair{addr: randIPv4Address(t), src: randIPv4Address(t)}
  106. }
  107. return randAddrs
  108. }
  109. func randIPv4Address(t *testing.T) *NetAddress {
  110. for {
  111. ip := fmt.Sprintf("%v.%v.%v.%v",
  112. rand.Intn(254)+1,
  113. rand.Intn(255),
  114. rand.Intn(255),
  115. rand.Intn(255),
  116. )
  117. port := rand.Intn(65535-1) + 1
  118. addr, err := NewNetAddressString(fmt.Sprintf("%v:%v", ip, port))
  119. assert.Nil(t, err, "error generating rand network address")
  120. if addr.Routable() {
  121. return addr
  122. }
  123. }
  124. }
  125. func TestAddrBookRemoveAddress(t *testing.T) {
  126. fname := createTempFileName("addrbook_test")
  127. book := NewAddrBook(fname, true)
  128. book.SetLogger(log.TestingLogger())
  129. addr := randIPv4Address(t)
  130. book.AddAddress(addr, addr)
  131. assert.Equal(t, 1, book.Size())
  132. book.RemoveAddress(addr)
  133. assert.Equal(t, 0, book.Size())
  134. nonExistingAddr := randIPv4Address(t)
  135. book.RemoveAddress(nonExistingAddr)
  136. assert.Equal(t, 0, book.Size())
  137. }