clientParameters_test.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  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. )
  29. func TestGetDefaultParameters(t *testing.T) {
  30. p, err := NewClientParameters(nil)
  31. if err != nil {
  32. t.Fatalf("NewClientParameters failed: %s", err)
  33. }
  34. for name, defaults := range defaultClientParameters {
  35. switch v := defaults.value.(type) {
  36. case string:
  37. g := p.Get().String(name)
  38. if v != g {
  39. t.Fatalf("String returned %+v expected %+v", g, v)
  40. }
  41. case int:
  42. g := p.Get().Int(name)
  43. if v != g {
  44. t.Fatalf("Int returned %+v expected %+v", g, v)
  45. }
  46. case float64:
  47. g := p.Get().Float(name)
  48. if v != g {
  49. t.Fatalf("Float returned %+v expected %+v", g, v)
  50. }
  51. case bool:
  52. g := p.Get().Bool(name)
  53. if v != g {
  54. t.Fatalf("Bool returned %+v expected %+v", g, v)
  55. }
  56. case time.Duration:
  57. g := p.Get().Duration(name)
  58. if v != g {
  59. t.Fatalf("Duration returned %+v expected %+v", g, v)
  60. }
  61. case protocol.TunnelProtocols:
  62. g := p.Get().TunnelProtocols(name)
  63. if !reflect.DeepEqual(v, g) {
  64. t.Fatalf("TunnelProtocols returned %+v expected %+v", g, v)
  65. }
  66. case protocol.TLSProfiles:
  67. g := p.Get().TLSProfiles(name)
  68. if !reflect.DeepEqual(v, g) {
  69. t.Fatalf("TLSProfiles returned %+v expected %+v", g, v)
  70. }
  71. case protocol.QUICVersions:
  72. g := p.Get().QUICVersions(name)
  73. if !reflect.DeepEqual(v, g) {
  74. t.Fatalf("QUICVersions returned %+v expected %+v", g, v)
  75. }
  76. case DownloadURLs:
  77. g := p.Get().DownloadURLs(name)
  78. if !reflect.DeepEqual(v, g) {
  79. t.Fatalf("DownloadURLs returned %+v expected %+v", g, v)
  80. }
  81. case common.RateLimits:
  82. g := p.Get().RateLimits(name)
  83. if !reflect.DeepEqual(v, g) {
  84. t.Fatalf("RateLimits returned %+v expected %+v", g, v)
  85. }
  86. case http.Header:
  87. g := p.Get().HTTPHeaders(name)
  88. if !reflect.DeepEqual(v, g) {
  89. t.Fatalf("HTTPHeaders returned %+v expected %+v", g, v)
  90. }
  91. case protocol.CustomTLSProfiles:
  92. g := p.Get().CustomTLSProfileNames()
  93. names := make([]string, len(v))
  94. for i, profile := range v {
  95. names[i] = profile.Name
  96. }
  97. if !reflect.DeepEqual(names, g) {
  98. t.Fatalf("CustomTLSProfileNames returned %+v expected %+v", g, names)
  99. }
  100. case KeyValues:
  101. g := p.Get().KeyValues(name)
  102. if !reflect.DeepEqual(v, g) {
  103. t.Fatalf("KeyValues returned %+v expected %+v", g, v)
  104. }
  105. case *BPFProgramSpec:
  106. ok, name, rawInstructions := p.Get().BPFProgram(name)
  107. if v != nil || ok || name != "" || rawInstructions != nil {
  108. t.Fatalf(
  109. "BPFProgramSpec returned %+v %+v %+v expected %+v",
  110. ok, name, rawInstructions, v)
  111. }
  112. default:
  113. t.Fatalf("Unhandled default type: %s", name)
  114. }
  115. }
  116. }
  117. func TestGetValueLogger(t *testing.T) {
  118. loggerCalled := false
  119. p, err := NewClientParameters(
  120. func(error) {
  121. loggerCalled = true
  122. })
  123. if err != nil {
  124. t.Fatalf("NewClientParameters failed: %s", err)
  125. }
  126. p.Get().Int("unknown-parameter-name")
  127. if !loggerCalled {
  128. t.Fatalf("logged not called")
  129. }
  130. }
  131. func TestOverrides(t *testing.T) {
  132. tag := "tag"
  133. applyParameters := make(map[string]interface{})
  134. // Below minimum, should not apply
  135. defaultConnectionWorkerPoolSize := defaultClientParameters[ConnectionWorkerPoolSize].value.(int)
  136. minimumConnectionWorkerPoolSize := defaultClientParameters[ConnectionWorkerPoolSize].minimum.(int)
  137. newConnectionWorkerPoolSize := minimumConnectionWorkerPoolSize - 1
  138. applyParameters[ConnectionWorkerPoolSize] = newConnectionWorkerPoolSize
  139. // Above minimum, should apply
  140. defaultInitialLimitTunnelProtocolsCandidateCount := defaultClientParameters[InitialLimitTunnelProtocolsCandidateCount].value.(int)
  141. minimumInitialLimitTunnelProtocolsCandidateCount := defaultClientParameters[InitialLimitTunnelProtocolsCandidateCount].minimum.(int)
  142. newInitialLimitTunnelProtocolsCandidateCount := minimumInitialLimitTunnelProtocolsCandidateCount + 1
  143. applyParameters[InitialLimitTunnelProtocolsCandidateCount] = newInitialLimitTunnelProtocolsCandidateCount
  144. p, err := NewClientParameters(nil)
  145. if err != nil {
  146. t.Fatalf("NewClientParameters failed: %s", err)
  147. }
  148. // No skip on error; should fail and not apply any changes
  149. _, err = p.Set(tag, false, applyParameters)
  150. if err == nil {
  151. t.Fatalf("Set succeeded unexpectedly")
  152. }
  153. if p.Get().Tag() != "" {
  154. t.Fatalf("GetTag returned unexpected value")
  155. }
  156. v := p.Get().Int(ConnectionWorkerPoolSize)
  157. if v != defaultConnectionWorkerPoolSize {
  158. t.Fatalf("GetInt returned unexpected ConnectionWorkerPoolSize: %d", v)
  159. }
  160. v = p.Get().Int(InitialLimitTunnelProtocolsCandidateCount)
  161. if v != defaultInitialLimitTunnelProtocolsCandidateCount {
  162. t.Fatalf("GetInt returned unexpected InitialLimitTunnelProtocolsCandidateCount: %d", v)
  163. }
  164. // Skip on error; should skip ConnectionWorkerPoolSize and apply InitialLimitTunnelProtocolsCandidateCount
  165. counts, err := p.Set(tag, true, applyParameters)
  166. if err != nil {
  167. t.Fatalf("Set failed: %s", err)
  168. }
  169. if counts[0] != 1 {
  170. t.Fatalf("Apply returned unexpected count: %d", counts[0])
  171. }
  172. v = p.Get().Int(ConnectionWorkerPoolSize)
  173. if v != defaultConnectionWorkerPoolSize {
  174. t.Fatalf("GetInt returned unexpected ConnectionWorkerPoolSize: %d", v)
  175. }
  176. v = p.Get().Int(InitialLimitTunnelProtocolsCandidateCount)
  177. if v != newInitialLimitTunnelProtocolsCandidateCount {
  178. t.Fatalf("GetInt returned unexpected InitialLimitTunnelProtocolsCandidateCount: %d", v)
  179. }
  180. }
  181. func TestNetworkLatencyMultiplier(t *testing.T) {
  182. p, err := NewClientParameters(nil)
  183. if err != nil {
  184. t.Fatalf("NewClientParameters failed: %s", err)
  185. }
  186. timeout1 := p.Get().Duration(TunnelConnectTimeout)
  187. applyParameters := map[string]interface{}{"NetworkLatencyMultiplier": 2.0}
  188. _, err = p.Set("", false, applyParameters)
  189. if err != nil {
  190. t.Fatalf("Set failed: %s", err)
  191. }
  192. timeout2 := p.Get().Duration(TunnelConnectTimeout)
  193. if 2*timeout1 != timeout2 {
  194. t.Fatalf("Unexpected timeouts: 2 * %s != %s", timeout1, timeout2)
  195. }
  196. }
  197. func TestCustomNetworkLatencyMultiplier(t *testing.T) {
  198. p, err := NewClientParameters(nil)
  199. if err != nil {
  200. t.Fatalf("NewClientParameters failed: %s", err)
  201. }
  202. timeout1 := p.Get().Duration(TunnelConnectTimeout)
  203. applyParameters := map[string]interface{}{"NetworkLatencyMultiplier": 2.0}
  204. _, err = p.Set("", false, applyParameters)
  205. if err != nil {
  206. t.Fatalf("Set failed: %s", err)
  207. }
  208. timeout2 := p.GetCustom(4.0).Duration(TunnelConnectTimeout)
  209. if 4*timeout1 != timeout2 {
  210. t.Fatalf("Unexpected timeouts: 4 * %s != %s", timeout1, timeout2)
  211. }
  212. }
  213. func TestLimitTunnelProtocolProbability(t *testing.T) {
  214. p, err := NewClientParameters(nil)
  215. if err != nil {
  216. t.Fatalf("NewClientParameters failed: %s", err)
  217. }
  218. // Default probability should be 1.0 and always return tunnelProtocols
  219. tunnelProtocols := protocol.TunnelProtocols{"OSSH", "SSH"}
  220. applyParameters := map[string]interface{}{
  221. "LimitTunnelProtocols": tunnelProtocols,
  222. }
  223. _, err = p.Set("", false, applyParameters)
  224. if err != nil {
  225. t.Fatalf("Set failed: %s", err)
  226. }
  227. for i := 0; i < 1000; i++ {
  228. l := p.Get().TunnelProtocols(LimitTunnelProtocols)
  229. if !reflect.DeepEqual(l, tunnelProtocols) {
  230. t.Fatalf("unexpected %+v != %+v", l, tunnelProtocols)
  231. }
  232. }
  233. // With probability set to 0.5, should return tunnelProtocols ~50%
  234. defaultLimitTunnelProtocols := protocol.TunnelProtocols{}
  235. applyParameters = map[string]interface{}{
  236. "LimitTunnelProtocolsProbability": 0.5,
  237. "LimitTunnelProtocols": tunnelProtocols,
  238. }
  239. _, err = p.Set("", false, applyParameters)
  240. if err != nil {
  241. t.Fatalf("Set failed: %s", err)
  242. }
  243. matchCount := 0
  244. for i := 0; i < 1000; i++ {
  245. l := p.Get().TunnelProtocols(LimitTunnelProtocols)
  246. if reflect.DeepEqual(l, tunnelProtocols) {
  247. matchCount += 1
  248. } else if !reflect.DeepEqual(l, defaultLimitTunnelProtocols) {
  249. t.Fatalf("unexpected %+v != %+v", l, defaultLimitTunnelProtocols)
  250. }
  251. }
  252. if matchCount < 250 || matchCount > 750 {
  253. t.Fatalf("Unexpected probability result: %d", matchCount)
  254. }
  255. }
  256. func TestCustomTLSProfiles(t *testing.T) {
  257. p, err := NewClientParameters(nil)
  258. if err != nil {
  259. t.Fatalf("NewClientParameters failed: %s", err)
  260. }
  261. customTLSProfiles := protocol.CustomTLSProfiles{
  262. &protocol.CustomTLSProfile{Name: "Profile1", UTLSSpec: &protocol.UTLSSpec{}},
  263. &protocol.CustomTLSProfile{Name: "Profile2", UTLSSpec: &protocol.UTLSSpec{}},
  264. }
  265. applyParameters := map[string]interface{}{
  266. "CustomTLSProfiles": customTLSProfiles}
  267. _, err = p.Set("", false, applyParameters)
  268. if err != nil {
  269. t.Fatalf("Set failed: %s", err)
  270. }
  271. names := p.Get().CustomTLSProfileNames()
  272. if len(names) != 2 || names[0] != "Profile1" || names[1] != "Profile2" {
  273. t.Fatalf("Unexpected CustomTLSProfileNames: %+v", names)
  274. }
  275. profile := p.Get().CustomTLSProfile("Profile1")
  276. if profile == nil || profile.Name != "Profile1" {
  277. t.Fatalf("Unexpected profile")
  278. }
  279. profile = p.Get().CustomTLSProfile("Profile2")
  280. if profile == nil || profile.Name != "Profile2" {
  281. t.Fatalf("Unexpected profile")
  282. }
  283. profile = p.Get().CustomTLSProfile("Profile3")
  284. if profile != nil {
  285. t.Fatalf("Unexpected profile")
  286. }
  287. }
  288. func TestApplicationParameters(t *testing.T) {
  289. parametersJSON := []byte(`
  290. {
  291. "ApplicationParameters" : {
  292. "AppFlag1" : true,
  293. "AppConfig1" : {"Option1" : "A", "Option2" : "B"},
  294. "AppSwitches1" : [1, 2, 3, 4]
  295. }
  296. }
  297. `)
  298. validators := map[string]func(v interface{}) bool{
  299. "AppFlag1": func(v interface{}) bool { return reflect.DeepEqual(v, true) },
  300. "AppConfig1": func(v interface{}) bool {
  301. return reflect.DeepEqual(v, map[string]interface{}{"Option1": "A", "Option2": "B"})
  302. },
  303. "AppSwitches1": func(v interface{}) bool {
  304. return reflect.DeepEqual(v, []interface{}{float64(1), float64(2), float64(3), float64(4)})
  305. },
  306. }
  307. var applyParameters map[string]interface{}
  308. err := json.Unmarshal(parametersJSON, &applyParameters)
  309. if err != nil {
  310. t.Fatalf("Unmarshal failed: %s", err)
  311. }
  312. p, err := NewClientParameters(nil)
  313. if err != nil {
  314. t.Fatalf("NewClientParameters failed: %s", err)
  315. }
  316. _, err = p.Set("", false, applyParameters)
  317. if err != nil {
  318. t.Fatalf("Set failed: %s", err)
  319. }
  320. keyValues := p.Get().KeyValues(ApplicationParameters)
  321. if len(keyValues) != len(validators) {
  322. t.Fatalf("Unexpected key value count")
  323. }
  324. for key, value := range keyValues {
  325. validator, ok := validators[key]
  326. if !ok {
  327. t.Fatalf("Unexpected key: %s", key)
  328. }
  329. var unmarshaledValue interface{}
  330. err := json.Unmarshal(value, &unmarshaledValue)
  331. if err != nil {
  332. t.Fatalf("Unmarshal failed: %s", err)
  333. }
  334. if !validator(unmarshaledValue) {
  335. t.Fatalf("Invalid value: %s, %T: %+v",
  336. key, unmarshaledValue, unmarshaledValue)
  337. }
  338. }
  339. }