You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@accumulo.apache.org by ct...@apache.org on 2013/06/10 18:38:23 UTC

svn commit: r1491529 - in /accumulo/trunk/core/src: main/java/org/apache/accumulo/core/security/Authorizations.java main/java/org/apache/accumulo/core/util/ByteBufferUtil.java test/java/org/apache/accumulo/core/security/AuthorizationsTest.java

Author: ctubbsii
Date: Mon Jun 10 16:38:22 2013
New Revision: 1491529

URL: http://svn.apache.org/r1491529
Log:
ACCUMULO-891 apply v2.patch with reservations, in case it needs to be rolled back due to performance impact

Modified:
    accumulo/trunk/core/src/main/java/org/apache/accumulo/core/security/Authorizations.java
    accumulo/trunk/core/src/main/java/org/apache/accumulo/core/util/ByteBufferUtil.java
    accumulo/trunk/core/src/test/java/org/apache/accumulo/core/security/AuthorizationsTest.java

Modified: accumulo/trunk/core/src/main/java/org/apache/accumulo/core/security/Authorizations.java
URL: http://svn.apache.org/viewvc/accumulo/trunk/core/src/main/java/org/apache/accumulo/core/security/Authorizations.java?rev=1491529&r1=1491528&r2=1491529&view=diff
==============================================================================
--- accumulo/trunk/core/src/main/java/org/apache/accumulo/core/security/Authorizations.java (original)
+++ accumulo/trunk/core/src/main/java/org/apache/accumulo/core/security/Authorizations.java Mon Jun 10 16:38:22 2013
@@ -40,7 +40,6 @@ public class Authorizations implements I
   
   private Set<ByteSequence> auths = new HashSet<ByteSequence>();
   private List<byte[]> authsList = new ArrayList<byte[]>();
-  private List<byte[]> immutableList = Collections.unmodifiableList(authsList);
   
   private static final boolean[] validAuthChars = new boolean[256];
   
@@ -183,7 +182,13 @@ public class Authorizations implements I
    * @see #Authorizations(Collection)
    */
   public List<byte[]> getAuthorizations() {
-    return immutableList;
+    ArrayList<byte[]> copy = new ArrayList<byte[]>(authsList.size());
+    for (byte[] auth : authsList) {
+      byte[] bytes = new byte[auth.length];
+      System.arraycopy(auth, 0, bytes, 0, auth.length);
+      copy.add(bytes);
+    }
+    return Collections.unmodifiableList(copy);
   }
   
   /**
@@ -192,7 +197,7 @@ public class Authorizations implements I
    * @see #Authorizations(List)
    */
   public List<ByteBuffer> getAuthorizationsBB() {
-    return ByteBufferUtil.toByteBuffers(immutableList);
+    return ByteBufferUtil.toImmutableByteBufferList(getAuthorizations());
   }
   
   @Override
