View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements. See the NOTICE file distributed with this
4    * work for additional information regarding copyright ownership. The ASF
5    * licenses this file to you under the Apache License, Version 2.0 (the
6    * "License"); you may not use this file except in compliance with the License.
7    * You may obtain a copy of the License at
8    *
9    * http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14   * License for the specific language governing permissions and limitations under
15   * the License.
16   */
17  package org.apache.hadoop.hbase.io.crypto;
18  
19  import static org.junit.Assert.assertEquals;
20  import static org.junit.Assert.assertNotNull;
21  import static org.junit.Assert.assertTrue;
22  
23  import java.security.Key;
24  
25  import org.apache.hadoop.conf.Configuration;
26  import org.apache.hadoop.hbase.HBaseConfiguration;
27  import org.apache.hadoop.hbase.HConstants;
28  import org.apache.hadoop.hbase.io.crypto.aes.AES;
29  
30  import org.apache.hadoop.hbase.testclassification.SmallTests;
31  import org.junit.Test;
32  import org.junit.experimental.categories.Category;
33  
34  @Category(SmallTests.class)
35  public class TestKeyProvider {
36  
37    @Test
38    public void testTestProvider() {
39      Configuration conf = HBaseConfiguration.create();
40      conf.set(HConstants.CRYPTO_KEYPROVIDER_CONF_KEY, KeyProviderForTesting.class.getName());
41      KeyProvider provider = Encryption.getKeyProvider(conf);
42      assertNotNull("Null returned for provider", provider);
43      assertTrue("Provider is not the expected type", provider instanceof KeyProviderForTesting);
44  
45      Key key = provider.getKey("foo");
46      assertNotNull("Test provider did not return a key as expected", key);
47      assertEquals("Test provider did not create a key for AES", key.getAlgorithm(), "AES");
48      assertEquals("Test provider did not create a key of adequate length",
49        key.getEncoded().length, AES.KEY_LENGTH);
50    }
51  
52  }