Skip to content

Commit

Permalink
More adversarial data tests for Index
Browse files Browse the repository at this point in the history
  • Loading branch information
mikera committed May 5, 2024
1 parent 0306e4e commit 53c1da1
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 3 deletions.
12 changes: 11 additions & 1 deletion convex-core/src/main/java/convex/core/data/Address.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public final class Address extends ABlobLike<CVMLong> {
public static final Address MAX_VALUE = Address.create(Long.MAX_VALUE);

/**
* Length of an Address in bytes (considered as a Blob)
* Length of an Address in bytes (when considered as a Blob)
*/
static final int BYTE_LENGTH = 8;

Expand All @@ -67,6 +67,16 @@ public static Address create(long number) {
}
return new Address(number);
}

/**
* Creates an Address without checking.
*
* @param number Account number
* @return Address instance, may be invalid
*/
public static Address unsafeCreate(long number) {
return new Address(number);
}

/**
* Obtains an Address from a blob. Must be a valid long value
Expand Down
4 changes: 4 additions & 0 deletions convex-core/src/main/java/convex/core/data/Index.java
Original file line number Diff line number Diff line change
Expand Up @@ -593,6 +593,10 @@ public static <K extends ABlobLike, V extends ACell> Index<K, V> read(Blob b, in
Index<K,V> result;
long depth = Format.readVLCCount(b,epos);
if (depth < 0) throw new BadFormatException("Negative depth!");
if (depth >=MAX_DEPTH) {
if (depth==MAX_DEPTH) throw new BadFormatException("More than one entry and MAX_DEPTH");
throw new BadFormatException("Excessive depth!");
}
epos+=Format.getVLCCountLength(depth);

// Need to include children
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,11 @@ public class AdversarialDataTest {
invalidTest(MapLeaf.unsafeCreate(new MapEntry[0]));
}

@Test public void testBadAddress() {
invalidTest(Address.unsafeCreate(-1));
invalidTest(Address.unsafeCreate(Long.MIN_VALUE));
}

@SuppressWarnings({ "unchecked", "null" })
@Test public void testBadSetTree() {
SetTree<CVMLong> a = Samples.INT_SET_300;
Expand Down Expand Up @@ -204,6 +209,19 @@ public void testBadIndex() {
Index c2=Index.create(Blobs.fromHex("1231"),CVMLong.ZERO);
invalidTest(Index.unsafeCreate(3, null, new Ref[] {c1.getRef(),c2.getRef()}, 5, 2));
}

{ // Two colliding children
Index c1=Index.create(Blobs.fromHex("1230"),CVMLong.ONE);
Index c2=Index.create(Blobs.fromHex("1230"),CVMLong.ZERO);
invalidTest(Index.unsafeCreate(3, null, new Ref[] {c1.getRef(),c2.getRef()}, 1, 2));
invalidTest(Index.unsafeCreate(3, null, new Ref[] {c1.getRef(),c2.getRef()}, 3, 2));
}

{ // Two colliding children at max depth
Index c1=Index.create(Blobs.fromHex("0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef00"),CVMLong.ONE);
Index c2=Index.create(Blobs.fromHex("0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef11"),CVMLong.ONE);
invalidTest(Index.unsafeCreate(64, null, new Ref[] {c1.getRef(),c2.getRef()}, 3, 2));
}

}

Expand Down Expand Up @@ -303,7 +321,7 @@ private void doEncodingTest(ACell b) {
Blob enc=null;
try {
enc= b.getEncoding();
} catch (Throwable t) {
} catch (Exception t) {
// probably no valid encoding, so skip this test
return;
}
Expand All @@ -316,7 +334,7 @@ private void doEncodingTest(ACell b) {
// not a readable format, so probably not dangerous
return;
} catch (InvalidDataException e) {
fail("Failed to validate after re-reading?");
fail("Failed to validate after re-reading?",e);
}

if (c.isCompletelyEncoded()) {
Expand Down

0 comments on commit 53c1da1

Please sign in to comment.