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
|
/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */
/* Any copyright is dedicated to the Public Domain.
* http://creativecommons.org/publicdomain/zero/1.0/ */
'use strict';
const { classes: Cc, interfaces: Ci, utils: Cu, results: Cr, Constructor: CC } = Components;
const ServerSocket = CC("@mozilla.org/network/server-socket;1",
"nsIServerSocket",
"init");
Cu.import('resource://gre/modules/XPCOMUtils.jsm');
Cu.import('resource://gre/modules/Services.jsm');
var testServer = null;
var clientTransport = null;
var serverTransport = null;
var clientBuilder = null;
var serverBuilder = null;
const clientMessage = "Client Message";
const serverMessage = "Server Message";
const address = Cc["@mozilla.org/supports-cstring;1"]
.createInstance(Ci.nsISupportsCString);
address.data = "127.0.0.1";
const addresses = Cc["@mozilla.org/array;1"].createInstance(Ci.nsIMutableArray);
addresses.appendElement(address, false);
const serverChannelDescription = {
QueryInterface: XPCOMUtils.generateQI([Ci.nsIPresentationChannelDescription]),
type: 1,
tcpAddress: addresses,
};
var isClientReady = false;
var isServerReady = false;
var isClientClosed = false;
var isServerClosed = false;
const clientCallback = {
QueryInterface: XPCOMUtils.generateQI([Ci.nsIPresentationSessionTransportCallback]),
notifyTransportReady: function () {
Assert.ok(true, "Client transport ready.");
isClientReady = true;
if (isClientReady && isServerReady) {
run_next_test();
}
},
notifyTransportClosed: function (aReason) {
Assert.ok(true, "Client transport is closed.");
isClientClosed = true;
if (isClientClosed && isServerClosed) {
run_next_test();
}
},
notifyData: function(aData) {
Assert.equal(aData, serverMessage, "Client transport receives data.");
run_next_test();
},
};
const serverCallback = {
QueryInterface: XPCOMUtils.generateQI([Ci.nsIPresentationSessionTransportCallback]),
notifyTransportReady: function () {
Assert.ok(true, "Server transport ready.");
isServerReady = true;
if (isClientReady && isServerReady) {
run_next_test();
}
},
notifyTransportClosed: function (aReason) {
Assert.ok(true, "Server transport is closed.");
isServerClosed = true;
if (isClientClosed && isServerClosed) {
run_next_test();
}
},
notifyData: function(aData) {
Assert.equal(aData, clientMessage, "Server transport receives data.");
run_next_test();
},
};
const clientListener = {
QueryInterface: XPCOMUtils.generateQI([Ci.nsIPresentationSessionTransportBuilderListener]),
onSessionTransport(aTransport) {
Assert.ok(true, "Client Transport is built.");
clientTransport = aTransport;
clientTransport.callback = clientCallback;
if (serverTransport) {
run_next_test();
}
}
}
const serverListener = {
QueryInterface: XPCOMUtils.generateQI([Ci.nsIPresentationSessionTransportBuilderListener]),
onSessionTransport(aTransport) {
Assert.ok(true, "Server Transport is built.");
serverTransport = aTransport;
serverTransport.callback = serverCallback;
serverTransport.enableDataNotification();
if (clientTransport) {
run_next_test();
}
}
}
function TestServer() {
this.serverSocket = ServerSocket(-1, true, -1);
this.serverSocket.asyncListen(this)
}
TestServer.prototype = {
onSocketAccepted: function(aSocket, aTransport) {
print("Test server gets a client connection.");
serverBuilder = Cc["@mozilla.org/presentation/presentationtcpsessiontransport;1"]
.createInstance(Ci.nsIPresentationTCPSessionTransportBuilder);
serverBuilder.buildTCPSenderTransport(aTransport, serverListener);
},
onStopListening: function(aSocket) {
print("Test server stops listening.");
},
close: function() {
if (this.serverSocket) {
this.serverSocket.close();
this.serverSocket = null;
}
}
};
// Set up the transport connection and ensure |notifyTransportReady| triggered
// at both sides.
function setup() {
clientBuilder = Cc["@mozilla.org/presentation/presentationtcpsessiontransport;1"]
.createInstance(Ci.nsIPresentationTCPSessionTransportBuilder);
clientBuilder.buildTCPReceiverTransport(serverChannelDescription, clientListener);
}
// Test |selfAddress| attribute of |nsIPresentationSessionTransport|.
function selfAddress() {
var serverSelfAddress = serverTransport.selfAddress;
Assert.equal(serverSelfAddress.address, address.data, "The self address of server transport should be set.");
Assert.equal(serverSelfAddress.port, testServer.serverSocket.port, "The port of server transport should be set.");
var clientSelfAddress = clientTransport.selfAddress;
Assert.ok(clientSelfAddress.address, "The self address of client transport should be set.");
Assert.ok(clientSelfAddress.port, "The port of client transport should be set.");
run_next_test();
}
// Test the client sends a message and then a corresponding notification gets
// triggered at the server side.
function clientSendMessage() {
clientTransport.send(clientMessage);
}
// Test the server sends a message an then a corresponding notification gets
// triggered at the client side.
function serverSendMessage() {
serverTransport.send(serverMessage);
// The client enables data notification even after the incoming message has
// been sent, and should still be able to consume it.
clientTransport.enableDataNotification();
}
function transportClose() {
clientTransport.close(Cr.NS_OK);
}
function shutdown() {
testServer.close();
run_next_test();
}
add_test(setup);
add_test(selfAddress);
add_test(clientSendMessage);
add_test(serverSendMessage);
add_test(transportClose);
add_test(shutdown);
function run_test() {
testServer = new TestServer();
// Get the port of the test server.
serverChannelDescription.tcpPort = testServer.serverSocket.port;
run_next_test();
}
|