Skip to content

Commit cb7422f

Browse files
Sanggyu Leeyichoi
authored andcommitted
Add dgram.setTTL test cases (#952)
setTTL test cases consist of server and client pair. - Set server ip address in client source. - Run server in one terminal - Run client in another. If the number of hops between server and client is 2+, it prints nothing. Otherwise (i.e., there is no hop between server and client), it prints messages like followings: ``` listening server got data : Hello IoT.js client address : _your address_ client port : 36130 client family : IPv4 ``` IoT.js-DCO-1.0-Signed-off-by: Sanggyu Lee [email protected]
1 parent 7c886d1 commit cb7422f

File tree

3 files changed

+100
-0
lines changed

3 files changed

+100
-0
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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 assert = require('assert');
17+
var dgram = require('dgram');
18+
19+
var port = 41234;
20+
var msg = 'Hello IoT.js';
21+
var addr = '192.168.0.1'; // Change to your ip address
22+
var server = dgram.createSocket('udp4');
23+
24+
var client = dgram.createSocket('udp4');
25+
26+
client.send(msg, port, addr, function(err, len) {
27+
assert.equal(err, null);
28+
assert.equal(len, msg.length);
29+
});
30+
31+
client.on('error', function(err) {
32+
assert.fail();
33+
});
34+
35+
client.on('listening', function(err) {
36+
client.setTTL(1);
37+
});
38+
39+
client.on('message', function(data, rinfo) {
40+
console.log('client got data : ' + data);
41+
console.log('server address : ' + rinfo.address);
42+
console.log('server port : ' + rinfo.port);
43+
console.log('server family : ' + rinfo.family);
44+
assert.equal(port, rinfo.port);
45+
assert.equal(data, msg);
46+
client.close();
47+
});
48+
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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 assert = require('assert');
17+
var dgram = require('dgram');
18+
19+
var port = 41234;
20+
var msg = 'Hello IoT.js';
21+
var server = dgram.createSocket('udp4');
22+
23+
server.on('error', function(err) {
24+
assert.fail();
25+
server.close();
26+
});
27+
28+
server.on('message', function(data, rinfo) {
29+
console.log('server got data : ' + data);
30+
console.log('client address : ' + rinfo.address);
31+
console.log('client port : ' + rinfo.port);
32+
console.log('client family : ' + rinfo.family);
33+
assert.equal(data, msg);
34+
server.send(msg, rinfo.port, rinfo.address, function (err, len) {
35+
assert.equal(err, null);
36+
assert.equal(len, msg.length);
37+
});
38+
});
39+
40+
server.on('listening', function() {
41+
console.log('listening');
42+
});
43+
44+
server.bind(port, function() {
45+
server.setTTL(1);
46+
});
47+
48+
process.on('exit', function(code) {
49+
assert.equal(code, 0);
50+
});

test/testsets.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
{ "name": "test_dgram_broadcast.js", "skip": ["all"], "reason": "need to setup test environment" },
1414
{ "name": "test_dgram_multicast_membership.js", "skip": ["all"], "reason": "need to setup test environment" },
1515
{ "name": "test_dgram_multicast_set_multicast_loop.js", "skip": ["all"], "reason": "need to setup test environment" },
16+
{ "name": "test_dgram_setttl_client.js", "skip": ["all"], "reason": "need to setup test environment" },
17+
{ "name": "test_dgram_setttl_server.js", "skip": ["all"], "reason": "need to setup test environment" },
1618
{ "name": "test_dns.js", "skip": ["nuttx"], "reason": "not implemented for nuttx" },
1719
{ "name": "test_dns_lookup.js", "skip": ["nuttx"], "reason": "not implemented for nuttx" },
1820
{ "name": "test_events.js", "skip": ["nuttx"], "reason": "not implemented for nuttx" },

0 commit comments

Comments
 (0)