]> git.immae.eu Git - perso/Immae/Projets/Cryptomonnaies/BIP39.git/blob - tests.js
Test changing tabs
[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 var testMaxTime = 5000;
8
9 page.onResourceError = function(e) {
10 console.log("Error loading " + e.url);
11 phantom.exit();
12 }
13
14 function fail() {
15 console.log("Failed");
16 phantom.exit();
17 }
18
19 function 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
47 function 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
59 tests = [
60
61 // Page loads with status of 'success'
62 function() {
63 page.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
73 function() {
74 page.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
87 function() {
88 page.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
95 waitForGenerate(function() {
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();
106 });
107 });
108 },
109
110 // Random button generates random mnemonic
111 function() {
112 page.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
126 waitForGenerate(function() {
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();
135 });
136 });
137 },
138
139 // Mnemonic length can be customized
140 function() {
141 page.open(url, function(status) {
142 // set the length to 6
143 var expectedLength = "6";
144 page.evaluate(function() {
145 $(".strength option[selected]").removeAttr("selected");
146 $(".strength option[value=6]").prop("selected", true);
147 });
148 // press the 'generate' button
149 page.evaluate(function() {
150 $(".generate").click();
151 });
152 // check the new phrase is six words long
153 waitForGenerate(function() {
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 }
164 next();
165 });
166 });
167 },
168
169 // Passphrase can be set
170 function() {
171 page.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
179 waitForGenerate(function() {
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();
190 });
191 });
192 },
193
194 // Network can be set to bitcoin testnet
195 function() {
196 page.open(url, function(status) {
197 // set the phrase and coin
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
207 waitForGenerate(function() {
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();
218 });
219 });
220 },
221
222 // Network can be set to litecoin
223 function() {
224 page.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
235 waitForGenerate(function() {
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();
246 });
247 });
248 },
249
250 // Network can be set to dogecoin
251 function() {
252 page.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
263 waitForGenerate(function() {
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();
274 });
275 });
276 },
277
278 // Network can be set to shadowcash
279 function() {
280 page.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
291 waitForGenerate(function() {
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();
302 });
303 });
304 },
305
306 // Network can be set to shadowcash testnet
307 function() {
308 page.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
319 waitForGenerate(function() {
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();
330 });
331 });
332 },
333
334 // Network can be set to viacoin
335 function() {
336 page.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
347 waitForGenerate(function() {
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();
358 });
359 });
360 },
361
362 // Network can be set to viacoin testnet
363 function() {
364 page.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
375 waitForGenerate(function() {
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();
386 });
387 });
388 },
389
390 // Network can be set to jumbucks
391 function() {
392 page.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
403 waitForGenerate(function() {
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();
414 });
415 });
416 },
417
418 // Network can be set to clam
419 function() {
420 page.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
431 waitForGenerate(function() {
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();
442 });
443 });
444 },
445
446 // BIP39 seed is set from phrase
447 function() {
448 page.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
456 waitForGenerate(function() {
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();
467 });
468 });
469 },
470
471 // BIP32 root key is set from phrase
472 function() {
473 page.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
481 waitForGenerate(function() {
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();
492 });
493 });
494 },
495
496 // Tabs show correct addresses when changed
497 function() {
498 page.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 },
526
527 // BIP44 derivation path is shown
528 // TODO finish these tests
529
530 // BIP44 extended private key is shown
531 // BIP44 extended public key is shown
532 // BIP44 purpose field changes address list
533 // BIP44 coin field changes address list
534 // BIP44 account field changes address list
535 // BIP44 external/internal field changes address list
536
537 // BIP32 derivation path can be set
538 // BIP32 can use hardened derivation paths
539 // BIP32 extended private key is shown
540 // BIP32 extended public key is shown
541
542 // Derivation path is shown in table
543 // Derivation path for address can be hardened
544 // Derivation path visibility can be toggled
545 // Address is shown
546 // Addresses are shown in order of derivation path
547 // Address visibility can be toggled
548 // Private key is shown
549 // Private key visibility can be toggled
550
551 // More addresses can be generated
552 // A custom number of additional addresses can be generated
553 // Additional addresses are shown in order of derivation path
554
555 // BIP32 root key can be set by the user
556 // Setting BIP32 root key clears the existing phrase, passphrase and seed
557 // Clearing of phrase, passphrase and seed can be cancelled by user
558 // Custom BIP32 root key is used when changing the derivation path
559
560 // Incorrect mnemonic shows error
561 // Incorrect word shows suggested replacement
562 // Incorrect BIP32 root key shows error
563 // Derivation path not starting with m shows error
564 // Derivation path containing invalid characters shows useful error
565
566 // Github Issue 11: Default word length is 15
567 // https://github.com/dcpos/bip39/issues/11
568
569 // Github Issue 12: Generate more rows with private keys hidden
570 // https://github.com/dcpos/bip39/issues/12
571
572 // Github Issue 19: Mnemonic is not sensitive to whitespace
573 // https://github.com/dcpos/bip39/issues/19
574
575 // Github Issue 23: Use correct derivation path when changing tabs
576 // https://github.com/dcpos/bip39/issues/23
577
578 ];
579
580 console.log("Running tests...");
581 next();