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/trusted_certs.pem | 7 // OtherResources=certificates/trusted_certs.pem |
8 | 8 |
9 import "package:expect/expect.dart"; | 9 import "package:expect/expect.dart"; |
10 import "package:path/path.dart"; | 10 import "package:path/path.dart"; |
11 import "package:async_helper/async_helper.dart"; | 11 import "package:async_helper/async_helper.dart"; |
12 | 12 |
13 import "dart:async"; | 13 import "dart:async"; |
14 import "dart:io"; | 14 import "dart:io"; |
15 import "dart:typed_data"; | 15 import "dart:typed_data"; |
16 | 16 |
17 String localFile(path) => Platform.script.resolve(path).toFilePath(); | 17 String localFile(path) => Platform.script.resolve(path).toFilePath(); |
18 | 18 |
19 SecurityContext serverContext = new SecurityContext() | 19 SecurityContext serverContext = new SecurityContext() |
20 ..useCertificateChain(localFile('certificates/server_chain.pem')) | 20 ..useCertificateChain(localFile('certificates/server_chain.pem')) |
21 ..usePrivateKey(localFile('certificates/server_key.pem'), | 21 ..usePrivateKey(localFile('certificates/server_key.pem'), |
22 password: 'dartdart'); | 22 password: 'dartdart'); |
23 | 23 |
24 SecurityContext clientContext = new SecurityContext() | 24 SecurityContext clientContext = new SecurityContext() |
25 ..setTrustedCertificates(localFile('certificates/trusted_certs.pem')); | 25 ..setTrustedCertificates(localFile('certificates/trusted_certs.pem')); |
26 | 26 |
27 // 10 KiB of i%256 data. | 27 // 10 KiB of i%256 data. |
28 Uint8List DATA = new Uint8List.fromList( | 28 Uint8List DATA = |
29 new List.generate(10 * 1024, (i) => i % 256)); | 29 new Uint8List.fromList(new List.generate(10 * 1024, (i) => i % 256)); |
30 | 30 |
31 Future<SecureServerSocket> startServer() { | 31 Future<SecureServerSocket> startServer() { |
32 return SecureServerSocket.bind("localhost", | 32 return SecureServerSocket.bind("localhost", 0, serverContext).then((server) { |
33 0, | |
34 serverContext).then((server) { | |
35 server.listen((SecureSocket request) async { | 33 server.listen((SecureSocket request) async { |
36 await request.drain(); | 34 await request.drain(); |
37 request..add(DATA)..close(); | 35 request |
| 36 ..add(DATA) |
| 37 ..close(); |
38 }); | 38 }); |
39 return server; | 39 return server; |
40 }); | 40 }); |
41 } | 41 } |
42 | 42 |
43 main() async { | 43 main() async { |
44 asyncStart(); | 44 asyncStart(); |
45 var server = await SecureServerSocket.bind("localhost", 0, serverContext); | 45 var server = await SecureServerSocket.bind("localhost", 0, serverContext); |
46 server.listen((SecureSocket request) async { | 46 server.listen((SecureSocket request) async { |
47 await request.drain(); | 47 await request.drain(); |
48 request..add(DATA)..close(); | 48 request |
| 49 ..add(DATA) |
| 50 ..close(); |
49 }); | 51 }); |
50 | 52 |
51 var socket = await RawSecureSocket.connect("localhost", | 53 var socket = await RawSecureSocket.connect("localhost", server.port, |
52 server.port, | 54 context: clientContext); |
53 context: clientContext); | |
54 List<int> body = <int>[]; | 55 List<int> body = <int>[]; |
55 // Close our end, since we're not sending data. | 56 // Close our end, since we're not sending data. |
56 socket.shutdown(SocketDirection.SEND); | 57 socket.shutdown(SocketDirection.SEND); |
57 | 58 |
58 socket.listen((RawSocketEvent event) { | 59 socket.listen((RawSocketEvent event) { |
59 switch (event) { | 60 switch (event) { |
60 case RawSocketEvent.READ: | 61 case RawSocketEvent.READ: |
61 // NOTE: We have a very low prime number here. The internal | 62 // NOTE: We have a very low prime number here. The internal |
62 // ring buffers will not have a size of 3. This means that | 63 // ring buffers will not have a size of 3. This means that |
63 // we'll reach the point where we would like to read 1/2 bytes | 64 // we'll reach the point where we would like to read 1/2 bytes |
64 // at the end and then wrap around and read the next 2/1 bytes. | 65 // at the end and then wrap around and read the next 2/1 bytes. |
65 // [This will ensure we trigger the bug.] | 66 // [This will ensure we trigger the bug.] |
66 body.addAll(socket.read(3)); | 67 body.addAll(socket.read(3)); |
67 break; | 68 break; |
68 case RawSocketEvent.WRITE: | 69 case RawSocketEvent.WRITE: |
69 break; | 70 break; |
70 case RawSocketEvent.READ_CLOSED: | 71 case RawSocketEvent.READ_CLOSED: |
71 break; | 72 break; |
72 default: throw "Unexpected event $event"; | 73 default: |
| 74 throw "Unexpected event $event"; |
73 } | 75 } |
74 }, onError: (e, _) { | 76 }, onError: (e, _) { |
75 Expect.fail('Unexpected error: $e'); | 77 Expect.fail('Unexpected error: $e'); |
76 }, onDone: () { | 78 }, onDone: () { |
77 Expect.equals(body.length, DATA.length); | 79 Expect.equals(body.length, DATA.length); |
78 for (int i = 0; i < body.length; i++) { | 80 for (int i = 0; i < body.length; i++) { |
79 Expect.equals(body[i], DATA[i]); | 81 Expect.equals(body[i], DATA[i]); |
80 } | 82 } |
81 server.close(); | 83 server.close(); |
82 asyncEnd(); | 84 asyncEnd(); |
83 }); | 85 }); |
84 } | 86 } |
OLD | NEW |