tests.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /*
  2. if any of the supplied credentials:
  3. * are invalid JSON
  4. * do not end with a newline character,
  5. this file will not even load because index.js will throw errors
  6. */
  7. var Peers = require("./index");
  8. var isIp = function (host) {
  9. return ([
  10. /^[\[\]0-9a-f:]*$/i, // ipv6
  11. /^[0-9\.:]*$/, // ipv4
  12. ].some(function (patt) {
  13. return patt.test(host);
  14. }));
  15. };
  16. var credsWithDns = Peers.filter(function (x, p) {
  17. return Object.keys(x).some(function (k) {
  18. if (Array.isArray(x[k])) { return; }
  19. return !isIp(k);
  20. });
  21. });
  22. /* Credentials should use IPs, not dns hostnames */
  23. if (credsWithDns.length) {
  24. console.log("The following peers are using DNS hostnames instead of IPs");
  25. console.log(credsWithDns);
  26. }
  27. /* Credentials must have the required fields:
  28. * ip/port ✓
  29. * password
  30. * publicKey
  31. * contact
  32. */
  33. var requiredFields = ['password', 'publicKey', 'contact', 'peerName'];
  34. var recommendedFields = ['gpg', 'peerName'];
  35. var insufficientFields = Peers.filter(function (x, p) {
  36. var problem = false;
  37. var comment = false;
  38. var path = '/' + p.join('/');
  39. var requiredMsg = "[%s] => %s is missing the required field '%s'";
  40. var recommendedMsg = "[%s] => '%s' is missing the recommended field '%s'";
  41. Object.keys(x).forEach(function (k) {
  42. if (Array.isArray(x[k])) { return; }
  43. var cred = x[k];
  44. var fields = Object.keys(cred);
  45. recommendedFields.forEach(function (field) {
  46. if (typeof(cred[field]) !== 'undefined') { return; }
  47. console.log(recommendedMsg, path, k, field);
  48. comment = true;
  49. problem = true;
  50. });
  51. requiredFields.forEach(function (field) {
  52. if (typeof(cred[field]) !== 'undefined') { return; }
  53. console.error(requiredMsg, path, k, field);
  54. problem = true;
  55. })
  56. });
  57. //if (comment || problem) { console.log(); }
  58. return problem;
  59. });
  60. if (insufficientFields.length) {
  61. //console.log("The following peers did not have all the required fields");
  62. //console.log(insufficientFields);
  63. }