| 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 import "dart:async"; | 5 import "dart:async"; |
| 6 import "dart:io"; | 6 import "dart:io"; |
| 7 | 7 |
| 8 import "package:async_helper/async_helper.dart"; | 8 import "package:async_helper/async_helper.dart"; |
| 9 import "package:expect/expect.dart"; | 9 import "package:expect/expect.dart"; |
| 10 import "package:path/path.dart"; | |
| 11 | 10 |
| 12 const HOST_NAME = "localhost"; | 11 InternetAddress HOST; |
| 13 const CERTIFICATE = "localhost_cert"; | 12 const CERTIFICATE = "localhost_cert"; |
| 14 | 13 |
| 15 Future testClientCertificate() { | 14 Future testClientCertificate() { |
| 16 var completer = new Completer(); | 15 var completer = new Completer(); |
| 17 SecureServerSocket.bind(HOST_NAME, | 16 SecureServerSocket.bind(HOST, |
| 18 0, | 17 0, |
| 19 CERTIFICATE, | 18 CERTIFICATE, |
| 20 requestClientCertificate: true).then((server) { | 19 requestClientCertificate: true).then((server) { |
| 21 var clientEndFuture = SecureSocket.connect(HOST_NAME, | 20 var clientEndFuture = SecureSocket.connect(HOST, |
| 22 server.port, | 21 server.port, |
| 23 sendClientCertificate: true); | 22 sendClientCertificate: true); |
| 24 server.listen((serverEnd) { | 23 server.listen((serverEnd) { |
| 25 X509Certificate certificate = serverEnd.peerCertificate; | 24 X509Certificate certificate = serverEnd.peerCertificate; |
| 26 Expect.isNotNull(certificate); | 25 Expect.isNotNull(certificate); |
| 27 Expect.equals("CN=localhost", certificate.subject); | 26 Expect.equals("CN=localhost", certificate.subject); |
| 28 Expect.equals("CN=myauthority", certificate.issuer); | 27 Expect.equals("CN=myauthority", certificate.issuer); |
| 29 clientEndFuture.then((clientEnd) { | 28 clientEndFuture.then((clientEnd) { |
| 30 X509Certificate certificate = clientEnd.peerCertificate; | 29 X509Certificate certificate = clientEnd.peerCertificate; |
| 31 Expect.isNotNull(certificate); | 30 Expect.isNotNull(certificate); |
| 32 Expect.equals("CN=localhost", certificate.subject); | 31 Expect.equals("CN=localhost", certificate.subject); |
| 33 Expect.equals("CN=myauthority", certificate.issuer); | 32 Expect.equals("CN=myauthority", certificate.issuer); |
| 34 clientEnd.close(); | 33 clientEnd.close(); |
| 35 serverEnd.close(); | 34 serverEnd.close(); |
| 36 server.close(); | 35 server.close(); |
| 37 completer.complete(); | 36 completer.complete(); |
| 38 }); | 37 }); |
| 39 }); | 38 }); |
| 40 }); | 39 }); |
| 41 return completer.future; | 40 return completer.future; |
| 42 } | 41 } |
| 43 | 42 |
| 44 Future testRequiredClientCertificate() { | 43 Future testRequiredClientCertificate() { |
| 45 var completer = new Completer(); | 44 var completer = new Completer(); |
| 46 SecureServerSocket.bind(HOST_NAME, | 45 SecureServerSocket.bind(HOST, |
| 47 0, | 46 0, |
| 48 CERTIFICATE, | 47 CERTIFICATE, |
| 49 requireClientCertificate: true).then((server) { | 48 requireClientCertificate: true).then((server) { |
| 50 var clientEndFuture = SecureSocket.connect(HOST_NAME, | 49 var clientEndFuture = SecureSocket.connect(HOST, |
| 51 server.port, | 50 server.port, |
| 52 sendClientCertificate: true); | 51 sendClientCertificate: true); |
| 53 server.listen((serverEnd) { | 52 server.listen((serverEnd) { |
| 54 X509Certificate certificate = serverEnd.peerCertificate; | 53 X509Certificate certificate = serverEnd.peerCertificate; |
| 55 Expect.isNotNull(certificate); | 54 Expect.isNotNull(certificate); |
| 56 Expect.equals("CN=localhost", certificate.subject); | 55 Expect.equals("CN=localhost", certificate.subject); |
| 57 Expect.equals("CN=myauthority", certificate.issuer); | 56 Expect.equals("CN=myauthority", certificate.issuer); |
| 58 clientEndFuture.then((clientEnd) { | 57 clientEndFuture.then((clientEnd) { |
| 59 X509Certificate certificate = clientEnd.peerCertificate; | 58 X509Certificate certificate = clientEnd.peerCertificate; |
| 60 Expect.isNotNull(certificate); | 59 Expect.isNotNull(certificate); |
| 61 Expect.equals("CN=localhost", certificate.subject); | 60 Expect.equals("CN=localhost", certificate.subject); |
| 62 Expect.equals("CN=myauthority", certificate.issuer); | 61 Expect.equals("CN=myauthority", certificate.issuer); |
| 63 clientEnd.close(); | 62 clientEnd.close(); |
| 64 serverEnd.close(); | 63 serverEnd.close(); |
| 65 server.close(); | 64 server.close(); |
| 66 completer.complete(); | 65 completer.complete(); |
| 67 }); | 66 }); |
| 68 }); | 67 }); |
| 69 }); | 68 }); |
| 70 return completer.future; | 69 return completer.future; |
| 71 } | 70 } |
| 72 | 71 |
| 73 void main() { | 72 void main() { |
| 74 String certificateDatabase = Platform.script.resolve('pkcert').toFilePath(); | 73 String certificateDatabase = Platform.script.resolve('pkcert').toFilePath(); |
| 75 SecureSocket.initialize(database: certificateDatabase, | 74 SecureSocket.initialize(database: certificateDatabase, |
| 76 password: 'dartdart', | 75 password: 'dartdart', |
| 77 useBuiltinRoots: false); | 76 useBuiltinRoots: false); |
| 78 | 77 |
| 79 asyncStart(); | 78 asyncStart(); |
| 80 testClientCertificate() | 79 InternetAddress.lookup("localhost").then((hosts) => HOST = hosts.first) |
| 80 .then((_) => testClientCertificate()) |
| 81 .then((_) => testRequiredClientCertificate()) | 81 .then((_) => testRequiredClientCertificate()) |
| 82 .then((_) => asyncEnd()); | 82 .then((_) => asyncEnd()); |
| 83 } | 83 } |
| OLD | NEW |