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 // OtherResources=certificates/server_chain.pem | 5 // OtherResources=certificates/server_chain.pem |
6 // OtherResources=certificates/server_key.pem | 6 // OtherResources=certificates/server_key.pem |
7 // OtherResources=certificates/untrusted_server_chain.pem | 7 // OtherResources=certificates/untrusted_server_chain.pem |
8 // OtherResources=certificates/untrusted_server_key.pem | 8 // OtherResources=certificates/untrusted_server_key.pem |
9 // OtherResources=certificates/trusted_certs.pem | 9 // OtherResources=certificates/trusted_certs.pem |
10 // OtherResources=https_unauthorized_client.dart | 10 // OtherResources=https_unauthorized_client.dart |
11 | 11 |
12 // This test verifies that secure connections that fail due to | 12 // This test verifies that secure connections that fail due to |
13 // unauthenticated certificates throw exceptions in HttpClient. | 13 // unauthenticated certificates throw exceptions in HttpClient. |
14 | 14 |
15 import "package:expect/expect.dart"; | 15 import "package:expect/expect.dart"; |
16 import "package:path/path.dart"; | 16 import "package:path/path.dart"; |
17 import "dart:async"; | 17 import "dart:async"; |
18 import "dart:io"; | 18 import "dart:io"; |
19 | 19 |
20 const HOST_NAME = "localhost"; | 20 const HOST_NAME = "localhost"; |
21 const CERTIFICATE = "localhost_cert"; | 21 const CERTIFICATE = "localhost_cert"; |
22 | 22 |
23 String localFile(path) => Platform.script.resolve(path).toFilePath(); | 23 String localFile(path) => Platform.script.resolve(path).toFilePath(); |
24 | 24 |
25 SecurityContext untrustedServerContext = new SecurityContext() | 25 SecurityContext untrustedServerContext = new SecurityContext() |
26 ..useCertificateChain(localFile('certificates/untrusted_server_chain.pem')) | 26 ..useCertificateChain(localFile('certificates/untrusted_server_chain.pem')) |
27 ..usePrivateKey(localFile('certificates/untrusted_server_key.pem'), | 27 ..usePrivateKey(localFile('certificates/untrusted_server_key.pem'), |
28 password: 'dartdart'); | 28 password: 'dartdart'); |
29 | 29 |
30 SecurityContext clientContext = new SecurityContext() | 30 SecurityContext clientContext = new SecurityContext() |
31 ..setTrustedCertificates(localFile('certificates/trusted_certs.pem')); | 31 ..setTrustedCertificates(localFile('certificates/trusted_certs.pem')); |
32 | 32 |
33 Future<SecureServerSocket> runServer() { | 33 Future<SecureServerSocket> runServer() { |
34 return HttpServer.bindSecure( | 34 return HttpServer |
35 HOST_NAME, 0, untrustedServerContext, backlog: 5) | 35 .bindSecure(HOST_NAME, 0, untrustedServerContext, backlog: 5) |
36 .then((server) { | 36 .then((server) { |
37 server.listen((HttpRequest request) { | 37 server.listen((HttpRequest request) { |
38 request.listen((_) { }, onDone: () { request.response.close(); }); | 38 request.listen((_) {}, onDone: () { |
39 }, onError: (e) { if (e is! HandshakeException) throw e; }); | 39 request.response.close(); |
| 40 }); |
| 41 }, onError: (e) { |
| 42 if (e is! HandshakeException) throw e; |
| 43 }); |
40 return server; | 44 return server; |
41 }); | 45 }); |
42 } | 46 } |
43 | 47 |
44 void main() { | 48 void main() { |
45 var clientScript = localFile('https_unauthorized_client.dart'); | 49 var clientScript = localFile('https_unauthorized_client.dart'); |
46 Future clientProcess(int port) { | 50 Future clientProcess(int port) { |
47 return Process.run(Platform.executable, | 51 return Process |
48 [clientScript, port.toString()]) | 52 .run(Platform.executable, [clientScript, port.toString()]).then( |
49 .then((ProcessResult result) { | 53 (ProcessResult result) { |
50 if (result.exitCode != 0 || !result.stdout.contains('SUCCESS')) { | 54 if (result.exitCode != 0 || !result.stdout.contains('SUCCESS')) { |
51 print("Client failed"); | 55 print("Client failed"); |
52 print(" stdout:"); | 56 print(" stdout:"); |
53 print(result.stdout); | 57 print(result.stdout); |
54 print(" stderr:"); | 58 print(" stderr:"); |
55 print(result.stderr); | 59 print(result.stderr); |
56 Expect.fail('Client subprocess exit code: ${result.exitCode}'); | 60 Expect.fail('Client subprocess exit code: ${result.exitCode}'); |
57 } | 61 } |
58 }); | 62 }); |
59 } | 63 } |
60 | 64 |
61 runServer().then((server) { | 65 runServer().then((server) { |
62 clientProcess(server.port).then((_) { | 66 clientProcess(server.port).then((_) { |
63 server.close(); | 67 server.close(); |
64 }); | 68 }); |
65 }); | 69 }); |
66 } | 70 } |
OLD | NEW |