parameters_test.go 16 KB

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