parsePemCertificate.js 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. const parsePemCertificate = require('../../../lib/credentials/certificate/parsePemCertificate');
  2. const APNCertificate = require('../../../lib/credentials/certificate/APNCertificate');
  3. const fs = require('fs');
  4. describe('parsePemCertificate', function () {
  5. describe('with PEM certificate', function () {
  6. let cert, certProperties;
  7. before(function () {
  8. cert = fs.readFileSync('test/credentials/support/cert.pem');
  9. });
  10. beforeEach(function () {
  11. certProperties = parsePemCertificate(cert);
  12. });
  13. describe('return value', function () {
  14. it('is an array', function () {
  15. expect(certProperties).to.be.an('array');
  16. });
  17. it('contains one element', function () {
  18. expect(certProperties).to.have.length(1);
  19. });
  20. describe('certificate [0]', function () {
  21. it('is an APNCertificate', function () {
  22. expect(certProperties[0]).to.be.an.instanceof(APNCertificate);
  23. });
  24. it('has the correct fingerprint', function () {
  25. expect(certProperties[0].key().fingerprint()).to.equal(
  26. '2d594c9861227dd22ba5ae37cc9354e9117a804d'
  27. );
  28. });
  29. });
  30. });
  31. });
  32. describe('with PEM containing multiple certificates', function () {
  33. let cert, certProperties;
  34. before(function () {
  35. cert = fs.readFileSync('test/credentials/support/certIssuerKey.pem');
  36. });
  37. beforeEach(function () {
  38. certProperties = parsePemCertificate(cert);
  39. });
  40. it('returns the correct number of certificates', function () {
  41. expect(certProperties).to.have.length(2);
  42. });
  43. describe('certificate [0]', function () {
  44. it('has the correct fingerprint', function () {
  45. expect(certProperties[0].key().fingerprint()).to.equal(
  46. '2d594c9861227dd22ba5ae37cc9354e9117a804d'
  47. );
  48. });
  49. });
  50. describe('certificate [1]', function () {
  51. it('has the correct fingerprint', function () {
  52. expect(certProperties[1].key().fingerprint()).to.equal(
  53. 'ccff221d67cb3335649f9b4fbb311948af76f4b2'
  54. );
  55. });
  56. });
  57. });
  58. describe('with a PKCS#12 file', function () {
  59. it('throws', function () {
  60. const pfx = fs.readFileSync('test/credentials/support/certIssuerKey.p12');
  61. expect(function () {
  62. parsePemCertificate(pfx);
  63. }).to.throw('unable to parse certificate, not a valid PEM file');
  64. });
  65. });
  66. describe('with a key', function () {
  67. it('returns an empty array', function () {
  68. const key = fs.readFileSync('test/credentials/support/key.pem');
  69. expect(parsePemCertificate(key)).to.be.empty;
  70. });
  71. });
  72. describe('returns null', function () {
  73. it('for null', function () {
  74. expect(parsePemCertificate(null)).to.be.null;
  75. });
  76. it('for undefined', function () {
  77. expect(parsePemCertificate(undefined)).to.be.null;
  78. });
  79. });
  80. });