util_spec.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * Javascript code in this page
  4. *
  5. * Copyright 2019 Mozilla Foundation
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License");
  8. * you may not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS,
  15. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. *
  19. * @licend The above is the entire license notice for the
  20. * Javascript code in this page
  21. */
  22. "use strict";
  23. var _util = require("../../shared/util");
  24. function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
  25. describe('util', function () {
  26. describe('bytesToString', function () {
  27. it('handles non-array arguments', function () {
  28. expect(function () {
  29. (0, _util.bytesToString)(null);
  30. }).toThrow(new Error('Invalid argument for bytesToString'));
  31. });
  32. it('handles array arguments with a length not exceeding the maximum', function () {
  33. expect((0, _util.bytesToString)(new Uint8Array([]))).toEqual('');
  34. expect((0, _util.bytesToString)(new Uint8Array([102, 111, 111]))).toEqual('foo');
  35. });
  36. it('handles array arguments with a length exceeding the maximum', function () {
  37. var length = 10000;
  38. var bytes = new Uint8Array(length);
  39. for (var i = 0; i < length; i++) {
  40. bytes[i] = 'a'.charCodeAt(0);
  41. }
  42. var string = Array(length + 1).join('a');
  43. expect((0, _util.bytesToString)(bytes)).toEqual(string);
  44. });
  45. });
  46. describe('isArrayBuffer', function () {
  47. it('handles array buffer values', function () {
  48. expect((0, _util.isArrayBuffer)(new ArrayBuffer(0))).toEqual(true);
  49. expect((0, _util.isArrayBuffer)(new Uint8Array(0))).toEqual(true);
  50. });
  51. it('handles non-array buffer values', function () {
  52. expect((0, _util.isArrayBuffer)('true')).toEqual(false);
  53. expect((0, _util.isArrayBuffer)(1)).toEqual(false);
  54. expect((0, _util.isArrayBuffer)(null)).toEqual(false);
  55. expect((0, _util.isArrayBuffer)(undefined)).toEqual(false);
  56. });
  57. });
  58. describe('isBool', function () {
  59. it('handles boolean values', function () {
  60. expect((0, _util.isBool)(true)).toEqual(true);
  61. expect((0, _util.isBool)(false)).toEqual(true);
  62. });
  63. it('handles non-boolean values', function () {
  64. expect((0, _util.isBool)('true')).toEqual(false);
  65. expect((0, _util.isBool)('false')).toEqual(false);
  66. expect((0, _util.isBool)(1)).toEqual(false);
  67. expect((0, _util.isBool)(0)).toEqual(false);
  68. expect((0, _util.isBool)(null)).toEqual(false);
  69. expect((0, _util.isBool)(undefined)).toEqual(false);
  70. });
  71. });
  72. describe('isEmptyObj', function () {
  73. it('handles empty objects', function () {
  74. expect((0, _util.isEmptyObj)({})).toEqual(true);
  75. });
  76. it('handles non-empty objects', function () {
  77. expect((0, _util.isEmptyObj)({
  78. foo: 'bar'
  79. })).toEqual(false);
  80. });
  81. });
  82. describe('isNum', function () {
  83. it('handles numeric values', function () {
  84. expect((0, _util.isNum)(1)).toEqual(true);
  85. expect((0, _util.isNum)(0)).toEqual(true);
  86. expect((0, _util.isNum)(-1)).toEqual(true);
  87. expect((0, _util.isNum)(1000000000000000000)).toEqual(true);
  88. expect((0, _util.isNum)(12.34)).toEqual(true);
  89. });
  90. it('handles non-numeric values', function () {
  91. expect((0, _util.isNum)('true')).toEqual(false);
  92. expect((0, _util.isNum)(true)).toEqual(false);
  93. expect((0, _util.isNum)(null)).toEqual(false);
  94. expect((0, _util.isNum)(undefined)).toEqual(false);
  95. });
  96. });
  97. describe('isSpace', function () {
  98. it('handles space characters', function () {
  99. expect((0, _util.isSpace)(0x20)).toEqual(true);
  100. expect((0, _util.isSpace)(0x09)).toEqual(true);
  101. expect((0, _util.isSpace)(0x0D)).toEqual(true);
  102. expect((0, _util.isSpace)(0x0A)).toEqual(true);
  103. });
  104. it('handles non-space characters', function () {
  105. expect((0, _util.isSpace)(0x0B)).toEqual(false);
  106. expect((0, _util.isSpace)(null)).toEqual(false);
  107. expect((0, _util.isSpace)(undefined)).toEqual(false);
  108. });
  109. });
  110. describe('isString', function () {
  111. it('handles string values', function () {
  112. expect((0, _util.isString)('foo')).toEqual(true);
  113. expect((0, _util.isString)('')).toEqual(true);
  114. });
  115. it('handles non-string values', function () {
  116. expect((0, _util.isString)(true)).toEqual(false);
  117. expect((0, _util.isString)(1)).toEqual(false);
  118. expect((0, _util.isString)(null)).toEqual(false);
  119. expect((0, _util.isString)(undefined)).toEqual(false);
  120. });
  121. });
  122. describe('log2', function () {
  123. it('handles values smaller than/equal to zero', function () {
  124. expect((0, _util.log2)(0)).toEqual(0);
  125. expect((0, _util.log2)(-1)).toEqual(0);
  126. });
  127. it('handles values larger than zero', function () {
  128. expect((0, _util.log2)(1)).toEqual(0);
  129. expect((0, _util.log2)(2)).toEqual(1);
  130. expect((0, _util.log2)(3)).toEqual(2);
  131. expect((0, _util.log2)(3.14)).toEqual(2);
  132. });
  133. });
  134. describe('string32', function () {
  135. it('converts unsigned 32-bit integers to strings', function () {
  136. expect((0, _util.string32)(0x74727565)).toEqual('true');
  137. expect((0, _util.string32)(0x74797031)).toEqual('typ1');
  138. expect((0, _util.string32)(0x4F54544F)).toEqual('OTTO');
  139. });
  140. });
  141. describe('stringToBytes', function () {
  142. it('handles non-string arguments', function () {
  143. expect(function () {
  144. (0, _util.stringToBytes)(null);
  145. }).toThrow(new Error('Invalid argument for stringToBytes'));
  146. });
  147. it('handles string arguments', function () {
  148. expect((0, _util.stringToBytes)('')).toEqual(new Uint8Array([]));
  149. expect((0, _util.stringToBytes)('foo')).toEqual(new Uint8Array([102, 111, 111]));
  150. });
  151. });
  152. describe('stringToPDFString', function () {
  153. it('handles ISO Latin 1 strings', function () {
  154. var str = '\x8Dstring\x8E';
  155. expect((0, _util.stringToPDFString)(str)).toEqual("\u201Cstring\u201D");
  156. });
  157. it('handles UTF-16BE strings', function () {
  158. var str = '\xFE\xFF\x00\x73\x00\x74\x00\x72\x00\x69\x00\x6E\x00\x67';
  159. expect((0, _util.stringToPDFString)(str)).toEqual('string');
  160. });
  161. it('handles empty strings', function () {
  162. var str1 = '';
  163. expect((0, _util.stringToPDFString)(str1)).toEqual('');
  164. var str2 = '\xFE\xFF';
  165. expect((0, _util.stringToPDFString)(str2)).toEqual('');
  166. });
  167. });
  168. describe('removeNullCharacters', function () {
  169. it('should not modify string without null characters', function () {
  170. var str = 'string without null chars';
  171. expect((0, _util.removeNullCharacters)(str)).toEqual('string without null chars');
  172. });
  173. it('should modify string with null characters', function () {
  174. var str = 'string\x00With\x00Null\x00Chars';
  175. expect((0, _util.removeNullCharacters)(str)).toEqual('stringWithNullChars');
  176. });
  177. });
  178. describe('ReadableStream', function () {
  179. it('should return an Object', function () {
  180. var readable = new _util.ReadableStream();
  181. expect(_typeof(readable)).toEqual('object');
  182. });
  183. it('should have property getReader', function () {
  184. var readable = new _util.ReadableStream();
  185. expect(_typeof(readable.getReader)).toEqual('function');
  186. });
  187. });
  188. describe('URL', function () {
  189. it('should return an Object', function () {
  190. var url = new _util.URL('https://example.com');
  191. expect(_typeof(url)).toEqual('object');
  192. });
  193. it('should have property `href`', function () {
  194. var url = new _util.URL('https://example.com');
  195. expect(_typeof(url.href)).toEqual('string');
  196. });
  197. });
  198. describe('isSameOrigin', function () {
  199. it('handles invalid base URLs', function () {
  200. expect((0, _util.isSameOrigin)('/foo', '/bar')).toEqual(false);
  201. expect((0, _util.isSameOrigin)('blob:foo', '/bar')).toEqual(false);
  202. });
  203. it('correctly checks if the origin of both URLs matches', function () {
  204. expect((0, _util.isSameOrigin)('https://www.mozilla.org/foo', 'https://www.mozilla.org/bar')).toEqual(true);
  205. expect((0, _util.isSameOrigin)('https://www.mozilla.org/foo', 'https://www.example.com/bar')).toEqual(false);
  206. });
  207. });
  208. describe('createValidAbsoluteUrl', function () {
  209. it('handles invalid URLs', function () {
  210. expect((0, _util.createValidAbsoluteUrl)(undefined, undefined)).toEqual(null);
  211. expect((0, _util.createValidAbsoluteUrl)(null, null)).toEqual(null);
  212. expect((0, _util.createValidAbsoluteUrl)('/foo', '/bar')).toEqual(null);
  213. });
  214. it('handles URLs that do not use a whitelisted protocol', function () {
  215. expect((0, _util.createValidAbsoluteUrl)('magnet:?foo', null)).toEqual(null);
  216. });
  217. it('correctly creates a valid URL for whitelisted protocols', function () {
  218. expect((0, _util.createValidAbsoluteUrl)('http://www.mozilla.org/foo', null)).toEqual(new _util.URL('http://www.mozilla.org/foo'));
  219. expect((0, _util.createValidAbsoluteUrl)('/foo', 'http://www.mozilla.org')).toEqual(new _util.URL('http://www.mozilla.org/foo'));
  220. expect((0, _util.createValidAbsoluteUrl)('https://www.mozilla.org/foo', null)).toEqual(new _util.URL('https://www.mozilla.org/foo'));
  221. expect((0, _util.createValidAbsoluteUrl)('/foo', 'https://www.mozilla.org')).toEqual(new _util.URL('https://www.mozilla.org/foo'));
  222. expect((0, _util.createValidAbsoluteUrl)('ftp://www.mozilla.org/foo', null)).toEqual(new _util.URL('ftp://www.mozilla.org/foo'));
  223. expect((0, _util.createValidAbsoluteUrl)('/foo', 'ftp://www.mozilla.org')).toEqual(new _util.URL('ftp://www.mozilla.org/foo'));
  224. expect((0, _util.createValidAbsoluteUrl)('mailto:foo@bar.baz', null)).toEqual(new _util.URL('mailto:foo@bar.baz'));
  225. expect((0, _util.createValidAbsoluteUrl)('/foo', 'mailto:foo@bar.baz')).toEqual(null);
  226. expect((0, _util.createValidAbsoluteUrl)('tel:+0123456789', null)).toEqual(new _util.URL('tel:+0123456789'));
  227. expect((0, _util.createValidAbsoluteUrl)('/foo', 'tel:0123456789')).toEqual(null);
  228. });
  229. });
  230. describe('createPromiseCapability', function () {
  231. it('should resolve with correct data', function (done) {
  232. var promiseCapability = (0, _util.createPromiseCapability)();
  233. expect(promiseCapability.settled).toEqual(false);
  234. promiseCapability.resolve({
  235. test: 'abc'
  236. });
  237. promiseCapability.promise.then(function (data) {
  238. expect(promiseCapability.settled).toEqual(true);
  239. expect(data).toEqual({
  240. test: 'abc'
  241. });
  242. done();
  243. }, done.fail);
  244. });
  245. it('should reject with correct reason', function (done) {
  246. var promiseCapability = (0, _util.createPromiseCapability)();
  247. expect(promiseCapability.settled).toEqual(false);
  248. promiseCapability.reject(new Error('reason'));
  249. promiseCapability.promise.then(done.fail, function (reason) {
  250. expect(promiseCapability.settled).toEqual(true);
  251. expect(reason instanceof Error).toEqual(true);
  252. expect(reason.message).toEqual('reason');
  253. done();
  254. });
  255. });
  256. });
  257. });