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