Search in sources :

Example 1 with Table2

use of org.apache.cayenne.crypto.db.Table2 in project cayenne by apache.

the class Runtime_AES128_GZIP_IT method testInsert.

@Test
public void testInsert() throws SQLException {
    ObjectContext context = runtime.newContext();
    // make sure compression is on...
    byte[] cryptoBytes = CryptoUnitUtils.bytesOfSize(GZIP_THRESHOLD + 100);
    Table2 t1 = context.newObject(Table2.class);
    t1.setPlainBytes("plain_1".getBytes());
    t1.setCryptoBytes(cryptoBytes);
    context.commitChanges();
    Object[] data = table2.select();
    assertArrayEquals("plain_1".getBytes(), (byte[]) data[1]);
    Header h = Header.create((byte[]) data[2], 0);
    assertTrue(h.isCompressed());
    assertArrayEquals(cryptoBytes, CryptoUnitUtils.gunzip(CryptoUnitUtils.decrypt_AES_CBC((byte[]) data[2], runtime)));
}
Also used : Header(org.apache.cayenne.crypto.transformer.bytes.Header) Table2(org.apache.cayenne.crypto.db.Table2) ObjectContext(org.apache.cayenne.ObjectContext) Test(org.junit.Test)

Example 2 with Table2

use of org.apache.cayenne.crypto.db.Table2 in project cayenne by apache.

the class Runtime_AES128_IT method testInsert_MultipleObjects.

@Test
public void testInsert_MultipleObjects() throws SQLException {
    ObjectContext context = runtime.newContext();
    Table2 t1 = context.newObject(Table2.class);
    t1.setPlainBytes("a".getBytes());
    t1.setCryptoBytes("crypto_1".getBytes());
    Table2 t2 = context.newObject(Table2.class);
    t2.setPlainBytes("b".getBytes());
    t2.setCryptoBytes("crypto_2".getBytes());
    Table2 t3 = context.newObject(Table2.class);
    t3.setPlainBytes("c".getBytes());
    t3.setCryptoBytes(null);
    context.commitChanges();
    List<Object[]> data = table2.selectAll();
    assertEquals(3, data.size());
    Map<String, byte[]> cipherByPlain = new HashMap<>();
    for (Object[] r : data) {
        cipherByPlain.put(new String((byte[]) r[1]), (byte[]) r[2]);
    }
    assertArrayEquals("crypto_1".getBytes(), CryptoUnitUtils.decrypt_AES_CBC(cipherByPlain.get("a"), runtime));
    assertArrayEquals("crypto_2".getBytes(), CryptoUnitUtils.decrypt_AES_CBC(cipherByPlain.get("b"), runtime));
    assertNull(cipherByPlain.get("c"));
}
Also used : HashMap(java.util.HashMap) Table2(org.apache.cayenne.crypto.db.Table2) ObjectContext(org.apache.cayenne.ObjectContext) Test(org.junit.Test)

Example 3 with Table2

use of org.apache.cayenne.crypto.db.Table2 in project cayenne by apache.

the class Runtime_AES128_GZIP_IT method test_SelectQuery.

@Test
public void test_SelectQuery() throws SQLException {
    // make sure compression is on...
    byte[] cryptoBytes1 = CryptoUnitUtils.bytesOfSize(GZIP_THRESHOLD + 101);
    byte[] cryptoBytes2 = CryptoUnitUtils.bytesOfSize(GZIP_THRESHOLD + 102);
    ObjectContext context = runtime.newContext();
    Table2 t1 = context.newObject(Table2.class);
    t1.setPlainBytes("a".getBytes());
    t1.setCryptoBytes(cryptoBytes1);
    Table2 t2 = context.newObject(Table2.class);
    t2.setPlainBytes("b".getBytes());
    t2.setCryptoBytes(cryptoBytes2);
    Table2 t3 = context.newObject(Table2.class);
    t3.setPlainBytes("c".getBytes());
    t3.setCryptoBytes(null);
    context.commitChanges();
    SelectQuery<Table2> select = SelectQuery.query(Table2.class);
    select.addOrdering(Table2.PLAIN_BYTES.asc());
    List<Table2> result = runtime.newContext().select(select);
    assertEquals(3, result.size());
    assertArrayEquals(cryptoBytes1, result.get(0).getCryptoBytes());
    assertArrayEquals(cryptoBytes2, result.get(1).getCryptoBytes());
    assertArrayEquals(null, result.get(2).getCryptoBytes());
}
Also used : Table2(org.apache.cayenne.crypto.db.Table2) ObjectContext(org.apache.cayenne.ObjectContext) Test(org.junit.Test)

