summaryrefslogtreecommitdiffstats
path: root/support/src/test/java/libcore/javax/net/ssl/SSLDefaultConfigurationAsserts.java
blob: d54f5e5e9c97d1772cdd5ba4800b1b4b27411566 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/*
 * Copyright (C) 2013 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package libcore.javax.net.ssl;

import java.io.IOException;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLEngine;
import javax.net.ssl.SSLParameters;
import javax.net.ssl.SSLServerSocket;
import javax.net.ssl.SSLServerSocketFactory;
import javax.net.ssl.SSLSocket;
import javax.net.ssl.SSLSocketFactory;
import junit.framework.Assert;
import libcore.java.security.StandardNames;

/**
 * Assertions about the default configuration of TLS/SSL primitives.
 */
public abstract class SSLDefaultConfigurationAsserts extends Assert {

  /** Hidden constructor to prevent instantiation. */
  private SSLDefaultConfigurationAsserts() {}

  /**
   * Asserts that the provided {@link SSLContext} has the expected default configuration.
   */
  public static void assertSSLContext(SSLContext sslContext) throws IOException {
    assertDefaultSSLParametersClient(sslContext.getDefaultSSLParameters());
    assertSupportedSSLParametersClient(sslContext.getSupportedSSLParameters());
    assertSSLSocketFactory(sslContext.getSocketFactory());
    assertSSLServerSocketFactory(sslContext.getServerSocketFactory());
    assertSSLEngine(sslContext.createSSLEngine());
    assertSSLEngine(sslContext.createSSLEngine(null, -1));
  }

  /**
   * Asserts that the provided {@link SSLSocketFactory} has the expected default configuration.
   */
  public static void assertSSLSocketFactory(SSLSocketFactory sslSocketFactory) throws IOException {
    StandardNames.assertDefaultCipherSuites(sslSocketFactory.getDefaultCipherSuites());
    StandardNames.assertSupportedCipherSuites(sslSocketFactory.getSupportedCipherSuites());
    assertContainsAll("Unsupported default cipher suites",
        sslSocketFactory.getSupportedCipherSuites(),
        sslSocketFactory.getDefaultCipherSuites());

    assertSSLSocket((SSLSocket) sslSocketFactory.createSocket());
  }

  /**
   * Asserts that the provided {@link SSLServerSocketFactory} has the expected default
   * configuration.
   */
  public static void assertSSLServerSocketFactory(SSLServerSocketFactory sslServerSocketFactory)
      throws IOException {
    StandardNames.assertDefaultCipherSuites(sslServerSocketFactory.getDefaultCipherSuites());
    StandardNames.assertSupportedCipherSuites(sslServerSocketFactory.getSupportedCipherSuites());
    assertContainsAll("Unsupported default cipher suites",
        sslServerSocketFactory.getSupportedCipherSuites(),
        sslServerSocketFactory.getDefaultCipherSuites());

    assertSSLServerSocket((SSLServerSocket) sslServerSocketFactory.createServerSocket());
  }

  /**
   * Asserts that the provided {@link SSLSocket} has the expected default configuration.
   */
  public static void assertSSLSocket(SSLSocket sslSocket) {
    assertSSLParametersClient(sslSocket.getSSLParameters());

    StandardNames.assertDefaultCipherSuites(sslSocket.getEnabledCipherSuites());
    StandardNames.assertSupportedCipherSuites(sslSocket.getSupportedCipherSuites());
    assertContainsAll("Unsupported enabled cipher suites",
        sslSocket.getSupportedCipherSuites(),
        sslSocket.getEnabledCipherSuites());

    StandardNames.assertDefaultProtocolsClient(sslSocket.getEnabledProtocols());
    StandardNames.assertSupportedProtocols(sslSocket.getSupportedProtocols());
    assertContainsAll("Unsupported enabled protocols",
        sslSocket.getSupportedProtocols(),
        sslSocket.getEnabledProtocols());

    assertTrue(sslSocket.getUseClientMode());
    assertTrue(sslSocket.getEnableSessionCreation());
    assertFalse(sslSocket.getNeedClientAuth());
    assertFalse(sslSocket.getWantClientAuth());
  }

