tests.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. return !isIp(k);
  19. });
  20. });
  21. /* Credentials should use IPs, not dns hostnames */
  22. if (credsWithDns.length) {
  23. console.log("The following peers are using DNS hostnames instead of IPs");
  24. console.log(credsWithDns);
  25. }
  26. /* Credentials must have the required fields:
  27. * ip/port ✓
  28. * password
  29. * publicKey
  30. * contact
  31. */
  32. var requiredFields = ['password', 'publicKey', 'contact'];
  33. var recommendedFields = ['gpg', 'peerName'];
  34. var insufficientFields = Peers.filter(function (x, p) {
  35. var problem = false;
  36. var comment = false;
  37. var path = '/' + p.join('/');
  38. var requiredMsg = "[%s] => %s is missing the required field '%s'";
  39. var recommendedMsg = "[%s] => '%s' is missing the recommended field '%s'";
  40. Object.keys(x).map(function (k) {
  41. var cred = x[k];
  42. var fields = Object.keys(cred);
  43. recommendedFields.forEach(function (field) {
  44. if (typeof(cred[field]) !== 'undefined') { return; }
  45. console.log(recommendedMsg, path, k, field);
  46. comment = true;
  47. problem = true;
  48. });
  49. requiredFields.forEach(function (field) {
  50. if (typeof(cred[field]) !== 'undefined') { return; }
  51. console.error(requiredMsg, path, k, field);
  52. problem = true;
  53. })
  54. });
  55. //if (comment || problem) { console.log(); }
  56. return problem;
  57. });
  58. if (insufficientFields.length) {
  59. //console.log("The following peers did not have all the required fields");
  60. //console.log(insufficientFields);
  61. }