controller_test.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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. }
  90. }))
  91. // Run controller, which establishes tunnels
  92. shutdownBroadcast := make(chan struct{})
  93. controllerWaitGroup := new(sync.WaitGroup)
  94. controllerWaitGroup.Add(1)
  95. go func() {
  96. defer controllerWaitGroup.Done()
  97. controller.Run(shutdownBroadcast)
  98. }()
  99. // Test: tunnel must be established within 60 seconds
  100. establishTimeout := time.NewTimer(60 * time.Second)
  101. select {
  102. case <-tunnelEstablished:
  103. // Test: fetch website through tunnel
  104. fetchWebsite(t, httpProxyPort)
  105. case <-establishTimeout.C:
  106. t.Errorf("tunnel establish timeout exceeded")
  107. // ...continue with cleanup
  108. }
  109. close(shutdownBroadcast)
  110. // Test: shutdown must complete within 10 seconds
  111. shutdownTimeout := time.NewTimer(10 * time.Second)
  112. shutdownOk := make(chan struct{}, 1)
  113. go func() {
  114. controllerWaitGroup.Wait()
  115. shutdownOk <- *new(struct{})
  116. }()
  117. select {
  118. case <-shutdownOk:
  119. case <-shutdownTimeout.C:
  120. t.Errorf("controller shutdown timeout exceeded")
  121. }
  122. }
  123. func fetchWebsite(t *testing.T, httpProxyPort int) {
  124. testUrl := "https://raw.githubusercontent.com/Psiphon-Labs/psiphon-tunnel-core/master/LICENSE"
  125. roundTripTimeout := 10 * time.Second
  126. expectedResponsePrefix := " GNU GENERAL PUBLIC LICENSE"
  127. expectedResponseSize := 35148
  128. checkResponse := func(responseBody string) bool {
  129. return strings.HasPrefix(responseBody, expectedResponsePrefix) && len(responseBody) == expectedResponseSize
  130. }
  131. // Test: use HTTP proxy
  132. proxyUrl, err := url.Parse(fmt.Sprintf("http://127.0.0.1:%d", httpProxyPort))
  133. if err != nil {
  134. t.Errorf("error initializing proxied HTTP request: %s", err)
  135. t.FailNow()
  136. }
  137. httpClient := &http.Client{
  138. Transport: &http.Transport{
  139. Proxy: http.ProxyURL(proxyUrl),
  140. },
  141. Timeout: roundTripTimeout,
  142. }
  143. response, err := httpClient.Get(testUrl)
  144. if err != nil {
  145. t.Errorf("error sending proxied HTTP request: %s", err)
  146. t.FailNow()
  147. }
  148. body, err := ioutil.ReadAll(response.Body)
  149. if err != nil {
  150. t.Errorf("error reading proxied HTTP response: %s", err)
  151. t.FailNow()
  152. }
  153. response.Body.Close()
  154. if !checkResponse(string(body)) {
  155. t.Errorf("unexpected proxied HTTP response")
  156. t.FailNow()
  157. }
  158. // Test: use direct URL proxy
  159. httpClient = &http.Client{
  160. Transport: http.DefaultTransport,
  161. Timeout: roundTripTimeout,
  162. }
  163. response, err = httpClient.Get(
  164. fmt.Sprintf("http://127.0.0.1:%d/direct/%s",
  165. httpProxyPort, url.QueryEscape(testUrl)))
  166. if err != nil {
  167. t.Errorf("error sending direct URL request: %s", err)
  168. t.FailNow()
  169. }
  170. body, err = ioutil.ReadAll(response.Body)
  171. if err != nil {
  172. t.Errorf("error reading direct URL response: %s", err)
  173. t.FailNow()
  174. }
  175. response.Body.Close()
  176. if !checkResponse(string(body)) {
  177. t.Errorf("unexpected direct URL response")
  178. t.FailNow()
  179. }
  180. // Test: use tunneled URL proxy
  181. response, err = httpClient.Get(
  182. fmt.Sprintf("http://127.0.0.1:%d/tunneled/%s",
  183. httpProxyPort, url.QueryEscape(testUrl)))
  184. if err != nil {
  185. t.Errorf("error sending tunneled URL request: %s", err)
  186. t.FailNow()
  187. }
  188. body, err = ioutil.ReadAll(response.Body)
  189. if err != nil {
  190. t.Errorf("error reading tunneled URL response: %s", err)
  191. t.FailNow()
  192. }
  193. response.Body.Close()
  194. if !checkResponse(string(body)) {
  195. t.Errorf("unexpected tunneled URL response")
  196. t.FailNow()
  197. }
  198. }