Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 // net/tools/testserver/testserver.py is picky about the format of what it | |
| 6 // calls its "echo" messages. One might go so far as to mutter to oneself that | |
| 7 // it isn't an echo server at all. | |
| 8 // | |
| 9 // The response is based on the request but obfuscated using a random key. | |
| 10 const request = "0100000005320000005hello"; | |
| 11 var expectedResponsePattern = /0100000005320000005.{11}/; | |
| 12 | |
| 5 const socket = chrome.experimental.socket; | 13 const socket = chrome.experimental.socket; |
| 6 const message = "helloECHO"; | 14 var address; |
| 7 const address = "127.0.0.1"; | 15 var bytesWritten = 0; |
| 16 var dataAsString; | |
| 17 var dataRead = []; | |
| 18 var port = -1; | |
| 8 var protocol = "none"; | 19 var protocol = "none"; |
| 9 var port = -1; | |
| 10 var socketId = 0; | 20 var socketId = 0; |
| 11 var dataRead = ""; | |
| 12 var succeeded = false; | 21 var succeeded = false; |
| 13 var waitCount = 0; | 22 var waitCount = 0; |
| 14 | 23 |
| 15 var testSocketCreation = function() { | 24 // Many thanks to Dennis for his StackOverflow answer: http://goo.gl/UDanx |
| 16 function onCreate(socketInfo) { | 25 function string2ArrayBuffer(string, callback) { |
| 17 chrome.test.assertTrue(socketInfo.socketId > 0); | 26 var bb = new WebKitBlobBuilder(); |
| 27 bb.append(string); | |
| 28 var f = new FileReader(); | |
| 29 f.onload = function(e) { | |
| 30 callback(e.target.result); | |
| 31 } | |
|
asargent_no_longer_on_chrome
2012/04/16 22:51:13
nit: add semicolon after } here
miket_OOO
2012/04/16 23:15:53
That is insanely stupid code. I am not going to du
| |
| 32 f.readAsArrayBuffer(bb.getBlob()); | |
| 33 } | |
| 18 | 34 |
| 19 // TODO(miket): this doesn't work yet. It's possible this will become | 35 function arrayBuffer2String(buf, callback) { |
| 20 // automatic, but either way we can't forget to clean up. | 36 var bb = new WebKitBlobBuilder(); |
| 21 //socket.destroy(socketInfo.socketId); | 37 bb.append(buf); |
| 38 var f = new FileReader(); | |
| 39 f.onload = function(e) { | |
| 40 callback(e.target.result); | |
| 41 } | |
|
asargent_no_longer_on_chrome
2012/04/16 22:51:13
same nit here
| |
| 42 f.readAsText(bb.getBlob()); | |
| 43 } | |
| 22 | 44 |
| 23 chrome.test.succeed(); | 45 function arrayBufferToArrayOfLongs(arrayBuffer) { |
| 46 var longs = []; | |
| 47 var arrayBufferView = new Uint8Array(arrayBuffer); | |
| 48 for (var i = 0; i < arrayBufferView.length; ++i) { | |
| 49 longs[i] = arrayBufferView[i]; | |
| 24 } | 50 } |
| 51 return longs; | |
| 52 } | |
| 25 | 53 |
| 26 socket.create(protocol, address, port, {}, onCreate); | 54 function arrayOfLongsToArrayBuffer(longs) { |
| 27 }; | 55 var arrayBuffer = new ArrayBuffer(longs.length); |
| 28 | 56 var arrayBufferView = new Uint8Array(arrayBuffer); |
| 29 // net/tools/testserver/testserver.py is picky about the format of what | 57 for (var i = 0; i < longs.length; ++i) { |
| 30 // it calls its "echo" messages. One might go so far as to mutter to | 58 arrayBufferView[i] = longs[i]; |
| 31 // oneself that it isn't an echo server at all. | 59 } |
| 32 // | 60 return arrayBuffer; |
| 33 // The response is based on the request but obfuscated using a random | 61 } |
| 34 // key. | |
| 35 const request = "0100000005320000005hello"; | |
| 36 var expectedResponsePattern = /0100000005320000005.{11}/; | |
| 37 | |
| 38 var address; | |
| 39 var protocol; | |
| 40 var port; | |
| 41 var socketId = 0; | |
| 42 var bytesWritten = 0; | |
| 43 var dataRead = ""; | |
| 44 var succeeded = false; | |
| 45 var waitCount = 0; | |
| 46 | 62 |
| 47 var testSocketCreation = function() { | 63 var testSocketCreation = function() { |
| 48 function onCreate(socketInfo) { | 64 function onCreate(socketInfo) { |
| 49 chrome.test.assertTrue(socketInfo.socketId > 0); | 65 chrome.test.assertTrue(socketInfo.socketId > 0); |
| 50 | 66 |
| 51 // TODO(miket): this doesn't work yet. It's possible this will become | 67 // TODO(miket): this doesn't work yet. It's possible this will become |
| 52 // automatic, but either way we can't forget to clean up. | 68 // automatic, but either way we can't forget to clean up. |
| 69 // | |
| 53 //socket.destroy(socketInfo.socketId); | 70 //socket.destroy(socketInfo.socketId); |
| 54 | 71 |
| 55 chrome.test.succeed(); | 72 chrome.test.succeed(); |
| 56 } | 73 } |
| 57 | 74 |
| 58 socket.create(protocol, address, port, {onEvent: function(e) {}}, onCreate); | 75 socket.create(protocol, address, port, {onEvent: function(e) {}}, onCreate); |
| 59 }; | 76 }; |
| 60 | 77 |
| 61 function onDataRead(readInfo) { | 78 function onDataRead(readInfo) { |
| 62 dataRead += readInfo.message; | 79 // TODO(miket): this isn't correct for multiple calls of onDataRead. |
| 63 if (dataRead.match(expectedResponsePattern)) { | 80 arrayBuffer2String(arrayOfLongsToArrayBuffer(readInfo.data), function(s) { |
| 64 succeeded = true; | 81 dataAsString = s; // save this for error reporting |
| 65 chrome.test.succeed(); | 82 if (s.match(expectedResponsePattern)) { |
| 66 } | 83 succeeded = true; |
| 84 chrome.test.succeed(); | |
| 85 } | |
| 86 }); | |
| 67 // Blocked. Wait for onEvent. | 87 // Blocked. Wait for onEvent. |
| 68 } | 88 } |
| 69 | 89 |
| 70 function onWriteComplete(writeInfo) { | 90 function onWriteComplete(writeInfo) { |
| 71 bytesWritten += writeInfo.bytesWritten; | 91 bytesWritten += writeInfo.bytesWritten; |
| 72 if (bytesWritten == request.length) { | 92 if (bytesWritten == request.length) { |
| 73 socket.read(socketId, onDataRead); | 93 socket.read(socketId, onDataRead); |
| 74 } | 94 } |
| 75 // Blocked. Wait for onEvent. | 95 // Blocked. Wait for onEvent. |
| 76 } | 96 } |
| 77 | 97 |
| 78 function onConnectComplete(connectResult) { | 98 function onConnectComplete(connectResult) { |
| 79 if (connectResult == 0) { | 99 if (connectResult == 0) { |
| 80 socket.write(socketId, request, onWriteComplete); | 100 string2ArrayBuffer(request, function(arrayBuffer) { |
| 101 var longs = arrayBufferToArrayOfLongs(arrayBuffer); | |
| 102 socket.write(socketId, longs, onWriteComplete); | |
| 103 }); | |
| 81 } | 104 } |
| 82 // Blocked. Wait for onEvent. | 105 // Blocked. Wait for onEvent. |
| 83 } | 106 } |
| 84 | 107 |
| 85 function onCreate(socketInfo) { | 108 function onCreate(socketInfo) { |
| 86 socketId = socketInfo.socketId; | 109 socketId = socketInfo.socketId; |
| 87 chrome.test.assertTrue(socketId > 0, "failed to create socket"); | 110 chrome.test.assertTrue(socketId > 0, "failed to create socket"); |
| 88 socket.connect(socketId, onConnectComplete); | 111 socket.connect(socketId, onConnectComplete); |
| 89 } | 112 } |
| 90 | 113 |
| 91 function onEvent(socketEvent) { | 114 function onEvent(socketEvent) { |
| 92 if (socketEvent.type == "connectComplete") { | 115 if (socketEvent.type == "connectComplete") { |
| 93 onConnectComplete(socketEvent.resultCode); | 116 onConnectComplete(socketEvent.resultCode); |
| 94 } else if (socketEvent.type == "dataRead") { | 117 } else if (socketEvent.type == "dataRead") { |
| 95 // TODO(miket): why one "message" and the other "data"? | 118 onDataRead({data: socketEvent.data}); |
| 96 onDataRead({message: socketEvent.data}); | |
| 97 } else if (socketEvent.type == "writeComplete") { | 119 } else if (socketEvent.type == "writeComplete") { |
| 98 onWriteComplete(socketEvent.resultCode); | 120 onWriteComplete(socketEvent.resultCode); |
| 99 } else { | 121 } else { |
| 100 console.log("Received unhandled socketEvent of type " + socketEvent.type); | 122 console.log("Received unhandled socketEvent of type " + socketEvent.type); |
| 101 } | 123 } |
| 102 }; | 124 }; |
| 103 | 125 |
| 104 function onRead(readInfo) { | |
| 105 if (readInfo.message == message) { | |
| 106 succeeded = true; | |
| 107 chrome.test.succeed(); | |
| 108 } else { | |
| 109 // The read blocked. Save what we've got so far, and wait for onEvent. | |
| 110 dataRead = readInfo.message; | |
| 111 } | |
| 112 } | |
| 113 | |
| 114 function onWrite(writeInfo) { | |
| 115 chrome.test.assertTrue(writeInfo.bytesWritten == message.length); | |
| 116 socket.read(socketId, onRead); | |
| 117 } | |
| 118 | |
| 119 function onConnect(connectResult) { | |
| 120 chrome.test.assertTrue(connectResult); | |
| 121 socket.write(socketId, message, onWrite); | |
| 122 } | |
| 123 | |
| 124 function waitForBlockingOperation() { | 126 function waitForBlockingOperation() { |
| 125 if (++waitCount < 10) { | 127 if (++waitCount < 10) { |
| 126 setTimeout(waitForBlockingOperation, 1000); | 128 setTimeout(waitForBlockingOperation, 1000); |
| 127 } else { | 129 } else { |
| 128 // We weren't able to succeed in the given time. | 130 // We weren't able to succeed in the given time. |
| 129 chrome.test.fail("Operations didn't complete after " + waitCount + " " + | 131 chrome.test.fail("Operations didn't complete after " + waitCount + " " + |
| 130 "seconds. Response so far was <" + dataRead + ">."); | 132 "seconds. Response so far was <" + dataAsString + ">."); |
| 131 } | 133 } |
| 132 } | 134 } |
| 133 | 135 |
| 134 var testSending = function() { | 136 var testSending = function() { |
| 135 dataRead = ""; | 137 dataRead = ""; |
| 136 succeeded = false; | 138 succeeded = false; |
| 137 waitCount = 0; | 139 waitCount = 0; |
| 138 | 140 |
| 139 setTimeout(waitForBlockingOperation, 1000); | 141 setTimeout(waitForBlockingOperation, 1000); |
| 140 socket.create(protocol, address, port, { onEvent: onEvent }, onCreate); | 142 socket.create(protocol, address, port, { onEvent: onEvent }, onCreate); |
| 141 }; | 143 }; |
| 142 | 144 |
| 143 var onMessageReply = function(message) { | 145 var onMessageReply = function(message) { |
| 144 var parts = message.split(":"); | 146 var parts = message.split(":"); |
| 145 protocol = parts[0]; | 147 protocol = parts[0]; |
| 146 address = parts[1]; | 148 address = parts[1]; |
| 147 port = parseInt(parts[2]); | 149 port = parseInt(parts[2]); |
| 148 console.log("Running tests, protocol " + protocol + ", echo server " + | 150 console.log("Running tests, protocol " + protocol + ", echo server " + |
| 149 address + ":" + port); | 151 address + ":" + port); |
| 150 chrome.test.runTests([ testSocketCreation, testSending ]); | 152 chrome.test.runTests([ testSocketCreation, testSending ]); |
| 151 }; | 153 }; |
| 152 | 154 |
| 153 // Find out which protocol we're supposed to test, and which echo server we | 155 // Find out which protocol we're supposed to test, and which echo server we |
| 154 // should be using, then kick off the tests. | 156 // should be using, then kick off the tests. |
| 155 chrome.test.sendMessage("info_please", onMessageReply); | 157 chrome.test.sendMessage("info_please", onMessageReply); |
| OLD | NEW |