parameters_test.go 15 KB

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