| 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"; | 10 import "package:path/path.dart"; |
| 11 | 11 |
| 12 const HOST_NAME = "localhost"; | 12 const HOST_NAME = "localhost"; |
| 13 const CERTIFICATE = "localhost_cert"; | 13 const CERTIFICATE = "localhost_cert"; |
| 14 | 14 |
| 15 void testClientCertificate() { | 15 Future testClientCertificate() { |
| 16 asyncStart(); | 16 var completer = new Completer(); |
| 17 SecureServerSocket.bind(HOST_NAME, | 17 SecureServerSocket.bind(HOST_NAME, |
| 18 0, | 18 0, |
| 19 CERTIFICATE, | 19 CERTIFICATE, |
| 20 requestClientCertificate: true).then((server) { | 20 requestClientCertificate: true).then((server) { |
| 21 var clientEndFuture = SecureSocket.connect(HOST_NAME, | 21 var clientEndFuture = SecureSocket.connect(HOST_NAME, |
| 22 server.port, | 22 server.port, |
| 23 sendClientCertificate: true); | 23 sendClientCertificate: true); |
| 24 server.listen((serverEnd) { | 24 server.listen((serverEnd) { |
| 25 X509Certificate certificate = serverEnd.peerCertificate; | 25 X509Certificate certificate = serverEnd.peerCertificate; |
| 26 Expect.isNotNull(certificate); | 26 Expect.isNotNull(certificate); |
| 27 Expect.equals("CN=localhost", certificate.subject); | 27 Expect.equals("CN=localhost", certificate.subject); |
| 28 Expect.equals("CN=myauthority", certificate.issuer); | 28 Expect.equals("CN=myauthority", certificate.issuer); |
| 29 clientEndFuture.then((clientEnd) { | 29 clientEndFuture.then((clientEnd) { |
| 30 X509Certificate certificate = clientEnd.peerCertificate; | 30 X509Certificate certificate = clientEnd.peerCertificate; |
| 31 Expect.isNotNull(certificate); | 31 Expect.isNotNull(certificate); |
| 32 Expect.equals("CN=localhost", certificate.subject); | 32 Expect.equals("CN=localhost", certificate.subject); |
| 33 Expect.equals("CN=myauthority", certificate.issuer); | 33 Expect.equals("CN=myauthority", certificate.issuer); |
| 34 clientEnd.close(); | 34 clientEnd.close(); |
| 35 serverEnd.close(); | 35 serverEnd.close(); |
| 36 server.close(); | 36 server.close(); |
| 37 asyncEnd(); | 37 completer.complete(); |
| 38 }); | 38 }); |
| 39 }); | 39 }); |
| 40 }); | 40 }); |
| 41 return completer.future; |
| 41 } | 42 } |
| 42 | 43 |
| 43 void testRequiredClientCertificate() { | 44 Future testRequiredClientCertificate() { |
| 44 asyncStart(); | 45 var completer = new Completer(); |
| 45 SecureServerSocket.bind(HOST_NAME, | 46 SecureServerSocket.bind(HOST_NAME, |
| 46 0, | 47 0, |
| 47 CERTIFICATE, | 48 CERTIFICATE, |
| 48 requireClientCertificate: true).then((server) { | 49 requireClientCertificate: true).then((server) { |
| 49 var clientEndFuture = SecureSocket.connect(HOST_NAME, | 50 var clientEndFuture = SecureSocket.connect(HOST_NAME, |
| 50 server.port, | 51 server.port, |
| 51 sendClientCertificate: true); | 52 sendClientCertificate: true); |
| 52 server.listen((serverEnd) { | 53 server.listen((serverEnd) { |
| 53 X509Certificate certificate = serverEnd.peerCertificate; | 54 X509Certificate certificate = serverEnd.peerCertificate; |
| 54 Expect.isNotNull(certificate); | 55 Expect.isNotNull(certificate); |
| 55 Expect.equals("CN=localhost", certificate.subject); | 56 Expect.equals("CN=localhost", certificate.subject); |
| 56 Expect.equals("CN=myauthority", certificate.issuer); | 57 Expect.equals("CN=myauthority", certificate.issuer); |
| 57 clientEndFuture.then((clientEnd) { | 58 clientEndFuture.then((clientEnd) { |
| 58 X509Certificate certificate = clientEnd.peerCertificate; | 59 X509Certificate certificate = clientEnd.peerCertificate; |
| 59 Expect.isNotNull(certificate); | 60 Expect.isNotNull(certificate); |
| 60 Expect.equals("CN=localhost", certificate.subject); | 61 Expect.equals("CN=localhost", certificate.subject); |
| 61 Expect.equals("CN=myauthority", certificate.issuer); | 62 Expect.equals("CN=myauthority", certificate.issuer); |
| 62 clientEnd.close(); | 63 clientEnd.close(); |
| 63 serverEnd.close(); | 64 serverEnd.close(); |
| 64 server.close(); | 65 server.close(); |
| 65 asyncEnd(); | 66 completer.complete(); |
| 66 }); | 67 }); |
| 67 }); | 68 }); |
| 68 }); | 69 }); |
| 69 } | 70 return completer.future; |
| 70 | |
| 71 void testNoClientCertificate() { | |
| 72 asyncStart(); | |
| 73 SecureServerSocket.bind(HOST_NAME, | |
| 74 0, | |
| 75 CERTIFICATE, | |
| 76 requestClientCertificate: true).then((server) { | |
| 77 var clientEndFuture = SecureSocket.connect(HOST_NAME, | |
| 78 server.port); | |
| 79 server.listen((serverEnd) { | |
| 80 X509Certificate certificate = serverEnd.peerCertificate; | |
| 81 Expect.isNull(certificate); | |
| 82 clientEndFuture.then((clientEnd) { | |
| 83 clientEnd.close(); | |
| 84 serverEnd.close(); | |
| 85 server.close(); | |
| 86 asyncEnd(); | |
| 87 }); | |
| 88 }); | |
| 89 }); | |
| 90 } | |
| 91 | |
| 92 void testNoRequiredClientCertificate() { | |
| 93 asyncStart(); | |
| 94 bool clientError = false; | |
| 95 SecureServerSocket.bind(HOST_NAME, | |
| 96 0, | |
| 97 CERTIFICATE, | |
| 98 requireClientCertificate: true).then((server) { | |
| 99 Future clientDone = SecureSocket.connect(HOST_NAME, server.port) | |
| 100 .catchError((e) { clientError = true; }); | |
| 101 server.listen((serverEnd) { | |
| 102 Expect.fail("Got a unverifiable connection"); | |
| 103 }, | |
| 104 onError: (e) { | |
| 105 clientDone.then((_) { | |
| 106 Expect.isTrue(clientError); | |
| 107 server.close(); | |
| 108 asyncEnd(); | |
| 109 }); | |
| 110 }); | |
| 111 }); | |
| 112 } | 71 } |
| 113 | 72 |
| 114 void main() { | 73 void main() { |
| 115 String certificateDatabase = join(dirname(Platform.script), 'pkcert'); | 74 String certificateDatabase = join(dirname(Platform.script), 'pkcert'); |
| 116 SecureSocket.initialize(database: certificateDatabase, | 75 SecureSocket.initialize(database: certificateDatabase, |
| 117 password: 'dartdart', | 76 password: 'dartdart', |
| 118 useBuiltinRoots: false); | 77 useBuiltinRoots: false); |
| 119 | 78 |
| 120 testClientCertificate(); | 79 asyncStart(); |
| 121 testRequiredClientCertificate(); | 80 testClientCertificate() |
| 122 testNoClientCertificate(); | 81 .then((_) => testRequiredClientCertificate()) |
| 123 testNoRequiredClientCertificate(); | 82 .then((_) => asyncEnd()); |
| 124 } | 83 } |
| OLD | NEW |