Skip to content

Commit

Permalink
Introduced exception handling for wrong payload sizes (in StreamDeser…
Browse files Browse the repository at this point in the history
…ializationContext) and wrong URIs (in PendingAttestation). Fixed broken test in TestUnknownAttestation, detailed tests for wrong URIs in TestPendingAttestation. Added new exceptions to all methods in source or tests where exceptions can occur.
  • Loading branch information
gregoreficint committed Jan 18, 2021
1 parent fe98435 commit 9b93afa
Show file tree
Hide file tree
Showing 21 changed files with 117 additions and 60 deletions.
9 changes: 5 additions & 4 deletions src/main/java/com/eternitywall/ots/Calendar.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.eternitywall.http.Request;
import com.eternitywall.http.Response;
import com.eternitywall.ots.exceptions.CommitmentNotFoundException;
import com.eternitywall.ots.exceptions.DeserializationException;
import com.eternitywall.ots.exceptions.ExceededSizeException;
import com.eternitywall.ots.exceptions.UrlException;
import org.bitcoinj.core.ECKey;
Expand Down Expand Up @@ -63,7 +64,7 @@ public String getUrl() {
*
* @param digest The digest hash to send.
* @return the Timestamp received from the calendar.
* @throws ExceededSizeException if response is too big.
* @throws DeserializationException if response is too big.
* @throws UrlException if url is not reachable.
*/
public Timestamp submit(byte[] digest) throws ExceededSizeException, UrlException {
Expand Down Expand Up @@ -100,11 +101,11 @@ public Timestamp submit(byte[] digest) throws ExceededSizeException, UrlExceptio
*
* @param commitment The digest hash to send.
* @return the Timestamp from the calendar server (with blockchain information if already written).
* @throws ExceededSizeException if response is too big.
* @throws DeserializationException if response is too big.
* @throws UrlException if url is not reachable.
* @throws CommitmentNotFoundException if commit is not found.
*/
public Timestamp getTimestamp(byte[] commitment) throws ExceededSizeException, CommitmentNotFoundException, UrlException {
public Timestamp getTimestamp(byte[] commitment) throws DeserializationException, CommitmentNotFoundException, UrlException {
try {
Map<String, String> headers = new HashMap<>();
headers.put("Accept", "application/vnd.opentimestamps.v1");
Expand All @@ -117,7 +118,7 @@ public Timestamp getTimestamp(byte[] commitment) throws ExceededSizeException, C
Response response = task.call();
byte[] body = response.getBytes();
if (body.length > 10000) {
throw new ExceededSizeException("Calendar response exceeded size limit");
throw new DeserializationException("Calendar response exceeded size limit");
}

if (!response.isOk()) {
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/com/eternitywall/ots/DetachedTimestampFile.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.eternitywall.ots;

import com.eternitywall.ots.exceptions.DeserializationException;
import com.eternitywall.ots.op.Op;
import com.eternitywall.ots.op.OpCrypto;
import com.eternitywall.ots.op.OpSHA256;
Expand Down Expand Up @@ -96,7 +97,7 @@ public byte[] serialize() {
* @param ctx The stream deserialization context.
* @return The generated com.eternitywall.ots.DetachedTimestampFile object.
*/
public static DetachedTimestampFile deserialize(StreamDeserializationContext ctx) {
public static DetachedTimestampFile deserialize(StreamDeserializationContext ctx) throws DeserializationException {
ctx.assertMagic(HEADER_MAGIC);
ctx.readVaruint();

Expand All @@ -115,7 +116,7 @@ public static DetachedTimestampFile deserialize(StreamDeserializationContext ctx
* @param ots The byte array of deserialization DetachedFileTimestamped.
* @return The generated com.eternitywall.ots.DetachedTimestampFile object.
*/
public static DetachedTimestampFile deserialize(byte[] ots) {
public static DetachedTimestampFile deserialize(byte[] ots) throws DeserializationException {
StreamDeserializationContext ctx = new StreamDeserializationContext(ots);

return DetachedTimestampFile.deserialize(ctx);
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/com/eternitywall/ots/OtsCli.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.eternitywall.ots.attestation.BitcoinBlockHeaderAttestation;
import com.eternitywall.ots.attestation.EthereumBlockHeaderAttestation;
import com.eternitywall.ots.attestation.LitecoinBlockHeaderAttestation;
import com.eternitywall.ots.exceptions.DeserializationException;
import com.eternitywall.ots.op.OpSHA256;
import org.apache.commons.cli.BasicParser;
import org.apache.commons.cli.CommandLine;
Expand Down Expand Up @@ -216,6 +217,8 @@ public static void info(String argsOts, boolean verbose) {
System.out.println(infoResult);
} catch (IOException e) {
log.severe("No valid file");
} catch (DeserializationException e) {
log.severe("Deserialization error: size was either too long or short");
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.eternitywall.ots;

import com.eternitywall.ots.exceptions.DeserializationException;
import java.util.Arrays;
import java.util.logging.Logger;

Expand Down Expand Up @@ -65,27 +66,29 @@ public int readVaruint() {
return value;
}

public byte[] readBytes(int expectedLength) {
public byte[] readBytes(int expectedLength) throws DeserializationException {


if (expectedLength == 0) {
return this.readVarbytes(1024, 0);
}

return this.read(expectedLength);
}

public byte[] readVarbytes(int maxLen) {
public byte[] readVarbytes(int maxLen) throws DeserializationException {
return readVarbytes(maxLen, 0);
}

public byte[] readVarbytes(int maxLen, int minLen) {
public byte[] readVarbytes(int maxLen, int minLen) throws DeserializationException {
int l = this.readVaruint();

if ((l & 0xff) > maxLen) {
if (l > maxLen) {
log.severe("varbytes max length exceeded;");
return null;
} else if ((l & 0xff) < minLen) {
throw new DeserializationException("varbytes max length exceeded;");
} else if (l < minLen) {
log.severe("varbytes min length not met;");
return null;
throw new DeserializationException("varbytes min length not met;");
}

return this.read(l);
Expand Down
9 changes: 6 additions & 3 deletions src/main/java/com/eternitywall/ots/Timestamp.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.eternitywall.ots.attestation.BitcoinBlockHeaderAttestation;
import com.eternitywall.ots.attestation.TimeAttestation;
import com.eternitywall.ots.exceptions.DeserializationException;
import com.eternitywall.ots.op.Op;
import com.eternitywall.ots.op.OpBinary;
import com.eternitywall.ots.op.OpSHA256;
Expand Down Expand Up @@ -44,7 +45,7 @@ public Timestamp(byte[] msg) {
* @param initialMsg - The initial message.
* @return The deserialized Timestamp.
*/
public static Timestamp deserialize(byte[] ots, byte[] initialMsg) {
public static Timestamp deserialize(byte[] ots, byte[] initialMsg) throws DeserializationException {
StreamDeserializationContext ctx = new StreamDeserializationContext(ots);

return Timestamp.deserialize(ctx, initialMsg);
Expand All @@ -63,7 +64,8 @@ public static Timestamp deserialize(byte[] ots, byte[] initialMsg) {
* @param initialMsg - The initial message.
* @return The deserialized Timestamp.
*/
public static Timestamp deserialize(StreamDeserializationContext ctx, byte[] initialMsg) {
public static Timestamp deserialize(StreamDeserializationContext ctx, byte[] initialMsg)
throws DeserializationException {
Timestamp self = new Timestamp(initialMsg);
byte tag = ctx.readBytes(1)[0];

Expand All @@ -78,7 +80,8 @@ public static Timestamp deserialize(StreamDeserializationContext ctx, byte[] ini
return self;
}

private static void doTagOrAttestation(Timestamp self, StreamDeserializationContext ctx, byte tag, byte[] initialMsg) {
private static void doTagOrAttestation(Timestamp self, StreamDeserializationContext ctx, byte tag, byte[] initialMsg)
throws DeserializationException {
if ((tag & 0xff) == 0x00) {
TimeAttestation attestation = TimeAttestation.deserialize(ctx);
self.attestations.add(attestation);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.eternitywall.ots.StreamSerializationContext;
import com.eternitywall.ots.Utils;

import com.eternitywall.ots.exceptions.DeserializationException;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.logging.Logger;
Expand Down Expand Up @@ -74,13 +75,20 @@ public static boolean checkUri(byte[] uri) {
return true;
}

public static PendingAttestation deserialize(StreamDeserializationContext ctxPayload) {
byte[] utf8Uri = ctxPayload.readVarbytes(PendingAttestation._MAX_URI_LENGTH);
public static PendingAttestation deserialize(StreamDeserializationContext ctxPayload)
throws DeserializationException {

byte[] utf8Uri;
try {
utf8Uri = ctxPayload.readVarbytes(PendingAttestation._MAX_URI_LENGTH);
} catch (DeserializationException e) {
log.severe("URI too long and thus invalid: ");
throw new DeserializationException("Invalid URI: ");
}

if (PendingAttestation.checkUri(utf8Uri) == false) {
log.severe("Invalid URI: ");

return null;
throw new DeserializationException("Invalid URI: ");
}

return new PendingAttestation(utf8Uri);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.eternitywall.ots.StreamSerializationContext;
import com.eternitywall.ots.Utils;

import com.eternitywall.ots.exceptions.DeserializationException;
import java.util.Arrays;
import java.util.logging.Logger;

Expand All @@ -30,7 +31,7 @@ public byte[] _TAG() {
* @param ctx The stream deserialization context.
* @return The specific subclass Attestation.
*/
public static TimeAttestation deserialize(StreamDeserializationContext ctx) {
public static TimeAttestation deserialize(StreamDeserializationContext ctx) throws DeserializationException {
// console.log('attestation deserialize');

byte[] tag = ctx.readBytes(_TAG_SIZE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.eternitywall.ots.StreamSerializationContext;
import com.eternitywall.ots.Utils;

import com.eternitywall.ots.exceptions.DeserializationException;
import java.util.Arrays;
import java.util.logging.Logger;

Expand Down Expand Up @@ -36,7 +37,7 @@ public void serializePayload(StreamSerializationContext ctx) {
ctx.writeBytes(this.payload);
}

public static UnknownAttestation deserialize(StreamDeserializationContext ctxPayload, byte[] tag) {
public static UnknownAttestation deserialize(StreamDeserializationContext ctxPayload, byte[] tag) throws DeserializationException {
byte[] payload = ctxPayload.readVarbytes(_MAX_PAYLOAD_SIZE);

return new UnknownAttestation(tag, payload);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.eternitywall.ots.exceptions;

public class DeserializationException extends Exception {
public DeserializationException(String message) {
super(message);
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.eternitywall.ots.exceptions;

public class ExceededSizeException extends Exception {
public ExceededSizeException(String message) {
super(message);
}
public ExceededSizeException(String message) {
super(message);
}
}
6 changes: 4 additions & 2 deletions src/main/java/com/eternitywall/ots/op/Op.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.eternitywall.ots.StreamSerializationContext;
import com.eternitywall.ots.Utils;

import com.eternitywall.ots.exceptions.DeserializationException;
import java.util.logging.Logger;

/**
Expand Down Expand Up @@ -61,7 +62,7 @@ public byte _TAG() {
* @param ctx The stream deserialization context.
* @return The subclass Operation.
*/
public static Op deserialize(StreamDeserializationContext ctx) {
public static Op deserialize(StreamDeserializationContext ctx) throws DeserializationException {
byte tag = ctx.readBytes(1)[0];

return Op.deserializeFromTag(ctx, tag);
Expand All @@ -74,7 +75,8 @@ public static Op deserialize(StreamDeserializationContext ctx) {
* @param tag The tag of the operation.
* @return The subclass Operation.
*/
public static Op deserializeFromTag(StreamDeserializationContext ctx, byte tag) {
public static Op deserializeFromTag(StreamDeserializationContext ctx, byte tag)
throws DeserializationException {
if (tag == OpAppend._TAG) {
return OpAppend.deserializeFromTag(ctx, tag);
} else if (tag == OpPrepend._TAG) {
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/com/eternitywall/ots/op/OpAppend.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.eternitywall.ots.StreamDeserializationContext;
import com.eternitywall.ots.Utils;

import com.eternitywall.ots.exceptions.DeserializationException;
import java.util.Arrays;
import java.util.logging.Logger;

Expand Down Expand Up @@ -44,7 +45,8 @@ public byte[] call(byte[] msg) {
return Utils.arraysConcat(msg, this.arg);
}

public static Op deserializeFromTag(StreamDeserializationContext ctx, byte tag) {
public static Op deserializeFromTag(StreamDeserializationContext ctx, byte tag)
throws DeserializationException {
return OpBinary.deserializeFromTag(ctx, tag);
}

Expand Down
4 changes: 3 additions & 1 deletion src/main/java/com/eternitywall/ots/op/OpBinary.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.eternitywall.ots.StreamSerializationContext;
import com.eternitywall.ots.Utils;

import com.eternitywall.ots.exceptions.DeserializationException;
import java.util.Arrays;
import java.util.logging.Logger;

Expand Down Expand Up @@ -33,7 +34,8 @@ public OpBinary(byte[] arg_) {
this.arg = arg_;
}

public static Op deserializeFromTag(StreamDeserializationContext ctx, byte tag) {
public static Op deserializeFromTag(StreamDeserializationContext ctx, byte tag)
throws DeserializationException {
byte[] arg = ctx.readVarbytes(_MAX_RESULT_LENGTH, 1);

if (tag == OpAppend._TAG) {
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/com/eternitywall/ots/op/OpPrepend.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.eternitywall.ots.StreamDeserializationContext;
import com.eternitywall.ots.Utils;

import com.eternitywall.ots.exceptions.DeserializationException;
import java.util.Arrays;
import java.util.logging.Logger;

Expand Down Expand Up @@ -44,7 +45,8 @@ public byte[] call(byte[] msg) {
return Utils.arraysConcat(this.arg, msg);
}

public static Op deserializeFromTag(StreamDeserializationContext ctx, byte tag) {
public static Op deserializeFromTag(StreamDeserializationContext ctx, byte tag)
throws DeserializationException {
return OpBinary.deserializeFromTag(ctx, tag);
}

Expand Down
3 changes: 2 additions & 1 deletion src/test/java/com/eternitywall/TestLongReceipt.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.eternitywall.ots.StreamDeserializationContext;
import com.eternitywall.ots.StreamSerializationContext;
import com.eternitywall.ots.Timestamp;
import com.eternitywall.ots.exceptions.DeserializationException;
import org.junit.Test;

import javax.xml.bind.DatatypeConverter;
Expand All @@ -13,7 +14,7 @@

public class TestLongReceipt {
@Test
public void testException() {
public void testException() throws DeserializationException {
String digest = "c858838f62f908c922f9cd734e49c8fa6ee9a3b8a77093ac0969cba429249412";
byte[] digestByte = DatatypeConverter.parseHexBinary(digest);
Timestamp root = new Timestamp(digestByte);
Expand Down
7 changes: 4 additions & 3 deletions src/test/java/com/eternitywall/TestOpenTimestamps.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import com.eternitywall.ots.Utils;
import com.eternitywall.ots.VerifyResult;
import com.eternitywall.ots.attestation.TimeAttestation;
import com.eternitywall.ots.exceptions.DeserializationException;
import com.eternitywall.ots.exceptions.VerificationException;
import com.eternitywall.ots.op.OpSHA256;
import org.junit.After;
Expand Down Expand Up @@ -79,7 +80,7 @@ public void loadData() throws ExecutionException, InterruptedException, IOExcept
}

@Test
public void info() {
public void info() throws DeserializationException {
String result1 = OpenTimestamps.info(DetachedTimestampFile.deserialize(incompleteOts));
assertNotNull(result1);
assertNotNull(incompleteOtsInfo);
Expand Down Expand Up @@ -157,7 +158,7 @@ public void verify2() throws Exception {
}

@Test
public void verify() throws NoSuchAlgorithmException, IOException {
public void verify() throws NoSuchAlgorithmException, IOException, DeserializationException {
{
DetachedTimestampFile detachedOts = DetachedTimestampFile.deserialize(helloWorldOts);
DetachedTimestampFile detached = DetachedTimestampFile.from(Hash.from(helloWorld, OpSHA256._TAG));
Expand Down Expand Up @@ -226,7 +227,7 @@ public void upgrade() {
}

@Test
public void test() {
public void test() throws DeserializationException {
byte[] ots = Utils.hexToBytes("F0105C3F2B3F8524A32854E07AD8ADDE9C1908F10458D95A36F008088D287213A8B9880083DFE30D2EF90C8E2C2B68747470733A2F2F626F622E6274632E63616C656E6461722E6F70656E74696D657374616D70732E6F7267");
byte[] digest = Utils.hexToBytes("7aa9273d2a50dbe0cc5a6ccc444a5ca90c9491dd2ac91849e45195ae46f64fe352c3a63ba02775642c96131df39b5b85");

Expand Down
Loading

0 comments on commit 9b93afa

Please sign in to comment.