From 345c01d522f2bc153f15e981f256424d8f25d0dd Mon Sep 17 00:00:00 2001 From: Peter Samarin Date: Mon, 4 Aug 2025 23:57:31 +0200 Subject: [PATCH] chore(tests): speed up dictionary tests The dictionary tests make sure that the annotations @DictionaryEntries and @DictionaryFile actually get used in the fuzz test. However, for these tests, the fuzzer runs for a long time on Windows. Here are the test runtime changes from before and after this commit: - DictionaryFuzzTests_fileTest - 200.8s => 14.2s - DictionaryFuzzTests_inlineTest 124.4s => 11.7s - DictionaryFuzzTests_mixedTest 87.8s => 16.9s --- .../java/com/example/DictionaryFuzzTests.java | 33 +++++++++++++++++-- .../src/test/resources/com/example/test.dict | 4 +-- .../src/test/resources/com/example/test2.dict | 2 +- .../src/test/resources/com/example/test3.dict | 2 +- 4 files changed, 33 insertions(+), 8 deletions(-) diff --git a/examples/junit/src/test/java/com/example/DictionaryFuzzTests.java b/examples/junit/src/test/java/com/example/DictionaryFuzzTests.java index 4d484d903..ed76cba17 100644 --- a/examples/junit/src/test/java/com/example/DictionaryFuzzTests.java +++ b/examples/junit/src/test/java/com/example/DictionaryFuzzTests.java @@ -32,7 +32,7 @@ public class DictionaryFuzzTests { private static final byte[] FLAG_SHA256 = Base64.getDecoder().decode("vCLInoVuMxJonT4UKjsMl0LPXTowkYS7t0uBpw0pRo8="); - @DictionaryEntries({"a_", "53Cr\"3T_", "fl4G"}) + @DictionaryEntries({"a_53Cr\"3T_fl4G"}) @FuzzTest public void inlineTest(FuzzedDataProvider data) throws NoSuchAlgorithmException, TestSuccessfulException { @@ -54,7 +54,27 @@ public void fileTest(FuzzedDataProvider data) } } - @DictionaryEntries("a_") + /** + * This test uses multiple dictionaries, one of which is loaded on-the-fly. The other two are + * loaded from files. The test will only succeed if entries from all three dictionaries have been + * used at least once to generate fuzzer data input completely. + */ + private static final byte[] ON_THE_FLY_FLAG_SHA256 = + Base64.getDecoder().decode("YgF0VLwI07ls++qMJ/Ptmc9Q7xlIErMXXXK1enpNqKw="); + + private static final byte[] FILE_TEST2_FLAG_SHA256 = + Base64.getDecoder().decode("nIQ780j1iRJbOouWMvwhmS63i5lcHlhBf780CFmXvdc="); + private static final byte[] FILE_TEST3_FLAG_SHA256 = + Base64.getDecoder().decode("JR9EuaWZ5hyEHy2Ynfh1KCLI60UL8evAYYTAoJhCcaY="); + + private static boolean onTheFlyDictionaryFound = + false; // =o1pPß?1bHJAfas => YgF0VLwI07ls++qMJ/Ptmc9Q7xlIErMXXXK1enpNqKw= + private static boolean fileTest2DictFound = + false; // 0Z1o21ka => nIQ780j1iRJbOouWMvwhmS63i5lcHlhBf780CFmXvdc= + private static boolean fileTest3DictFound = + false; // A)716&=Ko => JR9EuaWZ5hyEHy2Ynfh1KCLI60UL8evAYYTAoJhCcaY= + + @DictionaryEntries("=o1pPß?1bHJAfas") @DictionaryFile(resourcePath = "test2.dict") @DictionaryFile(resourcePath = "/com/example/test3.dict") @FuzzTest @@ -62,7 +82,14 @@ public void mixedTest(FuzzedDataProvider data) throws NoSuchAlgorithmException, TestSuccessfulException { String s = data.consumeRemainingAsString(); byte[] hash = MessageDigest.getInstance("SHA-256").digest(s.getBytes(StandardCharsets.UTF_8)); - if (MessageDigest.isEqual(hash, FLAG_SHA256)) { + if (MessageDigest.isEqual(hash, ON_THE_FLY_FLAG_SHA256)) { + onTheFlyDictionaryFound = true; + } else if (MessageDigest.isEqual(hash, FILE_TEST2_FLAG_SHA256)) { + fileTest2DictFound = true; + } else if (MessageDigest.isEqual(hash, FILE_TEST3_FLAG_SHA256)) { + fileTest3DictFound = true; + } + if (onTheFlyDictionaryFound && fileTest2DictFound && fileTest3DictFound) { throw new TestSuccessfulException("error found"); } } diff --git a/examples/junit/src/test/resources/com/example/test.dict b/examples/junit/src/test/resources/com/example/test.dict index 07d483550..e331f06ce 100644 --- a/examples/junit/src/test/resources/com/example/test.dict +++ b/examples/junit/src/test/resources/com/example/test.dict @@ -1,4 +1,2 @@ # test dictionary -"a_" -"53Cr\"3T_" -"fl4G" +"a_53Cr\"3T_fl4G" diff --git a/examples/junit/src/test/resources/com/example/test2.dict b/examples/junit/src/test/resources/com/example/test2.dict index 9eecf66d0..c97540d65 100644 --- a/examples/junit/src/test/resources/com/example/test2.dict +++ b/examples/junit/src/test/resources/com/example/test2.dict @@ -1 +1 @@ -"53Cr\"3T_" +"0Z1o21ka" diff --git a/examples/junit/src/test/resources/com/example/test3.dict b/examples/junit/src/test/resources/com/example/test3.dict index b0aa4e06a..57c72202e 100644 --- a/examples/junit/src/test/resources/com/example/test3.dict +++ b/examples/junit/src/test/resources/com/example/test3.dict @@ -1 +1 @@ -"fl4G" \ No newline at end of file +"A)716&=Ko" \ No newline at end of file