Search in sources :

Example 6 with Revision

use of com.google.nigori.common.Revision in project nigori by ucam-cl-dtg.

the class CryptoNigoriDatastore method getRevisions.

@Override
public List<Revision> getRevisions(Index index) throws NigoriCryptographyException, UnsupportedEncodingException, IOException, UnauthorisedException {
    byte[] encIndex = keyManager.encryptDeterministically(index.getBytes());
    try {
        GetRevisionsResponse getResponse = protocol.getRevisions(MessageLibrary.getRevisionsRequestAsProtobuf(keyManager.getServerName(), keyManager.signer(), encIndex));
        if (getResponse == null) {
            return null;
        }
        List<ByteString> revisions = getResponse.getRevisionsList();
        List<Revision> answer = new ArrayList<Revision>(revisions.size());
        for (ByteString revision : revisions) {
            answer.add(new Revision(keyManager.decrypt(revision.toByteArray())));
        }
        return answer;
    } catch (NotFoundException e) {
        return null;
    }
}
Also used : Revision(com.google.nigori.common.Revision) GetRevisionsResponse(com.google.nigori.common.NigoriMessages.GetRevisionsResponse) ByteString(com.google.protobuf.ByteString) ArrayList(java.util.ArrayList) NotFoundException(com.google.nigori.common.NotFoundException)

Example 7 with Revision

use of com.google.nigori.common.Revision in project nigori by ucam-cl-dtg.

the class ConcurrencyTest method singleUserConcurrency.

/**
 * Test that one user can do lots of things at the same time.
 *
 * @throws NigoriCryptographyException
 * @throws IOException
 * @throws UnauthorisedException
 */
@Test
public void singleUserConcurrency() throws NigoriCryptographyException, IOException, UnauthorisedException {
    failed = false;
    Thread[] threads = new Thread[THREADS];
    final NigoriDatastore nigori = getStore();
    assertTrue("Not registered", nigori.register());
    try {
        final List<Throwable> exceptionList = Collections.synchronizedList(new LinkedList<Throwable>());
        for (int j = 0; j < THREADS; ++j) {
            threads[j] = new Thread() {

                @Override
                public void run() {
                    boolean succeeded = false;
                    try {
                        Random r = new Random();
                        for (int i = 0; i < AcceptanceTests.REPEAT * REPEAT_FACTOR; ++i) {
                            // check we can do this more than once
                            for (IndexValue iv : SetGetDeleteTest.testCases) {
                                // once round for each
                                final Index index = new Index(new byte[16]);
                                // need to generate some different indices
                                r.nextBytes(index.getBytes());
                                final byte[] value = iv.revvalue.getValue();
                                final Revision revision = iv.revvalue.getRevision();
                                assertTrue("Not put" + i, nigori.put(index, revision, value));
                                try {
                                    assertArrayEquals("Got different" + i, value, nigori.getRevision(index, revision));
                                } finally {
                                    assertTrue("Not deleted" + i, nigori.delete(index, NULL_DELETE_TOKEN));
                                }
                                assertNull("Not deleted" + i, nigori.get(index));
                            }
                        }
                        succeeded = true;
                    } catch (Throwable e) {
                        exceptionList.add(e);
                    } finally {
                        if (!succeeded && !failed) {
                            failed = true;
                        }
                    }
                }
            };
        }
        startThenJoinThreads(threads);
        ifFailedPrintFailures(failed, exceptionList);
    } finally {
        assertTrue(nigori.unregister());
    }
}
Also used : Random(java.util.Random) Revision(com.google.nigori.common.Revision) NigoriDatastore(com.google.nigori.client.NigoriDatastore) Index(com.google.nigori.common.Index) IndexValue(com.google.nigori.client.accept.n.SetGetDeleteTest.IndexValue) Test(org.junit.Test)

Example 8 with Revision

use of com.google.nigori.common.Revision in project nigori by ucam-cl-dtg.

the class SetGetDeleteTest method setGetDelete.

@Test
public void setGetDelete() throws NigoriCryptographyException, IOException, UnauthorisedException {
    NigoriDatastore nigori = getStore();
    for (int i = 0; i < AcceptanceTests.REPEAT; ++i) {
        // check we can do this more than once
        try {
            assertTrue("Not registered" + i, nigori.register());
            assertTrue("Not a clean store " + i, nigori.getIndices().isEmpty());
            for (IndexValue iv : testCases) {
                // once round for each
                final Index index = iv.index;
                final Revision revision = iv.revvalue.getRevision();
                final byte[] value = iv.revvalue.getValue();
                assertTrue("Not put " + i, nigori.put(index, revision, value));
                List<RevValue> revs = nigori.get(index);
                assertFalse("Revisions must exist" + i, revs.isEmpty());
                assertThat(revs, hasItem(iv.revvalue));
                assertEquals("Not one revision " + i, 1, revs.size());
                assertTrue("Not deleted" + i, nigori.delete(index, NULL_DELETE_TOKEN));
                assertNull("Not deleted" + i, nigori.get(index));
                assertFalse("Could redelete", nigori.delete(index, NULL_DELETE_TOKEN));
            }
            // allow them to accumulate
            for (IndexValue iv : testCases) {
                final Index index = iv.index;
                final byte[] value = iv.revvalue.getValue();
                final Revision revision = iv.revvalue.getRevision();
                assertTrue("Not put" + i, nigori.put(index, revision, value));
            }
            try {
                for (IndexValue iv : testCases) {
                    final Index index = iv.index;
                    final byte[] value = iv.revvalue.getValue();
                    final Revision revision = iv.revvalue.getRevision();
                    assertArrayEquals("Got different" + i, value, nigori.getRevision(index, revision));
                }
            } finally {
                for (IndexValue iv : testCases) {
                    final Index index = iv.index;
                    if (!iv.later) {
                        assertTrue("Not deleted" + i, nigori.delete(index, NULL_DELETE_TOKEN));
                    } else {
                        // should have already been deleted
                        assertFalse("Not deleted" + i, nigori.delete(index, NULL_DELETE_TOKEN));
                    }
                }
            }
            for (IndexValue iv : testCases) {
                final Index index = iv.index;
                assertNull("Still there after deletion " + i, nigori.get(index));
            }
        } finally {
            assertTrue("Not unregistered", nigori.unregister());
        }
    }
}
Also used : Revision(com.google.nigori.common.Revision) RevValue(com.google.nigori.common.RevValue) NigoriDatastore(com.google.nigori.client.NigoriDatastore) Index(com.google.nigori.common.Index) Test(org.junit.Test)

Aggregations

Revision (com.google.nigori.common.Revision)8 NigoriDatastore (com.google.nigori.client.NigoriDatastore)5 Index (com.google.nigori.common.Index)5 Test (org.junit.Test)4 RevValue (com.google.nigori.common.RevValue)3 ArrayList (java.util.ArrayList)2 IndexValue (com.google.nigori.client.accept.n.SetGetDeleteTest.IndexValue)1 GetRevisionsResponse (com.google.nigori.common.NigoriMessages.GetRevisionsResponse)1 NotFoundException (com.google.nigori.common.NotFoundException)1 ByteString (com.google.protobuf.ByteString)1 IOException (java.io.IOException)1 Random (java.util.Random)1