Use creation data for TTL check

This commit is contained in:
Manfred Karrer 2016-05-24 18:55:55 +02:00
parent 4a3ec759f9
commit 551eb5b648
4 changed files with 20 additions and 10 deletions

View file

@ -40,6 +40,7 @@ import java.util.concurrent.TimeUnit;
// Run in UserThread
public class P2PDataStorage implements MessageListener, ConnectionListener {
private static final Logger log = LoggerFactory.getLogger(P2PDataStorage.class);
/**
* How many days to keep an entry before it is purged.
*/
@ -55,6 +56,7 @@ public class P2PDataStorage implements MessageListener, ConnectionListener {
private HashMap<ByteArray, MapValue> sequenceNumberMap = new HashMap<>();
private final Storage<HashMap> storage;
///////////////////////////////////////////////////////////////////////////////////////////
// Constructor
///////////////////////////////////////////////////////////////////////////////////////////
@ -237,7 +239,7 @@ public class P2PDataStorage implements MessageListener, ConnectionListener {
if (result) {
log.info("refreshDate called for storedData:\n\t" + StringUtils.abbreviate(storedData.toString(), 100));
storedData.updateTimeStamp();
storedData.refreshTTL();
storedData.updateSequenceNumber(sequenceNumber);
storedData.updateSignature(signature);

View file

@ -36,7 +36,7 @@ public class ProtectedMailboxStorageEntry extends ProtectedStorageEntry {
try {
in.defaultReadObject();
receiversPubKey = KeyFactory.getInstance(Sig.KEY_ALGO, "BC").generatePublic(new X509EncodedKeySpec(receiversPubKeyBytes));
updateTimeStamp();
checkCreationTimeStamp();
} catch (Throwable t) {
log.warn("Exception at readObject: " + t.getMessage());
}

View file

@ -26,14 +26,14 @@ public class ProtectedStorageEntry implements Payload {
public int sequenceNumber;
public byte[] signature;
@VisibleForTesting
transient public long timeStamp;
public long creationTimeStamp;
public ProtectedStorageEntry(StoragePayload storagePayload, PublicKey ownerPubKey, int sequenceNumber, byte[] signature) {
this.storagePayload = storagePayload;
this.ownerPubKey = ownerPubKey;
this.sequenceNumber = sequenceNumber;
this.signature = signature;
this.timeStamp = System.currentTimeMillis();
this.creationTimeStamp = System.currentTimeMillis();
this.ownerPubKeyBytes = new X509EncodedKeySpec(this.ownerPubKey.getEncoded()).getEncoded();
}
@ -41,7 +41,7 @@ public class ProtectedStorageEntry implements Payload {
try {
in.defaultReadObject();
ownerPubKey = KeyFactory.getInstance(Sig.KEY_ALGO, "BC").generatePublic(new X509EncodedKeySpec(ownerPubKeyBytes));
updateTimeStamp();
checkCreationTimeStamp();
} catch (Throwable t) {
log.warn("Exception at readObject: " + t.getMessage());
}
@ -51,8 +51,16 @@ public class ProtectedStorageEntry implements Payload {
return storagePayload;
}
public void updateTimeStamp() {
timeStamp = System.currentTimeMillis();
public void checkCreationTimeStamp() {
// We don't allow creation date in the future, but we cannot be too strict as clocks are not synced
// The 0 test is needed to be backward compatible as creationTimeStamp (timeStamp) was transient before 0.4.7
// TODO "|| creationTimeStamp == 0" can removed after we don't support 0.4.6 anymore
if (creationTimeStamp > System.currentTimeMillis() || creationTimeStamp == 0)
creationTimeStamp = System.currentTimeMillis();
}
public void refreshTTL() {
creationTimeStamp = System.currentTimeMillis();
}
public void updateSequenceNumber(int sequenceNumber) {
@ -64,14 +72,14 @@ public class ProtectedStorageEntry implements Payload {
}
public boolean isExpired() {
return (System.currentTimeMillis() - timeStamp) > storagePayload.getTTL();
return (System.currentTimeMillis() - creationTimeStamp) > storagePayload.getTTL();
}
@Override
public String toString() {
return "ProtectedStorageEntry{" +
"expirablePayload=" + storagePayload +
", timeStamp=" + timeStamp +
", creationTimeStamp=" + creationTimeStamp +
", sequenceNumber=" + sequenceNumber +
", ownerPubKey.hashCode()=" + (ownerPubKey != null ? ownerPubKey.hashCode() : "null") +
", signature.hashCode()=" + (signature != null ? Arrays.toString(signature).hashCode() : "null") +

View file

@ -113,7 +113,7 @@ public class ProtectedDataStorageTest {
public void testTTL() throws InterruptedException, NoSuchAlgorithmException, CertificateException, KeyStoreException, IOException, CryptoException, SignatureException, InvalidKeyException, NoSuchProviderException {
mockData.ttl = (int) (P2PDataStorage.CHECK_TTL_INTERVAL_SEC * 1.5);
ProtectedStorageEntry data = dataStorage1.getProtectedData(mockData, storageSignatureKeyPair1);
log.debug("data.date " + data.timeStamp);
log.debug("data.date " + data.creationTimeStamp);
Assert.assertTrue(dataStorage1.add(data, null, null, true));
log.debug("test 1");
Assert.assertEquals(1, dataStorage1.getMap().size());