001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.io.crypto;
019
020import static org.junit.Assert.assertEquals;
021import static org.junit.Assert.assertNotNull;
022import static org.junit.Assert.assertTrue;
023
024import java.io.IOException;
025import java.io.InputStream;
026import java.io.OutputStream;
027import java.security.Key;
028import java.util.Arrays;
029import org.apache.hadoop.conf.Configuration;
030import org.apache.hadoop.hbase.HBaseClassTestRule;
031import org.apache.hadoop.hbase.HBaseConfiguration;
032import org.apache.hadoop.hbase.HConstants;
033import org.apache.hadoop.hbase.io.crypto.aes.AES;
034import org.apache.hadoop.hbase.testclassification.MiscTests;
035import org.apache.hadoop.hbase.testclassification.SmallTests;
036import org.junit.ClassRule;
037import org.junit.Test;
038import org.junit.experimental.categories.Category;
039
040@Category({ MiscTests.class, SmallTests.class })
041public class TestCipherProvider {
042
043  @ClassRule
044  public static final HBaseClassTestRule CLASS_RULE =
045    HBaseClassTestRule.forClass(TestCipherProvider.class);
046
047  public static class MyCipherProvider implements CipherProvider {
048    private Configuration conf;
049
050    @Override
051    public Configuration getConf() {
052      return conf;
053    }
054
055    @Override
056    public void setConf(Configuration conf) {
057      this.conf = conf;
058    }
059
060    @Override
061    public String getName() {
062      return MyCipherProvider.class.getName();
063    }
064
065    @Override
066    public String[] getSupportedCiphers() {
067      return new String[] { "TEST" };
068    }
069
070    @Override
071    public Cipher getCipher(String name) {
072      if (name.equals("TEST")) {
073        return new Cipher(this) {
074          @Override
075          public String getName() {
076            return "TEST";
077          }
078
079          @Override
080          public int getKeyLength() {
081            return 0;
082          }
083
084          @Override
085          public int getIvLength() {
086            return 0;
087          }
088
089          @Override
090          public Key getRandomKey() {
091            return null;
092          }
093
094          @Override
095          public Encryptor getEncryptor() {
096            return null;
097          }
098
099          @Override
100          public Decryptor getDecryptor() {
101            return null;
102          }
103
104          @Override
105          public OutputStream createEncryptionStream(OutputStream out, Context context, byte[] iv)
106            throws IOException {
107            return null;
108          }
109
110          @Override
111          public OutputStream createEncryptionStream(OutputStream out, Encryptor encryptor)
112            throws IOException {
113            return null;
114          }
115
116          @Override
117          public InputStream createDecryptionStream(InputStream in, Context context, byte[] iv)
118            throws IOException {
119            return null;
120          }
121
122          @Override
123          public InputStream createDecryptionStream(InputStream in, Decryptor decryptor)
124            throws IOException {
125            return null;
126          }
127        };
128      }
129      return null;
130    }
131  }
132
133  @Test
134  public void testCustomProvider() {
135    Configuration conf = HBaseConfiguration.create();
136    conf.set(HConstants.CRYPTO_CIPHERPROVIDER_CONF_KEY, MyCipherProvider.class.getName());
137    CipherProvider provider = Encryption.getCipherProvider(conf);
138    assertTrue(provider instanceof MyCipherProvider);
139    assertTrue(Arrays.asList(provider.getSupportedCiphers()).contains("TEST"));
140    Cipher a = Encryption.getCipher(conf, "TEST");
141    assertNotNull(a);
142    assertTrue(a.getProvider() instanceof MyCipherProvider);
143    assertEquals("TEST", a.getName());
144    assertEquals(0, a.getKeyLength());
145  }
146
147  @Test
148  public void testDefaultProvider() {
149    Configuration conf = HBaseConfiguration.create();
150    CipherProvider provider = Encryption.getCipherProvider(conf);
151    assertTrue(provider instanceof DefaultCipherProvider);
152    String algorithm = conf.get(HConstants.CRYPTO_KEY_ALGORITHM_CONF_KEY, HConstants.CIPHER_AES);
153    assertTrue(Arrays.asList(provider.getSupportedCiphers()).contains(algorithm));
154    Cipher a = Encryption.getCipher(conf, algorithm);
155    assertNotNull(a);
156    assertTrue(a.getProvider() instanceof DefaultCipherProvider);
157    assertEquals(a.getName(), algorithm);
158    assertEquals(AES.KEY_LENGTH, a.getKeyLength());
159  }
160
161}