clientParameters_test.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. "net/http"
  22. "reflect"
  23. "testing"
  24. "time"
  25. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common"
  26. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/protocol"
  27. )
  28. func TestGetDefaultParameters(t *testing.T) {
  29. p, err := NewClientParameters(nil)
  30. if err != nil {
  31. t.Fatalf("NewClientParameters failed: %s", err)
  32. }
  33. for name, defaults := range defaultClientParameters {
  34. switch v := defaults.value.(type) {
  35. case string:
  36. g := p.Get().String(name)
  37. if v != g {
  38. t.Fatalf("String returned %+v expected %+v", v, g)
  39. }
  40. case int:
  41. g := p.Get().Int(name)
  42. if v != g {
  43. t.Fatalf("Int returned %+v expected %+v", v, g)
  44. }
  45. case float64:
  46. g := p.Get().Float(name)
  47. if v != g {
  48. t.Fatalf("Float returned %+v expected %+v", v, g)
  49. }
  50. case bool:
  51. g := p.Get().Bool(name)
  52. if v != g {
  53. t.Fatalf("Bool returned %+v expected %+v", v, g)
  54. }
  55. case time.Duration:
  56. g := p.Get().Duration(name)
  57. if v != g {
  58. t.Fatalf("Duration returned %+v expected %+v", v, g)
  59. }
  60. case protocol.TunnelProtocols:
  61. g := p.Get().TunnelProtocols(name)
  62. if !reflect.DeepEqual(v, g) {
  63. t.Fatalf("TunnelProtocols returned %+v expected %+v", v, g)
  64. }
  65. case protocol.TLSProfiles:
  66. g := p.Get().TLSProfiles(name)
  67. if !reflect.DeepEqual(v, g) {
  68. t.Fatalf("TLSProfiles returned %+v expected %+v", v, g)
  69. }
  70. case DownloadURLs:
  71. g := p.Get().DownloadURLs(name)
  72. if !reflect.DeepEqual(v, g) {
  73. t.Fatalf("DownloadURLs returned %+v expected %+v", v, g)
  74. }
  75. case common.RateLimits:
  76. g := p.Get().RateLimits(name)
  77. if !reflect.DeepEqual(v, g) {
  78. t.Fatalf("RateLimits returned %+v expected %+v", v, g)
  79. }
  80. case http.Header:
  81. g := p.Get().HTTPHeaders(name)
  82. if !reflect.DeepEqual(v, g) {
  83. t.Fatalf("HTTPHeaders returned %+v expected %+v", v, g)
  84. }
  85. default:
  86. t.Fatalf("Unhandled default type: %s", name)
  87. }
  88. }
  89. }
  90. func TestGetValueLogger(t *testing.T) {
  91. loggerCalled := false
  92. p, err := NewClientParameters(
  93. func(error) {
  94. loggerCalled = true
  95. })
  96. if err != nil {
  97. t.Fatalf("NewClientParameters failed: %s", err)
  98. }
  99. p.Get().Int("unknown-parameter-name")
  100. if !loggerCalled {
  101. t.Fatalf("logged not called")
  102. }
  103. }
  104. func TestOverrides(t *testing.T) {
  105. tag := "tag"
  106. applyParameters := make(map[string]interface{})
  107. // Below minimum, should not apply
  108. defaultConnectionWorkerPoolSize := defaultClientParameters[ConnectionWorkerPoolSize].value.(int)
  109. minimumConnectionWorkerPoolSize := defaultClientParameters[ConnectionWorkerPoolSize].minimum.(int)
  110. newConnectionWorkerPoolSize := minimumConnectionWorkerPoolSize - 1
  111. applyParameters[ConnectionWorkerPoolSize] = newConnectionWorkerPoolSize
  112. // Above minimum, should apply
  113. defaultPrioritizeTunnelProtocolsCandidateCount := defaultClientParameters[PrioritizeTunnelProtocolsCandidateCount].value.(int)
  114. minimumPrioritizeTunnelProtocolsCandidateCount := defaultClientParameters[PrioritizeTunnelProtocolsCandidateCount].minimum.(int)
  115. newPrioritizeTunnelProtocolsCandidateCount := minimumPrioritizeTunnelProtocolsCandidateCount + 1
  116. applyParameters[PrioritizeTunnelProtocolsCandidateCount] = newPrioritizeTunnelProtocolsCandidateCount
  117. p, err := NewClientParameters(nil)
  118. if err != nil {
  119. t.Fatalf("NewClientParameters failed: %s", err)
  120. }
  121. // No skip on error; should fail and not apply any changes
  122. _, err = p.Set(tag, false, applyParameters)
  123. if err == nil {
  124. t.Fatalf("Set succeeded unexpectedly")
  125. }
  126. if p.Get().Tag() != "" {
  127. t.Fatalf("GetTag returned unexpected value")
  128. }
  129. v := p.Get().Int(ConnectionWorkerPoolSize)
  130. if v != defaultConnectionWorkerPoolSize {
  131. t.Fatalf("GetInt returned unexpected ConnectionWorkerPoolSize: %d", v)
  132. }
  133. v = p.Get().Int(PrioritizeTunnelProtocolsCandidateCount)
  134. if v != defaultPrioritizeTunnelProtocolsCandidateCount {
  135. t.Fatalf("GetInt returned unexpected PrioritizeTunnelProtocolsCandidateCount: %d", v)
  136. }
  137. // Skip on error; should skip ConnectionWorkerPoolSize and apply PrioritizeTunnelProtocolsCandidateCount
  138. counts, err := p.Set(tag, true, applyParameters)
  139. if err != nil {
  140. t.Fatalf("Set failed: %s", err)
  141. }
  142. if counts[0] != 1 {
  143. t.Fatalf("Apply returned unexpected count: %d", counts[0])
  144. }
  145. v = p.Get().Int(ConnectionWorkerPoolSize)
  146. if v != defaultConnectionWorkerPoolSize {
  147. t.Fatalf("GetInt returned unexpected ConnectionWorkerPoolSize: %d", v)
  148. }
  149. v = p.Get().Int(PrioritizeTunnelProtocolsCandidateCount)
  150. if v != newPrioritizeTunnelProtocolsCandidateCount {
  151. t.Fatalf("GetInt returned unexpected PrioritizeTunnelProtocolsCandidateCount: %d", v)
  152. }
  153. }
  154. func TestNetworkLatencyMultiplier(t *testing.T) {
  155. p, err := NewClientParameters(nil)
  156. if err != nil {
  157. t.Fatalf("NewClientParameters failed: %s", err)
  158. }
  159. timeout1 := p.Get().Duration(TunnelConnectTimeout)
  160. applyParameters := map[string]interface{}{"NetworkLatencyMultiplier": 2.0}
  161. _, err = p.Set("", false, applyParameters)
  162. if err != nil {
  163. t.Fatalf("Set failed: %s", err)
  164. }
  165. timeout2 := p.Get().Duration(TunnelConnectTimeout)
  166. if 2*timeout1 != timeout2 {
  167. t.Fatalf("Unexpected timeouts: 2 * %s != %s", timeout1, timeout2)
  168. }
  169. }