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 |
| 14 Function test(Map options) { |
| 15 Future runTest([var unused]) { |
| 16 var completer = new Completer(); |
| 17 HttpsServer server = new HttpsServer(); |
| 18 Expect.throws(() => server.port); |
| 19 |
| 20 server.defaultRequestHandler = |
| 21 (HttpRequest request, HttpResponse response) { |
| 22 if (request.path == '/true') { |
| 23 // Client certificate sent |
| 24 numClientCertificatesReceived++; |
| 25 Expect.isNotNull(request.certificate); |
| 26 Expect.equals('CN=localhost', request.certificate.subject); |
| 27 } else { |
| 28 Expect.equals('/false', request.path); |
| 29 Expect.isNull(request.certificate); |
| 30 } |
| 31 |
| 32 request.inputStream.onClosed = () { |
| 33 response.outputStream.close(); |
| 34 }; |
| 35 }; |
| 36 |
| 37 server.listen(SERVER_ADDRESS, |
| 38 0, |
| 39 backlog: 5, |
| 40 certificate_name: 'CN=$HOST_NAME', |
| 41 requestClientCertificate: true); |
| 42 |
| 43 HttpClient client = new HttpClient(); |
| 44 Future testConnect(bool sendCertificate) { |
| 45 client.sendClientCertificate = sendCertificate; |
| 46 client.clientCertificate = options['certificateName']; |
| 47 var completer = new Completer(); |
| 48 HttpClientConnection conn = |
| 49 client.getUrl(new Uri.fromString( |
| 50 "https://$HOST_NAME:${server.port}/$sendCertificate")); |
| 51 conn.onRequest = (HttpClientRequest request) { |
| 52 request.outputStream.close(); |
| 53 }; |
| 54 conn.onResponse = (HttpClientResponse response) { |
| 55 Expect.isNotNull(response.certificate); |
| 56 Expect.equals('CN=myauthority', response.certificate.issuer); |
| 57 response.inputStream.onClosed = () { |
| 58 completer.complete(false); // Chained call will not send cert. |
| 59 }; |
| 60 }; |
| 61 conn.onError = (Exception e) { |
| 62 Expect.fail("Unexpected error in Https Client: $e"); |
| 63 }; |
| 64 return completer.future; |
| 65 } |
| 66 |
| 67 testConnect(true).chain(testConnect).then((_) { |
| 68 client.shutdown(); |
| 69 server.close(); |
| 70 Expect.throws(() => server.port); |
| 71 // Run second test with a certificate name. |
| 72 completer.complete(null); |
| 73 }); |
| 74 return completer.future; |
| 75 } |
| 76 return runTest; |
| 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({'certificateName': null})() |
| 91 .chain(test({'certificateName': 'localhost_cert'})) |
| 92 .then((_) { |
| 93 Expect.equals(2, numClientCertificatesReceived); |
| 94 keepAlive.close(); |
| 95 }); |
| 96 } |
OLD | NEW |