classic_test.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*
  2. * Copyright (c) 2024, 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 discovery
  20. import (
  21. "strconv"
  22. "testing"
  23. "time"
  24. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/server/psinet"
  25. )
  26. func TestDiscoveryBuckets(t *testing.T) {
  27. checkBuckets := func(buckets [][]*psinet.DiscoveryServer, expectedServerEntries [][]int) {
  28. if len(buckets) != len(expectedServerEntries) {
  29. t.Errorf(
  30. "unexpected bucket count: got %d expected %d",
  31. len(buckets), len(expectedServerEntries))
  32. return
  33. }
  34. for i := 0; i < len(buckets); i++ {
  35. if len(buckets[i]) != len(expectedServerEntries[i]) {
  36. t.Errorf(
  37. "unexpected bucket %d size: got %d expected %d",
  38. i, len(buckets[i]), len(expectedServerEntries[i]))
  39. return
  40. }
  41. for j := 0; j < len(buckets[i]); j++ {
  42. expectedServerEntry := strconv.Itoa(expectedServerEntries[i][j])
  43. if buckets[i][j].EncodedServerEntry != expectedServerEntry {
  44. t.Errorf(
  45. "unexpected bucket %d item %d: got %s expected %s",
  46. i, j, buckets[i][j].EncodedServerEntry, expectedServerEntry)
  47. return
  48. }
  49. }
  50. }
  51. }
  52. // Partition test cases from:
  53. // http://stackoverflow.com/questions/2659900/python-slicing-a-list-into-n-nearly-equal-length-partitions
  54. servers := make([]*psinet.DiscoveryServer, 0)
  55. for i := 0; i < 105; i++ {
  56. servers = append(servers, &psinet.DiscoveryServer{
  57. EncodedServerEntry: strconv.Itoa(i),
  58. DiscoveryDateRange: []time.Time{{}, time.Now()},
  59. })
  60. }
  61. t.Run("5 servers, 5 buckets", func(t *testing.T) {
  62. checkBuckets(
  63. bucketizeServerList(servers[0:5], 5),
  64. [][]int{{0}, {1}, {2}, {3}, {4}})
  65. })
  66. t.Run("5 servers, 2 buckets", func(t *testing.T) {
  67. checkBuckets(
  68. bucketizeServerList(servers[0:5], 2),
  69. [][]int{{0, 1, 2}, {3, 4}})
  70. })
  71. t.Run("5 servers, 3 buckets", func(t *testing.T) {
  72. checkBuckets(
  73. bucketizeServerList(servers[0:5], 3),
  74. [][]int{{0, 1}, {2}, {3, 4}})
  75. })
  76. t.Run("105 servers, 10 buckets", func(t *testing.T) {
  77. checkBuckets(
  78. bucketizeServerList(servers, 10),
  79. [][]int{
  80. {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10},
  81. {11, 12, 13, 14, 15, 16, 17, 18, 19, 20},
  82. {21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31},
  83. {32, 33, 34, 35, 36, 37, 38, 39, 40, 41},
  84. {42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52},
  85. {53, 54, 55, 56, 57, 58, 59, 60, 61, 62},
  86. {63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73},
  87. {74, 75, 76, 77, 78, 79, 80, 81, 82, 83},
  88. {84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94},
  89. {95, 96, 97, 98, 99, 100, 101, 102, 103, 104},
  90. })
  91. })
  92. t.Run("repeatedly discover with fixed IP address", func(t *testing.T) {
  93. // For a IP address values, only one bucket should be used; with enough
  94. // iterations, all and only the items in a single bucket should be discovered.
  95. discoveredServers := make(map[string]bool)
  96. // discoveryValue is derived from the client's IP address and indexes the bucket;
  97. // a value of 0 always maps to the first bucket.
  98. discoveryValue := 0
  99. for i := 0; i < 1000; i++ {
  100. buckets := bucketizeServerList(servers, calculateBucketCount(len(servers)))
  101. for _, server := range selectServers(buckets, i*int(time.Hour/time.Second), discoveryValue, time.Time{}) {
  102. discoveredServers[server.EncodedServerEntry] = true
  103. }
  104. }
  105. bucketCount := calculateBucketCount(len(servers))
  106. buckets := bucketizeServerList(servers, bucketCount)
  107. if len(buckets[0]) != len(discoveredServers) {
  108. t.Errorf(
  109. "unexpected discovered server count: got %d expected %d",
  110. len(discoveredServers), len(buckets[0]))
  111. return
  112. }
  113. for _, bucketServer := range buckets[0] {
  114. if _, ok := discoveredServers[bucketServer.EncodedServerEntry]; !ok {
  115. t.Errorf("unexpected missing discovery server: %s", bucketServer.EncodedServerEntry)
  116. return
  117. }
  118. }
  119. })
  120. }