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