clientParameters_test.go 5.6 KB

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