util_spec.js 10 KB

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