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.

161 lines
3.2 KiB

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