diff options
author | Norbert Schneider <norbert.schneider@code-intelligence.com> | 2023-04-25 13:58:16 +0200 |
---|---|---|
committer | Norbert Schneider <mail@bertschneider.de> | 2023-05-19 16:17:07 +0200 |
commit | adc1a7e12931e69d63a1c5fea3c4c709f786f32b (patch) | |
tree | 088e787415fc9150325ec4a7b7efe6e92cbccbaf /src/test/java/com | |
parent | 0f836a314533022e5187d51c24da55dd0156ac26 (diff) | |
download | jazzer-api-adc1a7e12931e69d63a1c5fea3c4c709f786f32b.tar.gz |
mutator: Byte array cross over
Diffstat (limited to 'src/test/java/com')
-rw-r--r-- | src/test/java/com/code_intelligence/jazzer/mutation/mutator/lang/ByteArrayMutatorTest.java | 67 |
1 files changed, 61 insertions, 6 deletions
diff --git a/src/test/java/com/code_intelligence/jazzer/mutation/mutator/lang/ByteArrayMutatorTest.java b/src/test/java/com/code_intelligence/jazzer/mutation/mutator/lang/ByteArrayMutatorTest.java index 748e0a3f..1592b17d 100644 --- a/src/test/java/com/code_intelligence/jazzer/mutation/mutator/lang/ByteArrayMutatorTest.java +++ b/src/test/java/com/code_intelligence/jazzer/mutation/mutator/lang/ByteArrayMutatorTest.java @@ -17,6 +17,7 @@ package com.code_intelligence.jazzer.mutation.mutator.lang; import static com.code_intelligence.jazzer.mutation.support.TestSupport.mockPseudoRandom; import static com.google.common.truth.Truth.assertThat; +import static org.junit.jupiter.api.Assertions.assertThrows; import com.code_intelligence.jazzer.mutation.annotation.NotNull; import com.code_intelligence.jazzer.mutation.annotation.WithLength; @@ -25,9 +26,9 @@ import com.code_intelligence.jazzer.mutation.mutator.libfuzzer.LibFuzzerMutator; import com.code_intelligence.jazzer.mutation.support.TestSupport.MockPseudoRandom; import com.code_intelligence.jazzer.mutation.support.TypeHolder; import org.junit.jupiter.api.AfterEach; -import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; +@SuppressWarnings({"unchecked", "ResultOfMethodCallIgnored"}) public class ByteArrayMutatorTest { /** * Some tests may set {@link LibFuzzerMutator#MOCK_SIZE_KEY} which can interfere with other tests @@ -75,8 +76,7 @@ public class ByteArrayMutatorTest { try (MockPseudoRandom prng = mockPseudoRandom()) { // the ByteArrayMutator will limit the maximum size of the data requested from libfuzzer to // WithLength::max so setting the mock mutator to make it bigger will cause an exception - Assertions.assertThrows( - ArrayIndexOutOfBoundsException.class, () -> { byte[] arr2 = mutator.mutate(arr, prng); }); + assertThrows(ArrayIndexOutOfBoundsException.class, () -> { mutator.mutate(arr, prng); }); } } @@ -88,10 +88,10 @@ public class ByteArrayMutatorTest { assertThat(mutator.toString()).isEqualTo("byte[]"); try (MockPseudoRandom prng = mockPseudoRandom(10)) { - // init will call closedrange(min, max) and the mock prng will assert that the given value + // init will call closedRange(min, max) and the mock prng will assert that the given value // above is between those values which we want to fail here to show that we're properly // clamping the range - Assertions.assertThrows(AssertionError.class, () -> { byte[] arr = mutator.init(prng); }); + assertThrows(AssertionError.class, () -> { mutator.init(prng); }); } } @@ -106,7 +106,7 @@ public class ByteArrayMutatorTest { // init will call closedrange(min, max) and the mock prng will assert that the given value // above is between those values which we want to fail here to show that we're properly // clamping the range - Assertions.assertThrows(AssertionError.class, () -> { byte[] arr = mutator.init(prng); }); + assertThrows(AssertionError.class, () -> { mutator.init(prng); }); } } @@ -131,4 +131,59 @@ public class ByteArrayMutatorTest { assertThat(arr).hasLength(5); assertThat(arr).isEqualTo(new byte[] {2, 4, 6, 0, 0}); } + + @Test + void testCrossOver() { + SerializingMutator<byte[]> mutator = + (SerializingMutator<byte[]>) LangMutators.newFactory().createOrThrow( + new TypeHolder<byte @NotNull[]>() {}.annotatedType()); + assertThat(mutator.toString()).isEqualTo("byte[]"); + + byte[] value = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; + byte[] otherValue = {10, 11, 12, 13, 14, 15, 16, 17, 18, 19}; + + byte[] crossedOver; + try (MockPseudoRandom prng = mockPseudoRandom( + // intersect arrays + 0, + // out length + 8, + // copy 3 from first + 3, + // copy 1 from second + 1, + // copy 1 from first, + 1, + // copy 3 from second + 3)) { + crossedOver = mutator.crossOver(value, otherValue, prng); + assertThat(crossedOver).isEqualTo(new byte[] {0, 1, 2, 10, 3, 11, 12, 13}); + } + + try (MockPseudoRandom prng = mockPseudoRandom( + // insert into action + 1, + // copy size + 3, + // from position + 5, + // to position + 2)) { + crossedOver = mutator.crossOver(value, otherValue, prng); + assertThat(crossedOver).isEqualTo(new byte[] {0, 1, 15, 16, 17, 2, 3, 4, 5, 6, 7, 8, 9}); + } + + try (MockPseudoRandom prng = mockPseudoRandom( + // overwrite action + 2, + // to position + 3, + // copy size + 3, + // from position + 4)) { + crossedOver = mutator.crossOver(value, otherValue, prng); + assertThat(crossedOver).isEqualTo(new byte[] {0, 1, 2, 14, 15, 16, 6, 7, 8, 9}); + } + } } |