blocklist_test.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /*
  2. * Copyright (c) 2019, Psiphon Inc.
  3. * All rights reserved.
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. */
  19. package server
  20. import (
  21. "fmt"
  22. "io/ioutil"
  23. "net"
  24. "os"
  25. "path/filepath"
  26. "testing"
  27. "time"
  28. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common"
  29. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/prng"
  30. )
  31. func TestBlocklist(t *testing.T) {
  32. testDataDirName, err := ioutil.TempDir("", "psiphon-blocklist-test")
  33. if err != nil {
  34. t.Fatalf("TempDir failed: %s", err)
  35. }
  36. defer os.RemoveAll(testDataDirName)
  37. filename := filepath.Join(testDataDirName, "blocklist")
  38. hitIPv4 := net.ParseIP("0.0.0.0")
  39. hitIPv6 := net.ParseIP("2001:db8:f75c::0951:58bc:ef22")
  40. hitDomain := "example.org"
  41. missIPv4 := net.ParseIP("255.255.255.255")
  42. sources := []string{"source1", "source2", "source3", "source4", "source4"}
  43. subjects := []string{"subject1", "subject2", "subject3", "subject4", "subject4"}
  44. hitPresent := []int{0, 1}
  45. entriesPerSource := 100000
  46. file, err := os.Create(filename)
  47. if err != nil {
  48. t.Fatalf("Open failed: %s", err)
  49. }
  50. defer file.Close()
  51. for i := 0; i < len(sources); i++ {
  52. _, err := fmt.Fprintf(file, "# comment\n# comment\n# comment\n")
  53. if err != nil {
  54. t.Fatalf("Fprintf failed: %s", err)
  55. }
  56. hitIPv4Index := -1
  57. hitIPv6Index := -1
  58. hitDomainIndex := -1
  59. if common.ContainsInt(hitPresent, i) {
  60. indices := prng.Perm(entriesPerSource)
  61. hitIPv4Index = indices[0] - 1
  62. hitIPv6Index = indices[1] - 1
  63. hitDomainIndex = indices[2] - 1
  64. }
  65. for j := 0; j < entriesPerSource; j++ {
  66. var address string
  67. if j == hitIPv4Index {
  68. address = hitIPv4.String()
  69. } else if j == hitIPv6Index {
  70. address = hitIPv6.String()
  71. } else if j == hitDomainIndex {
  72. address = hitDomain
  73. } else {
  74. address = fmt.Sprintf(
  75. "%d.%d.%d.%d",
  76. prng.Range(1, 254), prng.Range(1, 254),
  77. prng.Range(1, 254), prng.Range(1, 254))
  78. }
  79. _, err := fmt.Fprintf(file, "%s,%s,%s\n",
  80. address, sources[i], subjects[i])
  81. if err != nil {
  82. t.Fatalf("Fprintf failed: %s", err)
  83. }
  84. }
  85. }
  86. file.Close()
  87. b, err := NewBlocklist(filename)
  88. if err != nil {
  89. t.Fatalf("NewBlocklist failed: %s", err)
  90. }
  91. for _, hitIP := range []net.IP{hitIPv4, hitIPv6} {
  92. tags := b.LookupIP(hitIP)
  93. if tags == nil {
  94. t.Fatalf("unexpected miss")
  95. }
  96. if len(tags) != len(hitPresent) {
  97. t.Fatalf("unexpected hit tag count")
  98. }
  99. for _, tag := range tags {
  100. sourceFound := false
  101. subjectFound := false
  102. for _, i := range hitPresent {
  103. if tag.Source == sources[i] {
  104. sourceFound = true
  105. }
  106. if tag.Subject == subjects[i] {
  107. subjectFound = true
  108. }
  109. }
  110. if !sourceFound || !subjectFound {
  111. t.Fatalf("unexpected hit tag")
  112. }
  113. }
  114. }
  115. tags := b.LookupDomain(hitDomain)
  116. if tags == nil {
  117. t.Fatalf("unexpected miss")
  118. }
  119. if len(tags) != len(hitPresent) {
  120. t.Fatalf("unexpected hit tag count")
  121. }
  122. if b.LookupIP(missIPv4) != nil {
  123. t.Fatalf("unexpected hit")
  124. }
  125. numLookups := 10
  126. numIterations := 1000000
  127. lookups := make([]net.IP, numLookups)
  128. for i := 0; i < numLookups; i++ {
  129. lookups[i] = net.ParseIP(
  130. fmt.Sprintf(
  131. "%d.%d.%d.%d",
  132. prng.Range(1, 254), prng.Range(1, 254),
  133. prng.Range(1, 254), prng.Range(1, 254)))
  134. }
  135. start := time.Now()
  136. for i := 0; i < numIterations; i++ {
  137. _ = b.LookupIP(lookups[i%numLookups])
  138. }
  139. t.Logf(
  140. "average time per lookup in %d entries: %s",
  141. len(sources)*entriesPerSource,
  142. time.Since(start)/time.Duration(numIterations))
  143. }