manager.spec.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*
  2. * Copyright 2018 Scytl Secure Electronic Voting SA
  3. *
  4. * All rights reserved
  5. *
  6. * See our extended copyright notice in *file 'Copyright.txt' which is part of this source code package
  7. */
  8. /* jshint node:true, jasmine:true */
  9. 'use strict';
  10. var EntropyManager = require('../../lib/entropy/manager');
  11. var TestEntropyCollector = require('../testentropycollector');
  12. var PrivateKeyCollector = require('../../lib/entropy/collectors/privatekey');
  13. var forge = require('node-forge');
  14. describe('The entropy manager should be able to ..', function() {
  15. var entropyManager = new EntropyManager();
  16. it('fail if it has not been initialized', function() {
  17. expect(function() {
  18. entropyManager.nextRandom(1);
  19. }).toThrow();
  20. });
  21. it('support private key entropy collection', function() {
  22. var privateKeyPem =
  23. '-----BEGIN RSA PRIVATE KEY-----MIIEowIBAAKCAQEAglypZU45bnf2opnRI+Y51VqouBpvDT33xIB/OtbwKzwVpi+JrjBFtfk33tE9t/dSRs79CK94HRhCWcOiLa2qWPrjeZ9SBiEyScrhIvRZVBF41zBgwQNuRvJCsKmAqlZaFNJDZxEP4repmlBn1CfVFmfrXmOKqwP5F7l9ZtucveRzsfmF1yVPFkW8TMuB3YqMiyymyqHlS8ujCsu5I8tpgPbwuxdMOY94fNhSXrYkY8IuX1g1zdq/Z1jluOaR/UqK4UpnbuJaH/F0VgDNiWh6cTD0DFGEk0b70i5wU4Q3L/S6XZQRvSuADoCbhwBKuFL5pW5n865oLVb5S3wuVdWaGwIDAQABAoIBAC/tn34Wf3kE9BGeGc1oFLVDaqqdVVz5/oEpeR2J7q0GnzMFYUpAhzC7WvY52cYsUPyll1Q9Jx0TUTmteo/uvKWQQFfz4nVMeS+2PoXabolBDzuWlsv/1eiRo0FOYHa/3siu8YcQN9X0DpAkpbfTmT1uoZOHZ3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. var entropyManager = new EntropyManager();
  25. entropyManager.addCollector(new PrivateKeyCollector(
  26. entropyManager.collectEntropy, privateKeyPem, 100));
  27. entropyManager.startCollectors();
  28. entropyManager.stopCollectors();
  29. // The first collector should be the private key collector
  30. var lastCollector = entropyManager.getCollectors().slice(-1)[0];
  31. var privateKey = forge.pki.privateKeyFromPem(privateKeyPem);
  32. expect(lastCollector).toBeDefined();
  33. expect(lastCollector.getPrivateKey().d).toEqual(privateKey.d);
  34. });
  35. it('support the WebKit entropy collector', function() {
  36. var entropyManager = new EntropyManager(
  37. {windowObject: {}, cryptoApi: {getRandomValues: {}}});
  38. entropyManager.startCollectors();
  39. entropyManager.stopCollectors();
  40. expect(entropyManager.getCollectors().length).toBe(1);
  41. });
  42. it('set up the default collectors', function() {
  43. var entropyManager = new EntropyManager({windowObject: {}, cryptoApi: {}});
  44. expect(entropyManager.getCollectors().length).toBe(4);
  45. });
  46. it('start and stop collectors manually', function() {
  47. var entropyManager = new EntropyManager(
  48. {windowObject: {}, cryptoApi: {getRandomValues: {}}});
  49. entropyManager.startCollectors();
  50. expect(entropyManager.isCollecting()).toBe(true);
  51. entropyManager.stopCollectors();
  52. expect(entropyManager.isCollecting()).toBe(false);
  53. });
  54. it('stop entropy collection when capacity is reached', function(done) {
  55. // Enough entropy when the counter hits 1.
  56. // Do not add any collectors on start.
  57. var entropyManager =
  58. new EntropyManager({maxEntropyCounter: 1, noDefaultCollectors: true});
  59. // Only the test collectors contributes to entropy.
  60. var testEntropyCollector =
  61. entropyManager.addCollector(TestEntropyCollector);
  62. // Start collecting entropy, and call the following function when done.
  63. entropyManager.startCollectors(function() {
  64. expect(entropyManager.getEntropyPercentage()).toBe(100);
  65. done();
  66. });
  67. testEntropyCollector.entropyCounter = 2;
  68. });
  69. it('report 100% entropy collected when maximum entropy is reached',
  70. function(done) {
  71. var entropyManager = new EntropyManager(
  72. {maxEntropyCounter: 2, noDefaultCollectors: true});
  73. var testEntropyCollector =
  74. entropyManager.addCollector(TestEntropyCollector);
  75. testEntropyCollector.entropyCounter = 2;
  76. entropyManager.startCollectors(function() {
  77. expect(entropyManager.getEntropyPercentage()).toEqual(100);
  78. done();
  79. });
  80. });
  81. it('report 100% entropy collected when counter surpasses the maximum entropy',
  82. function(done) {
  83. var entropyManager = new EntropyManager(
  84. {maxEntropyCounter: 2, noDefaultCollectors: true});
  85. var testEntropyCollector =
  86. entropyManager.addCollector(TestEntropyCollector);
  87. testEntropyCollector.entropyCounter = 4;
  88. entropyManager.startCollectors(function() {
  89. expect(entropyManager.getEntropyPercentage()).toEqual(100);
  90. done();
  91. });
  92. });
  93. it('report 0% entropy collected when no entropy has been collected',
  94. function() {
  95. var entropyManager = new EntropyManager({noDefaultCollectors: true});
  96. var testEntropyCollector =
  97. entropyManager.addCollector(TestEntropyCollector);
  98. testEntropyCollector.entropyCounter = 0;
  99. expect(entropyManager.getEntropyPercentage()).toEqual(0);
  100. });
  101. it('report 50% entropy collected when entropy counter = max entropy / 2',
  102. function() {
  103. var entropyManager = new EntropyManager(
  104. {maxEntropyCounter: 2, noDefaultCollectors: true});
  105. var testEntropyCollector =
  106. entropyManager.addCollector(TestEntropyCollector);
  107. testEntropyCollector.entropyCounter = 1;
  108. entropyManager.startCollectors(function() {
  109. expect(entropyManager.getEntropyPercentage()).toEqual(50);
  110. });
  111. });
  112. it('report 10% entropy collected when entropy counter is 10% of maximum entropy',
  113. function() {
  114. var entropyManager = new EntropyManager(
  115. {maxEntropyCounter: 10, noDefaultCollectors: true});
  116. var testEntropyCollector =
  117. entropyManager.addCollector(TestEntropyCollector);
  118. testEntropyCollector.entropyCounter = 1;
  119. entropyManager.startCollectors(function() {
  120. expect(entropyManager.getEntropyPercentage()).toEqual(10);
  121. });
  122. });
  123. });