parameters_test.go 17 KB

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