server_test.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. /*
  2. * Copyright (c) 2016, 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 server
  20. import (
  21. "encoding/json"
  22. "flag"
  23. "fmt"
  24. "io/ioutil"
  25. "net/http"
  26. "net/url"
  27. "os"
  28. "sync"
  29. "testing"
  30. "time"
  31. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon"
  32. )
  33. func TestMain(m *testing.M) {
  34. flag.Parse()
  35. os.Remove(psiphon.DATA_STORE_FILENAME)
  36. psiphon.SetEmitDiagnosticNotices(true)
  37. os.Exit(m.Run())
  38. }
  39. func TestSSH(t *testing.T) {
  40. runServer(t, "SSH")
  41. }
  42. func TestOSSH(t *testing.T) {
  43. runServer(t, "OSSH")
  44. }
  45. func TestFrontedMeek(t *testing.T) {
  46. runServer(t, "FRONTED-MEEK-OSSH")
  47. }
  48. func TestUnfrontedMeek(t *testing.T) {
  49. runServer(t, "UNFRONTED-MEEK-OSSH")
  50. }
  51. func TestFrontedMeekHTTP(t *testing.T) {
  52. runServer(t, "FRONTED-MEEK-HTTP-OSSH")
  53. }
  54. func runServer(t *testing.T, tunnelProtocol string) {
  55. // create a server
  56. serverConfigFileContents, serverEntryFileContents, err := GenerateConfig(
  57. "127.0.0.1")
  58. if err != nil {
  59. t.Fatalf("error generating server config: %s", err)
  60. }
  61. // customize server config
  62. var serverConfig interface{}
  63. json.Unmarshal(serverConfigFileContents, &serverConfig)
  64. serverConfig.(map[string]interface{})["GeoIPDatabaseFilename"] = ""
  65. serverConfigFileContents, _ = json.Marshal(serverConfig)
  66. // run server
  67. serverWaitGroup := new(sync.WaitGroup)
  68. serverWaitGroup.Add(1)
  69. go func() {
  70. defer serverWaitGroup.Done()
  71. err := RunServices([][]byte{serverConfigFileContents})
  72. if err != nil {
  73. // TODO: wrong goroutine for t.FatalNow()
  74. t.Fatalf("error running server: %s", err)
  75. }
  76. }()
  77. defer func() {
  78. // Test: orderly server shutdown
  79. p, _ := os.FindProcess(os.Getpid())
  80. p.Signal(os.Interrupt)
  81. shutdownTimeout := time.NewTimer(5 * time.Second)
  82. shutdownOk := make(chan struct{}, 1)
  83. go func() {
  84. serverWaitGroup.Wait()
  85. shutdownOk <- *new(struct{})
  86. }()
  87. select {
  88. case <-shutdownOk:
  89. case <-shutdownTimeout.C:
  90. t.Fatalf("server shutdown timeout exceeded")
  91. }
  92. }()
  93. // connect to server with client
  94. // TODO: currently, TargetServerEntry only works with one tunnel
  95. numTunnels := 1
  96. localHTTPProxyPort := 8080
  97. establishTunnelPausePeriodSeconds := 1
  98. // Note: calling LoadConfig ensures all *int config fields are initialized
  99. configJson := `
  100. {
  101. "ClientVersion": "0",
  102. "PropagationChannelId": "0",
  103. "SponsorId": "0"
  104. }`
  105. clientConfig, _ := psiphon.LoadConfig([]byte(configJson))
  106. clientConfig.ConnectionWorkerPoolSize = numTunnels
  107. clientConfig.TunnelPoolSize = numTunnels
  108. clientConfig.DisableRemoteServerListFetcher = true
  109. clientConfig.EstablishTunnelPausePeriodSeconds = &establishTunnelPausePeriodSeconds
  110. clientConfig.TargetServerEntry = string(serverEntryFileContents)
  111. clientConfig.TunnelProtocol = tunnelProtocol
  112. clientConfig.LocalHttpProxyPort = localHTTPProxyPort
  113. err = psiphon.InitDataStore(clientConfig)
  114. if err != nil {
  115. t.Fatalf("error initializing client datastore: %s", err)
  116. }
  117. controller, err := psiphon.NewController(clientConfig)
  118. if err != nil {
  119. t.Fatalf("error creating client controller: %s", err)
  120. }
  121. tunnelsEstablished := make(chan struct{}, 1)
  122. psiphon.SetNoticeOutput(psiphon.NewNoticeReceiver(
  123. func(notice []byte) {
  124. fmt.Printf("%s\n", string(notice))
  125. noticeType, payload, err := psiphon.GetNotice(notice)
  126. if err != nil {
  127. return
  128. }
  129. switch noticeType {
  130. case "Tunnels":
  131. count := int(payload["count"].(float64))
  132. if count >= numTunnels {
  133. select {
  134. case tunnelsEstablished <- *new(struct{}):
  135. default:
  136. }
  137. }
  138. }
  139. }))
  140. controllerShutdownBroadcast := make(chan struct{})
  141. controllerWaitGroup := new(sync.WaitGroup)
  142. controllerWaitGroup.Add(1)
  143. go func() {
  144. defer controllerWaitGroup.Done()
  145. controller.Run(controllerShutdownBroadcast)
  146. }()
  147. defer close(controllerShutdownBroadcast)
  148. // Test: tunnels must be established within 30 seconds
  149. establishTimeout := time.NewTimer(30 * time.Second)
  150. select {
  151. case <-tunnelsEstablished:
  152. case <-establishTimeout.C:
  153. t.Fatalf("tunnel establish timeout exceeded")
  154. }
  155. // Test: tunneled web site fetch
  156. testUrl := "https://psiphon.ca"
  157. roundTripTimeout := 30 * time.Second
  158. proxyUrl, err := url.Parse(fmt.Sprintf("http://127.0.0.1:%d", localHTTPProxyPort))
  159. if err != nil {
  160. t.Fatalf("error initializing proxied HTTP request: %s", err)
  161. }
  162. httpClient := &http.Client{
  163. Transport: &http.Transport{
  164. Proxy: http.ProxyURL(proxyUrl),
  165. },
  166. Timeout: roundTripTimeout,
  167. }
  168. response, err := httpClient.Get(testUrl)
  169. if err != nil {
  170. t.Fatalf("error sending proxied HTTP request: %s", err)
  171. }
  172. _, err = ioutil.ReadAll(response.Body)
  173. if err != nil {
  174. t.Fatalf("error reading proxied HTTP response: %s", err)
  175. }
  176. response.Body.Close()
  177. }