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.

166 lines
3.4 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
  1. package p2p
  2. import (
  3. "fmt"
  4. "io/ioutil"
  5. "math/rand"
  6. "testing"
  7. "github.com/stretchr/testify/assert"
  8. )
  9. const addrBookStrict = true
  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 TestEmpty(t *testing.T) {
  23. fname := createTempFileName("addrbook_test")
  24. // t.Logf("New tempfile name: %v", fname)
  25. // Save an empty book & load it
  26. book := NewAddrBook(fname, addrBookStrict)
  27. book.saveToFile(fname)
  28. book = NewAddrBook(fname, addrBookStrict)
  29. book.loadFromFile(fname)
  30. if book.Size() != 0 {
  31. t.Errorf("Expected 0 addresses, found %v", book.Size())
  32. }
  33. }
  34. func randIPv4Address(t *testing.T) *NetAddress {
  35. for {
  36. ip := fmt.Sprintf("%v.%v.%v.%v",
  37. rand.Intn(254)+1,
  38. rand.Intn(255),
  39. rand.Intn(255),
  40. rand.Intn(255),
  41. )
  42. port := rand.Intn(65535-1) + 1
  43. addr, err := NewNetAddressString(fmt.Sprintf("%v:%v", ip, port))
  44. assert.Nil(t, err, "error generating rand network address")
  45. if addr.Routable() {
  46. return addr
  47. }
  48. }
  49. }
  50. func TestSaveAddresses(t *testing.T) {
  51. fname := createTempFileName("addrbook_test")
  52. //t.Logf("New tempfile name: %v", fname)
  53. // Create some random addresses
  54. randAddrs := []struct {
  55. addr *NetAddress
  56. src *NetAddress
  57. }{}
  58. for i := 0; i < 100; i++ {
  59. addr := randIPv4Address(t)
  60. src := randIPv4Address(t)
  61. randAddrs = append(randAddrs, struct {
  62. addr *NetAddress
  63. src *NetAddress
  64. }{
  65. addr: addr,
  66. src: src,
  67. })
  68. }
  69. // Create the book & populate & save
  70. book := NewAddrBook(fname, addrBookStrict)
  71. for _, addrSrc := range randAddrs {
  72. book.AddAddress(addrSrc.addr, addrSrc.src)
  73. }
  74. if book.Size() != 100 {
  75. t.Errorf("Expected 100 addresses, found %v", book.Size())
  76. }
  77. book.saveToFile(fname)
  78. // Reload the book
  79. book = NewAddrBook(fname, addrBookStrict)
  80. book.loadFromFile(fname)
  81. // Test ...
  82. if book.Size() != 100 {
  83. t.Errorf("Expected 100 addresses, found %v", book.Size())
  84. }
  85. for _, addrSrc := range randAddrs {
  86. addr := addrSrc.addr
  87. src := addrSrc.src
  88. ka := book.addrLookup[addr.String()]
  89. if ka == nil {
  90. t.Fatalf("Expected to find KnownAddress %v but wasn't there.", addr)
  91. }
  92. if !(ka.Addr.Equals(addr) && ka.Src.Equals(src)) {
  93. t.Fatalf("KnownAddress doesn't match addr & src")
  94. }
  95. }
  96. }
  97. func TestPromoteToOld(t *testing.T) {
  98. fname := createTempFileName("addrbook_test")
  99. t.Logf("New tempfile name: %v", fname)
  100. // Create some random addresses
  101. randAddrs := []struct {
  102. addr *NetAddress
  103. src *NetAddress
  104. }{}
  105. for i := 0; i < 100; i++ {
  106. addr := randIPv4Address(t)
  107. src := randIPv4Address(t)
  108. randAddrs = append(randAddrs, struct {
  109. addr *NetAddress
  110. src *NetAddress
  111. }{
  112. addr: addr,
  113. src: src,
  114. })
  115. }
  116. // Create the book & populate & save
  117. book := NewAddrBook(fname, addrBookStrict)
  118. for _, addrSrc := range randAddrs {
  119. book.AddAddress(addrSrc.addr, addrSrc.src)
  120. }
  121. // Attempt all addresses.
  122. for _, addrSrc := range randAddrs {
  123. book.MarkAttempt(addrSrc.addr)
  124. }
  125. // Promote half of them
  126. for i, addrSrc := range randAddrs {
  127. if i%2 == 0 {
  128. book.MarkGood(addrSrc.addr)
  129. }
  130. }
  131. book.saveToFile(fname)
  132. // Reload the book
  133. book = NewAddrBook(fname, addrBookStrict)
  134. book.loadFromFile(fname)
  135. // Test ...
  136. if book.Size() != 100 {
  137. t.Errorf("Expected 100 addresses, found %v", book.Size())
  138. }
  139. // TODO: do more testing :)
  140. selection := book.GetSelection()
  141. t.Logf("selection: %v", selection)
  142. }