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.useCertificateChain(localFile('certificates/server_chain.pem')); |
19 | 19 |
20 // Wrong password. | 20 // Wrong password. |
21 Expect.throws(() => c.usePrivateKeySync( | 21 Expect.throws(() => c.usePrivateKey( |
22 localFile('certificates/server_key.pem')), | 22 localFile('certificates/server_key.pem')), |
23 tlsException); | 23 tlsException); |
24 Expect.throws(() => c.usePrivateKeySync( | 24 Expect.throws(() => c.usePrivateKey( |
25 localFile('certificates/server_key.pem'), password: "iHackSites"), | 25 localFile('certificates/server_key.pem'), password: "iHackSites"), |
26 tlsException); | 26 tlsException); |
27 Expect.throws(() => c.usePrivateKeySync( | 27 Expect.throws(() => c.usePrivateKey( |
28 localFile('certificates/server_key.p12')), | 28 localFile('certificates/server_key.p12')), |
29 tlsException); | 29 tlsException); |
30 Expect.throws(() => c.usePrivateKeySync( | 30 Expect.throws(() => c.usePrivateKey( |
31 localFile('certificates/server_key.p12'), password: "iHackSites"), | 31 localFile('certificates/server_key.p12'), password: "iHackSites"), |
32 tlsException); | 32 tlsException); |
33 Expect.throws(() => c.setTrustedCertificatesSync( | 33 Expect.throws(() => c.setTrustedCertificates( |
34 localFile('certificates/server_key.p12')), | 34 localFile('certificates/server_key.p12')), |
35 tlsException); | 35 tlsException); |
36 Expect.throws(() => c.setTrustedCertificatesSync( | 36 Expect.throws(() => c.setTrustedCertificates( |
37 localFile('certificates/server_key.p12'), password: "iHackSites"), | 37 localFile('certificates/server_key.p12'), password: "iHackSites"), |
38 tlsException); | 38 tlsException); |
39 Expect.throws(() => c.useCertificateChainSync( | 39 Expect.throws(() => c.useCertificateChain( |
40 localFile('certificates/server_key.p12')), | 40 localFile('certificates/server_key.p12')), |
41 tlsException); | 41 tlsException); |
42 Expect.throws(() => c.useCertificateChainSync( | 42 Expect.throws(() => c.useCertificateChain( |
43 localFile('certificates/server_key.p12'), password: "iHackSites"), | 43 localFile('certificates/server_key.p12'), password: "iHackSites"), |
44 tlsException); | 44 tlsException); |
45 Expect.throws(() => c.setClientAuthoritiesSync( | 45 Expect.throws(() => c.setClientAuthorities( |
46 localFile('certificates/server_key.p12')), | 46 localFile('certificates/server_key.p12')), |
47 argumentError); | 47 argumentError); |
48 Expect.throws(() => c.setClientAuthoritiesSync( | 48 Expect.throws(() => c.setClientAuthorities( |
49 localFile('certificates/server_key.p12'), password: "iHackSites"), | 49 localFile('certificates/server_key.p12'), password: "iHackSites"), |
50 argumentError); | 50 argumentError); |
51 | 51 |
52 // File does not exist | 52 // File does not exist |
53 Expect.throws(() => c.usePrivateKeySync( | 53 Expect.throws(() => c.usePrivateKey( |
54 localFile('certificates/server_key_oops.pem'), | 54 localFile('certificates/server_key_oops.pem'), |
55 password: "dartdart"), | 55 password: "dartdart"), |
56 fileSystemException); | 56 fileSystemException); |
57 | 57 |
58 // Wrong type for file name or data | 58 // Wrong type for file name or data |
59 Expect.throws(() => c.usePrivateKeySync(1), argumentOrTypeError); | 59 Expect.throws(() => c.usePrivateKey(1), argumentOrTypeError); |
60 Expect.throws(() => c.usePrivateKeySync(null), argumentError); | 60 Expect.throws(() => c.usePrivateKey(null), argumentError); |
61 Expect.throws(() => c.usePrivateKeyBytes(1), argumentOrTypeError); | 61 Expect.throws(() => c.usePrivateKeyBytes(1), argumentOrTypeError); |
62 Expect.throws(() => c.usePrivateKeyBytes(null), argumentError); | 62 Expect.throws(() => c.usePrivateKeyBytes(null), argumentError); |
63 | 63 |
64 // Too-long passwords. | 64 // Too-long passwords. |
65 Expect.throws(() => c.usePrivateKeySync( | 65 Expect.throws(() => c.usePrivateKey( |
66 localFile('certificates/server_key.pem'), password: "dart" * 1000), | 66 localFile('certificates/server_key.pem'), password: "dart" * 1000), |
67 argumentError); | 67 argumentError); |
68 Expect.throws(() => c.usePrivateKeySync( | 68 Expect.throws(() => c.usePrivateKey( |
69 localFile('certificates/server_key.p12'), password: "dart" * 1000), | 69 localFile('certificates/server_key.p12'), password: "dart" * 1000), |
70 argumentOrTypeError); | 70 argumentOrTypeError); |
71 Expect.throws(() => c.setTrustedCertificatesSync( | 71 Expect.throws(() => c.setTrustedCertificates( |
72 localFile('certificates/server_key.p12'), password: "dart" * 1000), | 72 localFile('certificates/server_key.p12'), password: "dart" * 1000), |
73 argumentOrTypeError); | 73 argumentOrTypeError); |
74 Expect.throws(() => c.useCertificateChainSync( | 74 Expect.throws(() => c.useCertificateChain( |
75 localFile('certificates/server_key.p12'), password: "dart" * 1000), | 75 localFile('certificates/server_key.p12'), password: "dart" * 1000), |
76 argumentOrTypeError); | 76 argumentOrTypeError); |
77 Expect.throws(() => c.setClientAuthoritiesSync( | 77 Expect.throws(() => c.setClientAuthorities( |
78 localFile('certificates/server_key.p12'), password: "dart" * 1000), | 78 localFile('certificates/server_key.p12'), password: "dart" * 1000), |
79 argumentOrTypeError); | 79 argumentOrTypeError); |
80 | 80 |
81 // Bad password type. | 81 // Bad password type. |
82 Expect.throws(() => c.usePrivateKeySync( | 82 Expect.throws(() => c.usePrivateKey( |
83 localFile('certificates/server_key.pem'), password: 3), | 83 localFile('certificates/server_key.pem'), password: 3), |
84 argumentOrTypeError); | 84 argumentOrTypeError); |
85 Expect.throws(() => c.setTrustedCertificatesBytes( | 85 Expect.throws(() => c.setTrustedCertificatesBytes( |
86 localFile('certificates/server_key.pem'), password: 3), | 86 localFile('certificates/server_key.pem'), password: 3), |
87 argumentOrTypeError); | 87 argumentOrTypeError); |
88 Expect.throws(() => c.useCertificateChainBytes( | 88 Expect.throws(() => c.useCertificateChainBytes( |
89 localFile('certificates/server_key.pem'), password: 3), | 89 localFile('certificates/server_key.pem'), password: 3), |
90 argumentOrTypeError); | 90 argumentOrTypeError); |
91 Expect.throws(() => c.setClientAuthoritiesBytes( | 91 Expect.throws(() => c.setClientAuthoritiesBytes( |
92 localFile('certificates/server_key.pem'), password: 3), | 92 localFile('certificates/server_key.pem'), password: 3), |
93 argumentOrTypeError); | 93 argumentOrTypeError); |
94 | 94 |
95 // Empty data. | 95 // Empty data. |
96 Expect.throws(() => c.usePrivateKeyBytes([], password: 'dartdart'), | 96 Expect.throws(() => c.usePrivateKeyBytes([], password: 'dartdart'), |
97 tlsException); | 97 tlsException); |
98 Expect.throws(() => c.setTrustedCertificatesBytes([]), tlsException); | 98 Expect.throws(() => c.setTrustedCertificatesBytes([]), tlsException); |
99 Expect.throws(() => c.useCertificateChainBytes([]), tlsException); | 99 Expect.throws(() => c.useCertificateChainBytes([]), tlsException); |
100 Expect.throws(() => c.setClientAuthoritiesBytes([]), argumentError); | 100 Expect.throws(() => c.setClientAuthoritiesBytes([]), argumentError); |
101 | 101 |
102 // Malformed PEM certs. | 102 // Malformed PEM certs. |
103 Expect.throws(() => c.usePrivateKeySync( | 103 Expect.throws(() => c.usePrivateKey( |
104 localFile('certificates/client1_key_malformed.pem'), | 104 localFile('certificates/client1_key_malformed.pem'), |
105 password: "dartdart"), | 105 password: "dartdart"), |
106 tlsException); | 106 tlsException); |
107 Expect.throws(() => c.setTrustedCertificatesSync( | 107 Expect.throws(() => c.setTrustedCertificates( |
108 localFile('certificates/trusted_certs_malformed.pem')), | 108 localFile('certificates/trusted_certs_malformed.pem')), |
109 tlsException); | 109 tlsException); |
110 Expect.throws(() => c.useCertificateChainSync( | 110 Expect.throws(() => c.useCertificateChain( |
111 localFile('certificates/server_chain_malformed1.pem')), | 111 localFile('certificates/server_chain_malformed1.pem')), |
112 tlsException); | 112 tlsException); |
113 Expect.throws(() => c.useCertificateChainSync( | 113 Expect.throws(() => c.useCertificateChain( |
114 localFile('certificates/server_chain_malformed2.pem')), | 114 localFile('certificates/server_chain_malformed2.pem')), |
115 tlsException); | 115 tlsException); |
116 Expect.throws(() => c.setClientAuthoritiesSync( | 116 Expect.throws(() => c.setClientAuthorities( |
117 localFile('certificates/client_authority_malformed.pem')), | 117 localFile('certificates/client_authority_malformed.pem')), |
118 argumentError); | 118 argumentError); |
119 | 119 |
120 c.usePrivateKeySync( | 120 c.usePrivateKey( |
121 localFile('certificates/server_key.pem'), password: "dartdart"); | 121 localFile('certificates/server_key.pem'), password: "dartdart"); |
122 } | 122 } |
123 | 123 |
124 void main() { | 124 void main() { |
125 testUsePrivateKeyArguments(); | 125 testUsePrivateKeyArguments(); |
126 } | 126 } |
OLD | NEW |