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 // This test verifies that the bad certificate callback works in HttpClient. | 5 // This test verifies that the bad certificate callback works in HttpClient. |
6 | 6 |
7 import "dart:async"; | 7 import "dart:async"; |
8 import "dart:io"; | 8 import "dart:io"; |
9 | 9 |
10 import "package:expect/expect.dart"; | 10 import "package:expect/expect.dart"; |
11 | 11 |
12 final HOST_NAME = 'localhost'; | 12 final HOST_NAME = 'localhost'; |
13 | 13 |
14 String localFile(path) => Platform.script.resolve(path).toFilePath(); | 14 String localFile(path) => Platform.script.resolve(path).toFilePath(); |
15 List<int> readLocalFile(path) => (new File(localFile(path))).readAsBytesSync(); | |
16 | 15 |
17 SecurityContext serverContext = new SecurityContext() | 16 SecurityContext serverContext = new SecurityContext() |
18 ..useCertificateChainBytes(readLocalFile('certificates/server_chain.pem')) | 17 ..useCertificateChainSync(localFile('certificates/server_chain.pem')) |
19 ..usePrivateKeyBytes(readLocalFile('certificates/server_key.pem'), | 18 ..usePrivateKeySync(localFile('certificates/server_key.pem'), |
20 password: 'dartdart'); | 19 password: 'dartdart'); |
21 | 20 |
22 class CustomException {} | 21 class CustomException {} |
23 | 22 |
24 main() async { | 23 main() async { |
25 var HOST = (await InternetAddress.lookup(HOST_NAME)).first; | 24 var HOST = (await InternetAddress.lookup(HOST_NAME)).first; |
26 var server = await HttpServer.bindSecure(HOST, 0, serverContext, backlog: 5); | 25 var server = await HttpServer.bindSecure(HOST, 0, serverContext, backlog: 5); |
27 server.listen((request) { | 26 server.listen((request) { |
28 request.listen((_) { | 27 request.listen((_) { |
29 }, onDone: () { | 28 }, onDone: () { |
30 request.response.close(); | 29 request.response.close(); |
31 }); | 30 }); |
32 }); | 31 }); |
33 | 32 |
34 SecurityContext goodContext = new SecurityContext() | 33 SecurityContext goodContext = new SecurityContext() |
35 ..setTrustedCertificates(file: localFile('certificates/trusted_certs.pem')); | 34 ..setTrustedCertificatesSync(localFile('certificates/trusted_certs.pem')); |
36 SecurityContext badContext = new SecurityContext(); | 35 SecurityContext badContext = new SecurityContext(); |
37 SecurityContext defaultContext = SecurityContext.defaultContext; | 36 SecurityContext defaultContext = SecurityContext.defaultContext; |
38 | 37 |
39 await runClient(server.port, goodContext, true, 'pass'); | 38 await runClient(server.port, goodContext, true, 'pass'); |
40 await runClient(server.port, goodContext, false, 'pass'); | 39 await runClient(server.port, goodContext, false, 'pass'); |
41 await runClient(server.port, goodContext, 'fisk', 'pass'); | 40 await runClient(server.port, goodContext, 'fisk', 'pass'); |
42 await runClient(server.port, goodContext, 'exception', 'pass'); | 41 await runClient(server.port, goodContext, 'exception', 'pass'); |
43 await runClient(server.port, badContext, true, 'pass'); | 42 await runClient(server.port, badContext, true, 'pass'); |
44 await runClient(server.port, badContext, false, 'fail'); | 43 await runClient(server.port, badContext, false, 'fail'); |
45 await runClient(server.port, badContext, 'fisk', 'fail'); | 44 await runClient(server.port, badContext, 'fisk', 'fail'); |
(...skipping 27 matching lines...) Expand all Loading... |
73 Expect.notEquals(result, 'pass'); | 72 Expect.notEquals(result, 'pass'); |
74 if (result == 'fail') { | 73 if (result == 'fail') { |
75 Expect.isTrue(error is HandshakeException); | 74 Expect.isTrue(error is HandshakeException); |
76 } else if (result == 'throw') { | 75 } else if (result == 'throw') { |
77 Expect.isTrue(error is CustomException); | 76 Expect.isTrue(error is CustomException); |
78 } else { | 77 } else { |
79 Expect.fail('Unknown expectation $result'); | 78 Expect.fail('Unknown expectation $result'); |
80 } | 79 } |
81 } | 80 } |
82 } | 81 } |
OLD | NEW |