Repositorio del curso CCOM4030 el semestre B91 del proyecto Artesanías con el Instituto de Cultura

test-keyparser.js 4.6KB

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