ClassNotFoundException, InvalidKeyException, SignatureException, NoSuchPaddingException,
IllegalBlockSizeException, BadPaddingException {
KeyPairGenerator gen = KeyPairGenerator.getInstance("DSA");
KeyPair keyPairPeer1 = gen.generateKeyPair();
Peer p1 = new PeerMaker(Number160.createHash(1)).ports(4834).keyPair(keyPairPeer1)
.setEnableIndirectReplication(true).makeAndListen();
KeyPair keyPairPeer2 = gen.generateKeyPair();
Peer p2 = new PeerMaker(Number160.createHash(2)).masterPeer(p1).keyPair(keyPairPeer2)
.setEnableIndirectReplication(true).makeAndListen();
p2.bootstrap().setPeerAddress(p1.getPeerAddress()).start().awaitUninterruptibly();
p1.bootstrap().setPeerAddress(p2.getPeerAddress()).start().awaitUninterruptibly();
KeyPair keyPair1 = gen.generateKeyPair();
KeyPair keyPair2 = gen.generateKeyPair();
Number160 lKey = Number160.createHash("location");
Number160 dKey = Number160.createHash("domain");
Number160 cKey = Number160.createHash("content");
// put version 1 with protection keys 1
Data data1 = new Data("data1").setProtectedEntry();
Number160 vKey1 = Number160.ZERO;
FuturePut futurePut1 = p1.put(lKey).setData(cKey, data1).setDomainKey(dKey).setVersionKey(vKey1)
.keyPair(keyPair1).start();
futurePut1.awaitUninterruptibly();
assertTrue(futurePut1.isSuccess());
// verify put of version 1
Data retData = p2.get(lKey).setDomainKey(dKey).setContentKey(cKey).setVersionKey(vKey1).start()
.awaitUninterruptibly().getData();
Assert.assertEquals("data1", (String) retData.object());
// verify content protection keys 1 is set
Assert.assertEquals(keyPair1.getPublic(), retData.publicKey());
// try to overwrite version 1 without protection keys (expected to fail)
Data data1A = new Data("data1A");
FuturePut futurePut1A = p1.put(lKey).setData(cKey, data1A).setDomainKey(dKey).setVersionKey(vKey1)
.start();
futurePut1A.awaitUninterruptibly();
assertFalse(futurePut1A.isSuccess());
// verify that nothing changed
retData = p2.get(lKey).setDomainKey(dKey).setContentKey(cKey).setVersionKey(vKey1).start()
.awaitUninterruptibly().getData();
Assert.assertEquals("data1", (String) retData.object());
Assert.assertEquals(keyPair1.getPublic(), retData.publicKey());
// try to overwrite version 1 with wrong protection keys 2 (expected to fail)
Data data1B = new Data("data1B").setProtectedEntry();
FuturePut futurePut1B = p1.put(lKey).setData(cKey, data1B).setDomainKey(dKey).setVersionKey(vKey1)
.keyPair(keyPair2).start();
futurePut1B.awaitUninterruptibly();
assertFalse(futurePut1B.isSuccess());
// verify that nothing changed
retData = p2.get(lKey).setDomainKey(dKey).setContentKey(cKey).setVersionKey(vKey1).start()
.awaitUninterruptibly().getData();
Assert.assertEquals("data1", (String) retData.object());
Assert.assertEquals(keyPair1.getPublic(), retData.publicKey());
// overwrite version 1 with protection keys 1
Data data1Overwrite = new Data("data1Overwrite").setProtectedEntry();
FuturePut futurePutOverwrite = p1.put(lKey).setData(cKey, data1Overwrite).setDomainKey(dKey)
.setVersionKey(vKey1).keyPair(keyPair1).start();
futurePutOverwrite.awaitUninterruptibly();
assertTrue(futurePutOverwrite.isSuccess());
// verify overwrite
retData = p2.get(lKey).setDomainKey(dKey).setContentKey(cKey).setVersionKey(vKey1).start()
.awaitUninterruptibly().getData();
Assert.assertEquals("data1Overwrite", (String) retData.object());
Assert.assertEquals(keyPair1.getPublic(), retData.publicKey());
// try to put new version 2 (basing on version 1) with wrong protection keys 2 (expected to fail)
Data data2 = new Data("data2").basedOn(vKey1).setProtectedEntry();
// version 2 takes new version key
Number160 vKey2 = Number160.createHash("version2");
FuturePut futurePut2 = p1.put(lKey).setData(cKey, data2).setDomainKey(dKey).setVersionKey(vKey2)
.keyPair(keyPair2).start();
futurePut2.awaitUninterruptibly();
assertFalse(futurePut2.isSuccess());
// verify no put of version 2
assertNull(p2.get(lKey).setContentKey(cKey).setVersionKey(vKey2).setDomainKey(dKey).start()
.awaitUninterruptibly().getData());
// put new version 3 (basing on version 1) with correct protection keys 1
Data data3 = new Data("data3").basedOn(vKey1).setProtectedEntry();
Number160 vKey3 = Number160.createHash("version3");
FuturePut futurePut3 = p1.put(lKey).setData(cKey, data3).setDomainKey(dKey).setVersionKey(vKey3)
.keyPair(keyPair1).start();
futurePut3.awaitUninterruptibly();
assertTrue(futurePut3.isSuccess());
// verify put of version 3
assertEquals("data3",
(String) p2.get(lKey).setContentKey(cKey).setVersionKey(vKey3).setDomainKey(dKey).start()
.awaitUninterruptibly().getData().object());
retData = p2.get(lKey).setDomainKey(dKey).setContentKey(cKey).setVersionKey(vKey3).start()
.awaitUninterruptibly().getData();
Assert.assertEquals("data3", (String) retData.object());
Assert.assertEquals(keyPair1.getPublic(), retData.publicKey());
// try to put a version X in version key range of version 1 and 3 with wrong protection keys 2
// (expected to fail)
Data dataX = new Data("dataX").setProtectedEntry();
Number160 vKeyX = Number160.createHash("versionX");
FuturePut futurePut4 = p1.put(lKey).setData(cKey, dataX).setDomainKey(dKey).setVersionKey(vKeyX)
.keyPair(keyPair2).start();
futurePut4.awaitUninterruptibly();
assertFalse(futurePut4.isSuccess());
// verify no put of version X
assertNull(p2.get(lKey).setContentKey(cKey).setVersionKey(vKeyX).setDomainKey(dKey).start()
.awaitUninterruptibly().getData());
// try to put random version Y in version key range of version 1 and 3 without protection keys
// (expected to fail)
Data dataY = new Data("dataY").setProtectedEntry();
Number160 vKeyY = Number160.createHash("versionX");
futurePut4 = p1.put(lKey).setData(cKey, dataY).setDomainKey(dKey).setVersionKey(vKeyY).start();
futurePut4.awaitUninterruptibly();
assertFalse(futurePut4.isSuccess());
// verify no put of version Y
assertNull(p2.get(lKey).setContentKey(cKey).setVersionKey(vKeyY).setDomainKey(dKey).start()
.awaitUninterruptibly().getData());
p1.shutdown().awaitUninterruptibly();
p2.shutdown().awaitUninterruptibly();
}