blocklist_test.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. hit := net.ParseIP("0.0.0.0")
  39. miss := net.ParseIP("255.255.255.255")
  40. sources := []string{"source1", "source2", "source3", "source4", "source4"}
  41. subjects := []string{"subject1", "subject2", "subject3", "subject4", "subject4"}
  42. hitPresent := []int{0, 1}
  43. entriesPerSource := 100000
  44. file, err := os.Create(filename)
  45. if err != nil {
  46. t.Fatalf("Open failed: %s", err)
  47. }
  48. defer file.Close()
  49. for i := 0; i < len(sources); i++ {
  50. _, err := fmt.Fprintf(file, "# comment\n# comment\n# comment\n")
  51. if err != nil {
  52. t.Fatalf("Fprintf failed: %s", err)
  53. }
  54. for j := 0; j < entriesPerSource; j++ {
  55. var IPAddress string
  56. if j == entriesPerSource/2 && common.ContainsInt(hitPresent, i) {
  57. IPAddress = hit.String()
  58. } else {
  59. IPAddress = fmt.Sprintf(
  60. "%d.%d.%d.%d",
  61. prng.Range(1, 254), prng.Range(1, 254),
  62. prng.Range(1, 254), prng.Range(1, 254))
  63. }
  64. _, err := fmt.Fprintf(file, "%s,%s,%s\n",
  65. IPAddress, sources[i], subjects[i])
  66. if err != nil {
  67. t.Fatalf("Fprintf failed: %s", err)
  68. }
  69. }
  70. }
  71. file.Close()
  72. b, err := NewBlocklist(filename)
  73. if err != nil {
  74. t.Fatalf("NewBlocklist failed: %s", err)
  75. }
  76. tags := b.Lookup(hit)
  77. if tags == nil {
  78. t.Fatalf("unexpected miss")
  79. }
  80. if len(tags) != len(hitPresent) {
  81. t.Fatalf("unexpected hit tag count")
  82. }
  83. for _, tag := range tags {
  84. sourceFound := false
  85. subjectFound := false
  86. for _, i := range hitPresent {
  87. if tag.Source == sources[i] {
  88. sourceFound = true
  89. }
  90. if tag.Subject == subjects[i] {
  91. subjectFound = true
  92. }
  93. }
  94. if !sourceFound || !subjectFound {
  95. t.Fatalf("unexpected hit tag")
  96. }
  97. }
  98. if b.Lookup(miss) != nil {
  99. t.Fatalf("unexpected hit")
  100. }
  101. numLookups := 10
  102. numIterations := 1000000
  103. lookups := make([]net.IP, numLookups)
  104. for i := 0; i < numLookups; i++ {
  105. lookups[i] = net.ParseIP(
  106. fmt.Sprintf(
  107. "%d.%d.%d.%d",
  108. prng.Range(1, 254), prng.Range(1, 254),
  109. prng.Range(1, 254), prng.Range(1, 254)))
  110. }
  111. start := time.Now()
  112. for i := 0; i < numIterations; i++ {
  113. _ = b.Lookup(lookups[i%numLookups])
  114. }
  115. t.Logf(
  116. "average time per lookup in %d entries: %s",
  117. len(sources)*entriesPerSource,
  118. time.Since(start)/time.Duration(numIterations))
  119. }