tests.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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 insufficientFields = Peers.filter(function (x, p) {
  34. var problem = false;
  35. Object.keys(x).map(function (k) {
  36. var cred = x[k];
  37. var fields = Object.keys(cred);
  38. requiredFields.forEach(function (field) {
  39. if (fields.indexOf(field) === -1) {
  40. problem = true;
  41. }
  42. });
  43. });
  44. return problem;
  45. });
  46. if (insufficientFields.length) {
  47. console.log("The following peers did not have all the required fields");
  48. console.log(insufficientFields);
  49. }