clientlib_test.go 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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. "os"
  24. "testing"
  25. "time"
  26. )
  27. func TestStartTunnel(t *testing.T) {
  28. // TODO: More comprehensive tests. This is only a smoke test.
  29. clientPlatform := "clientlib_test.go"
  30. networkID := "UNKNOWN"
  31. timeout := 60
  32. quickTimeout := 1
  33. trueVal := true
  34. configJSON, err := os.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 := os.MkdirTemp("", "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. name: "Success: disable SOCKS proxy",
  126. args: args{
  127. ctxTimeout: 0,
  128. configJSON: configJSON,
  129. embeddedServerEntryList: "",
  130. params: Parameters{
  131. DataRootDirectory: &testDataDirName,
  132. ClientPlatform: &clientPlatform,
  133. NetworkID: &networkID,
  134. EstablishTunnelTimeoutSeconds: &timeout,
  135. DisableLocalSocksProxy: &trueVal,
  136. },
  137. paramsDelta: nil,
  138. noticeReceiver: nil,
  139. },
  140. wantTunnel: true,
  141. expectedErr: nil,
  142. },
  143. {
  144. name: "Success: disable HTTP proxy",
  145. args: args{
  146. ctxTimeout: 0,
  147. configJSON: configJSON,
  148. embeddedServerEntryList: "",
  149. params: Parameters{
  150. DataRootDirectory: &testDataDirName,
  151. ClientPlatform: &clientPlatform,
  152. NetworkID: &networkID,
  153. EstablishTunnelTimeoutSeconds: &timeout,
  154. DisableLocalHTTPProxy: &trueVal,
  155. },
  156. paramsDelta: nil,
  157. noticeReceiver: nil,
  158. },
  159. wantTunnel: true,
  160. expectedErr: nil,
  161. },
  162. {
  163. name: "Success: disable SOCKS and HTTP proxies",
  164. args: args{
  165. ctxTimeout: 0,
  166. configJSON: configJSON,
  167. embeddedServerEntryList: "",
  168. params: Parameters{
  169. DataRootDirectory: &testDataDirName,
  170. ClientPlatform: &clientPlatform,
  171. NetworkID: &networkID,
  172. EstablishTunnelTimeoutSeconds: &timeout,
  173. DisableLocalSocksProxy: &trueVal,
  174. DisableLocalHTTPProxy: &trueVal,
  175. },
  176. paramsDelta: nil,
  177. noticeReceiver: nil,
  178. },
  179. wantTunnel: true,
  180. expectedErr: nil,
  181. },
  182. }
  183. for _, tt := range tests {
  184. t.Run(tt.name, func(t *testing.T) {
  185. ctx := context.Background()
  186. var cancelFunc context.CancelFunc
  187. if tt.args.ctxTimeout > 0 {
  188. ctx, cancelFunc = context.WithTimeout(ctx, tt.args.ctxTimeout)
  189. }
  190. tunnel, err := StartTunnel(
  191. ctx,
  192. tt.args.configJSON,
  193. tt.args.embeddedServerEntryList,
  194. tt.args.params,
  195. tt.args.paramsDelta,
  196. tt.args.noticeReceiver)
  197. gotTunnel := (tunnel != nil)
  198. if cancelFunc != nil {
  199. cancelFunc()
  200. }
  201. if tunnel != nil {
  202. tunnel.Stop()
  203. }
  204. if gotTunnel != tt.wantTunnel {
  205. t.Errorf("StartTunnel() gotTunnel = %v, wantTunnel %v", err, tt.wantTunnel)
  206. }
  207. if err != tt.expectedErr {
  208. t.Fatalf("StartTunnel() error = %v, expectedErr %v", err, tt.expectedErr)
  209. return
  210. }
  211. if tunnel == nil {
  212. return
  213. }
  214. if tt.args.params.DisableLocalSocksProxy != nil && *tt.args.params.DisableLocalSocksProxy {
  215. if tunnel.SOCKSProxyPort != 0 {
  216. t.Fatalf("should not have started SOCKS proxy")
  217. }
  218. } else {
  219. if tunnel.SOCKSProxyPort == 0 {
  220. t.Fatalf("failed to start SOCKS proxy")
  221. }
  222. }
  223. if tt.args.params.DisableLocalHTTPProxy != nil && *tt.args.params.DisableLocalHTTPProxy {
  224. if tunnel.HTTPProxyPort != 0 {
  225. t.Fatalf("should not have started HTTP proxy")
  226. }
  227. } else {
  228. if tunnel.HTTPProxyPort == 0 {
  229. t.Fatalf("failed to start HTTP proxy")
  230. }
  231. }
  232. })
  233. }
  234. }
  235. func TestPsiphonTunnel_Dial(t *testing.T) {
  236. trueVal := true
  237. configJSON, err := os.ReadFile("../../psiphon/controller_test.config")
  238. if err != nil {
  239. // Skip, don't fail, if config file is not present
  240. t.Skipf("error loading configuration file: %s", err)
  241. }
  242. testDataDirName, err := os.MkdirTemp("", "psiphon-clientlib-test")
  243. if err != nil {
  244. t.Fatalf("ioutil.TempDir failed: %v", err)
  245. }
  246. defer os.RemoveAll(testDataDirName)
  247. type args struct {
  248. remoteAddr string
  249. }
  250. tests := []struct {
  251. name string
  252. args args
  253. wantErr bool
  254. }{
  255. {
  256. name: "Success: example.com",
  257. args: args{remoteAddr: "example.com:443"},
  258. wantErr: false,
  259. },
  260. {
  261. name: "Failure: invalid address",
  262. args: args{remoteAddr: "example.com:99999"},
  263. wantErr: true,
  264. },
  265. }
  266. for _, tt := range tests {
  267. t.Run(tt.name, func(t *testing.T) {
  268. tunnel, err := StartTunnel(
  269. context.Background(),
  270. configJSON,
  271. "",
  272. Parameters{
  273. DataRootDirectory: &testDataDirName,
  274. // Don't need local proxies for dial tests
  275. // (and this is likely the configuration that will be used by consumers of the library who utilitize Dial).
  276. DisableLocalSocksProxy: &trueVal,
  277. DisableLocalHTTPProxy: &trueVal,
  278. },
  279. nil,
  280. nil)
  281. if err != nil {
  282. t.Fatalf("StartTunnel() error = %v", err)
  283. }
  284. defer tunnel.Stop()
  285. conn, err := tunnel.Dial(tt.args.remoteAddr)
  286. if (err != nil) != tt.wantErr {
  287. t.Fatalf("PsiphonTunnel.Dial() error = %v, wantErr %v", err, tt.wantErr)
  288. return
  289. }
  290. if tt.wantErr != (conn == nil) {
  291. t.Fatalf("PsiphonTunnel.Dial() conn = %v, wantConn %v", conn, !tt.wantErr)
  292. }
  293. })
  294. }
  295. }