controller_test.go 6.3 KB

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