Example 4 with Table2

use of org.apache.cayenne.crypto.db.Table2 in project cayenne by apache.

the class Runtime_AES128_GZIP_IT method testInsert_Small.

@Test
public void testInsert_Small() throws SQLException {
    ObjectContext context = runtime.newContext();
    // make sure compression is on...
    byte[] cryptoBytes = CryptoUnitUtils.bytesOfSize(GZIP_THRESHOLD - 20);
    Table2 t1 = context.newObject(Table2.class);
    t1.setPlainBytes("plain_1".getBytes());
    t1.setCryptoBytes(cryptoBytes);
    context.commitChanges();
    Object[] data = table2.select();
    assertArrayEquals("plain_1".getBytes(), (byte[]) data[1]);
    Header h = Header.create((byte[]) data[2], 0);
    assertFalse(h.isCompressed());
    assertArrayEquals(cryptoBytes, CryptoUnitUtils.decrypt_AES_CBC((byte[]) data[2], runtime));
}
Also used : Header(org.apache.cayenne.crypto.transformer.bytes.Header) Table2(org.apache.cayenne.crypto.db.Table2) ObjectContext(org.apache.cayenne.ObjectContext) Test(org.junit.Test)

Example 5 with Table2

use of org.apache.cayenne.crypto.db.Table2 in project cayenne by apache.

the class Runtime_AES128_GZIP_IT method testInsert_MultipleObjects.

@Test
public void testInsert_MultipleObjects() throws SQLException {
    ObjectContext context = runtime.newContext();
    // make sure compression is on...
    byte[] cryptoBytes1 = CryptoUnitUtils.bytesOfSize(GZIP_THRESHOLD + 101);
    byte[] cryptoBytes2 = CryptoUnitUtils.bytesOfSize(GZIP_THRESHOLD + 102);
    Table2 t1 = context.newObject(Table2.class);
    t1.setPlainBytes("a".getBytes());
    t1.setCryptoBytes(cryptoBytes1);
    Table2 t2 = context.newObject(Table2.class);
    t2.setPlainBytes("b".getBytes());
    t2.setCryptoBytes(cryptoBytes2);
    Table2 t3 = context.newObject(Table2.class);
    t3.setPlainBytes("c".getBytes());
    t3.setCryptoBytes(null);
    context.commitChanges();
    List<Object[]> data = table2.selectAll();
    assertEquals(3, data.size());
    Map<String, byte[]> cipherByPlain = new HashMap<>();
    for (Object[] r : data) {
        cipherByPlain.put(new String((byte[]) r[1]), (byte[]) r[2]);
    }
    assertArrayEquals(cryptoBytes1, CryptoUnitUtils.gunzip(CryptoUnitUtils.decrypt_AES_CBC(cipherByPlain.get("a"), runtime)));
    assertArrayEquals(cryptoBytes2, CryptoUnitUtils.gunzip(CryptoUnitUtils.decrypt_AES_CBC(cipherByPlain.get("b"), runtime)));
    assertNull(cipherByPlain.get("c"));
}
Also used : HashMap(java.util.HashMap) Table2(org.apache.cayenne.crypto.db.Table2) ObjectContext(org.apache.cayenne.ObjectContext) Test(org.junit.Test)

Aggregations

ObjectContext (org.apache.cayenne.ObjectContext)7 Table2 (org.apache.cayenne.crypto.db.Table2)7 Test (org.junit.Test)7 HashMap (java.util.HashMap)2 Header (org.apache.cayenne.crypto.transformer.bytes.Header)2