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