2
0

input-validation.spec.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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 certificate = require('../lib/index');
  11. describe('The certificate module should be able to ...', function() {
  12. var certificatePem_ =
  13. '-----BEGIN CERTIFICATE-----MIIEKjCCAxKgAwIBAgIQQtWFdPN4NAvUIWyyJyUlbTANBgkqhkiG9w0BAQsFADBUMRAwDgYDVQQDDAdSb290IENOMRYwFAYDVQQLDA1Sb290IE9yZyBVbml0MREwDwYDVQQKDAhSb290IE9yZzEVMBMGA1UEBhMMUm9vdCBDb3VudHJ5MB4XDTE0MDYxODEwMjMyOFoXDTE1MDYxODEwMjMyOFowVDEQMA4GA1UEAwwHUm9vdCBDTjEWMBQGA1UECwwNUm9vdCBPcmcgVW5pdDERMA8GA1UECgwIUm9vdCBPcmcxFTATBgNVBAYTDFJvb3QgQ291bnRyeTCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBAJ4AidkWId1zr4IZgItjE9uv38eB8fIGJGEts2n/XNh/EJ7NYZyBzvKSJR83v7LVJjc4pup5crWyS0B5lQ7uuD3/XZ9QpQFtnGiqlKOKH45zsw3ekaHEAco07L2dZznBuQTGLTlhrmvkCFRa8b8WYC+k90oUPvSd/9S4kA1Jlo9JDKHLer0SbjZCcVRXoLSBKmGWE0xSNfmNuZNONDRbtHvSA8A10AOtxKii9w464MXYzmPil7uM1Og1HC+FXCmkzLNfqQ31Om0jra3nLmrCpBJjRPX7svVnoxajRqpazVQnmJjjpzV7yNLwnR9W8OPwqanXcbxmTkrXMxfLxiVXDFUCAwEAAaOB9zCB9DAPBgNVHRMBAf8EBTADAQH/MDUGCCsGAQUFBwEBAQH/BCYwJDAiBggrBgEFBQcwAYYWaHR0cDovL29jc3AudGhhd3RlLmNvbTA0BgNVHR8BAf8EKjAoMCagJKAihiBodHRwOi8vY3JsLnZlcmlzaWduLmNvbS9wY2EzLmNybDArBgNVHREBAf8EITAfpB0wGzEZMBcGA1UEAwwQUHJpdmF0ZUxhYmVsMy0xNTAOBgNVHQ8BAf8EBAMCAQYwNwYDVR0lAQH/BC0wKwYIKwYBBQUHAwEGCCsGAQUFBwMCBgpghkgBhvhFAQgBBglghkgBhvhCBAEwDQYJKoZIhvcNAQELBQADggEBADmtmjApZAXIkGLaZCdkRnhel53BtEdQnG990Oo/tBBboqy2ipum9ByTj3hNWJB3zuPN77rkrek9rbookNcCgVWhHtTk1lUpUK6ZohDsZh8k0MqIhkz+X+HiWGRsEOptjsCaknyWcWb4aXAevMAQMPm/ktkpQ8AOxAq+gtieewWQZP3kGPhBBCfn8TGjdrn9+ymf8EIbAUFXQ8m+oWeNlrdWhqzRXwQbj4EDds1kZdTo0nCYUdH+XEBF9nMyhAxSQWzCKQQTRFWv1dr3dKapzfgrdH8wEgvptiBYCY62O5+3DxiNK/VWquHz6S5GqIwkmSPDPMUU/qK3SNG3xIL1U1k=-----END CERTIFICATE-----';
  14. var validationData_ = {
  15. issuer: {
  16. commonName: 'Test CN',
  17. organizationalUnit: 'Test Org Unit',
  18. organization: 'Test Org',
  19. country: 'Test Country'
  20. }
  21. };
  22. var nonString_ = 999;
  23. var emptyString_ = [];
  24. var nonPemString_ = 'Not a PEM string';
  25. var emptyObject_ = {};
  26. var nonTwoDimensionalArray_ = '';
  27. var emptyTwoDimensionalArray_ = [];
  28. var certificateService_;
  29. var certificateValidator_;
  30. beforeAll(function() {
  31. certificateService_ = certificate.newService();
  32. certificateValidator_ = certificateService_.newValidator();
  33. });
  34. describe('create a certificate service that should be able to ..', function() {
  35. it('throw an error when creating a new X509Certificate object, using invalid input data',
  36. function() {
  37. expect(function() {
  38. certificateService_.newX509Certificate();
  39. }).toThrow();
  40. expect(function() {
  41. certificateService_.newX509Certificate(undefined);
  42. }).toThrow();
  43. expect(function() {
  44. certificateService_.newX509Certificate(null);
  45. }).toThrow();
  46. expect(function() {
  47. certificateService_.newX509Certificate(nonString_);
  48. }).toThrow();
  49. expect(function() {
  50. certificateService_.newX509Certificate(emptyString_);
  51. }).toThrow();
  52. expect(function() {
  53. certificateService_.newX509Certificate(nonPemString_);
  54. }).toThrow();
  55. });
  56. describe(
  57. 'create a new X509Certificate object that should be able to',
  58. function() {
  59. it('throw an error when verifying the signature of another X.509 certificate, using invalid input data',
  60. function() {
  61. var certificate =
  62. certificateService_.newX509Certificate(certificatePem_);
  63. expect(function() {
  64. certificate.verify();
  65. }).toThrow();
  66. expect(function() {
  67. certificate.verify(undefined);
  68. }).toThrow();
  69. expect(function() {
  70. certificate.verify(null);
  71. }).toThrow();
  72. expect(function() {
  73. certificate.verify(nonString_);
  74. }).toThrow();
  75. expect(function() {
  76. certificate.verify(nonPemString_);
  77. }).toThrow();
  78. });
  79. });
  80. describe(
  81. 'create a CertificateValidator object that should be able to ..',
  82. function() {
  83. it('throw an error when validating a certificate, using an invalid certificate',
  84. function() {
  85. expect(function() {
  86. certificateValidator_.validate(undefined, validationData_);
  87. }).toThrow();
  88. expect(function() {
  89. certificateValidator_.validate(null, validationData_);
  90. }).toThrow();
  91. expect(function() {
  92. certificateValidator_.validate(nonString_, validationData_);
  93. }).toThrow();
  94. expect(function() {
  95. certificateValidator_.validate(emptyString_, validationData_);
  96. }).toThrow();
  97. expect(function() {
  98. certificateValidator_.validate(nonPemString_, validationData_);
  99. }).toThrow();
  100. });
  101. it('throw an error when validating a certificate, using invalid validation data',
  102. function() {
  103. expect(function() {
  104. certificateValidator_.validate(certificatePem_);
  105. }).toThrow();
  106. expect(function() {
  107. certificateValidator_.validate(certificatePem_, undefined);
  108. }).toThrow();
  109. expect(function() {
  110. certificateValidator_.validate(certificatePem_, null);
  111. }).toThrow();
  112. expect(function() {
  113. certificateValidator_.validate(certificatePem_, emptyObject_);
  114. }).toThrow();
  115. });
  116. it('throw an error when validating a certificate chain, using invalid input data',
  117. function() {
  118. expect(function() {
  119. certificateValidator_.validateChain();
  120. }).toThrow();
  121. expect(function() {
  122. certificateValidator_.validateChain(undefined);
  123. }).toThrow();
  124. expect(function() {
  125. certificateValidator_.validateChain(null);
  126. }).toThrow();
  127. expect(function() {
  128. certificateValidator_.validateChain(emptyObject_);
  129. }).toThrow();
  130. });
  131. it('throw an error when flattening the failed validations of a certificate chain, using invalid input data',
  132. function() {
  133. expect(function() {
  134. certificateValidator_.flattenFailedValidations();
  135. }).toThrow();
  136. expect(function() {
  137. certificateValidator_.flattenFailedValidations(undefined);
  138. }).toThrow();
  139. expect(function() {
  140. certificateValidator_.flattenFailedValidations(null);
  141. }).toThrow();
  142. expect(function() {
  143. certificateValidator_.flattenFailedValidations(
  144. nonTwoDimensionalArray_);
  145. }).toThrow();
  146. expect(function() {
  147. certificateValidator_.flattenFailedValidations(
  148. emptyTwoDimensionalArray_);
  149. }).toThrow();
  150. });
  151. });
  152. });
  153. });