cayenne-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From aadamc...@apache.org
Subject svn commit: r1584622 - in /cayenne/main/trunk/cayenne-crypto/src: main/java/org/apache/cayenne/crypto/transformer/bytes/EncryptorWithKeyName.java test/java/org/apache/cayenne/crypto/transformer/bytes/EncryptorWithKeyNameTest.java
Date Fri, 04 Apr 2014 12:02:23 GMT
Author: aadamchik
Date: Fri Apr  4 12:02:22 2014
New Revision: 1584622

URL: http://svn.apache.org/r1584622
Log:
CAY-1916 cayenne-crypto module that enables data encryption for certain model attributes

BytesTransformer to encapsulate crypto protocol .. IN PROGRESS

Modified:
    cayenne/main/trunk/cayenne-crypto/src/main/java/org/apache/cayenne/crypto/transformer/bytes/EncryptorWithKeyName.java
    cayenne/main/trunk/cayenne-crypto/src/test/java/org/apache/cayenne/crypto/transformer/bytes/EncryptorWithKeyNameTest.java

Modified: cayenne/main/trunk/cayenne-crypto/src/main/java/org/apache/cayenne/crypto/transformer/bytes/EncryptorWithKeyName.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/cayenne-crypto/src/main/java/org/apache/cayenne/crypto/transformer/bytes/EncryptorWithKeyName.java?rev=1584622&r1=1584621&r2=1584622&view=diff
==============================================================================
--- cayenne/main/trunk/cayenne-crypto/src/main/java/org/apache/cayenne/crypto/transformer/bytes/EncryptorWithKeyName.java
(original)
+++ cayenne/main/trunk/cayenne-crypto/src/main/java/org/apache/cayenne/crypto/transformer/bytes/EncryptorWithKeyName.java
Fri Apr  4 12:02:22 2014
@@ -18,6 +18,8 @@
  ****************************************************************/
 package org.apache.cayenne.crypto.transformer.bytes;
 
+import org.apache.cayenne.crypto.CayenneCryptoException;
+
 public class EncryptorWithKeyName implements BytesTransformer {
 
     private BytesTransformer delegate;
@@ -28,6 +30,11 @@ public class EncryptorWithKeyName implem
         this.delegate = delegate;
         this.blockSize = blockSize;
         this.keyName = keyName;
+
+        if (blockSize != keyName.length) {
+            throw new CayenneCryptoException("keyName size is expected to be the same as
block size. Was "
+                    + keyName.length + "; block size was: " + blockSize);
+        }
     }
 
     @Override

Modified: cayenne/main/trunk/cayenne-crypto/src/test/java/org/apache/cayenne/crypto/transformer/bytes/EncryptorWithKeyNameTest.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/cayenne-crypto/src/test/java/org/apache/cayenne/crypto/transformer/bytes/EncryptorWithKeyNameTest.java?rev=1584622&r1=1584621&r2=1584622&view=diff
==============================================================================
--- cayenne/main/trunk/cayenne-crypto/src/test/java/org/apache/cayenne/crypto/transformer/bytes/EncryptorWithKeyNameTest.java
(original)
+++ cayenne/main/trunk/cayenne-crypto/src/test/java/org/apache/cayenne/crypto/transformer/bytes/EncryptorWithKeyNameTest.java
Fri Apr  4 12:02:22 2014
@@ -18,13 +18,17 @@
  ****************************************************************/
 package org.apache.cayenne.crypto.transformer.bytes;
 
+import static org.junit.Assert.assertArrayEquals;
 import static org.junit.Assert.assertEquals;
+import static org.mockito.Mockito.doAnswer;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.when;
 
 import java.io.UnsupportedEncodingException;
 
 import org.junit.Test;
+import org.mockito.invocation.InvocationOnMock;
+import org.mockito.stubbing.Answer;
 
 public class EncryptorWithKeyNameTest {
 
@@ -36,8 +40,44 @@ public class EncryptorWithKeyNameTest {
         when(delegate.getOutputSize(8)).thenReturn(8);
 
         // try with non-standard block size..
-        EncryptorWithKeyName encryptor = new EncryptorWithKeyName(delegate, keyName, 17);
-        assertEquals(25, encryptor.getOutputSize(8));
+        EncryptorWithKeyName encryptor = new EncryptorWithKeyName(delegate, keyName, 5);
+        assertEquals(13, encryptor.getOutputSize(8));
+    }
+
+    @Test
+    public void testTransform() throws UnsupportedEncodingException {
+
+        byte[] keyName = "mykey".getBytes("UTF-8");
+
+        BytesTransformer delegate = mock(BytesTransformer.class);
+        when(delegate.getOutputSize(8)).thenReturn(8);
+
+        byte[] input = { 1, 2, 3, 4, 5, 6, 7, 8 };
+        byte[] output = new byte[16];
+
+        doAnswer(new Answer<Object>() {
+            @Override
+            public Object answer(InvocationOnMock invocation) throws Throwable {
+
+                Object[] args = invocation.getArguments();
+                byte[] input = (byte[]) args[0];
+                byte[] output = (byte[]) args[1];
+                int offset = (Integer) args[2];
+
+                for (int i = 0; i < input.length; i++) {
+                    output[i + offset] = 1;
+                }
+
+                return null;
+            }
+        }).when(delegate).transform(input, output, 6);
+
+        // intentionally non-standard block size..
+        EncryptorWithKeyName encryptor = new EncryptorWithKeyName(delegate, keyName, 5);
+
+        encryptor.transform(input, output, 1);
+
+        assertArrayEquals(new byte[] { 0, 'm', 'y', 'k', 'e', 'y', 1, 1, 1, 1, 1, 1, 1, 1,
0, 0 }, output);
     }
 
 }



Mime
View raw message