1/* 2 * Copyright (C) 2010 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package org.conscrypt.javax.net.ssl; 18 19import static org.junit.Assert.assertEquals; 20import static org.junit.Assert.assertFalse; 21import static org.junit.Assert.assertNotNull; 22import static org.junit.Assert.assertNotSame; 23import static org.junit.Assert.assertNull; 24import static org.junit.Assert.assertTrue; 25import static org.junit.Assert.fail; 26 27import java.util.ArrayList; 28import java.util.Arrays; 29import java.util.Collection; 30import java.util.Collections; 31import javax.net.ssl.SNIHostName; 32import javax.net.ssl.SNIMatcher; 33import javax.net.ssl.SNIServerName; 34import javax.net.ssl.SSLParameters; 35import org.conscrypt.TestUtils; 36import org.junit.Test; 37import org.junit.runner.RunWith; 38import org.junit.runners.JUnit4; 39 40@RunWith(JUnit4.class) 41public class SSLParametersTest { 42 @Test 43 public void test_SSLParameters_emptyConstructor() { 44 SSLParameters p = new SSLParameters(); 45 assertNull(p.getCipherSuites()); 46 assertNull(p.getProtocols()); 47 assertFalse(p.getWantClientAuth()); 48 assertFalse(p.getNeedClientAuth()); 49 } 50 51 @Test 52 public void test_SSLParameters_cipherSuitesConstructor() { 53 String[] cipherSuites = new String[] {"foo", null, "bar"}; 54 SSLParameters p = new SSLParameters(cipherSuites); 55 assertNotNull(p.getCipherSuites()); 56 assertNotSame(cipherSuites, p.getCipherSuites()); 57 assertEquals(Arrays.asList(cipherSuites), Arrays.asList(p.getCipherSuites())); 58 assertNull(p.getProtocols()); 59 assertFalse(p.getWantClientAuth()); 60 assertFalse(p.getNeedClientAuth()); 61 } 62 63 @Test 64 public void test_SSLParameters_cpherSuitesProtocolsConstructor() { 65 String[] cipherSuites = new String[] {"foo", null, "bar"}; 66 String[] protocols = new String[] {"baz", null, "qux"}; 67 SSLParameters p = new SSLParameters(cipherSuites, protocols); 68 assertNotNull(p.getCipherSuites()); 69 assertNotNull(p.getProtocols()); 70 assertNotSame(cipherSuites, p.getCipherSuites()); 71 assertNotSame(protocols, p.getProtocols()); 72 assertEquals(Arrays.asList(cipherSuites), Arrays.asList(p.getCipherSuites())); 73 assertEquals(Arrays.asList(protocols), Arrays.asList(p.getProtocols())); 74 assertFalse(p.getWantClientAuth()); 75 assertFalse(p.getNeedClientAuth()); 76 } 77 78 @Test 79 public void test_SSLParameters_CipherSuites() { 80 SSLParameters p = new SSLParameters(); 81 assertNull(p.getCipherSuites()); 82 83 // confirm clone on input 84 String[] cipherSuites = new String[] {"fnord"}; 85 String[] copy = cipherSuites.clone(); 86 p.setCipherSuites(copy); 87 copy[0] = null; 88 assertEquals(Arrays.asList(cipherSuites), Arrays.asList(p.getCipherSuites())); 89 90 // confirm clone on output 91 assertNotSame(p.getCipherSuites(), p.getCipherSuites()); 92 } 93 94 @Test 95 public void test_SSLParameters_Protocols() { 96 SSLParameters p = new SSLParameters(); 97 assertNull(p.getProtocols()); 98 99 // confirm clone on input 100 String[] protocols = new String[] {"fnord"}; 101 String[] copy = protocols.clone(); 102 p.setProtocols(copy); 103 copy[0] = null; 104 assertEquals(Arrays.asList(protocols), Arrays.asList(p.getProtocols())); 105 106 // confirm clone on output 107 assertNotSame(p.getProtocols(), p.getProtocols()); 108 } 109 110 @Test 111 public void test_SSLParameters_ClientAuth() { 112 SSLParameters p = new SSLParameters(); 113 assertFalse(p.getWantClientAuth()); 114 assertFalse(p.getNeedClientAuth()); 115 116 // confirm turning one on by itself 117 p.setWantClientAuth(true); 118 assertTrue(p.getWantClientAuth()); 119 assertFalse(p.getNeedClientAuth()); 120 121 // confirm turning setting on toggles the other 122 p.setNeedClientAuth(true); 123 assertFalse(p.getWantClientAuth()); 124 assertTrue(p.getNeedClientAuth()); 125 126 // confirm toggling back 127 p.setWantClientAuth(true); 128 assertTrue(p.getWantClientAuth()); 129 assertFalse(p.getNeedClientAuth()); 130 } 131 132 @Test 133 public void test_SSLParameters_setServerNames_duplicatedNameThrows() throws Exception { 134 TestUtils.assumeSNIHostnameAvailable(); 135 136 SSLParameters p = new SSLParameters(); 137 ArrayList<SNIServerName> dupeNames = new ArrayList<SNIServerName>(); 138 dupeNames.add(new SNIHostName("www.example.com")); 139 dupeNames.add(new SNIHostName("www.example.com")); 140 try { 141 p.setServerNames(dupeNames); 142 fail("Should throw IllegalArgumentException when names are duplicated"); 143 } catch (IllegalArgumentException expected) { 144 // Ignored. 145 } 146 } 147 148 @Test 149 public void test_SSLParameters_setServerNames_setNull_getNull() throws Exception { 150 TestUtils.assumeSNIHostnameAvailable(); 151 SSLParameters p = new SSLParameters(); 152 p.setServerNames( 153 Collections.singletonList((SNIServerName) new SNIHostName("www.example.com"))); 154 assertNotNull(p.getServerNames()); 155 p.setServerNames(null); 156 assertNull(p.getServerNames()); 157 } 158 159 @Test 160 public void test_SSLParameters_setServerNames_setEmpty_getEmpty() throws Exception { 161 TestUtils.assumeSNIHostnameAvailable(); 162 SSLParameters p = new SSLParameters(); 163 p.setServerNames(new ArrayList<SNIServerName>()); 164 Collection<SNIServerName> actual = p.getServerNames(); 165 assertNotNull(actual); 166 assertEquals(0, actual.size()); 167 } 168 169 @Test 170 public void test_SSLParameters_getServerNames_unmodifiable() throws Exception { 171 TestUtils.assumeSNIHostnameAvailable(); 172 SSLParameters p = new SSLParameters(); 173 p.setServerNames( 174 Collections.singletonList((SNIServerName) new SNIHostName("www.example.com"))); 175 Collection<SNIServerName> actual = p.getServerNames(); 176 try { 177 actual.add(new SNIHostName("www.foo.com")); 178 fail("Should not allow modifications to the list"); 179 } catch (UnsupportedOperationException expected) { 180 // Ignored. 181 } 182 } 183 184 @Test 185 public void test_SSLParameters_setSNIMatchers_duplicatedNameThrows() throws Exception { 186 TestUtils.assumeSNIHostnameAvailable(); 187 SSLParameters p = new SSLParameters(); 188 ArrayList<SNIMatcher> dupeMatchers = new ArrayList<SNIMatcher>(); 189 dupeMatchers.add(SNIHostName.createSNIMatcher("www\\.example\\.com")); 190 dupeMatchers.add(SNIHostName.createSNIMatcher("www\\.example\\.com")); 191 try { 192 p.setSNIMatchers(dupeMatchers); 193 fail("Should throw IllegalArgumentException when matchers are duplicated"); 194 } catch (IllegalArgumentException expected) { 195 // Ignored. 196 } 197 } 198 199 @Test 200 public void test_SSLParameters_setSNIMatchers_setNull_getNull() throws Exception { 201 TestUtils.assumeSNIHostnameAvailable(); 202 SSLParameters p = new SSLParameters(); 203 p.setSNIMatchers( 204 Collections.singletonList(SNIHostName.createSNIMatcher("www\\.example\\.com"))); 205 assertNotNull(p.getSNIMatchers()); 206 p.setSNIMatchers(null); 207 assertNull(p.getSNIMatchers()); 208 } 209 210 @Test 211 public void test_SSLParameters_setSNIMatchers_setEmpty_getEmpty() throws Exception { 212 TestUtils.assumeSNIHostnameAvailable(); 213 SSLParameters p = new SSLParameters(); 214 p.setSNIMatchers( 215 Collections.singletonList(SNIHostName.createSNIMatcher("www\\.example\\.com"))); 216 assertEquals(1, p.getSNIMatchers().size()); 217 p.setSNIMatchers(Collections.<SNIMatcher>emptyList()); 218 Collection<SNIMatcher> actual = p.getSNIMatchers(); 219 assertNotNull(actual); 220 assertEquals(0, actual.size()); 221 } 222 223 @Test 224 public void test_SSLParameters_getSNIMatchers_unmodifiable() throws Exception { 225 TestUtils.assumeSNIHostnameAvailable(); 226 SSLParameters p = new SSLParameters(); 227 p.setSNIMatchers( 228 Collections.singletonList(SNIHostName.createSNIMatcher("www\\.example\\.com"))); 229 Collection<SNIMatcher> actual = p.getSNIMatchers(); 230 try { 231 actual.add(SNIHostName.createSNIMatcher("www\\.google\\.com")); 232 fail("Should not allow modification of list"); 233 } catch (UnsupportedOperationException expected) { 234 // Ignored. 235 } 236 } 237} 238