OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 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 | 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 // OtherResources=certificates/server_chain.pem | 5 // OtherResources=certificates/server_chain.pem |
6 // OtherResources=certificates/server_key.pem | 6 // OtherResources=certificates/server_key.pem |
7 // OtherResources=certificates/trusted_certs.pem | 7 // OtherResources=certificates/trusted_certs.pem |
8 | 8 |
9 import "dart:async"; | 9 import "dart:async"; |
10 import "dart:io"; | 10 import "dart:io"; |
11 import "dart:isolate"; | 11 import "dart:isolate"; |
12 | 12 |
13 import "package:expect/expect.dart"; | 13 import "package:expect/expect.dart"; |
14 | 14 |
15 InternetAddress HOST; | 15 InternetAddress HOST; |
16 | 16 |
17 String localFile(path) => Platform.script.resolve(path).toFilePath(); | 17 String localFile(path) => Platform.script.resolve(path).toFilePath(); |
18 | 18 |
19 SecurityContext serverContext = new SecurityContext() | 19 SecurityContext serverContext = new SecurityContext() |
20 ..useCertificateChain(localFile('certificates/server_chain.pem')) | 20 ..useCertificateChain(localFile('certificates/server_chain.pem')) |
21 ..usePrivateKey(localFile('certificates/server_key.pem'), | 21 ..usePrivateKey(localFile('certificates/server_key.pem'), |
22 password: 'dartdart'); | 22 password: 'dartdart'); |
23 | 23 |
24 SecurityContext clientContext = new SecurityContext() | 24 SecurityContext clientContext = new SecurityContext() |
25 ..setTrustedCertificates(localFile('certificates/trusted_certs.pem')); | 25 ..setTrustedCertificates(localFile('certificates/trusted_certs.pem')); |
26 | 26 |
27 void testListenOn() { | 27 void testListenOn() { |
28 void test(void onDone()) { | 28 void test(void onDone()) { |
29 HttpServer.bindSecure(HOST, | 29 HttpServer.bindSecure(HOST, 0, serverContext, backlog: 5).then((server) { |
30 0, | |
31 serverContext, | |
32 backlog: 5).then((server) { | |
33 ReceivePort serverPort = new ReceivePort(); | 30 ReceivePort serverPort = new ReceivePort(); |
34 server.listen((HttpRequest request) { | 31 server.listen((HttpRequest request) { |
35 request.listen( | 32 request.listen((_) {}, onDone: () { |
36 (_) { }, | 33 request.response.close(); |
37 onDone: () { | 34 serverPort.close(); |
38 request.response.close(); | 35 }); |
39 serverPort.close(); | |
40 }); | |
41 }); | 36 }); |
42 | 37 |
43 HttpClient client = new HttpClient(context: clientContext); | 38 HttpClient client = new HttpClient(context: clientContext); |
44 ReceivePort clientPort = new ReceivePort(); | 39 ReceivePort clientPort = new ReceivePort(); |
45 client.getUrl(Uri.parse("https://${HOST.host}:${server.port}/")) | 40 client |
46 .then((HttpClientRequest request) { | 41 .getUrl(Uri.parse("https://${HOST.host}:${server.port}/")) |
47 return request.close(); | 42 .then((HttpClientRequest request) { |
48 }) | 43 return request.close(); |
49 .then((HttpClientResponse response) { | 44 }).then((HttpClientResponse response) { |
50 response.listen( | 45 response.listen((_) {}, onDone: () { |
51 (_) { }, | 46 client.close(); |
52 onDone: () { | 47 clientPort.close(); |
53 client.close(); | 48 server.close(); |
54 clientPort.close(); | 49 Expect.throws(() => server.port); |
55 server.close(); | 50 onDone(); |
56 Expect.throws(() => server.port); | |
57 onDone(); | |
58 }); | |
59 }) | |
60 .catchError((e, trace) { | |
61 String msg = "Unexpected error in Https client: $e"; | |
62 if (trace != null) msg += "\nStackTrace: $trace"; | |
63 Expect.fail(msg); | |
64 }); | 51 }); |
| 52 }).catchError((e, trace) { |
| 53 String msg = "Unexpected error in Https client: $e"; |
| 54 if (trace != null) msg += "\nStackTrace: $trace"; |
| 55 Expect.fail(msg); |
| 56 }); |
65 }); | 57 }); |
66 } | 58 } |
67 | 59 |
68 // Test two servers in succession. | 60 // Test two servers in succession. |
69 test(() { | 61 test(() { |
70 test(() { }); | 62 test(() {}); |
71 }); | 63 }); |
72 } | 64 } |
73 | 65 |
74 void testEarlyClientClose() { | 66 void testEarlyClientClose() { |
75 HttpServer.bindSecure(HOST, | 67 HttpServer.bindSecure(HOST, 0, serverContext).then((server) { |
76 0, | 68 server.listen((request) { |
77 serverContext).then((server) { | 69 String name = Platform.script.toFilePath(); |
78 server.listen( | 70 new File(name) |
79 (request) { | 71 .openRead() |
80 String name = Platform.script.toFilePath(); | 72 .pipe(request.response) |
81 new File(name).openRead().pipe(request.response) | 73 .catchError((e) {/* ignore */}); |
82 .catchError((e) { /* ignore */ }); | 74 }); |
83 }); | |
84 | 75 |
85 var count = 0; | 76 var count = 0; |
86 makeRequest() { | 77 makeRequest() { |
87 Socket.connect(HOST, server.port).then((socket) { | 78 Socket.connect(HOST, server.port).then((socket) { |
88 var data = "Invalid TLS handshake"; | 79 var data = "Invalid TLS handshake"; |
89 socket.write(data); | 80 socket.write(data); |
90 socket.close(); | 81 socket.close(); |
91 socket.done.then((_) { | 82 socket.done.then((_) { |
92 socket.destroy(); | 83 socket.destroy(); |
93 if (++count < 10) { | 84 if (++count < 10) { |
94 makeRequest(); | 85 makeRequest(); |
95 } else { | 86 } else { |
96 server.close(); | 87 server.close(); |
97 } | 88 } |
98 }); | 89 }); |
99 }); | 90 }); |
100 } | 91 } |
| 92 |
101 makeRequest(); | 93 makeRequest(); |
102 }); | 94 }); |
103 } | 95 } |
104 | 96 |
105 void main() { | 97 void main() { |
106 InternetAddress.lookup("localhost").then((hosts) { | 98 InternetAddress.lookup("localhost").then((hosts) { |
107 HOST = hosts.first; | 99 HOST = hosts.first; |
108 testListenOn(); | 100 testListenOn(); |
109 testEarlyClientClose(); | 101 testEarlyClientClose(); |
110 }); | 102 }); |
111 } | 103 } |
OLD | NEW |