util.spec.js 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. /* global expect, it, describe, Parse, Params */
  2. /*
  3. jasmine tests for Snowflake utils
  4. */
  5. describe('Parse', function() {
  6. describe('cookie', function() {
  7. it('parses correctly', function() {
  8. expect(Parse.cookie('')).toEqual({});
  9. expect(Parse.cookie('a=b')).toEqual({
  10. a: 'b'
  11. });
  12. expect(Parse.cookie('a=b=c')).toEqual({
  13. a: 'b=c'
  14. });
  15. expect(Parse.cookie('a=b; c=d')).toEqual({
  16. a: 'b',
  17. c: 'd'
  18. });
  19. expect(Parse.cookie('a=b ; c=d')).toEqual({
  20. a: 'b',
  21. c: 'd'
  22. });
  23. expect(Parse.cookie('a= b')).toEqual({
  24. a: 'b'
  25. });
  26. expect(Parse.cookie('a=')).toEqual({
  27. a: ''
  28. });
  29. expect(Parse.cookie('key')).toBeNull();
  30. expect(Parse.cookie('key=%26%20')).toEqual({
  31. key: '& '
  32. });
  33. expect(Parse.cookie('a=\'\'')).toEqual({
  34. a: '\'\''
  35. });
  36. });
  37. });
  38. describe('address', function() {
  39. it('parses IPv4', function() {
  40. expect(Parse.address('')).toBeNull();
  41. expect(Parse.address('3.3.3.3:4444')).toEqual({
  42. host: '3.3.3.3',
  43. port: 4444
  44. });
  45. expect(Parse.address('3.3.3.3')).toBeNull();
  46. expect(Parse.address('3.3.3.3:0x1111')).toBeNull();
  47. expect(Parse.address('3.3.3.3:-4444')).toBeNull();
  48. expect(Parse.address('3.3.3.3:65536')).toBeNull();
  49. });
  50. it('parses IPv6', function() {
  51. expect(Parse.address('[1:2::a:f]:4444')).toEqual({
  52. host: '1:2::a:f',
  53. port: 4444
  54. });
  55. expect(Parse.address('[1:2::a:f]')).toBeNull();
  56. expect(Parse.address('[1:2::a:f]:0x1111')).toBeNull();
  57. expect(Parse.address('[1:2::a:f]:-4444')).toBeNull();
  58. expect(Parse.address('[1:2::a:f]:65536')).toBeNull();
  59. expect(Parse.address('[1:2::ffff:1.2.3.4]:4444')).toEqual({
  60. host: '1:2::ffff:1.2.3.4',
  61. port: 4444
  62. });
  63. });
  64. });
  65. describe('byte count', function() {
  66. it('returns null for bad inputs', function() {
  67. expect(Parse.byteCount("")).toBeNull();
  68. expect(Parse.byteCount("x")).toBeNull();
  69. expect(Parse.byteCount("1x")).toBeNull();
  70. expect(Parse.byteCount("1.x")).toBeNull();
  71. expect(Parse.byteCount("1.2x")).toBeNull();
  72. expect(Parse.byteCount("toString")).toBeNull();
  73. expect(Parse.byteCount("1toString")).toBeNull();
  74. expect(Parse.byteCount("1.toString")).toBeNull();
  75. expect(Parse.byteCount("1.2toString")).toBeNull();
  76. expect(Parse.byteCount("k")).toBeNull();
  77. expect(Parse.byteCount("m")).toBeNull();
  78. expect(Parse.byteCount("g")).toBeNull();
  79. expect(Parse.byteCount("K")).toBeNull();
  80. expect(Parse.byteCount("M")).toBeNull();
  81. expect(Parse.byteCount("G")).toBeNull();
  82. expect(Parse.byteCount("-1")).toBeNull();
  83. expect(Parse.byteCount("-1k")).toBeNull();
  84. expect(Parse.byteCount("1.2.3")).toBeNull();
  85. expect(Parse.byteCount("1.2.3k")).toBeNull();
  86. });
  87. it('handles numbers without a suffix', function() {
  88. expect(Parse.byteCount("10")).toEqual(10);
  89. expect(Parse.byteCount("10.")).toEqual(10);
  90. expect(Parse.byteCount("1.5")).toEqual(1.5);
  91. });
  92. it('handles lowercase suffixes', function() {
  93. expect(Parse.byteCount("10k")).toEqual(10*1024);
  94. expect(Parse.byteCount("10m")).toEqual(10*1024*1024);
  95. expect(Parse.byteCount("10g")).toEqual(10*1024*1024*1024);
  96. expect(Parse.byteCount("10.k")).toEqual(10*1024);
  97. expect(Parse.byteCount("10.m")).toEqual(10*1024*1024);
  98. expect(Parse.byteCount("10.g")).toEqual(10*1024*1024*1024);
  99. expect(Parse.byteCount("1.5k")).toEqual(1.5*1024);
  100. expect(Parse.byteCount("1.5m")).toEqual(1.5*1024*1024);
  101. expect(Parse.byteCount("1.5G")).toEqual(1.5*1024*1024*1024);
  102. });
  103. it('handles uppercase suffixes', function() {
  104. expect(Parse.byteCount("10K")).toEqual(10*1024);
  105. expect(Parse.byteCount("10M")).toEqual(10*1024*1024);
  106. expect(Parse.byteCount("10G")).toEqual(10*1024*1024*1024);
  107. expect(Parse.byteCount("10.K")).toEqual(10*1024);
  108. expect(Parse.byteCount("10.M")).toEqual(10*1024*1024);
  109. expect(Parse.byteCount("10.G")).toEqual(10*1024*1024*1024);
  110. expect(Parse.byteCount("1.5K")).toEqual(1.5*1024);
  111. expect(Parse.byteCount("1.5M")).toEqual(1.5*1024*1024);
  112. expect(Parse.byteCount("1.5G")).toEqual(1.5*1024*1024*1024);
  113. });
  114. });
  115. describe('ipFromSDP', function() {
  116. var testCases = [
  117. {
  118. // https://tools.ietf.org/html/rfc4566#section-5
  119. sdp: "v=0\no=jdoe 2890844526 2890842807 IN IP4 10.47.16.5\ns=SDP Seminar\ni=A Seminar on the session description protocol\nu=http://www.example.com/seminars/sdp.pdf\ne=j.doe@example.com (Jane Doe)\nc=IN IP4 224.2.17.12/127\nt=2873397496 2873404696\na=recvonly\nm=audio 49170 RTP/AVP 0\nm=video 51372 RTP/AVP 99\na=rtpmap:99 h263-1998/90000",
  120. expected: '224.2.17.12'
  121. },
  122. {
  123. // Missing c= line
  124. sdp: "v=0\no=jdoe 2890844526 2890842807 IN IP4 10.47.16.5\ns=SDP Seminar\ni=A Seminar on the session description protocol\nu=http://www.example.com/seminars/sdp.pdf\ne=j.doe@example.com (Jane Doe)\nt=2873397496 2873404696\na=recvonly\nm=audio 49170 RTP/AVP 0\nm=video 51372 RTP/AVP 99\na=rtpmap:99 h263-1998/90000",
  125. expected: void 0
  126. },
  127. {
  128. // Single line, IP address only
  129. sdp: "c=IN IP4 224.2.1.1\n",
  130. expected: '224.2.1.1'
  131. },
  132. {
  133. // Same, with TTL
  134. sdp: "c=IN IP4 224.2.1.1/127\n",
  135. expected: '224.2.1.1'
  136. },
  137. {
  138. // Same, with TTL and multicast addresses
  139. sdp: "c=IN IP4 224.2.1.1/127/3\n",
  140. expected: '224.2.1.1'
  141. },
  142. {
  143. // IPv6, address only
  144. sdp: "c=IN IP6 FF15::101\n",
  145. expected: 'ff15::101'
  146. },
  147. {
  148. // Same, with multicast addresses
  149. sdp: "c=IN IP6 FF15::101/3\n",
  150. expected: 'ff15::101'
  151. },
  152. {
  153. // Multiple c= lines
  154. sdp: "c=IN IP4 1.2.3.4\nc=IN IP4 5.6.7.8",
  155. expected: '1.2.3.4'
  156. },
  157. {
  158. // Modified from SDP sent by snowflake-client.
  159. sdp: "v=0\no=- 7860378660295630295 2 IN IP4 127.0.0.1\ns=-\nt=0 0\na=group:BUNDLE data\na=msid-semantic: WMS\nm=application 54653 DTLS/SCTP 5000\nc=IN IP4 1.2.3.4\na=candidate:3581707038 1 udp 2122260223 192.168.0.1 54653 typ host generation 0 network-id 1 network-cost 50\na=candidate:2617212910 1 tcp 1518280447 192.168.0.1 59673 typ host tcptype passive generation 0 network-id 1 network-cost 50\na=candidate:2082671819 1 udp 1686052607 1.2.3.4 54653 typ srflx raddr 192.168.0.1 rport 54653 generation 0 network-id 1 network-cost 50\na=ice-ufrag:IBdf\na=ice-pwd:G3lTrrC9gmhQx481AowtkhYz\na=fingerprint:sha-256 53:F8:84:D9:3C:1F:A0:44:AA:D6:3C:65:80:D3:CB:6F:23:90:17:41:06:F9:9C:10:D8:48:4A:A8:B6:FA:14:A1\na=setup:actpass\na=mid:data\na=sctpmap:5000 webrtc-datachannel 1024",
  160. expected: '1.2.3.4'
  161. },
  162. {
  163. // Improper character within IPv4
  164. sdp: "c=IN IP4 224.2z.1.1",
  165. expected: void 0
  166. },
  167. {
  168. // Improper character within IPv6
  169. sdp: "c=IN IP6 ff15:g::101",
  170. expected: void 0
  171. },
  172. {
  173. // Bogus "IP7" addrtype
  174. sdp: "c=IN IP7 1.2.3.4\n",
  175. expected: void 0
  176. }
  177. ];
  178. it('parses SDP', function() {
  179. var i, len, ref, ref1, results, test;
  180. results = [];
  181. for (i = 0, len = testCases.length; i < len; i++) {
  182. test = testCases[i];
  183. // https://tools.ietf.org/html/rfc4566#section-5: "The sequence # CRLF
  184. // (0x0d0a) is used to end a record, although parsers SHOULD be tolerant
  185. // and also accept records terminated with a single newline character."
  186. // We represent the test cases with LF line endings for convenience, and
  187. // test them both that way and with CRLF line endings.
  188. expect((ref = Parse.ipFromSDP(test.sdp)) != null ? ref.toLowerCase() : void 0).toEqual(test.expected);
  189. results.push(expect((ref1 = Parse.ipFromSDP(test.sdp.replace(/\n/, "\r\n"))) != null ? ref1.toLowerCase() : void 0).toEqual(test.expected));
  190. }
  191. return results;
  192. });
  193. });
  194. });
  195. describe('Params', function() {
  196. describe('bool', function() {
  197. var getBool = function(query) {
  198. return Params.getBool(new URLSearchParams(query), 'param', false);
  199. };
  200. it('parses correctly', function() {
  201. expect(getBool('param=true')).toBe(true);
  202. expect(getBool('param')).toBe(true);
  203. expect(getBool('param=')).toBe(true);
  204. expect(getBool('param=1')).toBe(true);
  205. expect(getBool('param=0')).toBe(false);
  206. expect(getBool('param=false')).toBe(false);
  207. expect(getBool('param=unexpected')).toBeNull();
  208. expect(getBool('pram=true')).toBe(false);
  209. });
  210. });
  211. describe('byteCount', function() {
  212. var DEFAULT = 77;
  213. var getByteCount = function(query) {
  214. return Params.getByteCount(new URLSearchParams(query), 'param', DEFAULT);
  215. };
  216. it('supports default values', function() {
  217. expect(getByteCount('param=x')).toBeNull();
  218. expect(getByteCount('param=10')).toEqual(10);
  219. expect(getByteCount('foo=10k')).toEqual(DEFAULT);
  220. });
  221. });
  222. });