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.io.IOException;
25  import java.security.Key;
26  
27  import org.apache.hadoop.conf.Configuration;
28  import org.apache.hadoop.hbase.HBaseConfiguration;
29  import org.apache.hadoop.hbase.HConstants;
30  import org.apache.hadoop.hbase.io.crypto.Cipher;
31  import org.apache.hadoop.hbase.io.crypto.CipherProvider;
32  import org.apache.hadoop.hbase.io.crypto.DefaultCipherProvider;
33  import org.apache.hadoop.hbase.io.crypto.KeyProvider;
34  import org.apache.hadoop.hbase.io.crypto.KeyProviderForTesting;
35  import org.apache.hadoop.hbase.testclassification.SmallTests;
36  import org.junit.Test;
37  import org.junit.experimental.categories.Category;
38  
39  @Category(SmallTests.class)
40  public class TestEncryptionTest {
41  
42    @Test
43    public void testTestKeyProvider() {
44      Configuration conf = HBaseConfiguration.create();
45      try {
46        conf.set(HConstants.CRYPTO_KEYPROVIDER_CONF_KEY, KeyProviderForTesting.class.getName());
47        EncryptionTest.testKeyProvider(conf);
48      } catch (Exception e) {
49        fail("Instantiation of test key provider should have passed");
50      }
51      try {
52        conf.set(HConstants.CRYPTO_KEYPROVIDER_CONF_KEY, FailingKeyProvider.class.getName());
53        EncryptionTest.testKeyProvider(conf);
54        fail("Instantiation of bad test key provider should have failed check");
55      } catch (IOException e) { }
56    }
57  
58    @Test
59    public void testTestCipherProvider() {
60      Configuration conf = HBaseConfiguration.create();
61      try {
62        conf.set(HConstants.CRYPTO_CIPHERPROVIDER_CONF_KEY, DefaultCipherProvider.class.getName());
63        EncryptionTest.testCipherProvider(conf);
64      } catch (Exception e) {
65        fail("Instantiation of test cipher provider should have passed");
66      }
67      try {
68        conf.set(HConstants.CRYPTO_CIPHERPROVIDER_CONF_KEY, FailingCipherProvider.class.getName());
69        EncryptionTest.testCipherProvider(conf);
70        fail("Instantiation of bad test cipher provider should have failed check");
71      } catch (IOException e) { }
72    }
73  
74    @Test
75    public void testTestCipher() {
76      Configuration conf = HBaseConfiguration.create();
77      conf.set(HConstants.CRYPTO_KEYPROVIDER_CONF_KEY, KeyProviderForTesting.class.getName());
78      String algorithm =
79          conf.get(HConstants.CRYPTO_KEY_ALGORITHM_CONF_KEY, HConstants.CIPHER_AES);
80      try {
81        EncryptionTest.testEncryption(conf, algorithm, null);
82      } catch (Exception e) {
83        fail("Test for cipher " + algorithm + " should have succeeded");
84      }
85      try {
86        EncryptionTest.testEncryption(conf, "foobar", null);
87        fail("Test for bogus cipher should have failed");
88      } catch (IOException e) { }
89    }
90  
91    public static class FailingKeyProvider implements KeyProvider {
92  
93      @Override
94      public void init(String params) {
95        throw new RuntimeException("BAD!");
96      }
97  
98      @Override
99      public Key getKey(String alias) {
100       return null;
101     }
102 
103     @Override
104     public Key[] getKeys(String[] aliases) {
105       return null;
106     }
107 
108   }
109 
110   public static class FailingCipherProvider implements CipherProvider {
111 
112     public FailingCipherProvider() {
113       super();
114       throw new RuntimeException("BAD!");
115     }
116 
117     @Override
118     public Configuration getConf() {
119       return null;
120     }
121 
122     @Override
123     public void setConf(Configuration conf) {
124     }
125 
126     @Override
127     public String getName() {
128       return null;
129     }
130 
131     @Override
132     public String[] getSupportedCiphers() {
133       return null;
134     }
135 
136     @Override
137     public Cipher getCipher(String name) {
138       return null;
139     }
140     
141   }
142 }