View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   * http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.hadoop.hbase.security.access;
19  
20  import static org.junit.Assert.*;
21  
22  import java.io.ByteArrayInputStream;
23  import java.io.ByteArrayOutputStream;
24  import java.io.DataInput;
25  import java.io.DataInputStream;
26  import java.io.DataOutput;
27  import java.io.DataOutputStream;
28  import java.io.IOException;
29  import java.io.Serializable;
30  import java.lang.reflect.Array;
31  import java.util.List;
32  import java.util.NavigableSet;
33  
34  import org.apache.hadoop.conf.Configuration;
35  import org.apache.hadoop.hbase.ClusterStatus;
36  import org.apache.hadoop.hbase.HBaseConfiguration;
37  import org.apache.hadoop.hbase.HColumnDescriptor;
38  import org.apache.hadoop.hbase.HConstants;
39  import org.apache.hadoop.hbase.HRegionInfo;
40  import org.apache.hadoop.hbase.HTableDescriptor;
41  import org.apache.hadoop.hbase.KeyValue;
42  import org.apache.hadoop.hbase.client.Action;
43  import org.apache.hadoop.hbase.client.Append;
44  import org.apache.hadoop.hbase.client.Delete;
45  import org.apache.hadoop.hbase.client.Get;
46  import org.apache.hadoop.hbase.client.Increment;
47  import org.apache.hadoop.hbase.client.MultiAction;
48  import org.apache.hadoop.hbase.client.MultiResponse;
49  import org.apache.hadoop.hbase.client.Put;
50  import org.apache.hadoop.hbase.client.Result;
51  import org.apache.hadoop.hbase.client.Row;
52  import org.apache.hadoop.hbase.client.RowMutations;
53  import org.apache.hadoop.hbase.client.Scan;
54  import org.apache.hadoop.hbase.filter.BinaryComparator;
55  import org.apache.hadoop.hbase.filter.BitComparator;
56  import org.apache.hadoop.hbase.filter.ColumnCountGetFilter;
57  import org.apache.hadoop.hbase.filter.ColumnPrefixFilter;
58  import org.apache.hadoop.hbase.filter.ColumnRangeFilter;
59  import org.apache.hadoop.hbase.filter.CompareFilter;
60  import org.apache.hadoop.hbase.filter.CompareFilter.CompareOp;
61  import org.apache.hadoop.hbase.filter.DependentColumnFilter;
62  import org.apache.hadoop.hbase.filter.FirstKeyOnlyFilter;
63  import org.apache.hadoop.hbase.filter.InclusiveStopFilter;
64  import org.apache.hadoop.hbase.filter.KeyOnlyFilter;
65  import org.apache.hadoop.hbase.filter.PageFilter;
66  import org.apache.hadoop.hbase.filter.PrefixFilter;
67  import org.apache.hadoop.hbase.filter.QualifierFilter;
68  import org.apache.hadoop.hbase.filter.RandomRowFilter;
69  import org.apache.hadoop.hbase.filter.RowFilter;
70  import org.apache.hadoop.hbase.filter.SingleColumnValueExcludeFilter;
71  import org.apache.hadoop.hbase.filter.SingleColumnValueFilter;
72  import org.apache.hadoop.hbase.filter.SkipFilter;
73  import org.apache.hadoop.hbase.filter.ValueFilter;
74  import org.apache.hadoop.hbase.filter.WhileMatchFilter;
75  import org.apache.hadoop.hbase.regionserver.HRegion;
76  import org.apache.hadoop.hbase.regionserver.RegionOpeningState;
77  import org.apache.hadoop.hbase.regionserver.wal.HLog;
78  import org.apache.hadoop.hbase.regionserver.wal.HLogKey;
79  import org.apache.hadoop.hbase.testclassification.SmallTests;
80  import org.apache.hadoop.io.MapWritable;
81  import org.apache.hadoop.io.Text;
82  import org.apache.hadoop.io.Writable;
83  import org.junit.Test;
84  import org.junit.experimental.categories.Category;
85  
86  import com.google.protobuf.Message;
87  
88  @Category({SmallTests.class})
89  public class TestHbaseObjectWritableFor96Migration {
90  
91    @Test
92    public void testCustomWritable() throws Exception {
93      Configuration conf = HBaseConfiguration.create();
94      // test proper serialization of un-encoded custom writables
95      CustomWritable custom = new CustomWritable("test phrase");
96      Object obj = doType(conf, custom, CustomWritable.class);
97      assertTrue(obj instanceof Writable);
98      assertTrue(obj instanceof CustomWritable);
99      assertEquals("test phrase", ((CustomWritable)obj).getValue());
100   }
101 
102   @Test
103   public void testCustomSerializable() throws Exception {
104     Configuration conf = HBaseConfiguration.create();
105   
106     Configuration legacyConf = HBaseConfiguration.create();
107     legacyConf.setBoolean(HConstants.ALLOW_LEGACY_OBJECT_SERIALIZATION_KEY, true);
108 
109     CustomSerializable custom = new CustomSerializable("test phrase");
110 
111     // check that we can't write by default
112     try {
113       writeType(conf, custom, CustomSerializable.class);
114       fail("IOException expected");
115     } catch (IOException e) {
116       // expected
117     }
118 
119     // check that we can't read by default
120     byte[] data = writeType(legacyConf, custom, CustomSerializable.class);
121     try {
122       readType(conf, data);
123       fail("IOException expected");
124     } catch (IOException e) {
125       // expected
126     }
127   }
128 
129   @Test
130   public void testLegacyCustomSerializable() throws Exception {
131     Configuration conf = HBaseConfiguration.create();
132     conf.setBoolean(HConstants.ALLOW_LEGACY_OBJECT_SERIALIZATION_KEY, true);
133     // test proper serialization of un-encoded serialized java objects
134     CustomSerializable custom = new CustomSerializable("test phrase");
135     byte[] data = writeType(conf, custom, CustomSerializable.class);
136     Object obj = readType(conf, data);
137     assertTrue(obj instanceof Serializable);
138     assertTrue(obj instanceof CustomSerializable);
139     assertEquals("test phrase", ((CustomSerializable)obj).getValue());
140   }
141 
142   private Object doType(final Configuration conf, final Object value,
143       final Class<?> clazz)
144   throws IOException {
145     return readType(conf, writeType(conf, value, clazz));
146   }
147 
148   @SuppressWarnings("deprecation")
149   private byte[] writeType(final Configuration conf, final Object value,
150       final Class<?> clazz) throws IOException {
151     ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
152     DataOutputStream out = new DataOutputStream(byteStream);
153     HbaseObjectWritableFor96Migration.writeObject(out, value, clazz, conf);
154     out.close();
155     return byteStream.toByteArray();
156   }
157 
158   @SuppressWarnings("deprecation")
159   private Object readType(final Configuration conf, final byte[] value)
160       throws IOException {
161     DataInputStream dis = new DataInputStream(new ByteArrayInputStream(value));
162     Object product = HbaseObjectWritableFor96Migration.readObject(dis, conf);
163     dis.close();
164     return product;
165   }
166 
167   public static class CustomSerializable implements Serializable {
168     private static final long serialVersionUID = 1048445561865740632L;
169     private String value = null;
170 
171     public CustomSerializable() {
172     }
173 
174     public CustomSerializable(String value) {
175       this.value = value;
176     }
177 
178     public String getValue() {
179       return value;
180     }
181 
182     public void setValue(String value) {
183       this.value = value;
184     }
185 
186   }
187 
188   public static class CustomWritable implements Writable {
189     private String value = null;
190 
191     public CustomWritable() {
192     }
193 
194     public CustomWritable(String val) {
195       this.value = val;
196     }
197 
198     public String getValue() { return value; }
199 
200     @Override
201     public void write(DataOutput out) throws IOException {
202       Text.writeString(out, this.value);
203     }
204 
205     @Override
206     public void readFields(DataInput in) throws IOException {
207       this.value = Text.readString(in);
208     }
209   }
210 
211   /**
212    * Test case to ensure ordering of CODE_TO_CLASS and CLASS_TO_CODE. In the
213    * past, and item was added in the middle of the static initializer, and that
214    * threw off all of the codes after the addition. This unintentionally broke
215    * the wire protocol for clients. The idea behind this test case is that if
216    * you unintentionally change the order, you will get a test failure. If you
217    * are actually intentionally change the order, just update the test case.
218    * This should be a clue to the reviewer that you are doing something to
219    * change the wire protocol.
220    */
221   @SuppressWarnings("deprecation")
222   @Test
223   public void testGetClassCode() throws IOException{
224     // Primitive types
225     assertEquals(1,HbaseObjectWritableFor96Migration.getClassCode(Boolean.TYPE).intValue());
226     assertEquals(2,HbaseObjectWritableFor96Migration.getClassCode(Byte.TYPE).intValue());
227     assertEquals(3,HbaseObjectWritableFor96Migration.getClassCode(Character.TYPE).intValue());
228     assertEquals(4,HbaseObjectWritableFor96Migration.getClassCode(Short.TYPE).intValue());
229     assertEquals(5,HbaseObjectWritableFor96Migration.getClassCode(Integer.TYPE).intValue());
230     assertEquals(6,HbaseObjectWritableFor96Migration.getClassCode(Long.TYPE).intValue());
231     assertEquals(7,HbaseObjectWritableFor96Migration.getClassCode(Float.TYPE).intValue());
232     assertEquals(8,HbaseObjectWritableFor96Migration.getClassCode(Double.TYPE).intValue());
233     assertEquals(9,HbaseObjectWritableFor96Migration.getClassCode(Void.TYPE).intValue());
234 
235     // Other java types
236     assertEquals(10,HbaseObjectWritableFor96Migration.getClassCode(String.class).intValue());
237     assertEquals(11,HbaseObjectWritableFor96Migration.getClassCode(byte [].class).intValue());
238     assertEquals(12,HbaseObjectWritableFor96Migration.getClassCode(byte [][].class).intValue());
239 
240     // Hadoop types
241     assertEquals(13,HbaseObjectWritableFor96Migration.getClassCode(Text.class).intValue());
242     assertEquals(14,HbaseObjectWritableFor96Migration.getClassCode(Writable.class).intValue());
243     assertEquals(15,HbaseObjectWritableFor96Migration.getClassCode(Writable [].class).intValue());
244     //assertEquals(16,HbaseObjectWritableFor96Migration.getClassCode(HbaseMapWritable.class).intValue());
245     // 17 is NullInstance which isn't visible from here
246 
247     // Hbase types
248     assertEquals(18,HbaseObjectWritableFor96Migration.getClassCode(HColumnDescriptor.class).intValue());
249     assertEquals(19,HbaseObjectWritableFor96Migration.getClassCode(HConstants.Modify.class).intValue());
250     // 20 and 21 are place holders for HMsg
251     // 22: Not pushed over the wire
252     // 23: Not pushed over the wire
253     assertEquals(24,HbaseObjectWritableFor96Migration.getClassCode(HRegionInfo.class).intValue());
254     assertEquals(25,HbaseObjectWritableFor96Migration.getClassCode(HRegionInfo[].class).intValue());
255     // 26: Removed
256     // 27: Removed
257     assertEquals(28,HbaseObjectWritableFor96Migration.getClassCode(HTableDescriptor.class).intValue());
258     assertEquals(29,HbaseObjectWritableFor96Migration.getClassCode(MapWritable.class).intValue());
259 
260     // HBASE-880
261     assertEquals(30,HbaseObjectWritableFor96Migration.getClassCode(ClusterStatus.class).intValue());
262     assertEquals(31,HbaseObjectWritableFor96Migration.getClassCode(Delete.class).intValue());
263     assertEquals(32,HbaseObjectWritableFor96Migration.getClassCode(Get.class).intValue());
264     assertEquals(33,HbaseObjectWritableFor96Migration.getClassCode(KeyValue.class).intValue());
265     assertEquals(34,HbaseObjectWritableFor96Migration.getClassCode(KeyValue[].class).intValue());
266     assertEquals(35,HbaseObjectWritableFor96Migration.getClassCode(Put.class).intValue());
267     assertEquals(36,HbaseObjectWritableFor96Migration.getClassCode(Put[].class).intValue());
268     assertEquals(37,HbaseObjectWritableFor96Migration.getClassCode(Result.class).intValue());
269     assertEquals(38,HbaseObjectWritableFor96Migration.getClassCode(Result[].class).intValue());
270     assertEquals(39,HbaseObjectWritableFor96Migration.getClassCode(Scan.class).intValue());
271 
272     assertEquals(40,HbaseObjectWritableFor96Migration.getClassCode(WhileMatchFilter.class).intValue());
273     assertEquals(41,HbaseObjectWritableFor96Migration.getClassCode(PrefixFilter.class).intValue());
274     assertEquals(42,HbaseObjectWritableFor96Migration.getClassCode(PageFilter.class).intValue());
275     assertEquals(43,HbaseObjectWritableFor96Migration.getClassCode(InclusiveStopFilter.class).intValue());
276     assertEquals(44,HbaseObjectWritableFor96Migration.getClassCode(ColumnCountGetFilter.class).intValue());
277     assertEquals(45,HbaseObjectWritableFor96Migration.getClassCode(SingleColumnValueFilter.class).intValue());
278     assertEquals(46,HbaseObjectWritableFor96Migration.getClassCode(SingleColumnValueExcludeFilter.class).intValue());
279     assertEquals(47,HbaseObjectWritableFor96Migration.getClassCode(BinaryComparator.class).intValue());
280     assertEquals(48,HbaseObjectWritableFor96Migration.getClassCode(BitComparator.class).intValue());
281     assertEquals(49,HbaseObjectWritableFor96Migration.getClassCode(CompareFilter.class).intValue());
282     assertEquals(50,HbaseObjectWritableFor96Migration.getClassCode(RowFilter.class).intValue());
283     assertEquals(51,HbaseObjectWritableFor96Migration.getClassCode(ValueFilter.class).intValue());
284     assertEquals(52,HbaseObjectWritableFor96Migration.getClassCode(QualifierFilter.class).intValue());
285     assertEquals(53,HbaseObjectWritableFor96Migration.getClassCode(SkipFilter.class).intValue());
286     // assertEquals(54,HbaseObjectWritableFor96Migration.getClassCode(WritableByteArrayComparable.class).intValue());
287     assertEquals(55,HbaseObjectWritableFor96Migration.getClassCode(FirstKeyOnlyFilter.class).intValue());
288     assertEquals(56,HbaseObjectWritableFor96Migration.getClassCode(DependentColumnFilter.class).intValue());
289 
290     assertEquals(57,HbaseObjectWritableFor96Migration.getClassCode(Delete [].class).intValue());
291 
292     assertEquals(58,HbaseObjectWritableFor96Migration.getClassCode(HLog.Entry.class).intValue());
293     assertEquals(59,HbaseObjectWritableFor96Migration.getClassCode(HLog.Entry[].class).intValue());
294     assertEquals(60,HbaseObjectWritableFor96Migration.getClassCode(HLogKey.class).intValue());
295 
296     assertEquals(61,HbaseObjectWritableFor96Migration.getClassCode(List.class).intValue());
297 
298     assertEquals(62,HbaseObjectWritableFor96Migration.getClassCode(NavigableSet.class).intValue());
299     assertEquals(63,HbaseObjectWritableFor96Migration.getClassCode(ColumnPrefixFilter.class).intValue());
300 
301     // Multi
302     assertEquals(64,HbaseObjectWritableFor96Migration.getClassCode(Row.class).intValue());
303     assertEquals(65,HbaseObjectWritableFor96Migration.getClassCode(Action.class).intValue());
304     assertEquals(66,HbaseObjectWritableFor96Migration.getClassCode(MultiAction.class).intValue());
305     assertEquals(67,HbaseObjectWritableFor96Migration.getClassCode(MultiResponse.class).intValue());
306 
307     // coprocessor execution
308     // assertEquals(68,HbaseObjectWritableFor96Migration.getClassCode(Exec.class).intValue());
309     assertEquals(69,HbaseObjectWritableFor96Migration.getClassCode(Increment.class).intValue());
310 
311     assertEquals(70,HbaseObjectWritableFor96Migration.getClassCode(KeyOnlyFilter.class).intValue());
312 
313     // serializable
314     assertEquals(71,HbaseObjectWritableFor96Migration.getClassCode(Serializable.class).intValue());
315     assertEquals(72,HbaseObjectWritableFor96Migration.getClassCode(RandomRowFilter.class).intValue());
316     assertEquals(73,HbaseObjectWritableFor96Migration.getClassCode(CompareOp.class).intValue());
317     assertEquals(74,HbaseObjectWritableFor96Migration.getClassCode(ColumnRangeFilter.class).intValue());
318     // assertEquals(75,HbaseObjectWritableFor96Migration.getClassCode(HServerLoad.class).intValue());
319     assertEquals(76,HbaseObjectWritableFor96Migration.getClassCode(RegionOpeningState.class).intValue());
320     assertEquals(77,HbaseObjectWritableFor96Migration.getClassCode(HTableDescriptor[].class).intValue());
321     assertEquals(78,HbaseObjectWritableFor96Migration.getClassCode(Append.class).intValue());
322     assertEquals(79,HbaseObjectWritableFor96Migration.getClassCode(RowMutations.class).intValue());
323     assertEquals(80,HbaseObjectWritableFor96Migration.getClassCode(Message.class).intValue());
324 
325     assertEquals(81,HbaseObjectWritableFor96Migration.getClassCode(Array.class).intValue());
326   }
327 
328   /**
329    * This test verifies that additional objects have not been added to the end of the list.
330    * If you are legitimately adding objects, this test will need to be updated, but see the
331    * note on the test above. 
332    */
333   public void testGetNextObjectCode(){
334     assertEquals(83,HbaseObjectWritableFor96Migration.getNextClassCode());
335   }
336 
337 }