clientParameters_test.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  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.LabeledTLSProfiles:
  72. for label, profiles := range v {
  73. g := p.Get().LabeledTLSProfiles(name, label)
  74. if !reflect.DeepEqual(profiles, g) {
  75. t.Fatalf("LabeledTLSProfiles returned %+v expected %+v", g, profiles)
  76. }
  77. }
  78. case protocol.QUICVersions:
  79. g := p.Get().QUICVersions(name)
  80. if !reflect.DeepEqual(v, g) {
  81. t.Fatalf("QUICVersions returned %+v expected %+v", g, v)
  82. }
  83. case protocol.LabeledQUICVersions:
  84. for label, versions := range v {
  85. g := p.Get().LabeledTLSProfiles(name, label)
  86. if !reflect.DeepEqual(versions, g) {
  87. t.Fatalf("LabeledQUICVersions returned %+v expected %+v", g, versions)
  88. }
  89. }
  90. case TransferURLs:
  91. g := p.Get().TransferURLs(name)
  92. if !reflect.DeepEqual(v, g) {
  93. t.Fatalf("TransferURLs returned %+v expected %+v", g, v)
  94. }
  95. case common.RateLimits:
  96. g := p.Get().RateLimits(name)
  97. if !reflect.DeepEqual(v, g) {
  98. t.Fatalf("RateLimits returned %+v expected %+v", g, v)
  99. }
  100. case http.Header:
  101. g := p.Get().HTTPHeaders(name)
  102. if !reflect.DeepEqual(v, g) {
  103. t.Fatalf("HTTPHeaders returned %+v expected %+v", g, v)
  104. }
  105. case protocol.CustomTLSProfiles:
  106. g := p.Get().CustomTLSProfileNames()
  107. names := make([]string, len(v))
  108. for i, profile := range v {
  109. names[i] = profile.Name
  110. }
  111. if !reflect.DeepEqual(names, g) {
  112. t.Fatalf("CustomTLSProfileNames returned %+v expected %+v", g, names)
  113. }
  114. case KeyValues:
  115. g := p.Get().KeyValues(name)
  116. if !reflect.DeepEqual(v, g) {
  117. t.Fatalf("KeyValues returned %+v expected %+v", g, v)
  118. }
  119. case *BPFProgramSpec:
  120. ok, name, rawInstructions := p.Get().BPFProgram(name)
  121. if v != nil || ok || name != "" || rawInstructions != nil {
  122. t.Fatalf(
  123. "BPFProgramSpec returned %+v %+v %+v expected %+v",
  124. ok, name, rawInstructions, v)
  125. }
  126. case PacketManipulationSpecs:
  127. g := p.Get().PacketManipulationSpecs(name)
  128. if !reflect.DeepEqual(v, g) {
  129. t.Fatalf("PacketManipulationSpecs returned %+v expected %+v", g, v)
  130. }
  131. case ProtocolPacketManipulations:
  132. g := p.Get().ProtocolPacketManipulations(name)
  133. if !reflect.DeepEqual(v, g) {
  134. t.Fatalf("ProtocolPacketManipulations returned %+v expected %+v", g, v)
  135. }
  136. default:
  137. t.Fatalf("Unhandled default type: %s", name)
  138. }
  139. }
  140. }
  141. func TestGetValueLogger(t *testing.T) {
  142. loggerCalled := false
  143. p, err := NewClientParameters(
  144. func(error) {
  145. loggerCalled = true
  146. })
  147. if err != nil {
  148. t.Fatalf("NewClientParameters failed: %s", err)
  149. }
  150. p.Get().Int("unknown-parameter-name")
  151. if !loggerCalled {
  152. t.Fatalf("logged not called")
  153. }
  154. }
  155. func TestOverrides(t *testing.T) {
  156. tag := "tag"
  157. applyParameters := make(map[string]interface{})
  158. // Below minimum, should not apply
  159. defaultConnectionWorkerPoolSize := defaultClientParameters[ConnectionWorkerPoolSize].value.(int)
  160. minimumConnectionWorkerPoolSize := defaultClientParameters[ConnectionWorkerPoolSize].minimum.(int)
  161. newConnectionWorkerPoolSize := minimumConnectionWorkerPoolSize - 1
  162. applyParameters[ConnectionWorkerPoolSize] = newConnectionWorkerPoolSize
  163. // Above minimum, should apply
  164. defaultInitialLimitTunnelProtocolsCandidateCount := defaultClientParameters[InitialLimitTunnelProtocolsCandidateCount].value.(int)
  165. minimumInitialLimitTunnelProtocolsCandidateCount := defaultClientParameters[InitialLimitTunnelProtocolsCandidateCount].minimum.(int)
  166. newInitialLimitTunnelProtocolsCandidateCount := minimumInitialLimitTunnelProtocolsCandidateCount + 1
  167. applyParameters[InitialLimitTunnelProtocolsCandidateCount] = newInitialLimitTunnelProtocolsCandidateCount
  168. p, err := NewClientParameters(nil)
  169. if err != nil {
  170. t.Fatalf("NewClientParameters failed: %s", err)
  171. }
  172. // No skip on error; should fail and not apply any changes
  173. _, err = p.Set(tag, false, applyParameters)
  174. if err == nil {
  175. t.Fatalf("Set succeeded unexpectedly")
  176. }
  177. if p.Get().Tag() != "" {
  178. t.Fatalf("GetTag returned unexpected value")
  179. }
  180. v := p.Get().Int(ConnectionWorkerPoolSize)
  181. if v != defaultConnectionWorkerPoolSize {
  182. t.Fatalf("GetInt returned unexpected ConnectionWorkerPoolSize: %d", v)
  183. }
  184. v = p.Get().Int(InitialLimitTunnelProtocolsCandidateCount)
  185. if v != defaultInitialLimitTunnelProtocolsCandidateCount {
  186. t.Fatalf("GetInt returned unexpected InitialLimitTunnelProtocolsCandidateCount: %d", v)
  187. }
  188. // Skip on error; should skip ConnectionWorkerPoolSize and apply InitialLimitTunnelProtocolsCandidateCount
  189. counts, err := p.Set(tag, true, applyParameters)
  190. if err != nil {
  191. t.Fatalf("Set failed: %s", err)
  192. }
  193. if counts[0] != 1 {
  194. t.Fatalf("Apply returned unexpected count: %d", counts[0])
  195. }
  196. v = p.Get().Int(ConnectionWorkerPoolSize)
  197. if v != defaultConnectionWorkerPoolSize {
  198. t.Fatalf("GetInt returned unexpected ConnectionWorkerPoolSize: %d", v)
  199. }
  200. v = p.Get().Int(InitialLimitTunnelProtocolsCandidateCount)
  201. if v != newInitialLimitTunnelProtocolsCandidateCount {
  202. t.Fatalf("GetInt returned unexpected InitialLimitTunnelProtocolsCandidateCount: %d", v)
  203. }
  204. }
  205. func TestNetworkLatencyMultiplier(t *testing.T) {
  206. p, err := NewClientParameters(nil)
  207. if err != nil {
  208. t.Fatalf("NewClientParameters failed: %s", err)
  209. }
  210. timeout1 := p.Get().Duration(TunnelConnectTimeout)
  211. applyParameters := map[string]interface{}{"NetworkLatencyMultiplier": 2.0}
  212. _, err = p.Set("", false, applyParameters)
  213. if err != nil {
  214. t.Fatalf("Set failed: %s", err)
  215. }
  216. timeout2 := p.Get().Duration(TunnelConnectTimeout)
  217. if 2*timeout1 != timeout2 {
  218. t.Fatalf("Unexpected timeouts: 2 * %s != %s", timeout1, timeout2)
  219. }
  220. }
  221. func TestCustomNetworkLatencyMultiplier(t *testing.T) {
  222. p, err := NewClientParameters(nil)
  223. if err != nil {
  224. t.Fatalf("NewClientParameters failed: %s", err)
  225. }
  226. timeout1 := p.Get().Duration(TunnelConnectTimeout)
  227. applyParameters := map[string]interface{}{"NetworkLatencyMultiplier": 2.0}
  228. _, err = p.Set("", false, applyParameters)
  229. if err != nil {
  230. t.Fatalf("Set failed: %s", err)
  231. }
  232. timeout2 := p.GetCustom(4.0).Duration(TunnelConnectTimeout)
  233. if 4*timeout1 != timeout2 {
  234. t.Fatalf("Unexpected timeouts: 4 * %s != %s", timeout1, timeout2)
  235. }
  236. }
  237. func TestLimitTunnelProtocolProbability(t *testing.T) {
  238. p, err := NewClientParameters(nil)
  239. if err != nil {
  240. t.Fatalf("NewClientParameters failed: %s", err)
  241. }
  242. // Default probability should be 1.0 and always return tunnelProtocols
  243. tunnelProtocols := protocol.TunnelProtocols{"OSSH", "SSH"}
  244. applyParameters := map[string]interface{}{
  245. "LimitTunnelProtocols": tunnelProtocols,
  246. }
  247. _, err = p.Set("", false, applyParameters)
  248. if err != nil {
  249. t.Fatalf("Set failed: %s", err)
  250. }
  251. for i := 0; i < 1000; i++ {
  252. l := p.Get().TunnelProtocols(LimitTunnelProtocols)
  253. if !reflect.DeepEqual(l, tunnelProtocols) {
  254. t.Fatalf("unexpected %+v != %+v", l, tunnelProtocols)
  255. }
  256. }
  257. // With probability set to 0.5, should return tunnelProtocols ~50%
  258. defaultLimitTunnelProtocols := protocol.TunnelProtocols{}
  259. applyParameters = map[string]interface{}{
  260. "LimitTunnelProtocolsProbability": 0.5,
  261. "LimitTunnelProtocols": tunnelProtocols,
  262. }
  263. _, err = p.Set("", false, applyParameters)
  264. if err != nil {
  265. t.Fatalf("Set failed: %s", err)
  266. }
  267. matchCount := 0
  268. for i := 0; i < 1000; i++ {
  269. l := p.Get().TunnelProtocols(LimitTunnelProtocols)
  270. if reflect.DeepEqual(l, tunnelProtocols) {
  271. matchCount += 1
  272. } else if !reflect.DeepEqual(l, defaultLimitTunnelProtocols) {
  273. t.Fatalf("unexpected %+v != %+v", l, defaultLimitTunnelProtocols)
  274. }
  275. }
  276. if matchCount < 250 || matchCount > 750 {
  277. t.Fatalf("Unexpected probability result: %d", matchCount)
  278. }
  279. }
  280. func TestLabeledLists(t *testing.T) {
  281. p, err := NewClientParameters(nil)
  282. if err != nil {
  283. t.Fatalf("NewClientParameters failed: %s", err)
  284. }
  285. tlsProfiles := make(protocol.TLSProfiles, 0)
  286. for i, tlsProfile := range protocol.SupportedTLSProfiles {
  287. if i%2 == 0 {
  288. tlsProfiles = append(tlsProfiles, tlsProfile)
  289. }
  290. }
  291. quicVersions := make(protocol.QUICVersions, 0)
  292. for i, quicVersion := range protocol.SupportedQUICVersions {
  293. if i%2 == 0 {
  294. quicVersions = append(quicVersions, quicVersion)
  295. }
  296. }
  297. applyParameters := map[string]interface{}{
  298. "DisableFrontingProviderTLSProfiles": protocol.LabeledTLSProfiles{"validLabel": tlsProfiles},
  299. "DisableFrontingProviderQUICVersions": protocol.LabeledQUICVersions{"validLabel": quicVersions},
  300. }
  301. _, err = p.Set("", false, applyParameters)
  302. if err != nil {
  303. t.Fatalf("Set failed: %s", err)
  304. }
  305. disableTLSProfiles := p.Get().LabeledTLSProfiles(DisableFrontingProviderTLSProfiles, "validLabel")
  306. if !reflect.DeepEqual(disableTLSProfiles, tlsProfiles) {
  307. t.Fatalf("LabeledTLSProfiles returned %+v expected %+v", disableTLSProfiles, tlsProfiles)
  308. }
  309. disableTLSProfiles = p.Get().LabeledTLSProfiles(DisableFrontingProviderTLSProfiles, "invalidLabel")
  310. if disableTLSProfiles != nil {
  311. t.Fatalf("LabeledTLSProfiles returned unexpected non-empty list %+v", disableTLSProfiles)
  312. }
  313. disableQUICVersions := p.Get().LabeledQUICVersions(DisableFrontingProviderQUICVersions, "validLabel")
  314. if !reflect.DeepEqual(disableQUICVersions, quicVersions) {
  315. t.Fatalf("LabeledQUICVersions returned %+v expected %+v", disableQUICVersions, quicVersions)
  316. }
  317. disableQUICVersions = p.Get().LabeledQUICVersions(DisableFrontingProviderQUICVersions, "invalidLabel")
  318. if disableQUICVersions != nil {
  319. t.Fatalf("LabeledQUICVersions returned unexpected non-empty list %+v", disableQUICVersions)
  320. }
  321. }
  322. func TestCustomTLSProfiles(t *testing.T) {
  323. p, err := NewClientParameters(nil)
  324. if err != nil {
  325. t.Fatalf("NewClientParameters failed: %s", err)
  326. }
  327. customTLSProfiles := protocol.CustomTLSProfiles{
  328. &protocol.CustomTLSProfile{Name: "Profile1", UTLSSpec: &protocol.UTLSSpec{}},
  329. &protocol.CustomTLSProfile{Name: "Profile2", UTLSSpec: &protocol.UTLSSpec{}},
  330. }
  331. applyParameters := map[string]interface{}{
  332. "CustomTLSProfiles": customTLSProfiles}
  333. _, err = p.Set("", false, applyParameters)
  334. if err != nil {
  335. t.Fatalf("Set failed: %s", err)
  336. }
  337. names := p.Get().CustomTLSProfileNames()
  338. if len(names) != 2 || names[0] != "Profile1" || names[1] != "Profile2" {
  339. t.Fatalf("Unexpected CustomTLSProfileNames: %+v", names)
  340. }
  341. profile := p.Get().CustomTLSProfile("Profile1")
  342. if profile == nil || profile.Name != "Profile1" {
  343. t.Fatalf("Unexpected profile")
  344. }
  345. profile = p.Get().CustomTLSProfile("Profile2")
  346. if profile == nil || profile.Name != "Profile2" {
  347. t.Fatalf("Unexpected profile")
  348. }
  349. profile = p.Get().CustomTLSProfile("Profile3")
  350. if profile != nil {
  351. t.Fatalf("Unexpected profile")
  352. }
  353. }
  354. func TestApplicationParameters(t *testing.T) {
  355. parametersJSON := []byte(`
  356. {
  357. "ApplicationParameters" : {
  358. "AppFlag1" : true,
  359. "AppConfig1" : {"Option1" : "A", "Option2" : "B"},
  360. "AppSwitches1" : [1, 2, 3, 4]
  361. }
  362. }
  363. `)
  364. validators := map[string]func(v interface{}) bool{
  365. "AppFlag1": func(v interface{}) bool { return reflect.DeepEqual(v, true) },
  366. "AppConfig1": func(v interface{}) bool {
  367. return reflect.DeepEqual(v, map[string]interface{}{"Option1": "A", "Option2": "B"})
  368. },
  369. "AppSwitches1": func(v interface{}) bool {
  370. return reflect.DeepEqual(v, []interface{}{float64(1), float64(2), float64(3), float64(4)})
  371. },
  372. }
  373. var applyParameters map[string]interface{}
  374. err := json.Unmarshal(parametersJSON, &applyParameters)
  375. if err != nil {
  376. t.Fatalf("Unmarshal failed: %s", err)
  377. }
  378. p, err := NewClientParameters(nil)
  379. if err != nil {
  380. t.Fatalf("NewClientParameters failed: %s", err)
  381. }
  382. _, err = p.Set("", false, applyParameters)
  383. if err != nil {
  384. t.Fatalf("Set failed: %s", err)
  385. }
  386. keyValues := p.Get().KeyValues(ApplicationParameters)
  387. if len(keyValues) != len(validators) {
  388. t.Fatalf("Unexpected key value count")
  389. }
  390. for key, value := range keyValues {
  391. validator, ok := validators[key]
  392. if !ok {
  393. t.Fatalf("Unexpected key: %s", key)
  394. }
  395. var unmarshaledValue interface{}
  396. err := json.Unmarshal(value, &unmarshaledValue)
  397. if err != nil {
  398. t.Fatalf("Unmarshal failed: %s", err)
  399. }
  400. if !validator(unmarshaledValue) {
  401. t.Fatalf("Invalid value: %s, %T: %+v",
  402. key, unmarshaledValue, unmarshaledValue)
  403. }
  404. }
  405. }