index-validation.spec.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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 symmetric = require('../lib/index');
  11. var codec = require('scytl-codec');
  12. describe('The symmetric cryptography module should be able to ...', function() {
  13. var data_ = codec.utf8Encode('anyString');
  14. var service = symmetric.newService();
  15. var keyGenerator = service.newKeyGenerator();
  16. var cipher_ = service.newCipher().init(keyGenerator.nextEncryptionKey());
  17. var macHandler_ = service.newMacHandler().init(keyGenerator.nextMacKey());
  18. var mac_ = macHandler_.generate(data_);
  19. var nonObject_ = 999;
  20. var emptyObject_ = {};
  21. var nonUint8Array_ = [];
  22. describe(
  23. 'create a symmetric cryptography service that should be able to ..',
  24. function() {
  25. it('throw an error when being created, using an invalid cryptographic policy',
  26. function() {
  27. expect(function() {
  28. Object.create(symmetric.newService({policy: null}));
  29. }).toThrow();
  30. expect(function() {
  31. Object.create(symmetric.newService({policy: nonObject_}));
  32. }).toThrow();
  33. expect(function() {
  34. Object.create(symmetric.newService({policy: emptyObject_}));
  35. }).toThrow();
  36. });
  37. it('throw an error when being created, using an invalid secure random service object',
  38. function() {
  39. expect(function() {
  40. Object.create(symmetric.newService({secureRandomService: null}));
  41. }).toThrow();
  42. expect(function() {
  43. Object.create(
  44. symmetric.newService({secureRandomService: nonObject_}));
  45. }).toThrow();
  46. expect(function() {
  47. Object.create(
  48. symmetric.newService({secureRandomService: emptyObject_}));
  49. }).toThrow();
  50. });
  51. describe(
  52. 'create a symmetric cipher that should be able to ...', function() {
  53. it('throw an error when being initialized, using invalid input data',
  54. function() {
  55. expect(function() {
  56. cipher_.init();
  57. }).toThrow();
  58. expect(function() {
  59. cipher_.init(undefined);
  60. }).toThrow();
  61. expect(function() {
  62. cipher_.init(null);
  63. }).toThrow();
  64. expect(function() {
  65. cipher_.init(nonUint8Array_);
  66. }).toThrow();
  67. });
  68. it('throw an error when encrypting, using invalid input data',
  69. function() {
  70. expect(function() {
  71. cipher_.encrypt();
  72. }).toThrow();
  73. expect(function() {
  74. cipher_.encrypt(undefined);
  75. }).toThrow();
  76. expect(function() {
  77. cipher_.encrypt(null);
  78. }).toThrow();
  79. expect(function() {
  80. cipher_.encrypt(nonUint8Array_);
  81. }).toThrow();
  82. });
  83. it('throw an error when decrypting, using invalid input data',
  84. function() {
  85. expect(function() {
  86. cipher_.decrypt();
  87. }).toThrow();
  88. expect(function() {
  89. cipher_.decrypt(undefined);
  90. }).toThrow();
  91. expect(function() {
  92. cipher_.decrypt(null);
  93. }).toThrow();
  94. expect(function() {
  95. cipher_.decrypt(nonUint8Array_);
  96. }).toThrow();
  97. });
  98. });
  99. describe('create a MAC handler that should be able to ...', function() {
  100. it('throw an error when being initialized, using invalid input data',
  101. function() {
  102. expect(function() {
  103. macHandler_.init();
  104. }).toThrow();
  105. expect(function() {
  106. macHandler_.init(undefined);
  107. }).toThrow();
  108. expect(function() {
  109. macHandler_.init(null);
  110. }).toThrow();
  111. expect(function() {
  112. macHandler_.init(nonUint8Array_);
  113. }).toThrow();
  114. });
  115. it('throw an error when generating a MAC, using invalid input data',
  116. function() {
  117. expect(function() {
  118. macHandler_.generate(null);
  119. }).toThrow();
  120. expect(function() {
  121. macHandler_.generate(nonUint8Array_);
  122. }).toThrow();
  123. });
  124. it('throw an error when verifying a MAC, using an invalid MAC',
  125. function() {
  126. expect(function() {
  127. macHandler_.verify();
  128. }).toThrow();
  129. expect(function() {
  130. macHandler_.verify(undefined, data_);
  131. }).toThrow();
  132. expect(function() {
  133. macHandler_.verify(null, data_);
  134. }).toThrow();
  135. expect(function() {
  136. macHandler_.verify(nonUint8Array_, data_);
  137. }).toThrow();
  138. });
  139. it('throw an error when verifying a MAC, using an invalid verification data',
  140. function() {
  141. expect(function() {
  142. macHandler_.verify(mac_, null);
  143. }).toThrow();
  144. expect(function() {
  145. macHandler_.verify(mac_, nonUint8Array_);
  146. }).toThrow();
  147. });
  148. it('throw an error when being updated, using invalid input data',
  149. function() {
  150. expect(function() {
  151. macHandler_.update();
  152. }).toThrow();
  153. expect(function() {
  154. macHandler_.update(undefined);
  155. }).toThrow();
  156. expect(function() {
  157. macHandler_.update(null);
  158. }).toThrow();
  159. expect(function() {
  160. macHandler_.update(nonUint8Array_);
  161. }).toThrow();
  162. });
  163. });
  164. });
  165. });