5
0
mirror of https://github.com/cwinfo/hyperboria-peers.git synced 2024-11-14 03:20:27 +00:00
hyperboria-peers/tests.js

81 lines
2.1 KiB
JavaScript
Raw Normal View History

/*
if any of the supplied credentials:
* are invalid JSON
* do not end with a newline character,
this file will not even load because index.js will throw errors
*/
var Peers = require("./index");
var isIp = function (host) {
return ([
/^[\[\]0-9a-f:]*$/i, // ipv6
/^[0-9\.:]*$/, // ipv4
].some(function (patt) {
return patt.test(host);
}));
};
var credsWithDns = Peers.filter(function (x, p) {
return Object.keys(x).some(function (k) {
2017-08-24 06:43:59 +00:00
if (Array.isArray(x[k])) { return; }
return !isIp(k);
});
});
/* Credentials should use IPs, not dns hostnames */
if (credsWithDns.length) {
console.log("The following peers are using DNS hostnames instead of IPs");
console.log(credsWithDns);
}
/* Credentials must have the required fields:
* ip/port
* password
* publicKey
* contact
*/
2017-12-22 07:37:10 +00:00
var requiredFields = ['password', 'publicKey', 'contact', 'peerName'];
var recommendedFields = ['gpg', 'peerName'];
var insufficientFields = Peers.filter(function (x, p) {
var problem = false;
2016-09-29 07:16:25 +00:00
var comment = false;
var path = '/' + p.join('/');
var requiredMsg = "[%s] => %s is missing the required field '%s'";
var recommendedMsg = "[%s] => '%s' is missing the recommended field '%s'";
2017-08-24 06:43:59 +00:00
Object.keys(x).forEach(function (k) {
if (Array.isArray(x[k])) { return; }
var cred = x[k];
var fields = Object.keys(cred);
2016-09-29 07:16:25 +00:00
recommendedFields.forEach(function (field) {
if (typeof(cred[field]) !== 'undefined') { return; }
console.log(recommendedMsg, path, k, field);
comment = true;
problem = true;
});
2016-09-29 07:16:25 +00:00
requiredFields.forEach(function (field) {
if (typeof(cred[field]) !== 'undefined') { return; }
console.error(requiredMsg, path, k, field);
problem = true;
})
});
2016-09-29 07:16:25 +00:00
//if (comment || problem) { console.log(); }
return problem;
});
if (insufficientFields.length) {
2016-09-29 07:16:25 +00:00
//console.log("The following peers did not have all the required fields");
//console.log(insufficientFields);
}