transferURLs_test.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /*
  2. * Copyright (c) 2018, 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 parameters
  20. import (
  21. "encoding/base64"
  22. "testing"
  23. )
  24. func TestTransferURLs(t *testing.T) {
  25. decodedA := "a.example.com"
  26. encodedA := base64.StdEncoding.EncodeToString([]byte(decodedA))
  27. encodedB := base64.StdEncoding.EncodeToString([]byte("b.example.com"))
  28. encodedC := base64.StdEncoding.EncodeToString([]byte("c.example.com"))
  29. testCases := []struct {
  30. description string
  31. transferURLs TransferURLs
  32. attempts int
  33. expectedValid bool
  34. expectedCanonicalURL string
  35. expectedDistinctSelections int
  36. }{
  37. {
  38. "missing OnlyAfterAttempts = 0",
  39. TransferURLs{
  40. {
  41. URL: encodedA,
  42. OnlyAfterAttempts: 1,
  43. },
  44. },
  45. 1,
  46. false,
  47. decodedA,
  48. 0,
  49. },
  50. {
  51. "single URL, multiple attempts",
  52. TransferURLs{
  53. {
  54. URL: encodedA,
  55. OnlyAfterAttempts: 0,
  56. },
  57. },
  58. 2,
  59. true,
  60. decodedA,
  61. 1,
  62. },
  63. {
  64. "multiple URLs, single attempt",
  65. TransferURLs{
  66. {
  67. URL: encodedA,
  68. OnlyAfterAttempts: 0,
  69. },
  70. {
  71. URL: encodedB,
  72. OnlyAfterAttempts: 1,
  73. },
  74. {
  75. URL: encodedC,
  76. OnlyAfterAttempts: 1,
  77. },
  78. },
  79. 1,
  80. true,
  81. decodedA,
  82. 1,
  83. },
  84. {
  85. "multiple URLs, multiple attempts",
  86. TransferURLs{
  87. {
  88. URL: encodedA,
  89. OnlyAfterAttempts: 0,
  90. },
  91. {
  92. URL: encodedB,
  93. OnlyAfterAttempts: 1,
  94. },
  95. {
  96. URL: encodedC,
  97. OnlyAfterAttempts: 1,
  98. },
  99. },
  100. 2,
  101. true,
  102. decodedA,
  103. 3,
  104. },
  105. {
  106. "multiple URLs, multiple attempts",
  107. TransferURLs{
  108. {
  109. URL: encodedA,
  110. OnlyAfterAttempts: 0,
  111. },
  112. {
  113. URL: encodedB,
  114. OnlyAfterAttempts: 1,
  115. },
  116. {
  117. URL: encodedC,
  118. OnlyAfterAttempts: 3,
  119. },
  120. },
  121. 4,
  122. true,
  123. decodedA,
  124. 3,
  125. },
  126. }
  127. for _, testCase := range testCases {
  128. t.Run(testCase.description, func(t *testing.T) {
  129. err := testCase.transferURLs.DecodeAndValidate()
  130. if testCase.expectedValid {
  131. if err != nil {
  132. t.Fatalf("unexpected validation error: %s", err)
  133. }
  134. } else {
  135. if err == nil {
  136. t.Fatalf("expected validation error")
  137. }
  138. return
  139. }
  140. // Track distinct selections for each attempt; the
  141. // expected number of distinct should be for at least
  142. // one particular attempt.
  143. attemptDistinctSelections := make(map[int]map[string]int)
  144. for i := 0; i < testCase.attempts; i++ {
  145. attemptDistinctSelections[i] = make(map[string]int)
  146. }
  147. // Perform enough runs to account for random selection.
  148. runs := 1000
  149. attempt := 0
  150. for i := 0; i < runs; i++ {
  151. canonicalURL := testCase.transferURLs.CanonicalURL()
  152. if canonicalURL != testCase.expectedCanonicalURL {
  153. t.Fatalf("unexpected canonical URL: %s", canonicalURL)
  154. }
  155. transferUrl := testCase.transferURLs.Select(attempt)
  156. if transferUrl.SkipVerify {
  157. t.Fatalf("unexpected skipVerify")
  158. }
  159. attemptDistinctSelections[attempt][transferUrl.URL] += 1
  160. attempt = (attempt + 1) % testCase.attempts
  161. }
  162. maxDistinctSelections := 0
  163. for _, m := range attemptDistinctSelections {
  164. if len(m) > maxDistinctSelections {
  165. maxDistinctSelections = len(m)
  166. }
  167. }
  168. if maxDistinctSelections != testCase.expectedDistinctSelections {
  169. t.Fatalf("got %d distinct selections, expected %d",
  170. maxDistinctSelections,
  171. testCase.expectedDistinctSelections)
  172. }
  173. })
  174. }
  175. }