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
{
42 public static final HBaseClassTestRule CLASS_RULE
=
43 HBaseClassTestRule
.forClass(TestEncryptionTest
.class);
46 public void testTestKeyProvider() {
47 Configuration conf
= HBaseConfiguration
.create();
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");
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
) { }
62 public void testTestCipherProvider() {
63 Configuration conf
= HBaseConfiguration
.create();
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");
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
) { }
78 public void testTestCipher() {
79 Configuration conf
= HBaseConfiguration
.create();
80 conf
.set(HConstants
.CRYPTO_KEYPROVIDER_CONF_KEY
, KeyProviderForTesting
.class.getName());
82 conf
.get(HConstants
.CRYPTO_KEY_ALGORITHM_CONF_KEY
, HConstants
.CIPHER_AES
);
84 EncryptionTest
.testEncryption(conf
, algorithm
, null);
85 } catch (Exception e
) {
86 fail("Test for cipher " + algorithm
+ " should have succeeded");
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
{
97 public void init(String params
) {
98 throw new RuntimeException("BAD!");
102 public Key
getKey(String alias
) {
107 public Key
[] getKeys(String
[] aliases
) {
113 public static class FailingCipherProvider
implements CipherProvider
{
115 public FailingCipherProvider() {
117 throw new RuntimeException("BAD!");
121 public Configuration
getConf() {
126 public void setConf(Configuration conf
) {
130 public String
getName() {
135 public String
[] getSupportedCiphers() {
140 public Cipher
getCipher(String name
) {