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