From df853ef46a9c12d319bf824ac106a411f5eddabd Mon Sep 17 00:00:00 2001 From: Matthias Andreas Benkard Date: Sun, 10 Dec 2023 20:29:35 +0100 Subject: Add property-based tests and fix the bugs discovered. Change-Id: I8deb1a7d75078c037714541d8f6f656052c2476c --- .../ostree/OstreeDecoderPropertyTest.java | 66 ++++++++++++++++++++++ .../mulk/jgvariant/ostree/OstreeDecoderTest.java | 14 ++--- 2 files changed, 71 insertions(+), 9 deletions(-) create mode 100644 jgvariant-ostree/src/test/java/eu/mulk/jgvariant/ostree/OstreeDecoderPropertyTest.java (limited to 'jgvariant-ostree/src') diff --git a/jgvariant-ostree/src/test/java/eu/mulk/jgvariant/ostree/OstreeDecoderPropertyTest.java b/jgvariant-ostree/src/test/java/eu/mulk/jgvariant/ostree/OstreeDecoderPropertyTest.java new file mode 100644 index 0000000..acd11c4 --- /dev/null +++ b/jgvariant-ostree/src/test/java/eu/mulk/jgvariant/ostree/OstreeDecoderPropertyTest.java @@ -0,0 +1,66 @@ +package eu.mulk.jgvariant.ostree; + +import eu.mulk.jgvariant.core.Decoder; +import java.util.Map; +import net.jqwik.api.*; + +@SuppressWarnings("java:S2187") +class OstreeDecoderPropertyTest { + + @Group + class SummaryRoundtripLaw implements RoundtripLaw { + + @Override + public Decoder decoder() { + return Summary.decoder(); + } + + @Override + public Arbitrary anyT() { + return anySummary(); + } + } + + interface RoundtripLaw { + + @Property + default boolean roundtripsWell(@ForAll(value = "anyT") T entityLeft) { + var decoder = decoder(); + var bytes = decoder.encode(entityLeft); + var entityRight = decoder.decode(bytes); + return entityLeft.equals(entityRight); + } + + Decoder decoder(); + + @Provide + Arbitrary anyT(); + } + + @Provide + Arbitrary anySummary() { + return Combinators.combine(anySummaryEntry().list(), anyMetadata()).as(Summary::new); + } + + @Provide + Arbitrary anyMetadata() { + return Arbitraries.of(new Metadata(Map.of())); + } + + @Provide + Arbitrary anySummaryEntry() { + return Combinators.combine(Arbitraries.strings(), anySummaryEntryValue()) + .as(Summary.Entry::new); + } + + @Provide + Arbitrary anySummaryEntryValue() { + return Combinators.combine(Arbitraries.integers(), anyChecksum(), anyMetadata()) + .as(Summary.Entry.Value::new); + } + + @Provide + Arbitrary anyChecksum() { + return Arbitraries.of(new Checksum(new ByteString(new byte[32]))); + } +} diff --git a/jgvariant-ostree/src/test/java/eu/mulk/jgvariant/ostree/OstreeDecoderTest.java b/jgvariant-ostree/src/test/java/eu/mulk/jgvariant/ostree/OstreeDecoderTest.java index 4465d02..793c5a5 100644 --- a/jgvariant-ostree/src/test/java/eu/mulk/jgvariant/ostree/OstreeDecoderTest.java +++ b/jgvariant-ostree/src/test/java/eu/mulk/jgvariant/ostree/OstreeDecoderTest.java @@ -110,10 +110,9 @@ class OstreeDecoderTest { summary.metadata().fields())); var encoded = decoder.encode(summary); + input.rewind(); assertEquals(input, encoded); - - System.out.println(summary); } @Test @@ -123,10 +122,9 @@ class OstreeDecoderTest { var commit = decoder.decode(input); var encoded = decoder.encode(commit); + input.rewind(); assertEquals(input, encoded); - - System.out.println(commit); } @Test @@ -136,10 +134,9 @@ class OstreeDecoderTest { var dirTree = decoder.decode(input); var encoded = decoder.encode(dirTree); + input.rewind(); assertEquals(input, encoded); - - System.out.println(dirTree); } @Test @@ -149,10 +146,9 @@ class OstreeDecoderTest { var dirMeta = decoder.decode(ByteBuffer.wrap(dirMetaBytes)); var encoded = decoder.encode(dirMeta); + input.rewind(); assertEquals(input, encoded); - - System.out.println(dirMeta); } @Test @@ -160,9 +156,9 @@ class OstreeDecoderTest { var decoder = DeltaSuperblock.decoder(); var input = ByteBuffer.wrap(deltaSuperblockBytes); var deltaSuperblock = decoder.decode(input); - System.out.println(deltaSuperblock); var encoded = decoder.encode(deltaSuperblock); + input.rewind(); assertEquals(input, encoded); } -- cgit v1.2.3