OLD | NEW |
1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2015, 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:io"; | 6 import "dart:io"; |
7 | 7 |
8 String localFile(path) => Platform.script.resolve(path).toFilePath(); | 8 String localFile(path) => Platform.script.resolve(path).toFilePath(); |
9 | 9 |
10 bool printException(e) { print(e); return true; } | 10 bool printException(e) { print(e); return true; } |
11 bool argumentError(e) => e is ArgumentError; | 11 bool argumentError(e) => e is ArgumentError; |
12 bool argumentOrTypeError(e) => e is ArgumentError || e is TypeError; | 12 bool argumentOrTypeError(e) => e is ArgumentError || e is TypeError; |
13 bool fileSystemException(e) => e is FileSystemException; | 13 bool fileSystemException(e) => e is FileSystemException; |
14 bool tlsException(e) => e is TlsException; | 14 bool tlsException(e) => e is TlsException; |
15 | 15 |
16 void testUsePrivateKeyArguments() { | 16 void testUsePrivateKeyArguments() { |
17 var c = new SecurityContext(); | 17 var c = new SecurityContext(); |
18 c.useCertificateChainSync(localFile('certificates/server_chain.pem')); | 18 c.useCertificateChainSync(localFile('certificates/server_chain.pem')); |
19 Expect.throws(() => c.usePrivateKeySync( | 19 |
20 localFile('certificates/server_key.pem'), password: "dart" * 1000), | 20 // Wrong password. |
21 argumentError); | |
22 Expect.throws(() => c.usePrivateKeySync( | 21 Expect.throws(() => c.usePrivateKeySync( |
23 localFile('certificates/server_key.pem')), | 22 localFile('certificates/server_key.pem')), |
24 tlsException); | 23 tlsException); |
25 Expect.throws(() => c.usePrivateKeySync( | 24 Expect.throws(() => c.usePrivateKeySync( |
26 localFile('certificates/server_key.pem'), password: "iHackSites"), | 25 localFile('certificates/server_key.pem'), password: "iHackSites"), |
27 tlsException); | 26 tlsException); |
28 Expect.throws(() => c.usePrivateKeySync( | 27 Expect.throws(() => c.usePrivateKeySync( |
| 28 localFile('certificates/server_key.p12')), |
| 29 tlsException); |
| 30 Expect.throws(() => c.usePrivateKeySync( |
| 31 localFile('certificates/server_key.p12'), password: "iHackSites"), |
| 32 tlsException); |
| 33 Expect.throws(() => c.setTrustedCertificatesSync( |
| 34 localFile('certificates/server_key.p12')), |
| 35 tlsException); |
| 36 Expect.throws(() => c.setTrustedCertificatesSync( |
| 37 localFile('certificates/server_key.p12'), password: "iHackSites"), |
| 38 tlsException); |
| 39 Expect.throws(() => c.useCertificateChainSync( |
| 40 localFile('certificates/server_key.p12')), |
| 41 tlsException); |
| 42 Expect.throws(() => c.useCertificateChainSync( |
| 43 localFile('certificates/server_key.p12'), password: "iHackSites"), |
| 44 tlsException); |
| 45 Expect.throws(() => c.setClientAuthoritiesSync( |
| 46 localFile('certificates/server_key.p12')), |
| 47 argumentError); |
| 48 Expect.throws(() => c.setClientAuthoritiesSync( |
| 49 localFile('certificates/server_key.p12'), password: "iHackSites"), |
| 50 argumentError); |
| 51 |
| 52 // File does not exist |
| 53 Expect.throws(() => c.usePrivateKeySync( |
29 localFile('certificates/server_key_oops.pem'), | 54 localFile('certificates/server_key_oops.pem'), |
30 password: "dartdart"), | 55 password: "dartdart"), |
31 fileSystemException); | 56 fileSystemException); |
| 57 |
| 58 // Wrong type for file name or data |
32 Expect.throws(() => c.usePrivateKeySync(1), argumentOrTypeError); | 59 Expect.throws(() => c.usePrivateKeySync(1), argumentOrTypeError); |
33 Expect.throws(() => c.usePrivateKeySync(null), argumentError); | 60 Expect.throws(() => c.usePrivateKeySync(null), argumentError); |
| 61 Expect.throws(() => c.usePrivateKeyBytes(1), argumentOrTypeError); |
| 62 Expect.throws(() => c.usePrivateKeyBytes(null), argumentError); |
| 63 |
| 64 // Too-long passwords. |
34 Expect.throws(() => c.usePrivateKeySync( | 65 Expect.throws(() => c.usePrivateKeySync( |
| 66 localFile('certificates/server_key.pem'), password: "dart" * 1000), |
| 67 argumentError); |
| 68 Expect.throws(() => c.usePrivateKeySync( |
| 69 localFile('certificates/server_key.p12'), password: "dart" * 1000), |
| 70 argumentOrTypeError); |
| 71 Expect.throws(() => c.setTrustedCertificatesSync( |
| 72 localFile('certificates/server_key.p12'), password: "dart" * 1000), |
| 73 argumentOrTypeError); |
| 74 Expect.throws(() => c.useCertificateChainSync( |
| 75 localFile('certificates/server_key.p12'), password: "dart" * 1000), |
| 76 argumentOrTypeError); |
| 77 Expect.throws(() => c.setClientAuthoritiesSync( |
| 78 localFile('certificates/server_key.p12'), password: "dart" * 1000), |
| 79 argumentOrTypeError); |
| 80 |
| 81 // Bad password type. |
| 82 Expect.throws(() => c.usePrivateKeySync( |
| 83 localFile('certificates/server_key.pem'), password: 3), |
| 84 argumentOrTypeError); |
| 85 Expect.throws(() => c.setTrustedCertificatesBytes( |
| 86 localFile('certificates/server_key.pem'), password: 3), |
| 87 argumentOrTypeError); |
| 88 Expect.throws(() => c.useCertificateChainBytes( |
| 89 localFile('certificates/server_key.pem'), password: 3), |
| 90 argumentOrTypeError); |
| 91 Expect.throws(() => c.setClientAuthoritiesBytes( |
35 localFile('certificates/server_key.pem'), password: 3), | 92 localFile('certificates/server_key.pem'), password: 3), |
36 argumentOrTypeError); | 93 argumentOrTypeError); |
37 | 94 |
38 // Empty data. | 95 // Empty data. |
39 Expect.throws(() => c.usePrivateKeyBytes([], password: 'dartdart'), | 96 Expect.throws(() => c.usePrivateKeyBytes([], password: 'dartdart'), |
40 tlsException); | 97 tlsException); |
41 Expect.throws(() => c.setTrustedCertificatesBytes([]), tlsException); | 98 Expect.throws(() => c.setTrustedCertificatesBytes([]), tlsException); |
42 Expect.throws(() => c.useCertificateChainBytes([]), tlsException); | 99 Expect.throws(() => c.useCertificateChainBytes([]), tlsException); |
43 Expect.throws(() => c.setClientAuthoritiesBytes([]), argumentError); | 100 Expect.throws(() => c.setClientAuthoritiesBytes([]), argumentError); |
44 | 101 |
(...skipping 15 matching lines...) Expand all Loading... |
60 localFile('certificates/client_authority_malformed.pem')), | 117 localFile('certificates/client_authority_malformed.pem')), |
61 argumentError); | 118 argumentError); |
62 | 119 |
63 c.usePrivateKeySync( | 120 c.usePrivateKeySync( |
64 localFile('certificates/server_key.pem'), password: "dartdart"); | 121 localFile('certificates/server_key.pem'), password: "dartdart"); |
65 } | 122 } |
66 | 123 |
67 void main() { | 124 void main() { |
68 testUsePrivateKeyArguments(); | 125 testUsePrivateKeyArguments(); |
69 } | 126 } |
OLD | NEW |