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 "dart:async"; | 6 import "dart:async"; |
7 import "dart:io"; | 7 import "dart:io"; |
8 import "dart:isolate"; | 8 import "dart:isolate"; |
9 | 9 |
10 const SERVER_ADDRESS = "127.0.0.1"; | |
11 const HOST_NAME = "localhost"; | 10 const HOST_NAME = "localhost"; |
12 const CERTIFICATE = "localhost_cert"; | 11 const CERTIFICATE = "localhost_cert"; |
13 | 12 |
14 void testClientCertificate() { | 13 void testClientCertificate() { |
15 ReceivePort port = new ReceivePort(); | 14 ReceivePort port = new ReceivePort(); |
16 SecureServerSocket.bind(SERVER_ADDRESS, | 15 SecureServerSocket.bind(HOST_NAME, |
17 0, | 16 0, |
18 5, | 17 5, |
19 CERTIFICATE, | 18 CERTIFICATE, |
20 requestClientCertificate: true).then((server) { | 19 requestClientCertificate: true).then((server) { |
21 var clientEndFuture = SecureSocket.connect(HOST_NAME, | 20 var clientEndFuture = SecureSocket.connect(HOST_NAME, |
22 server.port, | 21 server.port, |
23 sendClientCertificate: true); | 22 sendClientCertificate: true); |
24 server.listen((serverEnd) { | 23 server.listen((serverEnd) { |
25 X509Certificate certificate = serverEnd.peerCertificate; | 24 X509Certificate certificate = serverEnd.peerCertificate; |
26 Expect.isNotNull(certificate); | 25 Expect.isNotNull(certificate); |
27 Expect.equals("CN=localhost", certificate.subject); | 26 Expect.equals("CN=localhost", certificate.subject); |
28 Expect.equals("CN=myauthority", certificate.issuer); | 27 Expect.equals("CN=myauthority", certificate.issuer); |
29 clientEndFuture.then((clientEnd) { | 28 clientEndFuture.then((clientEnd) { |
30 X509Certificate certificate = clientEnd.peerCertificate; | 29 X509Certificate certificate = clientEnd.peerCertificate; |
31 Expect.isNotNull(certificate); | 30 Expect.isNotNull(certificate); |
32 Expect.equals("CN=localhost", certificate.subject); | 31 Expect.equals("CN=localhost", certificate.subject); |
33 Expect.equals("CN=myauthority", certificate.issuer); | 32 Expect.equals("CN=myauthority", certificate.issuer); |
34 clientEnd.close(); | 33 clientEnd.close(); |
35 serverEnd.close(); | 34 serverEnd.close(); |
36 server.close(); | 35 server.close(); |
37 port.close(); | 36 port.close(); |
38 }); | 37 }); |
39 }); | 38 }); |
40 }); | 39 }); |
41 } | 40 } |
42 | 41 |
43 void testRequiredClientCertificate() { | 42 void testRequiredClientCertificate() { |
44 ReceivePort port = new ReceivePort(); | 43 ReceivePort port = new ReceivePort(); |
45 SecureServerSocket.bind(SERVER_ADDRESS, | 44 SecureServerSocket.bind(HOST_NAME, |
46 0, | 45 0, |
47 5, | 46 5, |
48 CERTIFICATE, | 47 CERTIFICATE, |
49 requireClientCertificate: true).then((server) { | 48 requireClientCertificate: true).then((server) { |
50 var clientEndFuture = SecureSocket.connect(HOST_NAME, | 49 var clientEndFuture = SecureSocket.connect(HOST_NAME, |
51 server.port, | 50 server.port, |
52 sendClientCertificate: true); | 51 sendClientCertificate: true); |
53 server.listen((serverEnd) { | 52 server.listen((serverEnd) { |
54 X509Certificate certificate = serverEnd.peerCertificate; | 53 X509Certificate certificate = serverEnd.peerCertificate; |
55 Expect.isNotNull(certificate); | 54 Expect.isNotNull(certificate); |
56 Expect.equals("CN=localhost", certificate.subject); | 55 Expect.equals("CN=localhost", certificate.subject); |
57 Expect.equals("CN=myauthority", certificate.issuer); | 56 Expect.equals("CN=myauthority", certificate.issuer); |
58 clientEndFuture.then((clientEnd) { | 57 clientEndFuture.then((clientEnd) { |
59 X509Certificate certificate = clientEnd.peerCertificate; | 58 X509Certificate certificate = clientEnd.peerCertificate; |
60 Expect.isNotNull(certificate); | 59 Expect.isNotNull(certificate); |
61 Expect.equals("CN=localhost", certificate.subject); | 60 Expect.equals("CN=localhost", certificate.subject); |
62 Expect.equals("CN=myauthority", certificate.issuer); | 61 Expect.equals("CN=myauthority", certificate.issuer); |
63 clientEnd.close(); | 62 clientEnd.close(); |
64 serverEnd.close(); | 63 serverEnd.close(); |
65 server.close(); | 64 server.close(); |
66 port.close(); | 65 port.close(); |
67 }); | 66 }); |
68 }); | 67 }); |
69 }); | 68 }); |
70 } | 69 } |
71 | 70 |
72 void testNoClientCertificate() { | 71 void testNoClientCertificate() { |
73 ReceivePort port = new ReceivePort(); | 72 ReceivePort port = new ReceivePort(); |
74 SecureServerSocket.bind(SERVER_ADDRESS, | 73 SecureServerSocket.bind(HOST_NAME, |
75 0, | 74 0, |
76 5, | 75 5, |
77 CERTIFICATE, | 76 CERTIFICATE, |
78 requestClientCertificate: true).then((server) { | 77 requestClientCertificate: true).then((server) { |
79 var clientEndFuture = SecureSocket.connect(HOST_NAME, | 78 var clientEndFuture = SecureSocket.connect(HOST_NAME, |
80 server.port); | 79 server.port); |
81 server.listen((serverEnd) { | 80 server.listen((serverEnd) { |
82 X509Certificate certificate = serverEnd.peerCertificate; | 81 X509Certificate certificate = serverEnd.peerCertificate; |
83 Expect.isNull(certificate); | 82 Expect.isNull(certificate); |
84 clientEndFuture.then((clientEnd) { | 83 clientEndFuture.then((clientEnd) { |
85 clientEnd.close(); | 84 clientEnd.close(); |
86 serverEnd.close(); | 85 serverEnd.close(); |
87 server.close(); | 86 server.close(); |
88 port.close(); | 87 port.close(); |
89 }); | 88 }); |
90 }); | 89 }); |
91 }); | 90 }); |
92 } | 91 } |
93 | 92 |
94 void testNoRequiredClientCertificate() { | 93 void testNoRequiredClientCertificate() { |
95 ReceivePort port = new ReceivePort(); | 94 ReceivePort port = new ReceivePort(); |
96 bool clientError = false; | 95 bool clientError = false; |
97 SecureServerSocket.bind(SERVER_ADDRESS, | 96 SecureServerSocket.bind(HOST_NAME, |
98 0, | 97 0, |
99 5, | 98 5, |
100 CERTIFICATE, | 99 CERTIFICATE, |
101 requireClientCertificate: true).then((server) { | 100 requireClientCertificate: true).then((server) { |
102 Future clientDone = SecureSocket.connect(HOST_NAME, server.port) | 101 Future clientDone = SecureSocket.connect(HOST_NAME, server.port) |
103 .catchError((e) { clientError = true; }); | 102 .catchError((e) { clientError = true; }); |
104 server.listen((serverEnd) { | 103 server.listen((serverEnd) { |
105 Expect.fail("Got a unverifiable connection"); | 104 Expect.fail("Got a unverifiable connection"); |
106 }, | 105 }, |
107 onError: (e) { | 106 onError: (e) { |
(...skipping 11 matching lines...) Expand all Loading... |
119 Path certificateDatabase = scriptDir.append('pkcert'); | 118 Path certificateDatabase = scriptDir.append('pkcert'); |
120 SecureSocket.initialize(database: certificateDatabase.toNativePath(), | 119 SecureSocket.initialize(database: certificateDatabase.toNativePath(), |
121 password: 'dartdart', | 120 password: 'dartdart', |
122 useBuiltinRoots: false); | 121 useBuiltinRoots: false); |
123 | 122 |
124 testClientCertificate(); | 123 testClientCertificate(); |
125 testRequiredClientCertificate(); | 124 testRequiredClientCertificate(); |
126 testNoClientCertificate(); | 125 testNoClientCertificate(); |
127 testNoRequiredClientCertificate(); | 126 testNoRequiredClientCertificate(); |
128 } | 127 } |
OLD | NEW |