clientlib_test.go 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  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 TestMultpleStartTunnel(t *testing.T) {
  236. configJSON, err := os.ReadFile("../../psiphon/controller_test.config")
  237. if err != nil {
  238. // What to do if config file is not present?
  239. t.Skipf("error loading configuration file: %s", err)
  240. }
  241. testDataDirName, err := os.MkdirTemp("", "psiphon-clientlib-test")
  242. if err != nil {
  243. t.Fatalf("ioutil.TempDir failed: %v", err)
  244. }
  245. defer os.RemoveAll(testDataDirName)
  246. ctx := context.Background()
  247. tunnel1, err := StartTunnel(
  248. ctx,
  249. configJSON,
  250. "",
  251. Parameters{DataRootDirectory: &testDataDirName},
  252. nil,
  253. nil)
  254. if err != nil {
  255. t.Fatalf("first StartTunnel() error = %v", err)
  256. }
  257. // We have not stopped the tunnel, so a second StartTunnel() should fail
  258. _, err = StartTunnel(
  259. ctx,
  260. configJSON,
  261. "",
  262. Parameters{DataRootDirectory: &testDataDirName},
  263. nil,
  264. nil)
  265. if err != errMultipleStart {
  266. t.Fatalf("second StartTunnel() should have failed with errMultipleStart; got %v", err)
  267. }
  268. // Stop the tunnel and try again
  269. tunnel1.Stop()
  270. tunnel3, err := StartTunnel(
  271. ctx,
  272. configJSON,
  273. "",
  274. Parameters{DataRootDirectory: &testDataDirName},
  275. nil,
  276. nil)
  277. if err != nil {
  278. t.Fatalf("third StartTunnel() error = %v", err)
  279. }
  280. // Stop the tunnel so it doesn't interfere with other tests
  281. tunnel3.Stop()
  282. }
  283. func TestPsiphonTunnel_Dial(t *testing.T) {
  284. trueVal := true
  285. configJSON, err := os.ReadFile("../../psiphon/controller_test.config")
  286. if err != nil {
  287. // Skip, don't fail, if config file is not present
  288. t.Skipf("error loading configuration file: %s", err)
  289. }
  290. testDataDirName, err := os.MkdirTemp("", "psiphon-clientlib-test")
  291. if err != nil {
  292. t.Fatalf("ioutil.TempDir failed: %v", err)
  293. }
  294. defer os.RemoveAll(testDataDirName)
  295. type args struct {
  296. remoteAddr string
  297. }
  298. tests := []struct {
  299. name string
  300. args args
  301. wantErr bool
  302. }{
  303. {
  304. name: "Success: example.com",
  305. args: args{remoteAddr: "example.com:443"},
  306. wantErr: false,
  307. },
  308. {
  309. name: "Failure: invalid address",
  310. args: args{remoteAddr: "example.com:99999"},
  311. wantErr: true,
  312. },
  313. }
  314. for _, tt := range tests {
  315. t.Run(tt.name, func(t *testing.T) {
  316. tunnel, err := StartTunnel(
  317. context.Background(),
  318. configJSON,
  319. "",
  320. Parameters{
  321. DataRootDirectory: &testDataDirName,
  322. // Don't need local proxies for dial tests
  323. // (and this is likely the configuration that will be used by consumers of the library who utilitize Dial).
  324. DisableLocalSocksProxy: &trueVal,
  325. DisableLocalHTTPProxy: &trueVal,
  326. },
  327. nil,
  328. nil)
  329. if err != nil {
  330. t.Fatalf("StartTunnel() error = %v", err)
  331. }
  332. defer tunnel.Stop()
  333. conn, err := tunnel.Dial(tt.args.remoteAddr)
  334. if (err != nil) != tt.wantErr {
  335. t.Fatalf("PsiphonTunnel.Dial() error = %v, wantErr %v", err, tt.wantErr)
  336. return
  337. }
  338. if tt.wantErr != (conn == nil) {
  339. t.Fatalf("PsiphonTunnel.Dial() conn = %v, wantConn %v", conn, !tt.wantErr)
  340. }
  341. })
  342. }
  343. }