| Index: tests/standalone/io/secure_server_client_certificate_test.dart
|
| diff --git a/tests/standalone/io/secure_server_client_certificate_test.dart b/tests/standalone/io/secure_server_client_certificate_test.dart
|
| index 3f34d93042e447f2231c4c908f0fe4830538dd53..81f0995a6af14854051f86e287cebd9eae602b20 100644
|
| --- a/tests/standalone/io/secure_server_client_certificate_test.dart
|
| +++ b/tests/standalone/io/secure_server_client_certificate_test.dart
|
| @@ -15,75 +15,62 @@ String localFile(path) => Platform.script.resolve(path).toFilePath();
|
| SecurityContext serverContext = new SecurityContext()
|
| ..useCertificateChain(localFile('certificates/server_chain.pem'))
|
| ..usePrivateKey(localFile('certificates/server_key.pem'),
|
| - password: 'dartdart');
|
| + password: 'dartdart')
|
| + ..setTrustedCertificates(file: localFile('certificates/client_authority.pem'))
|
| + ..setClientAuthorities(localFile('certificates/client_authority.pem'));
|
|
|
| -SecurityContext clientContext = new SecurityContext()
|
| +SecurityContext clientCertContext = new SecurityContext()
|
| + ..setTrustedCertificates(file: localFile('certificates/trusted_certs.pem'))
|
| + ..useCertificateChain(localFile('certificates/client1.pem'))
|
| + ..usePrivateKey(localFile('certificates/client1_key.pem'),
|
| + password: 'dartdart');
|
| +
|
| +SecurityContext clientNoCertContext = new SecurityContext()
|
| ..setTrustedCertificates(file: localFile('certificates/trusted_certs.pem'));
|
|
|
| -Future testClientCertificate() {
|
| - var completer = new Completer();
|
| - SecureServerSocket.bind(HOST,
|
| - 0,
|
| - serverContext,
|
| - requestClientCertificate: true).then((server) {
|
| - var clientEndFuture = SecureSocket.connect(HOST,
|
| - server.port,
|
| - context: clientContext,
|
| - sendClientCertificate: true);
|
| - server.listen((serverEnd) {
|
| - X509Certificate certificate = serverEnd.peerCertificate;
|
| - Expect.isNotNull(certificate);
|
| - Expect.equals("CN=localhost", certificate.subject);
|
| - Expect.equals("CN=myauthority", certificate.issuer);
|
| - clientEndFuture.then((clientEnd) {
|
| - X509Certificate certificate = clientEnd.peerCertificate;
|
| - Expect.isNotNull(certificate);
|
| - Expect.equals("CN=localhost", certificate.subject);
|
| - Expect.equals("CN=myauthority", certificate.issuer);
|
| - clientEnd.close();
|
| - serverEnd.close();
|
| - server.close();
|
| - completer.complete();
|
| - });
|
| - });
|
| - });
|
| - return completer.future;
|
| -}
|
| +Future testClientCertificate({bool required, bool sendCert}) async {
|
| + var server = await SecureServerSocket.bind(HOST, 0, serverContext,
|
| + requestClientCertificate: true, requireClientCertificate: required);
|
| + var clientContext = sendCert ? clientCertContext : clientNoCertContext;
|
| + var clientEndFuture = SecureSocket.connect(HOST, server.port,
|
| + context: clientContext, sendClientCertificate: true);
|
| + if (required && !sendCert) {
|
| + try {
|
| + await server.first;
|
| + } catch (e) {
|
| + try {
|
| + await clientEndFuture;
|
| + } catch (e) {
|
| + return;
|
| + }
|
| + }
|
| + Expect.fail("Connection succeeded with no required client certificate");
|
| + }
|
| + var serverEnd = await server.first;
|
| + var clientEnd = await clientEndFuture;
|
|
|
| -Future testRequiredClientCertificate() {
|
| - var completer = new Completer();
|
| - SecureServerSocket.bind(HOST,
|
| - 0,
|
| - serverContext,
|
| - requireClientCertificate: true).then((server) {
|
| - var clientEndFuture = SecureSocket.connect(HOST,
|
| - server.port,
|
| - context: clientContext,
|
| - sendClientCertificate: true);
|
| - server.listen((serverEnd) {
|
| - X509Certificate certificate = serverEnd.peerCertificate;
|
| - Expect.isNotNull(certificate);
|
| - Expect.equals("CN=localhost", certificate.subject);
|
| - Expect.equals("CN=myauthority", certificate.issuer);
|
| - clientEndFuture.then((clientEnd) {
|
| - X509Certificate certificate = clientEnd.peerCertificate;
|
| - Expect.isNotNull(certificate);
|
| - Expect.equals("CN=localhost", certificate.subject);
|
| - Expect.equals("CN=myauthority", certificate.issuer);
|
| - clientEnd.close();
|
| - serverEnd.close();
|
| - server.close();
|
| - completer.complete();
|
| - });
|
| - });
|
| - });
|
| - return completer.future;
|
| + X509Certificate clientCertificate = serverEnd.peerCertificate;
|
| + if (sendCert) {
|
| + Expect.isNotNull(clientCertificate);
|
| + Expect.equals("/CN=user1", clientCertificate.subject);
|
| + Expect.equals("/CN=clientauthority", clientCertificate.issuer);
|
| + } else {
|
| + Expect.isNull(clientCertificate);
|
| + }
|
| + X509Certificate serverCertificate = clientEnd.peerCertificate;
|
| + Expect.isNotNull(serverCertificate);
|
| + Expect.equals("/CN=localhost", serverCertificate.subject);
|
| + Expect.equals("/CN=intermediateauthority", serverCertificate.issuer);
|
| + clientEnd.close();
|
| + serverEnd.close();
|
| }
|
|
|
| -void main() {
|
| +main() async {
|
| asyncStart();
|
| - InternetAddress.lookup("localhost").then((hosts) => HOST = hosts.first)
|
| - .then((_) => testClientCertificate())
|
| - .then((_) => testRequiredClientCertificate())
|
| - .then((_) => asyncEnd());
|
| + HOST = (await InternetAddress.lookup("localhost")).first;
|
| + await testClientCertificate(required: false, sendCert: true);
|
| + await testClientCertificate(required: true, sendCert: true);
|
| + await testClientCertificate(required: false, sendCert: false);
|
| + await testClientCertificate(required: true, sendCert: false);
|
| + asyncEnd();
|
| }
|
|
|