  /**
   * Asserts that the provided {@link SSLServerSocket} has the expected default configuration.
   */
  public static void assertSSLServerSocket(SSLServerSocket sslServerSocket) {
    // TODO: Check SSLParameters when supported by SSLServerSocket API

    StandardNames.assertDefaultCipherSuites(sslServerSocket.getEnabledCipherSuites());
    StandardNames.assertSupportedCipherSuites(sslServerSocket.getSupportedCipherSuites());
    assertContainsAll("Unsupported enabled cipher suites",
        sslServerSocket.getSupportedCipherSuites(),
        sslServerSocket.getEnabledCipherSuites());

    StandardNames.assertDefaultProtocolsServer(sslServerSocket.getEnabledProtocols());
    StandardNames.assertSupportedProtocols(sslServerSocket.getSupportedProtocols());
    assertContainsAll("Unsupported enabled protocols",
        sslServerSocket.getSupportedProtocols(),
        sslServerSocket.getEnabledProtocols());

    assertTrue(sslServerSocket.getEnableSessionCreation());
    assertFalse(sslServerSocket.getNeedClientAuth());
    assertFalse(sslServerSocket.getWantClientAuth());
  }

  /**
   * Asserts that the provided {@link SSLEngine} has the expected default configuration.
   */
  public static void assertSSLEngine(SSLEngine sslEngine) {
    assertFalse(sslEngine.getUseClientMode());
    assertSSLEngineSSLParameters(sslEngine.getSSLParameters());

    StandardNames.assertDefaultCipherSuites(sslEngine.getEnabledCipherSuites());
    StandardNames.assertSupportedCipherSuites(sslEngine.getSupportedCipherSuites());
    assertContainsAll("Unsupported enabled cipher suites",
        sslEngine.getSupportedCipherSuites(),
        sslEngine.getEnabledCipherSuites());

    StandardNames.assertSSLEngineDefaultProtocols(sslEngine.getEnabledProtocols());
    StandardNames.assertSupportedProtocols(sslEngine.getSupportedProtocols());
    assertContainsAll("Unsupported enabled protocols",
        sslEngine.getSupportedProtocols(),
        sslEngine.getEnabledProtocols());

    assertTrue(sslEngine.getEnableSessionCreation());
    assertFalse(sslEngine.getNeedClientAuth());
    assertFalse(sslEngine.getWantClientAuth());
  }

  /**
   * Asserts that the provided {@link SSLParameters} describe the expected default configuration
   * for client-side mode of operation.
   */
  public static void assertSSLParametersClient(SSLParameters sslParameters) {
    assertDefaultSSLParametersClient(sslParameters);
  }

  /**
   * Asserts that the provided default {@link SSLParameters} are as expected for client-side mode of
   * operation.
   */
  private static void assertDefaultSSLParametersClient(SSLParameters sslParameters) {
    StandardNames.assertDefaultCipherSuites(sslParameters.getCipherSuites());
    StandardNames.assertDefaultProtocolsClient(sslParameters.getProtocols());
    assertFalse(sslParameters.getWantClientAuth());
    assertFalse(sslParameters.getNeedClientAuth());
  }

  /**
   * Asserts that the provided supported {@link SSLParameters} are as expected for client-side mode
   * of operation.
   */
  private static void assertSupportedSSLParametersClient(SSLParameters sslParameters) {
    StandardNames.assertSupportedCipherSuites(sslParameters.getCipherSuites());
    StandardNames.assertSupportedProtocols(sslParameters.getProtocols());
    assertFalse(sslParameters.getWantClientAuth());
    assertFalse(sslParameters.getNeedClientAuth());
  }

  /**
   * Asserts that the provided {@link SSLParameters} has the expected default configuration for
   * {@link SSLEngine}.
   */
  public static void assertSSLEngineSSLParameters(SSLParameters sslParameters) {
    StandardNames.assertDefaultCipherSuites(sslParameters.getCipherSuites());
    StandardNames.assertSSLEngineDefaultProtocols(sslParameters.getProtocols());
    assertFalse(sslParameters.getWantClientAuth());
    assertFalse(sslParameters.getNeedClientAuth());
  }

  /**
   * Asserts that the {@code container} contains all the {@code elements}.
   */
  private static void assertContainsAll(String message, String[] container, String[] elements) {
    Set<String> elementsNotInContainer = new HashSet<String>(Arrays.asList(elements));
    elementsNotInContainer.removeAll(Arrays.asList(container));
    assertEquals(message, Collections.EMPTY_SET, elementsNotInContainer);
  }
}