001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.io.crypto.tls;
019
020import org.apache.hadoop.hbase.HBaseClassTestRule;
021import org.apache.hadoop.hbase.testclassification.SecurityTests;
022import org.apache.hadoop.hbase.testclassification.SmallTests;
023import org.junit.Assert;
024import org.junit.ClassRule;
025import org.junit.Test;
026import org.junit.experimental.categories.Category;
027
028/**
029 * This file has been copied from the Apache ZooKeeper project.
030 * @see <a href=
031 *      "https://github.com/apache/zookeeper/blob/master/zookeeper-server/src/test/java/org/apache/zookeeper/common/KeyStoreFileTypeTest.java">Base
032 *      revision</a>
033 */
034@Category({ SecurityTests.class, SmallTests.class })
035public class TestKeyStoreFileType {
036
037  @ClassRule
038  public static final HBaseClassTestRule CLASS_RULE =
039    HBaseClassTestRule.forClass(TestKeyStoreFileType.class);
040
041  @Test
042  public void testGetPropertyValue() {
043    Assert.assertEquals("PEM", KeyStoreFileType.PEM.getPropertyValue());
044    Assert.assertEquals("JKS", KeyStoreFileType.JKS.getPropertyValue());
045    Assert.assertEquals("PKCS12", KeyStoreFileType.PKCS12.getPropertyValue());
046    Assert.assertEquals("BCFKS", KeyStoreFileType.BCFKS.getPropertyValue());
047  }
048
049  @Test
050  public void testFromPropertyValue() {
051    Assert.assertEquals(KeyStoreFileType.PEM, KeyStoreFileType.fromPropertyValue("PEM"));
052    Assert.assertEquals(KeyStoreFileType.JKS, KeyStoreFileType.fromPropertyValue("JKS"));
053    Assert.assertEquals(KeyStoreFileType.PKCS12, KeyStoreFileType.fromPropertyValue("PKCS12"));
054    Assert.assertEquals(KeyStoreFileType.BCFKS, KeyStoreFileType.fromPropertyValue("BCFKS"));
055    Assert.assertNull(KeyStoreFileType.fromPropertyValue(""));
056    Assert.assertNull(KeyStoreFileType.fromPropertyValue(null));
057  }
058
059  @Test
060  public void testFromPropertyValueIgnoresCase() {
061    Assert.assertEquals(KeyStoreFileType.PEM, KeyStoreFileType.fromPropertyValue("pem"));
062    Assert.assertEquals(KeyStoreFileType.JKS, KeyStoreFileType.fromPropertyValue("jks"));
063    Assert.assertEquals(KeyStoreFileType.PKCS12, KeyStoreFileType.fromPropertyValue("pkcs12"));
064    Assert.assertEquals(KeyStoreFileType.BCFKS, KeyStoreFileType.fromPropertyValue("bcfks"));
065    Assert.assertNull(KeyStoreFileType.fromPropertyValue(""));
066    Assert.assertNull(KeyStoreFileType.fromPropertyValue(null));
067  }
068
069  @Test(expected = IllegalArgumentException.class)
070  public void testFromPropertyValueThrowsOnBadPropertyValue() {
071    KeyStoreFileType.fromPropertyValue("foobar");
072  }
073
074  @Test
075  public void testFromFilename() {
076    Assert.assertEquals(KeyStoreFileType.JKS, KeyStoreFileType.fromFilename("mykey.jks"));
077    Assert.assertEquals(KeyStoreFileType.JKS,
078      KeyStoreFileType.fromFilename("/path/to/key/dir/mykey.jks"));
079    Assert.assertEquals(KeyStoreFileType.PEM, KeyStoreFileType.fromFilename("mykey.pem"));
080    Assert.assertEquals(KeyStoreFileType.PEM,
081      KeyStoreFileType.fromFilename("/path/to/key/dir/mykey.pem"));
082    Assert.assertEquals(KeyStoreFileType.PKCS12, KeyStoreFileType.fromFilename("mykey.p12"));
083    Assert.assertEquals(KeyStoreFileType.PKCS12,
084      KeyStoreFileType.fromFilename("/path/to/key/dir/mykey.p12"));
085    Assert.assertEquals(KeyStoreFileType.BCFKS, KeyStoreFileType.fromFilename("mykey.bcfks"));
086    Assert.assertEquals(KeyStoreFileType.BCFKS,
087      KeyStoreFileType.fromFilename("/path/to/key/dir/mykey.bcfks"));
088  }
089
090  @Test(expected = IllegalArgumentException.class)
091  public void testFromFilenameThrowsOnBadFileExtension() {
092    KeyStoreFileType.fromFilename("prod.key");
093  }
094
095  @Test
096  public void testFromPropertyValueOrFileName() {
097    // Property value takes precedence if provided
098    Assert.assertEquals(KeyStoreFileType.JKS,
099      KeyStoreFileType.fromPropertyValueOrFileName("JKS", "prod.key"));
100    Assert.assertEquals(KeyStoreFileType.PEM,
101      KeyStoreFileType.fromPropertyValueOrFileName("PEM", "prod.key"));
102    Assert.assertEquals(KeyStoreFileType.PKCS12,
103      KeyStoreFileType.fromPropertyValueOrFileName("PKCS12", "prod.key"));
104    Assert.assertEquals(KeyStoreFileType.BCFKS,
105      KeyStoreFileType.fromPropertyValueOrFileName("BCFKS", "prod.key"));
106    // Falls back to filename detection if no property value
107    Assert.assertEquals(KeyStoreFileType.JKS,
108      KeyStoreFileType.fromPropertyValueOrFileName("", "prod.jks"));
109  }
110
111  @Test(expected = IllegalArgumentException.class)
112  public void testFromPropertyValueOrFileNameThrowsOnBadPropertyValue() {
113    KeyStoreFileType.fromPropertyValueOrFileName("foobar", "prod.jks");
114  }
115
116  @Test(expected = IllegalArgumentException.class)
117  public void testFromPropertyValueOrFileNameThrowsOnBadFileExtension() {
118    KeyStoreFileType.fromPropertyValueOrFileName("", "prod.key");
119  }
120}