controller_test.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. /*
  2. * Copyright (c) 2015, 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 psiphon
  20. import (
  21. "fmt"
  22. "io/ioutil"
  23. "net/http"
  24. "net/url"
  25. "strings"
  26. "sync"
  27. "testing"
  28. "time"
  29. )
  30. func TestControllerRunSSH(t *testing.T) {
  31. controllerRun(t, TUNNEL_PROTOCOL_SSH)
  32. }
  33. func TestControllerRunObfuscatedSSH(t *testing.T) {
  34. controllerRun(t, TUNNEL_PROTOCOL_OBFUSCATED_SSH)
  35. }
  36. func TestControllerRunUnfrontedMeek(t *testing.T) {
  37. controllerRun(t, TUNNEL_PROTOCOL_UNFRONTED_MEEK)
  38. }
  39. func TestControllerRunFrontedMeek(t *testing.T) {
  40. controllerRun(t, TUNNEL_PROTOCOL_FRONTED_MEEK)
  41. }
  42. func controllerRun(t *testing.T, protocol string) {
  43. configFileContents, err := ioutil.ReadFile("controller_test.config")
  44. if err != nil {
  45. // Skip, don't fail, if config file is not present
  46. t.Skipf("error loading configuration file: %s", err)
  47. }
  48. config, err := LoadConfig(configFileContents)
  49. if err != nil {
  50. t.Errorf("error processing configuration file: %s", err)
  51. t.FailNow()
  52. }
  53. config.TunnelProtocol = protocol
  54. err = InitDataStore(config)
  55. if err != nil {
  56. t.Errorf("error initializing datastore: %s", err)
  57. t.FailNow()
  58. }
  59. controller, err := NewController(config)
  60. if err != nil {
  61. t.Errorf("error creating controller: %s", err)
  62. t.FailNow()
  63. }
  64. // Monitor notices for "Tunnels" with count > 1, the
  65. // indication of tunnel establishment success.
  66. // Also record the selected HTTP proxy port to use
  67. // when fetching websites through the tunnel.
  68. httpProxyPort := 0
  69. tunnelEstablished := make(chan struct{}, 1)
  70. SetNoticeOutput(NewNoticeReceiver(
  71. func(notice []byte) {
  72. // TODO: log notices without logging server IPs:
  73. // fmt.Fprintf(os.Stderr, "%s\n", string(notice))
  74. noticeType, payload, err := GetNotice(notice)
  75. if err != nil {
  76. return
  77. }
  78. switch noticeType {
  79. case "Tunnels":
  80. count := int(payload["count"].(float64))
  81. if count > 0 {
  82. select {
  83. case tunnelEstablished <- *new(struct{}):
  84. default:
  85. }
  86. }
  87. case "ListeningHttpProxyPort":
  88. httpProxyPort = int(payload["port"].(float64))
  89. case "ConnectingServer":
  90. serverProtocol := payload["protocol"]
  91. if serverProtocol != protocol {
  92. t.Errorf("wrong protocol selected: %s", serverProtocol)
  93. t.FailNow()
  94. }
  95. }
  96. }))
  97. // Run controller, which establishes tunnels
  98. shutdownBroadcast := make(chan struct{})
  99. controllerWaitGroup := new(sync.WaitGroup)
  100. controllerWaitGroup.Add(1)
  101. go func() {
  102. defer controllerWaitGroup.Done()
  103. controller.Run(shutdownBroadcast)
  104. }()
  105. // Test: tunnel must be established within 60 seconds
  106. establishTimeout := time.NewTimer(60 * time.Second)
  107. select {
  108. case <-tunnelEstablished:
  109. // Allow for known race condition described in NewHttpProxy():
  110. time.Sleep(1 * time.Second)
  111. // Test: fetch website through tunnel
  112. fetchWebsite(t, httpProxyPort)
  113. case <-establishTimeout.C:
  114. t.Errorf("tunnel establish timeout exceeded")
  115. // ...continue with cleanup
  116. }
  117. close(shutdownBroadcast)
  118. // Test: shutdown must complete within 10 seconds
  119. shutdownTimeout := time.NewTimer(10 * time.Second)
  120. shutdownOk := make(chan struct{}, 1)
  121. go func() {
  122. controllerWaitGroup.Wait()
  123. shutdownOk <- *new(struct{})
  124. }()
  125. select {
  126. case <-shutdownOk:
  127. case <-shutdownTimeout.C:
  128. t.Errorf("controller shutdown timeout exceeded")
  129. }
  130. }
  131. func fetchWebsite(t *testing.T, httpProxyPort int) {
  132. testUrl := "https://raw.githubusercontent.com/Psiphon-Labs/psiphon-tunnel-core/master/LICENSE"
  133. roundTripTimeout := 10 * time.Second
  134. expectedResponsePrefix := " GNU GENERAL PUBLIC LICENSE"
  135. expectedResponseSize := 35148
  136. checkResponse := func(responseBody string) bool {
  137. return strings.HasPrefix(responseBody, expectedResponsePrefix) && len(responseBody) == expectedResponseSize
  138. }
  139. // Test: use HTTP proxy
  140. proxyUrl, err := url.Parse(fmt.Sprintf("http://127.0.0.1:%d", httpProxyPort))
  141. if err != nil {
  142. t.Errorf("error initializing proxied HTTP request: %s", err)
  143. t.FailNow()
  144. }
  145. httpClient := &http.Client{
  146. Transport: &http.Transport{
  147. Proxy: http.ProxyURL(proxyUrl),
  148. },
  149. Timeout: roundTripTimeout,
  150. }
  151. response, err := httpClient.Get(testUrl)
  152. if err != nil {
  153. t.Errorf("error sending proxied HTTP request: %s", err)
  154. t.FailNow()
  155. }
  156. body, err := ioutil.ReadAll(response.Body)
  157. if err != nil {
  158. t.Errorf("error reading proxied HTTP response: %s", err)
  159. t.FailNow()
  160. }
  161. response.Body.Close()
  162. if !checkResponse(string(body)) {
  163. t.Errorf("unexpected proxied HTTP response")
  164. t.FailNow()
  165. }
  166. // Test: use direct URL proxy
  167. httpClient = &http.Client{
  168. Transport: http.DefaultTransport,
  169. Timeout: roundTripTimeout,
  170. }
  171. response, err = httpClient.Get(
  172. fmt.Sprintf("http://127.0.0.1:%d/direct/%s",
  173. httpProxyPort, url.QueryEscape(testUrl)))
  174. if err != nil {
  175. t.Errorf("error sending direct URL request: %s", err)
  176. t.FailNow()
  177. }
  178. body, err = ioutil.ReadAll(response.Body)
  179. if err != nil {
  180. t.Errorf("error reading direct URL response: %s", err)
  181. t.FailNow()
  182. }
  183. response.Body.Close()
  184. if !checkResponse(string(body)) {
  185. t.Errorf("unexpected direct URL response")
  186. t.FailNow()
  187. }
  188. // Test: use tunneled URL proxy
  189. response, err = httpClient.Get(
  190. fmt.Sprintf("http://127.0.0.1:%d/tunneled/%s",
  191. httpProxyPort, url.QueryEscape(testUrl)))
  192. if err != nil {
  193. t.Errorf("error sending tunneled URL request: %s", err)
  194. t.FailNow()
  195. }
  196. body, err = ioutil.ReadAll(response.Body)
  197. if err != nil {
  198. t.Errorf("error reading tunneled URL response: %s", err)
  199. t.FailNow()
  200. }
  201. response.Body.Close()
  202. if !checkResponse(string(body)) {
  203. t.Errorf("unexpected tunneled URL response")
  204. t.FailNow()
  205. }
  206. }