7 var v8tools = sysapps_raw_socket_test.v8tools;
8 var api = xwalk.experimental.raw_socket;
20 function runNextTest() {
21 test_list[current_test++]();
24 function reportFail(message) {
26 document.title = "Fail";
30 document.title = "Pass";
33 function memoryManagement() {
35 var garbageCollectionCount = 0;
37 // We cannot run the GC inside a callback, because the callee of the
38 // callback holds a reference (that is why runGCandCheck is called from
39 // a setTimeout. Another important thing to notice is nullifying the
40 // server or client object right away won't work because they use
41 // delayed initialization, which is fine, because as soon as the
42 // initialization is completed, the object will be collected if no
43 // variable is keeping a reference to it.
44 function runGCandCheck() {
45 // The GC is called here more than once to make sure
46 // all the dangling objects get collected. Calling the GC
47 // one time does not give a guarantee that everything that
48 // can be collected will be collected.
53 if (garbageCollectionCount != 3)
54 reportFail("TCPSocket, TCPServerSocket or UDPSocket is leaking.");
59 var server = new api.TCPServerSocket(
60 {"localAddress": "127.0.0.1", "localPort": 54321});
61 server.onerror = server.onopen = nullifyServer;
62 server.tracker = v8tools.lifecycleTracker();
63 server.tracker.destructor = function() {
64 garbageCollectionCount++;
67 function nullifyServer() {
68 server.onerror = null;
72 if (++eventCount == 3)
73 setTimeout(runGCandCheck, 0);
76 var client = new api.TCPSocket("127.0.0.1", 54321);
77 client.onerror = client.onopen = nullifyClient;
78 client.tracker = v8tools.lifecycleTracker();
79 client.tracker.destructor = function() {
80 garbageCollectionCount++;
83 function nullifyClient() {
84 client.onerror = null;
88 if (++eventCount == 3)
89 setTimeout(runGCandCheck, 0);
92 var udp = new api.UDPSocket({localAddress: "127.0.0.1", localPort: 7000});
93 udp.onerror = udp.onopen = nullifyUDP;
94 udp.tracker = v8tools.lifecycleTracker();
95 udp.tracker.destructor = function() {
96 garbageCollectionCount++;
99 function nullifyUDP() {
104 if (++eventCount == 3)
105 setTimeout(runGCandCheck, 0);
109 // This test is designed like a data ping pong. First we open a server
110 // listening for incoming connections. Secondly we connect a client socket
111 // into this server, that immediately sends a greetings message that is
112 // readily verified. The client then sends back some data to the server,
113 // which also gets checked.
115 // This is not an API conformance test, it just verifies if the basic
116 // functionality works. W3C should provide the former.
117 function pingPongTCP(serverPort) {
118 serverPort = serverPort || 5000;
119 var serverPortMax = 5020;
120 var testData = "Hello World!";
122 var server = new api.TCPServerSocket(
123 {"localAddress": "127.0.0.1", "localPort": serverPort});
125 server.onerror = function() {
126 // The default port might be busy, so we try different ports
127 // before reporting failure.
128 if (serverPort < serverPortMax)
129 pingPongTCP(++serverPort);
131 reportFail("Not able to listen at port " + serverPort + ".");
134 server.onopen = function() {
135 var client = new api.TCPSocket("127.0.0.1", serverPort);
137 client.onerror = function() {
138 reportFail("Not able to connect to port " + serverPort + ".");
141 client.ondata = function(event) {
142 var view = new Uint8Array(event.data);
143 var data = String.fromCharCode.apply(null, view);
145 if (data != testData)
146 reportFail("Invalid data received by the client socket.");
148 client.send(testData);
152 server.onconnect = function(event) {
153 event.connectedSocket.send(testData);
154 event.connectedSocket.ondata = function (event) {
155 var view = new Uint8Array(event.data);
156 var data = String.fromCharCode.apply(null, view);
158 if (data != testData)
159 reportFail("Invalid data received by server socket.");
166 function pingPongUDP(serverPort) {
167 serverPort = serverPort || 6000;
168 var serverPortMax = 6020;
169 var testData = "Hello World!";
171 var server = new api.UDPSocket(
172 {"localAddress": "127.0.0.1", "localPort": serverPort});
174 server.onerror = function() {
175 // The default port might be busy, so we try different ports
176 // before reporting failure.
177 if (serverPort < serverPortMax)
178 pingPongUDP(++serverPort);
180 reportFail("Not able to listen at port " + serverPort + ".");
183 server.onopen = function() {
184 var client = new api.UDPSocket(
185 {remoteAddress: "127.0.0.1", remotePort: serverPort});
186 client.onopen = function() {
187 client.send(testData);
190 client.onerror = function() {
191 reportFail("Not able to connect to port " + serverPort + ".");
194 client.onmessage = function(event) {
195 var view = new Uint8Array(event.data);
196 var data = String.fromCharCode.apply(null, view);
198 if (data != testData)
199 reportFail("Invalid data received by the client socket.");
205 server.onmessage = function(event) {
206 var view = new Uint8Array(event.data);
207 var data = String.fromCharCode.apply(null, view);
209 if (data != testData)
210 reportFail("Invalid data received by server socket.");
212 server.send(data, event.remoteAddress, event.remotePort);
216 function serverPortBusy(Socket, serverPort) {
217 serverPort = serverPort || 7000;
218 var serverPortMax = 7020;
220 var server = new Socket(
221 {"localAddress": "127.0.0.1", "localPort": serverPort});
223 server.onerror = function() {
224 if (serverPort < serverPortMax)
225 serverPortBusy(Socket, ++serverPort);
227 reportFail("Not able to listen at port " + serverPort + ".");
230 server.onopen = function() {
231 var serverShouldFail = new Socket(
232 {"localAddress": "127.0.0.1", "localPort": serverPort});
234 // Should fail, port already in use by another server.
235 serverShouldFail.onerror = runNextTest();
236 serverShouldFail.onopen = function() {
237 reportFail("Port " + serverPort + " should be busy.");
242 function serverPortBusyTCP() {
243 serverPortBusy(api.TCPServerSocket);
246 function serverPortBusyUDP() {
247 serverPortBusy(api.UDPSocket);