clientlib_test.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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 clientlib
  20. import (
  21. "context"
  22. "encoding/json"
  23. "io/ioutil"
  24. "os"
  25. "testing"
  26. "time"
  27. )
  28. func TestStartTunnel(t *testing.T) {
  29. // TODO: More comprehensive tests. This is only a smoke test.
  30. clientPlatform := "clientlib_test.go"
  31. networkID := "UNKNOWN"
  32. timeout := 60
  33. quickTimeout := 1
  34. configJSON, err := ioutil.ReadFile("../../psiphon/controller_test.config")
  35. if err != nil {
  36. // Skip, don't fail, if config file is not present
  37. t.Skipf("error loading configuration file: %s", err)
  38. }
  39. // Initialize a fresh datastore and create a modified config which cannot
  40. // connect without known servers, to be used in timeout cases.
  41. testDataDirName, err := ioutil.TempDir("", "psiphon-clientlib-test")
  42. if err != nil {
  43. t.Fatalf("ioutil.TempDir failed: %v", err)
  44. }
  45. defer os.RemoveAll(testDataDirName)
  46. var config map[string]interface{}
  47. err = json.Unmarshal(configJSON, &config)
  48. if err != nil {
  49. t.Fatalf("json.Unmarshal failed: %v", err)
  50. }
  51. config["DisableRemoteServerListFetcher"] = true
  52. configJSONNoFetcher, err := json.Marshal(config)
  53. if err != nil {
  54. t.Fatalf("json.Marshal failed: %v", err)
  55. }
  56. type args struct {
  57. ctxTimeout time.Duration
  58. configJSON []byte
  59. embeddedServerEntryList string
  60. params Parameters
  61. paramsDelta ParametersDelta
  62. noticeReceiver func(NoticeEvent)
  63. }
  64. tests := []struct {
  65. name string
  66. args args
  67. wantTunnel bool
  68. expectedErr error
  69. }{
  70. {
  71. name: "Failure: context timeout",
  72. args: args{
  73. ctxTimeout: 10 * time.Millisecond,
  74. configJSON: configJSONNoFetcher,
  75. embeddedServerEntryList: "",
  76. params: Parameters{
  77. DataRootDirectory: &testDataDirName,
  78. ClientPlatform: &clientPlatform,
  79. NetworkID: &networkID,
  80. EstablishTunnelTimeoutSeconds: &timeout,
  81. },
  82. paramsDelta: nil,
  83. noticeReceiver: nil,
  84. },
  85. wantTunnel: false,
  86. expectedErr: ErrTimeout,
  87. },
  88. {
  89. name: "Failure: config timeout",
  90. args: args{
  91. ctxTimeout: 0,
  92. configJSON: configJSONNoFetcher,
  93. embeddedServerEntryList: "",
  94. params: Parameters{
  95. DataRootDirectory: &testDataDirName,
  96. ClientPlatform: &clientPlatform,
  97. NetworkID: &networkID,
  98. EstablishTunnelTimeoutSeconds: &quickTimeout,
  99. },
  100. paramsDelta: nil,
  101. noticeReceiver: nil,
  102. },
  103. wantTunnel: false,
  104. expectedErr: ErrTimeout,
  105. },
  106. {
  107. name: "Success: simple",
  108. args: args{
  109. ctxTimeout: 0,
  110. configJSON: configJSON,
  111. embeddedServerEntryList: "",
  112. params: Parameters{
  113. DataRootDirectory: &testDataDirName,
  114. ClientPlatform: &clientPlatform,
  115. NetworkID: &networkID,
  116. EstablishTunnelTimeoutSeconds: &timeout,
  117. },
  118. paramsDelta: nil,
  119. noticeReceiver: nil,
  120. },
  121. wantTunnel: true,
  122. expectedErr: nil,
  123. },
  124. }
  125. for _, tt := range tests {
  126. t.Run(tt.name, func(t *testing.T) {
  127. ctx := context.Background()
  128. var cancelFunc context.CancelFunc
  129. if tt.args.ctxTimeout > 0 {
  130. ctx, cancelFunc = context.WithTimeout(ctx, tt.args.ctxTimeout)
  131. }
  132. tunnel, err := StartTunnel(
  133. ctx,
  134. tt.args.configJSON,
  135. tt.args.embeddedServerEntryList,
  136. tt.args.params,
  137. tt.args.paramsDelta,
  138. tt.args.noticeReceiver)
  139. gotTunnel := (tunnel != nil)
  140. if cancelFunc != nil {
  141. cancelFunc()
  142. }
  143. if tunnel != nil {
  144. tunnel.Stop()
  145. }
  146. if gotTunnel != tt.wantTunnel {
  147. t.Errorf("StartTunnel() gotTunnel = %v, wantTunnel %v", err, tt.wantTunnel)
  148. }
  149. if err != tt.expectedErr {
  150. t.Fatalf("StartTunnel() error = %v, expectedErr %v", err, tt.expectedErr)
  151. return
  152. }
  153. })
  154. }
  155. }