public void testStepSuccessAndRollbackWithMetaFile() throws InterruptedException,
NoPeerConnectionException, DataLengthException, InvalidKeyException, IllegalStateException,
InvalidCipherTextException, IllegalBlockSizeException, BadPaddingException, IOException,
SignatureException, InvalidProcessStateException {
// where the process runs
NetworkManager getter = network.get(0);
// generate necessary keys
KeyPair chunkEncryptionKeys = EncryptionUtil.generateRSAKeyPair(H2HConstants.KEYLENGTH_CHUNK);
KeyPair metaFileEncryptionKeys = EncryptionUtil.generateRSAKeyPair(H2HConstants.KEYLENGTH_META_FILE);
KeyPair protectionKeysOld = EncryptionUtil.generateRSAKeyPair();
KeyPair protectionKeysNew = EncryptionUtil.generateRSAKeyPair();
// generate a fake meta file
List<MetaChunk> metaChunks1 = new ArrayList<MetaChunk>();
metaChunks1.add(new MetaChunk(NetworkTestUtil.randomString(), NetworkTestUtil.randomString()
.getBytes(), 0));
metaChunks1.add(new MetaChunk(NetworkTestUtil.randomString(), NetworkTestUtil.randomString()
.getBytes(), 1));
List<MetaChunk> metaChunks2 = new ArrayList<MetaChunk>();
metaChunks2.add(new MetaChunk(NetworkTestUtil.randomString(), NetworkTestUtil.randomString()
.getBytes(), 2));
List<FileVersion> fileVersions = new ArrayList<FileVersion>();
fileVersions.add(new FileVersion(0, 123, System.currentTimeMillis(), metaChunks1));
fileVersions.add(new FileVersion(1, 123, System.currentTimeMillis(), metaChunks2));
MetaFileSmall metaFileSmall = new MetaFileSmall(metaFileEncryptionKeys.getPublic(), fileVersions,
chunkEncryptionKeys);
// encrypt the meta file
HybridEncryptedContent encryptedMetaFile = H2HEncryptionUtil.encryptHybrid(metaFileSmall,
metaFileEncryptionKeys.getPublic());
encryptedMetaFile.generateVersionKey();
// initialize put
Parameters parameters = new Parameters()
.setLocationKey(H2HEncryptionUtil.key2String(metaFileSmall.getId()))
.setContentKey(H2HConstants.META_FILE).setVersionKey(encryptedMetaFile.getVersionKey())
.setProtectionKeys(protectionKeysOld).setData(encryptedMetaFile);
// indicate to generate hash
parameters.setHashFlag(true);
// put encrypted meta file into network
getter.getDataManager().putUnblocked(parameters).awaitUninterruptibly();
// verify put
Assert.assertNotNull(getter.getDataManager().getUnblocked(parameters).awaitUninterruptibly()
.getData());
// initialize a fake process context
BasePKUpdateContext context = new TestMetaFilePKUpdateContext(protectionKeysOld, protectionKeysNew,
metaFileSmall, parameters.getHash(), encryptedMetaFile.getVersionKey());
// create a change protection keys process step
ChangeProtectionKeysStep step = new ChangeProtectionKeysStep(context, getter.getDataManager());
// run process, should not fail
UseCaseTestUtil.executeProcessTillSucceded(step);
// verify if content protection keys have changed
Assert.assertEquals(protectionKeysNew.getPublic(), getter.getDataManager().getUnblocked(parameters)
.awaitUninterruptibly().getData().publicKey());
// manually trigger roll back
step.cancel(new RollbackReason("Testing rollback."));
// verify if content protection keys have changed to old ones
Assert.assertEquals(protectionKeysOld.getPublic(), getter.getDataManager().getUnblocked(parameters)
.awaitUninterruptibly().getData().publicKey());
}