From e432dc2c1bd373b9871ba2d927108c63a92c457d Mon Sep 17 00:00:00 2001 From: mikera Date: Thu, 5 Dec 2024 01:56:34 +0000 Subject: [PATCH] More testing edits --- .../main/java/convex/core/data/AVector.java | 4 +- .../main/java/convex/core/data/BlobTree.java | 1 + .../java/convex/core/data/VectorTree.java | 1 + .../java/convex/core/data/FuzzTestFormat.java | 38 ++++++++++++------- 4 files changed, 28 insertions(+), 16 deletions(-) diff --git a/convex-core/src/main/java/convex/core/data/AVector.java b/convex-core/src/main/java/convex/core/data/AVector.java index 7a9e61903..b63b63d47 100644 --- a/convex-core/src/main/java/convex/core/data/AVector.java +++ b/convex-core/src/main/java/convex/core/data/AVector.java @@ -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; } diff --git a/convex-core/src/main/java/convex/core/data/BlobTree.java b/convex-core/src/main/java/convex/core/data/BlobTree.java index f61412eae..6fe59f761 100644 --- a/convex-core/src/main/java/convex/core/data/BlobTree.java +++ b/convex-core/src/main/java/convex/core/data/BlobTree.java @@ -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 ref = Format.readRef(src,rpos); + if (ref==Ref.NULL_VALUE) throw new BadFormatException("Null BlobTree child"); children[i] = ref; rpos+=ref.getEncodingLength(); } diff --git a/convex-core/src/main/java/convex/core/data/VectorTree.java b/convex-core/src/main/java/convex/core/data/VectorTree.java index 8878d9902..1a4d2e62d 100644 --- a/convex-core/src/main/java/convex/core/data/VectorTree.java +++ b/convex-core/src/main/java/convex/core/data/VectorTree.java @@ -221,6 +221,7 @@ public static VectorTree read(long count, Blob b, int pos) Ref>[] items = (Ref>[]) new Ref[n]; for (int i = 0; i < n; i++) { Ref> ref = Format.readRef(b,rpos); + // if (ref==Ref.NULL_VALUE) throw new BadFormatException("Null VectorTree child"); items[i] = ref; rpos+=ref.getEncodingLength(); } diff --git a/convex-core/src/test/java/convex/core/data/FuzzTestFormat.java b/convex-core/src/test/java/convex/core/data/FuzzTestFormat.java index fd9cb4ca7..923ad21c9 100644 --- a/convex-core/src/test/java/convex/core/data/FuzzTestFormat.java +++ b/convex-core/src/test/java/convex/core/data/FuzzTestFormat.java @@ -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. @@ -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); @@ -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); } @@ -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 @@ -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) { @@ -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; } }