clientParameters_test.go 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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. "net/http"
  22. "reflect"
  23. "testing"
  24. "time"
  25. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common"
  26. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/protocol"
  27. )
  28. func TestGetDefaultParameters(t *testing.T) {
  29. p, err := NewClientParameters(nil)
  30. if err != nil {
  31. t.Fatalf("NewClientParameters failed: %s", err)
  32. }
  33. for name, defaults := range defaultClientParameters {
  34. switch v := defaults.value.(type) {
  35. case string:
  36. g := p.Get().String(name)
  37. if v != g {
  38. t.Fatalf("String returned %+v expected %+v", v, g)
  39. }
  40. case int:
  41. g := p.Get().Int(name)
  42. if v != g {
  43. t.Fatalf("Int returned %+v expected %+v", v, g)
  44. }
  45. case float64:
  46. g := p.Get().Float(name)
  47. if v != g {
  48. t.Fatalf("Float returned %+v expected %+v", v, g)
  49. }
  50. case bool:
  51. g := p.Get().Bool(name)
  52. if v != g {
  53. t.Fatalf("Bool returned %+v expected %+v", v, g)
  54. }
  55. case time.Duration:
  56. g := p.Get().Duration(name)
  57. if v != g {
  58. t.Fatalf("Duration returned %+v expected %+v", v, g)
  59. }
  60. case protocol.TunnelProtocols:
  61. g := p.Get().TunnelProtocols(name)
  62. if !reflect.DeepEqual(v, g) {
  63. t.Fatalf("TunnelProtocols returned %+v expected %+v", v, g)
  64. }
  65. case protocol.TLSProfiles:
  66. g := p.Get().TLSProfiles(name)
  67. if !reflect.DeepEqual(v, g) {
  68. t.Fatalf("TLSProfiles returned %+v expected %+v", v, g)
  69. }
  70. case protocol.QUICVersions:
  71. g := p.Get().QUICVersions(name)
  72. if !reflect.DeepEqual(v, g) {
  73. t.Fatalf("QUICVersions returned %+v expected %+v", v, g)
  74. }
  75. case DownloadURLs:
  76. g := p.Get().DownloadURLs(name)
  77. if !reflect.DeepEqual(v, g) {
  78. t.Fatalf("DownloadURLs returned %+v expected %+v", v, g)
  79. }
  80. case common.RateLimits:
  81. g := p.Get().RateLimits(name)
  82. if !reflect.DeepEqual(v, g) {
  83. t.Fatalf("RateLimits returned %+v expected %+v", v, g)
  84. }
  85. case http.Header:
  86. g := p.Get().HTTPHeaders(name)
  87. if !reflect.DeepEqual(v, g) {
  88. t.Fatalf("HTTPHeaders returned %+v expected %+v", v, g)
  89. }
  90. default:
  91. t.Fatalf("Unhandled default type: %s", name)
  92. }
  93. }
  94. }
  95. func TestGetValueLogger(t *testing.T) {
  96. loggerCalled := false
  97. p, err := NewClientParameters(
  98. func(error) {
  99. loggerCalled = true
  100. })
  101. if err != nil {
  102. t.Fatalf("NewClientParameters failed: %s", err)
  103. }
  104. p.Get().Int("unknown-parameter-name")
  105. if !loggerCalled {
  106. t.Fatalf("logged not called")
  107. }
  108. }
  109. func TestOverrides(t *testing.T) {
  110. tag := "tag"
  111. applyParameters := make(map[string]interface{})
  112. // Below minimum, should not apply
  113. defaultConnectionWorkerPoolSize := defaultClientParameters[ConnectionWorkerPoolSize].value.(int)
  114. minimumConnectionWorkerPoolSize := defaultClientParameters[ConnectionWorkerPoolSize].minimum.(int)
  115. newConnectionWorkerPoolSize := minimumConnectionWorkerPoolSize - 1
  116. applyParameters[ConnectionWorkerPoolSize] = newConnectionWorkerPoolSize
  117. // Above minimum, should apply
  118. defaultInitialLimitTunnelProtocolsCandidateCount := defaultClientParameters[InitialLimitTunnelProtocolsCandidateCount].value.(int)
  119. minimumInitialLimitTunnelProtocolsCandidateCount := defaultClientParameters[InitialLimitTunnelProtocolsCandidateCount].minimum.(int)
  120. newInitialLimitTunnelProtocolsCandidateCount := minimumInitialLimitTunnelProtocolsCandidateCount + 1
  121. applyParameters[InitialLimitTunnelProtocolsCandidateCount] = newInitialLimitTunnelProtocolsCandidateCount
  122. p, err := NewClientParameters(nil)
  123. if err != nil {
  124. t.Fatalf("NewClientParameters failed: %s", err)
  125. }
  126. // No skip on error; should fail and not apply any changes
  127. _, err = p.Set(tag, false, applyParameters)
  128. if err == nil {
  129. t.Fatalf("Set succeeded unexpectedly")
  130. }
  131. if p.Get().Tag() != "" {
  132. t.Fatalf("GetTag returned unexpected value")
  133. }
  134. v := p.Get().Int(ConnectionWorkerPoolSize)
  135. if v != defaultConnectionWorkerPoolSize {
  136. t.Fatalf("GetInt returned unexpected ConnectionWorkerPoolSize: %d", v)
  137. }
  138. v = p.Get().Int(InitialLimitTunnelProtocolsCandidateCount)
  139. if v != defaultInitialLimitTunnelProtocolsCandidateCount {
  140. t.Fatalf("GetInt returned unexpected InitialLimitTunnelProtocolsCandidateCount: %d", v)
  141. }
  142. // Skip on error; should skip ConnectionWorkerPoolSize and apply InitialLimitTunnelProtocolsCandidateCount
  143. counts, err := p.Set(tag, true, applyParameters)
  144. if err != nil {
  145. t.Fatalf("Set failed: %s", err)
  146. }
  147. if counts[0] != 1 {
  148. t.Fatalf("Apply returned unexpected count: %d", counts[0])
  149. }
  150. v = p.Get().Int(ConnectionWorkerPoolSize)
  151. if v != defaultConnectionWorkerPoolSize {
  152. t.Fatalf("GetInt returned unexpected ConnectionWorkerPoolSize: %d", v)
  153. }
  154. v = p.Get().Int(InitialLimitTunnelProtocolsCandidateCount)
  155. if v != newInitialLimitTunnelProtocolsCandidateCount {
  156. t.Fatalf("GetInt returned unexpected InitialLimitTunnelProtocolsCandidateCount: %d", v)
  157. }
  158. }
  159. func TestNetworkLatencyMultiplier(t *testing.T) {
  160. p, err := NewClientParameters(nil)
  161. if err != nil {
  162. t.Fatalf("NewClientParameters failed: %s", err)
  163. }
  164. timeout1 := p.Get().Duration(TunnelConnectTimeout)
  165. applyParameters := map[string]interface{}{"NetworkLatencyMultiplier": 2.0}
  166. _, err = p.Set("", false, applyParameters)
  167. if err != nil {
  168. t.Fatalf("Set failed: %s", err)
  169. }
  170. timeout2 := p.Get().Duration(TunnelConnectTimeout)
  171. if 2*timeout1 != timeout2 {
  172. t.Fatalf("Unexpected timeouts: 2 * %s != %s", timeout1, timeout2)
  173. }
  174. }
  175. func TestLimitTunnelProtocolProbability(t *testing.T) {
  176. p, err := NewClientParameters(nil)
  177. if err != nil {
  178. t.Fatalf("NewClientParameters failed: %s", err)
  179. }
  180. // Default probability should be 1.0 and always return tunnelProtocols
  181. tunnelProtocols := protocol.TunnelProtocols{"OSSH", "SSH"}
  182. applyParameters := map[string]interface{}{
  183. "LimitTunnelProtocols": tunnelProtocols,
  184. }
  185. _, err = p.Set("", false, applyParameters)
  186. if err != nil {
  187. t.Fatalf("Set failed: %s", err)
  188. }
  189. for i := 0; i < 1000; i++ {
  190. l := p.Get().TunnelProtocols(LimitTunnelProtocols)
  191. if !reflect.DeepEqual(l, tunnelProtocols) {
  192. t.Fatalf("unexpected %+v != %+v", l, tunnelProtocols)
  193. }
  194. }
  195. // With probability set to 0.5, should return tunnelProtocols ~50%
  196. defaultLimitTunnelProtocols := protocol.TunnelProtocols{}
  197. applyParameters = map[string]interface{}{
  198. "LimitTunnelProtocolsProbability": 0.5,
  199. "LimitTunnelProtocols": tunnelProtocols,
  200. }
  201. _, err = p.Set("", false, applyParameters)
  202. if err != nil {
  203. t.Fatalf("Set failed: %s", err)
  204. }
  205. matchCount := 0
  206. for i := 0; i < 1000; i++ {
  207. l := p.Get().TunnelProtocols(LimitTunnelProtocols)
  208. if reflect.DeepEqual(l, tunnelProtocols) {
  209. matchCount += 1
  210. } else if !reflect.DeepEqual(l, defaultLimitTunnelProtocols) {
  211. t.Fatalf("unexpected %+v != %+v", l, defaultLimitTunnelProtocols)
  212. }
  213. }
  214. if matchCount < 250 || matchCount > 750 {
  215. t.Fatalf("Unexpected probability result: %d", matchCount)
  216. }
  217. }