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.

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