Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

Commit

Permalink
tests: do not hardcode service name in test-dns.
Browse files Browse the repository at this point in the history
Instead of hard-coding http service name in test-dns, retrieve it from
/etc/services. This is not ideal, but it's still better than hard-coding
it.

Fixes #8047.
  • Loading branch information
Julien Gilli authored and tjfontaine committed Aug 12, 2014
1 parent 050f5d0 commit adcc1fb
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 2 deletions.
40 changes: 40 additions & 0 deletions test/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -226,3 +226,43 @@ exports.checkSpawnSyncRet = function(ret) {
assert.strictEqual(ret.status, 0);
assert.strictEqual(ret.error, undefined);
};

var etcServicesFileName = path.join('/etc', 'services');
if (process.platform === 'win32') {
etcServicesFileName = path.join('/Windows', 'System32', 'drivers', 'etc',
'services');
}

exports.getServiceName = function getServiceName(port, protocol) {
if (port == null) {
throw new Error("Missing port number");
}

if (typeof protocol !== 'string') {
throw new Error("Protocol must be a string");
}

/*
* I'm not a big fan of readFileSync, but reading /etc/services asynchronously
* here would require implementing a simple line parser, which seems overkill
* for a simple utility function that is not running concurrently with any
* other one.
*/
var servicesContent = fs.readFileSync(etcServicesFileName,
{ encoding: 'utf8'});
var regexp = util.format('^(\\w+)\\s+\\s%d/%s\\s', port, protocol);
var re = new RegExp(regexp, 'm');

/*
* By default, if a service can't be found in /etc/services,
* its name is considered to be its port number.
*/
var serviceName = port.toString();

var matches = re.exec(servicesContent);
if (matches && matches.length > 1) {
serviceName = matches[1];
}

return serviceName;
}
20 changes: 18 additions & 2 deletions test/internet/test-dns.js
Original file line number Diff line number Diff line change
Expand Up @@ -495,7 +495,15 @@ TEST(function test_lookupservice_ip_ipv4(done) {
var req = dns.lookupService('127.0.0.1', 80, function(err, host, service) {
if (err) throw err;
assert.strictEqual(host, 'localhost');
assert.strictEqual(service, 'http');

/*
* Retrieve the actual HTTP service name as setup on the host currently
* running the test by reading it from /etc/services. This is not ideal,
* as the service name lookup could use another mechanism (e.g nscd), but
* it's already better than hardcoding it.
*/
var httpServiceName = common.getServiceName(80, 'tcp');
assert.strictEqual(service, httpServiceName);

done();
});
Expand All @@ -515,7 +523,15 @@ TEST(function test_lookupservice_ip_ipv6(done) {
* that most sane platforms use either one of these two by default.
*/
assert(host === 'localhost' || host === 'ip6-localhost');
assert.strictEqual(service, 'http');

/*
* Retrieve the actual HTTP service name as setup on the host currently
* running the test by reading it from /etc/services. This is not ideal,
* as the service name lookup could use another mechanism (e.g nscd), but
* it's already better than hardcoding it.
*/
var httpServiceName = common.getServiceName(80, 'tcp');
assert.strictEqual(service, httpServiceName);

done();
});
Expand Down

0 comments on commit adcc1fb

Please sign in to comment.