View Javadoc

1   /*
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   */
19  
20  package org.apache.hadoop.hbase.util;
21  
22  import static org.junit.Assert.fail;
23  
24  import java.security.Key;
25  
26  import org.apache.hadoop.conf.Configuration;
27  import org.apache.hadoop.hbase.HBaseConfiguration;
28  import org.apache.hadoop.hbase.HConstants;
29  import org.apache.hadoop.hbase.io.crypto.Cipher;
30  import org.apache.hadoop.hbase.io.crypto.CipherProvider;
31  import org.apache.hadoop.hbase.io.crypto.DefaultCipherProvider;
32  import org.apache.hadoop.hbase.io.crypto.KeyProvider;
33  import org.apache.hadoop.hbase.io.crypto.KeyProviderForTesting;
34  import org.apache.hadoop.hbase.testclassification.SmallTests;
35  import org.junit.Test;
36  import org.junit.experimental.categories.Category;
37  
38  @Category(SmallTests.class)
39  public class TestEncryptionTest {
40  
41    @Test
42    public void testTestKeyProvider() {
43      Configuration conf = HBaseConfiguration.create();
44      try {
45        conf.set(HConstants.CRYPTO_KEYPROVIDER_CONF_KEY, KeyProviderForTesting.class.getName());
46        EncryptionTest.testKeyProvider(conf);
47      } catch (Exception e) {
48        fail("Instantiation of test key provider should have passed");
49      }
50      try {
51        conf.set(HConstants.CRYPTO_KEYPROVIDER_CONF_KEY, FailingKeyProvider.class.getName());
52        EncryptionTest.testKeyProvider(conf);
53        fail("Instantiation of bad test key provider should have failed check");
54      } catch (Exception e) { }
55    }
56  
57    @Test
58    public void testTestCipherProvider() {
59      Configuration conf = HBaseConfiguration.create();
60      try {
61        conf.set(HConstants.CRYPTO_CIPHERPROVIDER_CONF_KEY, DefaultCipherProvider.class.getName());
62        EncryptionTest.testCipherProvider(conf);
63      } catch (Exception e) {
64        fail("Instantiation of test cipher provider should have passed");
65      }
66      try {
67        conf.set(HConstants.CRYPTO_CIPHERPROVIDER_CONF_KEY, FailingCipherProvider.class.getName());
68        EncryptionTest.testCipherProvider(conf);
69        fail("Instantiation of bad test cipher provider should have failed check");
70      } catch (Exception e) { }
71    }
72  
73    @Test
74    public void testTestCipher() {
75      Configuration conf = HBaseConfiguration.create();
76      conf.set(HConstants.CRYPTO_KEYPROVIDER_CONF_KEY, KeyProviderForTesting.class.getName());
77      String algorithm =
78          conf.get(HConstants.CRYPTO_KEY_ALGORITHM_CONF_KEY, HConstants.CIPHER_AES);
79      try {
80        EncryptionTest.testEncryption(conf, algorithm, null);
81      } catch (Exception e) {
82        fail("Test for cipher " + algorithm + " should have succeeded");
83      }
84      try {
85        EncryptionTest.testEncryption(conf, "foobar", null);
86        fail("Test for bogus cipher should have failed");
87      } catch (Exception e) { }
88    }
89  
90    public static class FailingKeyProvider implements KeyProvider {
91  
92      @Override
93      public void init(String params) {
94        throw new RuntimeException("BAD!");
95      }
96  
97      @Override
98      public Key getKey(String alias) {
99        return null;
100     }
101 
102     @Override
103     public Key[] getKeys(String[] aliases) {
104       return null;
105     }
106 
107   }
108 
109   public static class FailingCipherProvider implements CipherProvider {
110 
111     public FailingCipherProvider() {
112       super();
113       throw new RuntimeException("BAD!");
114     }
115 
116     @Override
117     public Configuration getConf() {
118       return null;
119     }
120 
121     @Override
122     public void setConf(Configuration conf) {
123     }
124 
125     @Override
126     public String getName() {
127       return null;
128     }
129 
130     @Override
131     public String[] getSupportedCiphers() {
132       return null;
133     }
134 
135     @Override
136     public Cipher getCipher(String name) {
137       return null;
138     }
139     
140   }
141 }