Skip to content

Commit 011df6a

Browse files
ic0nsTLS-Attacker Developer
andauthored
Fix SpotBugs warnings in RandomHelper (#252)
- Added SpotBugs annotations dependency - Suppressed MS_EXPOSE_REP warning on getRandom() method - Suppressed EI_EXPOSE_STATIC_REP2 warning on setRandom() method - Added test to document intentional mutable Random exposure - Added detailed justification for the design decision The RandomHelper class is designed for test environments where controlled randomness is required. Exposing the mutable Random instance is intentional to allow testing frameworks full control over randomness. Co-authored-by: TLS-Attacker Developer <[email protected]>
1 parent 4055efc commit 011df6a

File tree

3 files changed

+58
-0
lines changed

3 files changed

+58
-0
lines changed

pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,11 @@
100100
<groupId>org.apache.logging.log4j</groupId>
101101
<artifactId>log4j-core</artifactId>
102102
</dependency>
103+
<dependency>
104+
<groupId>com.github.spotbugs</groupId>
105+
<artifactId>spotbugs-annotations</artifactId>
106+
<scope>provided</scope>
107+
</dependency>
103108
<!-- scope: test -->
104109
<dependency>
105110
<groupId>org.glassfish.jaxb</groupId>

src/main/java/de/rub/nds/modifiablevariable/util/RandomHelper.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
*/
88
package de.rub.nds.modifiablevariable.util;
99

10+
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
1011
import java.util.Random;
1112

1213
/**
@@ -30,8 +31,20 @@ public final class RandomHelper {
3031
* <p>The fixed seed ensures reproducible "random" behavior across test runs. If the Random
3132
* instance hasn't been initialized yet, this method initializes it.
3233
*
34+
* <p><b>Note:</b> This method intentionally returns the mutable Random instance directly to
35+
* allow for flexible testing scenarios. The setRandom() method is also provided to replace the
36+
* instance entirely. This design is intentional for testing frameworks that need full control
37+
* over randomness.
38+
*
3339
* @return A Random instance with a fixed seed of 0
3440
*/
41+
@SuppressFBWarnings(
42+
value = "MS_EXPOSE_REP",
43+
justification =
44+
"Intentionally exposing mutable static Random for testing flexibility. "
45+
+ "This class is designed for test environments where controlled randomness "
46+
+ "is required, and the ability to modify or replace the Random instance "
47+
+ "is a feature, not a bug.")
3548
public static synchronized Random getRandom() {
3649
if (random == null) {
3750
random = new Random(0);
@@ -60,6 +73,13 @@ public static BadRandom getBadSecureRandom() {
6073
*
6174
* @param randomInstance The Random instance to use as the singleton
6275
*/
76+
@SuppressFBWarnings(
77+
value = "EI_EXPOSE_STATIC_REP2",
78+
justification =
79+
"Intentionally allowing external Random instances to be set for testing flexibility. "
80+
+ "This class is designed for test environments where controlled randomness "
81+
+ "is required, and the ability to replace the Random instance "
82+
+ "is a feature, not a bug.")
6383
public static synchronized void setRandom(Random randomInstance) {
6484
random = randomInstance;
6585
}

src/test/java/de/rub/nds/modifiablevariable/util/RandomHelperTest.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,39 @@ void testBadRandomConstructors() {
108108
assertNotNull(badRandom2.nextInt()); // Just verify it returns a value
109109
}
110110

111+
@Test
112+
void testIntentionalMutableRandomExposure() {
113+
// This test documents the intentional design decision to expose the mutable Random instance
114+
Random random1 = RandomHelper.getRandom();
115+
Random random2 = RandomHelper.getRandom();
116+
117+
// Verify we get the same instance (singleton pattern)
118+
assertTrue(random1 == random2, "Should return the same Random instance");
119+
120+
// Verify that external modifications affect the singleton
121+
// This is intentional behavior for testing flexibility
122+
random1.setSeed(42);
123+
124+
// Create a separate Random with same seed to compare expected values
125+
Random expectedRandom = new Random(42);
126+
127+
// Both references should produce the same value as the expected Random
128+
int expectedValue = expectedRandom.nextInt();
129+
int random1Value = random1.nextInt();
130+
assertEquals(expectedValue, random1Value, "Modified Random should produce expected value");
131+
132+
// Reset and get second value to verify both references see same state
133+
random1.setSeed(42);
134+
expectedRandom.setSeed(42);
135+
assertEquals(
136+
expectedRandom.nextInt(),
137+
random2.nextInt(),
138+
"All references to the singleton should see the same state");
139+
140+
// Reset for other tests
141+
RandomHelper.setRandom(new Random(0));
142+
}
143+
111144
@Test
112145
void testBadFixedRandom() {
113146
byte fixedValue = 42; // Using a single byte as per the class implementation

0 commit comments

Comments
 (0)