parameters_test.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568
  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. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/transforms"
  29. )
  30. func TestGetDefaultParameters(t *testing.T) {
  31. p, err := NewParameters(nil)
  32. if err != nil {
  33. t.Fatalf("NewParameters failed: %s", err)
  34. }
  35. for name, defaults := range defaultParameters {
  36. switch v := defaults.value.(type) {
  37. case string:
  38. g := p.Get().String(name)
  39. if v != g {
  40. t.Fatalf("String returned %+v expected %+v", g, v)
  41. }
  42. case []string:
  43. g := p.Get().Strings(name)
  44. if !reflect.DeepEqual(v, g) {
  45. t.Fatalf("Strings returned %+v expected %+v", g, v)
  46. }
  47. case int:
  48. g := p.Get().Int(name)
  49. if v != g {
  50. t.Fatalf("Int returned %+v expected %+v", g, v)
  51. }
  52. case float64:
  53. g := p.Get().Float(name)
  54. if v != g {
  55. t.Fatalf("Float returned %+v expected %+v", g, v)
  56. }
  57. case bool:
  58. g := p.Get().Bool(name)
  59. if v != g {
  60. t.Fatalf("Bool returned %+v expected %+v", g, v)
  61. }
  62. case time.Duration:
  63. g := p.Get().Duration(name)
  64. if v != g {
  65. t.Fatalf("Duration returned %+v expected %+v", g, v)
  66. }
  67. case protocol.TunnelProtocols:
  68. g := p.Get().TunnelProtocols(name)
  69. if !reflect.DeepEqual(v, g) {
  70. t.Fatalf("TunnelProtocols returned %+v expected %+v", g, v)
  71. }
  72. case protocol.TLSProfiles:
  73. g := p.Get().TLSProfiles(name)
  74. if !reflect.DeepEqual(v, g) {
  75. t.Fatalf("TLSProfiles returned %+v expected %+v", g, v)
  76. }
  77. case protocol.LabeledTLSProfiles:
  78. for label, profiles := range v {
  79. g := p.Get().LabeledTLSProfiles(name, label)
  80. if !reflect.DeepEqual(profiles, g) {
  81. t.Fatalf("LabeledTLSProfiles returned %+v expected %+v", g, profiles)
  82. }
  83. }
  84. case protocol.QUICVersions:
  85. g := p.Get().QUICVersions(name)
  86. if !reflect.DeepEqual(v, g) {
  87. t.Fatalf("QUICVersions returned %+v expected %+v", g, v)
  88. }
  89. case protocol.LabeledQUICVersions:
  90. for label, versions := range v {
  91. g := p.Get().LabeledTLSProfiles(name, label)
  92. if !reflect.DeepEqual(versions, g) {
  93. t.Fatalf("LabeledQUICVersions returned %+v expected %+v", g, versions)
  94. }
  95. }
  96. case TransferURLs:
  97. g := p.Get().TransferURLs(name)
  98. if !reflect.DeepEqual(v, g) {
  99. t.Fatalf("TransferURLs returned %+v expected %+v", g, v)
  100. }
  101. case common.RateLimits:
  102. g := p.Get().RateLimits(name)
  103. if !reflect.DeepEqual(v, g) {
  104. t.Fatalf("RateLimits returned %+v expected %+v", g, v)
  105. }
  106. case http.Header:
  107. g := p.Get().HTTPHeaders(name)
  108. if !reflect.DeepEqual(v, g) {
  109. t.Fatalf("HTTPHeaders returned %+v expected %+v", g, v)
  110. }
  111. case protocol.CustomTLSProfiles:
  112. g := p.Get().CustomTLSProfileNames()
  113. names := make([]string, len(v))
  114. for i, profile := range v {
  115. names[i] = profile.Name
  116. }
  117. if !reflect.DeepEqual(names, g) {
  118. t.Fatalf("CustomTLSProfileNames returned %+v expected %+v", g, names)
  119. }
  120. case KeyValues:
  121. g := p.Get().KeyValues(name)
  122. if !reflect.DeepEqual(v, g) {
  123. t.Fatalf("KeyValues returned %+v expected %+v", g, v)
  124. }
  125. case KeyStrings:
  126. for key, strings := range v {
  127. g := p.Get().KeyStrings(name, key)
  128. if !reflect.DeepEqual(strings, g) {
  129. t.Fatalf("KeyStrings returned %+v expected %+v", g, strings)
  130. }
  131. }
  132. case KeyDurations:
  133. g := p.Get().KeyDurations(name)
  134. durations := make(map[string]time.Duration)
  135. for key, duration := range v {
  136. d, _ := time.ParseDuration(duration)
  137. durations[key] = d
  138. }
  139. if !reflect.DeepEqual(durations, g) {
  140. t.Fatalf("KeyDurations returned %+v expected %+v", g, durations)
  141. }
  142. case *BPFProgramSpec:
  143. ok, name, rawInstructions := p.Get().BPFProgram(name)
  144. if v != nil || ok || name != "" || rawInstructions != nil {
  145. t.Fatalf(
  146. "BPFProgramSpec returned %+v %+v %+v expected %+v",
  147. ok, name, rawInstructions, v)
  148. }
  149. case PacketManipulationSpecs:
  150. g := p.Get().PacketManipulationSpecs(name)
  151. if !reflect.DeepEqual(v, g) {
  152. t.Fatalf("PacketManipulationSpecs returned %+v expected %+v", g, v)
  153. }
  154. case ProtocolPacketManipulations:
  155. g := p.Get().ProtocolPacketManipulations(name)
  156. if !reflect.DeepEqual(v, g) {
  157. t.Fatalf("ProtocolPacketManipulations returned %+v expected %+v", g, v)
  158. }
  159. case RegexStrings:
  160. g := p.Get().RegexStrings(name)
  161. if !reflect.DeepEqual(v, g) {
  162. t.Fatalf("RegexStrings returned %+v expected %+v", g, v)
  163. }
  164. case FrontingSpecs:
  165. g := p.Get().FrontingSpecs(name)
  166. if !reflect.DeepEqual(v, g) {
  167. t.Fatalf("FrontingSpecs returned %+v expected %+v", g, v)
  168. }
  169. case TunnelProtocolPortLists:
  170. g := p.Get().TunnelProtocolPortLists(name)
  171. if !reflect.DeepEqual(v, g) {
  172. t.Fatalf("TunnelProtocolPortLists returned %+v expected %+v", g, v)
  173. }
  174. case LabeledCIDRs:
  175. for label, CIDRs := range v {
  176. g := p.Get().LabeledCIDRs(name, label)
  177. if !reflect.DeepEqual(CIDRs, g) {
  178. t.Fatalf("LabeledCIDRs returned %+v expected %+v", g, CIDRs)
  179. }
  180. }
  181. case transforms.Specs:
  182. g := p.Get().ProtocolTransformSpecs(name)
  183. if !reflect.DeepEqual(v, g) {
  184. t.Fatalf("ProtocolTransformSpecs returned %+v expected %+v", g, v)
  185. }
  186. case transforms.ScopedSpecNames:
  187. g := p.Get().ProtocolTransformScopedSpecNames(name)
  188. if !reflect.DeepEqual(v, g) {
  189. t.Fatalf("ProtocolTransformScopedSpecNames returned %+v expected %+v", g, v)
  190. }
  191. case protocol.LabeledTunnelProtocols:
  192. for label, protocols := range v {
  193. g := p.Get().LabeledTunnelProtocols(name, label)
  194. if !reflect.DeepEqual(protocols, g) {
  195. t.Fatalf("LabeledTunnelProtocols returned %+v expected %+v", g, protocols)
  196. }
  197. }
  198. case protocol.ConjureTransports:
  199. g := p.Get().ConjureTransports(name)
  200. if !reflect.DeepEqual(v, g) {
  201. t.Fatalf("ConjureTransports returned %+v expected %+v", g, v)
  202. }
  203. case InproxyBrokerSpecsValue:
  204. g := p.Get().InproxyBrokerSpecs(name)
  205. if !reflect.DeepEqual(v, g) {
  206. t.Fatalf("ConjureTransports returned %+v expected %+v", g, v)
  207. }
  208. case InproxyCompartmentIDsValue:
  209. g := p.Get().InproxyCompartmentIDs(name)
  210. if !reflect.DeepEqual(v, g) {
  211. t.Fatalf("ConjureTransports returned %+v expected %+v", g, v)
  212. }
  213. case InproxyDataChannelTrafficShapingParametersValue:
  214. g := p.Get().InproxyDataChannelTrafficShapingParameters(name)
  215. if !reflect.DeepEqual(v, g) {
  216. t.Fatalf("ConjureTransports returned %+v expected %+v", g, v)
  217. }
  218. default:
  219. t.Fatalf("Unhandled default type: %s (%T)", name, defaults.value)
  220. }
  221. }
  222. }
  223. func TestGetValueLogger(t *testing.T) {
  224. loggerCalled := false
  225. p, err := NewParameters(
  226. func(error) {
  227. loggerCalled = true
  228. })
  229. if err != nil {
  230. t.Fatalf("NewParameters failed: %s", err)
  231. }
  232. p.Get().Int("unknown-parameter-name")
  233. if !loggerCalled {
  234. t.Fatalf("logged not called")
  235. }
  236. }
  237. func TestOverrides(t *testing.T) {
  238. tag := "tag"
  239. applyParameters := make(map[string]interface{})
  240. // Below minimum, should not apply
  241. defaultConnectionWorkerPoolSize := defaultParameters[ConnectionWorkerPoolSize].value.(int)
  242. minimumConnectionWorkerPoolSize := defaultParameters[ConnectionWorkerPoolSize].minimum.(int)
  243. newConnectionWorkerPoolSize := minimumConnectionWorkerPoolSize - 1
  244. applyParameters[ConnectionWorkerPoolSize] = newConnectionWorkerPoolSize
  245. // Above minimum, should apply
  246. defaultInitialLimitTunnelProtocolsCandidateCount := defaultParameters[InitialLimitTunnelProtocolsCandidateCount].value.(int)
  247. minimumInitialLimitTunnelProtocolsCandidateCount := defaultParameters[InitialLimitTunnelProtocolsCandidateCount].minimum.(int)
  248. newInitialLimitTunnelProtocolsCandidateCount := minimumInitialLimitTunnelProtocolsCandidateCount + 1
  249. applyParameters[InitialLimitTunnelProtocolsCandidateCount] = newInitialLimitTunnelProtocolsCandidateCount
  250. p, err := NewParameters(nil)
  251. if err != nil {
  252. t.Fatalf("NewParameters failed: %s", err)
  253. }
  254. // No skip on error; should fail and not apply any changes
  255. _, err = p.Set(tag, 0, applyParameters)
  256. if err == nil {
  257. t.Fatalf("Set succeeded unexpectedly")
  258. }
  259. if p.Get().Tag() != "" {
  260. t.Fatalf("GetTag returned unexpected value")
  261. }
  262. v := p.Get().Int(ConnectionWorkerPoolSize)
  263. if v != defaultConnectionWorkerPoolSize {
  264. t.Fatalf("GetInt returned unexpected ConnectionWorkerPoolSize: %d", v)
  265. }
  266. v = p.Get().Int(InitialLimitTunnelProtocolsCandidateCount)
  267. if v != defaultInitialLimitTunnelProtocolsCandidateCount {
  268. t.Fatalf("GetInt returned unexpected InitialLimitTunnelProtocolsCandidateCount: %d", v)
  269. }
  270. // Skip on error; should skip ConnectionWorkerPoolSize and apply InitialLimitTunnelProtocolsCandidateCount
  271. counts, err := p.Set(tag, ValidationSkipOnError, applyParameters)
  272. if err != nil {
  273. t.Fatalf("Set failed: %s", err)
  274. }
  275. if counts[0] != 1 {
  276. t.Fatalf("Apply returned unexpected count: %d", counts[0])
  277. }
  278. v = p.Get().Int(ConnectionWorkerPoolSize)
  279. if v != defaultConnectionWorkerPoolSize {
  280. t.Fatalf("GetInt returned unexpected ConnectionWorkerPoolSize: %d", v)
  281. }
  282. v = p.Get().Int(InitialLimitTunnelProtocolsCandidateCount)
  283. if v != newInitialLimitTunnelProtocolsCandidateCount {
  284. t.Fatalf("GetInt returned unexpected InitialLimitTunnelProtocolsCandidateCount: %d", v)
  285. }
  286. }
  287. func TestNetworkLatencyMultiplier(t *testing.T) {
  288. p, err := NewParameters(nil)
  289. if err != nil {
  290. t.Fatalf("NewParameters failed: %s", err)
  291. }
  292. timeout1 := p.Get().Duration(TunnelConnectTimeout)
  293. applyParameters := map[string]interface{}{"NetworkLatencyMultiplier": 2.0}
  294. _, err = p.Set("", 0, applyParameters)
  295. if err != nil {
  296. t.Fatalf("Set failed: %s", err)
  297. }
  298. timeout2 := p.Get().Duration(TunnelConnectTimeout)
  299. if 2*timeout1 != timeout2 {
  300. t.Fatalf("Unexpected timeouts: 2 * %s != %s", timeout1, timeout2)
  301. }
  302. }
  303. func TestCustomNetworkLatencyMultiplier(t *testing.T) {
  304. p, err := NewParameters(nil)
  305. if err != nil {
  306. t.Fatalf("NewParameters failed: %s", err)
  307. }
  308. timeout1 := p.Get().Duration(TunnelConnectTimeout)
  309. applyParameters := map[string]interface{}{"NetworkLatencyMultiplier": 2.0}
  310. _, err = p.Set("", 0, applyParameters)
  311. if err != nil {
  312. t.Fatalf("Set failed: %s", err)
  313. }
  314. timeout2 := p.GetCustom(4.0).Duration(TunnelConnectTimeout)
  315. if 4*timeout1 != timeout2 {
  316. t.Fatalf("Unexpected timeouts: 4 * %s != %s", timeout1, timeout2)
  317. }
  318. }
  319. func TestLimitTunnelProtocolProbability(t *testing.T) {
  320. p, err := NewParameters(nil)
  321. if err != nil {
  322. t.Fatalf("NewParameters failed: %s", err)
  323. }
  324. // Default probability should be 1.0 and always return tunnelProtocols
  325. tunnelProtocols := protocol.TunnelProtocols{"OSSH", "SSH"}
  326. applyParameters := map[string]interface{}{
  327. "LimitTunnelProtocols": tunnelProtocols,
  328. }
  329. _, err = p.Set("", 0, applyParameters)
  330. if err != nil {
  331. t.Fatalf("Set failed: %s", err)
  332. }
  333. for i := 0; i < 1000; i++ {
  334. l := p.Get().TunnelProtocols(LimitTunnelProtocols)
  335. if !reflect.DeepEqual(l, tunnelProtocols) {
  336. t.Fatalf("unexpected %+v != %+v", l, tunnelProtocols)
  337. }
  338. }
  339. // With probability set to 0.5, should return tunnelProtocols ~50%
  340. defaultLimitTunnelProtocols := protocol.TunnelProtocols{}
  341. applyParameters = map[string]interface{}{
  342. "LimitTunnelProtocolsProbability": 0.5,
  343. "LimitTunnelProtocols": tunnelProtocols,
  344. }
  345. _, err = p.Set("", 0, applyParameters)
  346. if err != nil {
  347. t.Fatalf("Set failed: %s", err)
  348. }
  349. matchCount := 0
  350. for i := 0; i < 1000; i++ {
  351. l := p.Get().TunnelProtocols(LimitTunnelProtocols)
  352. if reflect.DeepEqual(l, tunnelProtocols) {
  353. matchCount += 1
  354. } else if !reflect.DeepEqual(l, defaultLimitTunnelProtocols) {
  355. t.Fatalf("unexpected %+v != %+v", l, defaultLimitTunnelProtocols)
  356. }
  357. }
  358. if matchCount < 250 || matchCount > 750 {
  359. t.Fatalf("Unexpected probability result: %d", matchCount)
  360. }
  361. }
  362. func TestLabeledLists(t *testing.T) {
  363. p, err := NewParameters(nil)
  364. if err != nil {
  365. t.Fatalf("NewParameters failed: %s", err)
  366. }
  367. tlsProfiles := make(protocol.TLSProfiles, 0)
  368. for i, tlsProfile := range protocol.SupportedTLSProfiles {
  369. if i%2 == 0 {
  370. tlsProfiles = append(tlsProfiles, tlsProfile)
  371. }
  372. }
  373. quicVersions := make(protocol.QUICVersions, 0)
  374. for i, quicVersion := range protocol.SupportedQUICVersions {
  375. if i%2 == 0 {
  376. quicVersions = append(quicVersions, quicVersion)
  377. }
  378. }
  379. applyParameters := map[string]interface{}{
  380. "DisableFrontingProviderTLSProfiles": protocol.LabeledTLSProfiles{"validLabel": tlsProfiles},
  381. "DisableFrontingProviderQUICVersions": protocol.LabeledQUICVersions{"validLabel": quicVersions},
  382. }
  383. _, err = p.Set("", 0, applyParameters)
  384. if err != nil {
  385. t.Fatalf("Set failed: %s", err)
  386. }
  387. disableTLSProfiles := p.Get().LabeledTLSProfiles(DisableFrontingProviderTLSProfiles, "validLabel")
  388. if !reflect.DeepEqual(disableTLSProfiles, tlsProfiles) {
  389. t.Fatalf("LabeledTLSProfiles returned %+v expected %+v", disableTLSProfiles, tlsProfiles)
  390. }
  391. disableTLSProfiles = p.Get().LabeledTLSProfiles(DisableFrontingProviderTLSProfiles, "invalidLabel")
  392. if disableTLSProfiles != nil {
  393. t.Fatalf("LabeledTLSProfiles returned unexpected non-empty list %+v", disableTLSProfiles)
  394. }
  395. disableQUICVersions := p.Get().LabeledQUICVersions(DisableFrontingProviderQUICVersions, "validLabel")
  396. if !reflect.DeepEqual(disableQUICVersions, quicVersions) {
  397. t.Fatalf("LabeledQUICVersions returned %+v expected %+v", disableQUICVersions, quicVersions)
  398. }
  399. disableQUICVersions = p.Get().LabeledQUICVersions(DisableFrontingProviderQUICVersions, "invalidLabel")
  400. if disableQUICVersions != nil {
  401. t.Fatalf("LabeledQUICVersions returned unexpected non-empty list %+v", disableQUICVersions)
  402. }
  403. }
  404. func TestCustomTLSProfiles(t *testing.T) {
  405. p, err := NewParameters(nil)
  406. if err != nil {
  407. t.Fatalf("NewParameters failed: %s", err)
  408. }
  409. customTLSProfiles := protocol.CustomTLSProfiles{
  410. &protocol.CustomTLSProfile{Name: "Profile1", UTLSSpec: &protocol.UTLSSpec{}},
  411. &protocol.CustomTLSProfile{Name: "Profile2", UTLSSpec: &protocol.UTLSSpec{}},
  412. }
  413. applyParameters := map[string]interface{}{
  414. "CustomTLSProfiles": customTLSProfiles}
  415. _, err = p.Set("", 0, applyParameters)
  416. if err != nil {
  417. t.Fatalf("Set failed: %s", err)
  418. }
  419. names := p.Get().CustomTLSProfileNames()
  420. if len(names) != 2 || names[0] != "Profile1" || names[1] != "Profile2" {
  421. t.Fatalf("Unexpected CustomTLSProfileNames: %+v", names)
  422. }
  423. profile := p.Get().CustomTLSProfile("Profile1")
  424. if profile == nil || profile.Name != "Profile1" {
  425. t.Fatalf("Unexpected profile")
  426. }
  427. profile = p.Get().CustomTLSProfile("Profile2")
  428. if profile == nil || profile.Name != "Profile2" {
  429. t.Fatalf("Unexpected profile")
  430. }
  431. profile = p.Get().CustomTLSProfile("Profile3")
  432. if profile != nil {
  433. t.Fatalf("Unexpected profile")
  434. }
  435. }
  436. func TestApplicationParameters(t *testing.T) {
  437. parametersJSON := []byte(`
  438. {
  439. "ApplicationParameters" : {
  440. "AppFlag1" : true,
  441. "AppConfig1" : {"Option1" : "A", "Option2" : "B"},
  442. "AppSwitches1" : [1, 2, 3, 4]
  443. }
  444. }
  445. `)
  446. validators := map[string]func(v interface{}) bool{
  447. "AppFlag1": func(v interface{}) bool { return reflect.DeepEqual(v, true) },
  448. "AppConfig1": func(v interface{}) bool {
  449. return reflect.DeepEqual(v, map[string]interface{}{"Option1": "A", "Option2": "B"})
  450. },
  451. "AppSwitches1": func(v interface{}) bool {
  452. return reflect.DeepEqual(v, []interface{}{float64(1), float64(2), float64(3), float64(4)})
  453. },
  454. }
  455. var applyParameters map[string]interface{}
  456. err := json.Unmarshal(parametersJSON, &applyParameters)
  457. if err != nil {
  458. t.Fatalf("Unmarshal failed: %s", err)
  459. }
  460. p, err := NewParameters(nil)
  461. if err != nil {
  462. t.Fatalf("NewParameters failed: %s", err)
  463. }
  464. _, err = p.Set("", 0, applyParameters)
  465. if err != nil {
  466. t.Fatalf("Set failed: %s", err)
  467. }
  468. keyValues := p.Get().KeyValues(ApplicationParameters)
  469. if len(keyValues) != len(validators) {
  470. t.Fatalf("Unexpected key value count")
  471. }
  472. for key, value := range keyValues {
  473. validator, ok := validators[key]
  474. if !ok {
  475. t.Fatalf("Unexpected key: %s", key)
  476. }
  477. var unmarshaledValue interface{}
  478. err := json.Unmarshal(value, &unmarshaledValue)
  479. if err != nil {
  480. t.Fatalf("Unmarshal failed: %s", err)
  481. }
  482. if !validator(unmarshaledValue) {
  483. t.Fatalf("Invalid value: %s, %T: %+v",
  484. key, unmarshaledValue, unmarshaledValue)
  485. }
  486. }
  487. }