util_spec.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * Javascript code in this page
  4. *
  5. * Copyright 2020 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 = Array(length + 1).join("a");
  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("isBool", function () {
  58. it("handles boolean values", function () {
  59. expect((0, _util.isBool)(true)).toEqual(true);
  60. expect((0, _util.isBool)(false)).toEqual(true);
  61. });
  62. it("handles non-boolean values", function () {
  63. expect((0, _util.isBool)("true")).toEqual(false);
  64. expect((0, _util.isBool)("false")).toEqual(false);
  65. expect((0, _util.isBool)(1)).toEqual(false);
  66. expect((0, _util.isBool)(0)).toEqual(false);
  67. expect((0, _util.isBool)(null)).toEqual(false);
  68. expect((0, _util.isBool)(undefined)).toEqual(false);
  69. });
  70. });
  71. describe("isNum", function () {
  72. it("handles numeric values", function () {
  73. expect((0, _util.isNum)(1)).toEqual(true);
  74. expect((0, _util.isNum)(0)).toEqual(true);
  75. expect((0, _util.isNum)(-1)).toEqual(true);
  76. expect((0, _util.isNum)(1000000000000000000)).toEqual(true);
  77. expect((0, _util.isNum)(12.34)).toEqual(true);
  78. });
  79. it("handles non-numeric values", function () {
  80. expect((0, _util.isNum)("true")).toEqual(false);
  81. expect((0, _util.isNum)(true)).toEqual(false);
  82. expect((0, _util.isNum)(null)).toEqual(false);
  83. expect((0, _util.isNum)(undefined)).toEqual(false);
  84. });
  85. });
  86. describe("isString", function () {
  87. it("handles string values", function () {
  88. expect((0, _util.isString)("foo")).toEqual(true);
  89. expect((0, _util.isString)("")).toEqual(true);
  90. });
  91. it("handles non-string values", function () {
  92. expect((0, _util.isString)(true)).toEqual(false);
  93. expect((0, _util.isString)(1)).toEqual(false);
  94. expect((0, _util.isString)(null)).toEqual(false);
  95. expect((0, _util.isString)(undefined)).toEqual(false);
  96. });
  97. });
  98. describe("string32", function () {
  99. it("converts unsigned 32-bit integers to strings", function () {
  100. expect((0, _util.string32)(0x74727565)).toEqual("true");
  101. expect((0, _util.string32)(0x74797031)).toEqual("typ1");
  102. expect((0, _util.string32)(0x4f54544f)).toEqual("OTTO");
  103. });
  104. });
  105. describe("stringToBytes", function () {
  106. it("handles non-string arguments", function () {
  107. expect(function () {
  108. (0, _util.stringToBytes)(null);
  109. }).toThrow(new Error("Invalid argument for stringToBytes"));
  110. });
  111. it("handles string arguments", function () {
  112. expect((0, _util.stringToBytes)("")).toEqual(new Uint8Array([]));
  113. expect((0, _util.stringToBytes)("foo")).toEqual(new Uint8Array([102, 111, 111]));
  114. });
  115. });
  116. describe("stringToPDFString", function () {
  117. it("handles ISO Latin 1 strings", function () {
  118. const str = "\x8Dstring\x8E";
  119. expect((0, _util.stringToPDFString)(str)).toEqual("\u201Cstring\u201D");
  120. });
  121. it("handles UTF-16 big-endian strings", function () {
  122. const str = "\xFE\xFF\x00\x73\x00\x74\x00\x72\x00\x69\x00\x6E\x00\x67";
  123. expect((0, _util.stringToPDFString)(str)).toEqual("string");
  124. });
  125. it("handles UTF-16 little-endian strings", function () {
  126. const str = "\xFF\xFE\x73\x00\x74\x00\x72\x00\x69\x00\x6E\x00\x67\x00";
  127. expect((0, _util.stringToPDFString)(str)).toEqual("string");
  128. });
  129. it("handles empty strings", function () {
  130. const str1 = "";
  131. expect((0, _util.stringToPDFString)(str1)).toEqual("");
  132. const str2 = "\xFE\xFF";
  133. expect((0, _util.stringToPDFString)(str2)).toEqual("");
  134. const str3 = "\xFF\xFE";
  135. expect((0, _util.stringToPDFString)(str3)).toEqual("");
  136. });
  137. });
  138. describe("removeNullCharacters", function () {
  139. it("should not modify string without null characters", function () {
  140. const str = "string without null chars";
  141. expect((0, _util.removeNullCharacters)(str)).toEqual("string without null chars");
  142. });
  143. it("should modify string with null characters", function () {
  144. const str = "string\x00With\x00Null\x00Chars";
  145. expect((0, _util.removeNullCharacters)(str)).toEqual("stringWithNullChars");
  146. });
  147. });
  148. describe("ReadableStream", function () {
  149. it("should return an Object", function () {
  150. const readable = new ReadableStream();
  151. expect(typeof readable).toEqual("object");
  152. });
  153. it("should have property getReader", function () {
  154. const readable = new ReadableStream();
  155. expect(typeof readable.getReader).toEqual("function");
  156. });
  157. });
  158. describe("URL", function () {
  159. it("should return an Object", function () {
  160. const url = new URL("https://example.com");
  161. expect(typeof url).toEqual("object");
  162. });
  163. it("should have property `href`", function () {
  164. const url = new URL("https://example.com");
  165. expect(typeof url.href).toEqual("string");
  166. });
  167. });
  168. describe("isSameOrigin", function () {
  169. it("handles invalid base URLs", function () {
  170. expect((0, _util.isSameOrigin)("/foo", "/bar")).toEqual(false);
  171. expect((0, _util.isSameOrigin)("blob:foo", "/bar")).toEqual(false);
  172. });
  173. it("correctly checks if the origin of both URLs matches", function () {
  174. expect((0, _util.isSameOrigin)("https://www.mozilla.org/foo", "https://www.mozilla.org/bar")).toEqual(true);
  175. expect((0, _util.isSameOrigin)("https://www.mozilla.org/foo", "https://www.example.com/bar")).toEqual(false);
  176. });
  177. });
  178. describe("createValidAbsoluteUrl", function () {
  179. it("handles invalid URLs", function () {
  180. expect((0, _util.createValidAbsoluteUrl)(undefined, undefined)).toEqual(null);
  181. expect((0, _util.createValidAbsoluteUrl)(null, null)).toEqual(null);
  182. expect((0, _util.createValidAbsoluteUrl)("/foo", "/bar")).toEqual(null);
  183. });
  184. it("handles URLs that do not use an allowed protocol", function () {
  185. expect((0, _util.createValidAbsoluteUrl)("magnet:?foo", null)).toEqual(null);
  186. });
  187. it("correctly creates a valid URL for allowed protocols", function () {
  188. expect((0, _util.createValidAbsoluteUrl)("http://www.mozilla.org/foo", null)).toEqual(new URL("http://www.mozilla.org/foo"));
  189. expect((0, _util.createValidAbsoluteUrl)("/foo", "http://www.mozilla.org")).toEqual(new URL("http://www.mozilla.org/foo"));
  190. expect((0, _util.createValidAbsoluteUrl)("https://www.mozilla.org/foo", null)).toEqual(new URL("https://www.mozilla.org/foo"));
  191. expect((0, _util.createValidAbsoluteUrl)("/foo", "https://www.mozilla.org")).toEqual(new URL("https://www.mozilla.org/foo"));
  192. expect((0, _util.createValidAbsoluteUrl)("ftp://www.mozilla.org/foo", null)).toEqual(new URL("ftp://www.mozilla.org/foo"));
  193. expect((0, _util.createValidAbsoluteUrl)("/foo", "ftp://www.mozilla.org")).toEqual(new URL("ftp://www.mozilla.org/foo"));
  194. expect((0, _util.createValidAbsoluteUrl)("mailto:foo@bar.baz", null)).toEqual(new URL("mailto:foo@bar.baz"));
  195. expect((0, _util.createValidAbsoluteUrl)("/foo", "mailto:foo@bar.baz")).toEqual(null);
  196. expect((0, _util.createValidAbsoluteUrl)("tel:+0123456789", null)).toEqual(new URL("tel:+0123456789"));
  197. expect((0, _util.createValidAbsoluteUrl)("/foo", "tel:0123456789")).toEqual(null);
  198. });
  199. });
  200. describe("createPromiseCapability", function () {
  201. it("should resolve with correct data", function (done) {
  202. const promiseCapability = (0, _util.createPromiseCapability)();
  203. expect(promiseCapability.settled).toEqual(false);
  204. promiseCapability.resolve({
  205. test: "abc"
  206. });
  207. promiseCapability.promise.then(function (data) {
  208. expect(promiseCapability.settled).toEqual(true);
  209. expect(data).toEqual({
  210. test: "abc"
  211. });
  212. done();
  213. }, done.fail);
  214. });
  215. it("should reject with correct reason", function (done) {
  216. const promiseCapability = (0, _util.createPromiseCapability)();
  217. expect(promiseCapability.settled).toEqual(false);
  218. promiseCapability.reject(new Error("reason"));
  219. promiseCapability.promise.then(done.fail, function (reason) {
  220. expect(promiseCapability.settled).toEqual(true);
  221. expect(reason instanceof Error).toEqual(true);
  222. expect(reason.message).toEqual("reason");
  223. done();
  224. });
  225. });
  226. });
  227. describe("escapeString", function () {
  228. it("should escape (, ) and \\", function () {
  229. expect((0, _util.escapeString)("((a\\a))(b(b\\b)b)")).toEqual("\\(\\(a\\\\a\\)\\)\\(b\\(b\\\\b\\)b\\)");
  230. });
  231. });
  232. describe("getModificationDate", function () {
  233. it("should get a correctly formatted date", function () {
  234. const date = new Date(Date.UTC(3141, 5, 9, 2, 6, 53));
  235. expect((0, _util.getModificationDate)(date)).toEqual("31410610020653");
  236. });
  237. });
  238. });