HBASE-23723 Ensure MOB compaction works in optimized mode after snapshot clone (...
[hbase.git] / hbase-server / src / test / java / org / apache / hadoop / hbase / util / TestEncryptionTest.java
bloba254eb3f3580efb42d33e71e40e81435e1bb0bc2
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 package org.apache.hadoop.hbase.util;
20 import static org.junit.Assert.fail;
22 import java.security.Key;
23 import org.apache.hadoop.conf.Configuration;
24 import org.apache.hadoop.hbase.HBaseClassTestRule;
25 import org.apache.hadoop.hbase.HBaseConfiguration;
26 import org.apache.hadoop.hbase.HConstants;
27 import org.apache.hadoop.hbase.io.crypto.Cipher;
28 import org.apache.hadoop.hbase.io.crypto.CipherProvider;
29 import org.apache.hadoop.hbase.io.crypto.DefaultCipherProvider;
30 import org.apache.hadoop.hbase.io.crypto.KeyProvider;
31 import org.apache.hadoop.hbase.io.crypto.KeyProviderForTesting;
32 import org.apache.hadoop.hbase.testclassification.MiscTests;
33 import org.apache.hadoop.hbase.testclassification.SmallTests;
34 import org.junit.ClassRule;
35 import org.junit.Test;
36 import org.junit.experimental.categories.Category;
38 @Category({MiscTests.class, SmallTests.class})
39 public class TestEncryptionTest {
41 @ClassRule
42 public static final HBaseClassTestRule CLASS_RULE =
43 HBaseClassTestRule.forClass(TestEncryptionTest.class);
45 @Test
46 public void testTestKeyProvider() {
47 Configuration conf = HBaseConfiguration.create();
48 try {
49 conf.set(HConstants.CRYPTO_KEYPROVIDER_CONF_KEY, KeyProviderForTesting.class.getName());
50 EncryptionTest.testKeyProvider(conf);
51 } catch (Exception e) {
52 fail("Instantiation of test key provider should have passed");
54 try {
55 conf.set(HConstants.CRYPTO_KEYPROVIDER_CONF_KEY, FailingKeyProvider.class.getName());
56 EncryptionTest.testKeyProvider(conf);
57 fail("Instantiation of bad test key provider should have failed check");
58 } catch (Exception e) { }
61 @Test
62 public void testTestCipherProvider() {
63 Configuration conf = HBaseConfiguration.create();
64 try {
65 conf.set(HConstants.CRYPTO_CIPHERPROVIDER_CONF_KEY, DefaultCipherProvider.class.getName());
66 EncryptionTest.testCipherProvider(conf);
67 } catch (Exception e) {
68 fail("Instantiation of test cipher provider should have passed");
70 try {
71 conf.set(HConstants.CRYPTO_CIPHERPROVIDER_CONF_KEY, FailingCipherProvider.class.getName());
72 EncryptionTest.testCipherProvider(conf);
73 fail("Instantiation of bad test cipher provider should have failed check");
74 } catch (Exception e) { }
77 @Test
78 public void testTestCipher() {
79 Configuration conf = HBaseConfiguration.create();
80 conf.set(HConstants.CRYPTO_KEYPROVIDER_CONF_KEY, KeyProviderForTesting.class.getName());
81 String algorithm =
82 conf.get(HConstants.CRYPTO_KEY_ALGORITHM_CONF_KEY, HConstants.CIPHER_AES);
83 try {
84 EncryptionTest.testEncryption(conf, algorithm, null);
85 } catch (Exception e) {
86 fail("Test for cipher " + algorithm + " should have succeeded");
88 try {
89 EncryptionTest.testEncryption(conf, "foobar", null);
90 fail("Test for bogus cipher should have failed");
91 } catch (Exception e) { }
94 public static class FailingKeyProvider implements KeyProvider {
96 @Override
97 public void init(String params) {
98 throw new RuntimeException("BAD!");
101 @Override
102 public Key getKey(String alias) {
103 return null;
106 @Override
107 public Key[] getKeys(String[] aliases) {
108 return null;
113 public static class FailingCipherProvider implements CipherProvider {
115 public FailingCipherProvider() {
116 super();
117 throw new RuntimeException("BAD!");
120 @Override
121 public Configuration getConf() {
122 return null;
125 @Override
126 public void setConf(Configuration conf) {
129 @Override
130 public String getName() {
131 return null;
134 @Override
135 public String[] getSupportedCiphers() {
136 return null;
139 @Override
140 public Cipher getCipher(String name) {
141 return null;