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 // VMOptions= | 5 // VMOptions= |
6 // VMOptions=--short_socket_read | 6 // VMOptions=--short_socket_read |
7 // VMOptions=--short_socket_write | 7 // VMOptions=--short_socket_write |
8 // VMOptions=--short_socket_read --short_socket_write | 8 // VMOptions=--short_socket_read --short_socket_write |
9 | 9 |
10 import "package:expect/expect.dart"; | 10 import "package:expect/expect.dart"; |
11 import "package:path/path.dart"; | 11 import "package:path/path.dart"; |
12 import "dart:async"; | 12 import "dart:async"; |
13 import "dart:io"; | 13 import "dart:io"; |
14 | 14 |
| 15 String localFile(path) => Platform.script.resolve(path).toFilePath(); |
| 16 |
| 17 SecurityContext serverContext = new SecurityContext() |
| 18 ..useCertificateChain(localFile('certificates/server_chain.pem')) |
| 19 ..usePrivateKey(localFile('certificates/server_key.pem'), |
| 20 password: 'dartdart'); |
| 21 |
| 22 SecurityContext clientContext = new SecurityContext() |
| 23 ..setTrustedCertificates(file: localFile('certificates/trusted_certs.pem')); |
| 24 |
15 Future<HttpServer> startServer() { | 25 Future<HttpServer> startServer() { |
16 return HttpServer.bindSecure( | 26 return HttpServer.bindSecure( |
17 "localhost", | 27 "localhost", |
18 0, | 28 0, |
19 backlog: 5, | 29 serverContext, |
20 certificateName: 'localhost_cert').then((server) { | 30 backlog: 5).then((server) { |
21 server.listen((HttpRequest request) { | 31 server.listen((HttpRequest request) { |
22 request.listen( | 32 request.listen( |
23 (_) { }, | 33 (_) { }, |
24 onDone: () { | 34 onDone: () { |
25 request.response.contentLength = 100; | 35 request.response.contentLength = 100; |
26 for (int i = 0; i < 10; i++) { | 36 for (int i = 0; i < 10; i++) { |
27 request.response.add([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]); | 37 request.response.add([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]); |
28 } | 38 } |
29 request.response.close(); | 39 request.response.close(); |
30 }); | 40 }); |
31 }); | 41 }); |
32 return server; | 42 return server; |
33 }); | 43 }); |
34 } | 44 } |
35 | 45 |
36 void InitializeSSL() { | |
37 var testPkcertDatabase = Platform.script.resolve('pkcert').toFilePath(); | |
38 SecureSocket.initialize(database: testPkcertDatabase, | |
39 password: 'dartdart'); | |
40 } | |
41 | |
42 void main() { | 46 void main() { |
43 InitializeSSL(); | |
44 List<int> body = <int>[]; | 47 List<int> body = <int>[]; |
45 startServer().then((server) { | 48 startServer().then((server) { |
46 SecureSocket.connect("localhost", server.port).then((socket) { | 49 SecureSocket.connect("localhost", server.port, context: clientContext) |
| 50 .then((socket) { |
47 socket.write("GET / HTTP/1.0\r\nHost: localhost\r\n\r\n"); | 51 socket.write("GET / HTTP/1.0\r\nHost: localhost\r\n\r\n"); |
48 socket.close(); | 52 socket.close(); |
49 socket.listen( | 53 socket.listen( |
50 (List<int> data) { | 54 (List<int> data) { |
51 body.addAll(data); | 55 body.addAll(data); |
52 }, | 56 }, |
53 onDone: () { | 57 onDone: () { |
54 Expect.isTrue(body.length > 100, "$body\n${body.length}"); | 58 Expect.isTrue(body.length > 100, "$body\n${body.length}"); |
55 Expect.equals(72, body[0]); | 59 Expect.equals(72, body[0]); |
56 Expect.equals(9, body[body.length - 1]); | 60 Expect.equals(9, body[body.length - 1]); |
57 server.close(); | 61 server.close(); |
58 }, | 62 }, |
59 onError: (e, trace) { | 63 onError: (e, trace) { |
60 String msg = "Unexpected error $e"; | 64 String msg = "Unexpected error $e"; |
61 if (trace != null) msg += "\nStackTrace: $trace"; | 65 if (trace != null) msg += "\nStackTrace: $trace"; |
62 Expect.fail(msg); | 66 Expect.fail(msg); |
63 }); | 67 }); |
64 }); | 68 }); |
65 }); | 69 }); |
66 } | 70 } |
OLD | NEW |