diff --git a/test/common.js b/test/common.js index 40f707283270..4f462e5bc7d1 100644 --- a/test/common.js +++ b/test/common.js @@ -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'); @@ -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); + } + }); +} diff --git a/test/internet/test-dns.js b/test/internet/test-dns.js index 902545c1752b..f76633be172f 100644 --- a/test/internet/test-dns.js +++ b/test/internet/test-dns.js @@ -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); @@ -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);