1 // Copyright (c) 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 // Tests for multicast UDP socket.
6 function testMulticast() {
7 function randomHexString(count
) {
9 for (var i
= 0; i
< count
; i
++) {
10 result
+= (Math
.random() * 16 >> 0).toString(16);
15 var kMulticastAddress
= "237.132.100.133";
16 var kTestMessageLength
= 128;
17 var kTestMessage
= randomHexString(128);
20 function arrayBufferToString(arrayBuffer
) {
22 return String
.fromCharCode
.apply(String
, new Uint16Array(arrayBuffer
));
25 function stringToArrayBuffer(string
) {
27 var buf
= new ArrayBuffer(string
.length
* 2);
28 var bufView
= new Uint16Array(buf
);
29 for (var i
= 0, strLen
= string
.length
; i
< strLen
; i
++) {
30 bufView
[i
] = string
.charCodeAt(i
);
35 function waitForMessage(socketId
, callback
) {
36 var cancelled
= false;
37 var relayCanceller
= null;
38 socket
.recvFrom(socketId
, 1024, function (result
) {
42 if (result
.resultCode
== kTestMessageLength
* 2 &&
43 kTestMessage
=== arrayBufferToString(result
.data
)) {
47 relayCanceller
= waitForMessage(socketId
, callback
);
50 return function canceller() {
60 function testMulticastSettings() {
61 socket
.create('udp', {}, function (socketInfo
) {
64 socket_id
= socketInfo
.socketId
;
65 socket
.setMulticastTimeToLive(socket_id
, 0, function (result
) {
66 chrome
.test
.assertEq(0, result
,
67 "Error setting multicast time to live.");
68 socket
.setMulticastTimeToLive(socket_id
, -3, function (result
) {
69 chrome
.test
.assertEq(-4, result
,
70 "Error setting multicast time to live.");
71 socket
.setMulticastLoopbackMode(socket_id
, false,
73 chrome
.test
.assertEq(0, result
,
74 "Error setting multicast loop back mode.");
75 socket
.setMulticastLoopbackMode(socket_id
, true,
77 chrome
.test
.assertEq(0, result
,
78 "Error setting multicast loop back mode.");
79 socket
.destroy(socket_id
);
86 chrome
.test
.fail("Cannot create server udp socket");
91 function testSendMessage(message
, address
) {
92 // Send the UDP message to the address with multicast ttl = 0.
93 address
= address
|| kMulticastAddress
;
94 socket
.create('udp', {}, function (socketInfo
) {
97 clientSocketId
= socketInfo
.socketId
;
98 chrome
.test
.assertTrue(clientSocketId
> 0,
99 "Cannot create client udp socket.");
100 socket
.setMulticastTimeToLive(clientSocketId
, 0, function (result
) {
101 chrome
.test
.assertEq(0, result
,
102 "Cannot create client udp socket.");
103 socket
.connect(clientSocketId
, address
, kPort
, function (result
) {
104 chrome
.test
.assertEq(0, result
,
105 "Cannot connect to localhost.");
106 socket
.write(clientSocketId
, stringToArrayBuffer(kTestMessage
),
108 chrome
.test
.assertTrue(result
.bytesWritten
>= 0,
109 "Send to failed. " + JSON
.stringify(result
));
110 socket
.destroy(clientSocketId
);
115 chrome
.test
.fail("Cannot create client udp socket");
120 function recvBeforeAddMembership(serverSocketId
) {
122 var canceller
= waitForMessage(serverSocketId
, function (cancelled
) {
123 clearTimeout(recvTimeout
);
125 recvWithMembership(serverSocketId
);
127 chrome
.test
.fail("Received message before joining the group");
130 testSendMessage(kTestMessage
); // Meant to be lost.
131 recvTimeout
= setTimeout(function () {
133 testSendMessage(kTestMessage
, "127.0.0.1", kPort
);
137 function recvWithMembership(serverSocketId
) {
139 socket
.joinGroup(serverSocketId
, kMulticastAddress
, function (result
) {
140 chrome
.test
.assertEq(0, result
, "Join group failed.");
142 var canceller
= waitForMessage(serverSocketId
, function (cancelled
) {
143 clearTimeout(recvTimeout
);
145 recvWithoutMembership(serverSocketId
);
147 chrome
.test
.fail("Faild to receive message after joining the group");
150 testSendMessage(kTestMessage
);
151 recvTimeout
= setTimeout(function () {
153 socket
.destroy(serverSocketId
);
154 chrome
.test
.fail("Cannot receive from multicast group.");
159 function recvWithoutMembership(serverSocketId
) {
160 socket
.leaveGroup(serverSocketId
, kMulticastAddress
, function (result
) {
161 chrome
.test
.assertEq(0, result
, "leave group failed.");
163 var canceller
= waitForMessage(serverSocketId
, function (cancelled
) {
164 clearTimeout(recvTimeout
);
166 socket
.destroy(serverSocketId
);
167 chrome
.test
.succeed();
169 chrome
.test
.fail("Received message after leaving the group");
170 socket
.destroy(serverSocketId
);
173 testSendMessage(request
);
174 recvTimeout
= setTimeout(function () {
176 socket
.destroy(serverSocketId
);
177 chrome
.test
.succeed();
182 function testMulticastRecv() {
183 socket
.create('udp', {}, function (socketInfo
) {
186 serverSocketId
= socketInfo
.socketId
;
187 socket
.bind(serverSocketId
, "0.0.0.0", kPort
, function (result
) {
188 chrome
.test
.assertEq(0, result
, "Bind failed.");
189 recvBeforeAddMembership(serverSocketId
);
192 chrome
.test
.fail("Cannot create server udp socket");
197 testMulticastSettings();