Skip to content

Commit

Permalink
More testing edits
Browse files Browse the repository at this point in the history
  • Loading branch information
mikera committed Dec 5, 2024
1 parent a16d0d2 commit e432dc2
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 16 deletions.
4 changes: 2 additions & 2 deletions convex-core/src/main/java/convex/core/data/AVector.java
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,8 @@ public boolean isPacked() {
@Override
public boolean print(BlobBuilder sb, long limit) {
sb.append('[');
int size = size();
for (int i = 0; i < size; i++) {
long size = count();
for (long i = 0; i < size; i++) {
if (i > 0) sb.append(' ');
if (!RT.print(sb,get(i),limit)) return false;
}
Expand Down
1 change: 1 addition & 0 deletions convex-core/src/main/java/convex/core/data/BlobTree.java
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,7 @@ public static BlobTree read(long count, Blob src, int pos) throws BadFormatExcep
int rpos=pos+headerLength; // ref position
for (int i = 0; i < numChildren; i++) {
Ref<ABlob> ref = Format.readRef(src,rpos);
if (ref==Ref.NULL_VALUE) throw new BadFormatException("Null BlobTree child");
children[i] = ref;
rpos+=ref.getEncodingLength();
}
Expand Down
1 change: 1 addition & 0 deletions convex-core/src/main/java/convex/core/data/VectorTree.java
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@ public static <T extends ACell> VectorTree<T> read(long count, Blob b, int pos)
Ref<AVector<T>>[] items = (Ref<AVector<T>>[]) new Ref<?>[n];
for (int i = 0; i < n; i++) {
Ref<AVector<T>> ref = Format.readRef(b,rpos);
// if (ref==Ref.NULL_VALUE) throw new BadFormatException("Null VectorTree child");
items[i] = ref;
rpos+=ref.getEncodingLength();
}
Expand Down
38 changes: 24 additions & 14 deletions convex-core/src/test/java/convex/core/data/FuzzTestFormat.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
package convex.core.data;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.nio.BufferUnderflowException;
import java.util.Random;

import org.junit.jupiter.api.Test;

import convex.core.cvm.Symbols;
import convex.core.data.prim.CVMDouble;
import convex.core.exceptions.BadFormatException;
import convex.core.exceptions.InvalidDataException;
import convex.core.exceptions.MissingDataException;
import convex.core.lang.RT;
import convex.core.util.Utils;
import convex.test.Samples;

/**
* Fuzz testing for data formats.
Expand All @@ -27,12 +29,12 @@ public class FuzzTestFormat {

@Test
public void fuzzTest() {
// create lots of blobs, see what is readable
// create lots of random blobs, see what is readable

for (int i = 0; i < NUM_FUZZ; i++) {
long stime = System.currentTimeMillis();
r.setSeed(i * 1007);
Blob b = Blob.createRandom(r, 100);
Blob b = Blob.createRandom(r, 200);
try {
doFuzzTest(b);
doMutationTest(b);
Expand All @@ -52,15 +54,25 @@ public void fuzzTest() {
@Test
public void fuzzExamples() throws BadFormatException {
doCellFuzzTests(Symbols.FOO);
doCellFuzzTests(Samples.INT_VECTOR_10);
doCellFuzzTests(CVMDouble.POSITIVE_INFINITY);

doFuzzTest(Blob.fromHex("31080000000000000034"));
}

@Test
public void regressionExamples() {
// Interpreted as BlobTree will nil children (count 0xff00 = 32640)
assertThrows(BadFormatException.class,()->doFuzzTest(Blob.fromHex("31ff0000000000000034")));

// Interpreted as VectorTree with 768 elements
assertThrows(BadFormatException.class,()->doFuzzTest(Blob.fromHex("80860000000000000034")));
}

public static void doCellFuzzTests(ACell c) {
for (int i = 0; i < 1000; i++) {
Blob b=Cells.encode(c);
try {
doFuzzTest(b);
doMutationTest(b);
} catch (Exception e) {
throw Utils.sneakyThrow(e);
}
Expand All @@ -72,16 +84,19 @@ private static void doFuzzTest(Blob b) throws BadFormatException {
try {
v = Format.read(b,0);
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("Badd fuzzed read: "+b);
throw e;
// We read past buffer, so basically OK up to that point
// Try again with bigger buffer!
if (b.count()>0) doFuzzTest(b.append(b).toFlatBlob());
return;
}


// If we have read the object, check that we can validate as a cell, at minimum
try {
RT.validate(v);
} catch (InvalidDataException e) {
throw new BadFormatException("Validation failed",e);
} catch (MissingDataException e) {
throw new BadFormatException("Validation due to missing data",e);
}

// if we manage to read the object and it is not a Ref, it must be in canonical
Expand All @@ -97,7 +112,6 @@ private static void doFuzzTest(Blob b) throws BadFormatException {
if (r.nextDouble() < 0.8) {
doMutationTest(b2);
}

}

public static void doMutationTest(Blob b) {
Expand All @@ -108,12 +122,8 @@ public static void doMutationTest(Blob b) {
doFuzzTest(fuzzed);
} catch (BadFormatException e) {
/* OK */
} catch (BufferUnderflowException e) {
/* also OK */
} catch (MissingDataException e) {
/* also OK */
} catch (Exception e) {
System.err.println("Fuzz test bad blob: "+b);
System.err.println("Fuzz test bad blob: "+fuzzed);
throw e;
}
}
Expand Down

0 comments on commit e432dc2

Please sign in to comment.