@@ -262,7 +267,7 @@ public class Authorizations implements I
   
   @Override
   public Iterator<byte[]> iterator() {
-    return immutableList.iterator();
+    return getAuthorizations().iterator();
   }
   
   /**
@@ -271,7 +276,7 @@ public class Authorizations implements I
   public String serialize() {
     StringBuilder sb = new StringBuilder(HEADER);
     String sep = "";
-    for (byte[] auth : immutableList) {
+    for (byte[] auth : authsList) {
       sb.append(sep);
       sep = ",";
       sb.append(new String(Base64.encodeBase64(auth), Constants.UTF8));

Modified: accumulo/trunk/core/src/main/java/org/apache/accumulo/core/util/ByteBufferUtil.java
URL: http://svn.apache.org/viewvc/accumulo/trunk/core/src/main/java/org/apache/accumulo/core/util/ByteBufferUtil.java?rev=1491529&r1=1491528&r2=1491529&view=diff
==============================================================================
--- accumulo/trunk/core/src/main/java/org/apache/accumulo/core/util/ByteBufferUtil.java (original)
+++ accumulo/trunk/core/src/main/java/org/apache/accumulo/core/util/ByteBufferUtil.java Mon Jun 10 16:38:22 2013
@@ -20,6 +20,7 @@ import java.nio.ByteBuffer;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collection;
+import java.util.Collections;
 import java.util.List;
 
 import org.apache.hadoop.io.Text;
@@ -41,6 +42,19 @@ public class ByteBufferUtil {
     return result;
   }
   
+  /**
+   * Provides an immutable view of a list of byte buffers, representing the same bytes. This does not protect against modifying the underlying byte arrays.
+   */
+  public static List<ByteBuffer> toImmutableByteBufferList(Collection<byte[]> bytesList) {
+    if (bytesList == null)
+      return null;
+    ArrayList<ByteBuffer> result = new ArrayList<ByteBuffer>();
+    for (byte[] bytes : bytesList) {
+      result.add(ByteBuffer.wrap(bytes));
+    }
+    return Collections.unmodifiableList(result);
+  }
+  
   public static List<byte[]> toBytesList(Collection<ByteBuffer> bytesList) {
     if (bytesList == null)
       return null;

Modified: accumulo/trunk/core/src/test/java/org/apache/accumulo/core/security/AuthorizationsTest.java
URL: http://svn.apache.org/viewvc/accumulo/trunk/core/src/test/java/org/apache/accumulo/core/security/AuthorizationsTest.java?rev=1491529&r1=1491528&r2=1491529&view=diff
==============================================================================
--- accumulo/trunk/core/src/test/java/org/apache/accumulo/core/security/AuthorizationsTest.java (original)
+++ accumulo/trunk/core/src/test/java/org/apache/accumulo/core/security/AuthorizationsTest.java Mon Jun 10 16:38:22 2013
@@ -16,9 +16,12 @@
  */
 package org.apache.accumulo.core.security;
 
+import static org.junit.Assert.assertArrayEquals;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertTrue;
 
+import java.nio.ByteBuffer;
+
 import org.apache.accumulo.core.util.ByteArraySet;
 import org.junit.Test;
 
@@ -57,4 +60,42 @@ public class AuthorizationsTest {
     assertEquals(a1, a2);
     assertEquals(a1.serialize(), a2.serialize());
   }
+  
+  @Test
+  public void testDefensiveAccess() {
+    Authorizations expected = new Authorizations("foo", "a");
+    Authorizations actual = new Authorizations("foo", "a");
+    
+    // foo to goo; test defensive iterator
+    for (byte[] bytes : actual) {
+      bytes[0]++;
+    }
+    assertArrayEquals(expected.getAuthorizationsArray(), actual.getAuthorizationsArray());
+    
+    // test defensive getter and serializer
+    actual.getAuthorizations().get(0)[0]++;
+    assertArrayEquals(expected.getAuthorizationsArray(), actual.getAuthorizationsArray());
+    assertEquals(expected.serialize(), actual.serialize());
+  }
+  
+  // This should throw ReadOnlyBufferException, but THRIFT-883 requires that the ByteBuffers themselves not be read-only
+  // @Test(expected = ReadOnlyBufferException.class)
+  @Test
+  public void testReadOnlyByteBuffer() {
+    Authorizations expected = new Authorizations("foo");
+    Authorizations actual = new Authorizations("foo");
+    
+    assertArrayEquals(expected.getAuthorizationsArray(), actual.getAuthorizationsArray());
+    actual.getAuthorizationsBB().get(0).array()[0]++;
+    assertArrayEquals(expected.getAuthorizationsArray(), actual.getAuthorizationsArray());
+  }
+  
+  @Test(expected = UnsupportedOperationException.class)
+  public void testUnmodifiableList() {
+    Authorizations expected = new Authorizations("foo");
+    Authorizations actual = new Authorizations("foo");
+    
+    assertArrayEquals(expected.getAuthorizationsArray(), actual.getAuthorizationsArray());
+    actual.getAuthorizationsBB().add(ByteBuffer.wrap(new byte[] {'a'}));
+  }
 }