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.

150 lines
3.1 KiB

9 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
9 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
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
  1. package p2p
  2. import (
  3. "fmt"
  4. "io/ioutil"
  5. "math/rand"
  6. "testing"
  7. "github.com/stretchr/testify/assert"
  8. )
  9. func createTempFileName(prefix string) string {
  10. f, err := ioutil.TempFile("", prefix)
  11. if err != nil {
  12. panic(err)
  13. }
  14. fname := f.Name()
  15. err = f.Close()
  16. if err != nil {
  17. panic(err)
  18. }
  19. return fname
  20. }
  21. func TestAddrBookSaveLoad(t *testing.T) {
  22. fname := createTempFileName("addrbook_test")
  23. // 0 addresses
  24. book := NewAddrBook(fname, true)
  25. book.saveToFile(fname)
  26. book = NewAddrBook(fname, true)
  27. book.loadFromFile(fname)
  28. assert.Zero(t, book.Size())
  29. // 100 addresses
  30. randAddrs := randNetAddressPairs(t, 100)
  31. for _, addrSrc := range randAddrs {
  32. book.AddAddress(addrSrc.addr, addrSrc.src)
  33. }
  34. assert.Equal(t, 100, book.Size())
  35. book.saveToFile(fname)
  36. book = NewAddrBook(fname, true)
  37. book.loadFromFile(fname)
  38. assert.Equal(t, 100, book.Size())
  39. }
  40. func TestAddrBookLookup(t *testing.T) {
  41. fname := createTempFileName("addrbook_test")
  42. randAddrs := randNetAddressPairs(t, 100)
  43. book := NewAddrBook(fname, true)
  44. for _, addrSrc := range randAddrs {
  45. addr := addrSrc.addr
  46. src := addrSrc.src
  47. book.AddAddress(addr, src)
  48. ka := book.addrLookup[addr.String()]
  49. assert.NotNil(t, ka, "Expected to find KnownAddress %v but wasn't there.", addr)
  50. if !(ka.Addr.Equals(addr) && ka.Src.Equals(src)) {
  51. t.Fatalf("KnownAddress doesn't match addr & src")
  52. }
  53. }
  54. }
  55. func TestAddrBookPromoteToOld(t *testing.T) {
  56. fname := createTempFileName("addrbook_test")
  57. randAddrs := randNetAddressPairs(t, 100)
  58. book := NewAddrBook(fname, true)
  59. for _, addrSrc := range randAddrs {
  60. book.AddAddress(addrSrc.addr, addrSrc.src)
  61. }
  62. // Attempt all addresses.
  63. for _, addrSrc := range randAddrs {
  64. book.MarkAttempt(addrSrc.addr)
  65. }
  66. // Promote half of them
  67. for i, addrSrc := range randAddrs {
  68. if i%2 == 0 {
  69. book.MarkGood(addrSrc.addr)
  70. }
  71. }
  72. // TODO: do more testing :)
  73. selection := book.GetSelection()
  74. t.Logf("selection: %v", selection)
  75. if len(selection) > book.Size() {
  76. t.Errorf("selection could not be bigger than the book")
  77. }
  78. }
  79. func TestAddrBookHandlesDuplicates(t *testing.T) {
  80. fname := createTempFileName("addrbook_test")
  81. book := NewAddrBook(fname, true)
  82. randAddrs := randNetAddressPairs(t, 100)
  83. differentSrc := randIPv4Address(t)
  84. for _, addrSrc := range randAddrs {
  85. book.AddAddress(addrSrc.addr, addrSrc.src)
  86. book.AddAddress(addrSrc.addr, addrSrc.src) // duplicate
  87. book.AddAddress(addrSrc.addr, differentSrc) // different src
  88. }
  89. assert.Equal(t, 100, book.Size())
  90. }
  91. type netAddressPair struct {
  92. addr *NetAddress
  93. src *NetAddress
  94. }
  95. func randNetAddressPairs(t *testing.T, n int) []netAddressPair {
  96. randAddrs := make([]netAddressPair, n)
  97. for i := 0; i < n; i++ {
  98. randAddrs[i] = netAddressPair{addr: randIPv4Address(t), src: randIPv4Address(t)}
  99. }
  100. return randAddrs
  101. }
  102. func randIPv4Address(t *testing.T) *NetAddress {
  103. for {
  104. ip := fmt.Sprintf("%v.%v.%v.%v",
  105. rand.Intn(254)+1,
  106. rand.Intn(255),
  107. rand.Intn(255),
  108. rand.Intn(255),
  109. )
  110. port := rand.Intn(65535-1) + 1
  111. addr, err := NewNetAddressString(fmt.Sprintf("%v:%v", ip, port))
  112. assert.Nil(t, err, "error generating rand network address")
  113. if addr.Routable() {
  114. return addr
  115. }
  116. }
  117. }