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.hc.core5.ssl;
29  
30  import static org.junit.jupiter.api.Assertions.assertAll;
31  import static org.junit.jupiter.api.Assertions.assertEquals;
32  import static org.junit.jupiter.api.Assertions.assertNotNull;
33  
34  import java.security.KeyManagementException;
35  import java.security.KeyStore;
36  import java.security.KeyStoreException;
37  import java.security.NoSuchAlgorithmException;
38  import java.security.UnrecoverableKeyException;
39  
40  import javax.net.ssl.KeyManagerFactory;
41  import javax.net.ssl.SSLContext;
42  import javax.net.ssl.TrustManagerFactory;
43  
44  import org.junit.jupiter.api.Test;
45  
46  public class SSLContextsTest {
47  
48      @Test
49      void createDefault() {
50          final SSLContext sslContext = SSLContexts.createDefault();
51          assertAll(
52                  () -> assertNotNull(sslContext),
53                  () -> assertEquals(SSLContextBuilder.TLS, sslContext.getProtocol()),
54                  () -> assertNotNull(sslContext.getProvider())
55          );
56      }
57  
58      @Test
59      void createSystemDefault() {
60          final SSLContext sslContext = SSLContexts.createSystemDefault();
61          assertAll(
62                  () -> assertNotNull(sslContext),
63                  () -> assertEquals("Default", sslContext.getProtocol()),
64                  () -> assertNotNull(sslContext.getProvider())
65          );
66      }
67  
68      @Test
69      void custom() throws NoSuchAlgorithmException, KeyStoreException, UnrecoverableKeyException, KeyManagementException {
70  
71          final SSLContext sslContext = SSLContexts.custom()
72                  .setKeyStoreType(KeyStore.getDefaultType())
73                  .setKeyManagerFactoryAlgorithm(KeyManagerFactory.getDefaultAlgorithm())
74                  .setTrustManagerFactoryAlgorithm(TrustManagerFactory.getDefaultAlgorithm())
75                  .setProvider("SunJSSE")
76                  .setProtocol("TLS")
77                  .setSecureRandom(null)
78                  .loadTrustMaterial((KeyStore) null, null)
79                  .loadKeyMaterial((KeyStore) null, null, null)
80                  .build();
81  
82          assertAll(
83                  () -> assertNotNull(sslContext),
84                  () -> assertEquals(SSLContextBuilder.TLS, sslContext.getProtocol()),
85                  () -> assertEquals("SunJSSE", sslContext.getProvider().getName())
86          );
87      }
88  }