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