clientParameters_test.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  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/json"
  22. "net/http"
  23. "reflect"
  24. "testing"
  25. "time"
  26. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common"
  27. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/protocol"
  28. )
  29. func TestGetDefaultParameters(t *testing.T) {
  30. p, err := NewClientParameters(nil)
  31. if err != nil {
  32. t.Fatalf("NewClientParameters failed: %s", err)
  33. }
  34. for name, defaults := range defaultClientParameters {
  35. switch v := defaults.value.(type) {
  36. case string:
  37. g := p.Get().String(name)
  38. if v != g {
  39. t.Fatalf("String returned %+v expected %+v", g, v)
  40. }
  41. case int:
  42. g := p.Get().Int(name)
  43. if v != g {
  44. t.Fatalf("Int returned %+v expected %+v", g, v)
  45. }
  46. case float64:
  47. g := p.Get().Float(name)
  48. if v != g {
  49. t.Fatalf("Float returned %+v expected %+v", g, v)
  50. }
  51. case bool:
  52. g := p.Get().Bool(name)
  53. if v != g {
  54. t.Fatalf("Bool returned %+v expected %+v", g, v)
  55. }
  56. case time.Duration:
  57. g := p.Get().Duration(name)
  58. if v != g {
  59. t.Fatalf("Duration returned %+v expected %+v", g, v)
  60. }
  61. case protocol.TunnelProtocols:
  62. g := p.Get().TunnelProtocols(name)
  63. if !reflect.DeepEqual(v, g) {
  64. t.Fatalf("TunnelProtocols returned %+v expected %+v", g, v)
  65. }
  66. case protocol.TLSProfiles:
  67. g := p.Get().TLSProfiles(name)
  68. if !reflect.DeepEqual(v, g) {
  69. t.Fatalf("TLSProfiles returned %+v expected %+v", g, v)
  70. }
  71. case protocol.QUICVersions:
  72. g := p.Get().QUICVersions(name)
  73. if !reflect.DeepEqual(v, g) {
  74. t.Fatalf("QUICVersions returned %+v expected %+v", g, v)
  75. }
  76. case DownloadURLs:
  77. g := p.Get().DownloadURLs(name)
  78. if !reflect.DeepEqual(v, g) {
  79. t.Fatalf("DownloadURLs returned %+v expected %+v", g, v)
  80. }
  81. case common.RateLimits:
  82. g := p.Get().RateLimits(name)
  83. if !reflect.DeepEqual(v, g) {
  84. t.Fatalf("RateLimits returned %+v expected %+v", g, v)
  85. }
  86. case http.Header:
  87. g := p.Get().HTTPHeaders(name)
  88. if !reflect.DeepEqual(v, g) {
  89. t.Fatalf("HTTPHeaders returned %+v expected %+v", g, v)
  90. }
  91. case protocol.CustomTLSProfiles:
  92. g := p.Get().CustomTLSProfileNames()
  93. names := make([]string, len(v))
  94. for i, profile := range v {
  95. names[i] = profile.Name
  96. }
  97. if !reflect.DeepEqual(names, g) {
  98. t.Fatalf("CustomTLSProfileNames returned %+v expected %+v", g, names)
  99. }
  100. case KeyValues:
  101. g := p.Get().KeyValues(name)
  102. if !reflect.DeepEqual(v, g) {
  103. t.Fatalf("KeyValues returned %+v expected %+v", g, v)
  104. }
  105. default:
  106. t.Fatalf("Unhandled default type: %s", name)
  107. }
  108. }
  109. }
  110. func TestGetValueLogger(t *testing.T) {
  111. loggerCalled := false
  112. p, err := NewClientParameters(
  113. func(error) {
  114. loggerCalled = true
  115. })
  116. if err != nil {
  117. t.Fatalf("NewClientParameters failed: %s", err)
  118. }
  119. p.Get().Int("unknown-parameter-name")
  120. if !loggerCalled {
  121. t.Fatalf("logged not called")
  122. }
  123. }
  124. func TestOverrides(t *testing.T) {
  125. tag := "tag"
  126. applyParameters := make(map[string]interface{})
  127. // Below minimum, should not apply
  128. defaultConnectionWorkerPoolSize := defaultClientParameters[ConnectionWorkerPoolSize].value.(int)
  129. minimumConnectionWorkerPoolSize := defaultClientParameters[ConnectionWorkerPoolSize].minimum.(int)
  130. newConnectionWorkerPoolSize := minimumConnectionWorkerPoolSize - 1
  131. applyParameters[ConnectionWorkerPoolSize] = newConnectionWorkerPoolSize
  132. // Above minimum, should apply
  133. defaultInitialLimitTunnelProtocolsCandidateCount := defaultClientParameters[InitialLimitTunnelProtocolsCandidateCount].value.(int)
  134. minimumInitialLimitTunnelProtocolsCandidateCount := defaultClientParameters[InitialLimitTunnelProtocolsCandidateCount].minimum.(int)
  135. newInitialLimitTunnelProtocolsCandidateCount := minimumInitialLimitTunnelProtocolsCandidateCount + 1
  136. applyParameters[InitialLimitTunnelProtocolsCandidateCount] = newInitialLimitTunnelProtocolsCandidateCount
  137. p, err := NewClientParameters(nil)
  138. if err != nil {
  139. t.Fatalf("NewClientParameters failed: %s", err)
  140. }
  141. // No skip on error; should fail and not apply any changes
  142. _, err = p.Set(tag, false, applyParameters)
  143. if err == nil {
  144. t.Fatalf("Set succeeded unexpectedly")
  145. }
  146. if p.Get().Tag() != "" {
  147. t.Fatalf("GetTag returned unexpected value")
  148. }
  149. v := p.Get().Int(ConnectionWorkerPoolSize)
  150. if v != defaultConnectionWorkerPoolSize {
  151. t.Fatalf("GetInt returned unexpected ConnectionWorkerPoolSize: %d", v)
  152. }
  153. v = p.Get().Int(InitialLimitTunnelProtocolsCandidateCount)
  154. if v != defaultInitialLimitTunnelProtocolsCandidateCount {
  155. t.Fatalf("GetInt returned unexpected InitialLimitTunnelProtocolsCandidateCount: %d", v)
  156. }
  157. // Skip on error; should skip ConnectionWorkerPoolSize and apply InitialLimitTunnelProtocolsCandidateCount
  158. counts, err := p.Set(tag, true, applyParameters)
  159. if err != nil {
  160. t.Fatalf("Set failed: %s", err)
  161. }
  162. if counts[0] != 1 {
  163. t.Fatalf("Apply returned unexpected count: %d", counts[0])
  164. }
  165. v = p.Get().Int(ConnectionWorkerPoolSize)
  166. if v != defaultConnectionWorkerPoolSize {
  167. t.Fatalf("GetInt returned unexpected ConnectionWorkerPoolSize: %d", v)
  168. }
  169. v = p.Get().Int(InitialLimitTunnelProtocolsCandidateCount)
  170. if v != newInitialLimitTunnelProtocolsCandidateCount {
  171. t.Fatalf("GetInt returned unexpected InitialLimitTunnelProtocolsCandidateCount: %d", v)
  172. }
  173. }
  174. func TestNetworkLatencyMultiplier(t *testing.T) {
  175. p, err := NewClientParameters(nil)
  176. if err != nil {
  177. t.Fatalf("NewClientParameters failed: %s", err)
  178. }
  179. timeout1 := p.Get().Duration(TunnelConnectTimeout)
  180. applyParameters := map[string]interface{}{"NetworkLatencyMultiplier": 2.0}
  181. _, err = p.Set("", false, applyParameters)
  182. if err != nil {
  183. t.Fatalf("Set failed: %s", err)
  184. }
  185. timeout2 := p.Get().Duration(TunnelConnectTimeout)
  186. if 2*timeout1 != timeout2 {
  187. t.Fatalf("Unexpected timeouts: 2 * %s != %s", timeout1, timeout2)
  188. }
  189. }
  190. func TestCustomNetworkLatencyMultiplier(t *testing.T) {
  191. p, err := NewClientParameters(nil)
  192. if err != nil {
  193. t.Fatalf("NewClientParameters failed: %s", err)
  194. }
  195. timeout1 := p.Get().Duration(TunnelConnectTimeout)
  196. applyParameters := map[string]interface{}{"NetworkLatencyMultiplier": 2.0}
  197. _, err = p.Set("", false, applyParameters)
  198. if err != nil {
  199. t.Fatalf("Set failed: %s", err)
  200. }
  201. timeout2 := p.GetCustom(4.0).Duration(TunnelConnectTimeout)
  202. if 4*timeout1 != timeout2 {
  203. t.Fatalf("Unexpected timeouts: 4 * %s != %s", timeout1, timeout2)
  204. }
  205. }
  206. func TestLimitTunnelProtocolProbability(t *testing.T) {
  207. p, err := NewClientParameters(nil)
  208. if err != nil {
  209. t.Fatalf("NewClientParameters failed: %s", err)
  210. }
  211. // Default probability should be 1.0 and always return tunnelProtocols
  212. tunnelProtocols := protocol.TunnelProtocols{"OSSH", "SSH"}
  213. applyParameters := map[string]interface{}{
  214. "LimitTunnelProtocols": tunnelProtocols,
  215. }
  216. _, err = p.Set("", false, applyParameters)
  217. if err != nil {
  218. t.Fatalf("Set failed: %s", err)
  219. }
  220. for i := 0; i < 1000; i++ {
  221. l := p.Get().TunnelProtocols(LimitTunnelProtocols)
  222. if !reflect.DeepEqual(l, tunnelProtocols) {
  223. t.Fatalf("unexpected %+v != %+v", l, tunnelProtocols)
  224. }
  225. }
  226. // With probability set to 0.5, should return tunnelProtocols ~50%
  227. defaultLimitTunnelProtocols := protocol.TunnelProtocols{}
  228. applyParameters = map[string]interface{}{
  229. "LimitTunnelProtocolsProbability": 0.5,
  230. "LimitTunnelProtocols": tunnelProtocols,
  231. }
  232. _, err = p.Set("", false, applyParameters)
  233. if err != nil {
  234. t.Fatalf("Set failed: %s", err)
  235. }
  236. matchCount := 0
  237. for i := 0; i < 1000; i++ {
  238. l := p.Get().TunnelProtocols(LimitTunnelProtocols)
  239. if reflect.DeepEqual(l, tunnelProtocols) {
  240. matchCount += 1
  241. } else if !reflect.DeepEqual(l, defaultLimitTunnelProtocols) {
  242. t.Fatalf("unexpected %+v != %+v", l, defaultLimitTunnelProtocols)
  243. }
  244. }
  245. if matchCount < 250 || matchCount > 750 {
  246. t.Fatalf("Unexpected probability result: %d", matchCount)
  247. }
  248. }
  249. func TestCustomTLSProfiles(t *testing.T) {
  250. p, err := NewClientParameters(nil)
  251. if err != nil {
  252. t.Fatalf("NewClientParameters failed: %s", err)
  253. }
  254. customTLSProfiles := protocol.CustomTLSProfiles{
  255. &protocol.CustomTLSProfile{Name: "Profile1", UTLSSpec: &protocol.UTLSSpec{}},
  256. &protocol.CustomTLSProfile{Name: "Profile2", UTLSSpec: &protocol.UTLSSpec{}},
  257. }
  258. applyParameters := map[string]interface{}{
  259. "CustomTLSProfiles": customTLSProfiles}
  260. _, err = p.Set("", false, applyParameters)
  261. if err != nil {
  262. t.Fatalf("Set failed: %s", err)
  263. }
  264. names := p.Get().CustomTLSProfileNames()
  265. if len(names) != 2 || names[0] != "Profile1" || names[1] != "Profile2" {
  266. t.Fatalf("Unexpected CustomTLSProfileNames: %+v", names)
  267. }
  268. profile := p.Get().CustomTLSProfile("Profile1")
  269. if profile == nil || profile.Name != "Profile1" {
  270. t.Fatalf("Unexpected profile")
  271. }
  272. profile = p.Get().CustomTLSProfile("Profile2")
  273. if profile == nil || profile.Name != "Profile2" {
  274. t.Fatalf("Unexpected profile")
  275. }
  276. profile = p.Get().CustomTLSProfile("Profile3")
  277. if profile != nil {
  278. t.Fatalf("Unexpected profile")
  279. }
  280. }
  281. func TestApplicationParameters(t *testing.T) {
  282. parametersJSON := []byte(`
  283. {
  284. "ApplicationParameters" : {
  285. "AppFlag1" : true,
  286. "AppConfig1" : {"Option1" : "A", "Option2" : "B"},
  287. "AppSwitches1" : [1, 2, 3, 4]
  288. }
  289. }
  290. `)
  291. validators := map[string]func(v interface{}) bool{
  292. "AppFlag1": func(v interface{}) bool { return reflect.DeepEqual(v, true) },
  293. "AppConfig1": func(v interface{}) bool {
  294. return reflect.DeepEqual(v, map[string]interface{}{"Option1": "A", "Option2": "B"})
  295. },
  296. "AppSwitches1": func(v interface{}) bool {
  297. return reflect.DeepEqual(v, []interface{}{float64(1), float64(2), float64(3), float64(4)})
  298. },
  299. }
  300. var applyParameters map[string]interface{}
  301. err := json.Unmarshal(parametersJSON, &applyParameters)
  302. if err != nil {
  303. t.Fatalf("Unmarshal failed: %s", err)
  304. }
  305. p, err := NewClientParameters(nil)
  306. if err != nil {
  307. t.Fatalf("NewClientParameters failed: %s", err)
  308. }
  309. _, err = p.Set("", false, applyParameters)
  310. if err != nil {
  311. t.Fatalf("Set failed: %s", err)
  312. }
  313. keyValues := p.Get().KeyValues(ApplicationParameters)
  314. if len(keyValues) != len(validators) {
  315. t.Fatalf("Unexpected key value count")
  316. }
  317. for key, value := range keyValues {
  318. validator, ok := validators[key]
  319. if !ok {
  320. t.Fatalf("Unexpected key: %s", key)
  321. }
  322. var unmarshaledValue interface{}
  323. err := json.Unmarshal(value, &unmarshaledValue)
  324. if err != nil {
  325. t.Fatalf("Unmarshal failed: %s", err)
  326. }
  327. if !validator(unmarshaledValue) {
  328. t.Fatalf("Invalid value: %s, %T: %+v",
  329. key, unmarshaledValue, unmarshaledValue)
  330. }
  331. }
  332. }