| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, 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:io"; | 5 import "dart:io"; |
| 6 import "dart:async"; | 6 import "dart:async"; |
| 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"; | |
| 11 | 10 |
| 12 void main(List<String> args) { | 11 Future testGoogleUrl(SecurityContext context, String outcome) async { |
| 13 if (!args.contains('--child')) { | 12 var client = new HttpClient(context: context); |
| 14 runAllTestsInChildProcesses(); | 13 // We need to use an external server that is backed by a |
| 15 } else { | 14 // built-in root certificate authority. |
| 16 InitializeSSL(useDatabase: args.contains('--database'), | 15 try { |
| 17 useBuiltinRoots: args.contains('--builtin-roots')); | 16 // First, check if the lookup works. |
| 18 testGoogleUrl(args.contains('--builtin-roots')); | 17 await InternetAddress.lookup('www.google.com'); |
| 18 var request = await client.getUrl(Uri.parse('https://www.google.com')); |
| 19 request.followRedirects = false; |
| 20 var response = await request.close(); |
| 21 Expect.equals('pass', outcome, 'Unexpected successful connection'); |
| 22 try { await response.drain(); } catch (e) { } |
| 23 } on HandshakeException { |
| 24 Expect.equals('fail', outcome, 'Unexpected failed connection'); |
| 25 } on SocketException { |
| 26 // Lookup failed or connection failed. Don't report a failure. |
| 27 } finally { |
| 28 client.close(); |
| 19 } | 29 } |
| 20 } | 30 } |
| 21 | 31 |
| 22 void InitializeSSL({bool useDatabase, bool useBuiltinRoots}) { | 32 |
| 23 // If the built-in root certificates aren't loaded, the connection | 33 main() async { |
| 24 // should signal an error. Even when an external database is loaded, | 34 asyncStart(); |
| 25 // they should not be loaded. | 35 await testGoogleUrl(null, "pass"); |
| 26 if (useDatabase) { | 36 await testGoogleUrl(SecurityContext.defaultContext, "pass"); |
| 27 var certificateDatabase = Platform.script.resolve('pkcert').toFilePath(); | 37 await testGoogleUrl(new SecurityContext(), "fail"); |
| 28 SecureSocket.initialize(database: certificateDatabase, | 38 asyncEnd(); |
| 29 password: 'dartdart', | |
| 30 useBuiltinRoots: useBuiltinRoots); | |
| 31 } else { | |
| 32 SecureSocket.initialize(useBuiltinRoots: useBuiltinRoots); | |
| 33 } | |
| 34 } | 39 } |
| 35 | |
| 36 void testGoogleUrl(bool expectSuccess) { | |
| 37 // We need to use an external server that is backed by a | |
| 38 // built-in root certificate authority. | |
| 39 | |
| 40 // First, check if the lookup fails. If not then run the test. | |
| 41 InternetAddress.lookup('www.google.com').then((_) { | |
| 42 HttpClient client = new HttpClient(); | |
| 43 client.getUrl(Uri.parse('https://www.google.com')) | |
| 44 .then((request) { | |
| 45 request.followRedirects = false; | |
| 46 return request.close(); | |
| 47 }) | |
| 48 .then((response) { | |
| 49 Expect.isTrue(expectSuccess, "Unexpected successful connection"); | |
| 50 print('SUCCESS'); | |
| 51 return response.drain().catchError((_) {}); | |
| 52 }) | |
| 53 .catchError((error) { | |
| 54 // Allow SocketExceptions if www.google.com is unreachable or down. | |
| 55 Expect.isTrue((!expectSuccess && error is HandshakeException) || | |
| 56 error is SocketException); | |
| 57 print('SUCCESS'); | |
| 58 }) | |
| 59 .whenComplete(client.close); | |
| 60 }, | |
| 61 onError: (e) { | |
| 62 // Lookup failed. | |
| 63 Expect.isTrue(e is SocketException); | |
| 64 print('SUCCESS'); | |
| 65 }); | |
| 66 } | |
| 67 | |
| 68 void runAllTestsInChildProcesses() { | |
| 69 Future runChild(List<String> scriptArguments) { | |
| 70 return Process.run(Platform.executable, | |
| 71 []..addAll(Platform.executableArguments) | |
| 72 ..add(Platform.script.toFilePath()) | |
| 73 ..addAll(scriptArguments)) | |
| 74 .then((ProcessResult result) { | |
| 75 if (result.exitCode != 0 || !result.stdout.contains('SUCCESS')) { | |
| 76 print("Client failed"); | |
| 77 print(" stdout:"); | |
| 78 print(result.stdout); | |
| 79 print(" stderr:"); | |
| 80 print(result.stderr); | |
| 81 Expect.fail('Client subprocess exit code: ${result.exitCode}'); | |
| 82 } | |
| 83 }); | |
| 84 } | |
| 85 | |
| 86 asyncStart(); | |
| 87 Future.wait([runChild(['--child']), | |
| 88 runChild(['--child', '--database']), | |
| 89 runChild(['--child', '--builtin-roots']), | |
| 90 runChild(['--child', '--builtin-roots', '--database'])]) | |
| 91 .then((_) => asyncEnd()); | |
| 92 } | |
| OLD | NEW |