]> git.immae.eu Git - perso/Immae/Projets/Cryptomonnaies/BIP39.git/blobdiff - tests.js
Test derivation path not starting with m
[perso/Immae/Projets/Cryptomonnaies/BIP39.git] / tests.js
index 7c67b02036e69a5c0089f1733403e531d0e9549b..49a95b7e3e48e96bda6fdf295c4e193d8754d740 100644 (file)
--- a/tests.js
+++ b/tests.js
@@ -44,6 +44,37 @@ function waitForGenerate(fn, maxTime) {
     wait();
 }
 
+function waitForFeedback(fn, maxTime) {
+    if (!maxTime) {
+        maxTime = testMaxTime;
+    }
+    var start = new Date().getTime();
+    var wait = function keepWaiting() {
+        var now = new Date().getTime();
+        var hasTimedOut = now - start > maxTime;
+        if (hasTimedOut) {
+            console.log("Test timed out");
+            fn();
+            return;
+        }
+        var feedback = page.evaluate(function() {
+            var feedback = $(".feedback");
+            if (feedback.css("display") == "none") {
+                return "";
+            }
+            return feedback.text();
+        });
+        var hasFinished = feedback.length > 0 && feedback != "Calculating...";
+        if (hasFinished) {
+            fn();
+        }
+        else {
+            setTimeout(keepWaiting, 100);
+        }
+    }
+    wait();
+}
+
 function next() {
     if (tests.length > 0) {
         var testsStr = tests.length == 1 ? "test" : "tests";
@@ -1372,12 +1403,135 @@ page.open(url, function(status) {
     next();
 });
 },
+
 // Custom BIP32 root key is used when changing the derivation path
+function() {
+page.open(url, function(status) {
+    var expected = "1Nq2Wmu726XHCuGhctEtGmhxo3wzk5wZ1H";
+    // set the root key
+    page.evaluate(function() {
+        $(".root-key").val("xprv9s21ZrQH143K2jkGDCeTLgRewT9F2pH5JZs2zDmmjXes34geVnFiuNa8KTvY5WoYvdn4Ag6oYRoB6cXtc43NgJAEqDXf51xPm6fhiMCKwpi").trigger("input");
+    });
+    waitForGenerate(function() {
+        // change the derivation path
+        page.evaluate(function() {
+            $("#account").val("1").trigger("input");
+        });
+        // check the bip32 root key is used for derivation, not the blank phrase
+        waitForGenerate(function() {
+            var actual = page.evaluate(function() {
+                return $(".address:first").text();
+            });
+            if (actual != expected) {
+                console.log("Changing the derivation path does not use BIP32 root key");
+                console.log("Expected: " + expected);
+                console.log("Actual: " + actual);
+                fail();
+            }
+            next();
+        });
+    });
+});
+},
 
 // Incorrect mnemonic shows error
+function() {
+page.open(url, function(status) {
+    // set the root key
+    page.evaluate(function() {
+        $(".phrase").val("abandon abandon abandon").trigger("input");
+    });
+    waitForFeedback(function() {
+        // check there is an error shown
+        var feedback = page.evaluate(function() {
+            return $(".feedback").text();
+        });
+        if (feedback.length <= 0) {
+            console.log("Invalid mnemonic does not show error");
+            fail();
+        }
+        next();
+    });
+});
+},
+
 // Incorrect word shows suggested replacement
+function() {
+page.open(url, function(status) {
+    // set the root key
+    page.evaluate(function() {
+        $(".phrase").val("abandon abandon abiliti").trigger("input");
+    });
+    // check there is a suggestion shown
+    waitForFeedback(function() {
+        var feedback = page.evaluate(function() {
+            return $(".feedback").text();
+        });
+        if (feedback.indexOf("did you mean ability?") < 0) {
+            console.log("Incorrect word does not show suggested replacement");
+            console.log("Error: " + error);
+            fail();
+        }
+        next();
+    });
+});
+},
+
 // Incorrect BIP32 root key shows error
+function() {
+page.open(url, function(status) {
+    // set the root key
+    page.evaluate(function() {
+        $(".root-key").val("xprv9s21ZrQH143K2jkGDCeTLgRewT9F2pH5JZs2zDmmjXes34geVnFiuNa8KTvY5WoYvdn4Ag6oYRoB6cXtc43NgJAEqDXf51xPm6fhiMCKwpj").trigger("input");
+    });
+    // check there is an error shown
+    waitForFeedback(function() {
+        var feedback = page.evaluate(function() {
+            return $(".feedback").text();
+        });
+        if (feedback != "Invalid root key") {
+            console.log("Invalid root key does not show error");
+            console.log("Error: " + error);
+            fail();
+        }
+        next();
+    });
+});
+},
+
 // Derivation path not starting with m shows error
+function() {
+page.open(url, function(status) {
+    // set the mnemonic phrase
+    page.evaluate(function() {
+        $(".phrase").val("abandon abandon ability").trigger("input");
+    });
+    waitForGenerate(function() {
+        // select the bip32 tab so custom derivation path can be set
+        page.evaluate(function() {
+            $("#bip32-tab a").click();
+        });
+        waitForGenerate(function() {
+            // set the incorrect derivation path
+            page.evaluate(function() {
+                $("#bip32 .path").val("n/0").trigger("input");
+            });
+            waitForFeedback(function() {
+                var feedback = page.evaluate(function() {
+                    return $(".feedback").text();
+                });
+                if (feedback != "First character must be 'm'") {
+                    console.log("Derivation path not starting with m should show error");
+                    console.log("Error: " + error);
+                    fail();
+                }
+                next();
+            });
+        });
+    });
+});
+},
+
 // Derivation path containing invalid characters shows useful error
 
 // Github Issue 11: Default word length is 15