2
0

index-validation.spec.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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 */
  9. 'use strict';
  10. var digitalEnvelope = require('../lib/index');
  11. var codec = require('scytl-codec');
  12. describe('The digital envelope module should be able to ...', function() {
  13. var PUBLIC_KEY_PEMS = [
  14. '-----BEGIN PUBLIC KEY-----MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAglypZU45bnf2opnRI+Y51VqouBpvDT33xIB/OtbwKzwVpi+JrjBFtfk33tE9t/dSRs79CK94HRhCWcOiLa2qWPrjeZ9SBiEyScrhIvRZVBF41zBgwQNuRvJCsKmAqlZaFNJDZxEP4repmlBn1CfVFmfrXmOKqwP5F7l9ZtucveRzsfmF1yVPFkW8TMuB3YqMiyymyqHlS8ujCsu5I8tpgPbwuxdMOY94fNhSXrYkY8IuX1g1zdq/Z1jluOaR/UqK4UpnbuJaH/F0VgDNiWh6cTD0DFGEk0b70i5wU4Q3L/S6XZQRvSuADoCbhwBKuFL5pW5n865oLVb5S3wuVdWaGwIDAQAB-----END PUBLIC KEY-----',
  15. '-----BEGIN PUBLIC KEY-----MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAngCJ2RYh3XOvghmAi2MT26/fx4Hx8gYkYS2zaf9c2H8Qns1hnIHO8pIlHze/stUmNzim6nlytbJLQ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/ltJiNxALMW97QGNWPjdnwPjQI8qCBhYn0Bk7T00fmZSFERUtms9H8ICdLyH1kHAmkIC/NgRriZzQEiW9CFF+8p4ViGcQDBbg1NqXsYReLn658i6mzA4DW8Svhd+igIviQ2JnP68Z4OeKZBx2tcrrOfwrRAoGAGyXTcOvhr+5ayXkQ36VAr2YcAUs2SeN0HwmYBP2ErCgd/KXri7RwWXqEQtvpvAOGJq/tXyc7P5BnBhlqQtUm9WX7kqTy/dIS+pJP0UMm02N9U+b02G0auPmBKM35LXO4YGhZep1Vig+MLycR8OSe0cd4gzk52u3Th9I3e0bV9Fw=-----END RSA PRIVATE KEY-----'
  20. ];
  21. var DATA = 'Ox2fUJq1gAbX';
  22. var data_;
  23. var envelopeService_;
  24. var envelopeGenerator_;
  25. var envelopeOpener_;
  26. var envelope_;
  27. var encryptedData_;
  28. var mac_;
  29. var encryptedSecretKeyPairs_;
  30. var nonString_;
  31. var emptyString_;
  32. var nonJsonString_;
  33. var nonObject_;
  34. var emptyObject_;
  35. var nonUint8Array_;
  36. var nonDigitalEnvelope_;
  37. var nonEncryptedSecretKeyPairs_;
  38. beforeAll(function() {
  39. data_ = codec.utf8Encode(DATA);
  40. envelopeService_ = digitalEnvelope.newService();
  41. nonString_ = 999;
  42. emptyString_ = '';
  43. nonJsonString_ = 'Not a JSON string';
  44. nonObject_ = 999;
  45. emptyObject_ = {};
  46. nonUint8Array_ = [];
  47. nonDigitalEnvelope_ = envelopeService_;
  48. nonEncryptedSecretKeyPairs_ = [1, 2, 3];
  49. });
  50. beforeEach(function() {
  51. envelopeGenerator_ = envelopeService_.newGenerator().init(
  52. PUBLIC_KEY_PEMS[0], PUBLIC_KEY_PEMS[1]);
  53. envelopeOpener_ = envelopeService_.newOpener().init(PRIVATE_KEY_PEMS[0]);
  54. envelope_ = envelopeGenerator_.generate(data_);
  55. encryptedData_ = envelope_.encryptedData;
  56. mac_ = envelope_.mac;
  57. encryptedSecretKeyPairs_ = envelope_.encryptedSecretKeyPairs;
  58. });
  59. describe('create a digital envelope service that should be able to ..', function() {
  60. it('throw an error when being created, using an invalid cryptographic policy object',
  61. function() {
  62. expect(function() {
  63. Object.create(digitalEnvelope.newService({policy: null}));
  64. }).toThrow();
  65. expect(function() {
  66. Object.create(digitalEnvelope.newService({policy: nonObject_}));
  67. }).toThrow();
  68. expect(function() {
  69. Object.create(digitalEnvelope.newService({policy: emptyObject_}));
  70. }).toThrow();
  71. });
  72. it('throw an error when being created, using an invalid symmetric cryptography service object',
  73. function() {
  74. expect(function() {
  75. Object.create(digitalEnvelope.newService(
  76. {symmetricCryptographyService: null}));
  77. }).toThrow();
  78. expect(function() {
  79. Object.create(digitalEnvelope.newService(
  80. {symmetricCryptographyService: nonObject_}));
  81. }).toThrow();
  82. expect(function() {
  83. Object.create(digitalEnvelope.newService(
  84. {symmetricCryptographyService: emptyObject_}));
  85. }).toThrow();
  86. });
  87. it('throw an error when being created, using an invalid asymmetric cryptography service object',
  88. function() {
  89. expect(function() {
  90. Object.create(digitalEnvelope.newService(
  91. {asymmetricCryptographyService: null}));
  92. }).toThrow();
  93. expect(function() {
  94. Object.create(digitalEnvelope.newService(
  95. {asymmetricCryptographyService: nonObject_}));
  96. }).toThrow();
  97. expect(function() {
  98. Object.create(digitalEnvelope.newService(
  99. {asymmetricCryptographyService: emptyObject_}));
  100. }).toThrow();
  101. });
  102. it('throw an error when creating a digital envelope object, using invalid encrypted data or an invalid JSON serialization',
  103. function() {
  104. expect(function() {
  105. envelopeService_.newEnvelope(
  106. undefined, mac_, encryptedSecretKeyPairs_);
  107. }).toThrow();
  108. expect(function() {
  109. envelopeService_.newEnvelope(null, mac_, encryptedSecretKeyPairs_);
  110. }).toThrow();
  111. expect(function() {
  112. envelopeService_.newEnvelope(
  113. nonUint8Array_, mac_, encryptedSecretKeyPairs_);
  114. }).toThrow();
  115. expect(function() {
  116. envelopeService_.newEnvelope(nonJsonString_);
  117. }).toThrow();
  118. });
  119. it('throw an error when creating a digital envelope object, using an invalid MAC',
  120. function() {
  121. expect(function() {
  122. envelopeService_.newEnvelope(
  123. encryptedData_, undefined, encryptedSecretKeyPairs_);
  124. }).toThrow();
  125. expect(function() {
  126. envelopeService_.newEnvelope(
  127. encryptedData_, null, encryptedSecretKeyPairs_);
  128. }).toThrow();
  129. expect(function() {
  130. envelopeService_.newEnvelope(
  131. encryptedData_, nonUint8Array_, encryptedSecretKeyPairs_);
  132. }).toThrow();
  133. });
  134. it('throw an error when creating a digital envelope object, using invalid encrypted secret key pairs',
  135. function() {
  136. expect(function() {
  137. envelopeService_.newEnvelope(encryptedData_, mac_);
  138. }).toThrow();
  139. expect(function() {
  140. envelopeService_.newEnvelope(encryptedData_, mac_, undefined);
  141. }).toThrow();
  142. expect(function() {
  143. envelopeService_.newEnvelope(encryptedData_, mac_, null);
  144. }).toThrow();
  145. expect(function() {
  146. envelopeService_.newEnvelope(
  147. encryptedData_, mac_, nonEncryptedSecretKeyPairs_);
  148. }).toThrow();
  149. });
  150. describe(
  151. 'create a digital envelope generator that should be able to ..',
  152. function() {
  153. it('throw an error when being initialized, using invalid input data',
  154. function() {
  155. expect(function() {
  156. envelopeGenerator_.init();
  157. }).toThrow();
  158. expect(function() {
  159. envelopeGenerator_.init(undefined);
  160. }).toThrow();
  161. expect(function() {
  162. envelopeGenerator_.init(null);
  163. }).toThrow();
  164. expect(function() {
  165. envelopeGenerator_.init(nonString_);
  166. }).toThrow();
  167. expect(function() {
  168. envelopeGenerator_.init(emptyString_);
  169. }).toThrow();
  170. expect(function() {
  171. envelopeGenerator_.init(PUBLIC_KEY_PEMS[0], nonString_);
  172. }).toThrow();
  173. expect(function() {
  174. envelopeGenerator_.init(PUBLIC_KEY_PEMS[0], emptyString_);
  175. }).toThrow();
  176. });
  177. it('throw an error when generating a digital envelope, using invalid input data',
  178. function() {
  179. expect(function() {
  180. envelopeGenerator_.generate();
  181. }).toThrow();
  182. expect(function() {
  183. envelopeGenerator_.generate(undefined);
  184. }).toThrow();
  185. expect(function() {
  186. envelopeGenerator_.generate(null);
  187. }).toThrow();
  188. expect(function() {
  189. envelopeGenerator_.generate(nonUint8Array_);
  190. }).toThrow();
  191. });
  192. });
  193. describe(
  194. 'create a digital envelope opener that should be able to ..',
  195. function() {
  196. it('throw an error when being initialized, using invalid input data',
  197. function() {
  198. expect(function() {
  199. envelopeOpener_.init();
  200. }).toThrow();
  201. expect(function() {
  202. envelopeOpener_.init(undefined);
  203. }).toThrow();
  204. expect(function() {
  205. envelopeOpener_.init(null);
  206. }).toThrow();
  207. expect(function() {
  208. envelopeOpener_.init(nonString_);
  209. }).toThrow();
  210. expect(function() {
  211. envelopeOpener_.init(emptyString_);
  212. }).toThrow();
  213. });
  214. it('throw an error when opening a digital envelope, using invalid input data',
  215. function() {
  216. expect(function() {
  217. envelopeOpener_.open();
  218. }).toThrow();
  219. expect(function() {
  220. envelopeOpener_.open(undefined);
  221. }).toThrow();
  222. expect(function() {
  223. envelopeOpener_.open(null);
  224. }).toThrow();
  225. expect(function() {
  226. envelopeOpener_.open(nonObject_);
  227. }).toThrow();
  228. expect(function() {
  229. envelopeOpener_.open(emptyObject_);
  230. }).toThrow();
  231. expect(function() {
  232. envelopeOpener_.open(nonDigitalEnvelope_);
  233. }).toThrow();
  234. });
  235. });
  236. });
  237. });