1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.hadoop.hbase.io.crypto;
18
19 import static org.junit.Assert.*;
20
21 import java.io.IOException;
22 import java.io.InputStream;
23 import java.io.OutputStream;
24 import java.security.Key;
25 import java.util.Arrays;
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.SmallTests;
31 import org.apache.hadoop.hbase.io.crypto.aes.AES;
32
33 import org.junit.Test;
34 import org.junit.experimental.categories.Category;
35
36 @Category(SmallTests.class)
37 public class TestCipherProvider {
38
39 public static class MyCipherProvider implements CipherProvider {
40 private Configuration conf;
41 @Override
42 public Configuration getConf() {
43 return conf;
44 }
45
46 @Override
47 public void setConf(Configuration conf) {
48 this.conf = conf;
49 }
50
51 @Override
52 public String getName() {
53 return MyCipherProvider.class.getName();
54 }
55
56 @Override
57 public String[] getSupportedCiphers() {
58 return new String[] { "TEST" };
59 }
60
61 @Override
62 public Cipher getCipher(String name) {
63 if (name.equals("TEST")) {
64 return new Cipher(this) {
65 @Override
66 public String getName() {
67 return "TEST";
68 }
69
70 @Override
71 public int getKeyLength() {
72 return 0;
73 }
74
75 @Override
76 public int getIvLength() {
77 return 0;
78 }
79
80 @Override
81 public Key getRandomKey() {
82 return null;
83 }
84
85 @Override
86 public Encryptor getEncryptor() {
87 return null;
88 }
89
90 @Override
91 public Decryptor getDecryptor() {
92 return null;
93 }
94
95 @Override
96 public OutputStream createEncryptionStream(OutputStream out, Context context, byte[] iv)
97 throws IOException {
98 return null;
99 }
100
101 @Override
102 public OutputStream createEncryptionStream(OutputStream out, Encryptor encryptor)
103 throws IOException {
104 return null;
105 }
106
107 @Override
108 public InputStream createDecryptionStream(InputStream in, Context context, byte[] iv)
109 throws IOException {
110 return null;
111 }
112
113 @Override
114 public InputStream createDecryptionStream(InputStream in, Decryptor decryptor)
115 throws IOException {
116 return null;
117 }
118 };
119 }
120 return null;
121 }
122 }
123
124 @Test
125 public void testCustomProvider() {
126 Configuration conf = HBaseConfiguration.create();
127 conf.set(HConstants.CRYPTO_CIPHERPROVIDER_CONF_KEY, MyCipherProvider.class.getName());
128 CipherProvider provider = Encryption.getCipherProvider(conf);
129 assertTrue(provider instanceof MyCipherProvider);
130 assertTrue(Arrays.asList(provider.getSupportedCiphers()).contains("TEST"));
131 Cipher a = Encryption.getCipher(conf, "TEST");
132 assertNotNull(a);
133 assertTrue(a.getProvider() instanceof MyCipherProvider);
134 assertEquals(a.getName(), "TEST");
135 assertEquals(a.getKeyLength(), 0);
136 }
137
138 @Test
139 public void testDefaultProvider() {
140 Configuration conf = HBaseConfiguration.create();
141 CipherProvider provider = Encryption.getCipherProvider(conf);
142 assertTrue(provider instanceof DefaultCipherProvider);
143 assertTrue(Arrays.asList(provider.getSupportedCiphers()).contains("AES"));
144 Cipher a = Encryption.getCipher(conf, "AES");
145 assertNotNull(a);
146 assertTrue(a.getProvider() instanceof DefaultCipherProvider);
147 assertEquals(a.getName(), "AES");
148 assertEquals(a.getKeyLength(), AES.KEY_LENGTH);
149 }
150
151 }