clientParameters_test.go 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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", g, v)
  39. }
  40. case int:
  41. g := p.Get().Int(name)
  42. if v != g {
  43. t.Fatalf("Int returned %+v expected %+v", g, v)
  44. }
  45. case float64:
  46. g := p.Get().Float(name)
  47. if v != g {
  48. t.Fatalf("Float returned %+v expected %+v", g, v)
  49. }
  50. case bool:
  51. g := p.Get().Bool(name)
  52. if v != g {
  53. t.Fatalf("Bool returned %+v expected %+v", g, v)
  54. }
  55. case time.Duration:
  56. g := p.Get().Duration(name)
  57. if v != g {
  58. t.Fatalf("Duration returned %+v expected %+v", g, v)
  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", g, v)
  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", g, v)
  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", g, v)
  74. }
  75. case DownloadURLs:
  76. g := p.Get().DownloadURLs(name)
  77. if !reflect.DeepEqual(v, g) {
  78. t.Fatalf("DownloadURLs returned %+v expected %+v", g, v)
  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", g, v)
  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", g, v)
  89. }
  90. case protocol.CustomTLSProfiles:
  91. g := p.Get().CustomTLSProfileNames()
  92. names := make([]string, len(v))
  93. for i, profile := range v {
  94. names[i] = profile.Name
  95. }
  96. if !reflect.DeepEqual(names, g) {
  97. t.Fatalf("CustomTLSProfileNames returned %+v expected %+v", g, names)
  98. }
  99. default:
  100. t.Fatalf("Unhandled default type: %s", name)
  101. }
  102. }
  103. }
  104. func TestGetValueLogger(t *testing.T) {
  105. loggerCalled := false
  106. p, err := NewClientParameters(
  107. func(error) {
  108. loggerCalled = true
  109. })
  110. if err != nil {
  111. t.Fatalf("NewClientParameters failed: %s", err)
  112. }
  113. p.Get().Int("unknown-parameter-name")
  114. if !loggerCalled {
  115. t.Fatalf("logged not called")
  116. }
  117. }
  118. func TestOverrides(t *testing.T) {
  119. tag := "tag"
  120. applyParameters := make(map[string]interface{})
  121. // Below minimum, should not apply
  122. defaultConnectionWorkerPoolSize := defaultClientParameters[ConnectionWorkerPoolSize].value.(int)
  123. minimumConnectionWorkerPoolSize := defaultClientParameters[ConnectionWorkerPoolSize].minimum.(int)
  124. newConnectionWorkerPoolSize := minimumConnectionWorkerPoolSize - 1
  125. applyParameters[ConnectionWorkerPoolSize] = newConnectionWorkerPoolSize
  126. // Above minimum, should apply
  127. defaultInitialLimitTunnelProtocolsCandidateCount := defaultClientParameters[InitialLimitTunnelProtocolsCandidateCount].value.(int)
  128. minimumInitialLimitTunnelProtocolsCandidateCount := defaultClientParameters[InitialLimitTunnelProtocolsCandidateCount].minimum.(int)
  129. newInitialLimitTunnelProtocolsCandidateCount := minimumInitialLimitTunnelProtocolsCandidateCount + 1
  130. applyParameters[InitialLimitTunnelProtocolsCandidateCount] = newInitialLimitTunnelProtocolsCandidateCount
  131. p, err := NewClientParameters(nil)
  132. if err != nil {
  133. t.Fatalf("NewClientParameters failed: %s", err)
  134. }
  135. // No skip on error; should fail and not apply any changes
  136. _, err = p.Set(tag, false, applyParameters)
  137. if err == nil {
  138. t.Fatalf("Set succeeded unexpectedly")
  139. }
  140. if p.Get().Tag() != "" {
  141. t.Fatalf("GetTag returned unexpected value")
  142. }
  143. v := p.Get().Int(ConnectionWorkerPoolSize)
  144. if v != defaultConnectionWorkerPoolSize {
  145. t.Fatalf("GetInt returned unexpected ConnectionWorkerPoolSize: %d", v)
  146. }
  147. v = p.Get().Int(InitialLimitTunnelProtocolsCandidateCount)
  148. if v != defaultInitialLimitTunnelProtocolsCandidateCount {
  149. t.Fatalf("GetInt returned unexpected InitialLimitTunnelProtocolsCandidateCount: %d", v)
  150. }
  151. // Skip on error; should skip ConnectionWorkerPoolSize and apply InitialLimitTunnelProtocolsCandidateCount
  152. counts, err := p.Set(tag, true, applyParameters)
  153. if err != nil {
  154. t.Fatalf("Set failed: %s", err)
  155. }
  156. if counts[0] != 1 {
  157. t.Fatalf("Apply returned unexpected count: %d", counts[0])
  158. }
  159. v = p.Get().Int(ConnectionWorkerPoolSize)
  160. if v != defaultConnectionWorkerPoolSize {
  161. t.Fatalf("GetInt returned unexpected ConnectionWorkerPoolSize: %d", v)
  162. }
  163. v = p.Get().Int(InitialLimitTunnelProtocolsCandidateCount)
  164. if v != newInitialLimitTunnelProtocolsCandidateCount {
  165. t.Fatalf("GetInt returned unexpected InitialLimitTunnelProtocolsCandidateCount: %d", v)
  166. }
  167. }
  168. func TestNetworkLatencyMultiplier(t *testing.T) {
  169. p, err := NewClientParameters(nil)
  170. if err != nil {
  171. t.Fatalf("NewClientParameters failed: %s", err)
  172. }
  173. timeout1 := p.Get().Duration(TunnelConnectTimeout)
  174. applyParameters := map[string]interface{}{"NetworkLatencyMultiplier": 2.0}
  175. _, err = p.Set("", false, applyParameters)
  176. if err != nil {
  177. t.Fatalf("Set failed: %s", err)
  178. }
  179. timeout2 := p.Get().Duration(TunnelConnectTimeout)
  180. if 2*timeout1 != timeout2 {
  181. t.Fatalf("Unexpected timeouts: 2 * %s != %s", timeout1, timeout2)
  182. }
  183. }
  184. func TestLimitTunnelProtocolProbability(t *testing.T) {
  185. p, err := NewClientParameters(nil)
  186. if err != nil {
  187. t.Fatalf("NewClientParameters failed: %s", err)
  188. }
  189. // Default probability should be 1.0 and always return tunnelProtocols
  190. tunnelProtocols := protocol.TunnelProtocols{"OSSH", "SSH"}
  191. applyParameters := map[string]interface{}{
  192. "LimitTunnelProtocols": tunnelProtocols,
  193. }
  194. _, err = p.Set("", false, applyParameters)
  195. if err != nil {
  196. t.Fatalf("Set failed: %s", err)
  197. }
  198. for i := 0; i < 1000; i++ {
  199. l := p.Get().TunnelProtocols(LimitTunnelProtocols)
  200. if !reflect.DeepEqual(l, tunnelProtocols) {
  201. t.Fatalf("unexpected %+v != %+v", l, tunnelProtocols)
  202. }
  203. }
  204. // With probability set to 0.5, should return tunnelProtocols ~50%
  205. defaultLimitTunnelProtocols := protocol.TunnelProtocols{}
  206. applyParameters = map[string]interface{}{
  207. "LimitTunnelProtocolsProbability": 0.5,
  208. "LimitTunnelProtocols": tunnelProtocols,
  209. }
  210. _, err = p.Set("", false, applyParameters)
  211. if err != nil {
  212. t.Fatalf("Set failed: %s", err)
  213. }
  214. matchCount := 0
  215. for i := 0; i < 1000; i++ {
  216. l := p.Get().TunnelProtocols(LimitTunnelProtocols)
  217. if reflect.DeepEqual(l, tunnelProtocols) {
  218. matchCount += 1
  219. } else if !reflect.DeepEqual(l, defaultLimitTunnelProtocols) {
  220. t.Fatalf("unexpected %+v != %+v", l, defaultLimitTunnelProtocols)
  221. }
  222. }
  223. if matchCount < 250 || matchCount > 750 {
  224. t.Fatalf("Unexpected probability result: %d", matchCount)
  225. }
  226. }
  227. func TestCustomTLSProfiles(t *testing.T) {
  228. p, err := NewClientParameters(nil)
  229. if err != nil {
  230. t.Fatalf("NewClientParameters failed: %s", err)
  231. }
  232. customTLSProfiles := protocol.CustomTLSProfiles{
  233. &protocol.CustomTLSProfile{Name: "Profile1", UTLSSpec: &protocol.UTLSSpec{}},
  234. &protocol.CustomTLSProfile{Name: "Profile2", UTLSSpec: &protocol.UTLSSpec{}},
  235. }
  236. applyParameters := map[string]interface{}{
  237. "CustomTLSProfiles": customTLSProfiles}
  238. _, err = p.Set("", false, applyParameters)
  239. if err != nil {
  240. t.Fatalf("Set failed: %s", err)
  241. }
  242. names := p.Get().CustomTLSProfileNames()
  243. if len(names) != 2 || names[0] != "Profile1" || names[1] != "Profile2" {
  244. t.Fatalf("Unexpected CustomTLSProfileNames: %+v", names)
  245. }
  246. profile := p.Get().CustomTLSProfile("Profile1")
  247. if profile == nil || profile.Name != "Profile1" {
  248. t.Fatalf("Unexpected profile")
  249. }
  250. profile = p.Get().CustomTLSProfile("Profile2")
  251. if profile == nil || profile.Name != "Profile2" {
  252. t.Fatalf("Unexpected profile")
  253. }
  254. profile = p.Get().CustomTLSProfile("Profile3")
  255. if profile != nil {
  256. t.Fatalf("Unexpected profile")
  257. }
  258. }