]> 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 124b6ce685b5cbed506133c6bcce285c7f99eef8..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";
@@ -1252,16 +1283,255 @@ page.open(url, function(status) {
 },
 
 // Additional addresses are shown in order of derivation path
+function() {
+page.open(url, function(status) {
+    // set the phrase
+    page.evaluate(function() {
+        $(".phrase").val("abandon abandon ability").trigger("input");
+    });
+    waitForGenerate(function() {
+        // generate more addresses
+        page.evaluate(function() {
+            $(".more").click();
+        });
+        // get the derivation paths
+        waitForGenerate(function() {
+            var paths = page.evaluate(function() {
+                return $(".index").map(function(i, e) {
+                    return $(e).text();
+                });
+            });
+            if (paths.length != 40) {
+                console.log("Total additional paths is less than expected: " + paths.length);
+                fail();
+            }
+            for (var i=0; i<paths.length; i++) {
+                var expected = "m/44'/0'/0'/0/" + i;
+                var actual = paths[i];
+                if (actual != expected) {
+                    console.log("Path " + i + " is not in correct order");
+                    console.log("Expected: " + expected);
+                    console.log("Actual: " + actual);
+                    fail();
+                }
+            }
+            next();
+        });
+    });
+});
+},
 
 // BIP32 root key can be set by the user
+function() {
+page.open(url, function(status) {
+    var expected = "1Di3Vp7tBWtyQaDABLAjfWtF6V7hYKJtug";
+    // set the root key
+    page.evaluate(function() {
+        $(".root-key").val("xprv9s21ZrQH143K2jkGDCeTLgRewT9F2pH5JZs2zDmmjXes34geVnFiuNa8KTvY5WoYvdn4Ag6oYRoB6cXtc43NgJAEqDXf51xPm6fhiMCKwpi").trigger("input");
+    });
+    waitForGenerate(function() {
+        var actual = page.evaluate(function() {
+            return $(".address:first").text();
+        });
+        if (actual != expected) {
+            console.log("Setting BIP32 root key results in wrong address");
+            console.log("Expected: " + expected);
+            console.log("Actual: " + actual);
+            fail();
+        }
+        next();
+    });
+});
+},
+
 // Setting BIP32 root key clears the existing phrase, passphrase and seed
+function() {
+page.open(url, function(status) {
+    var expected = "";
+    // set a mnemonic
+    page.evaluate(function() {
+        $(".phrase").val("A non-blank but invalid value");
+    });
+    // Accept any confirm dialogs
+    page.onConfirm = function() {
+        return true;
+    };
+    // set the root key
+    page.evaluate(function() {
+        $(".root-key").val("xprv9s21ZrQH143K2jkGDCeTLgRewT9F2pH5JZs2zDmmjXes34geVnFiuNa8KTvY5WoYvdn4Ag6oYRoB6cXtc43NgJAEqDXf51xPm6fhiMCKwpi").trigger("input");
+    });
+    waitForGenerate(function() {
+        var actual = page.evaluate(function() {
+            return $(".phrase").val();
+        });
+        if (actual != expected) {
+            console.log("Phrase not cleared when setting BIP32 root key");
+            console.log("Expected: " + expected);
+            console.log("Actual: " + actual);
+            fail();
+        }
+        next();
+    });
+});
+},
+
 // Clearing of phrase, passphrase and seed can be cancelled by user
+function() {
+page.open(url, function(status) {
+    var expected = "abandon abandon ability";
+    // set a mnemonic
+    page.evaluate(function() {
+        $(".phrase").val("abandon abandon ability");
+    });
+    // Cancel any confirm dialogs
+    page.onConfirm = function() {
+        return false;
+    };
+    // set the root key
+    page.evaluate(function() {
+        $(".root-key").val("xprv9s21ZrQH143K3d3vzEDD3KpSKmxsZ3y7CqhAL1tinwtP6wqK4TKEKjpBuo6P2hUhB6ZENo7TTSRytiP857hBZVpBdk8PooFuRspE1eywwNZ").trigger("input");
+    });
+    var actual = page.evaluate(function() {
+        return $(".phrase").val();
+    });
+    if (actual != expected) {
+        console.log("Phrase not retained when cancelling changes to BIP32 root key");
+        console.log("Expected: " + expected);
+        console.log("Actual: " + actual);
+        fail();
+    }
+    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