]> git.immae.eu Git - perso/Immae/Projets/Cryptomonnaies/BIP39.git/blame - tests.js
Test BIP44 derivation path is shown
[perso/Immae/Projets/Cryptomonnaies/BIP39.git] / tests.js
CommitLineData
88e2cdaa
IC
1// Usage:
2// $ phantomjs tests.js
3
4
5var page = require('webpage').create();
6var url = 'src/index.html';
3eef9d0d 7var testMaxTime = 5000;
88e2cdaa
IC
8
9page.onResourceError = function(e) {
10 console.log("Error loading " + e.url);
11 phantom.exit();
12}
13
14function fail() {
15 console.log("Failed");
16 phantom.exit();
17}
18
3eef9d0d
IC
19function waitForGenerate(fn, maxTime) {
20 if (!maxTime) {
21 maxTime = testMaxTime;
22 }
23 var start = new Date().getTime();
24 var prevAddressCount = -1;
25 var wait = function keepWaiting() {
26 var now = new Date().getTime();
27 var hasTimedOut = now - start > maxTime;
28 var addressCount = page.evaluate(function() {
29 return $(".address").length;
30 });
31 var hasFinished = addressCount > 0 && addressCount == prevAddressCount;
32 prevAddressCount = addressCount;
33 if (hasFinished) {
34 fn();
35 }
36 else if (hasTimedOut) {
37 console.log("Test timed out");
38 fn();
39 }
40 else {
41 setTimeout(keepWaiting, 100);
42 }
43 }
44 wait();
45}
46
88e2cdaa
IC
47function next() {
48 if (tests.length > 0) {
49 var testsStr = tests.length == 1 ? "test" : "tests";
50 console.log(tests.length + " " + testsStr + " remaining");
51 tests.shift()();
52 }
53 else {
54 console.log("Finished with 0 failures");
55 phantom.exit();
56 }
57}
58
59tests = [
60
61// Page loads with status of 'success'
62function() {
63page.open(url, function(status) {
64 if (status != "success") {
65 console.log("Page did not load with status 'success'");
66 fail();
67 }
68 next();
69});
70},
71
72// Page has text
73function() {
74page.open(url, function(status) {
75 var content = page.evaluate(function() {
76 return document.body.textContent.trim();
77 });
78 if (!content) {
79 console.log("Page does not have text");
80 fail();
81 }
82 next();
83});
84},
85
86// Entering mnemonic generates addresses
87function() {
88page.open(url, function(status) {
89 var expected = "1Di3Vp7tBWtyQaDABLAjfWtF6V7hYKJtug";
90 // set the phrase
91 page.evaluate(function() {
92 $(".phrase").val("abandon abandon ability").trigger("input");
93 });
94 // get the address
3eef9d0d 95 waitForGenerate(function() {
88e2cdaa
IC
96 var actual = page.evaluate(function() {
97 return $(".address:first").text();
98 });
99 if (actual != expected) {
100 console.log("Mnemonic did not generate address");
101 console.log("Expected: " + expected);
102 console.log("Got: " + actual);
103 fail();
104 }
105 next();
3eef9d0d 106 });
88e2cdaa
IC
107});
108},
109
110// Random button generates random mnemonic
111function() {
112page.open(url, function(status) {
113 // check initial phrase is empty
114 var phrase = page.evaluate(function() {
115 return $(".phrase").text();
116 });
117 if (phrase != "") {
118 console.log("Initial phrase is not blank");
119 fail();
120 }
121 // press the 'generate' button
122 page.evaluate(function() {
123 $(".generate").click();
124 });
125 // get the new phrase
3eef9d0d 126 waitForGenerate(function() {
88e2cdaa
IC
127 var phrase = page.evaluate(function() {
128 return $(".phrase").val();
129 });
130 if (phrase.length <= 0) {
131 console.log("Phrase not generated by pressing button");
132 fail();
133 }
134 next();
3eef9d0d 135 });
88e2cdaa
IC
136});
137},
138
139// Mnemonic length can be customized
140function() {
141page.open(url, function(status) {
142 // set the length to 6
54563907 143 var expectedLength = "6";
88e2cdaa 144 page.evaluate(function() {
54563907
IC
145 $(".strength option[selected]").removeAttr("selected");
146 $(".strength option[value=6]").prop("selected", true);
88e2cdaa
IC
147 });
148 // press the 'generate' button
149 page.evaluate(function() {
150 $(".generate").click();
151 });
152 // check the new phrase is six words long
3eef9d0d 153 waitForGenerate(function() {
88e2cdaa
IC
154 var actualLength = page.evaluate(function() {
155 var words = $(".phrase").val().split(" ");
156 return words.length;
157 });
158 if (actualLength != expectedLength) {
159 console.log("Phrase not generated with correct length");
160 console.log("Expected: " + expectedLength);
161 console.log("Actual: " + actualLength);
162 fail();
163 }
54563907 164 next();
3eef9d0d 165 });
88e2cdaa
IC
166});
167},
168
88e2cdaa 169// Passphrase can be set
54563907
IC
170function() {
171page.open(url, function(status) {
172 // set the phrase and passphrase
173 var expected = "15pJzUWPGzR7avffV9nY5by4PSgSKG9rba";
174 page.evaluate(function() {
175 $(".phrase").val("abandon abandon ability");
176 $(".passphrase").val("secure_passphrase").trigger("input");
177 });
178 // check the address is generated correctly
3eef9d0d 179 waitForGenerate(function() {
54563907
IC
180 var actual = page.evaluate(function() {
181 return $(".address:first").text();
182 });
183 if (actual != expected) {
184 console.log("Passphrase results in wrong address");
185 console.log("Expected: " + expected);
186 console.log("Actual: " + actual);
187 fail();
188 }
189 next();
3eef9d0d 190 });
54563907
IC
191});
192},
193
88e2cdaa 194// Network can be set to bitcoin testnet
54563907
IC
195function() {
196page.open(url, function(status) {
59193779 197 // set the phrase and coin
54563907
IC
198 var expected = "mucaU5iiDaJDb69BHLeDv8JFfGiyg2nJKi";
199 page.evaluate(function() {
200 $(".phrase").val("abandon abandon ability");
201 $(".phrase").trigger("input");
202 $(".network option[selected]").removeAttr("selected");
203 $(".network option[value=1]").prop("selected", true);
204 $(".network").trigger("change");
205 });
206 // check the address is generated correctly
3eef9d0d 207 waitForGenerate(function() {
54563907
IC
208 var actual = page.evaluate(function() {
209 return $(".address:first").text();
210 });
211 if (actual != expected) {
212 console.log("Bitcoin testnet address is incorrect");
213 console.log("Expected: " + expected);
214 console.log("Actual: " + actual);
215 fail();
216 }
217 next();
3eef9d0d 218 });
54563907
IC
219});
220},
221
88e2cdaa 222// Network can be set to litecoin
59193779
IC
223function() {
224page.open(url, function(status) {
225 // set the phrase and coin
226 var expected = "LQ4XU8RX2ULPmPq9FcUHdVmPVchP9nwXdn";
227 page.evaluate(function() {
228 $(".phrase").val("abandon abandon ability");
229 $(".phrase").trigger("input");
230 $(".network option[selected]").removeAttr("selected");
231 $(".network option[value=2]").prop("selected", true);
232 $(".network").trigger("change");
233 });
234 // check the address is generated correctly
3eef9d0d 235 waitForGenerate(function() {
59193779
IC
236 var actual = page.evaluate(function() {
237 return $(".address:first").text();
238 });
239 if (actual != expected) {
240 console.log("Litecoin address is incorrect");
241 console.log("Expected: " + expected);
242 console.log("Actual: " + actual);
243 fail();
244 }
245 next();
3eef9d0d 246 });
59193779
IC
247});
248},
249
88e2cdaa 250// Network can be set to dogecoin
59193779
IC
251function() {
252page.open(url, function(status) {
253 // set the phrase and coin
254 var expected = "DPQH2AtuzkVSG6ovjKk4jbUmZ6iXLpgbJA";
255 page.evaluate(function() {
256 $(".phrase").val("abandon abandon ability");
257 $(".phrase").trigger("input");
258 $(".network option[selected]").removeAttr("selected");
259 $(".network option[value=3]").prop("selected", true);
260 $(".network").trigger("change");
261 });
262 // check the address is generated correctly
3eef9d0d 263 waitForGenerate(function() {
59193779
IC
264 var actual = page.evaluate(function() {
265 return $(".address:first").text();
266 });
267 if (actual != expected) {
268 console.log("Dogecoin address is incorrect");
269 console.log("Expected: " + expected);
270 console.log("Actual: " + actual);
271 fail();
272 }
273 next();
3eef9d0d 274 });
59193779
IC
275});
276},
277
88e2cdaa 278// Network can be set to shadowcash
59193779
IC
279function() {
280page.open(url, function(status) {
281 // set the phrase and coin
282 var expected = "SiSZtfYAXEFvMm3XM8hmtkGDyViRwErtCG";
283 page.evaluate(function() {
284 $(".phrase").val("abandon abandon ability");
285 $(".phrase").trigger("input");
286 $(".network option[selected]").removeAttr("selected");
287 $(".network option[value=4]").prop("selected", true);
288 $(".network").trigger("change");
289 });
290 // check the address is generated correctly
3eef9d0d 291 waitForGenerate(function() {
59193779
IC
292 var actual = page.evaluate(function() {
293 return $(".address:first").text();
294 });
295 if (actual != expected) {
296 console.log("Shadowcash address is incorrect");
297 console.log("Expected: " + expected);
298 console.log("Actual: " + actual);
299 fail();
300 }
301 next();
3eef9d0d 302 });
59193779
IC
303});
304},
305
88e2cdaa 306// Network can be set to shadowcash testnet
59193779
IC
307function() {
308page.open(url, function(status) {
309 // set the phrase and coin
310 var expected = "tM2EDpVKaTiEg2NZg3yKg8eqjLr55BErHe";
311 page.evaluate(function() {
312 $(".phrase").val("abandon abandon ability");
313 $(".phrase").trigger("input");
314 $(".network option[selected]").removeAttr("selected");
315 $(".network option[value=5]").prop("selected", true);
316 $(".network").trigger("change");
317 });
318 // check the address is generated correctly
3eef9d0d 319 waitForGenerate(function() {
59193779
IC
320 var actual = page.evaluate(function() {
321 return $(".address:first").text();
322 });
323 if (actual != expected) {
324 console.log("Shadowcash testnet address is incorrect");
325 console.log("Expected: " + expected);
326 console.log("Actual: " + actual);
327 fail();
328 }
329 next();
3eef9d0d 330 });
59193779
IC
331});
332},
333
88e2cdaa 334// Network can be set to viacoin
59193779
IC
335function() {
336page.open(url, function(status) {
337 // set the phrase and coin
338 var expected = "Vq9Eq4N5SQnjqZvxtxzo7hZPW5XnyJsmXT";
339 page.evaluate(function() {
340 $(".phrase").val("abandon abandon ability");
341 $(".phrase").trigger("input");
342 $(".network option[selected]").removeAttr("selected");
343 $(".network option[value=6]").prop("selected", true);
344 $(".network").trigger("change");
345 });
346 // check the address is generated correctly
3eef9d0d 347 waitForGenerate(function() {
59193779
IC
348 var actual = page.evaluate(function() {
349 return $(".address:first").text();
350 });
351 if (actual != expected) {
352 console.log("Viacoin address is incorrect");
353 console.log("Expected: " + expected);
354 console.log("Actual: " + actual);
355 fail();
356 }
357 next();
3eef9d0d 358 });
59193779
IC
359});
360},
361
88e2cdaa 362// Network can be set to viacoin testnet
59193779
IC
363function() {
364page.open(url, function(status) {
365 // set the phrase and coin
366 var expected = "tM2EDpVKaTiEg2NZg3yKg8eqjLr55BErHe";
367 page.evaluate(function() {
368 $(".phrase").val("abandon abandon ability");
369 $(".phrase").trigger("input");
370 $(".network option[selected]").removeAttr("selected");
371 $(".network option[value=7]").prop("selected", true);
372 $(".network").trigger("change");
373 });
374 // check the address is generated correctly
3eef9d0d 375 waitForGenerate(function() {
59193779
IC
376 var actual = page.evaluate(function() {
377 return $(".address:first").text();
378 });
379 if (actual != expected) {
380 console.log("Viacoin testnet address is incorrect");
381 console.log("Expected: " + expected);
382 console.log("Actual: " + actual);
383 fail();
384 }
385 next();
3eef9d0d 386 });
59193779
IC
387});
388},
389
88e2cdaa 390// Network can be set to jumbucks
59193779
IC
391function() {
392page.open(url, function(status) {
393 // set the phrase and coin
394 var expected = "JLEXccwDXADK4RxBPkRez7mqsHVoJBEUew";
395 page.evaluate(function() {
396 $(".phrase").val("abandon abandon ability");
397 $(".phrase").trigger("input");
398 $(".network option[selected]").removeAttr("selected");
399 $(".network option[value=8]").prop("selected", true);
400 $(".network").trigger("change");
401 });
402 // check the address is generated correctly
3eef9d0d 403 waitForGenerate(function() {
59193779
IC
404 var actual = page.evaluate(function() {
405 return $(".address:first").text();
406 });
407 if (actual != expected) {
408 console.log("Jumbucks address is incorrect");
409 console.log("Expected: " + expected);
410 console.log("Actual: " + actual);
411 fail();
412 }
413 next();
3eef9d0d 414 });
59193779
IC
415});
416},
417
88e2cdaa 418// Network can be set to clam
59193779
IC
419function() {
420page.open(url, function(status) {
421 // set the phrase and coin
422 var expected = "xCp4sakjVx4pUAZ6cBCtuin8Ddb6U1sk9y";
423 page.evaluate(function() {
424 $(".phrase").val("abandon abandon ability");
425 $(".phrase").trigger("input");
426 $(".network option[selected]").removeAttr("selected");
427 $(".network option[value=9]").prop("selected", true);
428 $(".network").trigger("change");
429 });
430 // check the address is generated correctly
3eef9d0d 431 waitForGenerate(function() {
59193779
IC
432 var actual = page.evaluate(function() {
433 return $(".address:first").text();
434 });
435 if (actual != expected) {
436 console.log("CLAM address is incorrect");
437 console.log("Expected: " + expected);
438 console.log("Actual: " + actual);
439 fail();
440 }
441 next();
3eef9d0d 442 });
59193779
IC
443});
444},
445
88e2cdaa 446// BIP39 seed is set from phrase
c196ad55
IC
447function() {
448page.open(url, function(status) {
449 // set the phrase
450 var expected = "20da140d3dd1df8713cefcc4d54ce0e445b4151027a1ab567b832f6da5fcc5afc1c3a3f199ab78b8e0ab4652efd7f414ac2c9a3b81bceb879a70f377aa0a58f3";
451 page.evaluate(function() {
452 $(".phrase").val("abandon abandon ability");
453 $(".phrase").trigger("input");
454 });
455 // check the address is generated correctly
3eef9d0d 456 waitForGenerate(function() {
c196ad55
IC
457 var actual = page.evaluate(function() {
458 return $(".seed").val();
459 });
460 if (actual != expected) {
461 console.log("BIP39 seed is incorrectly generated from mnemonic");
462 console.log("Expected: " + expected);
463 console.log("Actual: " + actual);
464 fail();
465 }
466 next();
3eef9d0d 467 });
c196ad55
IC
468});
469},
470
88e2cdaa 471// BIP32 root key is set from phrase
ec60b662
IC
472function() {
473page.open(url, function(status) {
474 // set the phrase
475 var expected = "xprv9s21ZrQH143K2jkGDCeTLgRewT9F2pH5JZs2zDmmjXes34geVnFiuNa8KTvY5WoYvdn4Ag6oYRoB6cXtc43NgJAEqDXf51xPm6fhiMCKwpi";
476 page.evaluate(function() {
477 $(".phrase").val("abandon abandon ability");
478 $(".phrase").trigger("input");
479 });
480 // check the address is generated correctly
3eef9d0d 481 waitForGenerate(function() {
ec60b662
IC
482 var actual = page.evaluate(function() {
483 return $(".root-key").val();
484 });
485 if (actual != expected) {
486 console.log("Root key is incorrectly generated from mnemonic");
487 console.log("Expected: " + expected);
488 console.log("Actual: " + actual);
489 fail();
490 }
491 next();
3eef9d0d 492 });
ec60b662
IC
493});
494},
495
88e2cdaa 496// Tabs show correct addresses when changed
cf7258fd
IC
497function() {
498page.open(url, function(status) {
499 // set the phrase
500 var expected = "17uQ7s2izWPwBmEVFikTmZUjbBKWYdJchz";
501 page.evaluate(function() {
502 $(".phrase").val("abandon abandon ability");
503 $(".phrase").trigger("input");
504 });
505 // change tabs
506 waitForGenerate(function() {
507 page.evaluate(function() {
508 $("#bip32-tab a").click();
509 });
510 // check the address is generated correctly
511 waitForGenerate(function() {
512 var actual = page.evaluate(function() {
513 return $(".address:first").text();
514 });
515 if (actual != expected) {
516 console.log("Clicking tab generates incorrect address");
517 console.log("Expected: " + expected);
518 console.log("Actual: " + actual);
519 fail();
520 }
521 next();
522 });
523 });
524});
525},
88e2cdaa
IC
526
527// BIP44 derivation path is shown
d077e1e7
IC
528function() {
529page.open(url, function(status) {
530 // set the phrase
531 var expected = "m/44'/0'/0'/0";
532 page.evaluate(function() {
533 $(".phrase").val("abandon abandon ability");
534 $(".phrase").trigger("input");
535 });
536 // check the derivation path of the first address
537 waitForGenerate(function() {
538 var actual = page.evaluate(function() {
539 return $("#bip44 .path").val();
540 });
541 if (actual != expected) {
542 console.log("BIP44 derivation path is incorrect");
543 console.log("Expected: " + expected);
544 console.log("Actual: " + actual);
545 fail();
546 }
547 next();
548 });
549});
550},
551
cf7258fd
IC
552// TODO finish these tests
553
88e2cdaa
IC
554// BIP44 extended private key is shown
555// BIP44 extended public key is shown
556// BIP44 purpose field changes address list
557// BIP44 coin field changes address list
558// BIP44 account field changes address list
559// BIP44 external/internal field changes address list
560
561// BIP32 derivation path can be set
562// BIP32 can use hardened derivation paths
563// BIP32 extended private key is shown
564// BIP32 extended public key is shown
565
566// Derivation path is shown in table
567// Derivation path for address can be hardened
568// Derivation path visibility can be toggled
569// Address is shown
570// Addresses are shown in order of derivation path
571// Address visibility can be toggled
572// Private key is shown
573// Private key visibility can be toggled
574
575// More addresses can be generated
576// A custom number of additional addresses can be generated
577// Additional addresses are shown in order of derivation path
578
579// BIP32 root key can be set by the user
54563907
IC
580// Setting BIP32 root key clears the existing phrase, passphrase and seed
581// Clearing of phrase, passphrase and seed can be cancelled by user
88e2cdaa
IC
582// Custom BIP32 root key is used when changing the derivation path
583
584// Incorrect mnemonic shows error
585// Incorrect word shows suggested replacement
586// Incorrect BIP32 root key shows error
587// Derivation path not starting with m shows error
588// Derivation path containing invalid characters shows useful error
589
590// Github Issue 11: Default word length is 15
591// https://github.com/dcpos/bip39/issues/11
592
593// Github Issue 12: Generate more rows with private keys hidden
594// https://github.com/dcpos/bip39/issues/12
595
596// Github Issue 19: Mnemonic is not sensitive to whitespace
597// https://github.com/dcpos/bip39/issues/19
598
599// Github Issue 23: Use correct derivation path when changing tabs
600// https://github.com/dcpos/bip39/issues/23
601
602];
603
604console.log("Running tests...");
605next();