util_spec.js 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * JavaScript code in this page
  4. *
  5. * Copyright 2022 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.js");
  24. describe("util", function () {
  25. describe("bytesToString", function () {
  26. it("handles non-array arguments", function () {
  27. expect(function () {
  28. (0, _util.bytesToString)(null);
  29. }).toThrow(new Error("Invalid argument for bytesToString"));
  30. });
  31. it("handles array arguments with a length not exceeding the maximum", function () {
  32. expect((0, _util.bytesToString)(new Uint8Array([]))).toEqual("");
  33. expect((0, _util.bytesToString)(new Uint8Array([102, 111, 111]))).toEqual("foo");
  34. });
  35. it("handles array arguments with a length exceeding the maximum", function () {
  36. const length = 10000;
  37. const bytes = new Uint8Array(length);
  38. for (let i = 0; i < length; i++) {
  39. bytes[i] = "a".charCodeAt(0);
  40. }
  41. const string = "a".repeat(length);
  42. expect((0, _util.bytesToString)(bytes)).toEqual(string);
  43. });
  44. });
  45. describe("isArrayBuffer", function () {
  46. it("handles array buffer values", function () {
  47. expect((0, _util.isArrayBuffer)(new ArrayBuffer(0))).toEqual(true);
  48. expect((0, _util.isArrayBuffer)(new Uint8Array(0))).toEqual(true);
  49. });
  50. it("handles non-array buffer values", function () {
  51. expect((0, _util.isArrayBuffer)("true")).toEqual(false);
  52. expect((0, _util.isArrayBuffer)(1)).toEqual(false);
  53. expect((0, _util.isArrayBuffer)(null)).toEqual(false);
  54. expect((0, _util.isArrayBuffer)(undefined)).toEqual(false);
  55. });
  56. });
  57. describe("string32", function () {
  58. it("converts unsigned 32-bit integers to strings", function () {
  59. expect((0, _util.string32)(0x74727565)).toEqual("true");
  60. expect((0, _util.string32)(0x74797031)).toEqual("typ1");
  61. expect((0, _util.string32)(0x4f54544f)).toEqual("OTTO");
  62. });
  63. });
  64. describe("stringToBytes", function () {
  65. it("handles non-string arguments", function () {
  66. expect(function () {
  67. (0, _util.stringToBytes)(null);
  68. }).toThrow(new Error("Invalid argument for stringToBytes"));
  69. });
  70. it("handles string arguments", function () {
  71. expect((0, _util.stringToBytes)("")).toEqual(new Uint8Array([]));
  72. expect((0, _util.stringToBytes)("foo")).toEqual(new Uint8Array([102, 111, 111]));
  73. });
  74. });
  75. describe("stringToPDFString", function () {
  76. it("handles ISO Latin 1 strings", function () {
  77. const str = "\x8Dstring\x8E";
  78. expect((0, _util.stringToPDFString)(str)).toEqual("\u201Cstring\u201D");
  79. });
  80. it("handles UTF-16 big-endian strings", function () {
  81. const str = "\xFE\xFF\x00\x73\x00\x74\x00\x72\x00\x69\x00\x6E\x00\x67";
  82. expect((0, _util.stringToPDFString)(str)).toEqual("string");
  83. });
  84. it("handles UTF-16 little-endian strings", function () {
  85. const str = "\xFF\xFE\x73\x00\x74\x00\x72\x00\x69\x00\x6E\x00\x67\x00";
  86. expect((0, _util.stringToPDFString)(str)).toEqual("string");
  87. });
  88. it("handles UTF-8 strings", function () {
  89. const simpleStr = "\xEF\xBB\xBF\x73\x74\x72\x69\x6E\x67";
  90. expect((0, _util.stringToPDFString)(simpleStr)).toEqual("string");
  91. const complexStr = "\xEF\xBB\xBF\xE8\xA1\xA8\xE3\x83\x9D\xE3\x81\x82\x41\xE9\xB7\x97" + "\xC5\x92\xC3\xA9\xEF\xBC\xA2\xE9\x80\x8D\xC3\x9C\xC3\x9F\xC2\xAA" + "\xC4\x85\xC3\xB1\xE4\xB8\x82\xE3\x90\x80\xF0\xA0\x80\x80";
  92. expect((0, _util.stringToPDFString)(complexStr)).toEqual("表ポあA鷗ŒéB逍Üߪąñ丂㐀𠀀");
  93. });
  94. it("handles empty strings", function () {
  95. const str1 = "";
  96. expect((0, _util.stringToPDFString)(str1)).toEqual("");
  97. const str2 = "\xFE\xFF";
  98. expect((0, _util.stringToPDFString)(str2)).toEqual("");
  99. const str3 = "\xFF\xFE";
  100. expect((0, _util.stringToPDFString)(str3)).toEqual("");
  101. const str4 = "\xEF\xBB\xBF";
  102. expect((0, _util.stringToPDFString)(str4)).toEqual("");
  103. });
  104. });
  105. describe("ReadableStream", function () {
  106. it("should return an Object", function () {
  107. const readable = new ReadableStream();
  108. expect(typeof readable).toEqual("object");
  109. });
  110. it("should have property getReader", function () {
  111. const readable = new ReadableStream();
  112. expect(typeof readable.getReader).toEqual("function");
  113. });
  114. });
  115. describe("URL", function () {
  116. it("should return an Object", function () {
  117. const url = new URL("https://example.com");
  118. expect(typeof url).toEqual("object");
  119. });
  120. it("should have property `href`", function () {
  121. const url = new URL("https://example.com");
  122. expect(typeof url.href).toEqual("string");
  123. });
  124. });
  125. describe("createValidAbsoluteUrl", function () {
  126. it("handles invalid URLs", function () {
  127. expect((0, _util.createValidAbsoluteUrl)(undefined, undefined)).toEqual(null);
  128. expect((0, _util.createValidAbsoluteUrl)(null, null)).toEqual(null);
  129. expect((0, _util.createValidAbsoluteUrl)("/foo", "/bar")).toEqual(null);
  130. });
  131. it("handles URLs that do not use an allowed protocol", function () {
  132. expect((0, _util.createValidAbsoluteUrl)("magnet:?foo", null)).toEqual(null);
  133. });
  134. it("correctly creates a valid URL for allowed protocols", function () {
  135. expect((0, _util.createValidAbsoluteUrl)("http://www.mozilla.org/foo", null)).toEqual(new URL("http://www.mozilla.org/foo"));
  136. expect((0, _util.createValidAbsoluteUrl)("/foo", "http://www.mozilla.org")).toEqual(new URL("http://www.mozilla.org/foo"));
  137. expect((0, _util.createValidAbsoluteUrl)("https://www.mozilla.org/foo", null)).toEqual(new URL("https://www.mozilla.org/foo"));
  138. expect((0, _util.createValidAbsoluteUrl)("/foo", "https://www.mozilla.org")).toEqual(new URL("https://www.mozilla.org/foo"));
  139. expect((0, _util.createValidAbsoluteUrl)("ftp://www.mozilla.org/foo", null)).toEqual(new URL("ftp://www.mozilla.org/foo"));
  140. expect((0, _util.createValidAbsoluteUrl)("/foo", "ftp://www.mozilla.org")).toEqual(new URL("ftp://www.mozilla.org/foo"));
  141. expect((0, _util.createValidAbsoluteUrl)("mailto:foo@bar.baz", null)).toEqual(new URL("mailto:foo@bar.baz"));
  142. expect((0, _util.createValidAbsoluteUrl)("/foo", "mailto:foo@bar.baz")).toEqual(null);
  143. expect((0, _util.createValidAbsoluteUrl)("tel:+0123456789", null)).toEqual(new URL("tel:+0123456789"));
  144. expect((0, _util.createValidAbsoluteUrl)("/foo", "tel:0123456789")).toEqual(null);
  145. });
  146. });
  147. describe("createPromiseCapability", function () {
  148. it("should resolve with correct data", async function () {
  149. const promiseCapability = (0, _util.createPromiseCapability)();
  150. expect(promiseCapability.settled).toEqual(false);
  151. promiseCapability.resolve({
  152. test: "abc"
  153. });
  154. const data = await promiseCapability.promise;
  155. expect(promiseCapability.settled).toEqual(true);
  156. expect(data).toEqual({
  157. test: "abc"
  158. });
  159. });
  160. it("should reject with correct reason", async function () {
  161. const promiseCapability = (0, _util.createPromiseCapability)();
  162. expect(promiseCapability.settled).toEqual(false);
  163. promiseCapability.reject(new Error("reason"));
  164. try {
  165. await promiseCapability.promise;
  166. expect(false).toEqual(true);
  167. } catch (reason) {
  168. expect(promiseCapability.settled).toEqual(true);
  169. expect(reason instanceof Error).toEqual(true);
  170. expect(reason.message).toEqual("reason");
  171. }
  172. });
  173. });
  174. describe("escapeString", function () {
  175. it("should escape (, ), \\n, \\r, and \\", function () {
  176. expect((0, _util.escapeString)("((a\\a))\n(b(b\\b)\rb)")).toEqual("\\(\\(a\\\\a\\)\\)\\n\\(b\\(b\\\\b\\)\\rb\\)");
  177. });
  178. });
  179. describe("getModificationDate", function () {
  180. it("should get a correctly formatted date", function () {
  181. const date = new Date(Date.UTC(3141, 5, 9, 2, 6, 53));
  182. expect((0, _util.getModificationDate)(date)).toEqual("31410609020653");
  183. });
  184. });
  185. describe("isAscii", function () {
  186. it("handles ascii/non-ascii strings", function () {
  187. expect((0, _util.isAscii)("hello world")).toEqual(true);
  188. expect((0, _util.isAscii)("こんにちは世界の")).toEqual(false);
  189. expect((0, _util.isAscii)("hello world in Japanese is こんにちは世界の")).toEqual(false);
  190. });
  191. });
  192. describe("stringToUTF16BEString", function () {
  193. it("should encode a string in UTF16BE with a BOM", function () {
  194. expect((0, _util.stringToUTF16BEString)("hello world")).toEqual("\xfe\xff\0h\0e\0l\0l\0o\0 \0w\0o\0r\0l\0d");
  195. expect((0, _util.stringToUTF16BEString)("こんにちは世界の")).toEqual("\xfe\xff\x30\x53\x30\x93\x30\x6b\x30\x61" + "\x30\x6f\x4e\x16\x75\x4c\x30\x6e");
  196. });
  197. });
  198. });