19d996e166dabc58bbd6c22136b925b0d0b1e9c6
[platform/upstream/iotjs.git] / tools / test / run_pass / test_dns_lookup.js
1 /* Copyright 2017-present Samsung Electronics Co., Ltd. and other contributors
2  *
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
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
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.
14  */
15
16 var dns = require('dns');
17 var assert = require('assert');
18
19 var options = {
20   family: 4,
21   hints: dns.ADDRCONFIG | dns.V4MAPPED,
22 };
23
24 function isIPv4(ip) {
25   var IPv4Regex = new RegExp(
26     '^([0-9]{1,3}\.){3}[0-9]{1,3}(\/([0-9]|[1-2][0-9]|3[0-2]))?$'
27   );
28
29   return IPv4Regex.test(ip);
30 }
31
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);
37 });
38
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);
45 });
46
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);
51 });
52
53 // Test with empty hostname.
54 dns.lookup('', 4, function(err, ip, family) {
55   assert.notEqual(err, null);
56   assert.equal(err.code, -3008);
57 });
58
59 // Test with non string hostname.
60 assert.throws(function() {
61   dns.lookup(5000, options, function(err, ip, family) {});
62 }, TypeError);
63
64 // Test without callback function.
65 assert.throws(function() {
66   dns.lookup('localhost', options);
67 }, TypeError);
68
69 // Test with invalid callback parameter.
70 assert.throws(function() {
71   dns.lookup('localhost', options, 5000);
72 }, TypeError);
73
74 assert.throws(function() {
75   dns.lookup('localhost', options, 'callback');
76 }, TypeError);
77
78 // Test with invalid hints option.
79 options.hints = -1;
80 assert.throws(function() {
81   dns.lookup('localhost', options, function(err, ip, family) {});
82 }, TypeError);
83
84 options.hints = 5000;
85 assert.throws(function() {
86   dns.lookup('localhost', options, function(err, ip, family) {});
87 }, TypeError);
88
89 // Test with invalid options parameter.
90 assert.throws(function() {
91   dns.lookup('localhost', 'options', function(err, ip, family) {});
92 }, TypeError);
93
94 assert.throws(function() {
95   dns.lookup('localhost', '', function(err, ip, family) {});
96 }, TypeError);
97
98 // Test with invalid family option.
99 assert.throws(function() {
100   dns.lookup('localhost', 10, function(err, ip, family) {});
101 }, TypeError);
102
103 assert.throws(function() {
104   dns.lookup('localhost', -5, function(err, ip, family) {});
105 }, TypeError);