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 17 package libcore.javax.net.ssl; 18 19 import static org.junit.Assert.assertEquals; 20 import static org.junit.Assert.assertFalse; 21 import static org.junit.Assert.assertNotNull; 22 import static org.junit.Assert.assertNotSame; 23 import static org.junit.Assert.assertNull; 24 import static org.junit.Assert.assertTrue; 25 import static org.junit.Assert.fail; 26 27 import java.util.ArrayList; 28 import java.util.Arrays; 29 import java.util.Collection; 30 import java.util.Collections; 31 import javax.net.ssl.SNIHostName; 32 import javax.net.ssl.SNIMatcher; 33 import javax.net.ssl.SNIServerName; 34 import javax.net.ssl.SSLParameters; 35 import org.junit.Test; 36 import org.junit.runner.RunWith; 37 import org.junit.runners.JUnit4; 38 39 @RunWith(JUnit4.class) 40 public class SSLParametersTest extends AbstractSSLTest { 41 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 SSLParameters p = new SSLParameters(); 135 ArrayList<SNIServerName> dupeNames = new ArrayList<>(); 136 dupeNames.add(new SNIHostName("www.example.com")); 137 dupeNames.add(new SNIHostName("www.example.com")); 138 try { 139 p.setServerNames(dupeNames); 140 fail("Should throw IllegalArgumentException when names are duplicated"); 141 } catch (IllegalArgumentException expected) { 142 // Ignored. 143 } 144 } 145 146 @Test 147 public void test_SSLParameters_setServerNames_setNull_getNull() throws Exception { 148 SSLParameters p = new SSLParameters(); 149 p.setServerNames(Collections.singletonList(new SNIHostName("www.example.com"))); 150 assertNotNull(p.getServerNames()); 151 p.setServerNames(null); 152 assertNull(p.getServerNames()); 153 } 154 155 @Test 156 public void test_SSLParameters_setServerNames_setEmpty_getEmpty() throws Exception { 157 SSLParameters p = new SSLParameters(); 158 p.setServerNames(new ArrayList<>()); 159 Collection<SNIServerName> actual = p.getServerNames(); 160 assertNotNull(actual); 161 assertEquals(0, actual.size()); 162 } 163 164 @Test 165 public void test_SSLParameters_getServerNames_unmodifiable() throws Exception { 166 SSLParameters p = new SSLParameters(); 167 p.setServerNames(Collections.singletonList(new SNIHostName("www.example.com"))); 168 Collection<SNIServerName> actual = p.getServerNames(); 169 try { 170 actual.add(new SNIHostName("www.foo.com")); 171 fail("Should not allow modifications to the list"); 172 } catch (UnsupportedOperationException expected) { 173 // Ignored. 174 } 175 } 176 177 @Test 178 public void test_SSLParameters_setSNIMatchers_duplicatedNameThrows() throws Exception { 179 SSLParameters p = new SSLParameters(); 180 ArrayList<SNIMatcher> dupeMatchers = new ArrayList<>(); 181 dupeMatchers.add(SNIHostName.createSNIMatcher("www\\.example\\.com")); 182 dupeMatchers.add(SNIHostName.createSNIMatcher("www\\.example\\.com")); 183 try { 184 p.setSNIMatchers(dupeMatchers); 185 fail("Should throw IllegalArgumentException when matchers are duplicated"); 186 } catch (IllegalArgumentException expected) { 187 // Ignored. 188 } 189 } 190 191 @Test 192 public void test_SSLParameters_setSNIMatchers_setNull_getNull() throws Exception { 193 SSLParameters p = new SSLParameters(); 194 p.setSNIMatchers( 195 Collections.singletonList(SNIHostName.createSNIMatcher("www\\.example\\.com"))); 196 assertNotNull(p.getSNIMatchers()); 197 p.setSNIMatchers(null); 198 assertNull(p.getSNIMatchers()); 199 } 200 201 @Test 202 public void test_SSLParameters_setSNIMatchers_setEmpty_getEmpty() throws Exception { 203 SSLParameters p = new SSLParameters(); 204 p.setSNIMatchers( 205 Collections.singletonList(SNIHostName.createSNIMatcher("www\\.example\\.com"))); 206 assertEquals(1, p.getSNIMatchers().size()); 207 p.setSNIMatchers(Collections.emptyList()); 208 Collection<SNIMatcher> actual = p.getSNIMatchers(); 209 assertNotNull(actual); 210 assertEquals(0, actual.size()); 211 } 212 213 @Test 214 public void test_SSLParameters_getSNIMatchers_unmodifiable() throws Exception { 215 SSLParameters p = new SSLParameters(); 216 p.setSNIMatchers( 217 Collections.singletonList(SNIHostName.createSNIMatcher("www\\.example\\.com"))); 218 Collection<SNIMatcher> actual = p.getSNIMatchers(); 219 try { 220 actual.add(SNIHostName.createSNIMatcher("www\\.google\\.com")); 221 fail("Should not allow modification of list"); 222 } catch (UnsupportedOperationException expected) { 223 // Ignored. 224 } 225 } 226 } 227