Skip to content

Commit

Permalink
Update CollectionsBidiMapFuzzer.java Enhance Fuzzing Coverage and Inp…
Browse files Browse the repository at this point in the history
…ut Variability for CollectionsBidiMapFuzzer (#12848)

PR Description:

In this PR improve fuzzing coverage handle more diverse input sizes, and
ensure the `BidiMap` is freshly initialized for each test. The changes
focus on improving the randomness of the fuzzed input, ensuring that
each test runs with an independent map instance, and making the fuzzing
more comprehensive by introducing variable-length strings.
Changes Done in this:

1. **BidiMap Initialization Inside `runTest` Method**:
- The `BidiMap` (`m_bidiMap`) is now instantiated inside the `runTest`
method, ensuring that each fuzzing test operates on a fresh map
instance. This avoids potential issues where the map state is carried
over between tests, ensuring more accurate results.

2. **Handling Variable Size Strings**:
- The fuzzed input strings are now consumed with random lengths between
5 and 15 characters (instead of always using a fixed size of 10). This
change makes the fuzzing process more dynamic and tests the code against
a wider range of string sizes, increasing the likelihood of uncovering
edge cases.

3. **Improved Fuzzing Coverage**:
- The use of variable-length strings is applied across all test cases in
the `runTest` method (`put`, `get`, `getKey`, `removeValue`,
`inverseBidiMap`). By introducing more variability in the input size,
the test now covers a wider set of scenarios, ensuring a more thorough
fuzzing process.
  • Loading branch information
Shivam7-1 authored Feb 5, 2025
1 parent e007cb7 commit abb1871
Showing 1 changed file with 44 additions and 38 deletions.
82 changes: 44 additions & 38 deletions projects/apache-commons-collections/CollectionsBidiMapFuzzer.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,47 +15,53 @@
////////////////////////////////////////////////////////////////////////////////

import com.code_intelligence.jazzer.api.FuzzedDataProvider;

import org.apache.commons.collections4.BidiMap;
import org.apache.commons.collections4.bidimap.TreeBidiMap;

public class CollectionsBidiMapFuzzer {

BidiMap m_bidiMap = null;

public CollectionsBidiMapFuzzer(FuzzedDataProvider data) {
m_bidiMap = new TreeBidiMap();
}

void runTest(FuzzedDataProvider data) {
switch(data.consumeInt(1, 5)) {
case 1:
try {
m_bidiMap.put(data.consumeString(10), data.consumeString(10));
} catch(IllegalArgumentException ex) {
/* documented, ignore */
}
break;
case 2:
m_bidiMap.get(data.consumeString(10));
break;
case 3:
m_bidiMap.getKey(data.consumeString(10));
break;
case 4:
try {
m_bidiMap.removeValue(data.consumeString(10));
} catch(UnsupportedOperationException ex) {
/* documented, ignore */
}
break;
case 5:
m_bidiMap.inverseBidiMap();
}
}

public static void fuzzerTestOneInput(FuzzedDataProvider data) {
CollectionsBidiMapFuzzer testClosure = new CollectionsBidiMapFuzzer(data);
testClosure.runTest(data);
}
void runTest(FuzzedDataProvider data) {
// Initialize the BidiMap inside the method for each test run
BidiMap<String, String> m_bidiMap = new TreeBidiMap<>();

switch(data.consumeInt(1, 5)) {
case 1:
String key = data.consumeString(data.consumeInt(5, 15));
String value = data.consumeString(data.consumeInt(5, 15));
try {
m_bidiMap.put(key, value);
} catch (IllegalArgumentException ex) {

}
break;

case 2:
String getKey = data.consumeString(data.consumeInt(5, 15));
m_bidiMap.get(getKey);
break;

case 3:
String getValue = data.consumeString(data.consumeInt(5, 15));
m_bidiMap.getKey(getValue);
break;

case 4:
String removeValue = data.consumeString(data.consumeInt(5, 15));
try {
m_bidiMap.removeValue(removeValue);
} catch (UnsupportedOperationException ex) {

}
break;

case 5:
m_bidiMap.inverseBidiMap();
break;
}
}

public static void fuzzerTestOneInput(FuzzedDataProvider data) {
CollectionsBidiMapFuzzer testClosure = new CollectionsBidiMapFuzzer();
testClosure.runTest(data);
}
}

0 comments on commit abb1871

Please sign in to comment.