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      try {
78        EncryptionTest.testEncryption(conf, "AES", null);
79      } catch (Exception e) {
80        fail("Test for cipher AES should have succeeded");
81      }
82      try {
83        EncryptionTest.testEncryption(conf, "foobar", null);
84        fail("Test for bogus cipher should have failed");
85      } catch (Exception e) { }
86    }
87  
88    public static class FailingKeyProvider implements KeyProvider {
89  
90      @Override
91      public void init(String params) {
92        throw new RuntimeException("BAD!");
93      }
94  
95      @Override
96      public Key getKey(String alias) {
97        return null;
98      }
99  
100     @Override
101     public Key[] getKeys(String[] aliases) {
102       return null;
103     }
104 
105   }
106 
107   public static class FailingCipherProvider implements CipherProvider {
108 
109     public FailingCipherProvider() {
110       super();
111       throw new RuntimeException("BAD!");
112     }
113 
114     @Override
115     public Configuration getConf() {
116       return null;
117     }
118 
119     @Override
120     public void setConf(Configuration conf) {
121     }
122 
123     @Override
124     public String getName() {
125       return null;
126     }
127 
128     @Override
129     public String[] getSupportedCiphers() {
130       return null;
131     }
132 
133     @Override
134     public Cipher getCipher(String name) {
135       return null;
136     }
137     
138   }
139 }