nodeApi.mjs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. /* eslint no-console: 0 */
  2. /**
  3. * nodeApi.js
  4. *
  5. * Test node api utilities
  6. *
  7. * @author d98762625 [d98762625@gmail.com]
  8. * @copyright Crown Copyright 2018
  9. * @license Apache-2.0
  10. */
  11. import assert from "assert";
  12. import it from "../assertionHandler";
  13. import chef from "../../../src/node/index";
  14. import OperationError from "../../../src/core/errors/OperationError";
  15. import SyncDish from "../../../src/node/SyncDish";
  16. import fs from "fs";
  17. import { toBase32, Dish, SHA3 } from "../../../src/node/index";
  18. import TestRegister from "../../TestRegister";
  19. TestRegister.addApiTests([
  20. it("should have some operations", () => {
  21. assert(chef);
  22. assert(chef.toBase32);
  23. assert(chef.setUnion);
  24. assert(!chef.randomFunction);
  25. }),
  26. it("should export other functions at top level", () => {
  27. assert(toBase32);
  28. }),
  29. it("should be synchronous", () => {
  30. try {
  31. const result = chef.toBase32("input");
  32. assert.notEqual("something", result);
  33. } catch (e) {
  34. // shouldnt reach here
  35. assert(false);
  36. }
  37. try {
  38. const fail = chef.setUnion("1");
  39. // shouldnt get here
  40. assert(!fail || false);
  41. } catch (e) {
  42. assert(true);
  43. }
  44. }),
  45. it("should not catch Errors", () => {
  46. try {
  47. chef.setUnion("1");
  48. assert(false);
  49. } catch (e) {
  50. assert(e instanceof OperationError);
  51. }
  52. }),
  53. it("should accept arguments in object format for operations", () => {
  54. const result = chef.setUnion("1 2 3 4:3 4 5 6", {
  55. itemDelimiter: " ",
  56. sampleDelimiter: ":"
  57. });
  58. assert.equal(result.value, "1 2 3 4 5 6");
  59. }),
  60. it("should accept just some of the optional arguments being overriden", () => {
  61. const result = chef.setIntersection("1 2 3 4 5\\n\\n3 4 5", {
  62. itemDelimiter: " ",
  63. });
  64. assert.equal(result.value, "3 4 5");
  65. }),
  66. it("should accept no override arguments and just use the default values", () => {
  67. const result = chef.powerSet("1,2,3");
  68. assert.equal(result.value, "\n3\n2\n1\n2,3\n1,3\n1,2\n1,2,3\n");
  69. }),
  70. it("should return an object with a .to method", () => {
  71. const result = chef.toBase32("input");
  72. assert(result.to);
  73. assert.equal(result.to("string"), "NFXHA5LU");
  74. }),
  75. it("should return an object with a .get method", () => {
  76. const result = chef.toBase32("input");
  77. assert(result.get);
  78. assert.equal(result.get("string"), "NFXHA5LU");
  79. }),
  80. it("should return a SyncDish", () => {
  81. const result = chef.toBase32("input");
  82. assert(result instanceof SyncDish);
  83. }),
  84. it("should coerce to a string as you expect", () => {
  85. const result = chef.fromBase32(chef.toBase32("something"));
  86. assert.equal(String(result), "something");
  87. // This kind of coercion uses toValue
  88. assert.equal(""+result, "NaN");
  89. }),
  90. it("should coerce to a number as you expect", () => {
  91. const result = chef.fromBase32(chef.toBase32("32"));
  92. assert.equal(3 + result, 35);
  93. }),
  94. it("chef.help: should exist", () => {
  95. assert(chef.help);
  96. }),
  97. it("chef.help: should describe a operation", () => {
  98. const result = chef.help("tripleDESDecrypt");
  99. assert.strictEqual(result[0].name, "Triple DES Decrypt");
  100. assert.strictEqual(result[0].module, "Ciphers");
  101. assert.strictEqual(result[0].inputType, "string");
  102. assert.strictEqual(result[0].outputType, "string");
  103. assert.strictEqual(result[0].description, "Triple DES applies DES three times to each block to increase key size.<br><br><b>Key:</b> Triple DES uses a key length of 24 bytes (192 bits).<br>DES uses a key length of 8 bytes (64 bits).<br><br><b>IV:</b> The Initialization Vector should be 8 bytes long. If not entered, it will default to 8 null bytes.<br><br><b>Padding:</b> In CBC and ECB mode, PKCS#7 padding will be used.");
  104. assert.strictEqual(result[0].args.length, 5);
  105. }),
  106. it("chef.help: null for invalid operation", () => {
  107. const result = chef.help("some invalid function name");
  108. assert.strictEqual(result, null);
  109. }),
  110. it("chef.help: takes a wrapped operation as input", () => {
  111. const result = chef.help(chef.toBase32);
  112. assert.strictEqual(result[0].name, "To Base32");
  113. assert.strictEqual(result[0].module, "Default");
  114. }),
  115. it("chef.help: returns multiple results", () => {
  116. const result = chef.help("base 64");
  117. assert.strictEqual(result.length, 10);
  118. }),
  119. it("chef.help: looks in description for matches too", () => {
  120. // string only in one operation's description.
  121. const result = chef.help("Converts a unit of data to another format.");
  122. assert.strictEqual(result.length, 1);
  123. assert.strictEqual(result[0].name, "Convert data units");
  124. }),
  125. it("chef.help: lists name matches before desc matches", () => {
  126. const result = chef.help("MD5");
  127. assert.ok(result[0].name.includes("MD5"));
  128. assert.strictEqual(result[1].name.includes("MD5"), false);
  129. assert.strictEqual(result[2].name.includes("MD5"), false);
  130. assert.ok(result[1].description.includes("MD5"));
  131. assert.ok(result[2].description.includes("MD5"));
  132. }),
  133. it("chef.bake: should exist", () => {
  134. assert(chef.bake);
  135. }),
  136. it("chef.bake: should return SyncDish", () => {
  137. const result = chef.bake("input", "to base 64");
  138. assert(result instanceof SyncDish);
  139. }),
  140. it("chef.bake: should take an input and an op name and perform it", () => {
  141. const result = chef.bake("some input", "to base 32");
  142. assert.strictEqual(result.toString(), "ONXW2ZJANFXHA5LU");
  143. }),
  144. it("chef.bake: should complain if recipe isnt a valid object", () => {
  145. try {
  146. chef.bake("some input", 3264);
  147. } catch (e) {
  148. assert.strictEqual(e.name, "TypeError");
  149. assert.strictEqual(e.message, "Recipe can only contain function names or functions");
  150. }
  151. }),
  152. it("chef.bake: Should complain if string op is invalid", () => {
  153. try {
  154. chef.bake("some input", "not a valid operation");
  155. assert.fail("Shouldn't be hit");
  156. } catch (e) {
  157. assert.strictEqual(e.name, "TypeError");
  158. assert.strictEqual(e.message, "Couldn't find an operation with name 'not a valid operation'.");
  159. }
  160. }),
  161. it("chef.bake: Should take an input and an operation and perform it", () => {
  162. const result = chef.bake("https://google.com/search?q=help", chef.parseURI);
  163. assert.strictEqual(result.toString(), "Protocol:\thttps:\nHostname:\tgoogle.com\nPath name:\t/search\nArguments:\n\tq = help\n");
  164. }),
  165. it("chef.bake: Should complain if an invalid operation is inputted", () => {
  166. try {
  167. chef.bake("https://google.com/search?q=help", () => {});
  168. assert.fail("Shouldn't be hit");
  169. } catch (e) {
  170. assert.strictEqual(e.name, "TypeError");
  171. assert.strictEqual(e.message, "Inputted function not a Chef operation.");
  172. }
  173. }),
  174. it("chef.bake: accepts an array of operation names and performs them all in order", () => {
  175. const result = chef.bake("https://google.com/search?q=that's a complicated question", ["URL encode", "URL decode", "Parse URI"]);
  176. assert.strictEqual(result.toString(), "Protocol:\thttps:\nHostname:\tgoogle.com\nPath name:\t/search\nArguments:\n\tq = that's a complicated question\n");
  177. }),
  178. it("chef.bake: forgiving with operation names", () =>{
  179. const result = chef.bake("https://google.com/search?q=that's a complicated question", ["urlencode", "url decode", "parseURI"]);
  180. assert.strictEqual(result.toString(), "Protocol:\thttps:\nHostname:\tgoogle.com\nPath name:\t/search\nArguments:\n\tq = that's a complicated question\n");
  181. }),
  182. it("chef.bake: forgiving with operation names", () =>{
  183. const result = chef.bake("hello", ["to base 64"]);
  184. assert.strictEqual(result.toString(), "aGVsbG8=");
  185. }),
  186. it("chef.bake: if recipe is empty array, return input as dish", () => {
  187. const result = chef.bake("some input", []);
  188. assert.strictEqual(result.toString(), "some input");
  189. assert(result instanceof SyncDish, "Result is not instance of SyncDish");
  190. }),
  191. it("chef.bake: accepts an array of operations as recipe", () => {
  192. const result = chef.bake("https://google.com/search?q=that's a complicated question", [chef.URLEncode, chef.URLDecode, chef.parseURI]);
  193. assert.strictEqual(result.toString(), "Protocol:\thttps:\nHostname:\tgoogle.com\nPath name:\t/search\nArguments:\n\tq = that's a complicated question\n");
  194. }),
  195. it("should complain if an invalid operation is inputted as part of array", () => {
  196. try {
  197. chef.bake("something", [() => {}]);
  198. } catch (e) {
  199. assert.strictEqual(e.name, "TypeError");
  200. assert.strictEqual(e.message, "Inputted function not a Chef operation.");
  201. }
  202. }),
  203. it("chef.bake: should take single JSON object describing op and args OBJ", () => {
  204. const result = chef.bake("some input", {
  205. op: chef.toHex,
  206. args: {
  207. Delimiter: "Colon"
  208. }
  209. });
  210. assert.strictEqual(result.toString(), "73:6f:6d:65:20:69:6e:70:75:74");
  211. }),
  212. it("chef.bake: should take single JSON object describing op and args ARRAY", () => {
  213. const result = chef.bake("some input", {
  214. op: chef.toHex,
  215. args: ["Colon"]
  216. });
  217. assert.strictEqual(result.toString(), "73:6f:6d:65:20:69:6e:70:75:74");
  218. }),
  219. it("chef.bake: should error if op in JSON is not chef op", () => {
  220. try {
  221. chef.bake("some input", {
  222. op: () => {},
  223. args: ["Colon"],
  224. });
  225. } catch (e) {
  226. assert.strictEqual(e.name, "TypeError");
  227. assert.strictEqual(e.message, "Inputted function not a Chef operation.");
  228. }
  229. }),
  230. it("chef.bake: should take multiple ops in JSON object form, some ops by string", () => {
  231. const result = chef.bake("some input", [
  232. {
  233. op: chef.toHex,
  234. args: ["Colon"]
  235. },
  236. {
  237. op: "to octal",
  238. args: {
  239. delimiter: "Semi-colon",
  240. }
  241. }
  242. ]);
  243. assert.strictEqual(result.toString(), "67;63;72;66;146;72;66;144;72;66;65;72;62;60;72;66;71;72;66;145;72;67;60;72;67;65;72;67;64");
  244. }),
  245. it("chef.bake: should handle op with multiple args", () => {
  246. const result = chef.bake("some input", {
  247. op: "to morse code",
  248. args: {
  249. formatOptions: "Dash/Dot",
  250. wordDelimiter: "Comma",
  251. letterDelimiter: "Backslash",
  252. }
  253. });
  254. assert.strictEqual(result.toString(), "DotDotDot\\DashDashDash\\DashDash\\Dot,DotDot\\DashDot\\DotDashDashDot\\DotDotDash\\Dash");
  255. }),
  256. it("chef.bake: should take compact JSON format from Chef Website as recipe", () => {
  257. const result = chef.bake("some input", [{"op": "To Morse Code", "args": ["Dash/Dot", "Backslash", "Comma"]}, {"op": "Hex to PEM", "args": ["SOMETHING"]}, {"op": "To Snake case", "args": [false]}]);
  258. assert.strictEqual(result.toString(), "begin_something_anananaaaaak_da_aaak_da_aaaaananaaaaaaan_da_aaaaaaanan_da_aaak_end_something");
  259. }),
  260. it("chef.bake: should accept Clean JSON format from Chef website as recipe", () => {
  261. const result = chef.bake("some input", [
  262. { "op": "To Morse Code",
  263. "args": ["Dash/Dot", "Backslash", "Comma"] },
  264. { "op": "Hex to PEM",
  265. "args": ["SOMETHING"] },
  266. { "op": "To Snake case",
  267. "args": [false] }
  268. ]);
  269. assert.strictEqual(result.toString(), "begin_something_anananaaaaak_da_aaak_da_aaaaananaaaaaaan_da_aaaaaaanan_da_aaak_end_something");
  270. }),
  271. it("Composable Dish: Should have top level Dish object", () => {
  272. assert.ok(Dish);
  273. }),
  274. it("Composable Dish: Should construct empty dish object", () => {
  275. const dish = new Dish();
  276. assert.deepEqual(dish.value, []);
  277. assert.strictEqual(dish.type, 0);
  278. }),
  279. it("Composable Dish: constructed dish should have apply prototype functions", () => {
  280. const dish = new Dish();
  281. assert.ok(dish.apply);
  282. assert.throws(() => dish.someInvalidFunction());
  283. }),
  284. it("Composable Dish: composed function returns another dish", () => {
  285. const result = new Dish("some input").apply(toBase32);
  286. assert.ok(result instanceof SyncDish);
  287. }),
  288. it("Composable dish: infers type from input if needed", () => {
  289. const dish = new Dish("string input");
  290. assert.strictEqual(dish.type, 1);
  291. const numberDish = new Dish(333);
  292. assert.strictEqual(numberDish.type, 2);
  293. const arrayBufferDish = new Dish(Buffer.from("some buffer input").buffer);
  294. assert.strictEqual(arrayBufferDish.type, 4);
  295. const JSONDish = new Dish({key: "value"});
  296. assert.strictEqual(JSONDish.type, 6);
  297. }),
  298. it("Composable dish: Buffer type dishes should be converted to strings", () => {
  299. fs.writeFileSync("test.txt", "abc");
  300. const dish = new Dish(fs.readFileSync("test.txt"));
  301. assert.strictEqual(dish.type, 4);
  302. fs.unlinkSync("test.txt");
  303. }),
  304. it("Composable Dish: apply should allow set of arguments for operation", () => {
  305. const result = new Dish("input").apply(SHA3, {size: "256"});
  306. assert.strictEqual(result.toString(), "7640cc9b7e3662b2250a43d1757e318bb29fb4860276ac4373b67b1650d6d3e3");
  307. }),
  308. it("Composable Dish: apply functions can be chained", () => {
  309. const result = new Dish("input").apply(toBase32).apply(SHA3, {size: "224"});
  310. assert.strictEqual(result.toString(), "493e8136b759370a415ef2cf2f7a69690441ff86592aba082bc2e2e0");
  311. }),
  312. it("Excluded operations: throw a sensible error when you try and call one", () => {
  313. try {
  314. chef.fork();
  315. } catch (e) {
  316. assert.strictEqual(e.type, "ExcludedOperationError");
  317. assert.strictEqual(e.message, "Sorry, the Fork operation is not available in the Node.js version of CyberChef.");
  318. }
  319. }),
  320. it("Excluded operations: throw a sensible error when you try and call one", () => {
  321. try {
  322. chef.renderImage();
  323. } catch (e) {
  324. assert.strictEqual(e.type, "ExcludedOperationError");
  325. assert.strictEqual(e.message, "Sorry, the RenderImage operation is not available in the Node.js version of CyberChef.");
  326. }
  327. }),
  328. it("Operation arguments: should be accessible from operation object if op has array arg", () => {
  329. assert.ok(chef.toCharcode.argOptions);
  330. assert.equal(chef.unzip.argOptions, undefined);
  331. }),
  332. it("Operation arguments: should have key for each array-based argument in operation", () => {
  333. assert.ok(chef.convertDistance.argOptions.inputUnits);
  334. assert.ok(chef.convertDistance.argOptions.outputUnits);
  335. assert.ok(chef.bitShiftRight.argOptions.type);
  336. // is a number type, so not included.
  337. assert.equal(chef.bitShiftRight.argOptions.amount, undefined);
  338. }),
  339. it("Operation arguments: should list all options excluding subheadings", () => {
  340. // First element (subheading) removed
  341. assert.equal(chef.convertDistance.argOptions.inputUnits[0], "Nanometres (nm)");
  342. assert.equal(chef.defangURL.argOptions.process[1], "Only full URLs");
  343. })
  344. ]);