1 /* -*- Mode: Javascript; indent-tabs-mode: nil; js-indent-level: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 const HELLO_WORLD = "Hello World";
8 const EMPTY_MESSAGE = "";
10 add_test(function test_udp_message_raw_data() {
11 info("test for nsIUDPMessage.rawData");
13 let socket = Cc["@mozilla.org/network/udp-socket;1"].createInstance(
17 socket.init(-1, true, Services.scriptSecurityManager.getSystemPrincipal());
18 info("Port assigned : " + socket.port);
20 QueryInterface: ChromeUtils.generateQI(["nsIUDPSocketListener"]),
21 onPacketReceived(aSocket, aMessage) {
22 let recv_data = String.fromCharCode.apply(null, aMessage.rawData);
23 Assert.equal(recv_data, HELLO_WORLD);
24 Assert.equal(recv_data, aMessage.data);
28 onStopListening(aSocket, aStatus) {},
31 let rawData = new Uint8Array(HELLO_WORLD.length);
32 for (let i = 0; i < HELLO_WORLD.length; i++) {
33 rawData[i] = HELLO_WORLD.charCodeAt(i);
35 let written = socket.send("127.0.0.1", socket.port, rawData);
36 Assert.equal(written, HELLO_WORLD.length);
39 add_test(function test_udp_send_stream() {
40 info("test for nsIUDPSocket.sendBinaryStream");
42 let socket = Cc["@mozilla.org/network/udp-socket;1"].createInstance(
46 socket.init(-1, true, Services.scriptSecurityManager.getSystemPrincipal());
48 QueryInterface: ChromeUtils.generateQI(["nsIUDPSocketListener"]),
49 onPacketReceived(aSocket, aMessage) {
50 let recv_data = String.fromCharCode.apply(null, aMessage.rawData);
51 Assert.equal(recv_data, HELLO_WORLD);
55 onStopListening(aSocket, aStatus) {},
58 let stream = Cc["@mozilla.org/io/string-input-stream;1"].createInstance(
59 Ci.nsIStringInputStream
61 stream.setData(HELLO_WORLD, HELLO_WORLD.length);
62 socket.sendBinaryStream("127.0.0.1", socket.port, stream);
65 add_test(function test_udp_message_zero_length() {
66 info("test for nsIUDPMessage with zero length");
68 let socket = Cc["@mozilla.org/network/udp-socket;1"].createInstance(
72 socket.init(-1, true, Services.scriptSecurityManager.getSystemPrincipal());
73 info("Port assigned : " + socket.port);
75 QueryInterface: ChromeUtils.generateQI(["nsIUDPSocketListener"]),
76 onPacketReceived(aSocket, aMessage) {
77 let recv_data = String.fromCharCode.apply(null, aMessage.rawData);
78 Assert.equal(recv_data, EMPTY_MESSAGE);
79 Assert.equal(recv_data, aMessage.data);
83 onStopListening(aSocket, aStatus) {},
86 let rawData = new Uint8Array(EMPTY_MESSAGE.length);
87 let written = socket.send("127.0.0.1", socket.port, rawData);
88 Assert.equal(written, EMPTY_MESSAGE.length);