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 import "dart:async"; | 5 import "dart:async"; |
6 import "dart:io"; | 6 import "dart:io"; |
7 import "dart:uri"; | 7 import "dart:uri"; |
8 import "dart:isolate"; | 8 import "dart:isolate"; |
9 | 9 |
10 const SERVER_ADDRESS = "127.0.0.1"; | 10 const SERVER_ADDRESS = "127.0.0.1"; |
11 const HOST_NAME = "localhost"; | 11 const HOST_NAME = "localhost"; |
12 | 12 |
13 int numClientCertificatesReceived = 0; | |
14 | 13 |
15 Function test(Map options) { | 14 Function test() { |
16 Future runTest([var unused]) { | 15 var keepAlive = new ReceivePort(); |
17 var completer = new Completer(); | 16 HttpServer.bindSecure(SERVER_ADDRESS, |
18 HttpsServer server = new HttpsServer(); | 17 0, |
19 Expect.throws(() => server.port); | 18 backlog: 5, |
20 | 19 certificateName: 'localhost_cert', |
21 server.defaultRequestHandler = | 20 requestClientCertificate: true).then((server) { |
22 (HttpRequest request, HttpResponse response) { | 21 server.listen((HttpRequest request) { |
23 if (request.path == '/true') { | 22 Expect.isNotNull(request.certificate); |
24 // Client certificate sent | 23 Expect.equals('CN=localhost', request.certificate.subject); |
25 numClientCertificatesReceived++; | 24 request.response.addString("Hello"); |
26 Expect.isNotNull(request.certificate); | 25 request.response.close(); |
27 Expect.equals('CN=localhost', request.certificate.subject); | 26 }); |
28 } else { | |
29 Expect.equals('/false', request.path); | |
30 Expect.isNull(request.certificate); | |
31 } | |
32 | |
33 request.inputStream.onClosed = () { | |
34 response.outputStream.close(); | |
35 }; | |
36 }; | |
37 | |
38 server.listen(SERVER_ADDRESS, | |
39 0, | |
40 backlog: 5, | |
41 certificate_name: 'CN=$HOST_NAME', | |
42 requestClientCertificate: true); | |
43 | 27 |
44 HttpClient client = new HttpClient(); | 28 HttpClient client = new HttpClient(); |
45 Future testConnect(bool sendCertificate) { | 29 client.getUrl(Uri.parse("https://$HOST_NAME:${server.port}/")) |
46 client.sendClientCertificate = sendCertificate; | 30 .then((request) => request.close()) |
47 client.clientCertificate = options['certificateName']; | 31 .then((response) => |
48 var completer = new Completer(); | 32 response.reduce(<int>[], (message, data) => message..addAll(data))) |
49 HttpClientConnection conn = | 33 .then((message) { |
50 client.getUrl(Uri.parse( | 34 String received = new String.fromCharCodes(message); |
51 "https://$HOST_NAME:${server.port}/$sendCertificate")); | 35 Expect.equals(received, "Hello"); |
52 conn.onRequest = (HttpClientRequest request) { | 36 client.close(); |
53 request.outputStream.close(); | 37 server.close(); |
54 }; | 38 keepAlive.close(); |
55 conn.onResponse = (HttpClientResponse response) { | 39 }); |
56 Expect.isNotNull(response.certificate); | 40 }); |
57 Expect.equals('CN=myauthority', response.certificate.issuer); | |
58 response.inputStream.onClosed = () { | |
59 completer.complete(false); // Chained call will not send cert. | |
60 }; | |
61 }; | |
62 conn.onError = (Exception e) { | |
63 Expect.fail("Unexpected error in Https Client: $e"); | |
64 }; | |
65 return completer.future; | |
66 } | |
67 | |
68 testConnect(true).then(testConnect).then((_) { | |
69 client.shutdown(); | |
70 server.close(); | |
71 Expect.throws(() => server.port); | |
72 // Run second test with a certificate name. | |
73 completer.complete(null); | |
74 }); | |
75 return completer.future; | |
76 } | |
77 return runTest; | |
78 } | 41 } |
79 | 42 |
80 void InitializeSSL() { | 43 void InitializeSSL() { |
81 var testPkcertDatabase = | 44 var testPkcertDatabase = |
82 new Path(new Options().script).directoryPath.append('pkcert/'); | 45 new Path(new Options().script).directoryPath.append('pkcert/'); |
83 SecureSocket.initialize(database: testPkcertDatabase.toNativePath(), | 46 SecureSocket.initialize(database: testPkcertDatabase.toNativePath(), |
84 password: 'dartdart'); | 47 password: 'dartdart'); |
85 } | 48 } |
86 | 49 |
87 void main() { | 50 void main() { |
88 var keepAlive = new ReceivePort(); | |
89 InitializeSSL(); | 51 InitializeSSL(); |
90 // Test two connections in sequence. | 52 test(); |
91 test({'certificateName': null})() | |
92 .then((_) => test({'certificateName': 'localhost_cert'})()) | |
93 .then((_) { | |
94 Expect.equals(2, numClientCertificatesReceived); | |
95 keepAlive.close(); | |
96 }); | |
97 } | 53 } |
OLD | NEW |