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.visibility;
19  
20  import static org.apache.hadoop.hbase.security.visibility.VisibilityConstants.LABELS_TABLE_NAME;
21  import static org.junit.Assert.assertEquals;
22  import static org.junit.Assert.assertTrue;
23  
24  import java.io.IOException;
25  import java.io.InterruptedIOException;
26  import java.security.PrivilegedExceptionAction;
27  import java.util.ArrayList;
28  import java.util.List;
29  
30  import org.apache.hadoop.conf.Configuration;
31  import org.apache.hadoop.hbase.Cell;
32  import org.apache.hadoop.hbase.CellScanner;
33  import org.apache.hadoop.hbase.HBaseTestingUtility;
34  import org.apache.hadoop.hbase.HColumnDescriptor;
35  import org.apache.hadoop.hbase.HConstants;
36  import org.apache.hadoop.hbase.HTableDescriptor;
37  import org.apache.hadoop.hbase.testclassification.MediumTests;
38  import org.apache.hadoop.hbase.TableName;
39  import org.apache.hadoop.hbase.client.Delete;
40  import org.apache.hadoop.hbase.client.Get;
41  import org.apache.hadoop.hbase.client.HBaseAdmin;
42  import org.apache.hadoop.hbase.client.HTable;
43  import org.apache.hadoop.hbase.client.Put;
44  import org.apache.hadoop.hbase.client.Result;
45  import org.apache.hadoop.hbase.client.ResultScanner;
46  import org.apache.hadoop.hbase.client.RetriesExhaustedWithDetailsException;
47  import org.apache.hadoop.hbase.client.Scan;
48  import org.apache.hadoop.hbase.protobuf.generated.VisibilityLabelsProtos.VisibilityLabelsResponse;
49  import org.apache.hadoop.hbase.security.User;
50  import org.apache.hadoop.hbase.util.Bytes;
51  import org.junit.After;
52  import org.junit.AfterClass;
53  import org.junit.BeforeClass;
54  import org.junit.Rule;
55  import org.junit.Test;
56  import org.junit.experimental.categories.Category;
57  import org.junit.rules.TestName;
58  
59  /**
60   * Tests visibility labels with deletes
61   */
62  @Category(MediumTests.class)
63  public class TestVisibilityLabelsWithDeletes {
64    private static final String TOPSECRET = "TOPSECRET";
65    private static final String PUBLIC = "PUBLIC";
66    private static final String PRIVATE = "PRIVATE";
67    private static final String CONFIDENTIAL = "CONFIDENTIAL";
68    private static final String SECRET = "SECRET";
69    public static final HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
70    private static final byte[] row1 = Bytes.toBytes("row1");
71    private static final byte[] row2 = Bytes.toBytes("row2");
72    private final static byte[] fam = Bytes.toBytes("info");
73    private final static byte[] qual = Bytes.toBytes("qual");
74    private final static byte[] qual1 = Bytes.toBytes("qual1");
75    private final static byte[] qual2 = Bytes.toBytes("qual2");
76    private final static byte[] value = Bytes.toBytes("value");
77    private final static byte[] value1 = Bytes.toBytes("value1");
78    public static Configuration conf;
79  
80    @Rule
81    public final TestName TEST_NAME = new TestName();
82    public static User SUPERUSER;
83  
84    @BeforeClass
85    public static void setupBeforeClass() throws Exception {
86      // setup configuration
87      conf = TEST_UTIL.getConfiguration();
88      conf.setBoolean(HConstants.DISTRIBUTED_LOG_REPLAY_KEY, false);
89      VisibilityTestUtil.enableVisiblityLabels(conf);
90      conf.setClass(VisibilityUtils.VISIBILITY_LABEL_GENERATOR_CLASS, SimpleScanLabelGenerator.class,
91          ScanLabelGenerator.class);
92      conf.set("hbase.superuser", "admin");
93      TEST_UTIL.startMiniCluster(2);
94      SUPERUSER = User.createUserForTesting(conf, "admin", new String[] { "supergroup" });
95  
96      // Wait for the labels table to become available
97      TEST_UTIL.waitTableEnabled(LABELS_TABLE_NAME.getName(), 50000);
98      addLabels();
99    }
100 
101   @AfterClass
102   public static void tearDownAfterClass() throws Exception {
103     TEST_UTIL.shutdownMiniCluster();
104   }
105 
106   @After
107   public void tearDown() throws Exception {
108   }
109 
110   @Test
111   public void testVisibilityLabelsWithDeleteColumns() throws Throwable {
112     setAuths();
113     TableName tableName = TableName.valueOf(TEST_NAME.getMethodName());
114     final HTable table = createTableAndWriteDataWithLabels(tableName, SECRET + "&" + TOPSECRET,
115         SECRET);
116     try {
117       PrivilegedExceptionAction<Void> actiona = new PrivilegedExceptionAction<Void>() {
118         public Void run() throws Exception {
119           HTable table = null;
120           try {
121             table = new HTable(conf, TEST_NAME.getMethodName());
122             Delete d = new Delete(row1);
123             d.setCellVisibility(new CellVisibility(TOPSECRET + "&" + SECRET));
124             d.deleteColumns(fam, qual);
125             table.delete(d);
126           } catch (Throwable t) {
127             throw new IOException(t);
128           } finally {
129             table.close();
130           }
131           return null;
132         }
133       };
134       SUPERUSER.runAs(actiona);
135 
136       TEST_UTIL.getHBaseAdmin().flush(tableName.getNameAsString());
137       Scan s = new Scan();
138       s.setAuthorizations(new Authorizations(SECRET, PRIVATE, CONFIDENTIAL));
139       ResultScanner scanner = table.getScanner(s);
140       Result[] next = scanner.next(3);
141       assertTrue(next.length == 1);
142       CellScanner cellScanner = next[0].cellScanner();
143       cellScanner.advance();
144       Cell current = cellScanner.current();
145       assertTrue(Bytes.equals(current.getRowArray(), current.getRowOffset(),
146           current.getRowLength(), row2, 0, row2.length));
147 
148     } finally {
149       if (table != null) {
150         table.close();
151       }
152     }
153   }
154 
155   @Test
156   public void testVisibilityLabelsWithDeleteFamily() throws Exception {
157     setAuths();
158     TableName tableName = TableName.valueOf(TEST_NAME.getMethodName());
159     final HTable table = createTableAndWriteDataWithLabels(tableName, SECRET, CONFIDENTIAL + "|"
160         + TOPSECRET);
161     try {
162       PrivilegedExceptionAction<Void> actiona = new PrivilegedExceptionAction<Void>() {
163         public Void run() throws Exception {
164           try {
165             HTable table = new HTable(conf, TEST_NAME.getMethodName());
166             Delete d = new Delete(row2);
167             d.setCellVisibility(new CellVisibility(TOPSECRET + "|" + CONFIDENTIAL));
168             d.deleteFamily(fam);
169             table.delete(d);
170           } catch (Throwable t) {
171             throw new IOException(t);
172           }
173           return null;
174         }
175       };
176       SUPERUSER.runAs(actiona);
177 
178       TEST_UTIL.getHBaseAdmin().flush(tableName.getNameAsString());
179       Scan s = new Scan();
180       s.setAuthorizations(new Authorizations(SECRET, PRIVATE, CONFIDENTIAL));
181       ResultScanner scanner = table.getScanner(s);
182       Result[] next = scanner.next(3);
183       assertTrue(next.length == 1);
184       CellScanner cellScanner = next[0].cellScanner();
185       cellScanner.advance();
186       Cell current = cellScanner.current();
187       assertTrue(Bytes.equals(current.getRowArray(), current.getRowOffset(),
188           current.getRowLength(), row1, 0, row1.length));
189     } finally {
190       if (table != null) {
191         table.close();
192       }
193     }
194   }
195 
196   @Test
197   public void testVisibilityLabelsWithDeleteFamilyVersion() throws Exception {
198     setAuths();
199     TableName tableName = TableName.valueOf(TEST_NAME.getMethodName());
200     long[] ts = new long[] { 123l, 125l };
201     final HTable table = createTableAndWriteDataWithLabels(tableName, ts, CONFIDENTIAL + "|"
202         + TOPSECRET, SECRET);
203     try {
204       PrivilegedExceptionAction<Void> actiona = new PrivilegedExceptionAction<Void>() {
205         public Void run() throws Exception {
206           HTable table = null;
207           try {
208             table = new HTable(conf, TEST_NAME.getMethodName());
209             Delete d = new Delete(row1);
210             d.setCellVisibility(new CellVisibility(TOPSECRET + "|" + CONFIDENTIAL));
211             d.deleteFamilyVersion(fam, 123l);
212             table.delete(d);
213           } catch (Throwable t) {
214             throw new IOException(t);
215           } finally {
216             table.close();
217           }
218           return null;
219         }
220       };
221       SUPERUSER.runAs(actiona);
222 
223       TEST_UTIL.getHBaseAdmin().flush(tableName.getNameAsString());
224       Scan s = new Scan();
225       s.setAuthorizations(new Authorizations(SECRET, PRIVATE, CONFIDENTIAL));
226       ResultScanner scanner = table.getScanner(s);
227       Result[] next = scanner.next(3);
228       assertTrue(next.length == 1);
229       CellScanner cellScanner = next[0].cellScanner();
230       cellScanner.advance();
231       Cell current = cellScanner.current();
232       assertTrue(Bytes.equals(current.getRowArray(), current.getRowOffset(),
233           current.getRowLength(), row2, 0, row2.length));
234     } finally {
235       if (table != null) {
236         table.close();
237       }
238     }
239   }
240 
241   @Test
242   public void testVisibilityLabelsWithDeleteColumnExactVersion() throws Exception {
243     setAuths();
244     TableName tableName = TableName.valueOf(TEST_NAME.getMethodName());
245     long[] ts = new long[] { 123l, 125l };
246     final HTable table = createTableAndWriteDataWithLabels(tableName, ts, CONFIDENTIAL + "|"
247         + TOPSECRET, SECRET);
248     try {
249       PrivilegedExceptionAction<Void> actiona = new PrivilegedExceptionAction<Void>() {
250         public Void run() throws Exception {
251           HTable table = null;
252           try {
253             table = new HTable(conf, TEST_NAME.getMethodName());
254             Delete d = new Delete(row1);
255             d.setCellVisibility(new CellVisibility(TOPSECRET + "|" + CONFIDENTIAL));
256             d.deleteColumn(fam, qual, 123l);
257             table.delete(d);
258           } catch (Throwable t) {
259             throw new IOException(t);
260           } finally {
261             table.close();
262           }
263           return null;
264         }
265       };
266       SUPERUSER.runAs(actiona);
267 
268       TEST_UTIL.getHBaseAdmin().flush(tableName.getNameAsString());
269       Scan s = new Scan();
270       s.setAuthorizations(new Authorizations(SECRET, PRIVATE, CONFIDENTIAL));
271       ResultScanner scanner = table.getScanner(s);
272       Result[] next = scanner.next(3);
273       assertTrue(next.length == 1);
274       CellScanner cellScanner = next[0].cellScanner();
275       cellScanner.advance();
276       Cell current = cellScanner.current();
277       assertTrue(Bytes.equals(current.getRowArray(), current.getRowOffset(),
278           current.getRowLength(), row2, 0, row2.length));
279     } finally {
280       if (table != null) {
281         table.close();
282       }
283     }
284   }
285 
286   @Test
287   public void testVisibilityLabelsWithDeleteColumnsWithMultipleVersions() throws Exception {
288     setAuths();
289     TableName tableName = TableName.valueOf(TEST_NAME.getMethodName());
290     HTable table = null;
291     try {
292       table = doPuts(tableName);
293       TEST_UTIL.getHBaseAdmin().flush(tableName.getNameAsString());
294       PrivilegedExceptionAction<Void> actiona = new PrivilegedExceptionAction<Void>() {
295         public Void run() throws Exception {
296           try {
297             HTable table = new HTable(conf, TEST_NAME.getMethodName());
298             Delete d = new Delete(row1);
299             d.setCellVisibility(new CellVisibility("(" + PRIVATE + "&" + CONFIDENTIAL + ")|(" +
300                 SECRET + "&" + TOPSECRET+")"));
301             d.deleteColumns(fam, qual, 125l);
302             table.delete(d);
303           } catch (Throwable t) {
304             throw new IOException(t);
305           }
306           return null;
307         }
308       };
309       SUPERUSER.runAs(actiona);
310 
311       TEST_UTIL.getHBaseAdmin().flush(tableName.getNameAsString());
312       Scan s = new Scan();
313       s.setMaxVersions(5);
314       s.setAuthorizations(new Authorizations(SECRET, PRIVATE, CONFIDENTIAL, TOPSECRET));
315       ResultScanner scanner = table.getScanner(s);
316       Result[] next = scanner.next(3);
317       assertTrue(next.length == 2);
318       CellScanner cellScanner = next[0].cellScanner();
319       cellScanner.advance();
320       Cell current = cellScanner.current();
321       assertTrue(Bytes.equals(current.getRowArray(), current.getRowOffset(),
322           current.getRowLength(), row1, 0, row1.length));
323       assertEquals(current.getTimestamp(), 127l);
324       cellScanner.advance();
325       current = cellScanner.current();
326       assertTrue(Bytes.equals(current.getRowArray(), current.getRowOffset(),
327           current.getRowLength(), row1, 0, row1.length));
328       assertEquals(current.getTimestamp(), 126l);
329       cellScanner.advance();
330       current = cellScanner.current();
331       assertTrue(Bytes.equals(current.getRowArray(), current.getRowOffset(),
332           current.getRowLength(), row1, 0, row1.length));
333       assertEquals(current.getTimestamp(), 125l);
334       cellScanner = next[1].cellScanner();
335       cellScanner.advance();
336       current = cellScanner.current();
337       assertTrue(Bytes.equals(current.getRowArray(), current.getRowOffset(),
338           current.getRowLength(), row2, 0, row2.length));
339     } finally {
340       if (table != null) {
341         table.close();
342       }
343     }
344   }
345 
346   @Test
347   public void testVisibilityLabelsWithDeleteColumnsWithMultipleVersionsNoTimestamp()
348       throws Exception {
349     setAuths();
350     TableName tableName = TableName.valueOf(TEST_NAME.getMethodName());
351     HTable table = null;
352     try {
353       table = doPuts(tableName);
354       TEST_UTIL.getHBaseAdmin().flush(tableName.getNameAsString());
355       PrivilegedExceptionAction<Void> actiona = new PrivilegedExceptionAction<Void>() {
356         public Void run() throws Exception {
357           try {
358             HTable table = new HTable(conf, TEST_NAME.getMethodName());
359             Delete d = new Delete(row1);
360             d.setCellVisibility(new CellVisibility(CONFIDENTIAL));
361             d.deleteColumns(fam, qual);
362             table.delete(d);
363 
364             d = new Delete(row1);
365             d.setCellVisibility(new CellVisibility(SECRET + "&" + TOPSECRET));
366             d.deleteColumns(fam, qual);
367             table.delete(d);
368             table.flushCommits();
369 
370             d = new Delete(row1);
371             d.setCellVisibility(new CellVisibility("(" + PRIVATE + "&" + CONFIDENTIAL + ")|("
372                 + SECRET + "&" + TOPSECRET + ")"));
373             d.deleteColumns(fam, qual);
374             table.delete(d);
375             table.flushCommits();
376           } catch (Throwable t) {
377             throw new IOException(t);
378           }
379           return null;
380         }
381       };
382       SUPERUSER.runAs(actiona);
383       Scan s = new Scan();
384       s.setMaxVersions(5);
385       s.setAuthorizations(new Authorizations(SECRET, PRIVATE, CONFIDENTIAL, TOPSECRET));
386       ResultScanner scanner = table.getScanner(s);
387       Result[] next = scanner.next(3);
388       assertTrue(next.length == 1);
389       CellScanner cellScanner = next[0].cellScanner();
390       cellScanner.advance();
391       Cell current = cellScanner.current();
392       assertTrue(Bytes.equals(current.getRowArray(), current.getRowOffset(),
393           current.getRowLength(), row2, 0, row2.length));
394     } finally {
395       if (table != null) {
396         table.close();
397       }
398     }
399   }
400 
401   @Test
402   public void
403     testVisibilityLabelsWithDeleteColumnsWithNoMatchVisExpWithMultipleVersionsNoTimestamp()
404       throws Exception {
405     setAuths();
406     TableName tableName = TableName.valueOf(TEST_NAME.getMethodName());
407     HTable table = null;
408     try {
409       table = doPuts(tableName);
410       TEST_UTIL.getHBaseAdmin().flush(tableName.getNameAsString());
411       PrivilegedExceptionAction<Void> actiona = new PrivilegedExceptionAction<Void>() {
412         public Void run() throws Exception {
413           try {
414             HTable table = new HTable(conf, TEST_NAME.getMethodName());
415             Delete d = new Delete(row1);
416             d.setCellVisibility(new CellVisibility(CONFIDENTIAL));
417             d.deleteColumns(fam, qual);
418             table.delete(d);
419 
420             d = new Delete(row1);
421             d.setCellVisibility(new CellVisibility(SECRET));
422             d.deleteColumns(fam, qual);
423             table.delete(d);
424             table.flushCommits();
425 
426             d = new Delete(row1);
427             d.setCellVisibility(new CellVisibility("(" + PRIVATE + "&" + CONFIDENTIAL + ")|("
428                 + SECRET + "&" + TOPSECRET + ")"));
429             d.deleteColumns(fam, qual);
430             table.delete(d);
431             table.flushCommits();
432           } catch (Throwable t) {
433             throw new IOException(t);
434           }
435           return null;
436         }
437       };
438       SUPERUSER.runAs(actiona);
439       Scan s = new Scan();
440       s.setMaxVersions(5);
441       s.setAuthorizations(new Authorizations(SECRET, PRIVATE, CONFIDENTIAL, TOPSECRET));
442       ResultScanner scanner = table.getScanner(s);
443       Result[] next = scanner.next(3);
444       assertTrue(next.length == 2);
445       CellScanner cellScanner = next[0].cellScanner();
446       cellScanner.advance();
447       Cell current = cellScanner.current();
448       assertTrue(Bytes.equals(current.getRowArray(), current.getRowOffset(),
449           current.getRowLength(), row1, 0, row1.length));
450       cellScanner = next[1].cellScanner();
451       cellScanner.advance();
452       current = cellScanner.current();
453       assertTrue(Bytes.equals(current.getRowArray(), current.getRowOffset(),
454           current.getRowLength(), row2, 0, row2.length));
455     } finally {
456       if (table != null) {
457         table.close();
458       }
459     }
460   }
461 
462   @Test
463   public void testVisibilityLabelsWithDeleteFamilyWithMultipleVersionsNoTimestamp()
464       throws Exception {
465     setAuths();
466     TableName tableName = TableName.valueOf(TEST_NAME.getMethodName());
467     HTable table = null;
468     try {
469       table = doPuts(tableName);
470       TEST_UTIL.getHBaseAdmin().flush(tableName.getNameAsString());
471       PrivilegedExceptionAction<Void> actiona = new PrivilegedExceptionAction<Void>() {
472         public Void run() throws Exception {
473           try {
474             HTable table = new HTable(conf, TEST_NAME.getMethodName());
475             Delete d = new Delete(row1);
476             d.setCellVisibility(new CellVisibility(CONFIDENTIAL));
477             d.deleteFamily(fam);
478             table.delete(d);
479 
480             d = new Delete(row1);
481             d.setCellVisibility(new CellVisibility(SECRET + "&" + TOPSECRET));
482             d.deleteFamily(fam);
483             table.delete(d);
484             table.flushCommits();
485 
486             d = new Delete(row1);
487             d.setCellVisibility(new CellVisibility("(" + PRIVATE + "&" + CONFIDENTIAL + ")|("
488                 + SECRET + "&" + TOPSECRET + ")"));
489             d.deleteFamily(fam);
490             table.delete(d);
491             table.flushCommits();
492           } catch (Throwable t) {
493             throw new IOException(t);
494           }
495           return null;
496         }
497       };
498       SUPERUSER.runAs(actiona);
499       Scan s = new Scan();
500       s.setMaxVersions(5);
501       s.setAuthorizations(new Authorizations(SECRET, PRIVATE, CONFIDENTIAL, TOPSECRET));
502       ResultScanner scanner = table.getScanner(s);
503       Result[] next = scanner.next(3);
504       assertTrue(next.length == 1);
505       CellScanner cellScanner = next[0].cellScanner();
506       cellScanner.advance();
507       Cell current = cellScanner.current();
508       assertTrue(Bytes.equals(current.getRowArray(), current.getRowOffset(),
509           current.getRowLength(), row2, 0, row2.length));
510     } finally {
511       if (table != null) {
512         table.close();
513       }
514     }
515   }
516 
517   @Test
518   public void testDeleteColumnsWithoutAndWithVisibilityLabels() throws Exception {
519     final TableName tableName = TableName.valueOf(TEST_NAME.getMethodName());
520     HBaseAdmin hBaseAdmin = TEST_UTIL.getHBaseAdmin();
521     HColumnDescriptor colDesc = new HColumnDescriptor(fam);
522     HTableDescriptor desc = new HTableDescriptor(tableName);
523     desc.addFamily(colDesc);
524     hBaseAdmin.createTable(desc);
525     HTable table = null;
526     try {
527       table = new HTable(conf, TEST_NAME.getMethodName());
528       Put put = new Put(row1);
529       put.addImmutable(fam, qual, value);
530       put.setCellVisibility(new CellVisibility(CONFIDENTIAL));
531       table.put(put);
532       Delete d = new Delete(row1);
533       // without visibility
534       d.deleteColumns(fam, qual, HConstants.LATEST_TIMESTAMP);
535       table.delete(d);
536       PrivilegedExceptionAction<Void> scanAction = new PrivilegedExceptionAction<Void>() {
537         @Override
538         public Void run() throws Exception {
539           try {
540             HTable table = new HTable(conf, TEST_NAME.getMethodName());
541             Scan s = new Scan();
542             ResultScanner scanner = table.getScanner(s);
543             Result[] next = scanner.next(3);
544             assertEquals(next.length, 1);
545           } catch (Throwable t) {
546             throw new IOException(t);
547           }
548           return null;
549         }
550       };
551       SUPERUSER.runAs(scanAction);
552       d = new Delete(row1);
553       // with visibility
554       d.setCellVisibility(new CellVisibility(CONFIDENTIAL));
555       d.deleteColumns(fam, qual, HConstants.LATEST_TIMESTAMP);
556       table.delete(d);
557       scanAction = new PrivilegedExceptionAction<Void>() {
558         @Override
559         public Void run() throws Exception {
560           try {
561             HTable table = new HTable(conf, TEST_NAME.getMethodName());
562             Scan s = new Scan();
563             ResultScanner scanner = table.getScanner(s);
564             Result[] next = scanner.next(3);
565             assertEquals(next.length, 0);
566           } catch (Throwable t) {
567             throw new IOException(t);
568           }
569           return null;
570         }
571       };
572       SUPERUSER.runAs(scanAction);
573     } finally {
574       if (table != null) {
575         table.close();
576       }
577     }
578   }
579 
580   @Test
581   public void testDeleteColumnsWithAndWithoutVisibilityLabels() throws Exception {
582     final TableName tableName = TableName.valueOf(TEST_NAME.getMethodName());
583     HBaseAdmin hBaseAdmin = TEST_UTIL.getHBaseAdmin();
584     HColumnDescriptor colDesc = new HColumnDescriptor(fam);
585     HTableDescriptor desc = new HTableDescriptor(tableName);
586     desc.addFamily(colDesc);
587     hBaseAdmin.createTable(desc);
588     HTable table = null;
589     try {
590       table = new HTable(conf, TEST_NAME.getMethodName());
591       Put put = new Put(row1);
592       put.addImmutable(fam, qual, value);
593       put.setCellVisibility(new CellVisibility(CONFIDENTIAL));
594       table.put(put);
595       Delete d = new Delete(row1);
596       // with visibility
597       d.setCellVisibility(new CellVisibility(CONFIDENTIAL));
598       d.deleteColumns(fam, qual, HConstants.LATEST_TIMESTAMP);
599       table.delete(d);
600       PrivilegedExceptionAction<Void> scanAction = new PrivilegedExceptionAction<Void>() {
601         @Override
602         public Void run() throws Exception {
603           try {
604             HTable table = new HTable(conf, TEST_NAME.getMethodName());
605             Scan s = new Scan();
606             ResultScanner scanner = table.getScanner(s);
607             Result[] next = scanner.next(3);
608             assertEquals(next.length, 0);
609           } catch (Throwable t) {
610             throw new IOException(t);
611           }
612           return null;
613         }
614       };
615       SUPERUSER.runAs(scanAction);
616       d = new Delete(row1);
617       // without visibility
618       d.deleteColumns(fam, qual, HConstants.LATEST_TIMESTAMP);
619       table.delete(d);
620       scanAction = new PrivilegedExceptionAction<Void>() {
621         @Override
622         public Void run() throws Exception {
623           try {
624             HTable table = new HTable(conf, TEST_NAME.getMethodName());
625             Scan s = new Scan();
626             ResultScanner scanner = table.getScanner(s);
627             Result[] next = scanner.next(3);
628             assertEquals(next.length, 0);
629           } catch (Throwable t) {
630             throw new IOException(t);
631           }
632           return null;
633         }
634       };
635       SUPERUSER.runAs(scanAction);
636     } finally {
637       if (table != null) {
638         table.close();
639       }
640     }
641   }
642 
643   @Test
644   public void testDeleteFamiliesWithoutAndWithVisibilityLabels() throws Exception {
645     final TableName tableName = TableName.valueOf(TEST_NAME.getMethodName());
646     HBaseAdmin hBaseAdmin = TEST_UTIL.getHBaseAdmin();
647     HColumnDescriptor colDesc = new HColumnDescriptor(fam);
648     HTableDescriptor desc = new HTableDescriptor(tableName);
649     desc.addFamily(colDesc);
650     hBaseAdmin.createTable(desc);
651     HTable table = null;
652     try {
653       table = new HTable(conf, TEST_NAME.getMethodName());
654       Put put = new Put(row1);
655       put.addImmutable(fam, qual, value);
656       put.setCellVisibility(new CellVisibility(CONFIDENTIAL));
657       table.put(put);
658       Delete d = new Delete(row1);
659       // without visibility
660       d.deleteFamily(fam);
661       table.delete(d);
662       PrivilegedExceptionAction<Void> scanAction = new PrivilegedExceptionAction<Void>() {
663         @Override
664         public Void run() throws Exception {
665           try {
666             HTable table = new HTable(conf, TEST_NAME.getMethodName());
667             Scan s = new Scan();
668             ResultScanner scanner = table.getScanner(s);
669             Result[] next = scanner.next(3);
670             assertEquals(next.length, 1);
671           } catch (Throwable t) {
672             throw new IOException(t);
673           }
674           return null;
675         }
676       };
677       SUPERUSER.runAs(scanAction);
678       d = new Delete(row1);
679       // with visibility
680       d.setCellVisibility(new CellVisibility(CONFIDENTIAL));
681       d.deleteFamily(fam);
682       table.delete(d);
683       scanAction = new PrivilegedExceptionAction<Void>() {
684         @Override
685         public Void run() throws Exception {
686           try {
687             HTable table = new HTable(conf, TEST_NAME.getMethodName());
688             Scan s = new Scan();
689             ResultScanner scanner = table.getScanner(s);
690             Result[] next = scanner.next(3);
691             assertEquals(next.length, 0);
692           } catch (Throwable t) {
693             throw new IOException(t);
694           }
695           return null;
696         }
697       };
698       SUPERUSER.runAs(scanAction);
699     } finally {
700       if (table != null) {
701         table.close();
702       }
703     }
704   }
705 
706   @Test
707   public void testDeleteFamiliesWithAndWithoutVisibilityLabels() throws Exception {
708     final TableName tableName = TableName.valueOf(TEST_NAME.getMethodName());
709     HBaseAdmin hBaseAdmin = TEST_UTIL.getHBaseAdmin();
710     HColumnDescriptor colDesc = new HColumnDescriptor(fam);
711     HTableDescriptor desc = new HTableDescriptor(tableName);
712     desc.addFamily(colDesc);
713     hBaseAdmin.createTable(desc);
714     HTable table = null;
715     try {
716       table = new HTable(conf, TEST_NAME.getMethodName());
717       Put put = new Put(row1);
718       put.addImmutable(fam, qual, value);
719       put.setCellVisibility(new CellVisibility(CONFIDENTIAL));
720       table.put(put);
721       Delete d = new Delete(row1);
722       d.setCellVisibility(new CellVisibility(CONFIDENTIAL));
723       // with visibility
724       d.deleteFamily(fam);
725       table.delete(d);
726       PrivilegedExceptionAction<Void> scanAction = new PrivilegedExceptionAction<Void>() {
727         @Override
728         public Void run() throws Exception {
729           try {
730             HTable table = new HTable(conf, TEST_NAME.getMethodName());
731             Scan s = new Scan();
732             ResultScanner scanner = table.getScanner(s);
733             Result[] next = scanner.next(3);
734             assertEquals(next.length, 0);
735           } catch (Throwable t) {
736             throw new IOException(t);
737           }
738           return null;
739         }
740       };
741       SUPERUSER.runAs(scanAction);
742       d = new Delete(row1);
743       // without visibility
744       d.deleteFamily(fam);
745       table.delete(d);
746       scanAction = new PrivilegedExceptionAction<Void>() {
747         @Override
748         public Void run() throws Exception {
749           try {
750             HTable table = new HTable(conf, TEST_NAME.getMethodName());
751             Scan s = new Scan();
752             ResultScanner scanner = table.getScanner(s);
753             Result[] next = scanner.next(3);
754             assertEquals(next.length, 0);
755           } catch (Throwable t) {
756             throw new IOException(t);
757           }
758           return null;
759         }
760       };
761       SUPERUSER.runAs(scanAction);
762     } finally {
763       if (table != null) {
764         table.close();
765       }
766     }
767   }
768 
769   @Test
770   public void testDeletesWithoutAndWithVisibilityLabels() throws Exception {
771     final TableName tableName = TableName.valueOf(TEST_NAME.getMethodName());
772     HBaseAdmin hBaseAdmin = TEST_UTIL.getHBaseAdmin();
773     HColumnDescriptor colDesc = new HColumnDescriptor(fam);
774     HTableDescriptor desc = new HTableDescriptor(tableName);
775     desc.addFamily(colDesc);
776     hBaseAdmin.createTable(desc);
777     HTable table = null;
778     try {
779       table = new HTable(conf, TEST_NAME.getMethodName());
780       Put put = new Put(row1);
781       put.addImmutable(fam, qual, value);
782       put.setCellVisibility(new CellVisibility(CONFIDENTIAL));
783       table.put(put);
784       Delete d = new Delete(row1);
785       // without visibility
786       d.deleteColumn(fam, qual);
787       table.delete(d);
788       PrivilegedExceptionAction<Void> scanAction = new PrivilegedExceptionAction<Void>() {
789         @Override
790         public Void run() throws Exception {
791           try {
792             HTable table = new HTable(conf, TEST_NAME.getMethodName());
793             Scan s = new Scan();
794             ResultScanner scanner = table.getScanner(s);
795             // The delete would not be able to apply it because of visibility mismatch
796             Result[] next = scanner.next(3);
797             assertEquals(next.length, 1);
798           } catch (Throwable t) {
799             throw new IOException(t);
800           }
801           return null;
802         }
803       };
804       SUPERUSER.runAs(scanAction);
805       d = new Delete(row1);
806       // with visibility
807       d.setCellVisibility(new CellVisibility(CONFIDENTIAL));
808       d.deleteColumn(fam, qual);
809       table.delete(d);
810       scanAction = new PrivilegedExceptionAction<Void>() {
811         @Override
812         public Void run() throws Exception {
813           try {
814             HTable table = new HTable(conf, TEST_NAME.getMethodName());
815             Scan s = new Scan();
816             ResultScanner scanner = table.getScanner(s);
817             Result[] next = scanner.next(3);
818             // this will alone match
819             assertEquals(next.length, 0);
820           } catch (Throwable t) {
821             throw new IOException(t);
822           }
823           return null;
824         }
825       };
826       SUPERUSER.runAs(scanAction);
827     } finally {
828       if (table != null) {
829         table.close();
830       }
831     }
832   }
833 
834   @Test
835   public void testVisibilityLabelsWithDeleteFamilyWithPutsReAppearing() throws Exception {
836     TableName tableName = TableName.valueOf(TEST_NAME.getMethodName());
837     HTable table = null;
838     try {
839       HBaseAdmin hBaseAdmin = TEST_UTIL.getHBaseAdmin();
840       HColumnDescriptor colDesc = new HColumnDescriptor(fam);
841       colDesc.setMaxVersions(5);
842       HTableDescriptor desc = new HTableDescriptor(tableName);
843       desc.addFamily(colDesc);
844       hBaseAdmin.createTable(desc);
845       table = new HTable(conf, tableName);
846       Put put = new Put(Bytes.toBytes("row1"));
847       put.add(fam, qual, value);
848       put.setCellVisibility(new CellVisibility(CONFIDENTIAL));
849       table.put(put);
850       put = new Put(Bytes.toBytes("row1"));
851       put.add(fam, qual, value);
852       put.setCellVisibility(new CellVisibility(SECRET));
853       table.put(put);
854       TEST_UTIL.getHBaseAdmin().flush(tableName.getNameAsString());
855       PrivilegedExceptionAction<Void> actiona = new PrivilegedExceptionAction<Void>() {
856         public Void run() throws Exception {
857           try {
858             HTable table = new HTable(conf, TEST_NAME.getMethodName());
859             Delete d = new Delete(row1);
860             d.setCellVisibility(new CellVisibility(CONFIDENTIAL));
861             d.deleteFamily(fam);
862             table.delete(d);
863             table.flushCommits();
864           } catch (Throwable t) {
865             throw new IOException(t);
866           }
867           return null;
868         }
869       };
870       SUPERUSER.runAs(actiona);
871       Scan s = new Scan();
872       s.setMaxVersions(5);
873       s.setAuthorizations(new Authorizations(SECRET));
874       ResultScanner scanner = table.getScanner(s);
875       Result[] next = scanner.next(3);
876       assertEquals(next.length, 1);
877       put = new Put(Bytes.toBytes("row1"));
878       put.add(fam, qual, value1);
879       put.setCellVisibility(new CellVisibility(CONFIDENTIAL));
880       table.put(put);
881       actiona = new PrivilegedExceptionAction<Void>() {
882         public Void run() throws Exception {
883           try {
884             HTable table = new HTable(conf, TEST_NAME.getMethodName());
885             Delete d = new Delete(row1);
886             d.setCellVisibility(new CellVisibility(SECRET));
887             d.deleteFamily(fam);
888             table.delete(d);
889             table.flushCommits();
890           } catch (Throwable t) {
891             throw new IOException(t);
892           }
893           return null;
894         }
895       };
896       SUPERUSER.runAs(actiona);
897       s = new Scan();
898       s.setMaxVersions(5);
899       s.setAuthorizations(new Authorizations(CONFIDENTIAL));
900       scanner = table.getScanner(s);
901       next = scanner.next(3);
902       assertEquals(next.length, 1);
903       s = new Scan();
904       s.setMaxVersions(5);
905       s.setAuthorizations(new Authorizations(SECRET));
906       scanner = table.getScanner(s);
907       Result[] next1 = scanner.next(3);
908       assertEquals(next1.length, 0);
909     } finally {
910       if (table != null) {
911         table.close();
912       }
913     }
914   }
915 
916   @Test
917   public void testVisibilityLabelsWithDeleteColumnsWithPutsReAppearing() throws Exception {
918     TableName tableName = TableName.valueOf(TEST_NAME.getMethodName());
919     HTable table = null;
920     try {
921       HBaseAdmin hBaseAdmin = TEST_UTIL.getHBaseAdmin();
922       HColumnDescriptor colDesc = new HColumnDescriptor(fam);
923       colDesc.setMaxVersions(5);
924       HTableDescriptor desc = new HTableDescriptor(tableName);
925       desc.addFamily(colDesc);
926       hBaseAdmin.createTable(desc);
927       table = new HTable(conf, tableName);
928       Put put = new Put(Bytes.toBytes("row1"));
929       put.add(fam, qual, value);
930       put.setCellVisibility(new CellVisibility(CONFIDENTIAL));
931       table.put(put);
932       put = new Put(Bytes.toBytes("row1"));
933       put.add(fam, qual, value);
934       put.setCellVisibility(new CellVisibility(SECRET));
935       table.put(put);
936       TEST_UTIL.getHBaseAdmin().flush(tableName.getNameAsString());
937       PrivilegedExceptionAction<Void> actiona = new PrivilegedExceptionAction<Void>() {
938         public Void run() throws Exception {
939           try {
940             HTable table = new HTable(conf, TEST_NAME.getMethodName());
941             Delete d = new Delete(row1);
942             d.setCellVisibility(new CellVisibility(CONFIDENTIAL));
943             d.deleteColumns(fam, qual);
944             table.delete(d);
945             table.flushCommits();
946           } catch (Throwable t) {
947             throw new IOException(t);
948           }
949           return null;
950         }
951       };
952       SUPERUSER.runAs(actiona);
953       Scan s = new Scan();
954       s.setMaxVersions(5);
955       s.setAuthorizations(new Authorizations(SECRET));
956       ResultScanner scanner = table.getScanner(s);
957       Result[] next = scanner.next(3);
958       assertEquals(next.length, 1);
959       put = new Put(Bytes.toBytes("row1"));
960       put.add(fam, qual, value1);
961       put.setCellVisibility(new CellVisibility(CONFIDENTIAL));
962       table.put(put);
963       actiona = new PrivilegedExceptionAction<Void>() {
964         public Void run() throws Exception {
965           try {
966             HTable table = new HTable(conf, TEST_NAME.getMethodName());
967             Delete d = new Delete(row1);
968             d.setCellVisibility(new CellVisibility(SECRET));
969             d.deleteColumns(fam, qual);
970             table.delete(d);
971             table.flushCommits();
972           } catch (Throwable t) {
973             throw new IOException(t);
974           }
975           return null;
976         }
977       };
978       SUPERUSER.runAs(actiona);
979       s = new Scan();
980       s.setMaxVersions(5);
981       s.setAuthorizations(new Authorizations(CONFIDENTIAL));
982       scanner = table.getScanner(s);
983       next = scanner.next(3);
984       assertEquals(next.length, 1);
985       s = new Scan();
986       s.setMaxVersions(5);
987       s.setAuthorizations(new Authorizations(SECRET));
988       scanner = table.getScanner(s);
989       Result[] next1 = scanner.next(3);
990       assertEquals(next1.length, 0);
991     } finally {
992       if (table != null) {
993         table.close();
994       }
995     }
996   }
997 
998   @Test
999   public void testVisibilityCombinations() throws Exception {
1000     TableName tableName = TableName.valueOf(TEST_NAME.getMethodName());
1001     HTable table = null;
1002     try {
1003       HBaseAdmin hBaseAdmin = TEST_UTIL.getHBaseAdmin();
1004       HColumnDescriptor colDesc = new HColumnDescriptor(fam);
1005       colDesc.setMaxVersions(5);
1006       HTableDescriptor desc = new HTableDescriptor(tableName);
1007       desc.addFamily(colDesc);
1008       hBaseAdmin.createTable(desc);
1009       table = new HTable(conf, tableName);
1010       Put put = new Put(Bytes.toBytes("row1"));
1011       put.add(fam, qual, 123l, value);
1012       put.setCellVisibility(new CellVisibility(CONFIDENTIAL));
1013       table.put(put);
1014       put = new Put(Bytes.toBytes("row1"));
1015       put.add(fam, qual, 124l, value1);
1016       put.setCellVisibility(new CellVisibility(SECRET));
1017       table.put(put);
1018       table.flushCommits();
1019       PrivilegedExceptionAction<Void> actiona = new PrivilegedExceptionAction<Void>() {
1020         public Void run() throws Exception {
1021           try {
1022             HTable table = new HTable(conf, TEST_NAME.getMethodName());
1023             Delete d = new Delete(row1);
1024             d.setCellVisibility(new CellVisibility(SECRET));
1025             d.deleteColumns(fam, qual, 126l);
1026             table.delete(d);
1027 
1028             table = new HTable(conf, TEST_NAME.getMethodName());
1029             d = new Delete(row1);
1030             d.setCellVisibility(new CellVisibility(CONFIDENTIAL));
1031             d.deleteColumn(fam, qual, 123l);
1032             table.delete(d);
1033             table.flushCommits();
1034           } catch (Throwable t) {
1035             throw new IOException(t);
1036           }
1037           return null;
1038         }
1039       };
1040       SUPERUSER.runAs(actiona);
1041       Scan s = new Scan();
1042       s.setMaxVersions(5);
1043       s.setAuthorizations(new Authorizations(CONFIDENTIAL, SECRET));
1044       ResultScanner scanner = table.getScanner(s);
1045       Result[] next = scanner.next(3);
1046       assertEquals(next.length, 0);
1047     } finally {
1048       if (table != null) {
1049         table.close();
1050       }
1051     }
1052   }
1053   @Test
1054   public void testVisibilityLabelsWithDeleteColumnWithSpecificVersionWithPutsReAppearing()
1055       throws Exception {
1056     TableName tableName = TableName.valueOf(TEST_NAME.getMethodName());
1057     HTable table = null;
1058     try {
1059       HBaseAdmin hBaseAdmin = TEST_UTIL.getHBaseAdmin();
1060       HColumnDescriptor colDesc = new HColumnDescriptor(fam);
1061       colDesc.setMaxVersions(5);
1062       HTableDescriptor desc = new HTableDescriptor(tableName);
1063       desc.addFamily(colDesc);
1064       hBaseAdmin.createTable(desc);
1065       table = new HTable(conf, tableName);
1066       Put put = new Put(Bytes.toBytes("row1"));
1067       put.add(fam, qual, 123l, value);
1068       put.setCellVisibility(new CellVisibility(CONFIDENTIAL));
1069       table.put(put);
1070       put = new Put(Bytes.toBytes("row1"));
1071       put.add(fam, qual, 123l, value1);
1072       put.setCellVisibility(new CellVisibility(SECRET));
1073       table.put(put);
1074       table.flushCommits();
1075       //TEST_UTIL.getHBaseAdmin().flush(tableName.getNameAsString());
1076       Scan s = new Scan();
1077       s.setMaxVersions(5);
1078       s.setAuthorizations(new Authorizations(CONFIDENTIAL, SECRET));
1079       ResultScanner scanner = table.getScanner(s);
1080       Result[] next = scanner.next(3);
1081       assertEquals(next.length, 1);
1082       PrivilegedExceptionAction<Void> actiona = new PrivilegedExceptionAction<Void>() {
1083         public Void run() throws Exception {
1084           try {
1085             HTable table = new HTable(conf, TEST_NAME.getMethodName());
1086             Delete d = new Delete(row1);
1087             d.setCellVisibility(new CellVisibility(CONFIDENTIAL));
1088             d.deleteColumn(fam, qual, 123l);
1089             table.delete(d);
1090 
1091             table = new HTable(conf, TEST_NAME.getMethodName());
1092             d = new Delete(row1);
1093             d.setCellVisibility(new CellVisibility(SECRET));
1094             d.deleteColumn(fam, qual, 123l);
1095             table.delete(d);
1096             table.flushCommits();
1097           } catch (Throwable t) {
1098             throw new IOException(t);
1099           }
1100           return null;
1101         }
1102       };
1103       SUPERUSER.runAs(actiona);
1104       s = new Scan();
1105       s.setMaxVersions(5);
1106       s.setAuthorizations(new Authorizations(CONFIDENTIAL));
1107       scanner = table.getScanner(s);
1108       next = scanner.next(3);
1109       assertEquals(next.length, 0);
1110     } finally {
1111       if (table != null) {
1112         table.close();
1113       }
1114     }
1115   }
1116 
1117   @Test
1118   public void
1119     testVisibilityLabelsWithDeleteFamilyWithNoMatchingVisExpWithMultipleVersionsNoTimestamp()
1120       throws Exception {
1121     setAuths();
1122     TableName tableName = TableName.valueOf(TEST_NAME.getMethodName());
1123     HTable table = null;
1124     try {
1125       table = doPuts(tableName);
1126       TEST_UTIL.getHBaseAdmin().flush(tableName.getNameAsString());
1127       PrivilegedExceptionAction<Void> actiona = new PrivilegedExceptionAction<Void>() {
1128         public Void run() throws Exception {
1129           try {
1130             HTable table = new HTable(conf, TEST_NAME.getMethodName());
1131             Delete d = new Delete(row1);
1132             d.setCellVisibility(new CellVisibility(CONFIDENTIAL));
1133             d.deleteFamily(fam);
1134             table.delete(d);
1135 
1136             d = new Delete(row1);
1137             d.setCellVisibility(new CellVisibility(SECRET));
1138             d.deleteFamily(fam);
1139             table.delete(d);
1140             table.flushCommits();
1141 
1142             d = new Delete(row1);
1143             d.setCellVisibility(new CellVisibility("(" + PRIVATE + "&" + CONFIDENTIAL + ")|("
1144                 + SECRET + "&" + TOPSECRET + ")"));
1145             d.deleteFamily(fam);
1146             table.delete(d);
1147             table.flushCommits();
1148           } catch (Throwable t) {
1149             throw new IOException(t);
1150           }
1151           return null;
1152         }
1153       };
1154       SUPERUSER.runAs(actiona);
1155       Scan s = new Scan();
1156       s.setMaxVersions(5);
1157       s.setAuthorizations(new Authorizations(SECRET, PRIVATE, CONFIDENTIAL, TOPSECRET));
1158       ResultScanner scanner = table.getScanner(s);
1159       Result[] next = scanner.next(3);
1160       assertTrue(next.length == 2);
1161       CellScanner cellScanner = next[0].cellScanner();
1162       cellScanner.advance();
1163       Cell current = cellScanner.current();
1164       assertTrue(Bytes.equals(current.getRowArray(), current.getRowOffset(),
1165           current.getRowLength(), row1, 0, row1.length));
1166       cellScanner = next[1].cellScanner();
1167       cellScanner.advance();
1168       current = cellScanner.current();
1169       assertTrue(Bytes.equals(current.getRowArray(), current.getRowOffset(),
1170           current.getRowLength(), row2, 0, row2.length));
1171     } finally {
1172       if (table != null) {
1173         table.close();
1174       }
1175     }
1176   }
1177 
1178   @Test
1179   public void testDeleteFamilyAndDeleteColumnsWithAndWithoutVisibilityExp() throws Exception {
1180     setAuths();
1181     TableName tableName = TableName.valueOf(TEST_NAME.getMethodName());
1182     HTable table = null;
1183     try {
1184       table = doPuts(tableName);
1185       TEST_UTIL.getHBaseAdmin().flush(tableName.getNameAsString());
1186       PrivilegedExceptionAction<Void> actiona = new PrivilegedExceptionAction<Void>() {
1187         public Void run() throws Exception {
1188           try {
1189             HTable table = new HTable(conf, TEST_NAME.getMethodName());
1190             Delete d = new Delete(row1);
1191             d.deleteFamily(fam);
1192             table.delete(d);
1193 
1194             d = new Delete(row1);
1195             d.setCellVisibility(new CellVisibility(SECRET + "&" + TOPSECRET));
1196             d.deleteColumns(fam, qual);
1197             table.delete(d);
1198             table.flushCommits();
1199           } catch (Throwable t) {
1200             throw new IOException(t);
1201           }
1202           return null;
1203         }
1204       };
1205       SUPERUSER.runAs(actiona);
1206       Scan s = new Scan();
1207       s.setMaxVersions(5);
1208       s.setAuthorizations(new Authorizations(SECRET, PRIVATE, CONFIDENTIAL, TOPSECRET));
1209       ResultScanner scanner = table.getScanner(s);
1210       Result[] next = scanner.next(3);
1211       assertTrue(next.length == 2);
1212       CellScanner cellScanner = next[0].cellScanner();
1213       cellScanner.advance();
1214       Cell current = cellScanner.current();
1215       assertTrue(Bytes.equals(current.getRowArray(), current.getRowOffset(),
1216           current.getRowLength(), row1, 0, row1.length));
1217       assertEquals(current.getTimestamp(), 127l);
1218       cellScanner.advance();
1219       current = cellScanner.current();
1220       assertTrue(Bytes.equals(current.getRowArray(), current.getRowOffset(),
1221           current.getRowLength(), row1, 0, row1.length));
1222       assertEquals(current.getTimestamp(), 126l);
1223       cellScanner.advance();
1224       current = cellScanner.current();
1225       assertTrue(Bytes.equals(current.getRowArray(), current.getRowOffset(),
1226           current.getRowLength(), row1, 0, row1.length));
1227       assertEquals(current.getTimestamp(), 124l);
1228       cellScanner.advance();
1229       current = cellScanner.current();
1230       assertTrue(Bytes.equals(current.getRowArray(), current.getRowOffset(),
1231           current.getRowLength(), row1, 0, row1.length));
1232       assertEquals(current.getTimestamp(), 123l);
1233       cellScanner = next[1].cellScanner();
1234       cellScanner.advance();
1235       current = cellScanner.current();
1236       assertTrue(Bytes.equals(current.getRowArray(), current.getRowOffset(),
1237           current.getRowLength(), row2, 0, row2.length));
1238     } finally {
1239       if (table != null) {
1240         table.close();
1241       }
1242     }
1243   }
1244 
1245   private HTable doPuts(TableName tableName) throws IOException, InterruptedIOException,
1246       RetriesExhaustedWithDetailsException, InterruptedException {
1247     HTable table;
1248     HBaseAdmin hBaseAdmin = TEST_UTIL.getHBaseAdmin();
1249     HColumnDescriptor colDesc = new HColumnDescriptor(fam);
1250     colDesc.setMaxVersions(5);
1251     HTableDescriptor desc = new HTableDescriptor(tableName);
1252     desc.addFamily(colDesc);
1253     hBaseAdmin.createTable(desc);
1254     table = new HTable(conf, tableName);
1255     Put put = new Put(Bytes.toBytes("row1"));
1256     put.add(fam, qual, 123l, value);
1257     put.setCellVisibility(new CellVisibility(CONFIDENTIAL));
1258     table.put(put);
1259     put = new Put(Bytes.toBytes("row1"));
1260     put.add(fam, qual, 124l, value);
1261     put.setCellVisibility(new CellVisibility("(" + CONFIDENTIAL + "&" + PRIVATE + ")|("
1262     + TOPSECRET + "&" + SECRET+")"));
1263     table.put(put);
1264     put = new Put(Bytes.toBytes("row1"));
1265     put.add(fam, qual, 125l, value);
1266     put.setCellVisibility(new CellVisibility(SECRET + "&" + TOPSECRET));
1267     table.put(put);
1268     put = new Put(Bytes.toBytes("row1"));
1269     put.add(fam, qual, 126l, value);
1270     put.setCellVisibility(new CellVisibility("(" + CONFIDENTIAL + "&" + PRIVATE + ")|("
1271         + TOPSECRET + "&" + SECRET+")"));
1272     table.put(put);
1273     put = new Put(Bytes.toBytes("row1"));
1274     put.add(fam, qual, 127l, value);
1275     put.setCellVisibility(new CellVisibility("(" + CONFIDENTIAL + "&" + PRIVATE + ")|("
1276         + TOPSECRET + "&" + SECRET+")"));
1277     table.put(put);
1278     TEST_UTIL.getHBaseAdmin().flush(tableName.getNameAsString());
1279     put = new Put(Bytes.toBytes("row2"));
1280     put.add(fam, qual, 127l, value);
1281     put.setCellVisibility(new CellVisibility("(" + CONFIDENTIAL + "&" + PRIVATE + ")|(" + TOPSECRET
1282         + "&" + SECRET + ")"));
1283     table.put(put);
1284     return table;
1285   }
1286 
1287   private HTable doPutsWithDiffCols(TableName tableName) throws IOException,
1288       InterruptedIOException, RetriesExhaustedWithDetailsException, InterruptedException {
1289     HTable table;
1290     HBaseAdmin hBaseAdmin = TEST_UTIL.getHBaseAdmin();
1291     HColumnDescriptor colDesc = new HColumnDescriptor(fam);
1292     colDesc.setMaxVersions(5);
1293     HTableDescriptor desc = new HTableDescriptor(tableName);
1294     desc.addFamily(colDesc);
1295     hBaseAdmin.createTable(desc);
1296     table = new HTable(conf, tableName);
1297     Put put = new Put(Bytes.toBytes("row1"));
1298     put.add(fam, qual, 123l, value);
1299     put.setCellVisibility(new CellVisibility(CONFIDENTIAL));
1300     table.put(put);
1301     put = new Put(Bytes.toBytes("row1"));
1302     put.add(fam, qual, 124l, value);
1303     put.setCellVisibility(new CellVisibility("(" + CONFIDENTIAL + "&" + PRIVATE + ")|("
1304     + TOPSECRET + "&" + SECRET+")"));
1305     table.put(put);
1306     put = new Put(Bytes.toBytes("row1"));
1307     put.add(fam, qual, 125l, value);
1308     put.setCellVisibility(new CellVisibility(SECRET + "&" + TOPSECRET));
1309     table.put(put);
1310     put = new Put(Bytes.toBytes("row1"));
1311     put.add(fam, qual1, 126l, value);
1312     put.setCellVisibility(new CellVisibility(SECRET + "&" + TOPSECRET));
1313     table.put(put);
1314     put = new Put(Bytes.toBytes("row1"));
1315     put.add(fam, qual2, 127l, value);
1316     put.setCellVisibility(new CellVisibility("(" + CONFIDENTIAL + "&" + PRIVATE + ")|("
1317         + TOPSECRET + "&" + SECRET+")"));
1318     table.put(put);
1319     return table;
1320   }
1321 
1322   private HTable doPutsWithoutVisibility(TableName tableName) throws IOException,
1323       InterruptedIOException, RetriesExhaustedWithDetailsException, InterruptedException {
1324     HTable table;
1325     HBaseAdmin hBaseAdmin = TEST_UTIL.getHBaseAdmin();
1326     HColumnDescriptor colDesc = new HColumnDescriptor(fam);
1327     colDesc.setMaxVersions(5);
1328     HTableDescriptor desc = new HTableDescriptor(tableName);
1329     desc.addFamily(colDesc);
1330     hBaseAdmin.createTable(desc);
1331     table = new HTable(conf, tableName);
1332     Put put = new Put(Bytes.toBytes("row1"));
1333     put.add(fam, qual, 123l, value);
1334     table.put(put);
1335     put = new Put(Bytes.toBytes("row1"));
1336     put.add(fam, qual, 124l, value);
1337     table.put(put);
1338     put = new Put(Bytes.toBytes("row1"));
1339     put.add(fam, qual, 125l, value);
1340     table.put(put);
1341     put = new Put(Bytes.toBytes("row1"));
1342     put.add(fam, qual, 126l, value);
1343     table.put(put);
1344     put = new Put(Bytes.toBytes("row1"));
1345     put.add(fam, qual, 127l, value);
1346     table.put(put);
1347     TEST_UTIL.getHBaseAdmin().flush(tableName.getNameAsString());
1348     put = new Put(Bytes.toBytes("row2"));
1349     put.add(fam, qual, 127l, value);
1350     table.put(put);
1351     return table;
1352   }
1353 
1354 
1355   @Test
1356   public void testDeleteColumnWithSpecificTimeStampUsingMultipleVersionsUnMatchingVisExpression()
1357       throws Exception {
1358     setAuths();
1359     TableName tableName = TableName.valueOf(TEST_NAME.getMethodName());
1360     HTable table = null;
1361     try {
1362       table = doPuts(tableName);
1363       TEST_UTIL.getHBaseAdmin().flush(tableName.getNameAsString());
1364       PrivilegedExceptionAction<Void> actiona = new PrivilegedExceptionAction<Void>() {
1365         public Void run() throws Exception {
1366           try {
1367             HTable table = new HTable(conf, TEST_NAME.getMethodName());
1368             Delete d = new Delete(row1);
1369             d.setCellVisibility(new CellVisibility("(" + PRIVATE + "&" + CONFIDENTIAL + ")|(" +
1370                 SECRET + "&" + TOPSECRET+")"));
1371             d.deleteColumn(fam, qual, 125l);
1372             table.delete(d);
1373           } catch (Throwable t) {
1374             throw new IOException(t);
1375           }
1376           return null;
1377         }
1378       };
1379       SUPERUSER.runAs(actiona);
1380 
1381       TEST_UTIL.getHBaseAdmin().flush(tableName.getNameAsString());
1382       Scan s = new Scan();
1383       s.setMaxVersions(5);
1384       s.setAuthorizations(new Authorizations(SECRET, PRIVATE, CONFIDENTIAL, TOPSECRET));
1385       ResultScanner scanner = table.getScanner(s);
1386       Result[] next = scanner.next(3);
1387       assertTrue(next.length == 2);
1388       CellScanner cellScanner = next[0].cellScanner();
1389       cellScanner.advance();
1390       Cell current = cellScanner.current();
1391       assertTrue(Bytes.equals(current.getRowArray(), current.getRowOffset(),
1392           current.getRowLength(), row1, 0, row1.length));
1393       assertEquals(current.getTimestamp(), 127l);
1394       cellScanner.advance();
1395       current = cellScanner.current();
1396       assertTrue(Bytes.equals(current.getRowArray(), current.getRowOffset(),
1397           current.getRowLength(), row1, 0, row1.length));
1398       assertEquals(current.getTimestamp(), 126l);
1399       cellScanner.advance();
1400       current = cellScanner.current();
1401       assertTrue(Bytes.equals(current.getRowArray(), current.getRowOffset(),
1402           current.getRowLength(), row1, 0, row1.length));
1403       assertEquals(current.getTimestamp(), 125l);
1404       cellScanner.advance();
1405       current = cellScanner.current();
1406       assertTrue(Bytes.equals(current.getRowArray(), current.getRowOffset(),
1407           current.getRowLength(), row1, 0, row1.length));
1408       assertEquals(current.getTimestamp(), 124l);
1409       cellScanner.advance();
1410       current = cellScanner.current();
1411       assertTrue(Bytes.equals(current.getRowArray(), current.getRowOffset(),
1412           current.getRowLength(), row1, 0, row1.length));
1413       assertEquals(current.getTimestamp(), 123l);
1414       cellScanner = next[1].cellScanner();
1415       cellScanner.advance();
1416       current = cellScanner.current();
1417       assertTrue(Bytes.equals(current.getRowArray(), current.getRowOffset(),
1418           current.getRowLength(), row2, 0, row2.length));
1419     } finally {
1420       if (table != null) {
1421         table.close();
1422       }
1423     }
1424   }
1425 
1426   @Test
1427   public void testDeleteColumnWithLatestTimeStampUsingMultipleVersions() throws Exception {
1428     setAuths();
1429     TableName tableName = TableName.valueOf(TEST_NAME.getMethodName());
1430     HTable table = null;
1431     try {
1432       table = doPuts(tableName);
1433       TEST_UTIL.getHBaseAdmin().flush(tableName.getNameAsString());
1434       PrivilegedExceptionAction<Void> actiona = new PrivilegedExceptionAction<Void>() {
1435         public Void run() throws Exception {
1436           try {
1437             HTable table = new HTable(conf, TEST_NAME.getMethodName());
1438             Delete d = new Delete(row1);
1439             d.setCellVisibility(new CellVisibility(SECRET + "&" + TOPSECRET));
1440             d.deleteColumn(fam, qual);
1441             table.delete(d);
1442           } catch (Throwable t) {
1443             throw new IOException(t);
1444           }
1445           return null;
1446         }
1447       };
1448       SUPERUSER.runAs(actiona);
1449 
1450       TEST_UTIL.getHBaseAdmin().flush(tableName.getNameAsString());
1451       Scan s = new Scan();
1452       s.setMaxVersions(5);
1453       s.setAuthorizations(new Authorizations(SECRET, PRIVATE, CONFIDENTIAL, TOPSECRET));
1454       ResultScanner scanner = table.getScanner(s);
1455       Result[] next = scanner.next(3);
1456       assertTrue(next.length == 2);
1457       CellScanner cellScanner = next[0].cellScanner();
1458       cellScanner.advance();
1459       Cell current = cellScanner.current();
1460       assertTrue(Bytes.equals(current.getRowArray(), current.getRowOffset(),
1461           current.getRowLength(), row1, 0, row1.length));
1462       assertEquals(current.getTimestamp(), 127l);
1463       cellScanner.advance();
1464       current = cellScanner.current();
1465       assertTrue(Bytes.equals(current.getRowArray(), current.getRowOffset(),
1466           current.getRowLength(), row1, 0, row1.length));
1467       assertEquals(current.getTimestamp(), 126l);
1468       cellScanner.advance();
1469       current = cellScanner.current();
1470       assertTrue(Bytes.equals(current.getRowArray(), current.getRowOffset(),
1471           current.getRowLength(), row1, 0, row1.length));
1472       assertEquals(current.getTimestamp(), 124l);
1473       cellScanner.advance();
1474       current = cellScanner.current();
1475       assertTrue(Bytes.equals(current.getRowArray(), current.getRowOffset(),
1476           current.getRowLength(), row1, 0, row1.length));
1477       assertEquals(current.getTimestamp(), 123l);
1478       cellScanner = next[1].cellScanner();
1479       cellScanner.advance();
1480       current = cellScanner.current();
1481       assertTrue(Bytes.equals(current.getRowArray(), current.getRowOffset(),
1482           current.getRowLength(), row2, 0, row2.length));
1483     } finally {
1484       if (table != null) {
1485         table.close();
1486       }
1487     }
1488   }
1489 
1490   @Test (timeout=180000)
1491   public void testDeleteColumnWithLatestTimeStampWhenNoVersionMatches() throws Exception {
1492     setAuths();
1493     TableName tableName = TableName.valueOf(TEST_NAME.getMethodName());
1494     HTable table = null;
1495     try {
1496       table = doPuts(tableName);
1497       TEST_UTIL.getHBaseAdmin().flush(tableName.getNameAsString());
1498       Put put = new Put(Bytes.toBytes("row1"));
1499       put.add(fam, qual, 128l, value);
1500       put.setCellVisibility(new CellVisibility(TOPSECRET));
1501       table.put(put);
1502       PrivilegedExceptionAction<Void> actiona = new PrivilegedExceptionAction<Void>() {
1503         public Void run() throws Exception {
1504           try {
1505             HTable table = new HTable(conf, TEST_NAME.getMethodName());
1506             Delete d = new Delete(row1);
1507             d.setCellVisibility(new CellVisibility(SECRET ));
1508             d.deleteColumn(fam, qual);
1509             table.delete(d);
1510           } catch (Throwable t) {
1511             throw new IOException(t);
1512           }
1513           return null;
1514         }
1515       };
1516       SUPERUSER.runAs(actiona);
1517 
1518       TEST_UTIL.getHBaseAdmin().flush(tableName.getNameAsString());
1519       Scan s = new Scan();
1520       s.setMaxVersions(5);
1521       s.setAuthorizations(new Authorizations(SECRET, PRIVATE, CONFIDENTIAL, TOPSECRET));
1522       ResultScanner scanner = table.getScanner(s);
1523       Result[] next = scanner.next(3);
1524       assertTrue(next.length == 2);
1525       CellScanner cellScanner = next[0].cellScanner();
1526       cellScanner.advance();
1527       Cell current = cellScanner.current();
1528       assertTrue(Bytes.equals(current.getRowArray(), current.getRowOffset(),
1529           current.getRowLength(), row1, 0, row1.length));
1530       assertEquals(current.getTimestamp(), 128l);
1531       cellScanner.advance();
1532       current = cellScanner.current();
1533       assertTrue(Bytes.equals(current.getRowArray(), current.getRowOffset(),
1534           current.getRowLength(), row1, 0, row1.length));
1535       assertEquals(current.getTimestamp(), 127l);
1536       cellScanner.advance();
1537       current = cellScanner.current();
1538       assertTrue(Bytes.equals(current.getRowArray(), current.getRowOffset(),
1539           current.getRowLength(), row1, 0, row1.length));
1540       assertEquals(current.getTimestamp(), 126l);
1541       cellScanner.advance();
1542       current = cellScanner.current();
1543       assertTrue(Bytes.equals(current.getRowArray(), current.getRowOffset(),
1544           current.getRowLength(), row1, 0, row1.length));
1545       assertEquals(current.getTimestamp(), 125l);
1546       cellScanner.advance();
1547       current = cellScanner.current();
1548       assertTrue(Bytes.equals(current.getRowArray(), current.getRowOffset(),
1549           current.getRowLength(), row1, 0, row1.length));
1550       assertEquals(current.getTimestamp(), 124l);
1551       cellScanner = next[1].cellScanner();
1552       cellScanner.advance();
1553       current = cellScanner.current();
1554       assertTrue(Bytes.equals(current.getRowArray(), current.getRowOffset(),
1555           current.getRowLength(), row2, 0, row2.length));
1556 
1557       put = new Put(Bytes.toBytes("row1"));
1558       put.add(fam, qual, 129l, value);
1559       put.setCellVisibility(new CellVisibility(SECRET));
1560       table.put(put);
1561       table.flushCommits();
1562       TEST_UTIL.getHBaseAdmin().flush(tableName.getNameAsString());
1563       s = new Scan();
1564       s.setMaxVersions(5);
1565       s.setAuthorizations(new Authorizations(SECRET, PRIVATE, CONFIDENTIAL, TOPSECRET));
1566       scanner = table.getScanner(s);
1567       next = scanner.next(3);
1568       assertTrue(next.length == 2);
1569       cellScanner = next[0].cellScanner();
1570       cellScanner.advance();
1571       current = cellScanner.current();
1572       assertTrue(Bytes.equals(current.getRowArray(), current.getRowOffset(),
1573           current.getRowLength(), row1, 0, row1.length));
1574       assertEquals(current.getTimestamp(), 129l);
1575     } finally {
1576       if (table != null) {
1577         table.close();
1578       }
1579     }
1580   }
1581   @Test
1582   public void testDeleteColumnWithLatestTimeStampUsingMultipleVersionsAfterCompaction()
1583       throws Exception {
1584     setAuths();
1585     TableName tableName = TableName.valueOf(TEST_NAME.getMethodName());
1586     HTable table = null;
1587     try {
1588       table = doPuts(tableName);
1589       TEST_UTIL.getHBaseAdmin().flush(tableName.getNameAsString());
1590       PrivilegedExceptionAction<Void> actiona = new PrivilegedExceptionAction<Void>() {
1591         public Void run() throws Exception {
1592           try {
1593             HTable table = new HTable(conf, TEST_NAME.getMethodName());
1594             Delete d = new Delete(row1);
1595             d.setCellVisibility(new CellVisibility(SECRET + "&" + TOPSECRET));
1596             d.deleteColumn(fam, qual);
1597             table.delete(d);
1598           } catch (Throwable t) {
1599             throw new IOException(t);
1600           }
1601           return null;
1602         }
1603       };
1604       SUPERUSER.runAs(actiona);
1605       TEST_UTIL.getHBaseAdmin().flush(tableName.getNameAsString());
1606       Put put = new Put(Bytes.toBytes("row3"));
1607       put.add(fam, qual, 127l, value);
1608       put.setCellVisibility(new CellVisibility(CONFIDENTIAL + "&" + PRIVATE));
1609       table.put(put);
1610       TEST_UTIL.getHBaseAdmin().flush(tableName.getNameAsString());
1611       TEST_UTIL.getHBaseAdmin().majorCompact(tableName.getNameAsString());
1612       // Sleep to ensure compaction happens. Need to do it in a better way
1613       Thread.sleep(5000);
1614       Scan s = new Scan();
1615       s.setMaxVersions(5);
1616       s.setAuthorizations(new Authorizations(SECRET, PRIVATE, CONFIDENTIAL, TOPSECRET));
1617       ResultScanner scanner = table.getScanner(s);
1618       Result[] next = scanner.next(3);
1619       assertTrue(next.length == 3);
1620       CellScanner cellScanner = next[0].cellScanner();
1621       cellScanner.advance();
1622       Cell current = cellScanner.current();
1623       assertTrue(Bytes.equals(current.getRowArray(), current.getRowOffset(),
1624           current.getRowLength(), row1, 0, row1.length));
1625       assertEquals(current.getTimestamp(), 127l);
1626       cellScanner.advance();
1627       current = cellScanner.current();
1628       assertTrue(Bytes.equals(current.getRowArray(), current.getRowOffset(),
1629           current.getRowLength(), row1, 0, row1.length));
1630       assertEquals(current.getTimestamp(), 126l);
1631       cellScanner.advance();
1632       current = cellScanner.current();
1633       assertTrue(Bytes.equals(current.getRowArray(), current.getRowOffset(),
1634           current.getRowLength(), row1, 0, row1.length));
1635       assertEquals(current.getTimestamp(), 124l);
1636       cellScanner.advance();
1637       current = cellScanner.current();
1638       assertTrue(Bytes.equals(current.getRowArray(), current.getRowOffset(),
1639           current.getRowLength(), row1, 0, row1.length));
1640       assertEquals(current.getTimestamp(), 123l);
1641       cellScanner = next[1].cellScanner();
1642       cellScanner.advance();
1643       current = cellScanner.current();
1644       assertTrue(Bytes.equals(current.getRowArray(), current.getRowOffset(),
1645           current.getRowLength(), row2, 0, row2.length));
1646     } finally {
1647       if (table != null) {
1648         table.close();
1649       }
1650     }
1651   }
1652 
1653   @Test
1654   public void testDeleteFamilyLatestTimeStampWithMulipleVersions() throws Exception {
1655     setAuths();
1656     TableName tableName = TableName.valueOf(TEST_NAME.getMethodName());
1657     HTable table = null;
1658     try {
1659       table = doPuts(tableName);
1660       TEST_UTIL.getHBaseAdmin().flush(tableName.getNameAsString());
1661       PrivilegedExceptionAction<Void> actiona = new PrivilegedExceptionAction<Void>() {
1662         public Void run() throws Exception {
1663           try {
1664             HTable table = new HTable(conf, TEST_NAME.getMethodName());
1665             Delete d = new Delete(row1);
1666             d.setCellVisibility(new CellVisibility(SECRET + "&" + TOPSECRET));
1667             d.deleteFamily(fam);
1668             table.delete(d);
1669           } catch (Throwable t) {
1670             throw new IOException(t);
1671           }
1672           return null;
1673         }
1674       };
1675       SUPERUSER.runAs(actiona);
1676 
1677       TEST_UTIL.getHBaseAdmin().flush(tableName.getNameAsString());
1678       Scan s = new Scan();
1679       s.setMaxVersions(5);
1680       s.setAuthorizations(new Authorizations(SECRET, PRIVATE, CONFIDENTIAL, TOPSECRET));
1681       ResultScanner scanner = table.getScanner(s);
1682       Result[] next = scanner.next(3);
1683       assertTrue(next.length == 2);
1684       CellScanner cellScanner = next[0].cellScanner();
1685       cellScanner.advance();
1686       Cell current = cellScanner.current();
1687       assertTrue(Bytes.equals(current.getRowArray(), current.getRowOffset(),
1688           current.getRowLength(), row1, 0, row1.length));
1689       assertEquals(current.getTimestamp(), 127l);
1690       cellScanner.advance();
1691       current = cellScanner.current();
1692       assertTrue(Bytes.equals(current.getRowArray(), current.getRowOffset(),
1693           current.getRowLength(), row1, 0, row1.length));
1694       assertEquals(current.getTimestamp(), 126l);
1695       cellScanner = next[1].cellScanner();
1696       cellScanner.advance();
1697       current = cellScanner.current();
1698       assertTrue(Bytes.equals(current.getRowArray(), current.getRowOffset(),
1699           current.getRowLength(), row2, 0, row2.length));
1700     } finally {
1701       if (table != null) {
1702         table.close();
1703       }
1704     }
1705   }
1706 
1707   @Test
1708   public void testDeleteColumnswithMultipleColumnsWithMultipleVersions() throws Exception {
1709     setAuths();
1710     TableName tableName = TableName.valueOf(TEST_NAME.getMethodName());
1711     HTable table = null;
1712     try {
1713       table = doPutsWithDiffCols(tableName);
1714       TEST_UTIL.getHBaseAdmin().flush(tableName.getNameAsString());
1715       PrivilegedExceptionAction<Void> actiona = new PrivilegedExceptionAction<Void>() {
1716         public Void run() throws Exception {
1717           try {
1718             HTable table = new HTable(conf, TEST_NAME.getMethodName());
1719             Delete d = new Delete(row1);
1720             d.setCellVisibility(new CellVisibility(SECRET + "&" + TOPSECRET));
1721             d.deleteColumns(fam, qual, 125l);
1722             table.delete(d);
1723           } catch (Throwable t) {
1724             throw new IOException(t);
1725           }
1726           return null;
1727         }
1728       };
1729       SUPERUSER.runAs(actiona);
1730 
1731       TEST_UTIL.getHBaseAdmin().flush(tableName.getNameAsString());
1732       Scan s = new Scan();
1733       s.setMaxVersions(5);
1734       s.setAuthorizations(new Authorizations(SECRET, PRIVATE, CONFIDENTIAL, TOPSECRET));
1735       ResultScanner scanner = table.getScanner(s);
1736       Result[] next = scanner.next(3);
1737       assertTrue(next.length == 1);
1738       CellScanner cellScanner = next[0].cellScanner();
1739       cellScanner.advance();
1740       Cell current = cellScanner.current();
1741       assertTrue(Bytes.equals(current.getRowArray(), current.getRowOffset(),
1742           current.getRowLength(), row1, 0, row1.length));
1743       assertEquals(current.getTimestamp(), 124l);
1744       cellScanner.advance();
1745       current = cellScanner.current();
1746       assertTrue(Bytes.equals(current.getRowArray(), current.getRowOffset(),
1747           current.getRowLength(), row1, 0, row1.length));
1748       assertEquals(current.getTimestamp(), 123l);
1749       cellScanner.advance();
1750       current = cellScanner.current();
1751       assertTrue(Bytes.equals(current.getRowArray(), current.getRowOffset(),
1752           current.getRowLength(), row1, 0, row1.length));
1753       assertTrue(Bytes.equals(current.getQualifierArray(), current.getQualifierOffset(),
1754           current.getQualifierLength(), qual1, 0, qual1.length));
1755       assertEquals(current.getTimestamp(), 126l);
1756       cellScanner.advance();
1757       current = cellScanner.current();
1758       assertTrue(Bytes.equals(current.getRowArray(), current.getRowOffset(),
1759           current.getRowLength(), row1, 0, row1.length));
1760       assertEquals(current.getTimestamp(), 127l);
1761       assertTrue(Bytes.equals(current.getQualifierArray(), current.getQualifierOffset(),
1762           current.getQualifierLength(), qual2, 0, qual2.length));
1763     } finally {
1764       if (table != null) {
1765         table.close();
1766       }
1767     }
1768   }
1769 
1770   @Test
1771   public void testDeleteColumnsWithDiffColsAndTags() throws Exception {
1772     TableName tableName = TableName.valueOf(TEST_NAME.getMethodName());
1773     HTable table = null;
1774     try {
1775       HBaseAdmin hBaseAdmin = TEST_UTIL.getHBaseAdmin();
1776       HColumnDescriptor colDesc = new HColumnDescriptor(fam);
1777       colDesc.setMaxVersions(5);
1778       HTableDescriptor desc = new HTableDescriptor(tableName);
1779       desc.addFamily(colDesc);
1780       hBaseAdmin.createTable(desc);
1781       table = new HTable(conf, tableName);
1782       Put put = new Put(Bytes.toBytes("row1"));
1783       put.add(fam, qual1, 125l, value);
1784       put.setCellVisibility(new CellVisibility(CONFIDENTIAL));
1785       table.put(put);
1786       put = new Put(Bytes.toBytes("row1"));
1787       put.add(fam, qual1, 126l, value);
1788       put.setCellVisibility(new CellVisibility(SECRET));
1789       table.put(put);
1790       TEST_UTIL.getHBaseAdmin().flush(tableName.getNameAsString());
1791       PrivilegedExceptionAction<Void> actiona = new PrivilegedExceptionAction<Void>() {
1792         public Void run() throws Exception {
1793           try {
1794             HTable table = new HTable(conf, TEST_NAME.getMethodName());
1795             Delete d = new Delete(row1);
1796             d.setCellVisibility(new CellVisibility(SECRET));
1797             d.deleteColumns(fam, qual, 126l);
1798             table.delete(d);
1799             d = new Delete(row1);
1800             d.setCellVisibility(new CellVisibility(CONFIDENTIAL));
1801             d.deleteColumns(fam, qual1, 125l);
1802             table.delete(d);
1803             table.flushCommits();
1804           } catch (Throwable t) {
1805             throw new IOException(t);
1806           }
1807           return null;
1808         }
1809       };
1810       SUPERUSER.runAs(actiona);
1811       Scan s = new Scan();
1812       s.setMaxVersions(5);
1813       s.setAuthorizations(new Authorizations(SECRET, CONFIDENTIAL));
1814       ResultScanner scanner = table.getScanner(s);
1815       Result[] next = scanner.next(3);
1816       assertEquals(next.length, 1);
1817     } finally {
1818       if (table != null) {
1819         table.close();
1820       }
1821     }
1822   }
1823   @Test
1824   public void testDeleteColumnsWithDiffColsAndTags1() throws Exception {
1825     TableName tableName = TableName.valueOf(TEST_NAME.getMethodName());
1826     HTable table = null;
1827     try {
1828       HBaseAdmin hBaseAdmin = TEST_UTIL.getHBaseAdmin();
1829       HColumnDescriptor colDesc = new HColumnDescriptor(fam);
1830       colDesc.setMaxVersions(5);
1831       HTableDescriptor desc = new HTableDescriptor(tableName);
1832       desc.addFamily(colDesc);
1833       hBaseAdmin.createTable(desc);
1834       table = new HTable(conf, tableName);
1835       Put put = new Put(Bytes.toBytes("row1"));
1836       put.add(fam, qual1, 125l, value);
1837       put.setCellVisibility(new CellVisibility(CONFIDENTIAL));
1838       table.put(put);
1839       put = new Put(Bytes.toBytes("row1"));
1840       put.add(fam, qual1, 126l, value);
1841       put.setCellVisibility(new CellVisibility(SECRET));
1842       table.put(put);
1843       TEST_UTIL.getHBaseAdmin().flush(tableName.getNameAsString());
1844       PrivilegedExceptionAction<Void> actiona = new PrivilegedExceptionAction<Void>() {
1845         public Void run() throws Exception {
1846           try {
1847             HTable table = new HTable(conf, TEST_NAME.getMethodName());
1848             Delete d = new Delete(row1);
1849             d.setCellVisibility(new CellVisibility(SECRET));
1850             d.deleteColumns(fam, qual, 126l);
1851             table.delete(d);
1852             d = new Delete(row1);
1853             d.setCellVisibility(new CellVisibility(CONFIDENTIAL));
1854             d.deleteColumns(fam, qual1, 126l);
1855             table.delete(d);
1856             table.flushCommits();
1857           } catch (Throwable t) {
1858             throw new IOException(t);
1859           }
1860           return null;
1861         }
1862       };
1863       SUPERUSER.runAs(actiona);
1864       Scan s = new Scan();
1865       s.setMaxVersions(5);
1866       s.setAuthorizations(new Authorizations(SECRET, CONFIDENTIAL));
1867       ResultScanner scanner = table.getScanner(s);
1868       Result[] next = scanner.next(3);
1869       assertEquals(next.length, 1);
1870     } finally {
1871       if (table != null) {
1872         table.close();
1873       }
1874     }
1875   }
1876   @Test
1877   public void testDeleteFamilyWithoutCellVisibilityWithMulipleVersions() throws Exception {
1878     setAuths();
1879     TableName tableName = TableName.valueOf(TEST_NAME.getMethodName());
1880     HTable table = null;
1881     try {
1882       table = doPutsWithoutVisibility(tableName);
1883       TEST_UTIL.getHBaseAdmin().flush(tableName.getNameAsString());
1884       PrivilegedExceptionAction<Void> actiona = new PrivilegedExceptionAction<Void>() {
1885         public Void run() throws Exception {
1886           try {
1887             HTable table = new HTable(conf, TEST_NAME.getMethodName());
1888             Delete d = new Delete(row1);
1889             d.deleteFamily(fam);
1890             table.delete(d);
1891           } catch (Throwable t) {
1892             throw new IOException(t);
1893           }
1894           return null;
1895         }
1896       };
1897       SUPERUSER.runAs(actiona);
1898 
1899       TEST_UTIL.getHBaseAdmin().flush(tableName.getNameAsString());
1900       Scan s = new Scan();
1901       s.setMaxVersions(5);
1902       s.setAuthorizations(new Authorizations(SECRET, PRIVATE, CONFIDENTIAL, TOPSECRET));
1903       ResultScanner scanner = table.getScanner(s);
1904       Result[] next = scanner.next(3);
1905       assertTrue(next.length == 1);
1906       // All cells wrt row1 should be deleted as we are not passing the Cell Visibility
1907       CellScanner cellScanner = next[0].cellScanner();
1908       cellScanner.advance();
1909       Cell current = cellScanner.current();
1910       assertTrue(Bytes.equals(current.getRowArray(), current.getRowOffset(),
1911           current.getRowLength(), row2, 0, row2.length));
1912     } finally {
1913       if (table != null) {
1914         table.close();
1915       }
1916     }
1917   }
1918 
1919   @Test
1920   public void testDeleteFamilyLatestTimeStampWithMulipleVersionsWithoutCellVisibilityInPuts()
1921       throws Exception {
1922     setAuths();
1923     TableName tableName = TableName.valueOf(TEST_NAME.getMethodName());
1924     HTable table = null;
1925     try {
1926       table = doPutsWithoutVisibility(tableName);
1927       PrivilegedExceptionAction<Void> actiona = new PrivilegedExceptionAction<Void>() {
1928         public Void run() throws Exception {
1929           try {
1930             HTable table = new HTable(conf, TEST_NAME.getMethodName());
1931             Delete d = new Delete(row1);
1932             d.setCellVisibility(new CellVisibility(SECRET + "&" + TOPSECRET));
1933             d.deleteFamily(fam);
1934             table.delete(d);
1935           } catch (Throwable t) {
1936             throw new IOException(t);
1937           }
1938           return null;
1939         }
1940       };
1941       SUPERUSER.runAs(actiona);
1942       TEST_UTIL.getHBaseAdmin().flush(tableName.getNameAsString());
1943       Scan s = new Scan();
1944       s.setMaxVersions(5);
1945       s.setAuthorizations(new Authorizations(SECRET, PRIVATE, CONFIDENTIAL, TOPSECRET));
1946       ResultScanner scanner = table.getScanner(s);
1947       Result[] next = scanner.next(3);
1948       assertTrue(next.length == 2);
1949       CellScanner cellScanner = next[0].cellScanner();
1950       cellScanner.advance();
1951       Cell current = cellScanner.current();
1952       assertTrue(Bytes.equals(current.getRowArray(), current.getRowOffset(),
1953           current.getRowLength(), row1, 0, row1.length));
1954       assertEquals(current.getTimestamp(), 127l);
1955       cellScanner.advance();
1956       current = cellScanner.current();
1957       assertTrue(Bytes.equals(current.getRowArray(), current.getRowOffset(),
1958           current.getRowLength(), row1, 0, row1.length));
1959       assertEquals(current.getTimestamp(), 126l);
1960       cellScanner.advance();
1961       current = cellScanner.current();
1962       assertTrue(Bytes.equals(current.getRowArray(), current.getRowOffset(),
1963           current.getRowLength(), row1, 0, row1.length));
1964       assertEquals(current.getTimestamp(), 125l);
1965       cellScanner.advance();
1966       current = cellScanner.current();
1967       assertTrue(Bytes.equals(current.getRowArray(), current.getRowOffset(),
1968           current.getRowLength(), row1, 0, row1.length));
1969       assertEquals(current.getTimestamp(), 124l);
1970       cellScanner.advance();
1971       current = cellScanner.current();
1972       assertTrue(Bytes.equals(current.getRowArray(), current.getRowOffset(),
1973           current.getRowLength(), row1, 0, row1.length));
1974       assertEquals(current.getTimestamp(), 123l);
1975       cellScanner = next[1].cellScanner();
1976       cellScanner.advance();
1977       current = cellScanner.current();
1978       assertTrue(Bytes.equals(current.getRowArray(), current.getRowOffset(),
1979           current.getRowLength(), row2, 0, row2.length));
1980     } finally {
1981       if (table != null) {
1982         table.close();
1983       }
1984     }
1985   }
1986 
1987   @Test
1988   public void testDeleteFamilySpecificTimeStampWithMulipleVersions() throws Exception {
1989     setAuths();
1990     TableName tableName = TableName.valueOf(TEST_NAME.getMethodName());
1991     HTable table = null;
1992     try {
1993       table = doPuts(tableName);
1994       TEST_UTIL.getHBaseAdmin().flush(tableName.getNameAsString());
1995       PrivilegedExceptionAction<Void> actiona = new PrivilegedExceptionAction<Void>() {
1996         public Void run() throws Exception {
1997           try {
1998             HTable table = new HTable(conf, TEST_NAME.getMethodName());
1999             Delete d = new Delete(row1);
2000             d.setCellVisibility(new CellVisibility("(" + PRIVATE + "&" + CONFIDENTIAL + ")|("
2001                 + SECRET + "&" + TOPSECRET + ")"));
2002             d.deleteFamily(fam, 126l);
2003             table.delete(d);
2004           } catch (Throwable t) {
2005             throw new IOException(t);
2006           }
2007           return null;
2008         }
2009       };
2010       SUPERUSER.runAs(actiona);
2011 
2012       TEST_UTIL.getHBaseAdmin().flush(tableName.getNameAsString());
2013       Scan s = new Scan();
2014       s.setMaxVersions(5);
2015       s.setAuthorizations(new Authorizations(SECRET, PRIVATE, CONFIDENTIAL, TOPSECRET));
2016       ResultScanner scanner = table.getScanner(s);
2017       Result[] next = scanner.next(6);
2018       assertTrue(next.length == 2);
2019       CellScanner cellScanner = next[0].cellScanner();
2020       cellScanner.advance();
2021       Cell current = cellScanner.current();
2022       assertTrue(Bytes.equals(current.getRowArray(), current.getRowOffset(),
2023           current.getRowLength(), row1, 0, row1.length));
2024       assertEquals(current.getTimestamp(), 127l);
2025       cellScanner.advance();
2026       current = cellScanner.current();
2027       assertTrue(Bytes.equals(current.getRowArray(), current.getRowOffset(),
2028           current.getRowLength(), row1, 0, row1.length));
2029       assertEquals(current.getTimestamp(), 125l);
2030       cellScanner.advance();
2031       current = cellScanner.current();
2032       assertTrue(Bytes.equals(current.getRowArray(), current.getRowOffset(),
2033           current.getRowLength(), row1, 0, row1.length));
2034       assertEquals(current.getTimestamp(), 123l);
2035       cellScanner = next[1].cellScanner();
2036       cellScanner.advance();
2037       current = cellScanner.current();
2038       assertTrue(Bytes.equals(current.getRowArray(), current.getRowOffset(),
2039           current.getRowLength(), row2, 0, row2.length));
2040     } finally {
2041       if (table != null) {
2042         table.close();
2043       }
2044     }
2045   }
2046 
2047   @Test
2048   public void testScanAfterCompaction() throws Exception {
2049     setAuths();
2050     TableName tableName = TableName.valueOf(TEST_NAME.getMethodName());
2051     HTable table = null;
2052     try {
2053       table = doPuts(tableName);
2054       TEST_UTIL.getHBaseAdmin().flush(tableName.getNameAsString());
2055       PrivilegedExceptionAction<Void> actiona = new PrivilegedExceptionAction<Void>() {
2056         public Void run() throws Exception {
2057           try {
2058             HTable table = new HTable(conf, TEST_NAME.getMethodName());
2059             Delete d = new Delete(row1);
2060             d.setCellVisibility(new CellVisibility("(" + PRIVATE + "&" + CONFIDENTIAL + ")|(" +
2061                 SECRET + "&" + TOPSECRET+")"));
2062             d.deleteFamily(fam, 126l);
2063             table.delete(d);
2064           } catch (Throwable t) {
2065             throw new IOException(t);
2066           }
2067           return null;
2068         }
2069       };
2070       SUPERUSER.runAs(actiona);
2071 
2072       TEST_UTIL.getHBaseAdmin().flush(tableName.getNameAsString());
2073       Put put = new Put(Bytes.toBytes("row3"));
2074       put.add(fam, qual, 127l, value);
2075       put.setCellVisibility(new CellVisibility(CONFIDENTIAL + "&" + PRIVATE));
2076       table.put(put);
2077       TEST_UTIL.getHBaseAdmin().flush(tableName.getNameAsString());
2078       TEST_UTIL.getHBaseAdmin().compact(tableName.getNameAsString());
2079       Thread.sleep(5000);
2080       // Sleep to ensure compaction happens. Need to do it in a better way
2081       Scan s = new Scan();
2082       s.setMaxVersions(5);
2083       s.setAuthorizations(new Authorizations(SECRET, PRIVATE, CONFIDENTIAL, TOPSECRET));
2084       ResultScanner scanner = table.getScanner(s);
2085       Result[] next = scanner.next(3);
2086       assertTrue(next.length == 3);
2087       CellScanner cellScanner = next[0].cellScanner();
2088       cellScanner.advance();
2089       Cell current = cellScanner.current();
2090       assertTrue(Bytes.equals(current.getRowArray(), current.getRowOffset(),
2091           current.getRowLength(), row1, 0, row1.length));
2092       assertEquals(current.getTimestamp(), 127l);
2093       cellScanner = next[1].cellScanner();
2094       cellScanner.advance();
2095       current = cellScanner.current();
2096       assertTrue(Bytes.equals(current.getRowArray(), current.getRowOffset(),
2097           current.getRowLength(), row2, 0, row2.length));
2098     } finally {
2099       if (table != null) {
2100         table.close();
2101       }
2102     }
2103   }
2104 
2105   @Test
2106   public void testDeleteFamilySpecificTimeStampWithMulipleVersionsDoneTwice() throws Exception {
2107     setAuths();
2108     TableName tableName = TableName.valueOf(TEST_NAME.getMethodName());
2109     HTable table = null;
2110     try {
2111       // Do not flush here.
2112       table = doPuts(tableName);
2113       PrivilegedExceptionAction<Void> actiona = new PrivilegedExceptionAction<Void>() {
2114         public Void run() throws Exception {
2115           try {
2116             HTable table = new HTable(conf, TEST_NAME.getMethodName());
2117             Delete d = new Delete(row1);
2118             d.setCellVisibility(new CellVisibility("(" + PRIVATE + "&" + CONFIDENTIAL + ")|("
2119                 + TOPSECRET + "&" + SECRET+")"));
2120             d.deleteFamily(fam, 125l);
2121             table.delete(d);
2122           } catch (Throwable t) {
2123             throw new IOException(t);
2124           }
2125           return null;
2126         }
2127       };
2128       SUPERUSER.runAs(actiona);
2129 
2130       Scan s = new Scan();
2131       s.setMaxVersions(5);
2132       s.setAuthorizations(new Authorizations(SECRET, PRIVATE, CONFIDENTIAL, TOPSECRET));
2133       ResultScanner scanner = table.getScanner(s);
2134       Result[] next = scanner.next(3);
2135       assertTrue(next.length == 2);
2136       CellScanner cellScanner = next[0].cellScanner();
2137       cellScanner.advance();
2138       Cell current = cellScanner.current();
2139       assertTrue(Bytes.equals(current.getRowArray(), current.getRowOffset(),
2140           current.getRowLength(), row1, 0, row1.length));
2141       assertEquals(current.getTimestamp(), 127l);
2142       cellScanner.advance();
2143       current = cellScanner.current();
2144       assertTrue(Bytes.equals(current.getRowArray(), current.getRowOffset(),
2145           current.getRowLength(), row1, 0, row1.length));
2146       assertEquals(current.getTimestamp(), 126l);
2147       cellScanner.advance();
2148       current = cellScanner.current();
2149       assertTrue(Bytes.equals(current.getRowArray(), current.getRowOffset(),
2150           current.getRowLength(), row1, 0, row1.length));
2151       assertEquals(current.getTimestamp(), 125l);
2152       cellScanner.advance();
2153       current = cellScanner.current();
2154       assertTrue(Bytes.equals(current.getRowArray(), current.getRowOffset(),
2155           current.getRowLength(), row1, 0, row1.length));
2156       assertEquals(current.getTimestamp(), 123l);
2157       cellScanner = next[1].cellScanner();
2158       cellScanner.advance();
2159       current = cellScanner.current();
2160       assertTrue(Bytes.equals(current.getRowArray(), current.getRowOffset(),
2161           current.getRowLength(), row2, 0, row2.length));
2162 
2163       // Issue 2nd delete
2164       actiona = new PrivilegedExceptionAction<Void>() {
2165         public Void run() throws Exception {
2166           try {
2167             HTable table = new HTable(conf, TEST_NAME.getMethodName());
2168             Delete d = new Delete(row1);
2169             d.setCellVisibility(new CellVisibility("(" + CONFIDENTIAL + "&" + PRIVATE + ")|("
2170                 + TOPSECRET + "&" + SECRET+")"));
2171             d.deleteFamily(fam, 127l);
2172             table.delete(d);
2173           } catch (Throwable t) {
2174             throw new IOException(t);
2175           }
2176           return null;
2177         }
2178       };
2179       SUPERUSER.runAs(actiona);
2180       s = new Scan();
2181       s.setMaxVersions(5);
2182       s.setAuthorizations(new Authorizations(SECRET, PRIVATE, CONFIDENTIAL, TOPSECRET));
2183       scanner = table.getScanner(s);
2184       next = scanner.next(3);
2185       assertTrue(next.length == 2);
2186       cellScanner = next[0].cellScanner();
2187       cellScanner.advance();
2188       current = cellScanner.current();
2189       assertTrue(Bytes.equals(current.getRowArray(), current.getRowOffset(),
2190           current.getRowLength(), row1, 0, row1.length));
2191       assertEquals(current.getTimestamp(), 125l);
2192       cellScanner.advance();
2193       current = cellScanner.current();
2194       assertTrue(Bytes.equals(current.getRowArray(), current.getRowOffset(),
2195           current.getRowLength(), row1, 0, row1.length));
2196       assertEquals(current.getTimestamp(), 123l);
2197       cellScanner = next[1].cellScanner();
2198       cellScanner.advance();
2199       current = cellScanner.current();
2200       assertTrue(Bytes.equals(current.getRowArray(), current.getRowOffset(),
2201           current.getRowLength(), row2, 0, row2.length));
2202       assertEquals(current.getTimestamp(), 127l);
2203     } finally {
2204       if (table != null) {
2205         table.close();
2206       }
2207     }
2208   }
2209 
2210   @Test
2211   public void testMultipleDeleteFamilyVersionWithDiffLabels() throws Exception {
2212     PrivilegedExceptionAction<VisibilityLabelsResponse> action =
2213         new PrivilegedExceptionAction<VisibilityLabelsResponse>() {
2214       public VisibilityLabelsResponse run() throws Exception {
2215         try {
2216           return VisibilityClient.setAuths(conf, new String[] { CONFIDENTIAL, PRIVATE, SECRET },
2217               SUPERUSER.getShortName());
2218         } catch (Throwable e) {
2219         }
2220         return null;
2221       }
2222     };
2223     VisibilityLabelsResponse response = SUPERUSER.runAs(action);
2224     TableName tableName = TableName.valueOf(TEST_NAME.getMethodName());
2225     HTable table = doPuts(tableName);
2226     try {
2227       PrivilegedExceptionAction<Void> actiona = new PrivilegedExceptionAction<Void>() {
2228         public Void run() throws Exception {
2229           try {
2230             HTable table = new HTable(conf, TEST_NAME.getMethodName());
2231             Delete d = new Delete(row1);
2232             d.setCellVisibility(new CellVisibility(CONFIDENTIAL));
2233             d.deleteFamilyVersion(fam, 123l);
2234             table.delete(d);
2235             d = new Delete(row1);
2236             d.setCellVisibility(new CellVisibility(SECRET + "&" + TOPSECRET));
2237             d.deleteFamilyVersion(fam, 125l);
2238             table.delete(d);
2239           } catch (Throwable t) {
2240             throw new IOException(t);
2241           }
2242           return null;
2243         }
2244       };
2245       SUPERUSER.runAs(actiona);
2246 
2247       TEST_UTIL.getHBaseAdmin().flush(tableName.getNameAsString());
2248       Scan s = new Scan();
2249       s.setMaxVersions(5);
2250       s.setAuthorizations(new Authorizations(SECRET, PRIVATE, CONFIDENTIAL, TOPSECRET));
2251       ResultScanner scanner = table.getScanner(s);
2252       Result[] next = scanner.next(5);
2253       assertTrue(next.length == 2);
2254       CellScanner cellScanner = next[0].cellScanner();
2255       cellScanner.advance();
2256       Cell current = cellScanner.current();
2257       assertTrue(Bytes.equals(current.getRowArray(), current.getRowOffset(),
2258           current.getRowLength(), row1, 0, row1.length));
2259       assertEquals(current.getTimestamp(), 127l);
2260       cellScanner.advance();
2261       current = cellScanner.current();
2262       assertTrue(Bytes.equals(current.getRowArray(), current.getRowOffset(),
2263           current.getRowLength(), row1, 0, row1.length));
2264       assertEquals(current.getTimestamp(), 126l);
2265       cellScanner.advance();
2266       current = cellScanner.current();
2267       assertTrue(Bytes.equals(current.getRowArray(), current.getRowOffset(),
2268           current.getRowLength(), row1, 0, row1.length));
2269       assertEquals(current.getTimestamp(), 124l);
2270     } finally {
2271       if (table != null) {
2272         table.close();
2273       }
2274     }
2275   }
2276 
2277   @Test (timeout=180000)
2278   public void testSpecificDeletesFollowedByDeleteFamily() throws Exception {
2279     setAuths();
2280     TableName tableName = TableName.valueOf(TEST_NAME.getMethodName());
2281     HTable table = doPuts(tableName);
2282     try {
2283       PrivilegedExceptionAction<Void> actiona = new PrivilegedExceptionAction<Void>() {
2284         public Void run() throws Exception {
2285           try {
2286             HTable table = new HTable(conf, TEST_NAME.getMethodName());
2287             Delete d = new Delete(row1);
2288             d.setCellVisibility(new CellVisibility("(" + CONFIDENTIAL + "&" + PRIVATE + ")|("
2289                 + TOPSECRET + "&" + SECRET + ")"));
2290             d.deleteColumn(fam, qual, 126l);
2291             table.delete(d);
2292             d = new Delete(row1);
2293             d.setCellVisibility(new CellVisibility(SECRET + "&" + TOPSECRET));
2294             d.deleteFamilyVersion(fam, 125l);
2295             table.delete(d);
2296           } catch (Throwable t) {
2297             throw new IOException(t);
2298           }
2299           return null;
2300         }
2301       };
2302       SUPERUSER.runAs(actiona);
2303 
2304       TEST_UTIL.getHBaseAdmin().flush(tableName.getNameAsString());
2305       Scan s = new Scan();
2306       s.setMaxVersions(5);
2307       s.setAuthorizations(new Authorizations(SECRET, PRIVATE, CONFIDENTIAL, TOPSECRET));
2308       ResultScanner scanner = table.getScanner(s);
2309       Result[] next = scanner.next(5);
2310       assertTrue(next.length == 2);
2311       CellScanner cellScanner = next[0].cellScanner();
2312       cellScanner.advance();
2313       Cell current = cellScanner.current();
2314       assertTrue(Bytes.equals(current.getRowArray(), current.getRowOffset(),
2315           current.getRowLength(), row1, 0, row1.length));
2316       assertEquals(current.getTimestamp(), 127l);
2317       cellScanner.advance();
2318       current = cellScanner.current();
2319       assertTrue(Bytes.equals(current.getRowArray(), current.getRowOffset(),
2320           current.getRowLength(), row1, 0, row1.length));
2321       assertEquals(current.getTimestamp(), 124l);
2322       cellScanner.advance();
2323       current = cellScanner.current();
2324       assertTrue(Bytes.equals(current.getRowArray(), current.getRowOffset(),
2325           current.getRowLength(), row1, 0, row1.length));
2326       assertEquals(current.getTimestamp(), 123l);
2327       // Issue 2nd delete
2328       actiona = new PrivilegedExceptionAction<Void>() {
2329         public Void run() throws Exception {
2330           try {
2331             HTable table = new HTable(conf, TEST_NAME.getMethodName());
2332             Delete d = new Delete(row1);
2333             d.setCellVisibility(new CellVisibility(CONFIDENTIAL));
2334             d.deleteFamily(fam);
2335             table.delete(d);
2336           } catch (Throwable t) {
2337             throw new IOException(t);
2338           }
2339           return null;
2340         }
2341       };
2342       SUPERUSER.runAs(actiona);
2343       s = new Scan();
2344       s.setMaxVersions(5);
2345       s.setAuthorizations(new Authorizations(SECRET, PRIVATE, CONFIDENTIAL, TOPSECRET));
2346       scanner = table.getScanner(s);
2347       next = scanner.next(5);
2348       assertTrue(next.length == 2);
2349       cellScanner = next[0].cellScanner();
2350       cellScanner.advance();
2351       current = cellScanner.current();
2352       assertTrue(Bytes.equals(current.getRowArray(), current.getRowOffset(),
2353           current.getRowLength(), row1, 0, row1.length));
2354       assertEquals(current.getTimestamp(), 127l);
2355       cellScanner.advance();
2356       current = cellScanner.current();
2357       assertTrue(Bytes.equals(current.getRowArray(), current.getRowOffset(),
2358           current.getRowLength(), row1, 0, row1.length));
2359       assertEquals(current.getTimestamp(), 124l);
2360     } finally {
2361       if (table != null) {
2362         table.close();
2363       }
2364     }
2365   }
2366 
2367   @Test(timeout = 180000)
2368   public void testSpecificDeletesFollowedByDeleteFamily1() throws Exception {
2369     PrivilegedExceptionAction<VisibilityLabelsResponse> action =
2370         new PrivilegedExceptionAction<VisibilityLabelsResponse>() {
2371       public VisibilityLabelsResponse run() throws Exception {
2372         try {
2373           return VisibilityClient.setAuths(conf, new String[] { CONFIDENTIAL, PRIVATE, SECRET },
2374               SUPERUSER.getShortName());
2375         } catch (Throwable e) {
2376         }
2377         return null;
2378       }
2379     };
2380     VisibilityLabelsResponse response = SUPERUSER.runAs(action);
2381     TableName tableName = TableName.valueOf(TEST_NAME.getMethodName());
2382     HTable table = doPuts(tableName);
2383     try {
2384       PrivilegedExceptionAction<Void> actiona = new PrivilegedExceptionAction<Void>() {
2385         public Void run() throws Exception {
2386           try {
2387             HTable table = new HTable(conf, TEST_NAME.getMethodName());
2388             Delete d = new Delete(row1);
2389             d.setCellVisibility(new CellVisibility("(" + CONFIDENTIAL + "&" + PRIVATE + ")|("
2390                 + TOPSECRET + "&" + SECRET + ")"));
2391             d.deleteColumn(fam, qual);
2392             table.delete(d);
2393 
2394             d = new Delete(row1);
2395             d.setCellVisibility(new CellVisibility(SECRET + "&" + TOPSECRET));
2396             d.deleteFamilyVersion(fam, 125l);
2397             table.delete(d);
2398           } catch (Throwable t) {
2399             throw new IOException(t);
2400           }
2401           return null;
2402         }
2403       };
2404       SUPERUSER.runAs(actiona);
2405 
2406       TEST_UTIL.getHBaseAdmin().flush(tableName.getNameAsString());
2407       Scan s = new Scan();
2408       s.setMaxVersions(5);
2409       s.setAuthorizations(new Authorizations(SECRET, PRIVATE, CONFIDENTIAL, TOPSECRET));
2410       ResultScanner scanner = table.getScanner(s);
2411       Result[] next = scanner.next(5);
2412       assertTrue(next.length == 2);
2413       CellScanner cellScanner = next[0].cellScanner();
2414       cellScanner.advance();
2415       Cell current = cellScanner.current();
2416       assertTrue(Bytes.equals(current.getRowArray(), current.getRowOffset(),
2417           current.getRowLength(), row1, 0, row1.length));
2418       assertEquals(current.getTimestamp(), 126l);
2419       cellScanner.advance();
2420       current = cellScanner.current();
2421       assertTrue(Bytes.equals(current.getRowArray(), current.getRowOffset(),
2422           current.getRowLength(), row1, 0, row1.length));
2423       assertEquals(current.getTimestamp(), 124l);
2424       cellScanner.advance();
2425       current = cellScanner.current();
2426       assertTrue(Bytes.equals(current.getRowArray(), current.getRowOffset(),
2427           current.getRowLength(), row1, 0, row1.length));
2428       assertEquals(current.getTimestamp(), 123l);
2429       // Issue 2nd delete
2430       actiona = new PrivilegedExceptionAction<Void>() {
2431         public Void run() throws Exception {
2432           try {
2433             HTable table = new HTable(conf, TEST_NAME.getMethodName());
2434             Delete d = new Delete(row1);
2435             d.setCellVisibility(new CellVisibility(CONFIDENTIAL));
2436             d.deleteFamily(fam);
2437             table.delete(d);
2438           } catch (Throwable t) {
2439             throw new IOException(t);
2440           }
2441           return null;
2442         }
2443       };
2444       SUPERUSER.runAs(actiona);
2445       s = new Scan();
2446       s.setMaxVersions(5);
2447       s.setAuthorizations(new Authorizations(SECRET, PRIVATE, CONFIDENTIAL, TOPSECRET));
2448       scanner = table.getScanner(s);
2449       next = scanner.next(5);
2450       assertTrue(next.length == 2);
2451       cellScanner = next[0].cellScanner();
2452       cellScanner.advance();
2453       current = cellScanner.current();
2454       assertTrue(Bytes.equals(current.getRowArray(), current.getRowOffset(),
2455           current.getRowLength(), row1, 0, row1.length));
2456       assertEquals(current.getTimestamp(), 126l);
2457       cellScanner.advance();
2458       current = cellScanner.current();
2459       assertTrue(Bytes.equals(current.getRowArray(), current.getRowOffset(),
2460           current.getRowLength(), row1, 0, row1.length));
2461       assertEquals(current.getTimestamp(), 124l);
2462 
2463     } finally {
2464       if (table != null) {
2465         table.close();
2466       }
2467     }
2468   }
2469 
2470   @Test
2471   public void testDeleteColumnSpecificTimeStampWithMulipleVersionsDoneTwice() throws Exception {
2472     setAuths();
2473     TableName tableName = TableName.valueOf(TEST_NAME.getMethodName());
2474     HTable table = null;
2475     try {
2476       // Do not flush here.
2477       table = doPuts(tableName);
2478       PrivilegedExceptionAction<Void> actiona = new PrivilegedExceptionAction<Void>() {
2479         public Void run() throws Exception {
2480           try {
2481             HTable table = new HTable(conf, TEST_NAME.getMethodName());
2482             Delete d = new Delete(row1);
2483             d.setCellVisibility(new CellVisibility(SECRET + "&" + TOPSECRET));
2484             d.deleteColumn(fam, qual, 125l);
2485             table.delete(d);
2486           } catch (Throwable t) {
2487             throw new IOException(t);
2488           }
2489           return null;
2490         }
2491       };
2492       SUPERUSER.runAs(actiona);
2493 
2494       Scan s = new Scan();
2495       s.setMaxVersions(5);
2496       s.setAuthorizations(new Authorizations(SECRET, PRIVATE, CONFIDENTIAL, TOPSECRET));
2497       ResultScanner scanner = table.getScanner(s);
2498       Result[] next = scanner.next(3);
2499       assertTrue(next.length == 2);
2500       CellScanner cellScanner = next[0].cellScanner();
2501       cellScanner.advance();
2502       Cell current = cellScanner.current();
2503       assertTrue(Bytes.equals(current.getRowArray(), current.getRowOffset(),
2504           current.getRowLength(), row1, 0, row1.length));
2505       assertEquals(current.getTimestamp(), 127l);
2506       cellScanner.advance();
2507       current = cellScanner.current();
2508       assertTrue(Bytes.equals(current.getRowArray(), current.getRowOffset(),
2509           current.getRowLength(), row1, 0, row1.length));
2510       assertEquals(current.getTimestamp(), 126l);
2511       cellScanner.advance();
2512       current = cellScanner.current();
2513       assertTrue(Bytes.equals(current.getRowArray(), current.getRowOffset(),
2514           current.getRowLength(), row1, 0, row1.length));
2515       assertEquals(current.getTimestamp(), 124l);
2516       cellScanner.advance();
2517       current = cellScanner.current();
2518       assertTrue(Bytes.equals(current.getRowArray(), current.getRowOffset(),
2519           current.getRowLength(), row1, 0, row1.length));
2520       assertEquals(current.getTimestamp(), 123l);
2521       cellScanner = next[1].cellScanner();
2522       cellScanner.advance();
2523       current = cellScanner.current();
2524       assertTrue(Bytes.equals(current.getRowArray(), current.getRowOffset(),
2525           current.getRowLength(), row2, 0, row2.length));
2526 
2527       // Issue 2nd delete
2528       actiona = new PrivilegedExceptionAction<Void>() {
2529         public Void run() throws Exception {
2530           try {
2531             HTable table = new HTable(conf, TEST_NAME.getMethodName());
2532             Delete d = new Delete(row1);
2533             d.setCellVisibility(new CellVisibility("(" + CONFIDENTIAL + "&" + PRIVATE + ")|("
2534                 + TOPSECRET + "&" + SECRET+")"));
2535             d.deleteColumn(fam, qual, 127l);
2536             table.delete(d);
2537           } catch (Throwable t) {
2538             throw new IOException(t);
2539           }
2540           return null;
2541         }
2542       };
2543       SUPERUSER.runAs(actiona);
2544       s = new Scan();
2545       s.setMaxVersions(5);
2546       s.setAuthorizations(new Authorizations(SECRET, PRIVATE, CONFIDENTIAL, TOPSECRET));
2547       scanner = table.getScanner(s);
2548       next = scanner.next(3);
2549       assertTrue(next.length == 2);
2550       cellScanner = next[0].cellScanner();
2551       cellScanner.advance();
2552       current = cellScanner.current();
2553       assertTrue(Bytes.equals(current.getRowArray(), current.getRowOffset(),
2554           current.getRowLength(), row1, 0, row1.length));
2555       assertEquals(current.getTimestamp(), 126l);
2556       cellScanner.advance();
2557       current = cellScanner.current();
2558       assertTrue(Bytes.equals(current.getRowArray(), current.getRowOffset(),
2559           current.getRowLength(), row1, 0, row1.length));
2560       assertEquals(current.getTimestamp(), 124l);
2561       cellScanner.advance();
2562       current = cellScanner.current();
2563       assertTrue(Bytes.equals(current.getRowArray(), current.getRowOffset(),
2564           current.getRowLength(), row1, 0, row1.length));
2565       assertEquals(current.getTimestamp(), 123l);
2566       cellScanner = next[1].cellScanner();
2567       cellScanner.advance();
2568       current = cellScanner.current();
2569       assertTrue(Bytes.equals(current.getRowArray(), current.getRowOffset(),
2570           current.getRowLength(), row2, 0, row2.length));
2571       assertEquals(current.getTimestamp(), 127l);
2572     } finally {
2573       if (table != null) {
2574         table.close();
2575       }
2576     }
2577   }
2578 
2579   @Test
2580   public void testDeleteColumnSpecificTimeStampWithMulipleVersionsDoneTwice1() throws Exception {
2581     setAuths();
2582     TableName tableName = TableName.valueOf(TEST_NAME.getMethodName());
2583     HTable table = null;
2584     try {
2585       // Do not flush here.
2586       table = doPuts(tableName);
2587       PrivilegedExceptionAction<Void> actiona = new PrivilegedExceptionAction<Void>() {
2588         public Void run() throws Exception {
2589           try {
2590             HTable table = new HTable(conf, TEST_NAME.getMethodName());
2591             Delete d = new Delete(row1);
2592             d.setCellVisibility(new CellVisibility("(" + CONFIDENTIAL + "&" + PRIVATE + ")" +
2593                 "|(" + TOPSECRET + "&" + SECRET + ")"));
2594             d.deleteColumn(fam, qual, 127l);
2595             table.delete(d);
2596           } catch (Throwable t) {
2597             throw new IOException(t);
2598           }
2599           return null;
2600         }
2601       };
2602       SUPERUSER.runAs(actiona);
2603 
2604       Scan s = new Scan();
2605       s.setMaxVersions(5);
2606       s.setAuthorizations(new Authorizations(SECRET, PRIVATE, CONFIDENTIAL, TOPSECRET));
2607       ResultScanner scanner = table.getScanner(s);
2608       Result[] next = scanner.next(3);
2609       assertTrue(next.length == 2);
2610       CellScanner cellScanner = next[0].cellScanner();
2611       cellScanner.advance();
2612       Cell current = cellScanner.current();
2613       assertTrue(Bytes.equals(current.getRowArray(), current.getRowOffset(),
2614           current.getRowLength(), row1, 0, row1.length));
2615       assertEquals(current.getTimestamp(), 126l);
2616       cellScanner.advance();
2617       current = cellScanner.current();
2618       assertTrue(Bytes.equals(current.getRowArray(), current.getRowOffset(),
2619           current.getRowLength(), row1, 0, row1.length));
2620       assertEquals(current.getTimestamp(), 125l);
2621       cellScanner.advance();
2622       current = cellScanner.current();
2623       assertTrue(Bytes.equals(current.getRowArray(), current.getRowOffset(),
2624           current.getRowLength(), row1, 0, row1.length));
2625       assertEquals(current.getTimestamp(), 124l);
2626       cellScanner.advance();
2627       current = cellScanner.current();
2628       assertTrue(Bytes.equals(current.getRowArray(), current.getRowOffset(),
2629           current.getRowLength(), row1, 0, row1.length));
2630       assertEquals(current.getTimestamp(), 123l);
2631       cellScanner = next[1].cellScanner();
2632       cellScanner.advance();
2633       current = cellScanner.current();
2634       assertTrue(Bytes.equals(current.getRowArray(), current.getRowOffset(),
2635           current.getRowLength(), row2, 0, row2.length));
2636 
2637       // Issue 2nd delete
2638       actiona = new PrivilegedExceptionAction<Void>() {
2639         public Void run() throws Exception {
2640           try {
2641             HTable table = new HTable(conf, TEST_NAME.getMethodName());
2642             Delete d = new Delete(row1);
2643             d.setCellVisibility(new CellVisibility(SECRET + "&" + TOPSECRET));
2644             d.deleteColumn(fam, qual, 127l);
2645             table.delete(d);
2646           } catch (Throwable t) {
2647             throw new IOException(t);
2648           }
2649           return null;
2650         }
2651       };
2652       SUPERUSER.runAs(actiona);
2653       s = new Scan();
2654       s.setMaxVersions(5);
2655       s.setAuthorizations(new Authorizations(SECRET, PRIVATE, CONFIDENTIAL, TOPSECRET));
2656       scanner = table.getScanner(s);
2657       next = scanner.next(3);
2658       assertTrue(next.length == 2);
2659       cellScanner = next[0].cellScanner();
2660       cellScanner.advance();
2661       current = cellScanner.current();
2662       assertTrue(Bytes.equals(current.getRowArray(), current.getRowOffset(),
2663           current.getRowLength(), row1, 0, row1.length));
2664       assertEquals(current.getTimestamp(), 126l);
2665       cellScanner.advance();
2666       current = cellScanner.current();
2667       assertTrue(Bytes.equals(current.getRowArray(), current.getRowOffset(),
2668           current.getRowLength(), row1, 0, row1.length));
2669       assertEquals(current.getTimestamp(), 125l);
2670       cellScanner.advance();
2671       current = cellScanner.current();
2672       assertTrue(Bytes.equals(current.getRowArray(), current.getRowOffset(),
2673           current.getRowLength(), row1, 0, row1.length));
2674       assertEquals(current.getTimestamp(), 124l);
2675       cellScanner.advance();
2676       current = cellScanner.current();
2677       assertTrue(Bytes.equals(current.getRowArray(), current.getRowOffset(),
2678           current.getRowLength(), row1, 0, row1.length));
2679       assertEquals(current.getTimestamp(), 123l);
2680       cellScanner = next[1].cellScanner();
2681       cellScanner.advance();
2682       current = cellScanner.current();
2683       assertTrue(Bytes.equals(current.getRowArray(), current.getRowOffset(),
2684           current.getRowLength(), row2, 0, row2.length));
2685       assertEquals(current.getTimestamp(), 127l);
2686     } finally {
2687       if (table != null) {
2688         table.close();
2689       }
2690     }
2691   }
2692   @Test
2693   public void testDeleteColumnSpecificTimeStampWithMulipleVersionsDoneTwice2() throws Exception {
2694     setAuths();
2695     TableName tableName = TableName.valueOf(TEST_NAME.getMethodName());
2696     HTable table = null;
2697     try {
2698       // Do not flush here.
2699       table = doPuts(tableName);
2700       PrivilegedExceptionAction<Void> actiona = new PrivilegedExceptionAction<Void>() {
2701         public Void run() throws Exception {
2702           try {
2703             HTable table = new HTable(conf, TEST_NAME.getMethodName());
2704             Delete d = new Delete(row1);
2705             d.setCellVisibility(new CellVisibility("(" + PRIVATE + "&" + CONFIDENTIAL + ")|("
2706                 + TOPSECRET + "&" + SECRET+")"));
2707             d.deleteColumn(fam, qual, 125l);
2708             table.delete(d);
2709           } catch (Throwable t) {
2710             throw new IOException(t);
2711           }
2712           return null;
2713         }
2714       };
2715       SUPERUSER.runAs(actiona);
2716 
2717       Scan s = new Scan();
2718       s.setMaxVersions(5);
2719       s.setAuthorizations(new Authorizations(SECRET, PRIVATE, CONFIDENTIAL, TOPSECRET));
2720       ResultScanner scanner = table.getScanner(s);
2721       Result[] next = scanner.next(3);
2722       assertTrue(next.length == 2);
2723       CellScanner cellScanner = next[0].cellScanner();
2724       cellScanner.advance();
2725       Cell current = cellScanner.current();
2726       assertTrue(Bytes.equals(current.getRowArray(), current.getRowOffset(),
2727           current.getRowLength(), row1, 0, row1.length));
2728       assertEquals(current.getTimestamp(), 127l);
2729       cellScanner.advance();
2730       current = cellScanner.current();
2731       assertTrue(Bytes.equals(current.getRowArray(), current.getRowOffset(),
2732           current.getRowLength(), row1, 0, row1.length));
2733       assertEquals(current.getTimestamp(), 126l);
2734       cellScanner.advance();
2735       current = cellScanner.current();
2736       assertTrue(Bytes.equals(current.getRowArray(), current.getRowOffset(),
2737           current.getRowLength(), row1, 0, row1.length));
2738       assertEquals(current.getTimestamp(), 125l);
2739       cellScanner.advance();
2740       current = cellScanner.current();
2741       assertTrue(Bytes.equals(current.getRowArray(), current.getRowOffset(),
2742           current.getRowLength(), row1, 0, row1.length));
2743       assertEquals(current.getTimestamp(), 124l);
2744       cellScanner.advance();
2745       current = cellScanner.current();
2746       assertTrue(Bytes.equals(current.getRowArray(), current.getRowOffset(),
2747           current.getRowLength(), row1, 0, row1.length));
2748       assertEquals(current.getTimestamp(), 123l);
2749       cellScanner = next[1].cellScanner();
2750       cellScanner.advance();
2751       current = cellScanner.current();
2752       assertTrue(Bytes.equals(current.getRowArray(), current.getRowOffset(),
2753           current.getRowLength(), row2, 0, row2.length));
2754 
2755       // Issue 2nd delete
2756       actiona = new PrivilegedExceptionAction<Void>() {
2757         public Void run() throws Exception {
2758           try {
2759             HTable table = new HTable(conf, TEST_NAME.getMethodName());
2760             Delete d = new Delete(row1);
2761             d.setCellVisibility(new CellVisibility("(" + CONFIDENTIAL + "&" + PRIVATE + ")|("
2762                 + TOPSECRET + "&" + SECRET+")"));
2763             d.deleteColumn(fam, qual, 127l);
2764             table.delete(d);
2765           } catch (Throwable t) {
2766             throw new IOException(t);
2767           }
2768           return null;
2769         }
2770       };
2771       SUPERUSER.runAs(actiona);
2772       s = new Scan();
2773       s.setMaxVersions(5);
2774       s.setAuthorizations(new Authorizations(SECRET, PRIVATE, CONFIDENTIAL, TOPSECRET));
2775       scanner = table.getScanner(s);
2776       next = scanner.next(3);
2777       assertTrue(next.length == 2);
2778       cellScanner = next[0].cellScanner();
2779       cellScanner.advance();
2780       current = cellScanner.current();
2781       assertTrue(Bytes.equals(current.getRowArray(), current.getRowOffset(),
2782           current.getRowLength(), row1, 0, row1.length));
2783       assertEquals(current.getTimestamp(), 126l);
2784       cellScanner.advance();
2785       current = cellScanner.current();
2786       assertTrue(Bytes.equals(current.getRowArray(), current.getRowOffset(),
2787           current.getRowLength(), row1, 0, row1.length));
2788       assertEquals(current.getTimestamp(), 125l);
2789       cellScanner.advance();
2790       current = cellScanner.current();
2791       assertTrue(Bytes.equals(current.getRowArray(), current.getRowOffset(),
2792           current.getRowLength(), row1, 0, row1.length));
2793       assertEquals(current.getTimestamp(), 124l);
2794       cellScanner.advance();
2795       current = cellScanner.current();
2796       assertTrue(Bytes.equals(current.getRowArray(), current.getRowOffset(),
2797           current.getRowLength(), row1, 0, row1.length));
2798       assertEquals(current.getTimestamp(), 123l);
2799       cellScanner = next[1].cellScanner();
2800       cellScanner.advance();
2801       current = cellScanner.current();
2802       assertTrue(Bytes.equals(current.getRowArray(), current.getRowOffset(),
2803           current.getRowLength(), row2, 0, row2.length));
2804       assertEquals(current.getTimestamp(), 127l);
2805     } finally {
2806       if (table != null) {
2807         table.close();
2808       }
2809     }
2810   }
2811   @Test
2812   public void testDeleteColumnAndDeleteFamilylSpecificTimeStampWithMulipleVersion()
2813       throws Exception {
2814     setAuths();
2815     TableName tableName = TableName.valueOf(TEST_NAME.getMethodName());
2816     HTable table = null;
2817     try {
2818       // Do not flush here.
2819       table = doPuts(tableName);
2820       PrivilegedExceptionAction<Void> actiona = new PrivilegedExceptionAction<Void>() {
2821         public Void run() throws Exception {
2822           try {
2823             HTable table = new HTable(conf, TEST_NAME.getMethodName());
2824             Delete d = new Delete(row1);
2825             d.setCellVisibility(new CellVisibility(SECRET + "&" + TOPSECRET));
2826             d.deleteColumn(fam, qual, 125l);
2827             table.delete(d);
2828           } catch (Throwable t) {
2829             throw new IOException(t);
2830           }
2831           return null;
2832         }
2833       };
2834       SUPERUSER.runAs(actiona);
2835 
2836       Scan s = new Scan();
2837       s.setMaxVersions(5);
2838       s.setAuthorizations(new Authorizations(SECRET, PRIVATE, CONFIDENTIAL, TOPSECRET));
2839       ResultScanner scanner = table.getScanner(s);
2840       Result[] next = scanner.next(3);
2841       assertTrue(next.length == 2);
2842       CellScanner cellScanner = next[0].cellScanner();
2843       cellScanner.advance();
2844       Cell current = cellScanner.current();
2845       assertTrue(Bytes.equals(current.getRowArray(), current.getRowOffset(),
2846           current.getRowLength(), row1, 0, row1.length));
2847       assertEquals(current.getTimestamp(), 127l);
2848       cellScanner.advance();
2849       current = cellScanner.current();
2850       assertTrue(Bytes.equals(current.getRowArray(), current.getRowOffset(),
2851           current.getRowLength(), row1, 0, row1.length));
2852       assertEquals(current.getTimestamp(), 126l);
2853       cellScanner.advance();
2854       current = cellScanner.current();
2855       assertTrue(Bytes.equals(current.getRowArray(), current.getRowOffset(),
2856           current.getRowLength(), row1, 0, row1.length));
2857       assertEquals(current.getTimestamp(), 124l);
2858       cellScanner.advance();
2859       current = cellScanner.current();
2860       assertTrue(Bytes.equals(current.getRowArray(), current.getRowOffset(),
2861           current.getRowLength(), row1, 0, row1.length));
2862       assertEquals(current.getTimestamp(), 123l);
2863       cellScanner = next[1].cellScanner();
2864       cellScanner.advance();
2865       current = cellScanner.current();
2866       assertTrue(Bytes.equals(current.getRowArray(), current.getRowOffset(),
2867           current.getRowLength(), row2, 0, row2.length));
2868 
2869       // Issue 2nd delete
2870       actiona = new PrivilegedExceptionAction<Void>() {
2871         public Void run() throws Exception {
2872           try {
2873             HTable table = new HTable(conf, TEST_NAME.getMethodName());
2874             Delete d = new Delete(row1);
2875             d.setCellVisibility(new CellVisibility("(" + CONFIDENTIAL + "&" + PRIVATE + ")|("
2876                 + TOPSECRET + "&" + SECRET+")"));
2877             d.deleteFamily(fam, 124l);
2878             table.delete(d);
2879           } catch (Throwable t) {
2880             throw new IOException(t);
2881           }
2882           return null;
2883         }
2884       };
2885       SUPERUSER.runAs(actiona);
2886       s = new Scan();
2887       s.setMaxVersions(5);
2888       s.setAuthorizations(new Authorizations(SECRET, PRIVATE, CONFIDENTIAL, TOPSECRET));
2889       scanner = table.getScanner(s);
2890       next = scanner.next(3);
2891       assertTrue(next.length == 2);
2892       cellScanner = next[0].cellScanner();
2893       cellScanner.advance();
2894       current = cellScanner.current();
2895       assertTrue(Bytes.equals(current.getRowArray(), current.getRowOffset(),
2896           current.getRowLength(), row1, 0, row1.length));
2897       assertEquals(current.getTimestamp(), 127l);
2898       cellScanner.advance();
2899       current = cellScanner.current();
2900       assertTrue(Bytes.equals(current.getRowArray(), current.getRowOffset(),
2901           current.getRowLength(), row1, 0, row1.length));
2902       assertEquals(current.getTimestamp(), 126l);
2903       cellScanner = next[1].cellScanner();
2904       cellScanner.advance();
2905       current = cellScanner.current();
2906       assertTrue(Bytes.equals(current.getRowArray(), current.getRowOffset(),
2907           current.getRowLength(), row2, 0, row2.length));
2908       assertEquals(current.getTimestamp(), 127l);
2909     } finally {
2910       if (table != null) {
2911         table.close();
2912       }
2913     }
2914   }
2915 
2916   private void setAuths() throws IOException, InterruptedException {
2917     PrivilegedExceptionAction<VisibilityLabelsResponse> action =
2918         new PrivilegedExceptionAction<VisibilityLabelsResponse>() {
2919       public VisibilityLabelsResponse run() throws Exception {
2920         try {
2921           return VisibilityClient.setAuths(conf, new String[] { CONFIDENTIAL, PRIVATE, SECRET,
2922               TOPSECRET }, SUPERUSER.getShortName());
2923         } catch (Throwable e) {
2924         }
2925         return null;
2926       }
2927     };
2928     SUPERUSER.runAs(action);
2929   }
2930 
2931   @Test
2932   public void testDiffDeleteTypesForTheSameCellUsingMultipleVersions() throws Exception {
2933     setAuths();
2934     TableName tableName = TableName.valueOf(TEST_NAME.getMethodName());
2935     HTable table = null;
2936     try {
2937       // Do not flush here.
2938       table = doPuts(tableName);
2939       PrivilegedExceptionAction<Void> actiona = new PrivilegedExceptionAction<Void>() {
2940         public Void run() throws Exception {
2941           try {
2942             HTable table = new HTable(conf, TEST_NAME.getMethodName());
2943             Delete d = new Delete(row1);
2944             d.setCellVisibility(new CellVisibility("(" + PRIVATE + "&" + CONFIDENTIAL + ")|("
2945                 + TOPSECRET + "&" + SECRET+")"));
2946             d.deleteColumns(fam, qual, 125l);
2947             table.delete(d);
2948           } catch (Throwable t) {
2949             throw new IOException(t);
2950           }
2951           return null;
2952         }
2953       };
2954       SUPERUSER.runAs(actiona);
2955 
2956       Scan s = new Scan();
2957       s.setMaxVersions(5);
2958       s.setAuthorizations(new Authorizations(SECRET, PRIVATE, CONFIDENTIAL, TOPSECRET));
2959       ResultScanner scanner = table.getScanner(s);
2960       Result[] next = scanner.next(3);
2961       assertTrue(next.length == 2);
2962       CellScanner cellScanner = next[0].cellScanner();
2963       cellScanner.advance();
2964       Cell current = cellScanner.current();
2965       assertTrue(Bytes.equals(current.getRowArray(), current.getRowOffset(),
2966           current.getRowLength(), row1, 0, row1.length));
2967       assertEquals(current.getTimestamp(), 127l);
2968       cellScanner.advance();
2969       current = cellScanner.current();
2970       assertTrue(Bytes.equals(current.getRowArray(), current.getRowOffset(),
2971           current.getRowLength(), row1, 0, row1.length));
2972       assertEquals(current.getTimestamp(), 126l);
2973       cellScanner.advance();
2974       current = cellScanner.current();
2975       assertTrue(Bytes.equals(current.getRowArray(), current.getRowOffset(),
2976           current.getRowLength(), row1, 0, row1.length));
2977       assertEquals(current.getTimestamp(), 125l);
2978       cellScanner.advance();
2979       current = cellScanner.current();
2980       assertTrue(Bytes.equals(current.getRowArray(), current.getRowOffset(),
2981           current.getRowLength(), row1, 0, row1.length));
2982       assertEquals(current.getTimestamp(), 123l);
2983       cellScanner = next[1].cellScanner();
2984       cellScanner.advance();
2985       current = cellScanner.current();
2986       assertTrue(Bytes.equals(current.getRowArray(), current.getRowOffset(),
2987           current.getRowLength(), row2, 0, row2.length));
2988 
2989       // Issue 2nd delete
2990       actiona = new PrivilegedExceptionAction<Void>() {
2991         public Void run() throws Exception {
2992           try {
2993             HTable table = new HTable(conf, TEST_NAME.getMethodName());
2994             Delete d = new Delete(row1);
2995             d.setCellVisibility(new CellVisibility("(" + CONFIDENTIAL + "&" + PRIVATE + ")|("
2996                 + TOPSECRET + "&" + SECRET+")"));
2997             d.deleteColumn(fam, qual, 127l);
2998             table.delete(d);
2999           } catch (Throwable t) {
3000             throw new IOException(t);
3001           }
3002           return null;
3003         }
3004       };
3005       SUPERUSER.runAs(actiona);
3006       s = new Scan();
3007       s.setMaxVersions(5);
3008       s.setAuthorizations(new Authorizations(SECRET, PRIVATE, CONFIDENTIAL, TOPSECRET));
3009       scanner = table.getScanner(s);
3010       next = scanner.next(3);
3011       assertTrue(next.length == 2);
3012       cellScanner = next[0].cellScanner();
3013       cellScanner.advance();
3014       current = cellScanner.current();
3015       assertTrue(Bytes.equals(current.getRowArray(), current.getRowOffset(),
3016           current.getRowLength(), row1, 0, row1.length));
3017       assertEquals(current.getTimestamp(), 126l);
3018       cellScanner.advance();
3019       current = cellScanner.current();
3020       assertTrue(Bytes.equals(current.getRowArray(), current.getRowOffset(),
3021           current.getRowLength(), row1, 0, row1.length));
3022       assertEquals(current.getTimestamp(), 125l);
3023       cellScanner.advance();
3024       current = cellScanner.current();
3025       assertTrue(Bytes.equals(current.getRowArray(), current.getRowOffset(),
3026           current.getRowLength(), row1, 0, row1.length));
3027       assertEquals(current.getTimestamp(), 123l);
3028       cellScanner = next[1].cellScanner();
3029       cellScanner.advance();
3030       current = cellScanner.current();
3031       assertTrue(Bytes.equals(current.getRowArray(), current.getRowOffset(),
3032           current.getRowLength(), row2, 0, row2.length));
3033     } finally {
3034       if (table != null) {
3035         table.close();
3036       }
3037     }
3038   }
3039 
3040   @Test
3041   public void testDeleteColumnLatestWithNoCellVisibility() throws Exception {
3042     setAuths();
3043     TableName tableName = TableName.valueOf(TEST_NAME.getMethodName());
3044     HTable table = null;
3045     try {
3046       table = doPuts(tableName);
3047       TEST_UTIL.getHBaseAdmin().flush(tableName.getNameAsString());
3048       PrivilegedExceptionAction<Void> actiona = new PrivilegedExceptionAction<Void>() {
3049         public Void run() throws Exception {
3050           try {
3051             HTable table = new HTable(conf, TEST_NAME.getMethodName());
3052             Delete d = new Delete(row1);
3053             d.deleteColumn(fam, qual, 125l);
3054             table.delete(d);
3055           } catch (Throwable t) {
3056             throw new IOException(t);
3057           }
3058           return null;
3059         }
3060       };
3061       SUPERUSER.runAs(actiona);
3062 
3063       TEST_UTIL.getHBaseAdmin().flush(tableName.getNameAsString());
3064       Scan s = new Scan();
3065       s.setMaxVersions(5);
3066       s.setAuthorizations(new Authorizations(SECRET, PRIVATE, CONFIDENTIAL, TOPSECRET));
3067       ResultScanner scanner = table.getScanner(s);
3068       Result[] next = scanner.next(3);
3069       assertTrue(next.length == 2);
3070       scanAll(next);
3071       actiona = new PrivilegedExceptionAction<Void>() {
3072         public Void run() throws Exception {
3073           try {
3074             HTable table = new HTable(conf, TEST_NAME.getMethodName());
3075             Delete d = new Delete(row1);
3076             d.deleteColumns(fam, qual, 125l);
3077             table.delete(d);
3078           } catch (Throwable t) {
3079             throw new IOException(t);
3080           }
3081           return null;
3082         }
3083       };
3084       SUPERUSER.runAs(actiona);
3085 
3086       TEST_UTIL.getHBaseAdmin().flush(tableName.getNameAsString());
3087       s = new Scan();
3088       s.setMaxVersions(5);
3089       s.setAuthorizations(new Authorizations(SECRET, PRIVATE, CONFIDENTIAL, TOPSECRET));
3090       scanner = table.getScanner(s);
3091       next = scanner.next(3);
3092       assertTrue(next.length == 2);
3093       scanAll(next);
3094 
3095       actiona = new PrivilegedExceptionAction<Void>() {
3096         public Void run() throws Exception {
3097           try {
3098             HTable table = new HTable(conf, TEST_NAME.getMethodName());
3099             Delete d = new Delete(row1);
3100             d.deleteFamily(fam, 125l);
3101             table.delete(d);
3102           } catch (Throwable t) {
3103             throw new IOException(t);
3104           }
3105           return null;
3106         }
3107       };
3108       SUPERUSER.runAs(actiona);
3109 
3110       TEST_UTIL.getHBaseAdmin().flush(tableName.getNameAsString());
3111       s = new Scan();
3112       s.setMaxVersions(5);
3113       s.setAuthorizations(new Authorizations(SECRET, PRIVATE, CONFIDENTIAL, TOPSECRET));
3114       scanner = table.getScanner(s);
3115       next = scanner.next(3);
3116       assertTrue(next.length == 2);
3117       scanAll(next);
3118 
3119       actiona = new PrivilegedExceptionAction<Void>() {
3120         public Void run() throws Exception {
3121           try {
3122             HTable table = new HTable(conf, TEST_NAME.getMethodName());
3123             Delete d = new Delete(row1);
3124             d.deleteFamily(fam);
3125             table.delete(d);
3126           } catch (Throwable t) {
3127             throw new IOException(t);
3128           }
3129           return null;
3130         }
3131       };
3132       SUPERUSER.runAs(actiona);
3133 
3134       TEST_UTIL.getHBaseAdmin().flush(tableName.getNameAsString());
3135       s = new Scan();
3136       s.setMaxVersions(5);
3137       s.setAuthorizations(new Authorizations(SECRET, PRIVATE, CONFIDENTIAL, TOPSECRET));
3138       scanner = table.getScanner(s);
3139       next = scanner.next(3);
3140       assertTrue(next.length == 2);
3141       scanAll(next);
3142 
3143       actiona = new PrivilegedExceptionAction<Void>() {
3144         public Void run() throws Exception {
3145           try {
3146             HTable table = new HTable(conf, TEST_NAME.getMethodName());
3147             Delete d = new Delete(row1);
3148             d.deleteColumns(fam, qual);
3149             table.delete(d);
3150           } catch (Throwable t) {
3151             throw new IOException(t);
3152           }
3153           return null;
3154         }
3155       };
3156       SUPERUSER.runAs(actiona);
3157 
3158       TEST_UTIL.getHBaseAdmin().flush(tableName.getNameAsString());
3159       s = new Scan();
3160       s.setMaxVersions(5);
3161       s.setAuthorizations(new Authorizations(SECRET, PRIVATE, CONFIDENTIAL, TOPSECRET));
3162       scanner = table.getScanner(s);
3163       next = scanner.next(3);
3164       assertTrue(next.length == 2);
3165       scanAll(next);
3166 
3167       actiona = new PrivilegedExceptionAction<Void>() {
3168         public Void run() throws Exception {
3169           try {
3170             HTable table = new HTable(conf, TEST_NAME.getMethodName());
3171             Delete d = new Delete(row1);
3172             d.deleteFamilyVersion(fam, 126l);
3173             table.delete(d);
3174           } catch (Throwable t) {
3175             throw new IOException(t);
3176           }
3177           return null;
3178         }
3179       };
3180       SUPERUSER.runAs(actiona);
3181 
3182       TEST_UTIL.getHBaseAdmin().flush(tableName.getNameAsString());
3183       s = new Scan();
3184       s.setMaxVersions(5);
3185       s.setAuthorizations(new Authorizations(SECRET, PRIVATE, CONFIDENTIAL, TOPSECRET));
3186       scanner = table.getScanner(s);
3187       next = scanner.next(3);
3188       assertTrue(next.length == 2);
3189       scanAll(next);
3190     } finally {
3191       if (table != null) {
3192         table.close();
3193       }
3194     }
3195   }
3196 
3197   private void scanAll(Result[] next) throws IOException {
3198     CellScanner cellScanner = next[0].cellScanner();
3199     cellScanner.advance();
3200     Cell current = cellScanner.current();
3201     assertTrue(Bytes.equals(current.getRowArray(), current.getRowOffset(), current.getRowLength(),
3202         row1, 0, row1.length));
3203     assertEquals(current.getTimestamp(), 127l);
3204     cellScanner.advance();
3205     current = cellScanner.current();
3206     assertTrue(Bytes.equals(current.getRowArray(), current.getRowOffset(), current.getRowLength(),
3207         row1, 0, row1.length));
3208     assertEquals(current.getTimestamp(), 126l);
3209     cellScanner.advance();
3210     current = cellScanner.current();
3211     assertTrue(Bytes.equals(current.getRowArray(), current.getRowOffset(), current.getRowLength(),
3212         row1, 0, row1.length));
3213     assertEquals(current.getTimestamp(), 125l);
3214     cellScanner.advance();
3215     current = cellScanner.current();
3216     assertTrue(Bytes.equals(current.getRowArray(), current.getRowOffset(), current.getRowLength(),
3217         row1, 0, row1.length));
3218     assertEquals(current.getTimestamp(), 124l);
3219     cellScanner.advance();
3220     current = cellScanner.current();
3221     assertTrue(Bytes.equals(current.getRowArray(), current.getRowOffset(), current.getRowLength(),
3222         row1, 0, row1.length));
3223     assertEquals(current.getTimestamp(), 123l);
3224     cellScanner = next[1].cellScanner();
3225     cellScanner.advance();
3226     current = cellScanner.current();
3227     assertTrue(Bytes.equals(current.getRowArray(), current.getRowOffset(), current.getRowLength(),
3228         row2, 0, row2.length));
3229   }
3230 
3231   @Test
3232   public void testVisibilityExpressionWithNotEqualORCondition() throws Exception {
3233     setAuths();
3234     TableName tableName = TableName.valueOf(TEST_NAME.getMethodName());
3235     HTable table = null;
3236     try {
3237       HBaseAdmin hBaseAdmin = TEST_UTIL.getHBaseAdmin();
3238       HColumnDescriptor colDesc = new HColumnDescriptor(fam);
3239       colDesc.setMaxVersions(5);
3240       HTableDescriptor desc = new HTableDescriptor(tableName);
3241       desc.addFamily(colDesc);
3242       hBaseAdmin.createTable(desc);
3243       table = new HTable(conf, tableName);
3244       Put put = new Put(Bytes.toBytes("row1"));
3245       put.add(fam, qual, 123l, value);
3246       put.setCellVisibility(new CellVisibility(CONFIDENTIAL));
3247       table.put(put);
3248       put = new Put(Bytes.toBytes("row1"));
3249       put.add(fam, qual, 124l, value);
3250       put.setCellVisibility(new CellVisibility(CONFIDENTIAL + "|" + PRIVATE));
3251       table.put(put);
3252       TEST_UTIL.getHBaseAdmin().flush(tableName.getNameAsString());
3253       PrivilegedExceptionAction<Void> actiona = new PrivilegedExceptionAction<Void>() {
3254         public Void run() throws Exception {
3255           try {
3256             HTable table = new HTable(conf, TEST_NAME.getMethodName());
3257             Delete d = new Delete(row1);
3258             d.deleteColumn(fam, qual, 124l);
3259             d.setCellVisibility(new CellVisibility(PRIVATE ));
3260             table.delete(d);
3261           } catch (Throwable t) {
3262             throw new IOException(t);
3263           }
3264           return null;
3265         }
3266       };
3267       SUPERUSER.runAs(actiona);
3268 
3269       TEST_UTIL.getHBaseAdmin().flush(tableName.getNameAsString());
3270       Scan s = new Scan();
3271       s.setMaxVersions(5);
3272       s.setAuthorizations(new Authorizations(SECRET, PRIVATE, CONFIDENTIAL, TOPSECRET));
3273       ResultScanner scanner = table.getScanner(s);
3274       Result[] next = scanner.next(3);
3275       assertTrue(next.length == 1);
3276       CellScanner cellScanner = next[0].cellScanner();
3277       cellScanner.advance();
3278       Cell current = cellScanner.current();
3279       assertTrue(Bytes.equals(current.getRowArray(), current.getRowOffset(),
3280           current.getRowLength(), row1, 0, row1.length));
3281       assertEquals(current.getTimestamp(), 124l);
3282       cellScanner.advance();
3283       current = cellScanner.current();
3284       assertTrue(Bytes.equals(current.getRowArray(), current.getRowOffset(),
3285           current.getRowLength(), row1, 0, row1.length));
3286       assertEquals(current.getTimestamp(), 123l);
3287     } finally {
3288       if (table != null) {
3289         table.close();
3290       }
3291     }
3292   }
3293 
3294   @Test
3295   public void testDeleteWithNoVisibilitiesForPutsAndDeletes() throws Exception {
3296     final TableName tableName = TableName.valueOf(TEST_NAME.getMethodName());
3297     HBaseAdmin hBaseAdmin = TEST_UTIL.getHBaseAdmin();
3298     HColumnDescriptor colDesc = new HColumnDescriptor(fam);
3299     colDesc.setMaxVersions(5);
3300     HTableDescriptor desc = new HTableDescriptor(tableName);
3301     desc.addFamily(colDesc);
3302     hBaseAdmin.createTable(desc);
3303     HTable table = new HTable(conf, tableName);
3304     try {
3305       Put p = new Put(Bytes.toBytes("row1"));
3306       p.add(fam, qual, value);
3307       table.put(p);
3308       p = new Put(Bytes.toBytes("row1"));
3309       p.add(fam, qual1, value);
3310       table.put(p);
3311       p = new Put(Bytes.toBytes("row2"));
3312       p.add(fam, qual, value);
3313       table.put(p);
3314       p = new Put(Bytes.toBytes("row2"));
3315       p.add(fam, qual1, value);
3316       table.put(p);
3317       Delete d = new Delete(Bytes.toBytes("row1"));
3318       table.delete(d);
3319       Get g = new Get(Bytes.toBytes("row1"));
3320       g.setMaxVersions();
3321       g.setAuthorizations(new Authorizations(SECRET, PRIVATE));
3322       Result result = table.get(g);
3323       assertEquals(0, result.rawCells().length);
3324 
3325       p = new Put(Bytes.toBytes("row1"));
3326       p.add(fam, qual, value);
3327       table.put(p);
3328       result = table.get(g);
3329       assertEquals(1, result.rawCells().length);
3330     } finally {
3331       table.close();
3332     }
3333   }
3334 
3335   @Test
3336   public void testDeleteWithFamilyDeletesOfSameTsButDifferentVisibilities() throws Exception {
3337     final TableName tableName = TableName.valueOf(TEST_NAME.getMethodName());
3338     HBaseAdmin hBaseAdmin = TEST_UTIL.getHBaseAdmin();
3339     HColumnDescriptor colDesc = new HColumnDescriptor(fam);
3340     colDesc.setMaxVersions(5);
3341     HTableDescriptor desc = new HTableDescriptor(tableName);
3342     desc.addFamily(colDesc);
3343     hBaseAdmin.createTable(desc);
3344     HTable table = new HTable(conf, tableName);
3345     long t1 = 1234L;
3346     CellVisibility cellVisibility1 = new CellVisibility(SECRET);
3347     CellVisibility cellVisibility2 = new CellVisibility(PRIVATE);
3348     try {
3349       // Cell row1:info:qual:1234 with visibility SECRET
3350       Put p = new Put(row1);
3351       p.add(fam, qual, t1, value);
3352       p.setCellVisibility(cellVisibility1);
3353       table.put(p);
3354 
3355       // Cell row1:info:qual1:1234 with visibility PRIVATE
3356       p = new Put(row1);
3357       p.add(fam, qual1, t1, value);
3358       p.setCellVisibility(cellVisibility2);
3359       table.put(p);
3360 
3361       Delete d = new Delete(row1);
3362       d.deleteFamily(fam, t1);
3363       d.setCellVisibility(cellVisibility2);
3364       table.delete(d);
3365       d = new Delete(row1);
3366       d.deleteFamily(fam, t1);
3367       d.setCellVisibility(cellVisibility1);
3368       table.delete(d);
3369 
3370       Get g = new Get(row1);
3371       g.setMaxVersions();
3372       g.setAuthorizations(new Authorizations(SECRET, PRIVATE));
3373       Result result = table.get(g);
3374       assertEquals(0, result.rawCells().length);
3375 
3376       // Cell row2:info:qual:1234 with visibility SECRET
3377       p = new Put(row2);
3378       p.add(fam, qual, t1, value);
3379       p.setCellVisibility(cellVisibility1);
3380       table.put(p);
3381 
3382       // Cell row2:info:qual1:1234 with visibility PRIVATE
3383       p = new Put(row2);
3384       p.add(fam, qual1, t1, value);
3385       p.setCellVisibility(cellVisibility2);
3386       table.put(p);
3387 
3388       d = new Delete(row2);
3389       d.deleteFamilyVersion(fam, t1);
3390       d.setCellVisibility(cellVisibility2);
3391       table.delete(d);
3392       d = new Delete(row2);
3393       d.deleteFamilyVersion(fam, t1);
3394       d.setCellVisibility(cellVisibility1);
3395       table.delete(d);
3396 
3397       g = new Get(row2);
3398       g.setMaxVersions();
3399       g.setAuthorizations(new Authorizations(SECRET, PRIVATE));
3400       result = table.get(g);
3401       assertEquals(0, result.rawCells().length);
3402     } finally {
3403       table.close();
3404     }
3405   }
3406 
3407   public static HTable createTableAndWriteDataWithLabels(TableName tableName, String... labelExps)
3408       throws Exception {
3409     HTable table = null;
3410     table = TEST_UTIL.createTable(tableName, fam);
3411     int i = 1;
3412     List<Put> puts = new ArrayList<Put>();
3413     for (String labelExp : labelExps) {
3414       Put put = new Put(Bytes.toBytes("row" + i));
3415       put.add(fam, qual, HConstants.LATEST_TIMESTAMP, value);
3416       put.setCellVisibility(new CellVisibility(labelExp));
3417       puts.add(put);
3418       table.put(put);
3419       i++;
3420     }
3421     // table.put(puts);
3422     return table;
3423   }
3424 
3425   public static HTable createTableAndWriteDataWithLabels(TableName tableName, long[] timestamp,
3426       String... labelExps) throws Exception {
3427     HTable table = null;
3428     table = TEST_UTIL.createTable(tableName, fam);
3429     int i = 1;
3430     List<Put> puts = new ArrayList<Put>();
3431     for (String labelExp : labelExps) {
3432       Put put = new Put(Bytes.toBytes("row" + i));
3433       put.add(fam, qual, timestamp[i - 1], value);
3434       put.setCellVisibility(new CellVisibility(labelExp));
3435       puts.add(put);
3436       table.put(put);
3437       TEST_UTIL.getHBaseAdmin().flush(tableName.getNameAsString());
3438       i++;
3439     }
3440     return table;
3441   }
3442 
3443   public static void addLabels() throws Exception {
3444     PrivilegedExceptionAction<VisibilityLabelsResponse> action =
3445         new PrivilegedExceptionAction<VisibilityLabelsResponse>() {
3446       public VisibilityLabelsResponse run() throws Exception {
3447         String[] labels = { SECRET, TOPSECRET, CONFIDENTIAL, PUBLIC, PRIVATE };
3448         try {
3449           VisibilityClient.addLabels(conf, labels);
3450         } catch (Throwable t) {
3451           throw new IOException(t);
3452         }
3453         return null;
3454       }
3455     };
3456     SUPERUSER.runAs(action);
3457   }
3458 }