OLD | NEW |
(Empty) | |
| 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 |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 // This test verifies that client certificates work, if the client and server |
| 6 // are in separate processes, and that connection renegotiation works, and |
| 7 // can request a client certificate to be sent. |
| 8 |
| 9 import "package:expect/expect.dart"; |
| 10 import "package:pathos/path.dart" as path; |
| 11 import "dart:async"; |
| 12 import "dart:io"; |
| 13 |
| 14 const HOST_NAME = "localhost"; |
| 15 const CERTIFICATE = "localhost_cert"; |
| 16 |
| 17 |
| 18 String certificateDatabase() => |
| 19 path.join(path.dirname(new Options().script), 'pkcert', ''); |
| 20 |
| 21 |
| 22 Future<SecureServerSocket> runServer() { |
| 23 SecureSocket.initialize(database: certificateDatabase(), |
| 24 password: 'dartdart'); |
| 25 |
| 26 return SecureServerSocket.bind(HOST_NAME, 0, CERTIFICATE) |
| 27 .then((SecureServerSocket server) { |
| 28 server.listen((SecureSocket socket) { |
| 29 Expect.isNull(socket.peerCertificate); |
| 30 |
| 31 StreamIterator<String> input = |
| 32 new StreamIterator(socket.transform(new StringDecoder()) |
| 33 .transform(new LineTransformer())); |
| 34 input.moveNext().then((success) { |
| 35 Expect.isTrue(success); |
| 36 Expect.equals('first', input.current); |
| 37 socket.writeln('first reply'); |
| 38 return input.moveNext(); |
| 39 }).then((success) { |
| 40 Expect.isTrue(success); |
| 41 Expect.equals('renegotiated', input.current); |
| 42 Expect.isNull(socket.peerCertificate); |
| 43 socket.renegotiate(requestClientCertificate: true, |
| 44 requireClientCertificate: true, |
| 45 useSessionCache: false); |
| 46 socket.writeln('server renegotiated'); |
| 47 return input.moveNext(); |
| 48 }).then((success) { |
| 49 Expect.isTrue(success); |
| 50 Expect.equals('second', input.current); |
| 51 X509Certificate certificate = socket.peerCertificate; |
| 52 Expect.isNotNull(certificate); |
| 53 Expect.equals("CN=localhost", certificate.subject); |
| 54 Expect.equals("CN=myauthority", certificate.issuer); |
| 55 server.close(); |
| 56 socket.close(); |
| 57 }); |
| 58 }); |
| 59 return server; |
| 60 }); |
| 61 } |
| 62 |
| 63 |
| 64 void main() { |
| 65 runServer() |
| 66 .then((SecureServerSocket server) { |
| 67 final options = new Options(); |
| 68 var clientScript = |
| 69 options.script.replaceFirst("_test.dart", "_client.dart"); |
| 70 Expect.isTrue(clientScript.endsWith("_client.dart")); |
| 71 Process.run(options.executable, |
| 72 [clientScript, |
| 73 server.port.toString(), |
| 74 certificateDatabase()]) |
| 75 .then((ProcessResult result) { |
| 76 if (result.exitCode != 0) { |
| 77 print("Client failed, stdout:"); |
| 78 print(result.stdout); |
| 79 print(" stderr:"); |
| 80 print(result.stderr); |
| 81 Expect.fail('Client subprocess exit code: ${result.exitCode}'); |
| 82 } |
| 83 }); |
| 84 }); |
| 85 } |
OLD | NEW |