nodeApi.mjs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  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 { toBase32, Dish } from "../../../src/node/index";
  17. import TestRegister from "../../TestRegister";
  18. TestRegister.addApiTests([
  19. it("should have some operations", () => {
  20. assert(chef);
  21. assert(chef.toBase32);
  22. assert(chef.setUnion);
  23. assert(!chef.randomFunction);
  24. }),
  25. it("should export other functions at top level", () => {
  26. assert(toBase32);
  27. }),
  28. it("should be synchronous", () => {
  29. try {
  30. const result = chef.toBase32("input");
  31. assert.notEqual("something", result);
  32. } catch (e) {
  33. // shouldnt reach here
  34. assert(false);
  35. }
  36. try {
  37. const fail = chef.setUnion("1");
  38. // shouldnt get here
  39. assert(!fail || false);
  40. } catch (e) {
  41. assert(true);
  42. }
  43. }),
  44. it("should not catch Errors", () => {
  45. try {
  46. chef.setUnion("1");
  47. assert(false);
  48. } catch (e) {
  49. assert(e instanceof OperationError);
  50. }
  51. }),
  52. it("should accept arguments in object format for operations", () => {
  53. const result = chef.setUnion("1 2 3 4:3 4 5 6", {
  54. itemDelimiter: " ",
  55. sampleDelimiter: ":"
  56. });
  57. assert.equal(result.value, "1 2 3 4 5 6");
  58. }),
  59. it("should accept just some of the optional arguments being overriden", () => {
  60. const result = chef.setIntersection("1 2 3 4 5\\n\\n3 4 5", {
  61. itemDelimiter: " ",
  62. });
  63. assert.equal(result.value, "3 4 5");
  64. }),
  65. it("should accept no override arguments and just use the default values", () => {
  66. const result = chef.powerSet("1,2,3");
  67. assert.equal(result.value, "\n3\n2\n1\n2,3\n1,3\n1,2\n1,2,3\n");
  68. }),
  69. it("should return an object with a .to method", () => {
  70. const result = chef.toBase32("input");
  71. assert(result.to);
  72. assert.equal(result.to("string"), "NFXHA5LU");
  73. }),
  74. it("should return an object with a .get method", () => {
  75. const result = chef.toBase32("input");
  76. assert(result.get);
  77. assert.equal(result.get("string"), "NFXHA5LU");
  78. }),
  79. it("should return a SyncDish", () => {
  80. const result = chef.toBase32("input");
  81. assert(result instanceof SyncDish);
  82. }),
  83. it("should coerce to a string as you expect", () => {
  84. const result = chef.fromBase32(chef.toBase32("something"));
  85. assert.equal(String(result), "something");
  86. // This kind of coercion uses toValue
  87. assert.equal(""+result, "NaN");
  88. }),
  89. it("should coerce to a number as you expect", () => {
  90. const result = chef.fromBase32(chef.toBase32("32"));
  91. assert.equal(3 + result, 35);
  92. }),
  93. it("chef.help: should exist", () => {
  94. assert(chef.help);
  95. }),
  96. it("chef.help: should describe a operation", () => {
  97. const result = chef.help("tripleDESDecrypt");
  98. assert.strictEqual(result[0].name, "Triple DES Decrypt");
  99. assert.strictEqual(result[0].module, "Ciphers");
  100. assert.strictEqual(result[0].inputType, "string");
  101. assert.strictEqual(result[0].outputType, "string");
  102. 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.");
  103. assert.strictEqual(result[0].args.length, 5);
  104. }),
  105. it("chef.help: null for invalid operation", () => {
  106. const result = chef.help("some invalid function name");
  107. assert.strictEqual(result, null);
  108. }),
  109. it("chef.help: takes a wrapped operation as input", () => {
  110. const result = chef.help(chef.toBase32);
  111. assert.strictEqual(result[0].name, "To Base32");
  112. assert.strictEqual(result[0].module, "Default");
  113. }),
  114. it("chef.help: returns multiple results", () => {
  115. const result = chef.help("base 64");
  116. assert.strictEqual(result.length, 8);
  117. }),
  118. it("chef.help: looks in description for matches too", () => {
  119. // string only in one operation's description.
  120. const result = chef.help("Converts a unit of data to another format.");
  121. assert.strictEqual(result.length, 1);
  122. assert.strictEqual(result[0].name, "Convert data units");
  123. }),
  124. it("chef.help: lists name matches before desc matches", () => {
  125. const result = chef.help("MD5");
  126. assert.ok(result[0].name.includes("MD5"));
  127. assert.strictEqual(result[1].name.includes("MD5"), false);
  128. assert.strictEqual(result[2].name.includes("MD5"), false);
  129. assert.ok(result[1].description.includes("MD5"));
  130. assert.ok(result[2].description.includes("MD5"));
  131. }),
  132. it("chef.bake: should exist", () => {
  133. assert(chef.bake);
  134. }),
  135. it("chef.bake: should return SyncDish", () => {
  136. const result = chef.bake("input", "to base 64");
  137. assert(result instanceof SyncDish);
  138. }),
  139. it("chef.bake: should take an input and an op name and perform it", () => {
  140. const result = chef.bake("some input", "to base 32");
  141. assert.strictEqual(result.toString(), "ONXW2ZJANFXHA5LU");
  142. }),
  143. it("chef.bake: should complain if recipe isnt a valid object", () => {
  144. try {
  145. chef.bake("some input", 3264);
  146. } catch (e) {
  147. assert.strictEqual(e.name, "TypeError");
  148. assert.strictEqual(e.message, "Recipe can only contain function names or functions");
  149. }
  150. }),
  151. it("chef.bake: Should complain if string op is invalid", () => {
  152. try {
  153. chef.bake("some input", "not a valid operation");
  154. assert.fail("Shouldn't be hit");
  155. } catch (e) {
  156. assert.strictEqual(e.name, "TypeError");
  157. assert.strictEqual(e.message, "Couldn't find an operation with name 'not a valid operation'.");
  158. }
  159. }),
  160. it("chef.bake: Should take an input and an operation and perform it", () => {
  161. const result = chef.bake("https://google.com/search?q=help", chef.parseURI);
  162. assert.strictEqual(result.toString(), "Protocol:\thttps:\nHostname:\tgoogle.com\nPath name:\t/search\nArguments:\n\tq = help\n");
  163. }),
  164. it("chef.bake: Should complain if an invalid operation is inputted", () => {
  165. try {
  166. chef.bake("https://google.com/search?q=help", () => {});
  167. assert.fail("Shouldn't be hit");
  168. } catch (e) {
  169. assert.strictEqual(e.name, "TypeError");
  170. assert.strictEqual(e.message, "Inputted function not a Chef operation.");
  171. }
  172. }),
  173. it("chef.bake: accepts an array of operation names and performs them all in order", () => {
  174. const result = chef.bake("https://google.com/search?q=that's a complicated question", ["URL encode", "URL decode", "Parse URI"]);
  175. assert.strictEqual(result.toString(), "Protocol:\thttps:\nHostname:\tgoogle.com\nPath name:\t/search\nArguments:\n\tq = that's a complicated question\n");
  176. }),
  177. it("chef.bake: forgiving with operation names", () =>{
  178. const result = chef.bake("https://google.com/search?q=that's a complicated question", ["urlencode", "url decode", "parseURI"]);
  179. assert.strictEqual(result.toString(), "Protocol:\thttps:\nHostname:\tgoogle.com\nPath name:\t/search\nArguments:\n\tq = that's a complicated question\n");
  180. }),
  181. it("chef.bake: forgiving with operation names", () =>{
  182. const result = chef.bake("hello", ["to base 64"]);
  183. assert.strictEqual(result.toString(), "aGVsbG8=");
  184. }),
  185. it("chef.bake: if recipe is empty array, return input as dish", () => {
  186. const result = chef.bake("some input", []);
  187. assert.strictEqual(result.toString(), "some input");
  188. assert(result instanceof SyncDish, "Result is not instance of SyncDish");
  189. }),
  190. it("chef.bake: accepts an array of operations as recipe", () => {
  191. const result = chef.bake("https://google.com/search?q=that's a complicated question", [chef.URLEncode, chef.URLDecode, chef.parseURI]);
  192. assert.strictEqual(result.toString(), "Protocol:\thttps:\nHostname:\tgoogle.com\nPath name:\t/search\nArguments:\n\tq = that's a complicated question\n");
  193. }),
  194. it("should complain if an invalid operation is inputted as part of array", () => {
  195. try {
  196. chef.bake("something", [() => {}]);
  197. } catch (e) {
  198. assert.strictEqual(e.name, "TypeError");
  199. assert.strictEqual(e.message, "Inputted function not a Chef operation.");
  200. }
  201. }),
  202. it("chef.bake: should take single JSON object describing op and args OBJ", () => {
  203. const result = chef.bake("some input", {
  204. op: chef.toHex,
  205. args: {
  206. Delimiter: "Colon"
  207. }
  208. });
  209. assert.strictEqual(result.toString(), "73:6f:6d:65:20:69:6e:70:75:74");
  210. }),
  211. it("chef.bake: should take single JSON object describing op and args ARRAY", () => {
  212. const result = chef.bake("some input", {
  213. op: chef.toHex,
  214. args: ["Colon"]
  215. });
  216. assert.strictEqual(result.toString(), "73:6f:6d:65:20:69:6e:70:75:74");
  217. }),
  218. it("chef.bake: should error if op in JSON is not chef op", () => {
  219. try {
  220. chef.bake("some input", {
  221. op: () => {},
  222. args: ["Colon"],
  223. });
  224. } catch (e) {
  225. assert.strictEqual(e.name, "TypeError");
  226. assert.strictEqual(e.message, "Inputted function not a Chef operation.");
  227. }
  228. }),
  229. it("chef.bake: should take multiple ops in JSON object form, some ops by string", () => {
  230. const result = chef.bake("some input", [
  231. {
  232. op: chef.toHex,
  233. args: ["Colon"]
  234. },
  235. {
  236. op: "to octal",
  237. args: {
  238. delimiter: "Semi-colon",
  239. }
  240. }
  241. ]);
  242. 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");
  243. }),
  244. it("chef.bake: should handle op with multiple args", () => {
  245. const result = chef.bake("some input", {
  246. op: "to morse code",
  247. args: {
  248. formatOptions: "Dash/Dot",
  249. wordDelimiter: "Comma",
  250. letterDelimiter: "Backslash",
  251. }
  252. });
  253. assert.strictEqual(result.toString(), "DotDotDot\\DashDashDash\\DashDash\\Dot,DotDot\\DashDot\\DotDashDashDot\\DotDotDash\\Dash");
  254. }),
  255. it("chef.bake: should take compact JSON format from Chef Website as recipe", () => {
  256. 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]}]);
  257. assert.strictEqual(result.toString(), "begin_something_anananaaaaak_da_aaak_da_aaaaananaaaaaaan_da_aaaaaaanan_da_aaak_end_something");
  258. }),
  259. it("chef.bake: should accept Clean JSON format from Chef website as recipe", () => {
  260. const result = chef.bake("some input", [
  261. { "op": "To Morse Code",
  262. "args": ["Dash/Dot", "Backslash", "Comma"] },
  263. { "op": "Hex to PEM",
  264. "args": ["SOMETHING"] },
  265. { "op": "To Snake case",
  266. "args": [false] }
  267. ]);
  268. assert.strictEqual(result.toString(), "begin_something_anananaaaaak_da_aaak_da_aaaaananaaaaaaan_da_aaaaaaanan_da_aaak_end_something");
  269. }),
  270. it("Composable Dish: Should have top level Dish object", () => {
  271. assert.ok(Dish);
  272. }),
  273. it("Composable Dish: Should construct empty dish object", () => {
  274. const dish = new Dish();
  275. assert.deepEqual(dish.value, []);
  276. assert.strictEqual(dish.type, 0);
  277. }),
  278. it("Composable Dish: constructed dish should have operation prototype functions", () => {
  279. const dish = new Dish();
  280. assert.ok(dish.translateDateTimeFormat);
  281. assert.ok(dish.stripHTTPHeaders);
  282. assert.throws(() => dish.someInvalidFunction());
  283. }),
  284. it("Composable Dish: composed function returns another dish", () => {
  285. const result = new Dish("some input").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("Excluded operations: throw a sensible error when you try and call one", () => {
  299. try {
  300. chef.fork();
  301. } catch (e) {
  302. assert.strictEqual(e.type, "ExcludedOperationError");
  303. assert.strictEqual(e.message, "Sorry, the Fork operation is not available in the Node.js version of CyberChef.");
  304. }
  305. }),
  306. it("Excluded operations: throw a sensible error when you try and call one", () => {
  307. try {
  308. chef.renderImage();
  309. } catch (e) {
  310. assert.strictEqual(e.type, "ExcludedOperationError");
  311. assert.strictEqual(e.message, "Sorry, the RenderImage operation is not available in the Node.js version of CyberChef.");
  312. }
  313. })
  314. ]);