Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / chrome / test / data / extensions / api_test / socket / api / multicast.js
blob2447a82ebf4eb96263591badff4c1cb7d3b5ec90
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) {
8 var result = '';
9 for (var i = 0; i < count; i++) {
10 result += (Math.random() * 16 >> 0).toString(16);
12 return result;
15 var kMulticastAddress = "237.132.100.133";
16 var kTestMessageLength = 128;
17 var kTestMessage = randomHexString(128);
18 var kPort = 11103;
20 function arrayBufferToString(arrayBuffer) {
21 // UTF-16LE
22 return String.fromCharCode.apply(String, new Uint16Array(arrayBuffer));
25 function stringToArrayBuffer(string) {
26 // UTF-16LE
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);
32 return buf;
35 function waitForMessage(socketId, callback) {
36 var cancelled = false;
37 var relayCanceller = null;
38 socket.recvFrom(socketId, 1024, function (result) {
39 if (cancelled)
40 return;
42 if (result.resultCode == kTestMessageLength * 2 &&
43 kTestMessage === arrayBufferToString(result.data)) {
44 callback(cancelled);
45 } else {
46 // Restart waiting.
47 relayCanceller = waitForMessage(socketId, callback);
49 });
50 return function canceller() {
51 if (relayCanceller) {
52 relayCanceller();
53 } else {
54 cancelled = true;
55 callback(true);
60 function testMulticastSettings() {
61 socket.create('udp', {}, function (socketInfo) {
62 var socket_id;
63 if (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,
72 function (result) {
73 chrome.test.assertEq(0, result,
74 "Error setting multicast loop back mode.");
75 socket.setMulticastLoopbackMode(socket_id, true,
76 function (result) {
77 chrome.test.assertEq(0, result,
78 "Error setting multicast loop back mode.");
79 socket.destroy(socket_id);
80 testMulticastRecv();
81 });
82 });
83 });
84 });
85 } else {
86 chrome.test.fail("Cannot create server udp socket");
88 });
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) {
95 var clientSocketId;
96 if (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),
107 function (result) {
108 chrome.test.assertTrue(result.bytesWritten >= 0,
109 "Send to failed. " + JSON.stringify(result));
110 socket.destroy(clientSocketId);
114 } else {
115 chrome.test.fail("Cannot create client udp socket");
120 function recvBeforeAddMembership(serverSocketId) {
121 var recvTimeout;
122 var canceller = waitForMessage(serverSocketId, function (cancelled) {
123 clearTimeout(recvTimeout);
124 if (cancelled) {
125 recvWithMembership(serverSocketId);
126 } else {
127 chrome.test.fail("Received message before joining the group");
130 testSendMessage(kTestMessage); // Meant to be lost.
131 recvTimeout = setTimeout(function () {
132 canceller();
133 testSendMessage(kTestMessage, "127.0.0.1", kPort);
134 }, 2000);
137 function recvWithMembership(serverSocketId) {
138 // Join group.
139 socket.joinGroup(serverSocketId, kMulticastAddress, function (result) {
140 chrome.test.assertEq(0, result, "Join group failed.");
141 var recvTimeout;
142 var canceller = waitForMessage(serverSocketId, function (cancelled) {
143 clearTimeout(recvTimeout);
144 if (!cancelled) {
145 recvWithoutMembership(serverSocketId);
146 } else {
147 chrome.test.fail("Faild to receive message after joining the group");
150 testSendMessage(kTestMessage);
151 recvTimeout = setTimeout(function () {
152 canceller();
153 socket.destroy(serverSocketId);
154 chrome.test.fail("Cannot receive from multicast group.");
155 }, 2000);
159 function recvWithoutMembership(serverSocketId) {
160 socket.leaveGroup(serverSocketId, kMulticastAddress, function (result) {
161 chrome.test.assertEq(0, result, "leave group failed.");
162 var recvTimeout;
163 var canceller = waitForMessage(serverSocketId, function (cancelled) {
164 clearTimeout(recvTimeout);
165 if (cancelled) {
166 socket.destroy(serverSocketId);
167 chrome.test.succeed();
168 } else {
169 chrome.test.fail("Received message after leaving the group");
170 socket.destroy(serverSocketId);
173 testSendMessage(request);
174 recvTimeout = setTimeout(function () {
175 canceller();
176 socket.destroy(serverSocketId);
177 chrome.test.succeed();
178 }, 2000);
182 function testMulticastRecv() {
183 socket.create('udp', {}, function (socketInfo) {
184 var serverSocketId;
185 if (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);
191 } else {
192 chrome.test.fail("Cannot create server udp socket");
197 testMulticastSettings();