]> git.immae.eu Git - perso/Immae/Projets/Cryptomonnaies/BIP39.git/blob - tests.js
Tests for passphrase and coin selection
[perso/Immae/Projets/Cryptomonnaies/BIP39.git] / tests.js
1 // Usage:
2 // $ phantomjs tests.js
3
4
5 var page = require('webpage').create();
6 var url = 'src/index.html';
7
8 page.onResourceError = function(e) {
9 console.log("Error loading " + e.url);
10 phantom.exit();
11 }
12
13 function fail() {
14 console.log("Failed");
15 phantom.exit();
16 }
17
18 function next() {
19 if (tests.length > 0) {
20 var testsStr = tests.length == 1 ? "test" : "tests";
21 console.log(tests.length + " " + testsStr + " remaining");
22 tests.shift()();
23 }
24 else {
25 console.log("Finished with 0 failures");
26 phantom.exit();
27 }
28 }
29
30 tests = [
31
32 // Page loads with status of 'success'
33 function() {
34 page.open(url, function(status) {
35 if (status != "success") {
36 console.log("Page did not load with status 'success'");
37 fail();
38 }
39 next();
40 });
41 },
42
43 // Page has text
44 function() {
45 page.open(url, function(status) {
46 var content = page.evaluate(function() {
47 return document.body.textContent.trim();
48 });
49 if (!content) {
50 console.log("Page does not have text");
51 fail();
52 }
53 next();
54 });
55 },
56
57 // Entering mnemonic generates addresses
58 function() {
59 page.open(url, function(status) {
60 var expected = "1Di3Vp7tBWtyQaDABLAjfWtF6V7hYKJtug";
61 // set the phrase
62 page.evaluate(function() {
63 $(".phrase").val("abandon abandon ability").trigger("input");
64 });
65 // get the address
66 setTimeout(function() {
67 var actual = page.evaluate(function() {
68 return $(".address:first").text();
69 });
70 if (actual != expected) {
71 console.log("Mnemonic did not generate address");
72 console.log("Expected: " + expected);
73 console.log("Got: " + actual);
74 fail();
75 }
76 next();
77 }, 1000);
78 });
79 },
80
81 // Random button generates random mnemonic
82 function() {
83 page.open(url, function(status) {
84 // check initial phrase is empty
85 var phrase = page.evaluate(function() {
86 return $(".phrase").text();
87 });
88 if (phrase != "") {
89 console.log("Initial phrase is not blank");
90 fail();
91 }
92 // press the 'generate' button
93 page.evaluate(function() {
94 $(".generate").click();
95 });
96 // get the new phrase
97 setTimeout(function() {
98 var phrase = page.evaluate(function() {
99 return $(".phrase").val();
100 });
101 if (phrase.length <= 0) {
102 console.log("Phrase not generated by pressing button");
103 fail();
104 }
105 next();
106 }, 1000);
107 });
108 },
109
110 // Mnemonic length can be customized
111 function() {
112 page.open(url, function(status) {
113 // set the length to 6
114 var expectedLength = "6";
115 page.evaluate(function() {
116 $(".strength option[selected]").removeAttr("selected");
117 $(".strength option[value=6]").prop("selected", true);
118 });
119 // press the 'generate' button
120 page.evaluate(function() {
121 $(".generate").click();
122 });
123 // check the new phrase is six words long
124 setTimeout(function() {
125 var actualLength = page.evaluate(function() {
126 var words = $(".phrase").val().split(" ");
127 return words.length;
128 });
129 if (actualLength != expectedLength) {
130 console.log("Phrase not generated with correct length");
131 console.log("Expected: " + expectedLength);
132 console.log("Actual: " + actualLength);
133 fail();
134 }
135 next();
136 }, 1000);
137 });
138 },
139
140 // Passphrase can be set
141 function() {
142 page.open(url, function(status) {
143 // set the phrase and passphrase
144 var expected = "15pJzUWPGzR7avffV9nY5by4PSgSKG9rba";
145 page.evaluate(function() {
146 $(".phrase").val("abandon abandon ability");
147 $(".passphrase").val("secure_passphrase").trigger("input");
148 });
149 // check the address is generated correctly
150 setTimeout(function() {
151 var actual = page.evaluate(function() {
152 return $(".address:first").text();
153 });
154 if (actual != expected) {
155 console.log("Passphrase results in wrong address");
156 console.log("Expected: " + expected);
157 console.log("Actual: " + actual);
158 fail();
159 }
160 next();
161 }, 1000);
162 });
163 },
164
165 // Network can be set to bitcoin testnet
166 function() {
167 page.open(url, function(status) {
168 // set the phrase and passphrase
169 var expected = "mucaU5iiDaJDb69BHLeDv8JFfGiyg2nJKi";
170 page.evaluate(function() {
171 $(".phrase").val("abandon abandon ability");
172 $(".phrase").trigger("input");
173 $(".network option[selected]").removeAttr("selected");
174 $(".network option[value=1]").prop("selected", true);
175 $(".network").trigger("change");
176 });
177 // check the address is generated correctly
178 setTimeout(function() {
179 var actual = page.evaluate(function() {
180 return $(".address:first").text();
181 });
182 if (actual != expected) {
183 console.log("Bitcoin testnet address is incorrect");
184 console.log("Expected: " + expected);
185 console.log("Actual: " + actual);
186 fail();
187 }
188 next();
189 }, 1000);
190 });
191 },
192
193 // TODO finish these tests
194 // Network can be set to litecoin
195 // Network can be set to dogecoin
196 // Network can be set to shadowcash
197 // Network can be set to shadowcash testnet
198 // Network can be set to viacoin
199 // Network can be set to viacoin testnet
200 // Network can be set to jumbucks
201 // Network can be set to clam
202 // BIP39 seed is set from phrase
203 // BIP32 root key is set from phrase
204
205 // Tabs show correct addresses when changed
206
207 // BIP44 derivation path is shown
208 // BIP44 extended private key is shown
209 // BIP44 extended public key is shown
210 // BIP44 purpose field changes address list
211 // BIP44 coin field changes address list
212 // BIP44 account field changes address list
213 // BIP44 external/internal field changes address list
214
215 // BIP32 derivation path can be set
216 // BIP32 can use hardened derivation paths
217 // BIP32 extended private key is shown
218 // BIP32 extended public key is shown
219
220 // Derivation path is shown in table
221 // Derivation path for address can be hardened
222 // Derivation path visibility can be toggled
223 // Address is shown
224 // Addresses are shown in order of derivation path
225 // Address visibility can be toggled
226 // Private key is shown
227 // Private key visibility can be toggled
228
229 // More addresses can be generated
230 // A custom number of additional addresses can be generated
231 // Additional addresses are shown in order of derivation path
232
233 // BIP32 root key can be set by the user
234 // Setting BIP32 root key clears the existing phrase, passphrase and seed
235 // Clearing of phrase, passphrase and seed can be cancelled by user
236 // Custom BIP32 root key is used when changing the derivation path
237
238 // Incorrect mnemonic shows error
239 // Incorrect word shows suggested replacement
240 // Incorrect BIP32 root key shows error
241 // Derivation path not starting with m shows error
242 // Derivation path containing invalid characters shows useful error
243
244 // Github Issue 11: Default word length is 15
245 // https://github.com/dcpos/bip39/issues/11
246
247 // Github Issue 12: Generate more rows with private keys hidden
248 // https://github.com/dcpos/bip39/issues/12
249
250 // Github Issue 19: Mnemonic is not sensitive to whitespace
251 // https://github.com/dcpos/bip39/issues/19
252
253 // Github Issue 23: Use correct derivation path when changing tabs
254 // https://github.com/dcpos/bip39/issues/23
255
256 ];
257
258 console.log("Running tests...");
259 next();