common-validation.spec.js 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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 ValidationTestData = require('../data/validation-data');
  11. var zkProof = require('../../lib/index');
  12. describe('The zero-knowledge proof module should be able to ...', function() {
  13. var proofService_;
  14. var nonObject_;
  15. var emptyObject_;
  16. var nonPolicy_;
  17. var nonSecureRandomService_;
  18. var nonMathematicalService_;
  19. var nonMessageDigestService_;
  20. var nonString_;
  21. var nonJsonString_;
  22. var nonArray_;
  23. var emptyArray_;
  24. var nonObjectArray_;
  25. var nonProofObject_;
  26. var hash_;
  27. var values_;
  28. var exponents_;
  29. var phiOutputs_;
  30. beforeAll(function() {
  31. proofService_ = zkProof.newService();
  32. var testData = new ValidationTestData();
  33. nonObject_ = testData.getNonObject();
  34. emptyObject_ = testData.getEmptyObject();
  35. nonPolicy_ = testData.getNonPolicy();
  36. nonSecureRandomService_ = testData.getNonSecureRandomService();
  37. nonMathematicalService_ = testData.getNonMathematicalService();
  38. nonMessageDigestService_ = testData.getNonMessageDigestService();
  39. nonString_ = testData.getNonString();
  40. nonJsonString_ = testData.getNonJsonString();
  41. nonArray_ = testData.getNonArray();
  42. emptyArray_ = testData.getEmptyArray();
  43. nonObjectArray_ = testData.getNonObjectArray();
  44. nonProofObject_ = testData.getNonProofObject();
  45. hash_ = testData.getProofHash();
  46. values_ = testData.getProofValues();
  47. exponents_ = testData.getPreComputationExponents();
  48. phiOutputs_ = testData.getPreComputationPhiOutputs();
  49. });
  50. describe('create a zero-knowledge proof service that should be able to ..', function() {
  51. it('throw an error when being created, using an invalid cryptographic policy',
  52. function() {
  53. expect(function() {
  54. Object.create(zkProof.newService({policy: null}));
  55. }).toThrow();
  56. expect(function() {
  57. Object.create(zkProof.newService({policy: nonObject_}));
  58. }).toThrow();
  59. expect(function() {
  60. Object.create(zkProof.newService({policy: emptyObject_}));
  61. }).toThrow();
  62. });
  63. it('throw an error when being created, using an invalid secure random service object',
  64. function() {
  65. expect(function() {
  66. Object.create(zkProof.newService({secureRandomService: null}));
  67. }).toThrow();
  68. expect(function() {
  69. Object.create(zkProof.newService({secureRandomService: nonObject_}));
  70. }).toThrow();
  71. expect(function() {
  72. Object.create(
  73. zkProof.newService({secureRandomService: emptyObject_}));
  74. }).toThrow();
  75. });
  76. it('throw an error when being created, using an invalid mathematical service object',
  77. function() {
  78. expect(function() {
  79. Object.create(zkProof.newService({mathematicalService: null}));
  80. }).toThrow();
  81. expect(function() {
  82. Object.create(zkProof.newService({mathematicalService: nonObject_}));
  83. }).toThrow();
  84. expect(function() {
  85. Object.create(
  86. zkProof.newService({mathematicalService: emptyObject_}));
  87. }).toThrow();
  88. });
  89. it('throw an error when being created, using an invalid message digest service object',
  90. function() {
  91. expect(function() {
  92. Object.create(zkProof.newService({messageDigestService: null}));
  93. }).toThrow();
  94. expect(function() {
  95. Object.create(
  96. zkProof.newService({messageDigestService: nonObject_}));
  97. }).toThrow();
  98. expect(function() {
  99. Object.create(
  100. zkProof.newService({messageDigestService: emptyObject_}));
  101. }).toThrow();
  102. });
  103. it('throw an error when creating a new ZeroKnowledgeProof object, using invalid input data',
  104. function() {
  105. expect(function() {
  106. proofService_.newProof(undefined, values_);
  107. }).toThrow();
  108. expect(function() {
  109. proofService_.newProof(null, values_);
  110. }).toThrow();
  111. expect(function() {
  112. proofService_.newProof(nonObject_, values_);
  113. }).toThrow();
  114. expect(function() {
  115. proofService_.newProof(emptyObject_, values_);
  116. }).toThrow();
  117. expect(function() {
  118. proofService_.newProof(hash_);
  119. }).toThrow();
  120. expect(function() {
  121. proofService_.newProof(hash_, undefined);
  122. }).toThrow();
  123. expect(function() {
  124. proofService_.newProof(hash_, null);
  125. }).toThrow();
  126. expect(function() {
  127. proofService_.newProof(hash_, nonArray_);
  128. }).toThrow();
  129. expect(function() {
  130. proofService_.newProof(hash_, emptyArray_);
  131. }).toThrow();
  132. expect(function() {
  133. proofService_.newProof(hash_, nonObjectArray_);
  134. }).toThrow();
  135. });
  136. it('throw an error when creating a new ZeroKnowledgeProofPreComputation object, using invalid input data',
  137. function() {
  138. expect(function() {
  139. proofService_.newPreComputation(undefined, phiOutputs_);
  140. }).toThrow();
  141. expect(function() {
  142. proofService_.newPreComputation(null, phiOutputs_);
  143. }).toThrow();
  144. expect(function() {
  145. proofService_.newPreComputation(nonArray_, phiOutputs_);
  146. }).toThrow();
  147. expect(function() {
  148. proofService_.newPreComputation(emptyArray_, phiOutputs_);
  149. }).toThrow();
  150. expect(function() {
  151. proofService_.newPreComputation(nonObjectArray_, phiOutputs_);
  152. }).toThrow();
  153. expect(function() {
  154. proofService_.newPreComputation(exponents_);
  155. }).toThrow();
  156. expect(function() {
  157. proofService_.newPreComputation(exponents_, undefined);
  158. }).toThrow();
  159. expect(function() {
  160. proofService_.newPreComputation(exponents_, null);
  161. }).toThrow();
  162. expect(function() {
  163. proofService_.newPreComputation(exponents_, nonArray_);
  164. }).toThrow();
  165. expect(function() {
  166. proofService_.newPreComputation(exponents_, emptyArray_);
  167. }).toThrow();
  168. expect(function() {
  169. proofService_.newPreComputation(exponents_, nonObjectArray_);
  170. }).toThrow();
  171. });
  172. it('throw an error when serializing or deserializing a ZeroKnowledgeProof object, using invalid input data',
  173. function() {
  174. expect(function() {
  175. proofService_.proofToJson();
  176. }).toThrow();
  177. expect(function() {
  178. proofService_.proofToJson(undefined);
  179. }).toThrow();
  180. expect(function() {
  181. proofService_.proofToJson(null);
  182. }).toThrow();
  183. expect(function() {
  184. proofService_.proofToJson(nonProofObject_);
  185. }).toThrow();
  186. expect(function() {
  187. proofService_.jsonToProof(undefined);
  188. }).toThrow();
  189. expect(function() {
  190. proofService_.jsonToProof(null);
  191. }).toThrow();
  192. expect(function() {
  193. proofService_.jsonToProof(nonString_);
  194. }).toThrow();
  195. expect(function() {
  196. proofService_.jsonToProof(nonJsonString_);
  197. }).toThrow();
  198. });
  199. it('throw an error when serializing or deserializing a ZeroKnowledgeProofPreComputation object, using invalid input data',
  200. function() {
  201. expect(function() {
  202. proofService_.preComputationToJson();
  203. }).toThrow();
  204. expect(function() {
  205. proofService_.preComputationToJson(undefined);
  206. }).toThrow();
  207. expect(function() {
  208. proofService_.preComputationToJson(null);
  209. }).toThrow();
  210. expect(function() {
  211. proofService_.preComputationToJson(nonProofObject_);
  212. }).toThrow();
  213. expect(function() {
  214. proofService_.jsonToPreComputation(undefined);
  215. }).toThrow();
  216. expect(function() {
  217. proofService_.jsonToPreComputation(null);
  218. }).toThrow();
  219. expect(function() {
  220. proofService_.jsonToPreComputation(nonString_);
  221. }).toThrow();
  222. expect(function() {
  223. proofService_.jsonToPreComputation(nonJsonString_);
  224. }).toThrow();
  225. });
  226. });
  227. });