This repository has been archived by the owner on Aug 5, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 64
/
test-ws4-server-dhcp.js
210 lines (156 loc) · 6.01 KB
/
test-ws4-server-dhcp.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
// Copyright (c) 2017, Intel Corporation.
// Run this test case and run ws client on linux with
// tests/tools/test-ws4-client-dhcp.js
console.log("Test websocket server APIs on IPv4 with DHCP mode...");
var WebSocket = require("ws");
var assert = require("Assert.js");
var netConfig = require("netconfig");
netConfig.dhcp(function(address, subnet, gateway) {
console.log("IPv4 address: " + address);
var ServerHandler = function(protos) {
for (var i = 0; i < protos.length; i++) {
if (protos[i] == "testProtocol") {
return protos[i];
}
}
return false;
}
var WSServer = new WebSocket.Server({
host: "0.0.0.0",
port: 8080,
backlog: 5,
clientTracking: true,
maxPayload : 125,
acceptHandler: ServerHandler
});
assert(typeof WSServer === "object" && WSServer !== null,
"WSServer object is defined");
WSServer.on("connection", function(websocket) {
assert(true, "WSServerObject: accept client connected");
assert(true, "WSServerEvent: be called as 'connection' event");
console.log("Create websocket connection successful");
var messageFlag = true;
var sendNoMaskFlag = false;
var sendMaskFlag = false;
websocket.on("message", function(message) {
if (messageFlag) {
assert(true, "WebSocketEvent: be called as 'message' event");
assert(true, "WebSocketObject: receive data from client as '" +
message.toString("ascii") + "'");
messageFlag = false;
}
if (sendNoMaskFlag) {
assert(message.toString("ascii") === "hello",
"WebSocketObject: send data without mask");
sendNoMaskFlag = false;
}
if (sendMaskFlag) {
assert(message.toString("ascii") === "world",
"WebSocketObject: send data with mask");
sendMaskFlag = false;
}
console.log("ReceiveData: " + message.toString("ascii"));
});
var pingFlag = true;
var pingNoMaskFlag = false;
var pingMaskFlag = false;
websocket.on("ping", function(data) {
if (pingFlag) {
assert(true, "WebSocketEvent: be called as 'ping' event");
pingFlag = false;
}
if (pingNoMaskFlag) {
assert(data.toString("ascii") === "pongTest",
"WebSocketObject: pong and ping data without mask");
pingNoMaskFlag = false;
}
if (pingMaskFlag) {
assert(data.toString("ascii") === "testPong",
"WebSocketObject: pong and ping data with mask");
pingMaskFlag = false;
}
console.log("ReceivePing: " + data.toString("ascii"));
});
var pongFlag = true;
var pongNoMaskFlag = false;
var pongMaskFlag = false;
websocket.on("pong", function(data) {
if (pongFlag) {
assert(true, "WebSocketEvent: be called as 'pong' event");
pongFlag = false;
}
if (pongNoMaskFlag) {
assert(data.toString("ascii") === "pingTest",
"WebSocketObject: ping and pong data without mask");
pongNoMaskFlag = false;
}
if (pongMaskFlag) {
assert(data.toString("ascii") === "testPing",
"WebSocketObject: ping and pong data with mask");
pongMaskFlag = false;
}
console.log("ReceivePong: " + data.toString("ascii"));
});
var closeFlag = true;
websocket.on("close", function(code, reason) {
if (closeFlag) {
assert(true, "WebSocketEvent: be called as 'close' event");
assert.result();
closeFlag = false;
}
console.log("ReceiveClose: " + code + " : " + reason);
});
var errorFlag = true;
websocket.on("error", function(error) {
if (errorFlag) {
assert(true, "WebSocketEvent: be called as 'error' event");
errorFlag = false;
}
console.log("ReceiveError: " + error.name + " : " + error.message);
});
var Count = 0;
var Timer = setInterval(function () {
if (Count === 1) {
websocket.send(new Buffer("hello"));
console.log("SendData: hello");
sendNoMaskFlag = true;
}
if (Count === 2) {
websocket.send(new Buffer("world"), true);
console.log("SendData: world");
sendMaskFlag = true;
}
if (Count === 3) {
websocket.ping(new Buffer("pingTest"));
console.log("PingData: pingTest");
pongNoMaskFlag = true;
}
if (Count === 4) {
websocket.ping(new Buffer("testPing"), true);
console.log("PingData: testPing");
pongMaskFlag = true;
}
if (Count === 5) {
websocket.pong(new Buffer("pongTest"));
console.log("PongData: pongTest");
pingNoMaskFlag = true;
}
if (Count === 6) {
websocket.pong(new Buffer("testPong"), true);
console.log("PongData: testPong");
pingMaskFlag = true;
}
if (Count === 7) {
websocket.send(new Buffer("close"));
console.log("SendData: close");
}
Count = Count + 1;
if (Count === 8) {
clearInterval(Timer);
}
}, 3000);
});
WSServer.on('error', function (error) {
console.log('Server socket: ' + error.name + ': ' + error.message);
});
});