messages_test.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  1. package messages
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "testing"
  6. . "github.com/smartystreets/goconvey/convey"
  7. )
  8. func TestDecodeProxyPollRequest(t *testing.T) {
  9. Convey("Context", t, func() {
  10. for _, test := range []struct {
  11. sid string
  12. proxyType string
  13. natType string
  14. clients int
  15. data string
  16. err error
  17. acceptedRelayPattern string
  18. }{
  19. {
  20. //Version 1.0 proxy message
  21. sid: "ymbcCMto7KHNGYlp",
  22. proxyType: "unknown",
  23. natType: "unknown",
  24. clients: 0,
  25. data: `{"Sid":"ymbcCMto7KHNGYlp","Version":"1.0"}`,
  26. err: nil,
  27. },
  28. {
  29. //Version 1.1 proxy message
  30. sid: "ymbcCMto7KHNGYlp",
  31. proxyType: "standalone",
  32. natType: "unknown",
  33. clients: 0,
  34. data: `{"Sid":"ymbcCMto7KHNGYlp","Version":"1.1","Type":"standalone"}`,
  35. err: nil,
  36. },
  37. {
  38. //Version 1.2 proxy message
  39. sid: "ymbcCMto7KHNGYlp",
  40. proxyType: "standalone",
  41. natType: "restricted",
  42. clients: 0,
  43. data: `{"Sid":"ymbcCMto7KHNGYlp","Version":"1.2","Type":"standalone", "NAT":"restricted"}`,
  44. err: nil,
  45. },
  46. {
  47. //Version 1.2 proxy message with clients
  48. sid: "ymbcCMto7KHNGYlp",
  49. proxyType: "standalone",
  50. natType: "restricted",
  51. clients: 24,
  52. data: `{"Sid":"ymbcCMto7KHNGYlp","Version":"1.2","Type":"standalone", "NAT":"restricted","Clients":24}`,
  53. err: nil,
  54. },
  55. {
  56. //Version 1.3 proxy message with clients and proxyURL
  57. sid: "ymbcCMto7KHNGYlp",
  58. proxyType: "standalone",
  59. natType: "restricted",
  60. clients: 24,
  61. acceptedRelayPattern: "snowfalke.torproject.org",
  62. data: `{"Sid":"ymbcCMto7KHNGYlp","Version":"1.2","Type":"standalone", "NAT":"restricted","Clients":24, "AcceptedRelayPattern":"snowfalke.torproject.org"}`,
  63. err: nil,
  64. },
  65. {
  66. //Version 0.X proxy message:
  67. sid: "",
  68. proxyType: "",
  69. natType: "",
  70. clients: 0,
  71. data: "",
  72. err: &json.SyntaxError{},
  73. },
  74. {
  75. sid: "",
  76. proxyType: "",
  77. natType: "",
  78. clients: 0,
  79. data: `{"Sid":"ymbcCMto7KHNGYlp"}`,
  80. err: fmt.Errorf(""),
  81. },
  82. {
  83. sid: "",
  84. proxyType: "",
  85. natType: "",
  86. clients: 0,
  87. data: "{}",
  88. err: fmt.Errorf(""),
  89. },
  90. {
  91. sid: "",
  92. proxyType: "",
  93. natType: "",
  94. clients: 0,
  95. data: `{"Version":"1.0"}`,
  96. err: fmt.Errorf(""),
  97. },
  98. {
  99. sid: "",
  100. proxyType: "",
  101. natType: "",
  102. clients: 0,
  103. data: `{"Version":"2.0"}`,
  104. err: fmt.Errorf(""),
  105. },
  106. } {
  107. sid, proxyType, natType, clients, relayPattern, _, err := DecodeProxyPollRequestWithRelayPrefix([]byte(test.data))
  108. So(sid, ShouldResemble, test.sid)
  109. So(proxyType, ShouldResemble, test.proxyType)
  110. So(natType, ShouldResemble, test.natType)
  111. So(clients, ShouldEqual, test.clients)
  112. So(relayPattern, ShouldResemble, test.acceptedRelayPattern)
  113. So(err, ShouldHaveSameTypeAs, test.err)
  114. }
  115. })
  116. }
  117. func TestEncodeProxyPollRequests(t *testing.T) {
  118. Convey("Context", t, func() {
  119. b, err := EncodeProxyPollRequest("ymbcCMto7KHNGYlp", "standalone", "unknown", 16)
  120. So(err, ShouldBeNil)
  121. sid, proxyType, natType, clients, err := DecodeProxyPollRequest(b)
  122. So(sid, ShouldEqual, "ymbcCMto7KHNGYlp")
  123. So(proxyType, ShouldEqual, "standalone")
  124. So(natType, ShouldEqual, "unknown")
  125. So(clients, ShouldEqual, 16)
  126. So(err, ShouldBeNil)
  127. })
  128. }
  129. func TestDecodeProxyPollResponse(t *testing.T) {
  130. Convey("Context", t, func() {
  131. for _, test := range []struct {
  132. offer string
  133. data string
  134. relayURL string
  135. err error
  136. }{
  137. {
  138. offer: "fake offer",
  139. data: `{"Status":"client match","Offer":"fake offer","NAT":"unknown"}`,
  140. err: nil,
  141. },
  142. {
  143. offer: "fake offer",
  144. data: `{"Status":"client match","Offer":"fake offer","NAT":"unknown", "RelayURL":"wss://snowflake.torproject.org/proxy"}`,
  145. relayURL: "wss://snowflake.torproject.org/proxy",
  146. err: nil,
  147. },
  148. {
  149. offer: "",
  150. data: `{"Status":"no match"}`,
  151. err: nil,
  152. },
  153. {
  154. offer: "",
  155. data: `{"Status":"client match"}`,
  156. err: fmt.Errorf("no supplied offer"),
  157. },
  158. {
  159. offer: "",
  160. data: `{"Test":"test"}`,
  161. err: fmt.Errorf(""),
  162. },
  163. } {
  164. offer, _, relayURL, err := DecodePollResponseWithRelayURL([]byte(test.data))
  165. So(err, ShouldHaveSameTypeAs, test.err)
  166. So(offer, ShouldResemble, test.offer)
  167. So(relayURL, ShouldResemble, test.relayURL)
  168. }
  169. })
  170. }
  171. func TestEncodeProxyPollResponse(t *testing.T) {
  172. Convey("Context", t, func() {
  173. b, err := EncodePollResponse("fake offer", true, "restricted")
  174. So(err, ShouldBeNil)
  175. offer, natType, err := DecodePollResponse(b)
  176. So(offer, ShouldEqual, "fake offer")
  177. So(natType, ShouldEqual, "restricted")
  178. So(err, ShouldBeNil)
  179. b, err = EncodePollResponse("", false, "unknown")
  180. So(err, ShouldBeNil)
  181. offer, natType, err = DecodePollResponse(b)
  182. So(offer, ShouldEqual, "")
  183. So(natType, ShouldEqual, "unknown")
  184. So(err, ShouldBeNil)
  185. })
  186. }
  187. func TestEncodeProxyPollResponseWithProxyURL(t *testing.T) {
  188. Convey("Context", t, func() {
  189. b, err := EncodePollResponseWithRelayURL("fake offer", true, "restricted", "wss://test/", "")
  190. So(err, ShouldBeNil)
  191. offer, natType, err := DecodePollResponse(b)
  192. So(err, ShouldNotBeNil)
  193. offer, natType, relay, err := DecodePollResponseWithRelayURL(b)
  194. So(offer, ShouldEqual, "fake offer")
  195. So(natType, ShouldEqual, "restricted")
  196. So(relay, ShouldEqual, "wss://test/")
  197. So(err, ShouldBeNil)
  198. b, err = EncodePollResponse("", false, "unknown")
  199. So(err, ShouldBeNil)
  200. offer, natType, relay, err = DecodePollResponseWithRelayURL(b)
  201. So(offer, ShouldEqual, "")
  202. So(natType, ShouldEqual, "unknown")
  203. So(err, ShouldBeNil)
  204. b, err = EncodePollResponseWithRelayURL("fake offer", false, "restricted", "wss://test/", "test error reason")
  205. So(err, ShouldBeNil)
  206. offer, natType, relay, err = DecodePollResponseWithRelayURL(b)
  207. So(err, ShouldNotBeNil)
  208. So(err.Error(), ShouldContainSubstring, "test error reason")
  209. })
  210. }
  211. func TestDecodeProxyAnswerRequest(t *testing.T) {
  212. Convey("Context", t, func() {
  213. for _, test := range []struct {
  214. answer string
  215. sid string
  216. data string
  217. err error
  218. }{
  219. {
  220. "test",
  221. "test",
  222. `{"Version":"1.0","Sid":"test","Answer":"test"}`,
  223. nil,
  224. },
  225. {
  226. "",
  227. "",
  228. `{"type":"offer","sdp":"v=0\r\no=- 4358805017720277108 2 IN IP4 [scrubbed]\r\ns=-\r\nt=0 0\r\na=group:BUNDLE data\r\na=msid-semantic: WMS\r\nm=application 56688 DTLS/SCTP 5000\r\nc=IN IP4 [scrubbed]\r\na=candidate:3769337065 1 udp 2122260223 [scrubbed] 56688 typ host generation 0 network-id 1 network-cost 50\r\na=candidate:2921887769 1 tcp 1518280447 [scrubbed] 35441 typ host tcptype passive generation 0 network-id 1 network-cost 50\r\na=ice-ufrag:aMAZ\r\na=ice-pwd:jcHb08Jjgrazp2dzjdrvPPvV\r\na=ice-options:trickle\r\na=fingerprint:sha-256 C8:88:EE:B9:E7:02:2E:21:37:ED:7A:D1:EB:2B:A3:15:A2:3B:5B:1C:3D:D4:D5:1F:06:CF:52:40:03:F8:DD:66\r\na=setup:actpass\r\na=mid:data\r\na=sctpmap:5000 webrtc-datachannel 1024\r\n"}`,
  229. fmt.Errorf(""),
  230. },
  231. {
  232. "",
  233. "",
  234. `{"Version":"1.0","Answer":"test"}`,
  235. fmt.Errorf(""),
  236. },
  237. {
  238. "",
  239. "",
  240. `{"Version":"1.0","Sid":"test"}`,
  241. fmt.Errorf(""),
  242. },
  243. } {
  244. answer, sid, err := DecodeAnswerRequest([]byte(test.data))
  245. So(answer, ShouldResemble, test.answer)
  246. So(sid, ShouldResemble, test.sid)
  247. So(err, ShouldHaveSameTypeAs, test.err)
  248. }
  249. })
  250. }
  251. func TestEncodeProxyAnswerRequest(t *testing.T) {
  252. Convey("Context", t, func() {
  253. b, err := EncodeAnswerRequest("test answer", "test sid")
  254. So(err, ShouldBeNil)
  255. answer, sid, err := DecodeAnswerRequest(b)
  256. So(answer, ShouldEqual, "test answer")
  257. So(sid, ShouldEqual, "test sid")
  258. So(err, ShouldBeNil)
  259. })
  260. }
  261. func TestDecodeProxyAnswerResponse(t *testing.T) {
  262. Convey("Context", t, func() {
  263. for _, test := range []struct {
  264. success bool
  265. data string
  266. err error
  267. }{
  268. {
  269. true,
  270. `{"Status":"success"}`,
  271. nil,
  272. },
  273. {
  274. false,
  275. `{"Status":"client gone"}`,
  276. nil,
  277. },
  278. {
  279. false,
  280. `{"Test":"test"}`,
  281. fmt.Errorf(""),
  282. },
  283. } {
  284. success, err := DecodeAnswerResponse([]byte(test.data))
  285. So(success, ShouldResemble, test.success)
  286. So(err, ShouldHaveSameTypeAs, test.err)
  287. }
  288. })
  289. }
  290. func TestEncodeProxyAnswerResponse(t *testing.T) {
  291. Convey("Context", t, func() {
  292. b, err := EncodeAnswerResponse(true)
  293. So(err, ShouldBeNil)
  294. success, err := DecodeAnswerResponse(b)
  295. So(success, ShouldEqual, true)
  296. So(err, ShouldBeNil)
  297. b, err = EncodeAnswerResponse(false)
  298. So(err, ShouldBeNil)
  299. success, err = DecodeAnswerResponse(b)
  300. So(success, ShouldEqual, false)
  301. So(err, ShouldBeNil)
  302. })
  303. }
  304. func TestDecodeClientPollRequest(t *testing.T) {
  305. Convey("Context", t, func() {
  306. for _, test := range []struct {
  307. natType string
  308. offer string
  309. data string
  310. err error
  311. }{
  312. {
  313. //version 1.0 client message
  314. "unknown",
  315. "fake",
  316. `1.0
  317. {"nat":"unknown","offer":"fake"}`,
  318. nil,
  319. },
  320. {
  321. //version 1.0 client message
  322. "unknown",
  323. "fake",
  324. `1.0
  325. {"offer":"fake"}`,
  326. nil,
  327. },
  328. {
  329. //unknown version
  330. "",
  331. "",
  332. `{"version":"2.0"}`,
  333. fmt.Errorf(""),
  334. },
  335. {
  336. //no offer
  337. "",
  338. "",
  339. `1.0
  340. {"nat":"unknown"}`,
  341. fmt.Errorf(""),
  342. },
  343. } {
  344. req, err := DecodeClientPollRequest([]byte(test.data))
  345. So(err, ShouldHaveSameTypeAs, test.err)
  346. if test.err == nil {
  347. So(req.NAT, ShouldResemble, test.natType)
  348. So(req.Offer, ShouldResemble, test.offer)
  349. }
  350. }
  351. })
  352. }
  353. func TestEncodeClientPollRequests(t *testing.T) {
  354. Convey("Context", t, func() {
  355. for i, test := range []struct {
  356. natType string
  357. offer string
  358. fingerprint string
  359. err error
  360. }{
  361. {
  362. "unknown",
  363. "fake",
  364. "",
  365. nil,
  366. },
  367. {
  368. "unknown",
  369. "fake",
  370. defaultBridgeFingerprint,
  371. nil,
  372. },
  373. {
  374. "unknown",
  375. "fake",
  376. "123123",
  377. fmt.Errorf(""),
  378. },
  379. } {
  380. req1 := &ClientPollRequest{
  381. NAT: test.natType,
  382. Offer: test.offer,
  383. Fingerprint: test.fingerprint,
  384. }
  385. b, err := req1.EncodeClientPollRequest()
  386. So(err, ShouldBeNil)
  387. req2, err := DecodeClientPollRequest(b)
  388. So(err, ShouldHaveSameTypeAs, test.err)
  389. if test.err == nil {
  390. So(req2.Offer, ShouldEqual, req1.Offer)
  391. So(req2.NAT, ShouldEqual, req1.NAT)
  392. fingerprint := test.fingerprint
  393. if i == 0 {
  394. fingerprint = defaultBridgeFingerprint
  395. }
  396. So(req2.Fingerprint, ShouldEqual, fingerprint)
  397. }
  398. }
  399. })
  400. }
  401. func TestDecodeClientPollResponse(t *testing.T) {
  402. Convey("Context", t, func() {
  403. for _, test := range []struct {
  404. answer string
  405. msg string
  406. data string
  407. }{
  408. {
  409. "fake answer",
  410. "",
  411. `{"answer":"fake answer"}`,
  412. },
  413. {
  414. "",
  415. "no snowflakes",
  416. `{"error":"no snowflakes"}`,
  417. },
  418. } {
  419. resp, err := DecodeClientPollResponse([]byte(test.data))
  420. So(err, ShouldBeNil)
  421. So(resp.Answer, ShouldResemble, test.answer)
  422. So(resp.Error, ShouldResemble, test.msg)
  423. }
  424. })
  425. }
  426. func TestEncodeClientPollResponse(t *testing.T) {
  427. Convey("Context", t, func() {
  428. resp1 := &ClientPollResponse{
  429. Answer: "fake answer",
  430. }
  431. b, err := resp1.EncodePollResponse()
  432. So(err, ShouldBeNil)
  433. resp2, err := DecodeClientPollResponse(b)
  434. So(err, ShouldBeNil)
  435. So(resp1, ShouldResemble, resp2)
  436. resp1 = &ClientPollResponse{
  437. Error: "failed",
  438. }
  439. b, err = resp1.EncodePollResponse()
  440. So(err, ShouldBeNil)
  441. resp2, err = DecodeClientPollResponse(b)
  442. So(err, ShouldBeNil)
  443. So(resp1, ShouldResemble, resp2)
  444. })
  445. }