OLD | NEW |
---|---|
(Empty) | |
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 | |
3 // BSD-style license that can be found in the LICENSE file. | |
4 | |
5 import "dart:io"; | |
6 import "dart:uri"; | |
7 import "dart:isolate"; | |
8 | |
9 const SERVER_ADDRESS = "127.0.0.1"; | |
10 const HOST_NAME = "localhost"; | |
11 | |
12 int numClientCertificatesReceived = 0; | |
13 // Run first test with no certificate name, second test with "localhost_cert". | |
14 String testCertificateName = null; | |
15 | |
16 Future test([var unused]) { | |
17 var completer = new Completer(); | |
18 HttpsServer server = new HttpsServer(); | |
19 Expect.throws(() => server.port); | |
20 | |
21 server.defaultRequestHandler = | |
22 (HttpRequest request, HttpResponse response) { | |
23 if (request.path == '/true') { | |
24 // Client certificate sent | |
25 numClientCertificatesReceived++; | |
26 Expect.isNotNull(request.certificate); | |
27 Expect.equals('CN=localhost', request.certificate.subject); | |
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 | |
44 HttpClient client = new HttpClient(); | |
45 Future testConnect(bool sendCertificate) { | |
46 client.sendClientCertificate = sendCertificate; | |
47 client.clientCertificate = testCertificateName; | |
48 var completer = new Completer(); | |
49 HttpClientConnection conn = | |
50 client.getUrl(new Uri.fromString( | |
51 "https://$HOST_NAME:${server.port}/$sendCertificate")); | |
52 conn.onRequest = (HttpClientRequest request) { | |
53 request.outputStream.close(); | |
54 }; | |
55 conn.onResponse = (HttpClientResponse response) { | |
56 Expect.isNotNull(response.certificate); | |
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).chain(testConnect).then((_) { | |
69 client.shutdown(); | |
70 server.close(); | |
71 Expect.throws(() => server.port); | |
72 // Run second test with a certificate name. | |
Søren Gjesse
2012/12/12 15:27:07
How about moving this setting of the certificate n
| |
73 testCertificateName = 'localhost_cert'; | |
74 completer.complete(null); | |
75 }); | |
76 return completer.future; | |
77 } | |
78 | |
79 void InitializeSSL() { | |
80 var testPkcertDatabase = | |
81 new Path.fromNative(new Options().script).directoryPath.append('pkcert/'); | |
82 SecureSocket.initialize(database: testPkcertDatabase.toNativePath(), | |
83 password: 'dartdart'); | |
84 } | |
85 | |
86 void main() { | |
87 var keepAlive = new ReceivePort(); | |
88 InitializeSSL(); | |
89 // Test two connections in sequence. | |
90 test().chain(test).then((_) { | |
Bill Hesse
2012/12/12 14:00:58
Indentation is fixed.
| |
91 Expect.equals(2, numClientCertificatesReceived); | |
92 keepAlive.close(); | |
93 }); | |
94 } | |
OLD | NEW |