Skip to content

Commit b68be37

Browse files
committed
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
1 parent df26c39 commit b68be37

File tree

4 files changed

+33
-8
lines changed

4 files changed

+33
-8
lines changed

examples/junit/src/test/java/com/example/DictionaryFuzzTests.java

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public class DictionaryFuzzTests {
3232
private static final byte[] FLAG_SHA256 =
3333
Base64.getDecoder().decode("vCLInoVuMxJonT4UKjsMl0LPXTowkYS7t0uBpw0pRo8=");
3434

35-
@DictionaryEntries({"a_", "53Cr\"3T_", "fl4G"})
35+
@DictionaryEntries({"a_53Cr\"3T_fl4G"})
3636
@FuzzTest
3737
public void inlineTest(FuzzedDataProvider data)
3838
throws NoSuchAlgorithmException, TestSuccessfulException {
@@ -54,15 +54,42 @@ public void fileTest(FuzzedDataProvider data)
5454
}
5555
}
5656

57-
@DictionaryEntries("a_")
57+
/**
58+
* This test uses multiple dictionaries, one of which is loaded on-the-fly. The other two are
59+
* loaded from files. The test will only succeed if entries from all three dictionaries have been
60+
* used at least once to generate fuzzer data input completely.
61+
*/
62+
private static final byte[] ON_THE_FLY_FLAG_SHA256 =
63+
Base64.getDecoder().decode("YgF0VLwI07ls++qMJ/Ptmc9Q7xlIErMXXXK1enpNqKw=");
64+
65+
private static final byte[] FILE_TEST2_FLAG_SHA256 =
66+
Base64.getDecoder().decode("nIQ780j1iRJbOouWMvwhmS63i5lcHlhBf780CFmXvdc=");
67+
private static final byte[] FILE_TEST3_FLAG_SHA256 =
68+
Base64.getDecoder().decode("JR9EuaWZ5hyEHy2Ynfh1KCLI60UL8evAYYTAoJhCcaY=");
69+
70+
private static boolean onTheFlyDictionaryFound =
71+
false; // =o1pPß?1bHJAfas => YgF0VLwI07ls++qMJ/Ptmc9Q7xlIErMXXXK1enpNqKw=
72+
private static boolean fileTest2DictFound =
73+
false; // 0Z1o21ka => nIQ780j1iRJbOouWMvwhmS63i5lcHlhBf780CFmXvdc=
74+
private static boolean fileTest3DictFound =
75+
false; // A)716&=Ko => JR9EuaWZ5hyEHy2Ynfh1KCLI60UL8evAYYTAoJhCcaY=
76+
77+
@DictionaryEntries("=o1pPß?1bHJAfas")
5878
@DictionaryFile(resourcePath = "test2.dict")
5979
@DictionaryFile(resourcePath = "/com/example/test3.dict")
6080
@FuzzTest
6181
public void mixedTest(FuzzedDataProvider data)
6282
throws NoSuchAlgorithmException, TestSuccessfulException {
6383
String s = data.consumeRemainingAsString();
6484
byte[] hash = MessageDigest.getInstance("SHA-256").digest(s.getBytes(StandardCharsets.UTF_8));
65-
if (MessageDigest.isEqual(hash, FLAG_SHA256)) {
85+
if (MessageDigest.isEqual(hash, ON_THE_FLY_FLAG_SHA256)) {
86+
onTheFlyDictionaryFound = true;
87+
} else if (MessageDigest.isEqual(hash, FILE_TEST2_FLAG_SHA256)) {
88+
fileTest2DictFound = true;
89+
} else if (MessageDigest.isEqual(hash, FILE_TEST3_FLAG_SHA256)) {
90+
fileTest3DictFound = true;
91+
}
92+
if (onTheFlyDictionaryFound && fileTest2DictFound && fileTest3DictFound) {
6693
throw new TestSuccessfulException("error found");
6794
}
6895
}
Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,2 @@
11
# test dictionary
2-
"a_"
3-
"53Cr\"3T_"
4-
"fl4G"
2+
"a_53Cr\"3T_fl4G"
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
"53Cr\"3T_"
1+
"0Z1o21ka"
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
"fl4G"
1+
"A)716&=Ko"

0 commit comments

Comments
 (0)