Skip to content

Commit

Permalink
[tests] Add tests for UDP Server APIs with IPv4 and IPv6 (zephyrproje…
Browse files Browse the repository at this point in the history
  • Loading branch information
cuiyanx authored and grgustaf committed Mar 1, 2017
1 parent fc06a11 commit 29f9bad
Show file tree
Hide file tree
Showing 4 changed files with 300 additions and 0 deletions.
59 changes: 59 additions & 0 deletions tests/test-udp4-server-manual.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// Copyright (c) 2017, Intel Corporation.

// Testing UDP Server APIs

console.log("test UDP Server APIs for ipv4");

var dgram = require("dgram");

var PORT = 33333;
var HOST = "192.0.2.1";
var i = 0;

var keyWords = [
["hello", "hello"],
["WORLD", "ORLD"],
["^_^! *_*!", "^! *_*!"],
["~!@#$%^&*()_+", "#$%^&*()_+"],
["1234567890", "567890"]
];

var server = dgram.createSocket("udp4");

server.on("message", function(message, remote) {
console.log("received - " + remote.address + ":" +
remote.port + " - " + message.toString("ascii"));

if (i < keyWords.length) {
console.log("dgram.on(message, cb): received expected result '"
+ keyWords[i][0] + "'");

if (message.toString("ascii") === keyWords[i][0] + "\n" ||
message.toString("ascii") === keyWords[i][0] + "\r") {
server.send(message, i, message.length - i,
remote.port, remote.address);

console.log("dgram.on(message, cb): send expected result '"
+ keyWords[i][1] + "'");

i = i + 1;

if (i === keyWords.length) {
server.close();

console.log("Testing completed");
} else {
console.log("Please send '" + keyWords[i][0] + "' to " +
HOST + ":" + PORT);
}
}
}
});

server.on("error", function(err) {
console.log("error: " + err);
});

server.bind(PORT, HOST);

console.log("Please send '" + keyWords[i][0] + "' to " + HOST + ":" + PORT);
91 changes: 91 additions & 0 deletions tests/test-udp4-server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
// Copyright (c) 2017, Intel Corporation.

// Testing UDP Server APIs

console.log("test UDP Server APIs for ipv4");

var dgram = require("dgram");
var assert = require("Assert.js");

var PORT = 33333;
var HOST = "www.google.com";
var bindFlag = true;
var messageFlag = true;
var sendFlag = true;

var server = dgram.createSocket("udp4");
assert(server !== null && typeof server === "object",
"udp4: defined udp4 server");

server.on("message", function(message, remote) {
console.log(remote.address + ':' + remote.port +
' - ' + message.toString("ascii"));
server.send(message, 0, message.length, remote.port, remote.address);

if (bindFlag) {
assert(true, "udp4: bind on " + HOST + " : " + PORT);

bindFlag = false;
}

if (sendFlag) {
assert.throws(function() {
server.send(message, message.length + 1, message.length,
remote.port, remote.address);
}, "send: set offset greater than length");

assert.throws(function() {
server.send(message, 0, -1, remote.port, remote.address);
}, "send: set length as '-1'");

assert.throws(function() {
server.send(message, 0, message.length,
"value", remote.address);
}, "send: set port as 'value'");

assert.throws(function() {
server.send(message, 0, message.length,
remote.port, "www.google.com");
}, "send: set address as 'www.google.com'");

sendFlag = false;
}

if (messageFlag) {
assert(message !== null && typeof message === "object" &&
remote !== null && typeof remote === "object",
"udp4: callback value for server event 'message'");

assert.result();

messageFlag = false;
}
});

server.on("error", function(err) {
// In the current version, this callback is never called
assert(err !== null && typeof err === "object",
"udp4: callback value for server event 'error'");

console.log("error: " + err);
});

assert.throws(function() {
server.bind(PORT, HOST);
}, "udp4: set HOST as 'www.google.com'");

HOST = "192.0.2.10";
assert.throws(function() {
server.bind(PORT, HOST);
}, "udp4: set HOST as '192.0.2.10'");

HOST = "192.0.2.1";
PORT = "value";
assert.throws(function() {
server.bind(PORT, HOST);
}, "udp4: set PORT as 'value'");

PORT = 33333;
server.bind(PORT, HOST);

console.log("Please send some messages to " + HOST + " : " + PORT);
59 changes: 59 additions & 0 deletions tests/test-udp6-server-manual.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// Copyright (c) 2017, Intel Corporation.

// Testing UDP Server APIs

console.log("test UDP Server APIs for ipv6");

var dgram = require("dgram");

var PORT = 33333;
var HOST = "2001:db8::1";
var i = 0;

var keyWords = [
["hello", "hello"],
["WORLD", "ORLD"],
["^_^! *_*!", "^! *_*!"],
["~!@#$%^&*()_+", "#$%^&*()_+"],
["1234567890", "567890"]
];

var server = dgram.createSocket("udp6");

server.on("message", function(message, remote) {
console.log("received - " + remote.address + ":" +
remote.port + " - " + message.toString("ascii"));

if (i < keyWords.length) {
console.log("dgram.on(message, cb): received expected result '"
+ keyWords[i][0] + "'");

if (message.toString("ascii") === keyWords[i][0] + "\n" ||
message.toString("ascii") === keyWords[i][0] + "\r") {
server.send(message, i, message.length - i,
remote.port, remote.address);

console.log("dgram.on(message, cb): send expected result '"
+ keyWords[i][1] + "'");

i = i + 1;

if (i === keyWords.length) {
server.close();

console.log("Testing completed");
} else {
console.log("Please send '" + keyWords[i][0] + "' to " +
HOST + ":" + PORT);
}
}
}
});

server.on("error", function(err) {
console.log("error: " + err);
});

server.bind(PORT, HOST);

console.log("Please send '" + keyWords[i][0] + "' to " + HOST + ":" + PORT);
91 changes: 91 additions & 0 deletions tests/test-udp6-server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
// Copyright (c) 2017, Intel Corporation.

// Testing UDP Server APIs

console.log("test UDP Server APIs for ipv6");

var dgram = require("dgram");
var assert = require("Assert.js");

var PORT = 33333;
var HOST = "www.google.com";
var bindFlag = true;
var messageFlag = true;
var sendFlag = true;

var server = dgram.createSocket("udp6");
assert(server !== null && typeof server === "object",
"udp6: defined udp6 server");

server.on("message", function(message, remote) {
console.log(remote.address + ':' + remote.port +
' - ' + message.toString("ascii"));
server.send(message, 0, message.length, remote.port, remote.address);

if (bindFlag) {
assert(true, "udp6: bind on " + HOST + " : " + PORT);

bindFlag = false;
}

if (sendFlag) {
assert.throws(function() {
server.send(message, message.length + 1, message.length,
remote.port, remote.address);
}, "send: set offset greater than length");

assert.throws(function() {
server.send(message, 0, -1, remote.port, remote.address);
}, "send: set length as '-1'");

assert.throws(function() {
server.send(message, 0, message.length,
"value", remote.address);
}, "send: set port as 'value'");

assert.throws(function() {
server.send(message, 0, message.length,
remote.port, "www.google.com");
}, "send: set address as 'www.google.com'");

sendFlag = false;
}

if (messageFlag) {
assert(message !== null && typeof message === "object" &&
remote !== null && typeof remote === "object",
"udp6: callback value for server event 'message'");

assert.result();

messageFlag = false;
}
});

server.on("error", function(err) {
// In the current version, this callback is never called
assert(err !== null && typeof err === "object",
"udp6: callback value for server event 'error'");

console.log("error: " + err);
});

assert.throws(function() {
server.bind(PORT, HOST);
}, "udp6: set HOST as 'www.google.com'");

HOST = "2001:db8::10";
assert.throws(function() {
server.bind(PORT, HOST);
}, "udp4: set HOST as '2001:db8::10'");

HOST = "2001:db8::1";
PORT = "value";
assert.throws(function() {
server.bind(PORT, HOST);
}, "udp4: set PORT as 'value'");

PORT = 33333;
server.bind(PORT, HOST);

console.log("Please send some messages to " + HOST + " : " + PORT);

0 comments on commit 29f9bad

Please sign in to comment.