snowflake-broker_test.go 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799
  1. package main
  2. import (
  3. // "bytes"
  4. "container/heap"
  5. "io/ioutil"
  6. "log"
  7. // "net"
  8. // "net/http"
  9. // "net/http/httptest"
  10. "os"
  11. "sync"
  12. "testing"
  13. // "time"
  14. . "github.com/smartystreets/goconvey/convey"
  15. )
  16. func NullLogger() *log.Logger {
  17. logger := log.New(os.Stdout, "", 0)
  18. logger.SetOutput(ioutil.Discard)
  19. return logger
  20. }
  21. var promOnce sync.Once
  22. func TestBroker(t *testing.T) {
  23. Convey("Context", t, func() {
  24. ctx := NewBrokerContext(NullLogger())
  25. // i := &IPC{ctx}
  26. Convey("Adds Snowflake", func() {
  27. So(ctx.snowflakes.Len(), ShouldEqual, 0)
  28. So(len(ctx.idToSnowflake), ShouldEqual, 0)
  29. ctx.AddSnowflake("foo", "", NATUnrestricted)
  30. So(ctx.snowflakes.Len(), ShouldEqual, 1)
  31. So(len(ctx.idToSnowflake), ShouldEqual, 1)
  32. })
  33. Convey("Broker goroutine matches clients with proxies", func() {
  34. p := new(ProxyPoll)
  35. p.id = "test"
  36. p.natType = "unrestricted"
  37. p.offerChannel = make(chan *ClientOffer)
  38. go func(ctx *BrokerContext) {
  39. ctx.proxyPolls <- p
  40. close(ctx.proxyPolls)
  41. }(ctx)
  42. ctx.Broker()
  43. So(ctx.snowflakes.Len(), ShouldEqual, 1)
  44. snowflake := heap.Pop(ctx.snowflakes).(*Snowflake)
  45. snowflake.offerChannel <- &ClientOffer{sdp: []byte("test offer")}
  46. offer := <-p.offerChannel
  47. So(ctx.idToSnowflake["test"], ShouldNotBeNil)
  48. So(offer.sdp, ShouldResemble, []byte("test offer"))
  49. So(ctx.snowflakes.Len(), ShouldEqual, 0)
  50. })
  51. Convey("Request an offer from the Snowflake Heap", func() {
  52. done := make(chan *ClientOffer)
  53. go func() {
  54. offer := ctx.RequestOffer("test", "", NATUnrestricted)
  55. done <- offer
  56. }()
  57. request := <-ctx.proxyPolls
  58. request.offerChannel <- &ClientOffer{sdp: []byte("test offer")}
  59. offer := <-done
  60. So(offer.sdp, ShouldResemble, []byte("test offer"))
  61. })
  62. // Convey("Responds to client offers...", func() {
  63. // w := httptest.NewRecorder()
  64. // data := bytes.NewReader(
  65. // []byte("1.0\n{\"offer\": \"fake\", \"nat\": \"unknown\"}"))
  66. // r, err := http.NewRequest("POST", "snowflake.broker/client", data)
  67. // So(err, ShouldBeNil)
  68. //
  69. // Convey("with error when no snowflakes are available.", func() {
  70. // clientOffers(i, w, r)
  71. // So(w.Code, ShouldEqual, http.StatusOK)
  72. // So(w.Body.String(), ShouldEqual, `{"error":"no snowflake proxies currently available"}`)
  73. // })
  74. //
  75. // Convey("with a proxy answer if available.", func() {
  76. // done := make(chan bool)
  77. // // Prepare a fake proxy to respond with.
  78. // snowflake := ctx.AddSnowflake("fake", "", NATUnrestricted)
  79. // go func() {
  80. // clientOffers(i, w, r)
  81. // done <- true
  82. // }()
  83. // offer := <-snowflake.offerChannel
  84. // So(offer.sdp, ShouldResemble, []byte("fake"))
  85. // snowflake.answerChannel <- "fake answer"
  86. // <-done
  87. // So(w.Body.String(), ShouldEqual, `{"answer":"fake answer"}`)
  88. // So(w.Code, ShouldEqual, http.StatusOK)
  89. // })
  90. //
  91. // Convey("Times out when no proxy responds.", func() {
  92. // if testing.Short() {
  93. // return
  94. // }
  95. // done := make(chan bool)
  96. // snowflake := ctx.AddSnowflake("fake", "", NATUnrestricted)
  97. // go func() {
  98. // clientOffers(i, w, r)
  99. // // Takes a few seconds here...
  100. // done <- true
  101. // }()
  102. // offer := <-snowflake.offerChannel
  103. // So(offer.sdp, ShouldResemble, []byte("fake"))
  104. // <-done
  105. // So(w.Code, ShouldEqual, http.StatusOK)
  106. // So(w.Body.String(), ShouldEqual, `{"error":"timed out waiting for answer!"}`)
  107. // })
  108. // })
  109. //
  110. // Convey("Responds to legacy client offers...", func() {
  111. // w := httptest.NewRecorder()
  112. // data := bytes.NewReader([]byte("{test}"))
  113. // r, err := http.NewRequest("POST", "snowflake.broker/client", data)
  114. // So(err, ShouldBeNil)
  115. // r.Header.Set("Snowflake-NAT-TYPE", "restricted")
  116. //
  117. // Convey("with 503 when no snowflakes are available.", func() {
  118. // clientOffers(i, w, r)
  119. // So(w.Code, ShouldEqual, http.StatusServiceUnavailable)
  120. // So(w.Body.String(), ShouldEqual, "")
  121. // })
  122. //
  123. // Convey("with a proxy answer if available.", func() {
  124. // done := make(chan bool)
  125. // // Prepare a fake proxy to respond with.
  126. // snowflake := ctx.AddSnowflake("fake", "", NATUnrestricted)
  127. // go func() {
  128. // clientOffers(i, w, r)
  129. // done <- true
  130. // }()
  131. // offer := <-snowflake.offerChannel
  132. // So(offer.sdp, ShouldResemble, []byte("{test}"))
  133. // snowflake.answerChannel <- "fake answer"
  134. // <-done
  135. // So(w.Body.String(), ShouldEqual, "fake answer")
  136. // So(w.Code, ShouldEqual, http.StatusOK)
  137. // })
  138. //
  139. // Convey("Times out when no proxy responds.", func() {
  140. // if testing.Short() {
  141. // return
  142. // }
  143. // done := make(chan bool)
  144. // snowflake := ctx.AddSnowflake("fake", "", NATUnrestricted)
  145. // go func() {
  146. // clientOffers(i, w, r)
  147. // // Takes a few seconds here...
  148. // done <- true
  149. // }()
  150. // offer := <-snowflake.offerChannel
  151. // So(offer.sdp, ShouldResemble, []byte("{test}"))
  152. // <-done
  153. // So(w.Code, ShouldEqual, http.StatusGatewayTimeout)
  154. // })
  155. //
  156. // })
  157. //
  158. // Convey("Responds to proxy polls...", func() {
  159. // done := make(chan bool)
  160. // w := httptest.NewRecorder()
  161. // data := bytes.NewReader([]byte(`{"Sid":"ymbcCMto7KHNGYlp","Version":"1.0"}`))
  162. // r, err := http.NewRequest("POST", "snowflake.broker/proxy", data)
  163. // So(err, ShouldBeNil)
  164. //
  165. // Convey("with a client offer if available.", func() {
  166. // go func(ctx *BrokerContext) {
  167. // proxyPolls(i, w, r)
  168. // done <- true
  169. // }(ctx)
  170. // // Pass a fake client offer to this proxy
  171. // p := <-ctx.proxyPolls
  172. // So(p.id, ShouldEqual, "ymbcCMto7KHNGYlp")
  173. // p.offerChannel <- &ClientOffer{sdp: []byte("fake offer")}
  174. // <-done
  175. // So(w.Code, ShouldEqual, http.StatusOK)
  176. // So(w.Body.String(), ShouldEqual, `{"Status":"client match","Offer":"fake offer","NAT":""}`)
  177. // })
  178. //
  179. // Convey("return empty 200 OK when no client offer is available.", func() {
  180. // go func(ctx *BrokerContext) {
  181. // proxyPolls(i, w, r)
  182. // done <- true
  183. // }(ctx)
  184. // p := <-ctx.proxyPolls
  185. // So(p.id, ShouldEqual, "ymbcCMto7KHNGYlp")
  186. // // nil means timeout
  187. // p.offerChannel <- nil
  188. // <-done
  189. // So(w.Body.String(), ShouldEqual, `{"Status":"no match","Offer":"","NAT":""}`)
  190. // So(w.Code, ShouldEqual, http.StatusOK)
  191. // })
  192. // })
  193. // Convey("Responds to proxy answers...", func() {
  194. // s := ctx.AddSnowflake("test", "", NATUnrestricted)
  195. // w := httptest.NewRecorder()
  196. // data := bytes.NewReader([]byte(`{"Version":"1.0","Sid":"test","Answer":"test"}`))
  197. //
  198. // Convey("by passing to the client if valid.", func() {
  199. // r, err := http.NewRequest("POST", "snowflake.broker/answer", data)
  200. // So(err, ShouldBeNil)
  201. // go func(ctx *BrokerContext) {
  202. // proxyAnswers(i, w, r)
  203. // }(ctx)
  204. // answer := <-s.answerChannel
  205. // So(w.Code, ShouldEqual, http.StatusOK)
  206. // So(answer, ShouldResemble, "test")
  207. // })
  208. //
  209. // Convey("with client gone status if the proxy is not recognized", func() {
  210. // data = bytes.NewReader([]byte(`{"Version":"1.0","Sid":"invalid","Answer":"test"}`))
  211. // r, err := http.NewRequest("POST", "snowflake.broker/answer", data)
  212. // So(err, ShouldBeNil)
  213. // proxyAnswers(i, w, r)
  214. // So(w.Code, ShouldEqual, http.StatusOK)
  215. // b, err := ioutil.ReadAll(w.Body)
  216. // So(err, ShouldBeNil)
  217. // So(b, ShouldResemble, []byte(`{"Status":"client gone"}`))
  218. //
  219. // })
  220. //
  221. // Convey("with error if the proxy gives invalid answer", func() {
  222. // data := bytes.NewReader(nil)
  223. // r, err := http.NewRequest("POST", "snowflake.broker/answer", data)
  224. // So(err, ShouldBeNil)
  225. // proxyAnswers(i, w, r)
  226. // So(w.Code, ShouldEqual, http.StatusBadRequest)
  227. // })
  228. //
  229. // Convey("with error if the proxy writes too much data", func() {
  230. // data := bytes.NewReader(make([]byte, 100001))
  231. // r, err := http.NewRequest("POST", "snowflake.broker/answer", data)
  232. // So(err, ShouldBeNil)
  233. // proxyAnswers(i, w, r)
  234. // So(w.Code, ShouldEqual, http.StatusBadRequest)
  235. // })
  236. //
  237. // })
  238. })
  239. // Convey("End-To-End", t, func() {
  240. // ctx := NewBrokerContext(NullLogger())
  241. // i := &IPC{ctx}
  242. //
  243. // Convey("Check for client/proxy data race", func() {
  244. // proxy_done := make(chan bool)
  245. // client_done := make(chan bool)
  246. //
  247. // go ctx.Broker()
  248. //
  249. // // Make proxy poll
  250. // wp := httptest.NewRecorder()
  251. // datap := bytes.NewReader([]byte(`{"Sid":"ymbcCMto7KHNGYlp","Version":"1.0"}`))
  252. // rp, err := http.NewRequest("POST", "snowflake.broker/proxy", datap)
  253. // So(err, ShouldBeNil)
  254. //
  255. // go func(ctx *BrokerContext) {
  256. // proxyPolls(i, wp, rp)
  257. // proxy_done <- true
  258. // }(ctx)
  259. //
  260. // // Client offer
  261. // wc := httptest.NewRecorder()
  262. // datac := bytes.NewReader([]byte("test"))
  263. // rc, err := http.NewRequest("POST", "snowflake.broker/client", datac)
  264. // So(err, ShouldBeNil)
  265. //
  266. // go func() {
  267. // clientOffers(i, wc, rc)
  268. // client_done <- true
  269. // }()
  270. //
  271. // <-proxy_done
  272. // So(wp.Code, ShouldEqual, http.StatusOK)
  273. //
  274. // // Proxy answers
  275. // wp = httptest.NewRecorder()
  276. // datap = bytes.NewReader([]byte(`{"Version":"1.0","Sid":"ymbcCMto7KHNGYlp","Answer":"test"}`))
  277. // rp, err = http.NewRequest("POST", "snowflake.broker/answer", datap)
  278. // So(err, ShouldBeNil)
  279. // go func(ctx *BrokerContext) {
  280. // proxyAnswers(i, wp, rp)
  281. // proxy_done <- true
  282. // }(ctx)
  283. //
  284. // <-proxy_done
  285. // <-client_done
  286. //
  287. // })
  288. //
  289. // Convey("Ensure correct snowflake brokering", func() {
  290. // done := make(chan bool)
  291. // polled := make(chan bool)
  292. //
  293. // // Proxy polls with its ID first...
  294. // dataP := bytes.NewReader([]byte(`{"Sid":"ymbcCMto7KHNGYlp","Version":"1.0"}`))
  295. // wP := httptest.NewRecorder()
  296. // rP, err := http.NewRequest("POST", "snowflake.broker/proxy", dataP)
  297. // So(err, ShouldBeNil)
  298. // go func() {
  299. // proxyPolls(i, wP, rP)
  300. // polled <- true
  301. // }()
  302. //
  303. // // Manually do the Broker goroutine action here for full control.
  304. // p := <-ctx.proxyPolls
  305. // So(p.id, ShouldEqual, "ymbcCMto7KHNGYlp")
  306. // s := ctx.AddSnowflake(p.id, "", NATUnrestricted)
  307. // go func() {
  308. // offer := <-s.offerChannel
  309. // p.offerChannel <- offer
  310. // }()
  311. // So(ctx.idToSnowflake["ymbcCMto7KHNGYlp"], ShouldNotBeNil)
  312. //
  313. // // Client request blocks until proxy answer arrives.
  314. // dataC := bytes.NewReader(
  315. // []byte("1.0\n{\"offer\": \"fake\", \"nat\": \"unknown\"}"))
  316. // wC := httptest.NewRecorder()
  317. // rC, err := http.NewRequest("POST", "snowflake.broker/client", dataC)
  318. // So(err, ShouldBeNil)
  319. // go func() {
  320. // clientOffers(i, wC, rC)
  321. // done <- true
  322. // }()
  323. //
  324. // <-polled
  325. // So(wP.Code, ShouldEqual, http.StatusOK)
  326. // So(wP.Body.String(), ShouldResemble, `{"Status":"client match","Offer":"fake","NAT":"unknown"}`)
  327. // So(ctx.idToSnowflake["ymbcCMto7KHNGYlp"], ShouldNotBeNil)
  328. // // Follow up with the answer request afterwards
  329. // wA := httptest.NewRecorder()
  330. // dataA := bytes.NewReader([]byte(`{"Version":"1.0","Sid":"ymbcCMto7KHNGYlp","Answer":"test"}`))
  331. // rA, err := http.NewRequest("POST", "snowflake.broker/answer", dataA)
  332. // So(err, ShouldBeNil)
  333. // proxyAnswers(i, wA, rA)
  334. // So(wA.Code, ShouldEqual, http.StatusOK)
  335. //
  336. // <-done
  337. // So(wC.Code, ShouldEqual, http.StatusOK)
  338. // So(wC.Body.String(), ShouldEqual, `{"answer":"test"}`)
  339. // })
  340. // })
  341. }
  342. func TestSnowflakeHeap(t *testing.T) {
  343. Convey("SnowflakeHeap", t, func() {
  344. h := new(SnowflakeHeap)
  345. heap.Init(h)
  346. So(h.Len(), ShouldEqual, 0)
  347. s1 := new(Snowflake)
  348. s2 := new(Snowflake)
  349. s3 := new(Snowflake)
  350. s4 := new(Snowflake)
  351. s1.clients = 4
  352. s2.clients = 5
  353. s3.clients = 3
  354. s4.clients = 1
  355. heap.Push(h, s1)
  356. So(h.Len(), ShouldEqual, 1)
  357. heap.Push(h, s2)
  358. So(h.Len(), ShouldEqual, 2)
  359. heap.Push(h, s3)
  360. So(h.Len(), ShouldEqual, 3)
  361. heap.Push(h, s4)
  362. So(h.Len(), ShouldEqual, 4)
  363. heap.Remove(h, 0)
  364. So(h.Len(), ShouldEqual, 3)
  365. r := heap.Pop(h).(*Snowflake)
  366. So(h.Len(), ShouldEqual, 2)
  367. So(r.clients, ShouldEqual, 3)
  368. So(r.index, ShouldEqual, -1)
  369. r = heap.Pop(h).(*Snowflake)
  370. So(h.Len(), ShouldEqual, 1)
  371. So(r.clients, ShouldEqual, 4)
  372. So(r.index, ShouldEqual, -1)
  373. r = heap.Pop(h).(*Snowflake)
  374. So(h.Len(), ShouldEqual, 0)
  375. So(r.clients, ShouldEqual, 5)
  376. So(r.index, ShouldEqual, -1)
  377. })
  378. // Convey("End-To-End", t, func() {
  379. // ctx := NewBrokerContext(NullLogger())
  380. // i := &IPC{ctx}
  381. //
  382. // Convey("Check for client/proxy data race", func() {
  383. // proxy_done := make(chan bool)
  384. // client_done := make(chan bool)
  385. //
  386. // go ctx.Broker()
  387. //
  388. // // Make proxy poll
  389. // wp := httptest.NewRecorder()
  390. // datap := bytes.NewReader([]byte(`{"Sid":"ymbcCMto7KHNGYlp","Version":"1.0"}`))
  391. // rp, err := http.NewRequest("POST", "snowflake.broker/proxy", datap)
  392. // So(err, ShouldBeNil)
  393. //
  394. // go func(ctx *BrokerContext) {
  395. // proxyPolls(i, wp, rp)
  396. // proxy_done <- true
  397. // }(ctx)
  398. //
  399. // // Client offer
  400. // wc := httptest.NewRecorder()
  401. // datac := bytes.NewReader([]byte("test"))
  402. // rc, err := http.NewRequest("POST", "snowflake.broker/client", datac)
  403. // So(err, ShouldBeNil)
  404. //
  405. // go func() {
  406. // clientOffers(i, wc, rc)
  407. // client_done <- true
  408. // }()
  409. //
  410. // <-proxy_done
  411. // So(wp.Code, ShouldEqual, http.StatusOK)
  412. //
  413. // // Proxy answers
  414. // wp = httptest.NewRecorder()
  415. // datap = bytes.NewReader([]byte(`{"Version":"1.0","Sid":"ymbcCMto7KHNGYlp","Answer":"test"}`))
  416. // rp, err = http.NewRequest("POST", "snowflake.broker/answer", datap)
  417. // So(err, ShouldBeNil)
  418. // go func(ctx *BrokerContext) {
  419. // proxyAnswers(i, wp, rp)
  420. // proxy_done <- true
  421. // }(ctx)
  422. //
  423. // <-proxy_done
  424. // <-client_done
  425. //
  426. // })
  427. //
  428. // Convey("Ensure correct snowflake brokering", func() {
  429. // done := make(chan bool)
  430. // polled := make(chan bool)
  431. //
  432. // // Proxy polls with its ID first...
  433. // dataP := bytes.NewReader([]byte(`{"Sid":"ymbcCMto7KHNGYlp","Version":"1.0"}`))
  434. // wP := httptest.NewRecorder()
  435. // rP, err := http.NewRequest("POST", "snowflake.broker/proxy", dataP)
  436. // So(err, ShouldBeNil)
  437. // go func() {
  438. // proxyPolls(i, wP, rP)
  439. // polled <- true
  440. // }()
  441. //
  442. // // Manually do the Broker goroutine action here for full control.
  443. // p := <-ctx.proxyPolls
  444. // So(p.id, ShouldEqual, "ymbcCMto7KHNGYlp")
  445. // s := ctx.AddSnowflake(p.id, "", NATUnrestricted)
  446. // go func() {
  447. // offer := <-s.offerChannel
  448. // p.offerChannel <- offer
  449. // }()
  450. // So(ctx.idToSnowflake["ymbcCMto7KHNGYlp"], ShouldNotBeNil)
  451. //
  452. // // Client request blocks until proxy answer arrives.
  453. // dataC := bytes.NewReader([]byte("fake offer"))
  454. // wC := httptest.NewRecorder()
  455. // rC, err := http.NewRequest("POST", "snowflake.broker/client", dataC)
  456. // So(err, ShouldBeNil)
  457. // go func() {
  458. // clientOffers(i, wC, rC)
  459. // done <- true
  460. // }()
  461. //
  462. // <-polled
  463. // So(wP.Code, ShouldEqual, http.StatusOK)
  464. // So(wP.Body.String(), ShouldResemble, `{"Status":"client match","Offer":"fake offer","NAT":"unknown"}`)
  465. // So(ctx.idToSnowflake["ymbcCMto7KHNGYlp"], ShouldNotBeNil)
  466. // // Follow up with the answer request afterwards
  467. // wA := httptest.NewRecorder()
  468. // dataA := bytes.NewReader([]byte(`{"Version":"1.0","Sid":"ymbcCMto7KHNGYlp","Answer":"test"}`))
  469. // rA, err := http.NewRequest("POST", "snowflake.broker/answer", dataA)
  470. // So(err, ShouldBeNil)
  471. // proxyAnswers(i, wA, rA)
  472. // So(wA.Code, ShouldEqual, http.StatusOK)
  473. //
  474. // <-done
  475. // So(wC.Code, ShouldEqual, http.StatusOK)
  476. // So(wC.Body.String(), ShouldEqual, "test")
  477. // })
  478. // })
  479. }
  480. // func TestMetrics(t *testing.T) {
  481. // Convey("Test metrics...", t, func() {
  482. // done := make(chan bool)
  483. // buf := new(bytes.Buffer)
  484. // ctx := NewBrokerContext(log.New(buf, "", 0))
  485. // i := &IPC{ctx}
  486. //
  487. // err := ctx.metrics.LoadGeoipDatabases("test_geoip", "test_geoip6")
  488. // So(err, ShouldEqual, nil)
  489. //
  490. // //Test addition of proxy polls
  491. // Convey("for proxy polls", func() {
  492. // w := httptest.NewRecorder()
  493. // data := bytes.NewReader([]byte("{\"Sid\":\"ymbcCMto7KHNGYlp\",\"Version\":\"1.0\"}"))
  494. // r, err := http.NewRequest("POST", "snowflake.broker/proxy", data)
  495. // r.RemoteAddr = "129.97.208.23:8888" //CA geoip
  496. // So(err, ShouldBeNil)
  497. // go func(i *IPC) {
  498. // proxyPolls(i, w, r)
  499. // done <- true
  500. // }(i)
  501. // p := <-ctx.proxyPolls //manually unblock poll
  502. // p.offerChannel <- nil
  503. // <-done
  504. //
  505. // w = httptest.NewRecorder()
  506. // data = bytes.NewReader([]byte(`{"Sid":"ymbcCMto7KHNGYlp","Version":"1.0","Type":"standalone"}`))
  507. // r, err = http.NewRequest("POST", "snowflake.broker/proxy", data)
  508. // r.RemoteAddr = "129.97.208.23:8888" //CA geoip
  509. // So(err, ShouldBeNil)
  510. // go func(i *IPC) {
  511. // proxyPolls(i, w, r)
  512. // done <- true
  513. // }(i)
  514. // p = <-ctx.proxyPolls //manually unblock poll
  515. // p.offerChannel <- nil
  516. // <-done
  517. //
  518. // w = httptest.NewRecorder()
  519. // data = bytes.NewReader([]byte(`{"Sid":"ymbcCMto7KHNGYlp","Version":"1.0","Type":"badge"}`))
  520. // r, err = http.NewRequest("POST", "snowflake.broker/proxy", data)
  521. // r.RemoteAddr = "129.97.208.23:8888" //CA geoip
  522. // So(err, ShouldBeNil)
  523. // go func(i *IPC) {
  524. // proxyPolls(i, w, r)
  525. // done <- true
  526. // }(i)
  527. // p = <-ctx.proxyPolls //manually unblock poll
  528. // p.offerChannel <- nil
  529. // <-done
  530. //
  531. // w = httptest.NewRecorder()
  532. // data = bytes.NewReader([]byte(`{"Sid":"ymbcCMto7KHNGYlp","Version":"1.0","Type":"webext"}`))
  533. // r, err = http.NewRequest("POST", "snowflake.broker/proxy", data)
  534. // r.RemoteAddr = "129.97.208.23:8888" //CA geoip
  535. // So(err, ShouldBeNil)
  536. // go func(i *IPC) {
  537. // proxyPolls(i, w, r)
  538. // done <- true
  539. // }(i)
  540. // p = <-ctx.proxyPolls //manually unblock poll
  541. // p.offerChannel <- nil
  542. // <-done
  543. // ctx.metrics.printMetrics()
  544. // So(buf.String(), ShouldResemble, "snowflake-stats-end "+time.Now().UTC().Format("2006-01-02 15:04:05")+" (86400 s)\nsnowflake-ips CA=4\nsnowflake-ips-total 4\nsnowflake-ips-standalone 1\nsnowflake-ips-badge 1\nsnowflake-ips-webext 1\nsnowflake-idle-count 8\nclient-denied-count 0\nclient-restricted-denied-count 0\nclient-unrestricted-denied-count 0\nclient-snowflake-match-count 0\nsnowflake-ips-nat-restricted 0\nsnowflake-ips-nat-unrestricted 0\nsnowflake-ips-nat-unknown 1\n")
  545. //
  546. // })
  547. //
  548. // //Test addition of client failures
  549. // Convey("for no proxies available", func() {
  550. // w := httptest.NewRecorder()
  551. // data := bytes.NewReader(
  552. // []byte("1.0\n{\"offer\": \"fake\", \"nat\": \"unknown\"}"))
  553. // r, err := http.NewRequest("POST", "snowflake.broker/client", data)
  554. // So(err, ShouldBeNil)
  555. //
  556. // clientOffers(i, w, r)
  557. //
  558. // ctx.metrics.printMetrics()
  559. // So(buf.String(), ShouldContainSubstring, "client-denied-count 8\nclient-restricted-denied-count 8\nclient-unrestricted-denied-count 0\nclient-snowflake-match-count 0")
  560. //
  561. // // Test reset
  562. // buf.Reset()
  563. // ctx.metrics.zeroMetrics()
  564. // ctx.metrics.printMetrics()
  565. // So(buf.String(), ShouldContainSubstring, "snowflake-ips \nsnowflake-ips-total 0\nsnowflake-ips-standalone 0\nsnowflake-ips-badge 0\nsnowflake-ips-webext 0\nsnowflake-idle-count 0\nclient-denied-count 0\nclient-restricted-denied-count 0\nclient-unrestricted-denied-count 0\nclient-snowflake-match-count 0\nsnowflake-ips-nat-restricted 0\nsnowflake-ips-nat-unrestricted 0\nsnowflake-ips-nat-unknown 0\n")
  566. // })
  567. // //Test addition of client matches
  568. // Convey("for client-proxy match", func() {
  569. // w := httptest.NewRecorder()
  570. // data := bytes.NewReader(
  571. // []byte("1.0\n{\"offer\": \"fake\", \"nat\": \"unknown\"}"))
  572. // r, err := http.NewRequest("POST", "snowflake.broker/client", data)
  573. // So(err, ShouldBeNil)
  574. //
  575. // // Prepare a fake proxy to respond with.
  576. // snowflake := ctx.AddSnowflake("fake", "", NATUnrestricted)
  577. // go func() {
  578. // clientOffers(i, w, r)
  579. // done <- true
  580. // }()
  581. // offer := <-snowflake.offerChannel
  582. // So(offer.sdp, ShouldResemble, []byte("fake"))
  583. // snowflake.answerChannel <- "fake answer"
  584. // <-done
  585. //
  586. // ctx.metrics.printMetrics()
  587. // So(buf.String(), ShouldContainSubstring, "client-denied-count 0\nclient-restricted-denied-count 0\nclient-unrestricted-denied-count 0\nclient-snowflake-match-count 8")
  588. // })
  589. // //Test rounding boundary
  590. // Convey("binning boundary", func() {
  591. // w := httptest.NewRecorder()
  592. // data := bytes.NewReader(
  593. // []byte("1.0\n{\"offer\": \"fake\", \"nat\": \"restricted\"}"))
  594. // r, err := http.NewRequest("POST", "snowflake.broker/client", data)
  595. // So(err, ShouldBeNil)
  596. //
  597. // clientOffers(i, w, r)
  598. // w = httptest.NewRecorder()
  599. // data = bytes.NewReader(
  600. // []byte("1.0\n{\"offer\": \"fake\", \"nat\": \"restricted\"}"))
  601. // r, err = http.NewRequest("POST", "snowflake.broker/client", data)
  602. // So(err, ShouldBeNil)
  603. // clientOffers(i, w, r)
  604. // w = httptest.NewRecorder()
  605. // data = bytes.NewReader(
  606. // []byte("1.0\n{\"offer\": \"fake\", \"nat\": \"restricted\"}"))
  607. // r, err = http.NewRequest("POST", "snowflake.broker/client", data)
  608. // So(err, ShouldBeNil)
  609. // clientOffers(i, w, r)
  610. // w = httptest.NewRecorder()
  611. // data = bytes.NewReader(
  612. // []byte("1.0\n{\"offer\": \"fake\", \"nat\": \"restricted\"}"))
  613. // r, err = http.NewRequest("POST", "snowflake.broker/client", data)
  614. // So(err, ShouldBeNil)
  615. // clientOffers(i, w, r)
  616. // w = httptest.NewRecorder()
  617. // data = bytes.NewReader(
  618. // []byte("1.0\n{\"offer\": \"fake\", \"nat\": \"restricted\"}"))
  619. // r, err = http.NewRequest("POST", "snowflake.broker/client", data)
  620. // So(err, ShouldBeNil)
  621. // clientOffers(i, w, r)
  622. // w = httptest.NewRecorder()
  623. // data = bytes.NewReader(
  624. // []byte("1.0\n{\"offer\": \"fake\", \"nat\": \"restricted\"}"))
  625. // r, err = http.NewRequest("POST", "snowflake.broker/client", data)
  626. // So(err, ShouldBeNil)
  627. // clientOffers(i, w, r)
  628. // w = httptest.NewRecorder()
  629. // data = bytes.NewReader(
  630. // []byte("1.0\n{\"offer\": \"fake\", \"nat\": \"restricted\"}"))
  631. // r, err = http.NewRequest("POST", "snowflake.broker/client", data)
  632. // So(err, ShouldBeNil)
  633. // clientOffers(i, w, r)
  634. // w = httptest.NewRecorder()
  635. // data = bytes.NewReader(
  636. // []byte("1.0\n{\"offer\": \"fake\", \"nat\": \"restricted\"}"))
  637. // r, err = http.NewRequest("POST", "snowflake.broker/client", data)
  638. // So(err, ShouldBeNil)
  639. // clientOffers(i, w, r)
  640. //
  641. // ctx.metrics.printMetrics()
  642. // So(buf.String(), ShouldContainSubstring, "client-denied-count 8\nclient-restricted-denied-count 8\nclient-unrestricted-denied-count 0\n")
  643. //
  644. // w = httptest.NewRecorder()
  645. // data = bytes.NewReader(
  646. // []byte("1.0\n{\"offer\": \"fake\", \"nat\": \"restricted\"}"))
  647. // r, err = http.NewRequest("POST", "snowflake.broker/client", data)
  648. // So(err, ShouldBeNil)
  649. // clientOffers(i, w, r)
  650. // buf.Reset()
  651. // ctx.metrics.printMetrics()
  652. // So(buf.String(), ShouldContainSubstring, "client-denied-count 16\nclient-restricted-denied-count 16\nclient-unrestricted-denied-count 0\n")
  653. // })
  654. //
  655. // //Test unique ip
  656. // Convey("proxy counts by unique ip", func() {
  657. // w := httptest.NewRecorder()
  658. // data := bytes.NewReader([]byte(`{"Sid":"ymbcCMto7KHNGYlp","Version":"1.0"}`))
  659. // r, err := http.NewRequest("POST", "snowflake.broker/proxy", data)
  660. // r.RemoteAddr = "129.97.208.23:8888" //CA geoip
  661. // So(err, ShouldBeNil)
  662. // go func(ctx *BrokerContext) {
  663. // proxyPolls(i, w, r)
  664. // done <- true
  665. // }(ctx)
  666. // p := <-ctx.proxyPolls //manually unblock poll
  667. // p.offerChannel <- nil
  668. // <-done
  669. //
  670. // data = bytes.NewReader([]byte(`{"Sid":"ymbcCMto7KHNGYlp","Version":"1.0"}`))
  671. // r, err = http.NewRequest("POST", "snowflake.broker/proxy", data)
  672. // if err != nil {
  673. // log.Printf("unable to get NewRequest with error: %v", err)
  674. // }
  675. // r.RemoteAddr = "129.97.208.23:8888" //CA geoip
  676. // go func(i *IPC) {
  677. // proxyPolls(i, w, r)
  678. // done <- true
  679. // }(i)
  680. // p = <-ctx.proxyPolls //manually unblock poll
  681. // p.offerChannel <- nil
  682. // <-done
  683. //
  684. // ctx.metrics.printMetrics()
  685. // So(buf.String(), ShouldContainSubstring, "snowflake-ips CA=1\nsnowflake-ips-total 1")
  686. // })
  687. // //Test NAT types
  688. // Convey("proxy counts by NAT type", func() {
  689. // w := httptest.NewRecorder()
  690. // data := bytes.NewReader([]byte(`{"Sid":"ymbcCMto7KHNGYlp","Version":"1.2","Type":"unknown","NAT":"restricted"}`))
  691. // r, err := http.NewRequest("POST", "snowflake.broker/proxy", data)
  692. // r.RemoteAddr = "129.97.208.23:8888" //CA geoip
  693. // So(err, ShouldBeNil)
  694. // go func(i *IPC) {
  695. // proxyPolls(i, w, r)
  696. // done <- true
  697. // }(i)
  698. // p := <-ctx.proxyPolls //manually unblock poll
  699. // p.offerChannel <- nil
  700. // <-done
  701. //
  702. // ctx.metrics.printMetrics()
  703. // So(buf.String(), ShouldContainSubstring, "snowflake-ips-nat-restricted 1\nsnowflake-ips-nat-unrestricted 0\nsnowflake-ips-nat-unknown 0")
  704. //
  705. // data = bytes.NewReader([]byte(`{"Sid":"ymbcCMto7KHNGYlp","Version":"1.2","Type":"unknown","NAT":"unrestricted"}`))
  706. // r, err = http.NewRequest("POST", "snowflake.broker/proxy", data)
  707. // if err != nil {
  708. // log.Printf("unable to get NewRequest with error: %v", err)
  709. // }
  710. // r.RemoteAddr = "129.97.208.24:8888" //CA geoip
  711. // go func(i *IPC) {
  712. // proxyPolls(i, w, r)
  713. // done <- true
  714. // }(i)
  715. // p = <-ctx.proxyPolls //manually unblock poll
  716. // p.offerChannel <- nil
  717. // <-done
  718. //
  719. // ctx.metrics.printMetrics()
  720. // So(buf.String(), ShouldContainSubstring, "snowflake-ips-nat-restricted 1\nsnowflake-ips-nat-unrestricted 1\nsnowflake-ips-nat-unknown 0")
  721. // })
  722. // //Test client failures by NAT type
  723. // Convey("client failures by NAT type", func() {
  724. // w := httptest.NewRecorder()
  725. // data := bytes.NewReader(
  726. // []byte("1.0\n{\"offer\": \"fake\", \"nat\": \"restricted\"}"))
  727. // r, err := http.NewRequest("POST", "snowflake.broker/client", data)
  728. // So(err, ShouldBeNil)
  729. //
  730. // clientOffers(i, w, r)
  731. //
  732. // ctx.metrics.printMetrics()
  733. // So(buf.String(), ShouldContainSubstring, "client-denied-count 8\nclient-restricted-denied-count 8\nclient-unrestricted-denied-count 0\nclient-snowflake-match-count 0")
  734. //
  735. // buf.Reset()
  736. // ctx.metrics.zeroMetrics()
  737. //
  738. // data = bytes.NewReader(
  739. // []byte("1.0\n{\"offer\": \"fake\", \"nat\": \"unrestricted\"}"))
  740. // r, err = http.NewRequest("POST", "snowflake.broker/client", data)
  741. // So(err, ShouldBeNil)
  742. //
  743. // clientOffers(i, w, r)
  744. //
  745. // ctx.metrics.printMetrics()
  746. // So(buf.String(), ShouldContainSubstring, "client-denied-count 8\nclient-restricted-denied-count 0\nclient-unrestricted-denied-count 8\nclient-snowflake-match-count 0")
  747. //
  748. // buf.Reset()
  749. // ctx.metrics.zeroMetrics()
  750. //
  751. // data = bytes.NewReader(
  752. // []byte("1.0\n{\"offer\": \"fake\", \"nat\": \"unknown\"}"))
  753. // r, err = http.NewRequest("POST", "snowflake.broker/client", data)
  754. // So(err, ShouldBeNil)
  755. //
  756. // clientOffers(i, w, r)
  757. //
  758. // ctx.metrics.printMetrics()
  759. // So(buf.String(), ShouldContainSubstring, "client-denied-count 8\nclient-restricted-denied-count 8\nclient-unrestricted-denied-count 0\nclient-snowflake-match-count 0")
  760. // })
  761. // Convey("for country stats order", func() {
  762. //
  763. // stats := map[string]int{
  764. // "IT": 50,
  765. // "FR": 200,
  766. // "TZ": 100,
  767. // "CN": 250,
  768. // "RU": 150,
  769. // "CA": 1,
  770. // "BE": 1,
  771. // "PH": 1,
  772. // }
  773. // ctx.metrics.countryStats.counts = stats
  774. // So(ctx.metrics.countryStats.Display(), ShouldEqual, "CN=250,FR=200,RU=150,TZ=100,IT=50,BE=1,CA=1,PH=1")
  775. // })
  776. // })
  777. // }