| 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 import "dart:async"; | |
| 6 import "dart:io"; | |
| 7 | |
| 8 import "package:async_helper/async_helper.dart"; | |
| 9 import "package:expect/expect.dart"; | |
| 10 | |
| 11 InternetAddress HOST; | |
| 12 String localFile(path) => Platform.script.resolve(path).toFilePath(); | |
| 13 | |
| 14 SecurityContext serverContext = new SecurityContext() | |
| 15 ..useCertificateChain(localFile('certificates/server_chain.pem')) | |
| 16 ..usePrivateKey(localFile('certificates/server_key.pem'), | |
| 17 password: 'dartdart'); | |
| 18 | |
| 19 SecurityContext clientContext = new SecurityContext() | |
| 20 ..setTrustedCertificates(file: localFile('certificates/trusted_certs.pem')); | |
| 21 | |
| 22 Future testNoClientCertificate() { | |
| 23 var completer = new Completer(); | |
| 24 SecureServerSocket.bind(HOST, | |
| 25 0, | |
| 26 serverContext, | |
| 27 requestClientCertificate: true).then((server) { | |
| 28 var clientEndFuture = SecureSocket.connect(HOST, | |
| 29 server.port, | |
| 30 context: clientContext); | |
| 31 server.listen((serverEnd) { | |
| 32 X509Certificate certificate = serverEnd.peerCertificate; | |
| 33 Expect.isNull(certificate); | |
| 34 clientEndFuture.then((clientEnd) { | |
| 35 clientEnd.close(); | |
| 36 serverEnd.close(); | |
| 37 server.close(); | |
| 38 completer.complete(); | |
| 39 }); | |
| 40 }); | |
| 41 }); | |
| 42 return completer.future; | |
| 43 } | |
| 44 | |
| 45 Future testNoRequiredClientCertificate() { | |
| 46 var completer = new Completer(); | |
| 47 bool clientError = false; | |
| 48 SecureServerSocket.bind(HOST, | |
| 49 0, | |
| 50 serverContext, | |
| 51 requireClientCertificate: true).then((server) { | |
| 52 Future clientDone = | |
| 53 SecureSocket.connect(HOST, server.port, context: clientContext) | |
| 54 .catchError((e) { clientError = true; }); | |
| 55 server.listen((serverEnd) { | |
| 56 Expect.fail("Got a unverifiable connection"); | |
| 57 }, | |
| 58 onError: (e) { | |
| 59 clientDone.then((_) { | |
| 60 Expect.isTrue(clientError); | |
| 61 server.close(); | |
| 62 completer.complete(); | |
| 63 }); | |
| 64 }); | |
| 65 }); | |
| 66 return completer.future; | |
| 67 } | |
| 68 | |
| 69 void main() { | |
| 70 asyncStart(); | |
| 71 InternetAddress.lookup("localhost").then((hosts) => HOST = hosts.first) | |
| 72 .then((_) => testNoRequiredClientCertificate()) | |
| 73 .then((_) => testNoClientCertificate()) | |
| 74 .then((_) => asyncEnd()); | |
| 75 } | |
| OLD | NEW |