Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | <!DOCTYPE HTML> |
michael@0 | 2 | <html> |
michael@0 | 3 | <head> |
michael@0 | 4 | <title>Basic WebSocket test</title> |
michael@0 | 5 | <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script> |
michael@0 | 6 | <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" /> |
michael@0 | 7 | </head> |
michael@0 | 8 | |
michael@0 | 9 | <body onload="testWebSocket()"> |
michael@0 | 10 | <a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=472529">Mozilla Bug 472529</a> |
michael@0 | 11 | <p id="display"></p> |
michael@0 | 12 | <div id="content" style="display: none"> |
michael@0 | 13 | </div> |
michael@0 | 14 | <pre id="test"> |
michael@0 | 15 | <script class="testbody" type="text/javascript"> |
michael@0 | 16 | |
michael@0 | 17 | const kUrl = "ws://mochi.test:8888/tests/content/base/test/file_websocket_basic"; |
michael@0 | 18 | |
michael@0 | 19 | var gTestElement; |
michael@0 | 20 | var ws; |
michael@0 | 21 | |
michael@0 | 22 | function forcegc() { |
michael@0 | 23 | SpecialPowers.forceGC(); |
michael@0 | 24 | SpecialPowers.gc(); |
michael@0 | 25 | } |
michael@0 | 26 | |
michael@0 | 27 | function testWebSocket() { |
michael@0 | 28 | gTestElement = document.getElementById("test"); |
michael@0 | 29 | |
michael@0 | 30 | SimpleTest.executeSoon(testWebSocket1); |
michael@0 | 31 | } |
michael@0 | 32 | |
michael@0 | 33 | /** |
michael@0 | 34 | * Sends message keywords, then receives their values. |
michael@0 | 35 | */ |
michael@0 | 36 | function testWebSocket1() { |
michael@0 | 37 | gTestElement.textContent = "Running testWebSocket1()"; |
michael@0 | 38 | |
michael@0 | 39 | var results = ["test", |
michael@0 | 40 | "/tests/content/base/test/file_websocket_basic", |
michael@0 | 41 | "http://mochi.test:8888", |
michael@0 | 42 | "end"]; |
michael@0 | 43 | |
michael@0 | 44 | ws = new WebSocket(kUrl, "test"); |
michael@0 | 45 | is(ws.url, kUrl, "[1] WebSocket.url"); |
michael@0 | 46 | ws.onopen = function(e) { |
michael@0 | 47 | const params = ["protocol", "resource", "origin", "end"]; |
michael@0 | 48 | |
michael@0 | 49 | gTestElement.textContent += "\nSending :"; |
michael@0 | 50 | for (var i = 0; i < params.length; ++i) { |
michael@0 | 51 | gTestElement.textContent += " " + params[i]; |
michael@0 | 52 | ws.send(params[i]); |
michael@0 | 53 | } |
michael@0 | 54 | |
michael@0 | 55 | // Set this before onmessage() is called, so it is displayed once only. |
michael@0 | 56 | gTestElement.textContent += "\nReceived:"; |
michael@0 | 57 | }; |
michael@0 | 58 | ws.onclose = function(e) { |
michael@0 | 59 | is(results.length, 0, "[1] Number of unreceived messages"); |
michael@0 | 60 | ok(e.wasClean, "[1] Connection closed cleanly"); |
michael@0 | 61 | |
michael@0 | 62 | SimpleTest.executeSoon(testWebSocket2); |
michael@0 | 63 | }; |
michael@0 | 64 | ws.onerror = function(e) { |
michael@0 | 65 | ok(false, "[1] onerror() should not have been called!"); |
michael@0 | 66 | gTestElement.textContent += "\nonerror() should not have been called!"; |
michael@0 | 67 | SimpleTest.executeSoon(SimpleTest.finish); |
michael@0 | 68 | }; |
michael@0 | 69 | ws.onmessage = function(e) { |
michael@0 | 70 | is(e.data, results[0], "[1] Received message"); |
michael@0 | 71 | gTestElement.textContent += " " + e.data; |
michael@0 | 72 | results.shift(); |
michael@0 | 73 | }; |
michael@0 | 74 | } |
michael@0 | 75 | |
michael@0 | 76 | /** |
michael@0 | 77 | * Sends 1000+1 test messages, then receives them. |
michael@0 | 78 | */ |
michael@0 | 79 | function testWebSocket2() { |
michael@0 | 80 | gTestElement.textContent = "Running testWebSocket2()"; |
michael@0 | 81 | |
michael@0 | 82 | const displayInterval = 100; |
michael@0 | 83 | const testCount = 1000; |
michael@0 | 84 | const testMessage = "test message 2."; |
michael@0 | 85 | |
michael@0 | 86 | var messageCount = 0; |
michael@0 | 87 | |
michael@0 | 88 | ws = new WebSocket(kUrl, "test"); |
michael@0 | 89 | ws.onopen = function(e) { |
michael@0 | 90 | gTestElement.textContent += "\nSending :"; |
michael@0 | 91 | for (var i = 1; i <= testCount; ++i) { |
michael@0 | 92 | if (i % displayInterval == 1) { |
michael@0 | 93 | gTestElement.textContent += " " + i; |
michael@0 | 94 | } |
michael@0 | 95 | ws.send(testMessage + i); |
michael@0 | 96 | } |
michael@0 | 97 | gTestElement.textContent += " end"; |
michael@0 | 98 | ws.send("end"); |
michael@0 | 99 | |
michael@0 | 100 | // Set this before onmessage() is called, so it is displayed once only. |
michael@0 | 101 | gTestElement.textContent += "\nReceived:"; |
michael@0 | 102 | }; |
michael@0 | 103 | ws.onclose = function(e) { |
michael@0 | 104 | is(messageCount, testCount + 1, "[2] Number of received messages"); |
michael@0 | 105 | ok(e.wasClean, "[2] Connection closed cleanly"); |
michael@0 | 106 | |
michael@0 | 107 | SimpleTest.executeSoon(testWebSocket3); |
michael@0 | 108 | }; |
michael@0 | 109 | ws.onerror = function(e) { |
michael@0 | 110 | ok(false, "[2] onerror() should not have been called!"); |
michael@0 | 111 | gTestElement.textContent += "\nonerror() should not have been called!"; |
michael@0 | 112 | SimpleTest.executeSoon(SimpleTest.finish); |
michael@0 | 113 | }; |
michael@0 | 114 | ws.onmessage = function(e) { |
michael@0 | 115 | ++messageCount; |
michael@0 | 116 | if (messageCount > testCount) |
michael@0 | 117 | is(e.data, "end", "[2] Received message"); |
michael@0 | 118 | else |
michael@0 | 119 | is(e.data, testMessage + messageCount, "[2] Received message"); |
michael@0 | 120 | if (messageCount % displayInterval == 1) { |
michael@0 | 121 | gTestElement.textContent += " " + messageCount; |
michael@0 | 122 | } |
michael@0 | 123 | }; |
michael@0 | 124 | } |
michael@0 | 125 | |
michael@0 | 126 | /** |
michael@0 | 127 | * Sends testcount+1 test messages, then receives them, calling forcegc() at each step. |
michael@0 | 128 | */ |
michael@0 | 129 | function testWebSocket3() { |
michael@0 | 130 | gTestElement.textContent = "Running testWebSocket3() [can take a little while]"; |
michael@0 | 131 | |
michael@0 | 132 | const displayInterval = 10; |
michael@0 | 133 | const testCount = 10; |
michael@0 | 134 | const testMessage = "test message 3."; |
michael@0 | 135 | |
michael@0 | 136 | var messageCount = 0; |
michael@0 | 137 | |
michael@0 | 138 | ws = new WebSocket(kUrl, "test"); |
michael@0 | 139 | // Set this before onopen() is called, |
michael@0 | 140 | // otherwise its display would be delayed by forcegc() calls... |
michael@0 | 141 | gTestElement.textContent += "\nSending :"; |
michael@0 | 142 | ws.onopen = function(e) { |
michael@0 | 143 | for (var i = 1; i <= testCount; ++i) { |
michael@0 | 144 | forcegc(); |
michael@0 | 145 | if (i % displayInterval == 1) { |
michael@0 | 146 | // Actual display is delayed by forcegc() calls... |
michael@0 | 147 | gTestElement.textContent += " " + i; |
michael@0 | 148 | } |
michael@0 | 149 | ws.send(testMessage + i); |
michael@0 | 150 | } |
michael@0 | 151 | forcegc(); |
michael@0 | 152 | gTestElement.textContent += " end"; |
michael@0 | 153 | ws.send("end"); |
michael@0 | 154 | |
michael@0 | 155 | // Set this before onmessage() is called, so it is displayed once only. |
michael@0 | 156 | gTestElement.textContent += "\nReceived:"; |
michael@0 | 157 | }; |
michael@0 | 158 | ws.onclose = function(e) { |
michael@0 | 159 | is(messageCount, testCount + 1, "[3] Number of received messages"); |
michael@0 | 160 | ok(e.wasClean, "[3] Connection closed cleanly"); |
michael@0 | 161 | |
michael@0 | 162 | SimpleTest.executeSoon(testWebSocket4); |
michael@0 | 163 | }; |
michael@0 | 164 | ws.onerror = function(e) { |
michael@0 | 165 | ok(false, "[3] onerror() should not have been called!"); |
michael@0 | 166 | gTestElement.textContent += "\nonerror() should not have been called!"; |
michael@0 | 167 | SimpleTest.executeSoon(SimpleTest.finish); |
michael@0 | 168 | }; |
michael@0 | 169 | ws.onmessage = function(e) { |
michael@0 | 170 | forcegc(); |
michael@0 | 171 | ++messageCount; |
michael@0 | 172 | if (messageCount > testCount) |
michael@0 | 173 | is(e.data, "end", "[3] Received message"); |
michael@0 | 174 | else |
michael@0 | 175 | is(e.data, testMessage + messageCount, "[3] Received message"); |
michael@0 | 176 | if (messageCount % displayInterval == 1) { |
michael@0 | 177 | // Actual display is delayed by forcegc() call(s)... |
michael@0 | 178 | gTestElement.textContent += " " + messageCount; |
michael@0 | 179 | } |
michael@0 | 180 | }; |
michael@0 | 181 | } |
michael@0 | 182 | |
michael@0 | 183 | /** |
michael@0 | 184 | * Sends a huge test message, then receives it, then closes the WebSocket from client-side. |
michael@0 | 185 | */ |
michael@0 | 186 | function testWebSocket4() { |
michael@0 | 187 | gTestElement.textContent = "Running testWebSocket4()"; |
michael@0 | 188 | |
michael@0 | 189 | // String length = 13 + ((10,000 - 1) * 26) + 11 = 259,998 = almost 254 KiB. |
michael@0 | 190 | const longString = "messageStart " + new Array(10000).join(" -huge WebSocket message- ") + " messageEnd"; |
michael@0 | 191 | |
michael@0 | 192 | ws = new WebSocket(kUrl, "test"); |
michael@0 | 193 | ws.onopen = function(e) { |
michael@0 | 194 | is(this, ws, "[4, onopen()] 'this' should point to the WebSocket."); |
michael@0 | 195 | gTestElement.textContent += "\nSending the huge message"; |
michael@0 | 196 | ws.send(longString); |
michael@0 | 197 | }; |
michael@0 | 198 | ws.onclose = function(e) { |
michael@0 | 199 | is(this, ws, "[4, onclose()] 'this' should point to the WebSocket."); |
michael@0 | 200 | ok(e.wasClean, "[4] Connection closed cleanly"); |
michael@0 | 201 | |
michael@0 | 202 | SimpleTest.executeSoon(testWebSocket5); |
michael@0 | 203 | }; |
michael@0 | 204 | ws.onerror = function(e) { |
michael@0 | 205 | is(this, ws, "[4, onerror()] 'this' should point to the WebSocket."); |
michael@0 | 206 | ok(false, "[4, onerror()] should not have been called!"); |
michael@0 | 207 | gTestElement.textContent += "\nonerror() should not have been called!"; |
michael@0 | 208 | SimpleTest.executeSoon(SimpleTest.finish); |
michael@0 | 209 | }; |
michael@0 | 210 | ws.onmessage = function(e) { |
michael@0 | 211 | is(this, ws, "[4, onmessage()] 'this' should point to the WebSocket."); |
michael@0 | 212 | // Do not use |is(e.data, longString, "...");| that results in a _very_ long line. |
michael@0 | 213 | is(e.data.length, longString.length, "[4] Length of received message"); |
michael@0 | 214 | ok(e.data == longString, "[4] Content of received message"); |
michael@0 | 215 | gTestElement.textContent += "\nReceived the huge message"; |
michael@0 | 216 | this.close(); |
michael@0 | 217 | }; |
michael@0 | 218 | } |
michael@0 | 219 | |
michael@0 | 220 | /** |
michael@0 | 221 | * Closes the WebSocket from client-side, then sends a test message that should be buffered. |
michael@0 | 222 | */ |
michael@0 | 223 | function testWebSocket5() { |
michael@0 | 224 | gTestElement.textContent = "Running testWebSocket5()"; |
michael@0 | 225 | |
michael@0 | 226 | ws = new WebSocket(kUrl, "test"); |
michael@0 | 227 | ws.onopen = function(e) { |
michael@0 | 228 | is(this.bufferedAmount, 0, "[5] Length of empty buffer before closing"); |
michael@0 | 229 | this.close(); |
michael@0 | 230 | }; |
michael@0 | 231 | ws.onclose = function(e) { |
michael@0 | 232 | ok(e.wasClean, "[5] Connection closed cleanly"); |
michael@0 | 233 | is(this.bufferedAmount, 0, "[5] Length of empty buffer after closing"); |
michael@0 | 234 | |
michael@0 | 235 | var msg = "test message to be buffered"; |
michael@0 | 236 | this.send(msg); |
michael@0 | 237 | is(this.bufferedAmount, msg.length, "[5] Length of buffered message sent after closing"); |
michael@0 | 238 | |
michael@0 | 239 | gTestElement.textContent += "\ntestWebSocket5() completed"; |
michael@0 | 240 | |
michael@0 | 241 | SimpleTest.executeSoon(SimpleTest.finish); |
michael@0 | 242 | }; |
michael@0 | 243 | ws.onerror = function(e) { |
michael@0 | 244 | ok(false, "[5] onerror() should not have been called!"); |
michael@0 | 245 | gTestElement.textContent += "\nonerror() should not have been called!"; |
michael@0 | 246 | SimpleTest.executeSoon(SimpleTest.finish); |
michael@0 | 247 | }; |
michael@0 | 248 | } |
michael@0 | 249 | |
michael@0 | 250 | SimpleTest.waitForExplicitFinish(); |
michael@0 | 251 | |
michael@0 | 252 | </script> |
michael@0 | 253 | </pre> |
michael@0 | 254 | </body> |
michael@0 | 255 | </html> |