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());
// put new version 2 (basing on version 1) with protection keys 1
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(keyPair1).start();
futurePut2.awaitUninterruptibly();
assertTrue(futurePut2.isSuccess());
// change protection key on version 1 with a put meta
Data dataMeta = new Data().setProtectedEntry().publicKey(keyPair2.getPublic()).duplicateMeta();
// use the old protection key 1 to sign the message
FuturePut futurePutMeta = p1.put(lKey).setDomainKey(dKey).setVersionKey(vKey1).putMeta()
.setData(cKey, dataMeta).keyPair(keyPair1).start();
futurePutMeta.awaitUninterruptibly();
assertTrue(futurePutMeta.isSuccess());
// verify change at version 1
Data retData = p2.get(lKey).setDomainKey(dKey).setContentKey(cKey).setVersionKey(vKey1).start()
.awaitUninterruptibly().getData();
Assert.assertEquals("data1", (String) retData.object());
// verify change of content protection keys to 2
Assert.assertEquals(keyPair2.getPublic(), retData.publicKey());
// verify change at version 2
retData = p2.get(lKey).setDomainKey(dKey).setContentKey(cKey).setVersionKey(vKey2).start()
.awaitUninterruptibly().getData();
Assert.assertEquals("data2", (String) retData.object());
// verify change of content protection keys to 2
Assert.assertEquals(keyPair2.getPublic(), retData.publicKey()); // <==== is keypair1 but should be keypair2
// overwrite version 1 with protection keys 2
Data data1Overwrite = new Data("data1Overwrite").setProtectedEntry();
FuturePut futurePut1Overwrite = p1.put(lKey).setData(cKey, data1Overwrite).setDomainKey(dKey)
.setVersionKey(vKey1).keyPair(keyPair2).start();
futurePut1Overwrite.awaitUninterruptibly();
assertTrue(futurePut1Overwrite.isSuccess());
// verify overwrite version 1
retData = p2.get(lKey).setDomainKey(dKey).setContentKey(cKey).setVersionKey(vKey1).start()
.awaitUninterruptibly().getData();
Assert.assertEquals("data1Overwrite", (String) retData.object());
Assert.assertEquals(keyPair2.getPublic(), retData.publicKey());
// overwrite version 2 with protection keys 2
Data data2Overwrite = new Data("data2Overwrite").setProtectedEntry();
FuturePut futurePut2Overwrite = p1.put(lKey).setData(cKey, data2Overwrite).setDomainKey(dKey)
.setVersionKey(vKey2).keyPair(keyPair2).start();
futurePut2Overwrite.awaitUninterruptibly();
assertTrue(futurePut2Overwrite.isSuccess());
// verify overwrite version 2
retData = p2.get(lKey).setDomainKey(dKey).setContentKey(cKey).setVersionKey(vKey2).start()
.awaitUninterruptibly().getData();
Assert.assertEquals("data2Overwrite", (String) retData.object());
Assert.assertEquals(keyPair2.getPublic(), retData.publicKey());
// put new version 3 (basing on version 2) with protection keys 2
Data data3 = new Data("data3").basedOn(vKey2).setProtectedEntry();
Number160 vKey3 = Number160.createHash("version3");
FuturePut futurePut3 = p1.put(lKey).setData(cKey, data3).setDomainKey(dKey).setVersionKey(vKey3)
.keyPair(keyPair2).start();
futurePut3.awaitUninterruptibly();
assertTrue(futurePut3.isSuccess());
// verify put version 3
retData = p2.get(lKey).setDomainKey(dKey).setContentKey(cKey).setVersionKey(vKey3).start()
.awaitUninterruptibly().getData();
Assert.assertEquals("data3", (String) retData.object());
Assert.assertEquals(keyPair2.getPublic(), retData.publicKey());
// try to put a version X in version key range of version 1, 2 and 3 with wrong protection keys 1
// (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(keyPair1).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, 2 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());