server_test.go 4.7 KB

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