Skip to content

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 nodejs#8047.
  • Loading branch information
Julien Gilli committed Aug 6, 2014
1 parent 63a4268 commit a375a6b
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 4 deletions.
29 changes: 29 additions & 0 deletions test/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
var path = require('path');
var fs = require('fs');
var assert = require('assert');
var child_process = require('child_process');

exports.testDir = path.dirname(__filename);
exports.fixturesDir = path.join(exports.testDir, 'fixtures');
Expand Down Expand Up @@ -219,3 +220,31 @@ exports.checkSpawnSyncRet = function(ret) {
assert.strictEqual(ret.status, 0);
assert.strictEqual(ret.error, undefined);
};

/*
* Sometimes, the service for port 80/tcp is not mentioned in
* /etc/services, and in this case getnameinfo will return the port
* number as the service name.
*/
exports.defaultHttpServiceName = '80'

exports.getServiceNameFromEtcServices = function(port, protocol, callback) {
var shellCmd = "grep -v '^#' /etc/services" +
" | grep '\\b" + port + '/' + protocol + "\\b' " +
" | tr \"\\t\" \" \" " +
" | cut -d ' ' -f 1";

var servicesGrep = child_process.exec(shellCmd, function (err, stdout, stderr) {
if (err) {
callback({ stderr: stderr});
} else {
var serviceName;
var services = stdout.split('\n', 1);
if (services.length > 0) {
serviceName = services[0].toString().replace(/\n|\r/g, "");
}

callback(null, serviceName);
}
});
}
34 changes: 30 additions & 4 deletions test/internet/test-dns.js
Original file line number Diff line number Diff line change
Expand Up @@ -495,9 +495,22 @@ 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');

done();
/*
* 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.defaultHttpServiceName;
common.getServiceNameFromEtcServices(80, 'tcp', function (err, serviceName) {
if (!err) {
httpServiceName = serviceName;
}

assert.strictEqual(service, httpServiceName);
done();
});
});

checkWrap(req);
Expand All @@ -515,9 +528,22 @@ 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');

done();
/*
* 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.defaultHttpServiceName;
common.getServiceNameFromEtcServices(80, 'tcp', function (err, serviceName) {
if (!err) {
httpServiceName = serviceName;
}

assert.strictEqual(service, httpServiceName);
done();
});
});

checkWrap(req);
Expand Down

0 comments on commit a375a6b

Please sign in to comment.