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 libcore.javax.net.ssl; 18 19import java.security.KeyStore.Builder; 20import java.security.KeyStore.PasswordProtection; 21import javax.net.ssl.KeyStoreBuilderParameters; 22import java.util.Arrays; 23import java.util.List; 24import junit.framework.TestCase; 25import libcore.java.security.TestKeyStore; 26 27public class KeyStoreBuilderParametersTest extends TestCase { 28 public void test_init_Builder_null() { 29 try { 30 new KeyStoreBuilderParameters((Builder) null); 31 fail(); 32 } catch (NullPointerException expected) { 33 } 34 } 35 36 public void test_init_Builder() { 37 TestKeyStore testKeyStore = TestKeyStore.getClient(); 38 Builder builder = Builder.newInstance(testKeyStore.keyStore, 39 new PasswordProtection(testKeyStore.storePassword)); 40 KeyStoreBuilderParameters ksbp = new KeyStoreBuilderParameters(builder); 41 assertNotNull(ksbp); 42 assertNotNull(ksbp.getParameters()); 43 assertEquals(1, ksbp.getParameters().size()); 44 assertSame(builder, ksbp.getParameters().get(0)); 45 } 46 47 public void test_init_List_null() { 48 try { 49 new KeyStoreBuilderParameters((List) null); 50 } catch (NullPointerException expected) { 51 } 52 } 53 54 public void test_init_List() { 55 TestKeyStore testKeyStore1 = TestKeyStore.getClient(); 56 TestKeyStore testKeyStore2 = TestKeyStore.getServer(); 57 Builder builder1 = Builder.newInstance(testKeyStore1.keyStore, 58 new PasswordProtection(testKeyStore1.storePassword)); 59 Builder builder2 = Builder.newInstance(testKeyStore2.keyStore, 60 new PasswordProtection(testKeyStore2.storePassword)); 61 62 List list = Arrays.asList(builder1, builder2); 63 KeyStoreBuilderParameters ksbp = new KeyStoreBuilderParameters(list); 64 assertNotNull(ksbp); 65 assertNotNull(ksbp.getParameters()); 66 assertNotSame(list, ksbp.getParameters()); 67 assertEquals(2, ksbp.getParameters().size()); 68 assertSame(builder1, ksbp.getParameters().get(0)); 69 assertSame(builder2, ksbp.getParameters().get(1)); 70 71 // confirm result is not modifiable 72 try { 73 ksbp.getParameters().set(0, builder2); 74 fail(); 75 } catch (UnsupportedOperationException expected) { 76 } 77 78 // confirm result is a copy of original 79 list.set(0, builder2); 80 assertSame(builder1, ksbp.getParameters().get(0)); 81 } 82} 83