1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one 3 * or more contributor license agreements. See the NOTICE file 4 * distributed with this work for additional information 5 * regarding copyright ownership. The ASF licenses this file 6 * to you under the Apache License, Version 2.0 (the 7 * "License"); you may not use this file except in compliance 8 * with the License. You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, software 13 * distributed under the License is distributed on an "AS IS" BASIS, 14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 * See the License for the specific language governing permissions and 16 * limitations under the License. 17 */ 18 19 package org.apache.zookeeper.common; 20 21 import static org.junit.jupiter.api.Assertions.assertEquals; 22 import static org.junit.jupiter.api.Assertions.assertThrows; 23 import java.io.IOException; 24 import java.security.KeyStore; 25 import org.junit.jupiter.params.ParameterizedTest; 26 import org.junit.jupiter.params.provider.MethodSource; 27 28 public class JKSFileLoaderTest extends BaseX509ParameterizedTestCase { 29 30 @ParameterizedTest 31 @MethodSource("data") testLoadKeyStore( X509KeyType caKeyType, X509KeyType certKeyType, String keyPassword, Integer paramIndex)32 public void testLoadKeyStore( 33 X509KeyType caKeyType, X509KeyType certKeyType, String keyPassword, Integer paramIndex) 34 throws Exception { 35 init(caKeyType, certKeyType, keyPassword, paramIndex); 36 String path = x509TestContext.getKeyStoreFile(KeyStoreFileType.JKS).getAbsolutePath(); 37 KeyStore ks = new JKSFileLoader.Builder().setKeyStorePath(path).setKeyStorePassword(x509TestContext.getKeyStorePassword()).build().loadKeyStore(); 38 assertEquals(1, ks.size()); 39 } 40 41 @ParameterizedTest 42 @MethodSource("data") testLoadKeyStoreWithWrongPassword( X509KeyType caKeyType, X509KeyType certKeyType, String keyPassword, Integer paramIndex)43 public void testLoadKeyStoreWithWrongPassword( 44 X509KeyType caKeyType, X509KeyType certKeyType, String keyPassword, Integer paramIndex) 45 throws Exception { 46 init(caKeyType, certKeyType, keyPassword, paramIndex); 47 assertThrows(Exception.class, () -> { 48 String path = x509TestContext.getKeyStoreFile(KeyStoreFileType.JKS).getAbsolutePath(); 49 new JKSFileLoader.Builder().setKeyStorePath(path).setKeyStorePassword("wrong password").build().loadKeyStore(); 50 }); 51 } 52 53 @ParameterizedTest 54 @MethodSource("data") testLoadKeyStoreWithWrongFilePath( X509KeyType caKeyType, X509KeyType certKeyType, String keyPassword, Integer paramIndex)55 public void testLoadKeyStoreWithWrongFilePath( 56 X509KeyType caKeyType, X509KeyType certKeyType, String keyPassword, Integer paramIndex) 57 throws Exception { 58 init(caKeyType, certKeyType, keyPassword, paramIndex); 59 assertThrows(IOException.class, () -> { 60 String path = x509TestContext.getKeyStoreFile(KeyStoreFileType.JKS).getAbsolutePath(); 61 new JKSFileLoader.Builder().setKeyStorePath(path 62 + ".does_not_exist").setKeyStorePassword(x509TestContext.getKeyStorePassword()).build().loadKeyStore(); 63 }); 64 } 65 66 @ParameterizedTest 67 @MethodSource("data") testLoadKeyStoreWithNullFilePath( X509KeyType caKeyType, X509KeyType certKeyType, String keyPassword, Integer paramIndex)68 public void testLoadKeyStoreWithNullFilePath( 69 X509KeyType caKeyType, X509KeyType certKeyType, String keyPassword, Integer paramIndex) 70 throws Exception { 71 init(caKeyType, certKeyType, keyPassword, paramIndex); 72 assertThrows(NullPointerException.class, () -> { 73 new JKSFileLoader.Builder().setKeyStorePassword(x509TestContext.getKeyStorePassword()).build().loadKeyStore(); 74 }); 75 } 76 77 @ParameterizedTest 78 @MethodSource("data") testLoadKeyStoreWithWrongFileType( X509KeyType caKeyType, X509KeyType certKeyType, String keyPassword, Integer paramIndex)79 public void testLoadKeyStoreWithWrongFileType( 80 X509KeyType caKeyType, X509KeyType certKeyType, String keyPassword, Integer paramIndex) 81 throws Exception { 82 init(caKeyType, certKeyType, keyPassword, paramIndex); 83 assertThrows(IOException.class, () -> { 84 // Trying to load a PEM file with JKS loader should fail 85 String path = x509TestContext.getKeyStoreFile(KeyStoreFileType.PEM).getAbsolutePath(); 86 new JKSFileLoader.Builder().setKeyStorePath(path).setKeyStorePassword(x509TestContext.getKeyStorePassword()).build().loadKeyStore(); 87 }); 88 } 89 90 @ParameterizedTest 91 @MethodSource("data") testLoadTrustStore( X509KeyType caKeyType, X509KeyType certKeyType, String keyPassword, Integer paramIndex)92 public void testLoadTrustStore( 93 X509KeyType caKeyType, X509KeyType certKeyType, String keyPassword, Integer paramIndex) 94 throws Exception { 95 init(caKeyType, certKeyType, keyPassword, paramIndex); 96 String path = x509TestContext.getTrustStoreFile(KeyStoreFileType.JKS).getAbsolutePath(); 97 KeyStore ts = new JKSFileLoader.Builder().setTrustStorePath(path).setTrustStorePassword(x509TestContext.getTrustStorePassword()).build().loadTrustStore(); 98 assertEquals(1, ts.size()); 99 } 100 101 @ParameterizedTest 102 @MethodSource("data") testLoadTrustStoreWithWrongPassword( X509KeyType caKeyType, X509KeyType certKeyType, String keyPassword, Integer paramIndex)103 public void testLoadTrustStoreWithWrongPassword( 104 X509KeyType caKeyType, X509KeyType certKeyType, String keyPassword, Integer paramIndex) 105 throws Exception { 106 init(caKeyType, certKeyType, keyPassword, paramIndex); 107 assertThrows(Exception.class, () -> { 108 String path = x509TestContext.getTrustStoreFile(KeyStoreFileType.JKS).getAbsolutePath(); 109 new JKSFileLoader.Builder().setTrustStorePath(path).setTrustStorePassword("wrong password").build().loadTrustStore(); 110 }); 111 } 112 113 @ParameterizedTest 114 @MethodSource("data") testLoadTrustStoreWithWrongFilePath( X509KeyType caKeyType, X509KeyType certKeyType, String keyPassword, Integer paramIndex)115 public void testLoadTrustStoreWithWrongFilePath( 116 X509KeyType caKeyType, X509KeyType certKeyType, String keyPassword, Integer paramIndex) 117 throws Exception { 118 init(caKeyType, certKeyType, keyPassword, paramIndex); 119 assertThrows(IOException.class, () -> { 120 String path = x509TestContext.getTrustStoreFile(KeyStoreFileType.JKS).getAbsolutePath(); 121 new JKSFileLoader.Builder().setTrustStorePath(path 122 + ".does_not_exist").setTrustStorePassword(x509TestContext.getTrustStorePassword()).build().loadTrustStore(); 123 }); 124 } 125 126 @ParameterizedTest 127 @MethodSource("data") testLoadTrustStoreWithNullFilePath( X509KeyType caKeyType, X509KeyType certKeyType, String keyPassword, Integer paramIndex)128 public void testLoadTrustStoreWithNullFilePath( 129 X509KeyType caKeyType, X509KeyType certKeyType, String keyPassword, Integer paramIndex) 130 throws Exception { 131 init(caKeyType, certKeyType, keyPassword, paramIndex); 132 assertThrows(NullPointerException.class, () -> { 133 new JKSFileLoader.Builder().setTrustStorePassword(x509TestContext.getTrustStorePassword()).build().loadTrustStore(); 134 }); 135 } 136 137 @ParameterizedTest 138 @MethodSource("data") testLoadTrustStoreWithWrongFileType( X509KeyType caKeyType, X509KeyType certKeyType, String keyPassword, Integer paramIndex)139 public void testLoadTrustStoreWithWrongFileType( 140 X509KeyType caKeyType, X509KeyType certKeyType, String keyPassword, Integer paramIndex) 141 throws Exception { 142 init(caKeyType, certKeyType, keyPassword, paramIndex); 143 assertThrows(IOException.class, () -> { 144 // Trying to load a PEM file with JKS loader should fail 145 String path = x509TestContext.getTrustStoreFile(KeyStoreFileType.PEM).getAbsolutePath(); 146 new JKSFileLoader.Builder().setTrustStorePath(path).setTrustStorePassword(x509TestContext.getTrustStorePassword()).build().loadTrustStore(); 147 }); 148 } 149 150 } 151