Skip to content

Commit e6aae30

Browse files
authored
fix(mock): avoid overflow in inclusive integer ranges (#8495)
1 parent 80559e8 commit e6aae30

3 files changed

Lines changed: 49 additions & 1 deletion

File tree

‎shenyu-plugin/shenyu-plugin-mock/src/main/java/org/apache/shenyu/plugin/mock/util/RandomUtil.java‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ public class RandomUtil {
3535
*/
3636
public static int randomInt(final int min, final int max) {
3737
ThreadLocalRandom random = ThreadLocalRandom.current();
38-
return random.nextInt(max - min + 1) + min;
38+
// Widen the exclusive upper bound to preserve inclusive ranges across integer overflow.
39+
return (int) random.nextLong(min, (long) max + 1);
3940
}
4041

4142
/**

‎shenyu-plugin/shenyu-plugin-mock/src/test/java/org/apache/shenyu/plugin/mock/MockPluginTest.java‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import org.junit.jupiter.api.Test;
3232
import org.junit.jupiter.api.extension.ExtendWith;
3333
import org.junit.jupiter.params.ParameterizedTest;
34+
import org.junit.jupiter.params.provider.CsvSource;
3435
import org.junit.jupiter.params.provider.ValueSource;
3536
import org.mockito.Mock;
3637
import org.mockito.junit.jupiter.MockitoExtension;
@@ -45,6 +46,7 @@
4546
import static org.hamcrest.MatcherAssert.assertThat;
4647
import static org.hamcrest.core.Is.is;
4748
import static org.junit.jupiter.api.Assertions.assertEquals;
49+
import static org.junit.jupiter.api.Assertions.assertTrue;
4850
import static org.mockito.ArgumentMatchers.any;
4951
import static org.mockito.Mockito.mock;
5052
import static org.mockito.Mockito.never;
@@ -147,6 +149,24 @@ public void testDoExecuteWithoutCachedHandle() {
147149
verify(chain).execute(any());
148150
}
149151

152+
@ParameterizedTest
153+
@CsvSource({"0, 2147483647", "2147483647, 2147483647", "0, 0", "10, 15"})
154+
public void testIntegerResponseSupportsInclusiveBounds(final int min, final int max) {
155+
RuleData ruleData = buildRuleData();
156+
MockHandle mockHandle = new MockHandle();
157+
mockHandle.setResponseContent(String.format("${int|%d-%d}", min, max));
158+
MockPluginHandler.CACHED_HANDLE.get().cachedHandle(CacheKeyUtils.INST.getKey(ruleData), mockHandle);
159+
160+
StepVerifier.create(mockPlugin.doExecute(exchange, chain, mock(SelectorData.class), ruleData)).verifyComplete();
161+
162+
String body = exchange.getResponse().getBodyAsString().block();
163+
int value = Integer.parseInt(body);
164+
assertTrue(value >= min && value <= max);
165+
assertEquals(HttpStatus.OK, exchange.getResponse().getStatusCode());
166+
assertEquals(body.getBytes(StandardCharsets.UTF_8).length, exchange.getResponse().getHeaders().getContentLength());
167+
verify(chain, never()).execute(any());
168+
}
169+
150170
@Test
151171
public void testGetOrder() {
152172
final int result = mockPlugin.getOrder();

‎shenyu-plugin/shenyu-plugin-mock/src/test/java/org/apache/shenyu/plugin/mock/util/MockUtilTest.java‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,41 @@
1818
package org.apache.shenyu.plugin.mock.util;
1919

2020
import org.junit.jupiter.api.Test;
21+
import org.junit.jupiter.params.ParameterizedTest;
22+
import org.junit.jupiter.params.provider.CsvSource;
2123

24+
import static org.junit.jupiter.api.Assertions.assertThrows;
2225
import static org.junit.jupiter.api.Assertions.assertTrue;
2326

2427
/**
2528
* Test case for {@link MockUtil}.
2629
*/
2730
public final class MockUtilTest {
2831

32+
@ParameterizedTest
33+
@CsvSource({
34+
"0, 2147483647",
35+
"-2147483648, 2147483647",
36+
"-2147483648, 0",
37+
"-2147483648, -1",
38+
"2147483647, 2147483647",
39+
"-2147483648, -2147483648",
40+
"0, 0",
41+
"10, 15"
42+
})
43+
public void randomIntShouldSupportInclusiveIntegerBounds(final int min, final int max) {
44+
for (int i = 0; i < 100; i++) {
45+
int value = MockUtil.randomInt(min, max);
46+
assertTrue(value >= min && value <= max, "Generated value must remain within the inclusive bounds");
47+
}
48+
}
49+
50+
@ParameterizedTest
51+
@CsvSource({"1, 0", "2147483647, -2147483648"})
52+
public void randomIntShouldRejectReversedBounds(final int min, final int max) {
53+
assertThrows(IllegalArgumentException.class, () -> MockUtil.randomInt(min, max));
54+
}
55+
2956
@Test
3057
public void zhShouldReturnLengthsWithinTheInclusiveRange() {
3158
for (int i = 0; i < 500; i++) {

0 commit comments

Comments
 (0)