| OLD | NEW |
| (Empty) | |
| 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 // |
| 5 // VMOptions= |
| 6 // VMOptions=--short_socket_read |
| 7 // VMOptions=--short_socket_write |
| 8 // VMOptions=--short_socket_read --short_socket_write |
| 9 |
| 10 #library("StreamPipeTest"); |
| 11 #source("TestingServer.dart"); |
| 12 |
| 13 // Helper method to be able to run the test from the runtime |
| 14 // directory, or the top directory. |
| 15 String getDataFilename(String path) => |
| 16 new File(path).existsSync() ? path : '../' + path; |
| 17 |
| 18 |
| 19 bool compareFileContent(String fileName1, |
| 20 String fileName2, |
| 21 [int file1Offset = 0, |
| 22 int file2Offset = 0, |
| 23 int count]) { |
| 24 var file1 = new File(fileName1).openSync(); |
| 25 var file2 = new File(fileName2).openSync(); |
| 26 var length1 = file1.lengthSync(); |
| 27 var length2 = file2.lengthSync(); |
| 28 if (file1Offset == 0 && file2Offset == 0 && count == null) { |
| 29 if (length1 != length2) { |
| 30 file1.closeSync(); |
| 31 file2.closeSync(); |
| 32 return false; |
| 33 } |
| 34 } |
| 35 if (count == null) count = length1; |
| 36 var data1 = new List<int>(count); |
| 37 var data2 = new List<int>(count); |
| 38 if (file1Offset != 0) file1.setPositionSync(file1Offset); |
| 39 if (file2Offset != 0) file2.setPositionSync(file2Offset); |
| 40 var read1 = file1.readListSync(data1, 0, count); |
| 41 Expect.equals(count, read1); |
| 42 var read2 = file2.readListSync(data2, 0, count); |
| 43 Expect.equals(count, read2); |
| 44 for (var i = 0; i < count; i++) { |
| 45 if (data1[i] != data2[i]) { |
| 46 file1.closeSync(); |
| 47 file2.closeSync(); |
| 48 return false; |
| 49 } |
| 50 } |
| 51 file1.closeSync(); |
| 52 file2.closeSync(); |
| 53 return true; |
| 54 } |
| 55 |
| 56 |
| 57 // This test does: |
| 58 // 1. Opens a socket to the testing server. |
| 59 // 2. Pipes the content of a file to that sockets input stream. |
| 60 // 3. Creates a temp file. |
| 61 // 4. Pipes the socket output stream to the temp file. |
| 62 // 5. Expects the original file and the temp file to be equal. |
| 63 class PipeServerGame { |
| 64 int count = 0; |
| 65 |
| 66 PipeServerGame.start() |
| 67 : _receivePort = new ReceivePort(), |
| 68 _sendPort = null, |
| 69 _messages = 0 { |
| 70 new PipeServer().spawn().then((SendPort port) { |
| 71 _sendPort = port; |
| 72 start(); |
| 73 }); |
| 74 } |
| 75 |
| 76 void runTest() { |
| 77 |
| 78 void connectHandler() { |
| 79 String srcFileName = |
| 80 getDataFilename("tests/standalone/src/data/dart-logo.png"); |
| 81 |
| 82 SocketOutputStream socketOutput = _socket.outputStream; |
| 83 InputStream fileInput = new File(srcFileName).openInputStream(); |
| 84 |
| 85 fileInput.closeHandler = () { |
| 86 SocketInputStream socketInput = _socket.inputStream; |
| 87 var tempDir = new Directory(''); |
| 88 tempDir.createTempSync(); |
| 89 var dstFileName = tempDir.path + "/dart-logo.png"; |
| 90 var dstFile = new File(dstFileName); |
| 91 dstFile.createSync(); |
| 92 var fileOutput = dstFile.openOutputStream(); |
| 93 |
| 94 socketInput.closeHandler = () { |
| 95 // Check that the resulting file is equal to the initial |
| 96 // file. |
| 97 bool result = compareFileContent(srcFileName, dstFileName); |
| 98 new File(dstFileName).deleteSync(); |
| 99 Expect.isTrue(result); |
| 100 |
| 101 _socket.close(); |
| 102 |
| 103 // Run this twice. |
| 104 if (count++ < 2) { |
| 105 runTest(); |
| 106 } else { |
| 107 shutdown(); |
| 108 } |
| 109 }; |
| 110 |
| 111 socketInput.pipe(fileOutput); |
| 112 }; |
| 113 |
| 114 fileInput.pipe(socketOutput); |
| 115 } |
| 116 |
| 117 // Connect to the server. |
| 118 _socket = new Socket(TestingServer.HOST, _port); |
| 119 if (_socket !== null) { |
| 120 _socket.connectHandler = connectHandler; |
| 121 } else { |
| 122 Expect.fail("socket creation failed"); |
| 123 } |
| 124 } |
| 125 |
| 126 void start() { |
| 127 _receivePort.receive((var message, SendPort replyTo) { |
| 128 _port = message; |
| 129 runTest(); |
| 130 }); |
| 131 _sendPort.send(TestingServer.INIT, _receivePort.toSendPort()); |
| 132 } |
| 133 |
| 134 void shutdown() { |
| 135 _sendPort.send(TestingServer.SHUTDOWN, _receivePort.toSendPort()); |
| 136 _receivePort.close(); |
| 137 } |
| 138 |
| 139 int _port; |
| 140 ReceivePort _receivePort; |
| 141 SendPort _sendPort; |
| 142 Socket _socket; |
| 143 int _messages; |
| 144 } |
| 145 |
| 146 |
| 147 // The testing server will simply pipe each connecting sockets input |
| 148 // stream to its output stream. |
| 149 class PipeServer extends TestingServer { |
| 150 void connectionHandler(Socket connection) { |
| 151 connection.errorHandler = () { Expect.fail("Socket error"); }; |
| 152 connection.inputStream.pipe(connection.outputStream); |
| 153 } |
| 154 } |
| 155 |
| 156 |
| 157 // Test piping from one file to another and closing both streams |
| 158 // after wards. |
| 159 testFileToFilePipe1() { |
| 160 String srcFileName = |
| 161 getDataFilename("tests/standalone/src/data/dart-logo.png"); |
| 162 var srcStream = new File(srcFileName).openInputStream(); |
| 163 |
| 164 var tempDir = new Directory(''); |
| 165 tempDir.createTempSync(); |
| 166 String dstFileName = tempDir.path + "/dart-logo.png"; |
| 167 new File(dstFileName).createSync(); |
| 168 var dstStream = new File(dstFileName).openOutputStream(); |
| 169 |
| 170 srcStream.closeHandler = () { |
| 171 bool result = compareFileContent(srcFileName, dstFileName); |
| 172 new File(dstFileName).deleteSync(); |
| 173 Expect.isTrue(result); |
| 174 }; |
| 175 |
| 176 srcStream.pipe(dstStream); |
| 177 } |
| 178 |
| 179 |
| 180 // Test piping from one file to another and write additional data to |
| 181 // the output stream after piping finished. |
| 182 testFileToFilePipe2() { |
| 183 String srcFileName = |
| 184 getDataFilename("tests/standalone/src/data/dart-logo.png"); |
| 185 var srcFile = new File(srcFileName); |
| 186 var srcStream = srcFile.openInputStream(); |
| 187 |
| 188 var tempDir = new Directory(''); |
| 189 tempDir.createTempSync(); |
| 190 var dstFileName = tempDir.path + "/dart-logo.png"; |
| 191 var dstFile = new File(dstFileName); |
| 192 dstFile.createSync(); |
| 193 var dstStream = dstFile.openOutputStream(); |
| 194 |
| 195 srcStream.closeHandler = () { |
| 196 dstStream.write([32]); |
| 197 dstStream.close(); |
| 198 var src = srcFile.openSync(); |
| 199 var dst = dstFile.openSync(); |
| 200 var srcLength = src.lengthSync(); |
| 201 var dstLength = dst.lengthSync(); |
| 202 Expect.equals(srcLength + 1, dstLength); |
| 203 Expect.isTrue(compareFileContent(srcFileName, |
| 204 dstFileName, |
| 205 count: srcLength)); |
| 206 dst.setPositionSync(srcLength); |
| 207 var data = new List<int>(1); |
| 208 var read2 = dst.readListSync(data, 0, 1); |
| 209 Expect.equals(32, data[0]); |
| 210 src.closeSync(); |
| 211 dst.closeSync(); |
| 212 dstFile.deleteSync(); |
| 213 }; |
| 214 |
| 215 srcStream.pipe(dstStream, close: false); |
| 216 } |
| 217 |
| 218 |
| 219 // Test piping two copies of one file to another. |
| 220 testFileToFilePipe3() { |
| 221 String srcFileName = |
| 222 getDataFilename("tests/standalone/src/data/dart-logo.png"); |
| 223 var srcFile = new File(srcFileName); |
| 224 var srcStream = srcFile.openInputStream(); |
| 225 |
| 226 var tempDir = new Directory(''); |
| 227 tempDir.createTempSync(); |
| 228 var dstFileName = tempDir.path + "/dart-logo.png"; |
| 229 var dstFile = new File(dstFileName); |
| 230 dstFile.createSync(); |
| 231 var dstStream = dstFile.openOutputStream(); |
| 232 |
| 233 srcStream.closeHandler = () { |
| 234 var srcStream2 = srcFile.openInputStream(); |
| 235 |
| 236 srcStream2.closeHandler = () { |
| 237 var src = srcFile.openSync(); |
| 238 var dst = dstFile.openSync(); |
| 239 var srcLength = src.lengthSync(); |
| 240 var dstLength = dst.lengthSync(); |
| 241 Expect.equals(srcLength * 2, dstLength); |
| 242 Expect.isTrue(compareFileContent(srcFileName, |
| 243 dstFileName, |
| 244 count: srcLength)); |
| 245 Expect.isTrue(compareFileContent(srcFileName, |
| 246 dstFileName, |
| 247 file2Offset: srcLength, |
| 248 count: srcLength)); |
| 249 src.closeSync(); |
| 250 dst.closeSync(); |
| 251 dstFile.deleteSync(); |
| 252 }; |
| 253 |
| 254 // Pipe another copy of the source file. |
| 255 srcStream2.pipe(dstStream); |
| 256 }; |
| 257 |
| 258 srcStream.pipe(dstStream, close: false); |
| 259 } |
| 260 |
| 261 |
| 262 main() { |
| 263 testFileToFilePipe1(); |
| 264 testFileToFilePipe2(); |
| 265 testFileToFilePipe3(); |
| 266 PipeServerGame echoServerGame = new PipeServerGame.start(); |
| 267 } |
| OLD | NEW |