parse.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. const sinon = require('sinon');
  2. const APNCertificate = require('../../../lib/credentials/certificate/APNCertificate');
  3. const APNKey = require('../../../lib/credentials/certificate/APNKey');
  4. describe('parseCredentials', function () {
  5. let fakes, parseCredentials;
  6. const pfxKey = new APNKey({ n: 1, e: 1 });
  7. const pfxCert = new APNCertificate({ publicKey: {}, validity: {}, subject: {} });
  8. const pemKey = new APNKey({ n: 2, e: 1 });
  9. const pemCert = new APNCertificate({ publicKey: {}, validity: {}, subject: {} });
  10. beforeEach(function () {
  11. fakes = {
  12. parsePkcs12: sinon.stub(),
  13. parsePemKey: sinon.stub(),
  14. parsePemCert: sinon.stub(),
  15. };
  16. fakes.parsePemKey.withArgs('pemkey').returns(pemKey);
  17. fakes.parsePemKey.withArgs('pemcert').returns(pemCert);
  18. parseCredentials = require('../../../lib/credentials/certificate/parse')(fakes);
  19. });
  20. describe('with PFX file', function () {
  21. it('returns the parsed key', function () {
  22. fakes.parsePkcs12.withArgs('pfxData').returns({ key: pfxKey, certificates: [pfxCert] });
  23. const parsed = parseCredentials({ pfx: 'pfxData' });
  24. expect(parsed.key).to.be.an.instanceof(APNKey);
  25. });
  26. it('returns the parsed certificates', function () {
  27. fakes.parsePkcs12.withArgs('pfxData').returns({ key: pfxKey, certificates: [pfxCert] });
  28. const parsed = parseCredentials({ pfx: 'pfxData' });
  29. expect(parsed.certificates[0]).to.be.an.instanceof(APNCertificate);
  30. });
  31. describe('having passphrase', function () {
  32. beforeEach(function () {
  33. fakes.parsePkcs12
  34. .withArgs('encryptedPfxData', 'apntest')
  35. .returns({ key: pfxKey, certificates: [pfxCert] });
  36. fakes.parsePkcs12
  37. .throws(new Error('unable to read credentials, incorrect passphrase'));
  38. });
  39. it('returns the parsed key', function () {
  40. const parsed = parseCredentials({ pfx: 'encryptedPfxData', passphrase: 'apntest' });
  41. expect(parsed.key).to.be.an.instanceof(APNKey);
  42. });
  43. it('throws when passphrase is incorrect', function () {
  44. expect(function () {
  45. parseCredentials({ pfx: 'encryptedPfxData', passphrase: 'incorrectpassphrase' });
  46. }).to.throw(/incorrect passphrase/);
  47. });
  48. it('throws when passphrase is not supplied', function () {
  49. expect(function () {
  50. parseCredentials({ pfx: 'encryptedPfxData' });
  51. }).to.throw(/incorrect passphrase/);
  52. });
  53. });
  54. });
  55. describe('with PEM key', function () {
  56. it('returns the parsed key', function () {
  57. fakes.parsePemKey.withArgs('pemKeyData').returns(pemKey);
  58. const parsed = parseCredentials({ key: 'pemKeyData' });
  59. expect(parsed.key).to.be.an.instanceof(APNKey);
  60. });
  61. describe('having passphrase', function () {
  62. beforeEach(function () {
  63. fakes.parsePemKey.withArgs('encryptedPemKeyData', 'apntest').returns(pemKey);
  64. fakes.parsePemKey.throws(new Error('unable to load key, incorrect passphrase'));
  65. });
  66. it('returns the parsed key', function () {
  67. const parsed = parseCredentials({ key: 'encryptedPemKeyData', passphrase: 'apntest' });
  68. expect(parsed.key).to.be.an.instanceof(APNKey);
  69. });
  70. it('throws when passphrase is incorrect', function () {
  71. expect(function () {
  72. parseCredentials({ key: 'encryptedPemKeyData', passphrase: 'incorrectpassphrase' });
  73. }).to.throw(/incorrect passphrase/);
  74. });
  75. it('throws when passphrase is not supplied', function () {
  76. expect(function () {
  77. parseCredentials({ key: 'encryptedPemKeyData' });
  78. }).to.throw(/incorrect passphrase/);
  79. });
  80. });
  81. });
  82. describe('with PEM certificate', function () {
  83. it('returns the parsed certificate', function () {
  84. fakes.parsePemCert.withArgs('pemCertData').returns([pemCert]);
  85. const parsed = parseCredentials({ cert: 'pemCertData' });
  86. expect(parsed.certificates[0]).to.be.an.instanceof(APNCertificate);
  87. });
  88. });
  89. describe('both PEM and PFX data is supplied', function () {
  90. it('it prefers PFX to PEM', function () {
  91. fakes.parsePkcs12.withArgs('pfxData').returns({ key: pfxKey, certificates: [pfxCert] });
  92. fakes.parsePemKey.withArgs('pemKeyData').returns(pemKey);
  93. fakes.parsePemCert.withArgs('pemCertData').returns([pemCert]);
  94. const parsed = parseCredentials({ pfx: 'pfxData', key: 'pemKeyData', cert: 'pemCertData' });
  95. expect(parsed.key).to.equal(pfxKey);
  96. expect(parsed.certificates[0]).to.equal(pfxCert);
  97. });
  98. });
  99. });