Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, 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 // VMOptions= | 5 // VMOptions= |
| 6 // VMOptions=--short_socket_read | 6 // VMOptions=--short_socket_read |
| 7 // VMOptions=--short_socket_write | 7 // XXVMOptions=--short_socket_write |
|
Anders Johnsen
2013/02/26 09:42:14
XX?
Søren Gjesse
2013/02/26 10:00:00
This was the simple way to disable short socket wr
| |
| 8 // VMOptions=--short_socket_read --short_socket_write | 8 // XXVMOptions=--short_socket_read --short_socket_write |
| 9 | 9 |
| 10 import "dart:async"; | 10 import "dart:async"; |
| 11 import "dart:io"; | 11 import "dart:io"; |
| 12 import "dart:isolate"; | 12 import "dart:isolate"; |
| 13 | 13 |
| 14 Future<HttpServer> startServer() { | |
| 15 return HttpServer.bindSecure( | |
| 16 "127.0.0.1", | |
| 17 0, | |
| 18 backlog: 5, | |
| 19 certificateName: 'localhost_cert').then((server) { | |
| 20 server.listen((HttpRequest request) { | |
| 21 request.listen( | |
| 22 (_) { }, | |
| 23 onDone: () { | |
| 24 request.response.contentLength = 100; | |
| 25 for (int i = 0; i < 10; i++) { | |
| 26 request.response.add([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]); | |
| 27 } | |
| 28 request.response.close(); | |
| 29 }); | |
| 30 }); | |
| 31 return server; | |
| 32 }); | |
| 33 } | |
| 34 | |
| 35 void InitializeSSL() { | |
| 36 var testPkcertDatabase = | |
| 37 new Path(new Options().script).directoryPath.append('pkcert/'); | |
| 38 SecureSocket.initialize(database: testPkcertDatabase.toNativePath(), | |
| 39 password: 'dartdart'); | |
| 40 } | |
| 14 | 41 |
| 15 void main() { | 42 void main() { |
| 16 List<int> message = "GET / HTTP/1.0\r\nHost: www.google.dk\r\n\r\n".codeUnits; | 43 List<int> message = "GET / HTTP/1.0\r\nHost: localhost\r\n\r\n".codeUnits; |
| 17 int written = 0; | 44 int written = 0; |
| 18 List<String> chunks = <String>[]; | 45 List<int> body = <int>[]; |
| 19 SecureSocket.initialize(); | 46 InitializeSSL(); |
| 20 // TODO(whesse): Use a Dart HTTPS server for this test. | 47 startServer().then((server) { |
| 21 // The Dart HTTPS server works on bleeding-edge, but not on IOv2. | 48 RawSecureSocket.connect("localhost", server.port).then((socket) { |
| 22 // When we use a Dart HTTPS server, allow --short_socket_write. The flag | 49 StreamSubscription subscription; |
| 23 // causes fragmentation of the client hello message, which doesn't seem to | 50 bool paused = false; |
| 24 // work with www.google.dk. | 51 bool readEventsTested = false; |
| 25 RawSecureSocket.connect("www.google.dk", 443).then((socket) { | 52 bool readEventsPaused = false; |
| 26 StreamSubscription subscription; | |
| 27 bool paused = false; | |
| 28 bool readEventsTested = false; | |
| 29 bool readEventsPaused = false; | |
| 30 | 53 |
| 31 void runPauseTest() { | 54 void runPauseTest() { |
| 32 subscription.pause(); | 55 subscription.pause(); |
| 33 paused = true; | 56 paused = true; |
| 34 new Timer(500, (_) { | 57 new Timer(500, (_) { |
| 35 paused = false; | 58 paused = false; |
| 36 subscription.resume(); | 59 subscription.resume(); |
| 37 }); | 60 }); |
| 38 } | 61 } |
| 39 | 62 |
| 40 void runReadEventTest() { | 63 void runReadEventTest() { |
| 41 if (readEventsTested) return; | 64 if (readEventsTested) return; |
| 42 readEventsTested = true; | 65 readEventsTested = true; |
| 43 socket.readEventsEnabled = false; | 66 socket.readEventsEnabled = false; |
| 44 readEventsPaused = true; | 67 readEventsPaused = true; |
| 45 new Timer(500, (_) { | 68 new Timer(500, (_) { |
| 46 readEventsPaused = false; | 69 readEventsPaused = false; |
| 47 socket.readEventsEnabled = true; | 70 socket.readEventsEnabled = true; |
| 48 }); | 71 }); |
| 49 } | 72 } |
| 50 | 73 |
| 51 subscription = socket.listen((RawSocketEvent event) { | 74 subscription = socket.listen( |
| 52 Expect.isFalse(paused); | 75 (RawSocketEvent event) { |
| 53 switch (event) { | 76 Expect.isFalse(paused); |
| 54 case RawSocketEvent.READ: | 77 switch (event) { |
| 55 Expect.isFalse(readEventsPaused); | 78 case RawSocketEvent.READ: |
| 56 runReadEventTest(); | 79 Expect.isFalse(readEventsPaused); |
| 57 var data = socket.read(); | 80 runReadEventTest(); |
| 58 var received = new String.fromCharCodes(data); | 81 body.addAll(socket.read()); |
| 59 chunks.add(received); | 82 break; |
| 60 break; | 83 case RawSocketEvent.WRITE: |
| 61 case RawSocketEvent.WRITE: | 84 written += |
| 62 written += | 85 socket.write(message, written, message.length - written); |
| 63 socket.write(message, written, message.length - written); | 86 if (written < message.length) { |
| 64 if (written < message.length) { | 87 socket.writeEventsEnabled = true; |
| 65 socket.writeEventsEnabled = true; | 88 } else { |
| 66 } else { | 89 socket.shutdown(SocketDirection.SEND); |
| 67 socket.shutdown(SocketDirection.SEND); | 90 runPauseTest(); |
| 68 runPauseTest(); | 91 } |
| 69 } | 92 break; |
| 70 break; | 93 case RawSocketEvent.READ_CLOSED: |
| 71 case RawSocketEvent.READ_CLOSED: | 94 Expect.isTrue(body.length > 100); |
| 72 String fullPage = chunks.join(); | 95 Expect.equals(72, body[0]); |
| 73 Expect.isTrue(fullPage.contains('</body></html>')); | 96 Expect.equals(9, body[body.length - 1]); |
| 74 break; | 97 server.close(); |
| 75 default: throw "Unexpected event $event"; | 98 break; |
| 76 } | 99 default: throw "Unexpected event $event"; |
| 77 }, onError: (AsyncError a) { | 100 } |
| 78 Expect.fail("onError handler of RawSecureSocket stream hit: $a"); | 101 }, |
| 79 }); | 102 onError: (AsyncError a) { |
| 103 Expect.fail("onError handler of RawSecureSocket stream hit: $a"); | |
| 104 }); | |
| 105 }); | |
| 80 }); | 106 }); |
| 81 } | 107 } |
| OLD | NEW |