mirror of
				https://git.coolaj86.com/coolaj86/acme-challenge-test.js.git
				synced 2025-11-04 02:22:49 +00:00 
			
		
		
		
	v3.1.1: perform all sets, then all gets, then all removes
This commit is contained in:
		
							parent
							
								
									17e409b158
								
							
						
					
					
						commit
						ea508c68f4
					
				
							
								
								
									
										116
									
								
								index.js
									
									
									
									
									
								
							
							
						
						
									
										116
									
								
								index.js
									
									
									
									
									
								
							@ -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:
 | 
			
		||||
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;
 | 
			
		||||
		if ('http-01' === ch.type && ch.wildname) {
 | 
			
		||||
			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
 | 
			
		||||
		// 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
 | 
			
		||||
			// _test is used by the manual cli reference implementations
 | 
			
		||||
			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.comment = 'unknown challenge type, supplying everything';
 | 
			
		||||
			}
 | 
			
		||||
		return get({ challenge: query })
 | 
			
		||||
			.then(function(secret) {
 | 
			
		||||
			opts.query = query;
 | 
			
		||||
			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) {
 | 
			
		||||
						console.info(
 | 
			
		||||
							'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() {
 | 
			
		||||
				return remove(opts).then(function() {
 | 
			
		||||
					return get(opts).then(function(result) {
 | 
			
		||||
			return mapAsync(all, function(opts) {
 | 
			
		||||
				return chr.remove(opts).then(function() {
 | 
			
		||||
					return chr.get(opts).then(function(result) {
 | 
			
		||||
						if (result) {
 | 
			
		||||
							throw new Error(
 | 
			
		||||
								'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'
 | 
			
		||||
							);
 | 
			
		||||
						}
 | 
			
		||||
						console.info("PASS '%s'", opts.domain);
 | 
			
		||||
					});
 | 
			
		||||
				});
 | 
			
		||||
			});
 | 
			
		||||
	});
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
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() {
 | 
			
		||||
		})
 | 
			
		||||
		.then(function() {
 | 
			
		||||
			console.info('All soft tests: PASS');
 | 
			
		||||
			console.warn(
 | 
			
		||||
				'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."
 | 
			
		||||
			);
 | 
			
		||||
		});
 | 
			
		||||
};
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
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 token = crypto.randomBytes(8).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;
 | 
			
		||||
	challenge.dnsHost += altname;
 | 
			
		||||
 | 
			
		||||
	return run(challenger, { challenge: challenge });
 | 
			
		||||
	return challenge;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
function testRecord(type, altname, challenger) {
 | 
			
		||||
	return testEach(type, [altname], challenger);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
module.exports.testRecord = testRecord;
 | 
			
		||||
 | 
			
		||||
@ -1,6 +1,6 @@
 | 
			
		||||
{
 | 
			
		||||
	"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.",
 | 
			
		||||
	"main": "index.js",
 | 
			
		||||
	"homepage": "https://git.rootprojects.org/root/acme-challenge-test.js",
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user