2
0

util_spec.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * Javascript code in this page
  4. *
  5. * Copyright 2018 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. var _primitives = require("../../core/primitives");
  25. var _test_utils = require("./test_utils");
  26. 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); }
  27. describe('util', function () {
  28. describe('bytesToString', function () {
  29. it('handles non-array arguments', function () {
  30. expect(function () {
  31. (0, _util.bytesToString)(null);
  32. }).toThrow(new Error('Invalid argument for bytesToString'));
  33. });
  34. it('handles array arguments with a length not exceeding the maximum', function () {
  35. expect((0, _util.bytesToString)(new Uint8Array([]))).toEqual('');
  36. expect((0, _util.bytesToString)(new Uint8Array([102, 111, 111]))).toEqual('foo');
  37. });
  38. it('handles array arguments with a length exceeding the maximum', function () {
  39. var length = 10000;
  40. var bytes = new Uint8Array(length);
  41. for (var i = 0; i < length; i++) {
  42. bytes[i] = 'a'.charCodeAt(0);
  43. }
  44. var string = Array(length + 1).join('a');
  45. expect((0, _util.bytesToString)(bytes)).toEqual(string);
  46. });
  47. });
  48. describe('getInheritableProperty', function () {
  49. it('handles non-dictionary arguments', function () {
  50. expect((0, _util.getInheritableProperty)({
  51. dict: null,
  52. key: 'foo'
  53. })).toEqual(undefined);
  54. expect((0, _util.getInheritableProperty)({
  55. dict: undefined,
  56. key: 'foo'
  57. })).toEqual(undefined);
  58. });
  59. it('handles dictionaries that do not contain the property', function () {
  60. var emptyDict = new _primitives.Dict();
  61. expect((0, _util.getInheritableProperty)({
  62. dict: emptyDict,
  63. key: 'foo'
  64. })).toEqual(undefined);
  65. var filledDict = new _primitives.Dict();
  66. filledDict.set('bar', 'baz');
  67. expect((0, _util.getInheritableProperty)({
  68. dict: filledDict,
  69. key: 'foo'
  70. })).toEqual(undefined);
  71. });
  72. it('fetches the property if it is not inherited', function () {
  73. var ref = new _primitives.Ref(10, 0);
  74. var xref = new _test_utils.XRefMock([{
  75. ref: ref,
  76. data: 'quux'
  77. }]);
  78. var dict = new _primitives.Dict(xref);
  79. dict.set('foo', 'bar');
  80. expect((0, _util.getInheritableProperty)({
  81. dict: dict,
  82. key: 'foo'
  83. })).toEqual('bar');
  84. dict.set('baz', ['qux', ref]);
  85. expect((0, _util.getInheritableProperty)({
  86. dict: dict,
  87. key: 'baz',
  88. getArray: true
  89. })).toEqual(['qux', 'quux']);
  90. });
  91. it('fetches the property if it is inherited and present on one level', function () {
  92. var ref = new _primitives.Ref(10, 0);
  93. var xref = new _test_utils.XRefMock([{
  94. ref: ref,
  95. data: 'quux'
  96. }]);
  97. var firstDict = new _primitives.Dict(xref);
  98. var secondDict = new _primitives.Dict(xref);
  99. firstDict.set('Parent', secondDict);
  100. secondDict.set('foo', 'bar');
  101. expect((0, _util.getInheritableProperty)({
  102. dict: firstDict,
  103. key: 'foo'
  104. })).toEqual('bar');
  105. secondDict.set('baz', ['qux', ref]);
  106. expect((0, _util.getInheritableProperty)({
  107. dict: firstDict,
  108. key: 'baz',
  109. getArray: true
  110. })).toEqual(['qux', 'quux']);
  111. });
  112. it('fetches the property if it is inherited and present on multiple levels', function () {
  113. var ref = new _primitives.Ref(10, 0);
  114. var xref = new _test_utils.XRefMock([{
  115. ref: ref,
  116. data: 'quux'
  117. }]);
  118. var firstDict = new _primitives.Dict(xref);
  119. var secondDict = new _primitives.Dict(xref);
  120. firstDict.set('Parent', secondDict);
  121. firstDict.set('foo', 'bar1');
  122. secondDict.set('foo', 'bar2');
  123. expect((0, _util.getInheritableProperty)({
  124. dict: firstDict,
  125. key: 'foo'
  126. })).toEqual('bar1');
  127. expect((0, _util.getInheritableProperty)({
  128. dict: firstDict,
  129. key: 'foo',
  130. getArray: false,
  131. stopWhenFound: false
  132. })).toEqual(['bar1', 'bar2']);
  133. firstDict.set('baz', ['qux1', ref]);
  134. secondDict.set('baz', ['qux2', ref]);
  135. expect((0, _util.getInheritableProperty)({
  136. dict: firstDict,
  137. key: 'baz',
  138. getArray: true,
  139. stopWhenFound: false
  140. })).toEqual([['qux1', 'quux'], ['qux2', 'quux']]);
  141. });
  142. it('stops searching when the loop limit is reached', function () {
  143. var dict = new _primitives.Dict();
  144. var currentDict = dict;
  145. var parentDict = null;
  146. for (var i = 0; i < 150; i++) {
  147. parentDict = new _primitives.Dict();
  148. currentDict.set('Parent', parentDict);
  149. currentDict = parentDict;
  150. }
  151. parentDict.set('foo', 'bar');
  152. expect((0, _util.getInheritableProperty)({
  153. dict: dict,
  154. key: 'foo'
  155. })).toEqual(undefined);
  156. dict.set('foo', 'baz');
  157. expect((0, _util.getInheritableProperty)({
  158. dict: dict,
  159. key: 'foo',
  160. getArray: false,
  161. stopWhenFound: false
  162. })).toEqual(['baz']);
  163. });
  164. });
  165. describe('isArrayBuffer', function () {
  166. it('handles array buffer values', function () {
  167. expect((0, _util.isArrayBuffer)(new ArrayBuffer(0))).toEqual(true);
  168. expect((0, _util.isArrayBuffer)(new Uint8Array(0))).toEqual(true);
  169. });
  170. it('handles non-array buffer values', function () {
  171. expect((0, _util.isArrayBuffer)('true')).toEqual(false);
  172. expect((0, _util.isArrayBuffer)(1)).toEqual(false);
  173. expect((0, _util.isArrayBuffer)(null)).toEqual(false);
  174. expect((0, _util.isArrayBuffer)(undefined)).toEqual(false);
  175. });
  176. });
  177. describe('isBool', function () {
  178. it('handles boolean values', function () {
  179. expect((0, _util.isBool)(true)).toEqual(true);
  180. expect((0, _util.isBool)(false)).toEqual(true);
  181. });
  182. it('handles non-boolean values', function () {
  183. expect((0, _util.isBool)('true')).toEqual(false);
  184. expect((0, _util.isBool)('false')).toEqual(false);
  185. expect((0, _util.isBool)(1)).toEqual(false);
  186. expect((0, _util.isBool)(0)).toEqual(false);
  187. expect((0, _util.isBool)(null)).toEqual(false);
  188. expect((0, _util.isBool)(undefined)).toEqual(false);
  189. });
  190. });
  191. describe('isEmptyObj', function () {
  192. it('handles empty objects', function () {
  193. expect((0, _util.isEmptyObj)({})).toEqual(true);
  194. });
  195. it('handles non-empty objects', function () {
  196. expect((0, _util.isEmptyObj)({
  197. foo: 'bar'
  198. })).toEqual(false);
  199. });
  200. });
  201. describe('isNum', function () {
  202. it('handles numeric values', function () {
  203. expect((0, _util.isNum)(1)).toEqual(true);
  204. expect((0, _util.isNum)(0)).toEqual(true);
  205. expect((0, _util.isNum)(-1)).toEqual(true);
  206. expect((0, _util.isNum)(1000000000000000000)).toEqual(true);
  207. expect((0, _util.isNum)(12.34)).toEqual(true);
  208. });
  209. it('handles non-numeric values', function () {
  210. expect((0, _util.isNum)('true')).toEqual(false);
  211. expect((0, _util.isNum)(true)).toEqual(false);
  212. expect((0, _util.isNum)(null)).toEqual(false);
  213. expect((0, _util.isNum)(undefined)).toEqual(false);
  214. });
  215. });
  216. describe('isSpace', function () {
  217. it('handles space characters', function () {
  218. expect((0, _util.isSpace)(0x20)).toEqual(true);
  219. expect((0, _util.isSpace)(0x09)).toEqual(true);
  220. expect((0, _util.isSpace)(0x0D)).toEqual(true);
  221. expect((0, _util.isSpace)(0x0A)).toEqual(true);
  222. });
  223. it('handles non-space characters', function () {
  224. expect((0, _util.isSpace)(0x0B)).toEqual(false);
  225. expect((0, _util.isSpace)(null)).toEqual(false);
  226. expect((0, _util.isSpace)(undefined)).toEqual(false);
  227. });
  228. });
  229. describe('isString', function () {
  230. it('handles string values', function () {
  231. expect((0, _util.isString)('foo')).toEqual(true);
  232. expect((0, _util.isString)('')).toEqual(true);
  233. });
  234. it('handles non-string values', function () {
  235. expect((0, _util.isString)(true)).toEqual(false);
  236. expect((0, _util.isString)(1)).toEqual(false);
  237. expect((0, _util.isString)(null)).toEqual(false);
  238. expect((0, _util.isString)(undefined)).toEqual(false);
  239. });
  240. });
  241. describe('log2', function () {
  242. it('handles values smaller than/equal to zero', function () {
  243. expect((0, _util.log2)(0)).toEqual(0);
  244. expect((0, _util.log2)(-1)).toEqual(0);
  245. });
  246. it('handles values larger than zero', function () {
  247. expect((0, _util.log2)(1)).toEqual(0);
  248. expect((0, _util.log2)(2)).toEqual(1);
  249. expect((0, _util.log2)(3)).toEqual(2);
  250. expect((0, _util.log2)(3.14)).toEqual(2);
  251. });
  252. });
  253. describe('string32', function () {
  254. it('converts unsigned 32-bit integers to strings', function () {
  255. expect((0, _util.string32)(0x74727565)).toEqual('true');
  256. expect((0, _util.string32)(0x74797031)).toEqual('typ1');
  257. expect((0, _util.string32)(0x4F54544F)).toEqual('OTTO');
  258. });
  259. });
  260. describe('stringToBytes', function () {
  261. it('handles non-string arguments', function () {
  262. expect(function () {
  263. (0, _util.stringToBytes)(null);
  264. }).toThrow(new Error('Invalid argument for stringToBytes'));
  265. });
  266. it('handles string arguments', function () {
  267. expect((0, _util.stringToBytes)('')).toEqual(new Uint8Array([]));
  268. expect((0, _util.stringToBytes)('foo')).toEqual(new Uint8Array([102, 111, 111]));
  269. });
  270. });
  271. describe('stringToPDFString', function () {
  272. it('handles ISO Latin 1 strings', function () {
  273. var str = '\x8Dstring\x8E';
  274. expect((0, _util.stringToPDFString)(str)).toEqual("\u201Cstring\u201D");
  275. });
  276. it('handles UTF-16BE strings', function () {
  277. var str = '\xFE\xFF\x00\x73\x00\x74\x00\x72\x00\x69\x00\x6E\x00\x67';
  278. expect((0, _util.stringToPDFString)(str)).toEqual('string');
  279. });
  280. it('handles empty strings', function () {
  281. var str1 = '';
  282. expect((0, _util.stringToPDFString)(str1)).toEqual('');
  283. var str2 = '\xFE\xFF';
  284. expect((0, _util.stringToPDFString)(str2)).toEqual('');
  285. });
  286. });
  287. describe('removeNullCharacters', function () {
  288. it('should not modify string without null characters', function () {
  289. var str = 'string without null chars';
  290. expect((0, _util.removeNullCharacters)(str)).toEqual('string without null chars');
  291. });
  292. it('should modify string with null characters', function () {
  293. var str = 'string\x00With\x00Null\x00Chars';
  294. expect((0, _util.removeNullCharacters)(str)).toEqual('stringWithNullChars');
  295. });
  296. });
  297. describe('ReadableStream', function () {
  298. it('should return an Object', function () {
  299. var readable = new _util.ReadableStream();
  300. expect(_typeof(readable)).toEqual('object');
  301. });
  302. it('should have property getReader', function () {
  303. var readable = new _util.ReadableStream();
  304. expect(_typeof(readable.getReader)).toEqual('function');
  305. });
  306. });
  307. describe('toRomanNumerals', function () {
  308. it('handles invalid arguments', function () {
  309. var _arr = ['foo', -1, 0];
  310. var _loop = function _loop() {
  311. var input = _arr[_i];
  312. expect(function () {
  313. (0, _util.toRomanNumerals)(input);
  314. }).toThrow(new Error('The number should be a positive integer.'));
  315. };
  316. for (var _i = 0; _i < _arr.length; _i++) {
  317. _loop();
  318. }
  319. });
  320. it('converts numbers to uppercase Roman numerals', function () {
  321. expect((0, _util.toRomanNumerals)(1)).toEqual('I');
  322. expect((0, _util.toRomanNumerals)(6)).toEqual('VI');
  323. expect((0, _util.toRomanNumerals)(7)).toEqual('VII');
  324. expect((0, _util.toRomanNumerals)(8)).toEqual('VIII');
  325. expect((0, _util.toRomanNumerals)(10)).toEqual('X');
  326. expect((0, _util.toRomanNumerals)(40)).toEqual('XL');
  327. expect((0, _util.toRomanNumerals)(100)).toEqual('C');
  328. expect((0, _util.toRomanNumerals)(500)).toEqual('D');
  329. expect((0, _util.toRomanNumerals)(1000)).toEqual('M');
  330. expect((0, _util.toRomanNumerals)(2019)).toEqual('MMXIX');
  331. });
  332. it('converts numbers to lowercase Roman numerals', function () {
  333. expect((0, _util.toRomanNumerals)(1, true)).toEqual('i');
  334. expect((0, _util.toRomanNumerals)(6, true)).toEqual('vi');
  335. expect((0, _util.toRomanNumerals)(7, true)).toEqual('vii');
  336. expect((0, _util.toRomanNumerals)(8, true)).toEqual('viii');
  337. expect((0, _util.toRomanNumerals)(10, true)).toEqual('x');
  338. expect((0, _util.toRomanNumerals)(40, true)).toEqual('xl');
  339. expect((0, _util.toRomanNumerals)(100, true)).toEqual('c');
  340. expect((0, _util.toRomanNumerals)(500, true)).toEqual('d');
  341. expect((0, _util.toRomanNumerals)(1000, true)).toEqual('m');
  342. expect((0, _util.toRomanNumerals)(2019, true)).toEqual('mmxix');
  343. });
  344. });
  345. describe('URL', function () {
  346. it('should return an Object', function () {
  347. var url = new _util.URL('https://example.com');
  348. expect(_typeof(url)).toEqual('object');
  349. });
  350. it('should have property `href`', function () {
  351. var url = new _util.URL('https://example.com');
  352. expect(_typeof(url.href)).toEqual('string');
  353. });
  354. });
  355. describe('isSameOrigin', function () {
  356. it('handles invalid base URLs', function () {
  357. expect((0, _util.isSameOrigin)('/foo', '/bar')).toEqual(false);
  358. expect((0, _util.isSameOrigin)('blob:foo', '/bar')).toEqual(false);
  359. });
  360. it('correctly checks if the origin of both URLs matches', function () {
  361. expect((0, _util.isSameOrigin)('https://www.mozilla.org/foo', 'https://www.mozilla.org/bar')).toEqual(true);
  362. expect((0, _util.isSameOrigin)('https://www.mozilla.org/foo', 'https://www.example.com/bar')).toEqual(false);
  363. });
  364. });
  365. describe('createValidAbsoluteUrl', function () {
  366. it('handles invalid URLs', function () {
  367. expect((0, _util.createValidAbsoluteUrl)(undefined, undefined)).toEqual(null);
  368. expect((0, _util.createValidAbsoluteUrl)(null, null)).toEqual(null);
  369. expect((0, _util.createValidAbsoluteUrl)('/foo', '/bar')).toEqual(null);
  370. });
  371. it('handles URLs that do not use a whitelisted protocol', function () {
  372. expect((0, _util.createValidAbsoluteUrl)('magnet:?foo', null)).toEqual(null);
  373. });
  374. it('correctly creates a valid URL for whitelisted protocols', function () {
  375. expect((0, _util.createValidAbsoluteUrl)('http://www.mozilla.org/foo', null)).toEqual(new _util.URL('http://www.mozilla.org/foo'));
  376. expect((0, _util.createValidAbsoluteUrl)('/foo', 'http://www.mozilla.org')).toEqual(new _util.URL('http://www.mozilla.org/foo'));
  377. expect((0, _util.createValidAbsoluteUrl)('https://www.mozilla.org/foo', null)).toEqual(new _util.URL('https://www.mozilla.org/foo'));
  378. expect((0, _util.createValidAbsoluteUrl)('/foo', 'https://www.mozilla.org')).toEqual(new _util.URL('https://www.mozilla.org/foo'));
  379. expect((0, _util.createValidAbsoluteUrl)('ftp://www.mozilla.org/foo', null)).toEqual(new _util.URL('ftp://www.mozilla.org/foo'));
  380. expect((0, _util.createValidAbsoluteUrl)('/foo', 'ftp://www.mozilla.org')).toEqual(new _util.URL('ftp://www.mozilla.org/foo'));
  381. expect((0, _util.createValidAbsoluteUrl)('mailto:foo@bar.baz', null)).toEqual(new _util.URL('mailto:foo@bar.baz'));
  382. expect((0, _util.createValidAbsoluteUrl)('/foo', 'mailto:foo@bar.baz')).toEqual(null);
  383. expect((0, _util.createValidAbsoluteUrl)('tel:+0123456789', null)).toEqual(new _util.URL('tel:+0123456789'));
  384. expect((0, _util.createValidAbsoluteUrl)('/foo', 'tel:0123456789')).toEqual(null);
  385. });
  386. });
  387. describe('createPromiseCapability', function () {
  388. it('should resolve with correct data', function (done) {
  389. var promiseCapability = (0, _util.createPromiseCapability)();
  390. expect(promiseCapability.settled).toEqual(false);
  391. promiseCapability.resolve({
  392. test: 'abc'
  393. });
  394. promiseCapability.promise.then(function (data) {
  395. expect(promiseCapability.settled).toEqual(true);
  396. expect(data).toEqual({
  397. test: 'abc'
  398. });
  399. done();
  400. }, done.fail);
  401. });
  402. it('should reject with correct reason', function (done) {
  403. var promiseCapability = (0, _util.createPromiseCapability)();
  404. expect(promiseCapability.settled).toEqual(false);
  405. promiseCapability.reject(new Error('reason'));
  406. promiseCapability.promise.then(done.fail, function (reason) {
  407. expect(promiseCapability.settled).toEqual(true);
  408. expect(reason instanceof Error).toEqual(true);
  409. expect(reason.message).toEqual('reason');
  410. done();
  411. });
  412. });
  413. });
  414. });