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.util; 019 020import static org.junit.Assert.fail; 021 022import java.security.Key; 023import org.apache.hadoop.conf.Configuration; 024import org.apache.hadoop.hbase.HBaseClassTestRule; 025import org.apache.hadoop.hbase.HBaseConfiguration; 026import org.apache.hadoop.hbase.HConstants; 027import org.apache.hadoop.hbase.io.crypto.Cipher; 028import org.apache.hadoop.hbase.io.crypto.CipherProvider; 029import org.apache.hadoop.hbase.io.crypto.DefaultCipherProvider; 030import org.apache.hadoop.hbase.io.crypto.KeyProvider; 031import org.apache.hadoop.hbase.io.crypto.KeyProviderForTesting; 032import org.apache.hadoop.hbase.testclassification.MiscTests; 033import org.apache.hadoop.hbase.testclassification.SmallTests; 034import org.junit.ClassRule; 035import org.junit.Test; 036import org.junit.experimental.categories.Category; 037 038@Category({MiscTests.class, SmallTests.class}) 039public class TestEncryptionTest { 040 041 @ClassRule 042 public static final HBaseClassTestRule CLASS_RULE = 043 HBaseClassTestRule.forClass(TestEncryptionTest.class); 044 045 @Test 046 public void testTestKeyProvider() { 047 Configuration conf = HBaseConfiguration.create(); 048 try { 049 conf.set(HConstants.CRYPTO_KEYPROVIDER_CONF_KEY, KeyProviderForTesting.class.getName()); 050 EncryptionTest.testKeyProvider(conf); 051 } catch (Exception e) { 052 fail("Instantiation of test key provider should have passed"); 053 } 054 try { 055 conf.set(HConstants.CRYPTO_KEYPROVIDER_CONF_KEY, FailingKeyProvider.class.getName()); 056 EncryptionTest.testKeyProvider(conf); 057 fail("Instantiation of bad test key provider should have failed check"); 058 } catch (Exception e) { } 059 } 060 061 @Test 062 public void testTestCipherProvider() { 063 Configuration conf = HBaseConfiguration.create(); 064 try { 065 conf.set(HConstants.CRYPTO_CIPHERPROVIDER_CONF_KEY, DefaultCipherProvider.class.getName()); 066 EncryptionTest.testCipherProvider(conf); 067 } catch (Exception e) { 068 fail("Instantiation of test cipher provider should have passed"); 069 } 070 try { 071 conf.set(HConstants.CRYPTO_CIPHERPROVIDER_CONF_KEY, FailingCipherProvider.class.getName()); 072 EncryptionTest.testCipherProvider(conf); 073 fail("Instantiation of bad test cipher provider should have failed check"); 074 } catch (Exception e) { } 075 } 076 077 @Test 078 public void testTestCipher() { 079 Configuration conf = HBaseConfiguration.create(); 080 conf.set(HConstants.CRYPTO_KEYPROVIDER_CONF_KEY, KeyProviderForTesting.class.getName()); 081 String algorithm = 082 conf.get(HConstants.CRYPTO_KEY_ALGORITHM_CONF_KEY, HConstants.CIPHER_AES); 083 try { 084 EncryptionTest.testEncryption(conf, algorithm, null); 085 } catch (Exception e) { 086 fail("Test for cipher " + algorithm + " should have succeeded"); 087 } 088 try { 089 EncryptionTest.testEncryption(conf, "foobar", null); 090 fail("Test for bogus cipher should have failed"); 091 } catch (Exception e) { } 092 } 093 094 public static class FailingKeyProvider implements KeyProvider { 095 096 @Override 097 public void init(String params) { 098 throw new RuntimeException("BAD!"); 099 } 100 101 @Override 102 public Key getKey(String alias) { 103 return null; 104 } 105 106 @Override 107 public Key[] getKeys(String[] aliases) { 108 return null; 109 } 110 111 } 112 113 public static class FailingCipherProvider implements CipherProvider { 114 115 public FailingCipherProvider() { 116 super(); 117 throw new RuntimeException("BAD!"); 118 } 119 120 @Override 121 public Configuration getConf() { 122 return null; 123 } 124 125 @Override 126 public void setConf(Configuration conf) { 127 } 128 129 @Override 130 public String getName() { 131 return null; 132 } 133 134 @Override 135 public String[] getSupportedCiphers() { 136 return null; 137 } 138 139 @Override 140 public Cipher getCipher(String name) { 141 return null; 142 } 143 144 } 145}