1 /* Copyright 2017-present Samsung Electronics Co., Ltd. and other contributors
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
16 var dns = require('dns');
17 var assert = require('assert');
21 hints: dns.ADDRCONFIG | dns.V4MAPPED,
25 var IPv4Regex = new RegExp(
26 '^([0-9]{1,3}\.){3}[0-9]{1,3}(\/([0-9]|[1-2][0-9]|3[0-2]))?$'
29 return IPv4Regex.test(ip);
32 dns.lookup('localhost', 4, function(err, ip, family) {
33 assert.equal(err, null);
34 assert.equal(isIPv4(ip), true);
35 assert.equal(ip, '127.0.0.1');
36 assert.strictEqual(family, 4);
39 // Test with IPv4 option.
40 dns.lookup('localhost', 4, function(err, ip, family) {
41 assert.equal(err, null);
42 assert.equal(isIPv4(ip), true);
43 assert.equal(ip, '127.0.0.1');
44 assert.strictEqual(family, 4);
47 // Test with invalid hostname.
48 dns.lookup('invalid', 4, function(err, ip, family) {
49 assert.notEqual(err, null);
50 assert.equal(err.code, -3008);
53 // Test with empty hostname.
54 dns.lookup('', 4, function(err, ip, family) {
55 assert.notEqual(err, null);
56 assert.equal(err.code, -3008);
59 // Test with non string hostname.
60 assert.throws(function() {
61 dns.lookup(5000, options, function(err, ip, family) {});
64 // Test without callback function.
65 assert.throws(function() {
66 dns.lookup('localhost', options);
69 // Test with invalid callback parameter.
70 assert.throws(function() {
71 dns.lookup('localhost', options, 5000);
74 assert.throws(function() {
75 dns.lookup('localhost', options, 'callback');
78 // Test with invalid hints option.
80 assert.throws(function() {
81 dns.lookup('localhost', options, function(err, ip, family) {});
85 assert.throws(function() {
86 dns.lookup('localhost', options, function(err, ip, family) {});
89 // Test with invalid options parameter.
90 assert.throws(function() {
91 dns.lookup('localhost', 'options', function(err, ip, family) {});
94 assert.throws(function() {
95 dns.lookup('localhost', '', function(err, ip, family) {});
98 // Test with invalid family option.
99 assert.throws(function() {
100 dns.lookup('localhost', 10, function(err, ip, family) {});
103 assert.throws(function() {
104 dns.lookup('localhost', -5, function(err, ip, family) {});