transferstats_test.go 13 KB

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