| OLD | NEW |
| (Empty) |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 import 'dart:async'; | |
| 6 import 'dart:convert'; | |
| 7 import 'dart:io'; | |
| 8 import 'dart:typed_data'; | |
| 9 | |
| 10 import 'package:_mojo_for_test_only/expect.dart'; | |
| 11 import 'package:mojo/core.dart'; | |
| 12 | |
| 13 invalidHandleTest() { | |
| 14 MojoHandle invalidHandle = new MojoHandle(MojoHandle.INVALID); | |
| 15 | |
| 16 // Close. | |
| 17 int result = invalidHandle.close(); | |
| 18 Expect.isTrue(result == MojoResult.kInvalidArgument); | |
| 19 | |
| 20 // Wait. | |
| 21 MojoWaitResult mwr = | |
| 22 invalidHandle.wait(MojoHandleSignals.kReadWrite, 1000000); | |
| 23 Expect.isTrue(mwr.result == MojoResult.kInvalidArgument); | |
| 24 | |
| 25 MojoWaitManyResult mwmr = MojoHandle.waitMany([invalidHandle.h], | |
| 26 [MojoHandleSignals.kReadWrite], MojoHandle.DEADLINE_INDEFINITE); | |
| 27 Expect.isTrue(mwmr.result == MojoResult.kInvalidArgument); | |
| 28 | |
| 29 // Message pipe. | |
| 30 MojoMessagePipe pipe = new MojoMessagePipe(); | |
| 31 Expect.isNotNull(pipe); | |
| 32 ByteData bd = new ByteData(10); | |
| 33 pipe.endpoints[0].handle.close(); | |
| 34 pipe.endpoints[1].handle.close(); | |
| 35 result = pipe.endpoints[0].write(bd); | |
| 36 Expect.isTrue(result == MojoResult.kInvalidArgument); | |
| 37 | |
| 38 MojoMessagePipeReadResult readResult = pipe.endpoints[0].read(bd); | |
| 39 Expect.isTrue(pipe.endpoints[0].status == MojoResult.kInvalidArgument); | |
| 40 | |
| 41 // Data pipe. | |
| 42 MojoDataPipe dataPipe = new MojoDataPipe(); | |
| 43 Expect.isNotNull(dataPipe); | |
| 44 dataPipe.producer.handle.close(); | |
| 45 dataPipe.consumer.handle.close(); | |
| 46 | |
| 47 int bytesWritten = dataPipe.producer.write(bd); | |
| 48 Expect.isTrue(dataPipe.producer.status == MojoResult.kInvalidArgument); | |
| 49 | |
| 50 ByteData writeData = dataPipe.producer.beginWrite(10); | |
| 51 Expect.isNull(writeData); | |
| 52 Expect.isTrue(dataPipe.producer.status == MojoResult.kInvalidArgument); | |
| 53 dataPipe.producer.endWrite(10); | |
| 54 Expect.isTrue(dataPipe.producer.status == MojoResult.kInvalidArgument); | |
| 55 | |
| 56 int read = dataPipe.consumer.read(bd); | |
| 57 Expect.isTrue(dataPipe.consumer.status == MojoResult.kInvalidArgument); | |
| 58 | |
| 59 ByteData readData = dataPipe.consumer.beginRead(10); | |
| 60 Expect.isNull(readData); | |
| 61 Expect.isTrue(dataPipe.consumer.status == MojoResult.kInvalidArgument); | |
| 62 dataPipe.consumer.endRead(10); | |
| 63 Expect.isTrue(dataPipe.consumer.status == MojoResult.kInvalidArgument); | |
| 64 | |
| 65 // Shared buffer. | |
| 66 MojoSharedBuffer sharedBuffer = new MojoSharedBuffer.create(10); | |
| 67 Expect.isNotNull(sharedBuffer); | |
| 68 sharedBuffer.close(); | |
| 69 MojoSharedBuffer duplicate = new MojoSharedBuffer.duplicate(sharedBuffer); | |
| 70 Expect.isNull(duplicate); | |
| 71 | |
| 72 sharedBuffer = new MojoSharedBuffer.create(10); | |
| 73 Expect.isNotNull(sharedBuffer); | |
| 74 sharedBuffer.close(); | |
| 75 ByteData data = sharedBuffer.map(0, 10); | |
| 76 Expect.isTrue(sharedBuffer.status == MojoResult.kInvalidArgument); | |
| 77 Expect.isNull(data); | |
| 78 } | |
| 79 | |
| 80 basicMessagePipeTest() { | |
| 81 MojoMessagePipe pipe = new MojoMessagePipe(); | |
| 82 Expect.isNotNull(pipe); | |
| 83 Expect.isTrue(pipe.status == MojoResult.kOk); | |
| 84 Expect.isNotNull(pipe.endpoints); | |
| 85 | |
| 86 MojoMessagePipeEndpoint end0 = pipe.endpoints[0]; | |
| 87 MojoMessagePipeEndpoint end1 = pipe.endpoints[1]; | |
| 88 Expect.isTrue(end0.handle.isValid); | |
| 89 Expect.isTrue(end1.handle.isValid); | |
| 90 | |
| 91 // Not readable, yet. | |
| 92 MojoWaitResult mwr = end0.handle.wait(MojoHandleSignals.kReadable, 0); | |
| 93 Expect.isTrue(mwr.result == MojoResult.kDeadlineExceeded); | |
| 94 | |
| 95 // Should be writable. | |
| 96 mwr = end0.handle.wait(MojoHandleSignals.kWritable, 0); | |
| 97 Expect.isTrue(mwr.result == MojoResult.kOk); | |
| 98 | |
| 99 // Try to read. | |
| 100 ByteData data = new ByteData(10); | |
| 101 end0.read(data); | |
| 102 Expect.isTrue(end0.status == MojoResult.kShouldWait); | |
| 103 | |
| 104 // Write end1. | |
| 105 String hello = "hello"; | |
| 106 ByteData helloData = | |
| 107 new ByteData.view((new Uint8List.fromList(hello.codeUnits)).buffer); | |
| 108 int result = end1.write(helloData); | |
| 109 Expect.isTrue(result == MojoResult.kOk); | |
| 110 | |
| 111 // end0 should now be readable. | |
| 112 MojoWaitManyResult mwmr = MojoHandle.waitMany([end0.handle.h], | |
| 113 [MojoHandleSignals.kReadable], MojoHandle.DEADLINE_INDEFINITE); | |
| 114 Expect.isTrue(mwmr.result == MojoResult.kOk); | |
| 115 | |
| 116 // Read from end0. | |
| 117 MojoMessagePipeReadResult readResult = end0.read(data); | |
| 118 Expect.isNotNull(readResult); | |
| 119 Expect.isTrue(readResult.status == MojoResult.kOk); | |
| 120 Expect.equals(readResult.bytesRead, helloData.lengthInBytes); | |
| 121 Expect.equals(readResult.handlesRead, 0); | |
| 122 | |
| 123 String hello_result = new String.fromCharCodes( | |
| 124 data.buffer.asUint8List().sublist(0, readResult.bytesRead).toList()); | |
| 125 Expect.equals(hello_result, "hello"); | |
| 126 | |
| 127 // end0 should no longer be readable. | |
| 128 mwr = end0.handle.wait(MojoHandleSignals.kReadable, 10); | |
| 129 Expect.isTrue(mwr.result == MojoResult.kDeadlineExceeded); | |
| 130 | |
| 131 // Close end0's handle. | |
| 132 result = end0.handle.close(); | |
| 133 Expect.isTrue(result == MojoResult.kOk); | |
| 134 | |
| 135 // end1 should no longer be readable or writable. | |
| 136 mwr = end1.handle.wait(MojoHandleSignals.kReadWrite, 1000); | |
| 137 Expect.isTrue(mwr.result == MojoResult.kFailedPrecondition); | |
| 138 | |
| 139 result = end1.handle.close(); | |
| 140 Expect.isTrue(result == MojoResult.kOk); | |
| 141 } | |
| 142 | |
| 143 basicDataPipeTest() { | |
| 144 MojoDataPipe pipe = new MojoDataPipe(); | |
| 145 Expect.isNotNull(pipe); | |
| 146 Expect.isTrue(pipe.status == MojoResult.kOk); | |
| 147 Expect.isTrue(pipe.consumer.handle.isValid); | |
| 148 Expect.isTrue(pipe.producer.handle.isValid); | |
| 149 | |
| 150 MojoDataPipeProducer producer = pipe.producer; | |
| 151 MojoDataPipeConsumer consumer = pipe.consumer; | |
| 152 Expect.isTrue(producer.handle.isValid); | |
| 153 Expect.isTrue(consumer.handle.isValid); | |
| 154 | |
| 155 // Consumer should not be readable. | |
| 156 MojoWaitResult mwr = consumer.handle.wait(MojoHandleSignals.kReadable, 0); | |
| 157 Expect.isTrue(mwr.result == MojoResult.kDeadlineExceeded); | |
| 158 | |
| 159 // Producer should be writable. | |
| 160 mwr = producer.handle.wait(MojoHandleSignals.kWritable, 0); | |
| 161 Expect.isTrue(mwr.result == MojoResult.kOk); | |
| 162 | |
| 163 // Try to read from consumer. | |
| 164 ByteData buffer = new ByteData(20); | |
| 165 consumer.read(buffer, buffer.lengthInBytes, MojoDataPipeConsumer.FLAG_NONE); | |
| 166 Expect.isTrue(consumer.status == MojoResult.kShouldWait); | |
| 167 | |
| 168 // Try to begin a two-phase read from consumer. | |
| 169 ByteData b = consumer.beginRead(20, MojoDataPipeConsumer.FLAG_NONE); | |
| 170 Expect.isNull(b); | |
| 171 Expect.isTrue(consumer.status == MojoResult.kShouldWait); | |
| 172 | |
| 173 // Write to producer. | |
| 174 String hello = "hello "; | |
| 175 ByteData helloData = | |
| 176 new ByteData.view((new Uint8List.fromList(hello.codeUnits)).buffer); | |
| 177 int written = producer.write( | |
| 178 helloData, helloData.lengthInBytes, MojoDataPipeProducer.FLAG_NONE); | |
| 179 Expect.isTrue(producer.status == MojoResult.kOk); | |
| 180 Expect.equals(written, helloData.lengthInBytes); | |
| 181 | |
| 182 // Now that we have written, the consumer should be readable. | |
| 183 MojoWaitManyResult mwmr = MojoHandle.waitMany([consumer.handle.h], | |
| 184 [MojoHandleSignals.kReadable], MojoHandle.DEADLINE_INDEFINITE); | |
| 185 Expect.isTrue(mwr.result == MojoResult.kOk); | |
| 186 | |
| 187 // Do a two-phase write to the producer. | |
| 188 ByteData twoPhaseWrite = | |
| 189 producer.beginWrite(20, MojoDataPipeProducer.FLAG_NONE); | |
| 190 Expect.isTrue(producer.status == MojoResult.kOk); | |
| 191 Expect.isNotNull(twoPhaseWrite); | |
| 192 Expect.isTrue(twoPhaseWrite.lengthInBytes >= 20); | |
| 193 | |
| 194 String world = "world"; | |
| 195 twoPhaseWrite.buffer.asUint8List().setAll(0, world.codeUnits); | |
| 196 producer.endWrite(Uint8List.BYTES_PER_ELEMENT * world.codeUnits.length); | |
| 197 Expect.isTrue(producer.status == MojoResult.kOk); | |
| 198 | |
| 199 // Read one character from consumer. | |
| 200 int read = consumer.read(buffer, 1, MojoDataPipeConsumer.FLAG_NONE); | |
| 201 Expect.isTrue(consumer.status == MojoResult.kOk); | |
| 202 Expect.equals(read, 1); | |
| 203 | |
| 204 // Close the producer. | |
| 205 int result = producer.handle.close(); | |
| 206 Expect.isTrue(result == MojoResult.kOk); | |
| 207 | |
| 208 // Consumer should still be readable. | |
| 209 mwr = consumer.handle.wait(MojoHandleSignals.kReadable, 0); | |
| 210 Expect.isTrue(mwr.result == MojoResult.kOk); | |
| 211 | |
| 212 // Get the number of remaining bytes. | |
| 213 int remaining = consumer.read(null, 0, MojoDataPipeConsumer.FLAG_QUERY); | |
| 214 Expect.isTrue(consumer.status == MojoResult.kOk); | |
| 215 Expect.equals(remaining, "hello world".length - 1); | |
| 216 | |
| 217 // Do a two-phase read. | |
| 218 ByteData twoPhaseRead = | |
| 219 consumer.beginRead(remaining, MojoDataPipeConsumer.FLAG_NONE); | |
| 220 Expect.isTrue(consumer.status == MojoResult.kOk); | |
| 221 Expect.isNotNull(twoPhaseRead); | |
| 222 Expect.isTrue(twoPhaseRead.lengthInBytes <= remaining); | |
| 223 | |
| 224 Uint8List uint8_list = buffer.buffer.asUint8List(); | |
| 225 uint8_list.setAll(1, twoPhaseRead.buffer.asUint8List()); | |
| 226 uint8_list = uint8_list.sublist(0, 1 + twoPhaseRead.lengthInBytes); | |
| 227 | |
| 228 consumer.endRead(twoPhaseRead.lengthInBytes); | |
| 229 Expect.isTrue(consumer.status == MojoResult.kOk); | |
| 230 | |
| 231 String helloWorld = new String.fromCharCodes(uint8_list.toList()); | |
| 232 Expect.equals("hello world", helloWorld); | |
| 233 | |
| 234 result = consumer.handle.close(); | |
| 235 Expect.isTrue(result == MojoResult.kOk); | |
| 236 } | |
| 237 | |
| 238 basicSharedBufferTest() { | |
| 239 MojoSharedBuffer mojoBuffer = | |
| 240 new MojoSharedBuffer.create(100, MojoSharedBuffer.createFlagNone); | |
| 241 Expect.isNotNull(mojoBuffer); | |
| 242 Expect.isNotNull(mojoBuffer.status); | |
| 243 Expect.isTrue(mojoBuffer.status == MojoResult.kOk); | |
| 244 Expect.isNotNull(mojoBuffer.handle); | |
| 245 Expect.isTrue(mojoBuffer.handle is MojoHandle); | |
| 246 Expect.isTrue(mojoBuffer.handle.isValid); | |
| 247 | |
| 248 var mapping = mojoBuffer.map(0, 100, MojoSharedBuffer.mapFlagNone); | |
| 249 Expect.isNotNull(mojoBuffer.status); | |
| 250 Expect.isTrue(mojoBuffer.status == MojoResult.kOk); | |
| 251 Expect.isNotNull(mapping); | |
| 252 Expect.isTrue(mapping is ByteData); | |
| 253 | |
| 254 mapping.setInt8(50, 42); | |
| 255 | |
| 256 MojoSharedBuffer duplicate = new MojoSharedBuffer.duplicate( | |
| 257 mojoBuffer, MojoSharedBuffer.duplicateFlagNone); | |
| 258 Expect.isNotNull(duplicate); | |
| 259 Expect.isNotNull(duplicate.status); | |
| 260 Expect.isTrue(duplicate.status == MojoResult.kOk); | |
| 261 Expect.isTrue(duplicate.handle is MojoHandle); | |
| 262 Expect.isTrue(duplicate.handle.isValid); | |
| 263 | |
| 264 mapping = duplicate.map(0, 100, MojoSharedBuffer.mapFlagNone); | |
| 265 Expect.isTrue(duplicate.status == MojoResult.kOk); | |
| 266 Expect.isNotNull(mapping); | |
| 267 Expect.isTrue(mapping is ByteData); | |
| 268 | |
| 269 mojoBuffer.close(); | |
| 270 mojoBuffer = null; | |
| 271 | |
| 272 mapping.setInt8(51, 43); | |
| 273 | |
| 274 mapping = duplicate.map(50, 50, MojoSharedBuffer.mapFlagNone); | |
| 275 Expect.isNotNull(duplicate.status); | |
| 276 Expect.isTrue(duplicate.status == MojoResult.kOk); | |
| 277 Expect.isNotNull(mapping); | |
| 278 Expect.isTrue(mapping is ByteData); | |
| 279 | |
| 280 Expect.equals(mapping.getInt8(0), 42); | |
| 281 Expect.equals(mapping.getInt8(1), 43); | |
| 282 | |
| 283 duplicate.close(); | |
| 284 duplicate = null; | |
| 285 } | |
| 286 | |
| 287 fillerDrainerTest() async { | |
| 288 MojoDataPipe pipe = new MojoDataPipe(); | |
| 289 Expect.isNotNull(pipe); | |
| 290 Expect.isTrue(pipe.status == MojoResult.kOk); | |
| 291 Expect.isTrue(pipe.consumer.handle.isValid); | |
| 292 Expect.isTrue(pipe.producer.handle.isValid); | |
| 293 | |
| 294 MojoDataPipeProducer producer = pipe.producer; | |
| 295 MojoDataPipeConsumer consumer = pipe.consumer; | |
| 296 Expect.isTrue(producer.handle.isValid); | |
| 297 Expect.isTrue(consumer.handle.isValid); | |
| 298 | |
| 299 String sentMessage = "Hello world!" * 1000; | |
| 300 ByteData producerData = new ByteData.view(UTF8.encode(sentMessage).buffer); | |
| 301 DataPipeFiller.fillHandle(producer, producerData); | |
| 302 | |
| 303 ByteData consumerData = await DataPipeDrainer.drainHandle(consumer); | |
| 304 Uint8List receivedBytes = new Uint8List.view(consumerData.buffer); | |
| 305 String receivedMessage = new String.fromCharCodes(receivedBytes); | |
| 306 | |
| 307 Expect.equals(sentMessage, receivedMessage); | |
| 308 Expect.isTrue(producer.status == MojoResult.kOk); | |
| 309 Expect.isTrue(consumer.status == MojoResult.kOk); | |
| 310 } | |
| 311 | |
| 312 utilsTest() { | |
| 313 int ticksa = getTimeTicksNow(); | |
| 314 Expect.isTrue(1000 < ticksa); | |
| 315 | |
| 316 // Wait for the clock to advance. | |
| 317 MojoWaitResult mwr = (new MojoMessagePipe()).endpoints[0] | |
| 318 .handle | |
| 319 .wait(MojoHandleSignals.kReadable, 1); | |
| 320 Expect.isTrue(mwr.result == MojoResult.kDeadlineExceeded); | |
| 321 | |
| 322 int ticksb = getTimeTicksNow(); | |
| 323 Expect.isTrue(ticksa < ticksb); | |
| 324 } | |
| 325 | |
| 326 // TODO(rudominer) This probably belongs in a different file. | |
| 327 processTest() { | |
| 328 Expect.isTrue(pid > 0); | |
| 329 } | |
| 330 | |
| 331 main() async { | |
| 332 invalidHandleTest(); | |
| 333 basicMessagePipeTest(); | |
| 334 basicDataPipeTest(); | |
| 335 basicSharedBufferTest(); | |
| 336 await fillerDrainerTest(); | |
| 337 utilsTest(); | |
| 338 processTest(); | |
| 339 } | |
| OLD | NEW |