OLD | NEW |
1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2015, 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:convert'; | 6 import 'dart:convert'; |
7 | 7 |
8 import 'package:expect/expect.dart'; | 8 import 'package:expect/expect.dart'; |
9 import 'package:async_helper/async_helper.dart'; | 9 import 'package:async_helper/async_helper.dart'; |
10 | 10 |
11 const String NAME_LENGTH_ERROR = | 11 const String NAME_LENGTH_ERROR = |
12 'Length of protocol must be between 1 and 255'; | 12 'Length of protocol must be between 1 and 255'; |
13 | 13 |
14 const String MESSAGE_LENGTH_ERROR = | 14 const String MESSAGE_LENGTH_ERROR = |
15 'The maximum message length supported is 2^13-1'; | 15 'The maximum message length supported is 2^13-1'; |
16 | 16 |
17 String localFile(path) => Platform.script.resolve(path).toFilePath(); | 17 String localFile(path) => Platform.script.resolve(path).toFilePath(); |
| 18 List<int> readLocalFile(path) => (new File(localFile(path))).readAsBytesSync(); |
18 | 19 |
19 SecurityContext clientContext() => new SecurityContext() | 20 SecurityContext clientContext() => new SecurityContext() |
20 ..setTrustedCertificates(file: localFile('certificates/trusted_certs.pem')); | 21 ..setTrustedCertificates(file: localFile('certificates/trusted_certs.pem')); |
21 | 22 |
22 SecurityContext serverContext() => new SecurityContext() | 23 SecurityContext serverContext() => new SecurityContext() |
23 ..useCertificateChain(localFile('certificates/server_chain.pem')) | 24 ..useCertificateChain(localFile('certificates/server_chain.pem')) |
24 ..usePrivateKey(localFile('certificates/server_key.pem'), | 25 ..usePrivateKeyAsBytes(readLocalFile('certificates/server_key.pem'), |
25 password: 'dartdart'); | 26 password: 'dartdart'); |
26 | 27 |
27 // Tests that client/server with same protocol can securely establish a | 28 // Tests that client/server with same protocol can securely establish a |
28 // connection, negotiate the protocol and can send data to each other. | 29 // connection, negotiate the protocol and can send data to each other. |
29 void testSuccessfulAlpnNegotiationConnection(List<String> clientProtocols, | 30 void testSuccessfulAlpnNegotiationConnection(List<String> clientProtocols, |
30 List<String> serverProtocols, | 31 List<String> serverProtocols, |
31 String selectedProtocol) { | 32 String selectedProtocol) { |
32 asyncStart(); | 33 asyncStart(); |
33 var sContext = serverContext()..setAlpnProtocols(serverProtocols, true); | 34 var sContext = serverContext()..setAlpnProtocols(serverProtocols, true); |
34 SecureServerSocket.bind('localhost', 0, sContext) | 35 SecureServerSocket.bind('localhost', 0, sContext) |
35 .then((SecureServerSocket server) { | 36 .then((SecureServerSocket server) { |
(...skipping 24 matching lines...) Expand all Loading... |
60 }); | 61 }); |
61 } | 62 } |
62 | 63 |
63 void testInvalidArgument(List<String> protocols, String errorIncludes) { | 64 void testInvalidArgument(List<String> protocols, String errorIncludes) { |
64 testInvalidArgumentServerContext(protocols, errorIncludes); | 65 testInvalidArgumentServerContext(protocols, errorIncludes); |
65 testInvalidArgumentClientContext(protocols, errorIncludes); | 66 testInvalidArgumentClientContext(protocols, errorIncludes); |
66 testInvalidArgumentClientConnect(protocols, errorIncludes); | 67 testInvalidArgumentClientConnect(protocols, errorIncludes); |
67 } | 68 } |
68 | 69 |
69 void testInvalidArgumentServerContext(List<String> protocols, | 70 void testInvalidArgumentServerContext(List<String> protocols, |
70 String errorIncludes) { | 71 String errorIncludes) { |
71 Expect.throws(() => serverContext().setAlpnProtocols(protocols, true), (e) { | 72 Expect.throws(() => serverContext().setAlpnProtocols(protocols, true), (e) { |
72 Expect.isTrue(e is ArgumentError); | 73 Expect.isTrue(e is ArgumentError); |
73 Expect.isTrue(e.toString().contains(errorIncludes)); | 74 Expect.isTrue(e.toString().contains(errorIncludes)); |
74 return true; | 75 return true; |
75 }); | 76 }); |
76 } | 77 } |
77 | 78 |
78 void testInvalidArgumentClientContext(List<String> protocols, | 79 void testInvalidArgumentClientContext(List<String> protocols, |
79 String errorIncludes) { | 80 String errorIncludes) { |
80 Expect.throws(() => clientContext().setAlpnProtocols(protocols, false), (e) { | 81 Expect.throws(() => clientContext().setAlpnProtocols(protocols, false), (e) { |
81 Expect.isTrue(e is ArgumentError); | 82 Expect.isTrue(e is ArgumentError); |
82 Expect.isTrue(e.toString().contains(errorIncludes)); | 83 Expect.isTrue(e.toString().contains(errorIncludes)); |
83 return true; | 84 return true; |
84 }); | 85 }); |
85 } | 86 } |
86 | 87 |
87 void testInvalidArgumentClientConnect(List<String> protocols, | 88 void testInvalidArgumentClientConnect(List<String> protocols, |
88 String errorIncludes) { | 89 String errorIncludes) { |
89 asyncStart(); | 90 asyncStart(); |
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
195 // Issue https://github.com/dart-lang/sdk/issues/23580 | 196 // Issue https://github.com/dart-lang/sdk/issues/23580 |
196 // Chromium issue https://code.google.com/p/chromium/issues/detail?id=497770 | 197 // Chromium issue https://code.google.com/p/chromium/issues/detail?id=497770 |
197 testSuccessfulAlpnNegotiationConnection(['a'], ['b'], null); | 198 testSuccessfulAlpnNegotiationConnection(['a'], ['b'], null); |
198 | 199 |
199 // Test too short / too long protocol names. | 200 // Test too short / too long protocol names. |
200 testInvalidArgument([longname256], NAME_LENGTH_ERROR); | 201 testInvalidArgument([longname256], NAME_LENGTH_ERROR); |
201 testInvalidArgument([strangelongname256], NAME_LENGTH_ERROR); | 202 testInvalidArgument([strangelongname256], NAME_LENGTH_ERROR); |
202 testInvalidArgument([''], NAME_LENGTH_ERROR); | 203 testInvalidArgument([''], NAME_LENGTH_ERROR); |
203 testInvalidArgument(tooManyProtocols, MESSAGE_LENGTH_ERROR); | 204 testInvalidArgument(tooManyProtocols, MESSAGE_LENGTH_ERROR); |
204 } | 205 } |
OLD | NEW |