v3.1.1: perform all sets, then all gets, then all removes

This commit is contained in:
AJ ONeal 2019-06-07 00:14:10 -06:00
parent 17e409b158
commit ea508c68f4
2 changed files with 171 additions and 141 deletions

116
index.js
View File

@ -54,28 +54,40 @@ function promiseCheckAndCatch(obj, name) {
}; };
} }
function mapAsync(els, doer) {
els = els.slice(0);
var results = [];
function next() {
var el = els.shift();
if (!el) {
return results;
}
return doer(el).then(function(result) {
results.push(result);
return next();
});
}
return next();
}
// Here's the meat, where the tests are happening: // Here's the meat, where the tests are happening:
function run(challenger, opts) { function testEach(type, domains, challenger) {
var chr = wrapChallenger(challenger);
var all = domains.map(function(d) {
return { domain: d };
});
return mapAsync(all, function(opts) {
console.info("TEST '%s'", opts.domain);
opts.challenge = getChallenge(type, opts.domain);
var ch = opts.challenge; var ch = opts.challenge;
if ('http-01' === ch.type && ch.wildname) { if ('http-01' === ch.type && ch.wildname) {
throw new Error('http-01 cannot be used for wildcard domains'); throw new Error('http-01 cannot be used for wildcard domains');
} }
var set = promiseCheckAndCatch(challenger, 'set');
if ('function' !== typeof challenger.get) {
throw new Error(
"'challenge.get' should be implemented for the sake of testing." +
' It should be implemented as the internal method for fetching the challenge' +
' (i.e. reading from a database, file system or API, not return internal),' +
' not the external check (the http call, dns query, etc), which will already be done as part of this test.'
);
}
var get = promiseCheckAndCatch(challenger, 'get');
var remove = promiseCheckAndCatch(challenger, 'remove');
// The first time we just check it against itself // The first time we just check it against itself
// this will cause the prompt to appear // this will cause the prompt to appear
return set(opts).then(function() { return chr.set(opts).then(function() {
// this will cause the final completion message to appear // this will cause the final completion message to appear
// _test is used by the manual cli reference implementations // _test is used by the manual cli reference implementations
var query = { type: ch.type, /*debug*/ status: ch.status, _test: true }; var query = { type: ch.type, /*debug*/ status: ch.status, _test: true };
@ -96,8 +108,14 @@ function run(challenger, opts) {
query = JSON.parse(JSON.stringify(ch)); query = JSON.parse(JSON.stringify(ch));
query.comment = 'unknown challenge type, supplying everything'; query.comment = 'unknown challenge type, supplying everything';
} }
return get({ challenge: query }) opts.query = query;
.then(function(secret) { return opts;
});
})
.then(function(all) {
return mapAsync(all, function(opts) {
var ch = opts.challenge;
return chr.get({ challenge: opts.query }).then(function(secret) {
if ('string' === typeof secret) { if ('string' === typeof secret) {
console.info( console.info(
'secret was passed as a string, which works historically, but should be an object instead:' 'secret was passed as a string, which works historically, but should be an object instead:'
@ -159,10 +177,13 @@ function run(challenger, opts) {
); );
} }
} }
});
});
}) })
.then(function() { .then(function() {
return remove(opts).then(function() { return mapAsync(all, function(opts) {
return get(opts).then(function(result) { return chr.remove(opts).then(function() {
return chr.get(opts).then(function(result) {
if (result) { if (result) {
throw new Error( throw new Error(
'challenge.remove() should have made it not possible for challenge.get() to return a value' 'challenge.remove() should have made it not possible for challenge.get() to return a value'
@ -173,31 +194,12 @@ function run(challenger, opts) {
'challenge.get() should return null when the value is not set' 'challenge.get() should return null when the value is not set'
); );
} }
console.info("PASS '%s'", opts.domain);
}); });
}); });
}); });
}); })
} .then(function() {
function testZone(type, zone, challenger) {
var domains = [zone, 'foo.' + zone];
if ('dns-01' === type) {
domains.push('*.foo.' + zone);
}
function next() {
var domain = domains.shift();
if (!domain) {
return;
}
console.info("TEST '%s'", domain);
return testRecord(type, domain, challenger).then(function() {
console.info("PASS '%s'", domain);
return next();
});
}
return next().then(function() {
console.info('All soft tests: PASS'); console.info('All soft tests: PASS');
console.warn( console.warn(
'Hard tests (actually checking http URLs and dns records) is implemented in acme-v2.' 'Hard tests (actually checking http URLs and dns records) is implemented in acme-v2.'
@ -206,9 +208,33 @@ function testZone(type, zone, challenger) {
"We'll copy them over here as well, but that's a TODO for next week." "We'll copy them over here as well, but that's a TODO for next week."
); );
}); });
}; }
function testRecord(type, altname, challenger) { function testZone(type, zone, challenger) {
var domains = [zone, 'foo.' + zone];
if ('dns-01' === type) {
domains.push('*.foo.' + zone);
}
return testEach(type, domains, challenger);
}
function wrapChallenger(challenger) {
var set = promiseCheckAndCatch(challenger, 'set');
if ('function' !== typeof challenger.get) {
throw new Error(
"'challenge.get' should be implemented for the sake of testing." +
' It should be implemented as the internal method for fetching the challenge' +
' (i.e. reading from a database, file system or API, not return internal),' +
' not the external check (the http call, dns query, etc), which will already be done as part of this test.'
);
}
var get = promiseCheckAndCatch(challenger, 'get');
var remove = promiseCheckAndCatch(challenger, 'remove');
return { set: set, get: get, remove: remove };
}
function getChallenge(type, altname) {
var expires = new Date(Date.now() + 10 * 60 * 1000).toISOString(); var expires = new Date(Date.now() + 10 * 60 * 1000).toISOString();
var token = crypto.randomBytes(8).toString('hex'); var token = crypto.randomBytes(8).toString('hex');
var thumb = crypto.randomBytes(16).toString('hex'); var thumb = crypto.randomBytes(16).toString('hex');
@ -245,7 +271,11 @@ function testRecord(type, altname, challenger) {
'http://' + altname + '/.well-known/acme-challenge/' + challenge.token; 'http://' + altname + '/.well-known/acme-challenge/' + challenge.token;
challenge.dnsHost += altname; challenge.dnsHost += altname;
return run(challenger, { challenge: challenge }); return challenge;
}
function testRecord(type, altname, challenger) {
return testEach(type, [altname], challenger);
} }
module.exports.testRecord = testRecord; module.exports.testRecord = testRecord;

View File

@ -1,6 +1,6 @@
{ {
"name": "acme-challenge-test", "name": "acme-challenge-test",
"version": "3.1.0", "version": "3.1.1",
"description": "The base set of tests for all ACME challenge strategies. Any `acme-http-01-`, `acme-dns-01-`, `acme-challenge-`, or greenlock plugin should be able to pass these tests.", "description": "The base set of tests for all ACME challenge strategies. Any `acme-http-01-`, `acme-dns-01-`, `acme-challenge-`, or greenlock plugin should be able to pass these tests.",
"main": "index.js", "main": "index.js",
"homepage": "https://git.rootprojects.org/root/acme-challenge-test.js", "homepage": "https://git.rootprojects.org/root/acme-challenge-test.js",