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 | 9 |
| 10 import "package:async_helper/async_helper.dart"; |
10 import "package:expect/expect.dart"; | 11 import "package:expect/expect.dart"; |
11 import "package:path/path.dart"; | 12 import "package:path/path.dart"; |
12 import "dart:async"; | 13 import "dart:async"; |
13 import "dart:io"; | 14 import "dart:io"; |
14 | 15 |
15 String localFile(path) => Platform.script.resolve(path).toFilePath(); | 16 String localFile(path) => Platform.script.resolve(path).toFilePath(); |
16 | 17 |
17 SecurityContext serverContext = new SecurityContext() | 18 SecurityContext serverContext(String certType) => new SecurityContext() |
18 ..useCertificateChainSync(localFile('certificates/server_chain.pem')) | 19 ..useCertificateChainSync(localFile('certificates/server_chain.$certType')) |
19 ..usePrivateKeySync(localFile('certificates/server_key.pem'), | 20 ..usePrivateKeySync(localFile('certificates/server_key.$certType'), |
20 password: 'dartdart'); | 21 password: 'dartdart'); |
21 | 22 |
22 SecurityContext clientContext = new SecurityContext() | 23 SecurityContext clientContext(String certType) => new SecurityContext() |
23 ..setTrustedCertificatesSync(localFile('certificates/trusted_certs.pem')); | 24 ..setTrustedCertificatesSync(localFile( |
| 25 'certificates/trusted_certs.$certType')); |
24 | 26 |
25 Future<HttpServer> startServer() { | 27 Future<HttpServer> startServer(String certType) { |
26 return HttpServer.bindSecure( | 28 return HttpServer.bindSecure( |
27 "localhost", | 29 "localhost", |
28 0, | 30 0, |
29 serverContext, | 31 serverContext(certType), |
30 backlog: 5).then((server) { | 32 backlog: 5).then((server) { |
31 server.listen((HttpRequest request) { | 33 server.listen((HttpRequest request) { |
32 request.listen( | 34 request.listen( |
33 (_) { }, | 35 (_) { }, |
34 onDone: () { | 36 onDone: () { |
35 request.response.contentLength = 100; | 37 request.response.contentLength = 100; |
36 for (int i = 0; i < 10; i++) { | 38 for (int i = 0; i < 10; i++) { |
37 request.response.add([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]); | 39 request.response.add([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]); |
38 } | 40 } |
39 request.response.close(); | 41 request.response.close(); |
40 }); | 42 }); |
41 }); | 43 }); |
42 return server; | 44 return server; |
43 }); | 45 }); |
44 } | 46 } |
45 | 47 |
46 void main() { | 48 Future test(String certType) { |
47 List<int> body = <int>[]; | 49 List<int> body = <int>[]; |
48 startServer().then((server) { | 50 startServer(certType).then((server) { |
49 SecureSocket.connect("localhost", server.port, context: clientContext) | 51 SecureSocket.connect( |
| 52 "localhost", server.port, context: clientContext(certType)) |
50 .then((socket) { | 53 .then((socket) { |
51 socket.write("GET / HTTP/1.0\r\nHost: localhost\r\n\r\n"); | 54 socket.write("GET / HTTP/1.0\r\nHost: localhost\r\n\r\n"); |
52 socket.close(); | 55 socket.close(); |
53 socket.listen( | 56 socket.listen( |
54 (List<int> data) { | 57 (List<int> data) { |
55 body.addAll(data); | 58 body.addAll(data); |
56 }, | 59 }, |
57 onDone: () { | 60 onDone: () { |
58 Expect.isTrue(body.length > 100, "$body\n${body.length}"); | 61 Expect.isTrue(body.length > 100, "$body\n${body.length}"); |
59 Expect.equals(72, body[0]); | 62 Expect.equals(72, body[0]); |
60 Expect.equals(9, body[body.length - 1]); | 63 Expect.equals(9, body[body.length - 1]); |
61 server.close(); | 64 server.close(); |
62 }, | 65 }, |
63 onError: (e, trace) { | 66 onError: (e, trace) { |
64 String msg = "Unexpected error $e"; | 67 String msg = "Unexpected error $e"; |
65 if (trace != null) msg += "\nStackTrace: $trace"; | 68 if (trace != null) msg += "\nStackTrace: $trace"; |
66 Expect.fail(msg); | 69 Expect.fail(msg); |
67 }); | 70 }); |
68 }); | 71 }); |
69 }); | 72 }); |
70 } | 73 } |
| 74 |
| 75 main() async { |
| 76 asyncStart(); |
| 77 await test('pem'); |
| 78 await test('p12'); |
| 79 asyncEnd(); |
| 80 } |
OLD | NEW |