View Javadoc

1   /*
2    * ====================================================================
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   * ====================================================================
20   *
21   * This software consists of voluntary contributions made by many
22   * individuals on behalf of the Apache Software Foundation.  For more
23   * information on the Apache Software Foundation, please see
24   * <http://www.apache.org/>.
25   *
26   */
27  
28  package org.apache.http.conn.ssl;
29  
30  import java.io.InputStream;
31  import java.net.URL;
32  import java.security.KeyStore;
33  import java.security.UnrecoverableKeyException;
34  
35  import org.junit.Test;
36  
37  /**
38   * Unit tests for {@link org.apache.http.conn.ssl.SSLContextBuilder}.
39   */
40  public class TestSSLContextBuilder {
41  
42      private static KeyStore load(final String res, final char[] passwd) throws Exception {
43          final KeyStore keystore  = KeyStore.getInstance("jks");
44          final ClassLoader cl = TestSSLContextBuilder.class.getClassLoader();
45          final URL url = cl.getResource(res);
46          final InputStream instream = url.openStream();
47          try {
48              keystore.load(instream, passwd);
49          } finally {
50              instream.close();
51          }
52          return keystore;
53      }
54  
55      @Test
56      public void testBuildDefault() throws Exception {
57          new SSLContextBuilder().build();
58      }
59  
60      @Test
61      public void testBuildAllNull() throws Exception {
62          new SSLContextBuilder()
63                  .useProtocol(null)
64                  .setSecureRandom(null)
65                  .loadTrustMaterial(null)
66                  .loadKeyMaterial(null, null)
67                  .build();
68      }
69  
70      @Test
71      public void testLoadTrustMultipleMaterial() throws Exception {
72          final KeyStore truststore1 = load("hc-test-1.truststore", "nopassword".toCharArray());
73          final KeyStore truststore2 = load("hc-test-2.truststore", "nopassword".toCharArray());
74          new SSLContextBuilder()
75                  .loadTrustMaterial(truststore1)
76                  .loadTrustMaterial(truststore2)
77                  .build();
78      }
79  
80      @Test
81      public void testKeyWithAlternatePassword() throws Exception {
82          final KeyStore keystore = load("test-keypasswd.keystore", "nopassword".toCharArray());
83          final String keyPassword = "password";
84          new SSLContextBuilder()
85                  .loadKeyMaterial(keystore, keyPassword.toCharArray())
86                  .loadTrustMaterial(keystore)
87                  .build();
88      }
89  
90      @Test(expected=UnrecoverableKeyException.class)
91      public void testKeyWithAlternatePasswordInvalid() throws Exception {
92          final KeyStore keystore = load("test-keypasswd.keystore", "nopassword".toCharArray());
93          final String keyPassword = "!password";
94          new SSLContextBuilder()
95                  .loadKeyMaterial(keystore, keyPassword.toCharArray())
96                  .loadTrustMaterial(keystore)
97                  .build();
98      }
99  
100 }