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    @Override
050    public Configuration getConf() {
051      return conf;
052    }
053
054    @Override
055    public void setConf(Configuration conf) {
056      this.conf = conf;
057    }
058
059    @Override
060    public String getName() {
061      return MyCipherProvider.class.getName();
062    }
063
064    @Override
065    public String[] getSupportedCiphers() {
066      return new String[] { "TEST" };
067    }
068
069    @Override
070    public Cipher getCipher(String name) {
071      if (name.equals("TEST")) {
072        return new Cipher(this) {
073          @Override
074          public String getName() {
075            return "TEST";
076          }
077
078          @Override
079          public int getKeyLength() {
080            return 0;
081          }
082
083          @Override
084          public int getIvLength() {
085            return 0;
086          }
087
088          @Override
089          public Key getRandomKey() {
090            return null;
091          }
092
093          @Override
094          public Encryptor getEncryptor() {
095            return null;
096          }
097
098          @Override
099          public Decryptor getDecryptor() {
100            return null;
101          }
102
103          @Override
104          public OutputStream createEncryptionStream(OutputStream out, Context context, byte[] iv)
105              throws IOException {
106            return null;
107          }
108
109          @Override
110          public OutputStream createEncryptionStream(OutputStream out, Encryptor encryptor)
111              throws IOException {
112            return null;
113          }
114
115          @Override
116          public InputStream createDecryptionStream(InputStream in, Context context, byte[] iv)
117              throws IOException {
118            return null;
119          }
120
121          @Override
122          public InputStream createDecryptionStream(InputStream in, Decryptor decryptor)
123              throws IOException {
124            return null;
125          }
126        };
127      }
128      return null;
129    }
130  }
131
132  @Test
133  public void testCustomProvider() {
134    Configuration conf = HBaseConfiguration.create();
135    conf.set(HConstants.CRYPTO_CIPHERPROVIDER_CONF_KEY, MyCipherProvider.class.getName());
136    CipherProvider provider = Encryption.getCipherProvider(conf);
137    assertTrue(provider instanceof MyCipherProvider);
138    assertTrue(Arrays.asList(provider.getSupportedCiphers()).contains("TEST"));
139    Cipher a = Encryption.getCipher(conf, "TEST");
140    assertNotNull(a);
141    assertTrue(a.getProvider() instanceof MyCipherProvider);
142    assertEquals("TEST", a.getName());
143    assertEquals(0, a.getKeyLength());
144  }
145
146  @Test
147  public void testDefaultProvider() {
148    Configuration conf = HBaseConfiguration.create();
149    CipherProvider provider = Encryption.getCipherProvider(conf);
150    assertTrue(provider instanceof DefaultCipherProvider);
151    String algorithm =
152        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}