OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2014, 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:io'; |
| 6 import 'dart:convert'; |
| 7 |
| 8 import 'package:expect/expect.dart'; |
| 9 import 'package:async_helper/async_helper.dart'; |
| 10 |
| 11 const String MAX_LEN_ERROR = |
| 12 'Length of protocol must be between 1 and 255'; |
| 13 |
| 14 const String MAX_MSG_LEN_ERROR = |
| 15 'The maximum message length supported is 2^15-1'; |
| 16 |
| 17 void InitializeSSL() { |
| 18 var testPkcertDatabase = Platform.script.resolve('pkcert').toFilePath(); |
| 19 SecureSocket.initialize(database: testPkcertDatabase, |
| 20 password: 'dartdart'); |
| 21 } |
| 22 |
| 23 // Tests that client/server with same protocol can securly establish a |
| 24 // connection, negogiate the protocol and can send data to each other. |
| 25 void testSuccessfulAlpnNegogiationConnection(List<String> clientProtocols, |
| 26 List<String> serverProtocols, |
| 27 String selectedProtocol) { |
| 28 asyncStart(); |
| 29 SecureServerSocket.bind('localhost', 0, 'localhost_cert', |
| 30 supportedProtocols: serverProtocols).then((SecureServerSocket server) { |
| 31 |
| 32 asyncStart(); |
| 33 server.first.then((SecureSocket socket) { |
| 34 Expect.equals(selectedProtocol, socket.selectedProtocol); |
| 35 socket..write('server message')..close(); |
| 36 socket.transform(ASCII.decoder).join('').then((String s) { |
| 37 Expect.equals('client message', s); |
| 38 asyncEnd(); |
| 39 }); |
| 40 }); |
| 41 |
| 42 asyncStart(); |
| 43 SecureSocket.connect('localhost', server.port, |
| 44 supportedProtocols: clientProtocols).then((socket) { |
| 45 Expect.equals(selectedProtocol, socket.selectedProtocol); |
| 46 socket..write('client message')..close(); |
| 47 socket.transform(ASCII.decoder).join('').then((String s) { |
| 48 Expect.equals('server message', s); |
| 49 server.close(); |
| 50 asyncEnd(); |
| 51 }); |
| 52 }); |
| 53 |
| 54 asyncEnd(); |
| 55 }); |
| 56 } |
| 57 |
| 58 void testFailedAlpnNegogiationConnection(List<String> clientProtocols, |
| 59 List<String> serverProtocols) { |
| 60 asyncStart(); |
| 61 SecureServerSocket.bind('localhost', 0, 'localhost_cert', |
| 62 supportedProtocols: serverProtocols).then((SecureServerSocket server) { |
| 63 |
| 64 asyncStart(); |
| 65 server.first.catchError((error, stack) { |
| 66 Expect.isTrue(error is HandshakeException); |
| 67 asyncEnd(); |
| 68 }); |
| 69 |
| 70 asyncStart(); |
| 71 SecureSocket.connect('localhost', |
| 72 server.port, |
| 73 supportedProtocols: clientProtocols) |
| 74 .catchError((error, stack) { |
| 75 Expect.isTrue(error is HandshakeException); |
| 76 asyncEnd(); |
| 77 }); |
| 78 |
| 79 asyncEnd(); |
| 80 }); |
| 81 } |
| 82 |
| 83 void testInvalidArgumentsLongName(List<String> protocols, |
| 84 bool isLenError, |
| 85 bool isMsgLenError) { |
| 86 asyncStart(); |
| 87 SecureServerSocket.bind('localhost', 0, 'localhost_cert', |
| 88 supportedProtocols: protocols).then((SecureServerSocket server) { |
| 89 |
| 90 asyncStart(); |
| 91 server.first.catchError((error, stack) { |
| 92 String errorString = '${(error as ArgumentError)}'; |
| 93 if (isLenError) { |
| 94 Expect.isTrue(errorString.contains(MAX_LEN_ERROR)); |
| 95 } else if (isMsgLenError) { |
| 96 Expect.isTrue(errorString.contains(MAX_MSG_LEN_ERROR)); |
| 97 } else { |
| 98 throw 'unreachable'; |
| 99 } |
| 100 asyncEnd(); |
| 101 }); |
| 102 |
| 103 asyncStart(); |
| 104 SecureSocket.connect('localhost', |
| 105 server.port, |
| 106 supportedProtocols: protocols) |
| 107 .catchError((error, stack) { |
| 108 String errorString = '${(error as ArgumentError)}'; |
| 109 if (isLenError) { |
| 110 Expect.isTrue(errorString.contains(MAX_LEN_ERROR)); |
| 111 } else if (isMsgLenError) { |
| 112 Expect.isTrue(errorString.contains(MAX_MSG_LEN_ERROR)); |
| 113 } else { |
| 114 throw 'unreachable'; |
| 115 } |
| 116 asyncEnd(); |
| 117 }); |
| 118 |
| 119 asyncEnd(); |
| 120 }); |
| 121 } |
| 122 |
| 123 main() { |
| 124 InitializeSSL(); |
| 125 final longname256 = 'p' * 256; |
| 126 final String longname255 = 'p' * 255; |
| 127 final String strangelongname255 = 'ø' + 'p' * 253; |
| 128 final String strangelongname256 = 'ø' + 'p' * 254; |
| 129 |
| 130 // This produces a message of (1 << 15)-2 bytes (1 length and 1 ascii byte). |
| 131 final List<String> allProtocols = new Iterable.generate( |
| 132 (1 << 14) - 1, (i) => '0').toList(); |
| 133 |
| 134 // This produces a message of (1 << 15) bytes (1 length and 1 ascii byte). |
| 135 final List<String> allProtocolsPlusOne = new Iterable.generate( |
| 136 (1 << 14), (i) => '0').toList(); |
| 137 |
| 138 // Protocols are in order of decreasing priority. First matching protocol |
| 139 // will be taken. |
| 140 |
| 141 // Test successfull negotiation, including priority. |
| 142 testSuccessfulAlpnNegogiationConnection(['a'], |
| 143 ['a'], |
| 144 'a'); |
| 145 |
| 146 testSuccessfulAlpnNegogiationConnection([longname255], |
| 147 [longname255], |
| 148 longname255); |
| 149 |
| 150 testSuccessfulAlpnNegogiationConnection([strangelongname255], |
| 151 [strangelongname255], |
| 152 strangelongname255); |
| 153 |
| 154 testSuccessfulAlpnNegogiationConnection(allProtocols, |
| 155 allProtocols, |
| 156 '0'); |
| 157 |
| 158 testSuccessfulAlpnNegogiationConnection(['a', 'b', 'c'], |
| 159 ['a', 'b', 'c'], |
| 160 'a'); |
| 161 |
| 162 testSuccessfulAlpnNegogiationConnection(['a', 'b', 'c'], |
| 163 ['c'], |
| 164 'c'); |
| 165 |
| 166 // Server precedence. |
| 167 testSuccessfulAlpnNegogiationConnection(['a', 'b', 'c'], |
| 168 ['c', 'b', 'a'], |
| 169 'a'); |
| 170 |
| 171 testSuccessfulAlpnNegogiationConnection(['c'], |
| 172 ['a', 'b', 'c'], |
| 173 'c'); |
| 174 |
| 175 testSuccessfulAlpnNegogiationConnection(['s1', 'b', 'e1'], |
| 176 ['s2', 'b', 'e2'], |
| 177 'b'); |
| 178 |
| 179 // Test no protocol negotiation support |
| 180 testSuccessfulAlpnNegogiationConnection(null, |
| 181 null, |
| 182 null); |
| 183 |
| 184 testSuccessfulAlpnNegogiationConnection(['a', 'b', 'c'], |
| 185 null, |
| 186 null); |
| 187 |
| 188 testSuccessfulAlpnNegogiationConnection(null, |
| 189 ['a', 'b', 'c'], |
| 190 null); |
| 191 |
| 192 testSuccessfulAlpnNegogiationConnection([], |
| 193 [], |
| 194 null); |
| 195 |
| 196 testSuccessfulAlpnNegogiationConnection(['a', 'b', 'c'], |
| 197 [], |
| 198 null); |
| 199 |
| 200 testSuccessfulAlpnNegogiationConnection([], |
| 201 ['a', 'b', 'c'], |
| 202 null); |
| 203 |
| 204 // Test non-overlapping protocols. |
| 205 testFailedAlpnNegogiationConnection(['a'], ['b']); |
| 206 |
| 207 |
| 208 // Test too short / too long protocol names. |
| 209 testInvalidArgumentsLongName([longname256], true, false); |
| 210 testInvalidArgumentsLongName([strangelongname256], true, false); |
| 211 testInvalidArgumentsLongName([''], true, false); |
| 212 testInvalidArgumentsLongName(allProtocolsPlusOne, false, true); |
| 213 testInvalidArgumentsLongName(allProtocolsPlusOne, false, true); |
| 214 } |
OLD | NEW |