OLD | NEW |
1 // Copyright (c) 2013, 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 // VMOptions=--short_socket_write |
8 // VMOptions=--short_socket_read --short_socket_write | 8 // VMOptions=--short_socket_read --short_socket_write |
9 // OtherResources=certificates/server_chain.pem | 9 // OtherResources=certificates/server_chain.pem |
10 // OtherResources=certificates/server_key.pem | 10 // OtherResources=certificates/server_key.pem |
11 // OtherResources=certificates/trusted_certs.pem | 11 // OtherResources=certificates/trusted_certs.pem |
12 | 12 |
13 import "package:expect/expect.dart"; | 13 import "package:expect/expect.dart"; |
14 import "package:path/path.dart"; | 14 import "package:path/path.dart"; |
15 import "dart:async"; | 15 import "dart:async"; |
16 import "dart:io"; | 16 import "dart:io"; |
17 import "dart:isolate"; | 17 import "dart:isolate"; |
18 | 18 |
19 String localFile(path) => Platform.script.resolve(path).toFilePath(); | 19 String localFile(path) => Platform.script.resolve(path).toFilePath(); |
20 | 20 |
21 SecurityContext serverContext = new SecurityContext() | 21 SecurityContext serverContext = new SecurityContext() |
22 ..useCertificateChain(localFile('certificates/server_chain.pem')) | 22 ..useCertificateChain(localFile('certificates/server_chain.pem')) |
23 ..usePrivateKey(localFile('certificates/server_key.pem'), | 23 ..usePrivateKey(localFile('certificates/server_key.pem'), |
24 password: 'dartdart'); | 24 password: 'dartdart'); |
25 | 25 |
26 SecurityContext clientContext = new SecurityContext() | 26 SecurityContext clientContext = new SecurityContext() |
27 ..setTrustedCertificates(localFile('certificates/trusted_certs.pem')); | 27 ..setTrustedCertificates(localFile('certificates/trusted_certs.pem')); |
28 | 28 |
29 Future<HttpServer> startServer() { | 29 Future<HttpServer> startServer() { |
30 return HttpServer.bindSecure( | 30 return HttpServer |
31 "localhost", | 31 .bindSecure("localhost", 0, serverContext, backlog: 5) |
32 0, | 32 .then((server) { |
33 serverContext, | |
34 backlog: 5).then((server) { | |
35 server.listen((HttpRequest request) { | 33 server.listen((HttpRequest request) { |
36 request.listen( | 34 request.listen((_) {}, onDone: () { |
37 (_) { }, | 35 request.response.contentLength = 100; |
38 onDone: () { | 36 for (int i = 0; i < 10; i++) { |
39 request.response.contentLength = 100; | 37 request.response.add([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]); |
40 for (int i = 0; i < 10; i++) { | 38 } |
41 request.response.add([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]); | 39 request.response.close(); |
42 } | 40 }); |
43 request.response.close(); | |
44 }); | |
45 }); | 41 }); |
46 return server; | 42 return server; |
47 }); | 43 }); |
48 } | 44 } |
49 | 45 |
50 main() async { | 46 main() async { |
51 List<int> message = "GET / HTTP/1.0\r\nHost: localhost\r\n\r\n".codeUnits; | 47 List<int> message = "GET / HTTP/1.0\r\nHost: localhost\r\n\r\n".codeUnits; |
52 int written = 0; | 48 int written = 0; |
53 List<int> body = <int>[]; | 49 List<int> body = <int>[]; |
54 var server = await startServer(); | 50 var server = await startServer(); |
55 var socket = await RawSecureSocket.connect("localhost", | 51 var socket = await RawSecureSocket.connect("localhost", server.port, |
56 server.port, | 52 context: clientContext); |
57 context: clientContext); | |
58 StreamSubscription subscription; | 53 StreamSubscription subscription; |
59 bool paused = false; | 54 bool paused = false; |
60 bool readEventsTested = false; | 55 bool readEventsTested = false; |
61 bool readEventsPaused = false; | 56 bool readEventsPaused = false; |
62 | 57 |
63 void runPauseTest() { | 58 void runPauseTest() { |
64 subscription.pause(); | 59 subscription.pause(); |
65 paused = true; | 60 paused = true; |
66 new Timer(const Duration(milliseconds: 500), () { | 61 new Timer(const Duration(milliseconds: 500), () { |
67 paused = false; | 62 paused = false; |
68 subscription.resume(); | 63 subscription.resume(); |
69 }); | 64 }); |
70 } | 65 } |
71 | 66 |
72 void runReadEventTest() { | 67 void runReadEventTest() { |
73 if (readEventsTested) return; | 68 if (readEventsTested) return; |
74 readEventsTested = true; | 69 readEventsTested = true; |
75 socket.readEventsEnabled = false; | 70 socket.readEventsEnabled = false; |
76 readEventsPaused = true; | 71 readEventsPaused = true; |
77 new Timer(const Duration(milliseconds: 500), () { | 72 new Timer(const Duration(milliseconds: 500), () { |
78 readEventsPaused = false; | 73 readEventsPaused = false; |
79 socket.readEventsEnabled = true; | 74 socket.readEventsEnabled = true; |
80 }); | 75 }); |
81 } | 76 } |
82 | 77 |
83 void handleRawEvent(RawSocketEvent event) { | 78 void handleRawEvent(RawSocketEvent event) { |
84 Expect.isFalse(paused); | 79 Expect.isFalse(paused); |
85 switch (event) { | 80 switch (event) { |
86 case RawSocketEvent.READ: | 81 case RawSocketEvent.READ: |
87 Expect.isFalse(readEventsPaused); | 82 Expect.isFalse(readEventsPaused); |
88 runReadEventTest(); | 83 runReadEventTest(); |
89 body.addAll(socket.read()); | 84 body.addAll(socket.read()); |
90 break; | 85 break; |
91 case RawSocketEvent.WRITE: | 86 case RawSocketEvent.WRITE: |
92 written += | 87 written += socket.write(message, written, message.length - written); |
93 socket.write(message, written, message.length - written); | |
94 if (written < message.length) { | 88 if (written < message.length) { |
95 socket.writeEventsEnabled = true; | 89 socket.writeEventsEnabled = true; |
96 } else { | 90 } else { |
97 socket.shutdown(SocketDirection.SEND); | 91 socket.shutdown(SocketDirection.SEND); |
98 runPauseTest(); | 92 runPauseTest(); |
99 } | 93 } |
100 break; | 94 break; |
101 case RawSocketEvent.READ_CLOSED: | 95 case RawSocketEvent.READ_CLOSED: |
102 Expect.isTrue(body.length > 100); | 96 Expect.isTrue(body.length > 100); |
103 Expect.equals(72, body.first); | 97 Expect.equals(72, body.first); |
104 Expect.equals(9, body.last); | 98 Expect.equals(9, body.last); |
105 server.close(); | 99 server.close(); |
106 break; | 100 break; |
107 default: throw "Unexpected event $event"; | 101 default: |
| 102 throw "Unexpected event $event"; |
108 } | 103 } |
109 } | 104 } |
110 | 105 |
111 subscription = socket.listen( | 106 subscription = socket.listen(handleRawEvent, onError: (e, trace) { |
112 handleRawEvent, | 107 String msg = "onError handler of RawSecureSocket stream hit: $e"; |
113 onError: (e, trace) { | 108 if (trace != null) msg += "\nStackTrace: $trace"; |
114 String msg = "onError handler of RawSecureSocket stream hit: $e"; | 109 Expect.fail(msg); |
115 if (trace != null) msg += "\nStackTrace: $trace"; | 110 }); |
116 Expect.fail(msg); | |
117 }); | |
118 } | 111 } |
OLD | NEW |