-
Notifications
You must be signed in to change notification settings - Fork 74
/
tests.js
executable file
·80 lines (63 loc) · 2.1 KB
/
tests.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/*
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) {
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
*/
var requiredFields = ['password', 'publicKey', 'contact', 'peerName'];
var recommendedFields = ['gpg', 'peerName'];
var insufficientFields = Peers.filter(function (x, p) {
var problem = false;
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'";
Object.keys(x).forEach(function (k) {
if (Array.isArray(x[k])) { return; }
var cred = x[k];
var fields = Object.keys(cred);
recommendedFields.forEach(function (field) {
if (typeof(cred[field]) !== 'undefined') { return; }
console.log(recommendedMsg, path, k, field);
comment = true;
problem = true;
});
requiredFields.forEach(function (field) {
if (typeof(cred[field]) !== 'undefined') { return; }
console.error(requiredMsg, path, k, field);
problem = true;
})
});
//if (comment || problem) { console.log(); }
return problem;
});
if (insufficientFields.length) {
//console.log("The following peers did not have all the required fields");
//console.log(insufficientFields);
}