test-keyparser.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. var parseKey = require('../lib/keyParser').parseKey;
  2. var path = require('path');
  3. var assert = require('assert');
  4. var inspect = require('util').inspect;
  5. var fs = require('fs');
  6. function failMsg(name, message, exit) {
  7. var msg = '[' + name + '] ' + message;
  8. if (!exit)
  9. return msg;
  10. console.error(msg);
  11. process.exit(1);
  12. }
  13. fs.readdirSync(__dirname + '/fixtures').forEach(function(name) {
  14. if (/\.result$/i.test(name))
  15. return;
  16. var isPublic = /\.pub$/i.test(name);
  17. var isEncrypted = /_enc/i.test(name);
  18. var isPPK = /^ppk_/i.test(name);
  19. var key = fs.readFileSync(__dirname + '/fixtures/' + name);
  20. var res;
  21. if (isEncrypted)
  22. res = parseKey(key, (isPPK ? 'node.js' : 'password'));
  23. else
  24. res = parseKey(key);
  25. var expected = JSON.parse(
  26. fs.readFileSync(__dirname + '/fixtures/' + name + '.result', 'utf8')
  27. );
  28. if (typeof expected === 'string') {
  29. if (!(res instanceof Error))
  30. failMsg(name, 'Expected error: ' + expected, true);
  31. assert.strictEqual(expected,
  32. res.message,
  33. failMsg(name,
  34. 'Error message mismatch.\n'
  35. + 'Expected: ' + inspect(expected) + '\n'
  36. + 'Received: ' + inspect(res.message)));
  37. } else if (res instanceof Error) {
  38. failMsg(name, 'Unexpected error: ' + res.stack, true);
  39. } else {
  40. if (Array.isArray(expected) && !Array.isArray(res))
  41. failMsg(name, 'Expected array but did not receive one', true);
  42. if (!Array.isArray(expected) && Array.isArray(res))
  43. failMsg(name, 'Received array but did not expect one', true);
  44. if (!Array.isArray(res)) {
  45. res = [res];
  46. expected = [expected];
  47. } else if (res.length !== expected.length) {
  48. failMsg(name,
  49. 'Expected ' + expected.length + ' keys, but received '
  50. + res.length,
  51. true);
  52. }
  53. res.forEach((curKey, i) => {
  54. var details = {
  55. type: curKey.type,
  56. comment: curKey.comment,
  57. public: curKey.getPublicPEM(),
  58. publicSSH: curKey.getPublicSSH()
  59. && curKey.getPublicSSH().toString('base64'),
  60. private: curKey.getPrivatePEM()
  61. };
  62. assert.deepEqual(details,
  63. expected[i],
  64. failMsg(name,
  65. 'Parser output mismatch.\n'
  66. + 'Expected: ' + inspect(expected[i])
  67. + '\n\nReceived: ' + inspect(details)));
  68. });
  69. }
  70. if (isEncrypted && !isPublic) {
  71. // Make sure parsing encrypted keys without a passhprase or incorrect
  72. // passphrase results in an appropriate error
  73. var err = parseKey(key);
  74. if (!(err instanceof Error))
  75. failMsg(name, 'Expected error during parse without passphrase', true);
  76. if (!/no passphrase/i.test(err.message)) {
  77. failMsg(name,
  78. 'Unexpected error during parse without passphrase: '
  79. + err.message,
  80. true);
  81. }
  82. }
  83. if (!isPublic) {
  84. // Try signing and verifying to make sure the private/public key PEMs are
  85. // correct
  86. var data = Buffer.from('hello world');
  87. res.forEach((curKey) => {
  88. var sig = curKey.sign(data);
  89. if (sig instanceof Error) {
  90. failMsg(name,
  91. 'Error while signing data with key: ' + sig.message,
  92. true);
  93. }
  94. var verified = curKey.verify(data, sig);
  95. if (verified instanceof Error) {
  96. failMsg(name,
  97. 'Error while verifying signed data with key: '
  98. + verified.message,
  99. true);
  100. }
  101. if (!verified)
  102. failMsg(name, 'Failed to verify signed data with key', true);
  103. });
  104. if (res.length === 1 && !isPPK) {
  105. var pubFile = fs.readFileSync(__dirname + '/fixtures/' + name + '.pub');
  106. var pubParsed = parseKey(pubFile);
  107. if (!(pubParsed instanceof Error)) {
  108. var sig = res[0].sign(data);
  109. if (sig instanceof Error) {
  110. failMsg(name,
  111. 'Error while signing data with key: ' + sig.message,
  112. true);
  113. }
  114. var verified = pubParsed.verify(data, sig);
  115. if (verified instanceof Error) {
  116. failMsg(name,
  117. 'Error while verifying signed data with separate public key: '
  118. + verified.message,
  119. true);
  120. }
  121. if (!verified) {
  122. failMsg(name,
  123. 'Failed to verify signed data with separate public key',
  124. true);
  125. }
  126. }
  127. }
  128. }
  129. });