OLD | NEW |
1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 main() { | 5 import "dart:async"; |
| 6 import "dart:io"; |
| 7 |
| 8 testSimpleBind() async { |
| 9 var s = await RawServerSocket.bind(InternetAddress.LOOPBACK_IP_V4, 0); |
| 10 print("port = ${s.port}"); |
| 11 await s.close(); |
| 12 } |
| 13 |
| 14 testSimpleConnect() async { |
| 15 var server = await RawServerSocket.bind(InternetAddress.LOOPBACK_IP_V4, 0); |
| 16 print("server port = ${server.port}"); |
| 17 server.listen((socket) { |
| 18 print("listen socket port = ${socket.port}"); |
| 19 socket.close(); |
| 20 }); |
| 21 var socket = await RawSocket.connect("127.0.0.1", server.port); |
| 22 print("socket port = ${socket.port}"); |
| 23 await server.close(); |
| 24 await socket.close(); |
| 25 } |
| 26 |
| 27 testSimpleReadWrite({bool dropReads}) async { |
| 28 // This test creates a server and a client connects. The client then |
| 29 // writes and the server echos. When the server has finished its |
| 30 // echo it half-closes. When the client gets the close event is |
| 31 // closes fully. |
| 32 |
| 33 const messageSize = 1000; |
| 34 int serverReadCount = 0; |
| 35 int clientReadCount = 0; |
| 36 |
| 37 List<int> createTestData() { |
| 38 return new List<int>.generate(messageSize, (index) => index & 0xff); |
| 39 } |
| 40 |
| 41 void verifyTestData(List<int> data) { |
| 42 assert(messageSize == data.length); |
| 43 List<int> expected = createTestData(); |
| 44 for (int i = 0; i < messageSize; i++) { |
| 45 assert(expected[i] == data[i]); |
| 46 } |
| 47 } |
| 48 |
| 49 var server = await RawServerSocket.bind(InternetAddress.LOOPBACK_IP_V4, 0); |
| 50 server.listen((client) { |
| 51 int bytesRead = 0; |
| 52 int bytesWritten = 0; |
| 53 bool closedEventReceived = false; |
| 54 List<int> data = new List<int>(messageSize); |
| 55 bool doneReading = false; |
| 56 |
| 57 client.writeEventsEnabled = false; |
| 58 client.listen((event) { |
| 59 switch (event) { |
| 60 case RawSocketEvent.READ: |
| 61 if (doneReading) { |
| 62 break; |
| 63 } |
| 64 if (dropReads) { |
| 65 if (serverReadCount != 10) { |
| 66 serverReadCount++; |
| 67 break; |
| 68 } else { |
| 69 serverReadCount = 0; |
| 70 } |
| 71 } |
| 72 print("client READ event bytesRead = $bytesRead"); |
| 73 assert(bytesWritten == 0); |
| 74 assert(client.available() > 0); |
| 75 var buffer = client.read(200); |
| 76 print("client READ event: read ${buffer.length} more bytes"); |
| 77 data.setRange(bytesRead, bytesRead + buffer.length, buffer); |
| 78 bytesRead += buffer.length; |
| 79 if (bytesRead == data.length) { |
| 80 verifyTestData(data); |
| 81 print("client READ event. Done reading, enabling writes"); |
| 82 client.writeEventsEnabled = true; |
| 83 doneReading = true; |
| 84 } |
| 85 break; |
| 86 case RawSocketEvent.WRITE: |
| 87 assert(!client.writeEventsEnabled); |
| 88 bytesWritten += client.write( |
| 89 data, bytesWritten, data.length - bytesWritten); |
| 90 print("client WRITE event: $bytesWritten written"); |
| 91 if (bytesWritten < data.length) { |
| 92 client.writeEventsEnabled = true; |
| 93 } |
| 94 if (bytesWritten == data.length) { |
| 95 print("client WRITE event: done writing."); |
| 96 client.shutdown(SocketDirection.SEND); |
| 97 } |
| 98 break; |
| 99 case RawSocketEvent.READ_CLOSED: |
| 100 print("client READ_CLOSED event"); |
| 101 server.close(); |
| 102 break; |
| 103 case RawSocketEvent.CLOSED: |
| 104 assert(!closedEventReceived); |
| 105 print("client CLOSED event"); |
| 106 closedEventReceived = true; |
| 107 break; |
| 108 default: throw "Unexpected event $event"; |
| 109 } |
| 110 }, |
| 111 onDone: () {assert(closedEventReceived);}); |
| 112 }); |
| 113 |
| 114 { |
| 115 var completer = new Completer(); |
| 116 var socket = await RawSocket.connect("127.0.0.1", server.port); |
| 117 int bytesRead = 0; |
| 118 int bytesWritten = 0; |
| 119 bool closedEventReceived = false; |
| 120 List<int> data = createTestData(); |
| 121 |
| 122 socket.listen((event) { |
| 123 switch (event) { |
| 124 case RawSocketEvent.READ: |
| 125 assert(socket.available() > 0); |
| 126 if (dropReads) { |
| 127 if (clientReadCount != 10) { |
| 128 clientReadCount++; |
| 129 break; |
| 130 } else { |
| 131 clientReadCount = 0; |
| 132 } |
| 133 } |
| 134 print("server READ event: ${bytesRead} read"); |
| 135 var buffer = socket.read(); |
| 136 print("server READ event: read ${buffer.length} more bytes"); |
| 137 data.setRange(bytesRead, bytesRead + buffer.length, buffer); |
| 138 bytesRead += buffer.length; |
| 139 break; |
| 140 case RawSocketEvent.WRITE: |
| 141 assert(bytesRead == 0); |
| 142 assert(!socket.writeEventsEnabled); |
| 143 bytesWritten += socket.write( |
| 144 data, bytesWritten, data.length - bytesWritten); |
| 145 print("server WRITE event: ${bytesWritten} written"); |
| 146 if (bytesWritten < data.length) { |
| 147 socket.writeEventsEnabled = true; |
| 148 } else { |
| 149 print("server WRITE event: done writing"); |
| 150 data = new List<int>(messageSize); |
| 151 } |
| 152 break; |
| 153 case RawSocketEvent.READ_CLOSED: |
| 154 print("server READ_CLOSED event"); |
| 155 verifyTestData(data); |
| 156 socket.close(); |
| 157 break; |
| 158 case RawSocketEvent.CLOSED: |
| 159 assert(!closedEventReceived); |
| 160 print("server CLOSED event"); |
| 161 closedEventReceived = true; |
| 162 break; |
| 163 default: throw "Unexpected event $event"; |
| 164 } |
| 165 }, |
| 166 onDone: () { |
| 167 assert(closedEventReceived); |
| 168 completer.complete(null); |
| 169 }); |
| 170 |
| 171 return completer.future; |
| 172 } |
| 173 } |
| 174 |
| 175 Future testGoogleUrl(SecurityContext context, String outcome) async { |
| 176 var client = new HttpClient(context: context); |
| 177 // We need to use an external server that is backed by a |
| 178 // built-in root certificate authority. |
| 179 try { |
| 180 // First, check if the lookup works. |
| 181 await InternetAddress.lookup('www.google.com'); |
| 182 var request = await client.getUrl(Uri.parse('http://www.google.com')); |
| 183 request.followRedirects = false; |
| 184 var response = await request.close(); |
| 185 assert('pass' == outcome); |
| 186 try { await response.drain(); } catch (e) { |
| 187 print('drain failed: $e'); |
| 188 } |
| 189 } catch (e) { |
| 190 // Lookup failed or connection failed. Don't report a failure. |
| 191 print("SocketException: $e"); |
| 192 } finally { |
| 193 client.close(); |
| 194 } |
| 195 } |
| 196 |
| 197 main() async { |
6 print("Hello, Fuchsia!"); | 198 print("Hello, Fuchsia!"); |
7 } | 199 |
| 200 print("testSimpleBind"); |
| 201 await testSimpleBind(); |
| 202 print("testSimpleBind done"); |
| 203 |
| 204 print("testSimpleConnect"); |
| 205 await testSimpleConnect(); |
| 206 print("testSimpleConnect done"); |
| 207 |
| 208 // print("testSimpleReadWrite"); |
| 209 // await testSimpleReadWrite(dropReads: false); |
| 210 // print("testSimpleReadWrite done"); |
| 211 |
| 212 // print("testGoogleUrl"); |
| 213 // await testGoogleUrl(null, 'pass'); |
| 214 // print("testGoogleUrl done"); |
| 215 |
| 216 print("Goodbyte, Fuchsia!"); |
| 217 } |
OLD | NEW |