Browse Source

WIP: start testing ops with various arg types. AESENCRYPT result encoding error

d98762625 7 years ago
parent
commit
a5d58071cd
4 changed files with 120 additions and 5 deletions
  1. 19 4
      src/node/apiUtils.mjs
  2. 1 0
      test/index.mjs
  3. 17 1
      test/tests/assertionHandler.mjs
  4. 83 0
      test/tests/nodeApi/ops.mjs

+ 19 - 4
src/node/apiUtils.mjs

@@ -14,7 +14,15 @@ import SyncDish from "./SyncDish";
  */
 function extractArg(arg) {
     if (arg.type === "option" || arg.type === "editableOption") {
-        return arg.value[0];
+        // pick default option if not already chosen
+        return typeof arg.value === "string" ? arg.value : arg.value[0];
+    }
+
+    if (arg.type === "toggleString") {
+        // ensure string and option exist when user hasn't defined
+        arg.string = arg.string || "";
+        arg.option = arg.option || arg.toggleValues[0];
+        return arg;
     }
 
     return arg.value;
@@ -24,8 +32,8 @@ function extractArg(arg) {
  * transformArgs
  *
  * Take the default args array and update with any user-defined
- * operation arguments. Allows user to define argyments in object style,
- * with accommodation name matching. Using named args in the API is more
+ * operation arguments. Allows user to define arguments in object style,
+ * with accommodating name matching. Using named args in the API is more
  * clear to the user.
  *
  * Argument name matching is case and space insensitive
@@ -42,8 +50,15 @@ function transformArgs(originalArgs, newArgs) {
                 return arg.name.toLowerCase().replace(/ /g, "") ===
                     key.toLowerCase().replace(/ /g, "");
             });
+
             if (index > -1) {
-                allArgs[index].value = newArgs[key];
+                const argument = allArgs[index];
+                if (["toggleString"].indexOf(argument.type) > -1) {
+                    argument.string = newArgs[key].string;
+                    argument.option = newArgs[key].option;
+                } else {
+                    argument.value = newArgs[key];
+                }
             }
         });
     }

+ 1 - 0
test/index.mjs

@@ -63,6 +63,7 @@ import "./tests/operations/SetUnion";
 import "./tests/operations/SymmetricDifference";
 
 import "./tests/nodeApi/nodeApi";
+import "./tests/nodeApi/ops";
 
 let allTestsPassing = true;
 const testStatusCounts = {

+ 17 - 1
test/tests/assertionHandler.mjs

@@ -9,6 +9,22 @@
  * @license Apache-2.0
  */
 
+/* eslint no-console: 0 */
+
+
+/**
+ * 
+ */
+const wrapRun = (run) => () => {
+    try {
+        run();
+    } catch (e) {
+        console.dir(e);
+        throw e;
+    }
+};
+
+
 /**
  * it - wrapper for assertions to provide a helpful description
  * to the TestRegister
@@ -37,7 +53,7 @@
 export function it(name, run) {
     return {
         name,
-        run
+        run: wrapRun(run),
     };
 }
 

+ 83 - 0
test/tests/nodeApi/ops.mjs

@@ -0,0 +1,83 @@
+/* eslint no-console: 0 */
+
+/**
+ * nodeApi.js
+ *
+ * Test node api operations
+ *
+ * @author d98762625 [d98762625@gmail.com]
+ * @copyright Crown Copyright 2018
+ * @license Apache-2.0
+ */
+
+import assert from "assert";
+import it from "../assertionHandler";
+import Utils from "../../../src/core/Utils";
+// import chef from "../../../src/node/index";
+// import OperationError from "../../../src/core/errors/OperationError";
+// import SyncDish from "../../../src/node/SyncDish";
+
+import {
+    ADD,
+    addLineNumbers,
+    adler32Checksum,
+    AESDecrypt,
+    AESEncrypt,
+} from "../../../src/node/index";
+import TestRegister from "../../TestRegister";
+
+TestRegister.addApiTests([
+
+    it("ADD: toggleString argument", () => {
+        const result = ADD("sample input", {
+            key: {
+                string: "some key",
+                option: "Hex"
+            }
+        });
+        assert.equal(result.toString(), "aO[^ZS\u000eW\\^cb");
+    }),
+
+    it("addLineNumbers: No arguments", () => {
+        const result = addLineNumbers("sample input");
+        assert.equal(result.toString(), "1 sample input");
+    }),
+
+    it("adler32Checksum: No args", () => {
+        const result = adler32Checksum("sample input");
+        assert.equal(result.toString(), "1f2304d3");
+    }),
+
+    it("AES decrypt: toggleString and option", () => {
+        const result = AESDecrypt("812c34ae6af353244a63c6ce23b7c34286b60be28ea4645523d4494700e7", {
+            key: {
+                string: "some longer key1",
+                option: "utf8",
+            },
+            iv: {
+                string: "some iv",
+                option: "utf8",
+            },
+            mode: "OFB",
+        });
+        assert.equal(result.toString(), "a slightly longer sampleinput?");
+    }),
+
+    it("AES encrypt: toggleString and options", () => {
+        const result = AESEncrypt("something", {
+            key: {
+                string: "a key that is long enuff",
+                option: "utf8",
+            },
+            iv: {
+                string: "another iv",
+                option: "utf8",
+            },
+            mode: "ECB",
+            output: "Raw",
+        });
+
+        assert.equal(result.toString(), "Ä)\u0005D”Sa;”F£nÐ");
+    })
+]);
+