-
-
Notifications
You must be signed in to change notification settings - Fork 719
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5780 from thc202/fuzz/address-lossy-warns
- Loading branch information
Showing
6 changed files
with
145 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
addOns/fuzz/src/main/java/org/zaproxy/zap/extension/fuzz/impl/Utils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/* | ||
* Zed Attack Proxy (ZAP) and its related class files. | ||
* | ||
* ZAP is an HTTP/HTTPS proxy for assessing web application security. | ||
* | ||
* Copyright 2024 The ZAP Development Team | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.zaproxy.zap.extension.fuzz.impl; | ||
|
||
import java.util.stream.LongStream; | ||
|
||
public final class Utils { | ||
|
||
private Utils() {} | ||
|
||
public static int sumLongToInt(LongStream values) { | ||
try { | ||
long result = values.reduce(0, Math::addExact); | ||
if (result > Integer.MAX_VALUE) { | ||
return Integer.MAX_VALUE; | ||
} | ||
return (int) result; | ||
} catch (ArithmeticException e) { | ||
return Integer.MAX_VALUE; | ||
} | ||
} | ||
|
||
public static long sum(LongStream values) { | ||
try { | ||
return values.reduce(0, Math::addExact); | ||
} catch (ArithmeticException e) { | ||
return Long.MAX_VALUE; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
addOns/fuzz/src/test/java/org/zaproxy/zap/extension/fuzz/impl/UtilsUnitTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
/* | ||
* Zed Attack Proxy (ZAP) and its related class files. | ||
* | ||
* ZAP is an HTTP/HTTPS proxy for assessing web application security. | ||
* | ||
* Copyright 2024 The ZAP Development Team | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.zaproxy.zap.extension.fuzz.impl; | ||
|
||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.Matchers.equalTo; | ||
import static org.hamcrest.Matchers.is; | ||
|
||
import java.util.stream.LongStream; | ||
import org.junit.jupiter.api.Test; | ||
|
||
/** Unit test for {@link Utils}. */ | ||
class UtilsUnitTest { | ||
|
||
@Test | ||
void shouldSumLongs() { | ||
// Given | ||
var values = LongStream.of(1L, 2L); | ||
// When | ||
long result = Utils.sum(values); | ||
// Then | ||
assertThat(result, is(equalTo(3L))); | ||
} | ||
|
||
@Test | ||
void shouldSumLongsHandlingOverflow() { | ||
// Given | ||
var values = LongStream.of(Long.MAX_VALUE, Long.MAX_VALUE); | ||
// When | ||
long result = Utils.sum(values); | ||
// Then | ||
assertThat(result, is(equalTo(Long.MAX_VALUE))); | ||
} | ||
|
||
@Test | ||
void shouldSumLongsToInt() { | ||
// Given | ||
var values = LongStream.of(1L, 2L); | ||
// When | ||
int result = Utils.sumLongToInt(values); | ||
// Then | ||
assertThat(result, is(equalTo(3))); | ||
} | ||
|
||
@Test | ||
void shouldSumLongsToIntHandlingLongOverflow() { | ||
// Given | ||
var values = LongStream.of(Long.MAX_VALUE, Long.MAX_VALUE); | ||
// When | ||
int result = Utils.sumLongToInt(values); | ||
// Then | ||
assertThat(result, is(equalTo(Integer.MAX_VALUE))); | ||
} | ||
|
||
@Test | ||
void shouldSumLongsToIntHandlingIntegerOverflow() { | ||
// Given | ||
var values = LongStream.of(Integer.MAX_VALUE, 1L); | ||
// When | ||
int result = Utils.sumLongToInt(values); | ||
// Then | ||
assertThat(result, is(equalTo(Integer.MAX_VALUE))); | ||
} | ||
} |