evaluator_spec.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  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 { createIdFactory, XRefMock } from "./test_utils.js";
  16. import { Dict, Name } from "../../src/core/primitives.js";
  17. import { FormatError, OPS } from "../../src/shared/util.js";
  18. import { Stream, StringStream } from "../../src/core/stream.js";
  19. import { OperatorList } from "../../src/core/operator_list.js";
  20. import { PartialEvaluator } from "../../src/core/evaluator.js";
  21. import { WorkerTask } from "../../src/core/worker.js";
  22. describe("evaluator", function () {
  23. function HandlerMock() {
  24. this.inputs = [];
  25. }
  26. HandlerMock.prototype = {
  27. send(name, data) {
  28. this.inputs.push({ name, data });
  29. },
  30. };
  31. function ResourcesMock() {}
  32. ResourcesMock.prototype = {
  33. get(name) {
  34. return this[name];
  35. },
  36. };
  37. async function runOperatorListCheck(evaluator, stream, resources) {
  38. const operatorList = new OperatorList();
  39. const task = new WorkerTask("OperatorListCheck");
  40. await evaluator.getOperatorList({
  41. stream,
  42. task,
  43. resources,
  44. operatorList,
  45. });
  46. return operatorList;
  47. }
  48. let partialEvaluator;
  49. beforeAll(function () {
  50. partialEvaluator = new PartialEvaluator({
  51. xref: new XRefMock(),
  52. handler: new HandlerMock(),
  53. pageIndex: 0,
  54. idFactory: createIdFactory(/* pageIndex = */ 0),
  55. });
  56. });
  57. afterAll(function () {
  58. partialEvaluator = null;
  59. });
  60. describe("splitCombinedOperations", function () {
  61. it("should reject unknown operations", async function () {
  62. const stream = new StringStream("fTT");
  63. const result = await runOperatorListCheck(
  64. partialEvaluator,
  65. stream,
  66. new ResourcesMock()
  67. );
  68. expect(!!result.fnArray && !!result.argsArray).toEqual(true);
  69. expect(result.fnArray.length).toEqual(1);
  70. expect(result.fnArray[0]).toEqual(OPS.fill);
  71. expect(result.argsArray[0]).toEqual(null);
  72. });
  73. it("should handle one operation", async function () {
  74. const stream = new StringStream("Q");
  75. const result = await runOperatorListCheck(
  76. partialEvaluator,
  77. stream,
  78. new ResourcesMock()
  79. );
  80. expect(!!result.fnArray && !!result.argsArray).toEqual(true);
  81. expect(result.fnArray.length).toEqual(1);
  82. expect(result.fnArray[0]).toEqual(OPS.restore);
  83. });
  84. it("should handle two glued operations", async function () {
  85. const imgDict = new Dict();
  86. imgDict.set("Subtype", Name.get("Image"));
  87. imgDict.set("Width", 1);
  88. imgDict.set("Height", 1);
  89. const imgStream = new Stream([0]);
  90. imgStream.dict = imgDict;
  91. const xObject = new Dict();
  92. xObject.set("Res1", imgStream);
  93. const resources = new ResourcesMock();
  94. resources.XObject = xObject;
  95. const stream = new StringStream("/Res1 DoQ");
  96. const result = await runOperatorListCheck(
  97. partialEvaluator,
  98. stream,
  99. resources
  100. );
  101. expect(result.fnArray.length).toEqual(3);
  102. expect(result.fnArray[0]).toEqual(OPS.dependency);
  103. expect(result.fnArray[1]).toEqual(OPS.paintImageXObject);
  104. expect(result.fnArray[2]).toEqual(OPS.restore);
  105. expect(result.argsArray.length).toEqual(3);
  106. expect(result.argsArray[0]).toEqual(["img_p0_1"]);
  107. expect(result.argsArray[1]).toEqual(["img_p0_1", 1, 1]);
  108. expect(result.argsArray[2]).toEqual(null);
  109. });
  110. it("should handle three glued operations", async function () {
  111. const stream = new StringStream("fff");
  112. const result = await runOperatorListCheck(
  113. partialEvaluator,
  114. stream,
  115. new ResourcesMock()
  116. );
  117. expect(!!result.fnArray && !!result.argsArray).toEqual(true);
  118. expect(result.fnArray.length).toEqual(3);
  119. expect(result.fnArray[0]).toEqual(OPS.fill);
  120. expect(result.fnArray[1]).toEqual(OPS.fill);
  121. expect(result.fnArray[2]).toEqual(OPS.fill);
  122. });
  123. it("should handle three glued operations #2", async function () {
  124. const resources = new ResourcesMock();
  125. resources.Res1 = {};
  126. const stream = new StringStream("B*Bf*");
  127. const result = await runOperatorListCheck(
  128. partialEvaluator,
  129. stream,
  130. resources
  131. );
  132. expect(!!result.fnArray && !!result.argsArray).toEqual(true);
  133. expect(result.fnArray.length).toEqual(3);
  134. expect(result.fnArray[0]).toEqual(OPS.eoFillStroke);
  135. expect(result.fnArray[1]).toEqual(OPS.fillStroke);
  136. expect(result.fnArray[2]).toEqual(OPS.eoFill);
  137. });
  138. it("should handle glued operations and operands", async function () {
  139. const stream = new StringStream("f5 Ts");
  140. const result = await runOperatorListCheck(
  141. partialEvaluator,
  142. stream,
  143. new ResourcesMock()
  144. );
  145. expect(!!result.fnArray && !!result.argsArray).toEqual(true);
  146. expect(result.fnArray.length).toEqual(2);
  147. expect(result.fnArray[0]).toEqual(OPS.fill);
  148. expect(result.fnArray[1]).toEqual(OPS.setTextRise);
  149. expect(result.argsArray.length).toEqual(2);
  150. expect(result.argsArray[1].length).toEqual(1);
  151. expect(result.argsArray[1][0]).toEqual(5);
  152. });
  153. it("should handle glued operations and literals", async function () {
  154. const stream = new StringStream("trueifalserinulln");
  155. const result = await runOperatorListCheck(
  156. partialEvaluator,
  157. stream,
  158. new ResourcesMock()
  159. );
  160. expect(!!result.fnArray && !!result.argsArray).toEqual(true);
  161. expect(result.fnArray.length).toEqual(3);
  162. expect(result.fnArray[0]).toEqual(OPS.setFlatness);
  163. expect(result.fnArray[1]).toEqual(OPS.setRenderingIntent);
  164. expect(result.fnArray[2]).toEqual(OPS.endPath);
  165. expect(result.argsArray.length).toEqual(3);
  166. expect(result.argsArray[0].length).toEqual(1);
  167. expect(result.argsArray[0][0]).toEqual(true);
  168. expect(result.argsArray[1].length).toEqual(1);
  169. expect(result.argsArray[1][0]).toEqual(false);
  170. expect(result.argsArray[2]).toEqual(null);
  171. });
  172. });
  173. describe("validateNumberOfArgs", function () {
  174. it("should execute if correct number of arguments", async function () {
  175. const stream = new StringStream("5 1 d0");
  176. const result = await runOperatorListCheck(
  177. partialEvaluator,
  178. stream,
  179. new ResourcesMock()
  180. );
  181. expect(result.argsArray[0][0]).toEqual(5);
  182. expect(result.argsArray[0][1]).toEqual(1);
  183. expect(result.fnArray[0]).toEqual(OPS.setCharWidth);
  184. });
  185. it("should execute if too many arguments", async function () {
  186. const stream = new StringStream("5 1 4 d0");
  187. const result = await runOperatorListCheck(
  188. partialEvaluator,
  189. stream,
  190. new ResourcesMock()
  191. );
  192. expect(result.argsArray[0][0]).toEqual(1);
  193. expect(result.argsArray[0][1]).toEqual(4);
  194. expect(result.fnArray[0]).toEqual(OPS.setCharWidth);
  195. });
  196. it("should execute if nested commands", async function () {
  197. const gState = new Dict();
  198. gState.set("LW", 2);
  199. gState.set("CA", 0.5);
  200. const extGState = new Dict();
  201. extGState.set("GS2", gState);
  202. const resources = new ResourcesMock();
  203. resources.ExtGState = extGState;
  204. const stream = new StringStream("/F2 /GS2 gs 5.711 Tf");
  205. const result = await runOperatorListCheck(
  206. partialEvaluator,
  207. stream,
  208. resources
  209. );
  210. expect(result.fnArray.length).toEqual(3);
  211. expect(result.fnArray[0]).toEqual(OPS.setGState);
  212. expect(result.fnArray[1]).toEqual(OPS.dependency);
  213. expect(result.fnArray[2]).toEqual(OPS.setFont);
  214. expect(result.argsArray.length).toEqual(3);
  215. expect(result.argsArray[0]).toEqual([
  216. [
  217. ["LW", 2],
  218. ["CA", 0.5],
  219. ],
  220. ]);
  221. expect(result.argsArray[1]).toEqual(["g_font_error"]);
  222. expect(result.argsArray[2]).toEqual(["g_font_error", 5.711]);
  223. });
  224. it("should skip if too few arguments", async function () {
  225. const stream = new StringStream("5 d0");
  226. const result = await runOperatorListCheck(
  227. partialEvaluator,
  228. stream,
  229. new ResourcesMock()
  230. );
  231. expect(result.argsArray).toEqual([]);
  232. expect(result.fnArray).toEqual([]);
  233. });
  234. it(
  235. "should error if (many) path operators have too few arguments " +
  236. "(bug 1443140)",
  237. async function () {
  238. const NUM_INVALID_OPS = 25;
  239. // Non-path operators, should be ignored.
  240. const invalidMoveText = "10 Td\n".repeat(NUM_INVALID_OPS);
  241. const moveTextStream = new StringStream(invalidMoveText);
  242. const result = await runOperatorListCheck(
  243. partialEvaluator,
  244. moveTextStream,
  245. new ResourcesMock()
  246. );
  247. expect(result.argsArray).toEqual([]);
  248. expect(result.fnArray).toEqual([]);
  249. // Path operators, should throw error.
  250. const invalidLineTo = "20 l\n".repeat(NUM_INVALID_OPS);
  251. const lineToStream = new StringStream(invalidLineTo);
  252. try {
  253. await runOperatorListCheck(
  254. partialEvaluator,
  255. lineToStream,
  256. new ResourcesMock()
  257. );
  258. // Shouldn't get here.
  259. expect(false).toEqual(true);
  260. } catch (reason) {
  261. expect(reason instanceof FormatError).toEqual(true);
  262. expect(reason.message).toEqual(
  263. "Invalid command l: expected 2 args, but received 1 args."
  264. );
  265. }
  266. }
  267. );
  268. it("should close opened saves", async function () {
  269. const stream = new StringStream("qq");
  270. const result = await runOperatorListCheck(
  271. partialEvaluator,
  272. stream,
  273. new ResourcesMock()
  274. );
  275. expect(!!result.fnArray && !!result.argsArray).toEqual(true);
  276. expect(result.fnArray.length).toEqual(4);
  277. expect(result.fnArray[0]).toEqual(OPS.save);
  278. expect(result.fnArray[1]).toEqual(OPS.save);
  279. expect(result.fnArray[2]).toEqual(OPS.restore);
  280. expect(result.fnArray[3]).toEqual(OPS.restore);
  281. });
  282. it("should error on paintXObject if name is missing", async function () {
  283. const stream = new StringStream("/ Do");
  284. try {
  285. await runOperatorListCheck(
  286. partialEvaluator,
  287. stream,
  288. new ResourcesMock()
  289. );
  290. // Shouldn't get here.
  291. expect(false).toEqual(true);
  292. } catch (reason) {
  293. expect(reason instanceof FormatError).toEqual(true);
  294. expect(reason.message).toEqual("XObject should be a stream");
  295. }
  296. });
  297. it("should skip paintXObject if subtype is PS", async function () {
  298. const xobjStreamDict = new Dict();
  299. xobjStreamDict.set("Subtype", Name.get("PS"));
  300. const xobjStream = new Stream([], 0, 0, xobjStreamDict);
  301. const xobjs = new Dict();
  302. xobjs.set("Res1", xobjStream);
  303. const resources = new Dict();
  304. resources.set("XObject", xobjs);
  305. const stream = new StringStream("/Res1 Do");
  306. const result = await runOperatorListCheck(
  307. partialEvaluator,
  308. stream,
  309. resources
  310. );
  311. expect(result.argsArray).toEqual([]);
  312. expect(result.fnArray).toEqual([]);
  313. });
  314. });
  315. describe("thread control", function () {
  316. it("should abort operator list parsing", async function () {
  317. const stream = new StringStream("qqQQ");
  318. const resources = new ResourcesMock();
  319. const result = new OperatorList();
  320. const task = new WorkerTask("OperatorListAbort");
  321. task.terminate();
  322. try {
  323. await partialEvaluator.getOperatorList({
  324. stream,
  325. task,
  326. resources,
  327. operatorList: result,
  328. });
  329. // Shouldn't get here.
  330. expect(false).toEqual(true);
  331. } catch (_) {
  332. expect(!!result.fnArray && !!result.argsArray).toEqual(true);
  333. expect(result.fnArray.length).toEqual(0);
  334. }
  335. });
  336. it("should abort text content parsing", async function () {
  337. const resources = new ResourcesMock();
  338. const stream = new StringStream("qqQQ");
  339. const task = new WorkerTask("TextContentAbort");
  340. task.terminate();
  341. try {
  342. await partialEvaluator.getTextContent({
  343. stream,
  344. task,
  345. resources,
  346. });
  347. // Shouldn't get here.
  348. expect(false).toEqual(true);
  349. } catch (_) {
  350. expect(true).toEqual(true);
  351. }
  352. });
  353. });
  354. describe("operator list", function () {
  355. class StreamSinkMock {
  356. enqueue() {}
  357. }
  358. it("should get correct total length after flushing", function () {
  359. const operatorList = new OperatorList(null, new StreamSinkMock());
  360. operatorList.addOp(OPS.save, null);
  361. operatorList.addOp(OPS.restore, null);
  362. expect(operatorList.totalLength).toEqual(2);
  363. expect(operatorList.length).toEqual(2);
  364. operatorList.flush();
  365. expect(operatorList.totalLength).toEqual(2);
  366. expect(operatorList.length).toEqual(0);
  367. });
  368. });
  369. });