util_spec.js 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. /* Copyright 2017 Mozilla Foundation
  2. *
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. import {
  16. bytesToString,
  17. createPromiseCapability,
  18. createValidAbsoluteUrl,
  19. getModificationDate,
  20. isArrayBuffer,
  21. string32,
  22. stringToBytes,
  23. stringToPDFString,
  24. } from "../../src/shared/util.js";
  25. describe("util", function () {
  26. describe("bytesToString", function () {
  27. it("handles non-array arguments", function () {
  28. expect(function () {
  29. 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(bytesToString(new Uint8Array([]))).toEqual("");
  34. expect(bytesToString(new Uint8Array([102, 111, 111]))).toEqual("foo");
  35. });
  36. it("handles array arguments with a length exceeding the maximum", function () {
  37. const length = 10000; // Larger than MAX_ARGUMENT_COUNT = 8192.
  38. // Create an array with `length` 'a' character codes.
  39. const bytes = new Uint8Array(length);
  40. for (let i = 0; i < length; i++) {
  41. bytes[i] = "a".charCodeAt(0);
  42. }
  43. // Create a string with `length` 'a' characters.
  44. const string = "a".repeat(length);
  45. expect(bytesToString(bytes)).toEqual(string);
  46. });
  47. });
  48. describe("isArrayBuffer", function () {
  49. it("handles array buffer values", function () {
  50. expect(isArrayBuffer(new ArrayBuffer(0))).toEqual(true);
  51. expect(isArrayBuffer(new Uint8Array(0))).toEqual(true);
  52. });
  53. it("handles non-array buffer values", function () {
  54. expect(isArrayBuffer("true")).toEqual(false);
  55. expect(isArrayBuffer(1)).toEqual(false);
  56. expect(isArrayBuffer(null)).toEqual(false);
  57. expect(isArrayBuffer(undefined)).toEqual(false);
  58. });
  59. });
  60. describe("string32", function () {
  61. it("converts unsigned 32-bit integers to strings", function () {
  62. expect(string32(0x74727565)).toEqual("true");
  63. expect(string32(0x74797031)).toEqual("typ1");
  64. expect(string32(0x4f54544f)).toEqual("OTTO");
  65. });
  66. });
  67. describe("stringToBytes", function () {
  68. it("handles non-string arguments", function () {
  69. expect(function () {
  70. stringToBytes(null);
  71. }).toThrow(new Error("Invalid argument for stringToBytes"));
  72. });
  73. it("handles string arguments", function () {
  74. expect(stringToBytes("")).toEqual(new Uint8Array([]));
  75. expect(stringToBytes("foo")).toEqual(new Uint8Array([102, 111, 111]));
  76. });
  77. });
  78. describe("stringToPDFString", function () {
  79. it("handles ISO Latin 1 strings", function () {
  80. const str = "\x8Dstring\x8E";
  81. expect(stringToPDFString(str)).toEqual("\u201Cstring\u201D");
  82. });
  83. it("handles UTF-16 big-endian strings", function () {
  84. const str = "\xFE\xFF\x00\x73\x00\x74\x00\x72\x00\x69\x00\x6E\x00\x67";
  85. expect(stringToPDFString(str)).toEqual("string");
  86. });
  87. it("handles UTF-16 little-endian strings", function () {
  88. const str = "\xFF\xFE\x73\x00\x74\x00\x72\x00\x69\x00\x6E\x00\x67\x00";
  89. expect(stringToPDFString(str)).toEqual("string");
  90. });
  91. it("handles UTF-8 strings", function () {
  92. const simpleStr = "\xEF\xBB\xBF\x73\x74\x72\x69\x6E\x67";
  93. expect(stringToPDFString(simpleStr)).toEqual("string");
  94. const complexStr =
  95. "\xEF\xBB\xBF\xE8\xA1\xA8\xE3\x83\x9D\xE3\x81\x82\x41\xE9\xB7\x97" +
  96. "\xC5\x92\xC3\xA9\xEF\xBC\xA2\xE9\x80\x8D\xC3\x9C\xC3\x9F\xC2\xAA" +
  97. "\xC4\x85\xC3\xB1\xE4\xB8\x82\xE3\x90\x80\xF0\xA0\x80\x80";
  98. expect(stringToPDFString(complexStr)).toEqual(
  99. "表ポあA鷗ŒéB逍Üߪąñ丂㐀𠀀"
  100. );
  101. });
  102. it("handles empty strings", function () {
  103. // ISO Latin 1
  104. const str1 = "";
  105. expect(stringToPDFString(str1)).toEqual("");
  106. // UTF-16BE
  107. const str2 = "\xFE\xFF";
  108. expect(stringToPDFString(str2)).toEqual("");
  109. // UTF-16LE
  110. const str3 = "\xFF\xFE";
  111. expect(stringToPDFString(str3)).toEqual("");
  112. // UTF-8
  113. const str4 = "\xEF\xBB\xBF";
  114. expect(stringToPDFString(str4)).toEqual("");
  115. });
  116. });
  117. describe("ReadableStream", function () {
  118. it("should return an Object", function () {
  119. const readable = new ReadableStream();
  120. expect(typeof readable).toEqual("object");
  121. });
  122. it("should have property getReader", function () {
  123. const readable = new ReadableStream();
  124. expect(typeof readable.getReader).toEqual("function");
  125. });
  126. });
  127. describe("URL", function () {
  128. it("should return an Object", function () {
  129. const url = new URL("https://example.com");
  130. expect(typeof url).toEqual("object");
  131. });
  132. it("should have property `href`", function () {
  133. const url = new URL("https://example.com");
  134. expect(typeof url.href).toEqual("string");
  135. });
  136. });
  137. describe("createValidAbsoluteUrl", function () {
  138. it("handles invalid URLs", function () {
  139. expect(createValidAbsoluteUrl(undefined, undefined)).toEqual(null);
  140. expect(createValidAbsoluteUrl(null, null)).toEqual(null);
  141. expect(createValidAbsoluteUrl("/foo", "/bar")).toEqual(null);
  142. });
  143. it("handles URLs that do not use an allowed protocol", function () {
  144. expect(createValidAbsoluteUrl("magnet:?foo", null)).toEqual(null);
  145. });
  146. it("correctly creates a valid URL for allowed protocols", function () {
  147. // `http` protocol
  148. expect(
  149. createValidAbsoluteUrl("http://www.mozilla.org/foo", null)
  150. ).toEqual(new URL("http://www.mozilla.org/foo"));
  151. expect(createValidAbsoluteUrl("/foo", "http://www.mozilla.org")).toEqual(
  152. new URL("http://www.mozilla.org/foo")
  153. );
  154. // `https` protocol
  155. expect(
  156. createValidAbsoluteUrl("https://www.mozilla.org/foo", null)
  157. ).toEqual(new URL("https://www.mozilla.org/foo"));
  158. expect(createValidAbsoluteUrl("/foo", "https://www.mozilla.org")).toEqual(
  159. new URL("https://www.mozilla.org/foo")
  160. );
  161. // `ftp` protocol
  162. expect(createValidAbsoluteUrl("ftp://www.mozilla.org/foo", null)).toEqual(
  163. new URL("ftp://www.mozilla.org/foo")
  164. );
  165. expect(createValidAbsoluteUrl("/foo", "ftp://www.mozilla.org")).toEqual(
  166. new URL("ftp://www.mozilla.org/foo")
  167. );
  168. // `mailto` protocol (base URLs have no meaning and should yield `null`)
  169. expect(createValidAbsoluteUrl("mailto:foo@bar.baz", null)).toEqual(
  170. new URL("mailto:foo@bar.baz")
  171. );
  172. expect(createValidAbsoluteUrl("/foo", "mailto:foo@bar.baz")).toEqual(
  173. null
  174. );
  175. // `tel` protocol (base URLs have no meaning and should yield `null`)
  176. expect(createValidAbsoluteUrl("tel:+0123456789", null)).toEqual(
  177. new URL("tel:+0123456789")
  178. );
  179. expect(createValidAbsoluteUrl("/foo", "tel:0123456789")).toEqual(null);
  180. });
  181. });
  182. describe("createPromiseCapability", function () {
  183. it("should resolve with correct data", async function () {
  184. const promiseCapability = createPromiseCapability();
  185. expect(promiseCapability.settled).toEqual(false);
  186. promiseCapability.resolve({ test: "abc" });
  187. const data = await promiseCapability.promise;
  188. expect(promiseCapability.settled).toEqual(true);
  189. expect(data).toEqual({ test: "abc" });
  190. });
  191. it("should reject with correct reason", async function () {
  192. const promiseCapability = createPromiseCapability();
  193. expect(promiseCapability.settled).toEqual(false);
  194. promiseCapability.reject(new Error("reason"));
  195. try {
  196. await promiseCapability.promise;
  197. // Shouldn't get here.
  198. expect(false).toEqual(true);
  199. } catch (reason) {
  200. expect(promiseCapability.settled).toEqual(true);
  201. expect(reason instanceof Error).toEqual(true);
  202. expect(reason.message).toEqual("reason");
  203. }
  204. });
  205. });
  206. describe("getModificationDate", function () {
  207. it("should get a correctly formatted date", function () {
  208. const date = new Date(Date.UTC(3141, 5, 9, 2, 6, 53));
  209. expect(getModificationDate(date)).toEqual("31410609020653");
  210. });
  211. });
  212. });