parameters_test.go 16 KB

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