transferstats_test.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  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 transferstats
  20. import (
  21. "errors"
  22. "fmt"
  23. "net"
  24. "net/http"
  25. "regexp"
  26. "testing"
  27. mapset "github.com/deckarep/golang-set"
  28. "github.com/stretchr/testify/suite"
  29. )
  30. const (
  31. _SERVER_ID = "myserverid"
  32. )
  33. var nextServerID = 0
  34. type StatsTestSuite struct {
  35. suite.Suite
  36. serverID string
  37. httpClient *http.Client
  38. }
  39. func TestStatsTestSuite(t *testing.T) {
  40. suite.Run(t, new(StatsTestSuite))
  41. }
  42. func (suite *StatsTestSuite) SetupTest() {
  43. suite.serverID = fmt.Sprintf("%s-%d", _SERVER_ID, nextServerID)
  44. nextServerID++
  45. suite.httpClient = &http.Client{
  46. Transport: &http.Transport{
  47. Dial: makeStatsDialer(suite.serverID, &Regexps{}),
  48. },
  49. }
  50. }
  51. func (suite *StatsTestSuite) TearDownTest() {
  52. suite.httpClient = nil
  53. }
  54. func makeStatsDialer(serverID string, regexps *Regexps) func(network, addr string) (conn net.Conn, err error) {
  55. return func(network, addr string) (conn net.Conn, err error) {
  56. var subConn net.Conn
  57. switch network {
  58. case "tcp", "tcp4", "tcp6":
  59. tcpAddr, err := net.ResolveTCPAddr(network, addr)
  60. if err != nil {
  61. return nil, err
  62. }
  63. subConn, err = net.DialTCP(network, nil, tcpAddr)
  64. if err != nil {
  65. return nil, err
  66. }
  67. default:
  68. err = errors.New("using an unsupported testing network type")
  69. return
  70. }
  71. conn = NewConn(subConn, serverID, regexps)
  72. err = nil
  73. return
  74. }
  75. }
  76. func (suite *StatsTestSuite) Test_StatsConn() {
  77. resp, err := suite.httpClient.Get("http://example.com/index.html")
  78. suite.Nil(err, "basic HTTP requests should succeed")
  79. resp.Body.Close()
  80. resp, err = suite.httpClient.Get("https://example.org/index.html")
  81. suite.Nil(err, "basic HTTPS requests should succeed")
  82. resp.Body.Close()
  83. }
  84. func (suite *StatsTestSuite) Test_TakeOutStatsForServer() {
  85. zeroPayload := &AccumulatedStats{hostnameToStats: make(map[string]*hostStats)}
  86. payload := TakeOutStatsForServer(suite.serverID)
  87. suite.Equal(payload, zeroPayload, "should get zero stats before any traffic")
  88. resp, err := suite.httpClient.Get("http://example.com/index.html")
  89. suite.Nil(err, "need successful http to proceed with tests")
  90. resp.Body.Close()
  91. payload = TakeOutStatsForServer(suite.serverID)
  92. suite.NotNil(payload, "should receive valid payload for valid server ID")
  93. // After we retrieve the stats for a server, they should be cleared out of the tracked stats
  94. payload = TakeOutStatsForServer(suite.serverID)
  95. suite.Equal(payload, zeroPayload, "after retrieving stats for a server, there should be zero stats (until more data goes through)")
  96. }
  97. func (suite *StatsTestSuite) Test_PutBackStatsForServer() {
  98. // Set a regexp for the httpClient to ensure it at least records "(OTHER)" domain bytes;
  99. // The regex is set to "nomatch.com" so that it _will_ exercise the "(OTHER)" case.
  100. regexp, _ := regexp.Compile(`^[a-z0-9\.]*\.(nomatch\.com)$`)
  101. replace := "$1"
  102. regexps := &Regexps{regexpReplace{regexp: regexp, replace: replace}}
  103. suite.httpClient = &http.Client{
  104. Transport: &http.Transport{
  105. Dial: makeStatsDialer(suite.serverID, regexps),
  106. },
  107. }
  108. resp, err := suite.httpClient.Get("http://example.com/index.html")
  109. suite.Nil(err, "need successful http to proceed with tests")
  110. resp.Body.Close()
  111. payloadToPutBack := TakeOutStatsForServer(suite.serverID)
  112. suite.NotNil(payloadToPutBack, "should receive valid payload for valid server ID")
  113. zeroPayload := &AccumulatedStats{hostnameToStats: make(map[string]*hostStats)}
  114. payload := TakeOutStatsForServer(suite.serverID)
  115. suite.Equal(payload, zeroPayload, "should be zero stats after getting them")
  116. PutBackStatsForServer(suite.serverID, payloadToPutBack)
  117. payload = TakeOutStatsForServer(suite.serverID)
  118. suite.NotEqual(payload, zeroPayload, "stats should be re-added after putting back")
  119. suite.Equal(payload, payloadToPutBack, "stats should be the same as after the first retrieval")
  120. }
  121. func (suite *StatsTestSuite) Test_NoRegexes() {
  122. // Set no regexps for the httpClient
  123. suite.httpClient = &http.Client{
  124. Transport: &http.Transport{
  125. Dial: makeStatsDialer(suite.serverID, &Regexps{}),
  126. },
  127. }
  128. // Ensure there are no stats before making the no-regex request
  129. _ = TakeOutStatsForServer(suite.serverID)
  130. resp, err := suite.httpClient.Get("http://example.com/index.html")
  131. suite.Nil(err, "need successful http to proceed with tests")
  132. resp.Body.Close()
  133. zeroPayload := &AccumulatedStats{hostnameToStats: make(map[string]*hostStats)}
  134. payload := TakeOutStatsForServer(suite.serverID)
  135. suite.Equal(payload, zeroPayload, "should be zero stats after getting them")
  136. }
  137. func (suite *StatsTestSuite) Test_MakeRegexps() {
  138. pageViewRegexes := []map[string]string{make(map[string]string)}
  139. pageViewRegexes[0]["regex"] = `(^http://[a-z0-9\.]*\.example\.[a-z\.]*)/.*`
  140. pageViewRegexes[0]["replace"] = "$1"
  141. httpsRequestRegexes := []map[string]string{make(map[string]string), make(map[string]string)}
  142. httpsRequestRegexes[0]["regex"] = `^[a-z0-9\.]*\.(example\.com)$`
  143. httpsRequestRegexes[0]["replace"] = "$1"
  144. httpsRequestRegexes[1]["regex"] = `^.*example\.org$`
  145. httpsRequestRegexes[1]["replace"] = "replacement"
  146. regexps, notices := MakeRegexps(pageViewRegexes, httpsRequestRegexes)
  147. suite.NotNil(regexps, "should return a valid object")
  148. suite.Len(*regexps, 2, "should only have processed httpsRequestRegexes")
  149. suite.Len(notices, 0, "should return no notices")
  150. //
  151. // Test some bad regexps
  152. //
  153. httpsRequestRegexes[0]["regex"] = ""
  154. httpsRequestRegexes[0]["replace"] = "$1"
  155. regexps, notices = MakeRegexps(pageViewRegexes, httpsRequestRegexes)
  156. suite.NotNil(regexps, "should return a valid object")
  157. suite.Len(*regexps, 1, "should have discarded one regexp")
  158. suite.Len(notices, 1, "should have returned one notice")
  159. httpsRequestRegexes[0]["regex"] = `^[a-z0-9\.]*\.(example\.com)$`
  160. httpsRequestRegexes[0]["replace"] = ""
  161. regexps, notices = MakeRegexps(pageViewRegexes, httpsRequestRegexes)
  162. suite.NotNil(regexps, "should return a valid object")
  163. suite.Len(*regexps, 1, "should have discarded one regexp")
  164. suite.Len(notices, 1, "should have returned one notice")
  165. httpsRequestRegexes[0]["regex"] = `^[a-z0-9\.]*\.(example\.com$` // missing closing paren
  166. httpsRequestRegexes[0]["replace"] = "$1"
  167. regexps, notices = MakeRegexps(pageViewRegexes, httpsRequestRegexes)
  168. suite.NotNil(regexps, "should return a valid object")
  169. suite.Len(*regexps, 1, "should have discarded one regexp")
  170. suite.Len(notices, 1, "should have returned one notice")
  171. }
  172. func (suite *StatsTestSuite) Test_Regex() {
  173. // We'll make a new client with actual regexps.
  174. pageViewRegexes := make([]map[string]string, 0)
  175. httpsRequestRegexes := []map[string]string{make(map[string]string), make(map[string]string)}
  176. httpsRequestRegexes[0]["regex"] = `^[a-z0-9\.]*\.(example\.com)$`
  177. httpsRequestRegexes[0]["replace"] = "$1"
  178. httpsRequestRegexes[1]["regex"] = `^.*example\.org$`
  179. httpsRequestRegexes[1]["replace"] = "replacement"
  180. regexps, _ := MakeRegexps(pageViewRegexes, httpsRequestRegexes)
  181. suite.httpClient = &http.Client{
  182. Transport: &http.Transport{
  183. Dial: makeStatsDialer(suite.serverID, regexps),
  184. },
  185. }
  186. // Using both HTTP and HTTPS will help us to exercise both methods of hostname parsing
  187. for _, scheme := range []string{"http", "https"} {
  188. // No subdomain, so won't match regex
  189. url := fmt.Sprintf("%s://example.com/index.html", scheme)
  190. resp, err := suite.httpClient.Get(url)
  191. suite.Nil(err)
  192. resp.Body.Close()
  193. // Will match the first regex
  194. url = fmt.Sprintf("%s://www.example.com/index.html", scheme)
  195. resp, err = suite.httpClient.Get(url)
  196. suite.Nil(err)
  197. resp.Body.Close()
  198. // Will match the second regex
  199. url = fmt.Sprintf("%s://example.org/index.html", scheme)
  200. resp, err = suite.httpClient.Get(url)
  201. suite.Nil(err)
  202. resp.Body.Close()
  203. payload := TakeOutStatsForServer(suite.serverID)
  204. suite.NotNil(payload, "should get stats because we made HTTP reqs; %s", scheme)
  205. expectedHostnames := mapset.NewSet()
  206. expectedHostnames.Add("(OTHER)")
  207. expectedHostnames.Add("example.com")
  208. expectedHostnames.Add("replacement")
  209. hostnames := make([]interface{}, 0)
  210. for hostname := range payload.hostnameToStats {
  211. hostnames = append(hostnames, hostname)
  212. }
  213. actualHostnames := mapset.NewSetFromSlice(hostnames)
  214. suite.Equal(expectedHostnames, actualHostnames, "post-regex hostnames should be processed as expecteds; %s", scheme)
  215. }
  216. }
  217. func (suite *StatsTestSuite) Test_getTLSHostname() {
  218. // TODO: Create a more robust/antagonistic set of negative tests.
  219. // We can write raw TCP to simulate any arbitrary degree of "almost looks
  220. // like a TLS handshake".
  221. // These tests are basically just checking for crashes.
  222. //
  223. // An easier way to construct valid client-hello messages (but not malicious ones)
  224. // would be to use the clientHelloMsg struct and marshal function from:
  225. // https://github.com/golang/go/blob/master/src/crypto/tls/handshake_messages.go
  226. // TODO: Talk to a local TCP server instead of spamming example.com
  227. dialer := makeStatsDialer(suite.serverID, nil)
  228. // Data too short
  229. conn, err := dialer("tcp", "example.com:80")
  230. suite.Nil(err)
  231. b := []byte(`my bytes`)
  232. n, err := conn.Write(b)
  233. suite.Nil(err)
  234. suite.Equal(len(b), n)
  235. err = conn.Close()
  236. suite.Nil(err)
  237. // Data long enough, but wrong first byte
  238. conn, err = dialer("tcp", "example.com:80")
  239. suite.Nil(err)
  240. b = []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
  241. n, err = conn.Write(b)
  242. suite.Nil(err)
  243. suite.Equal(len(b), n)
  244. err = conn.Close()
  245. suite.Nil(err)
  246. // Data long enough, correct first byte
  247. conn, err = dialer("tcp", "example.com:80")
  248. suite.Nil(err)
  249. b = []byte{22, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
  250. n, err = conn.Write(b)
  251. suite.Nil(err)
  252. suite.Equal(len(b), n)
  253. err = conn.Close()
  254. suite.Nil(err)
  255. // Correct until after SSL version
  256. conn, err = dialer("tcp", "example.com:80")
  257. suite.Nil(err)
  258. b = []byte{22, 3, 1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
  259. n, err = conn.Write(b)
  260. suite.Nil(err)
  261. suite.Equal(len(b), n)
  262. err = conn.Close()
  263. suite.Nil(err)
  264. plaintextLen := byte(70)
  265. // Correct until after plaintext length
  266. conn, err = dialer("tcp", "example.com:80")
  267. suite.Nil(err)
  268. b = []byte{22, 3, 1, 0, plaintextLen, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
  269. n, err = conn.Write(b)
  270. suite.Nil(err)
  271. suite.Equal(len(b), n)
  272. err = conn.Close()
  273. suite.Nil(err)
  274. // Correct until after handshake type
  275. conn, err = dialer("tcp", "example.com:80")
  276. suite.Nil(err)
  277. b = []byte{22, 3, 1, 0, plaintextLen, 1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
  278. n, err = conn.Write(b)
  279. suite.Nil(err)
  280. suite.Equal(len(b), n)
  281. err = conn.Close()
  282. suite.Nil(err)
  283. // Correct until after handshake length
  284. conn, err = dialer("tcp", "example.com:80")
  285. suite.Nil(err)
  286. b = []byte{22, 3, 1, 0, plaintextLen, 1, 0, 0, plaintextLen - 4, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
  287. n, err = conn.Write(b)
  288. suite.Nil(err)
  289. suite.Equal(len(b), n)
  290. err = conn.Close()
  291. suite.Nil(err)
  292. // Correct until after protocol version
  293. conn, err = dialer("tcp", "example.com:80")
  294. suite.Nil(err)
  295. b = []byte{22, 3, 1, 0, plaintextLen, 1, 0, 0, plaintextLen - 4, 3, 3, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
  296. n, err = conn.Write(b)
  297. suite.Nil(err)
  298. suite.Equal(len(b), n)
  299. err = conn.Close()
  300. suite.Nil(err)
  301. }