From 409cbc0c92bc4eacdbff1431c9b0ab5183a47c78 Mon Sep 17 00:00:00 2001 From: Robert Merget Date: Thu, 19 Jun 2025 06:24:28 +0000 Subject: [PATCH 1/3] Add XML serialization tests for ModifiableLengthField - Add comprehensive JAXB serialization/deserialization tests - Test serialization with and without modifications - Test handling of null byte array values - Ensure private no-args constructor is covered --- ...odifiableLengthFieldSerializationTest.java | 125 ++++++++++++++++++ 1 file changed, 125 insertions(+) create mode 100644 src/test/java/de/rub/nds/modifiablevariable/length/ModifiableLengthFieldSerializationTest.java diff --git a/src/test/java/de/rub/nds/modifiablevariable/length/ModifiableLengthFieldSerializationTest.java b/src/test/java/de/rub/nds/modifiablevariable/length/ModifiableLengthFieldSerializationTest.java new file mode 100644 index 00000000..9739ac24 --- /dev/null +++ b/src/test/java/de/rub/nds/modifiablevariable/length/ModifiableLengthFieldSerializationTest.java @@ -0,0 +1,125 @@ +/* + * ModifiableVariable - A Variable Concept for Runtime Modifications + * + * Ruhr University Bochum, Paderborn University, Technology Innovation Institute, and Hackmanit GmbH + * + * Licensed under Apache License 2.0 http://www.apache.org/licenses/LICENSE-2.0 + */ +package de.rub.nds.modifiablevariable.length; + +import static org.junit.jupiter.api.Assertions.*; + +import de.rub.nds.modifiablevariable.bytearray.ModifiableByteArray; +import de.rub.nds.modifiablevariable.integer.IntegerAddModification; +import jakarta.xml.bind.JAXBContext; +import jakarta.xml.bind.JAXBException; +import jakarta.xml.bind.Marshaller; +import jakarta.xml.bind.Unmarshaller; +import java.io.StringReader; +import java.io.StringWriter; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class ModifiableLengthFieldSerializationTest { + + private static final Logger LOGGER = + LogManager.getLogger(ModifiableLengthFieldSerializationTest.class); + + private ModifiableLengthField lengthField; + private ModifiableByteArray byteArray; + private JAXBContext context; + private Marshaller marshaller; + private Unmarshaller unmarshaller; + + @BeforeEach + public void setUp() throws JAXBException { + byteArray = new ModifiableByteArray(); + byteArray.setOriginalValue(new byte[] {1, 2, 3, 4, 5}); + lengthField = new ModifiableLengthField(byteArray); + + context = + JAXBContext.newInstance( + ModifiableLengthField.class, + ModifiableByteArray.class, + IntegerAddModification.class); + marshaller = context.createMarshaller(); + marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); + unmarshaller = context.createUnmarshaller(); + } + + @Test + public void testSerializeDeserializeSimple() throws Exception { + StringWriter writer = new StringWriter(); + marshaller.marshal(lengthField, writer); + + String xmlString = writer.toString(); + LOGGER.debug("Serialized XML: {}", xmlString); + + ModifiableLengthField deserialized = + (ModifiableLengthField) unmarshaller.unmarshal(new StringReader(xmlString)); + + assertNotNull(deserialized); + assertEquals(5, (int) deserialized.getOriginalValue()); + assertEquals(5, (int) deserialized.getValue()); + } + + @Test + public void testSerializeDeserializeWithModification() throws Exception { + lengthField.setModifications(new IntegerAddModification(10)); + assertEquals(15, (int) lengthField.getValue()); + + StringWriter writer = new StringWriter(); + marshaller.marshal(lengthField, writer); + + String xmlString = writer.toString(); + LOGGER.debug("Serialized XML with modification: {}", xmlString); + + ModifiableLengthField deserialized = + (ModifiableLengthField) unmarshaller.unmarshal(new StringReader(xmlString)); + + assertNotNull(deserialized); + assertEquals(5, (int) deserialized.getOriginalValue()); + assertEquals(15, (int) deserialized.getValue()); + } + + @Test + public void testSerializeDeserializeWithNullByteArrayValue() throws Exception { + byteArray.setOriginalValue(null); + assertNull(lengthField.getOriginalValue()); + assertNull(lengthField.getValue()); + + StringWriter writer = new StringWriter(); + marshaller.marshal(lengthField, writer); + + String xmlString = writer.toString(); + LOGGER.debug("Serialized XML with null byte array: {}", xmlString); + + ModifiableLengthField deserialized = + (ModifiableLengthField) unmarshaller.unmarshal(new StringReader(xmlString)); + + assertNotNull(deserialized); + assertNull(deserialized.getOriginalValue()); + assertNull(deserialized.getValue()); + } + + @Test + public void testSerializedFieldReferencesArePreserved() throws Exception { + lengthField.setModifications(new IntegerAddModification(3)); + + StringWriter writer = new StringWriter(); + marshaller.marshal(lengthField, writer); + String xmlString = writer.toString(); + + ModifiableLengthField deserialized = + (ModifiableLengthField) unmarshaller.unmarshal(new StringReader(xmlString)); + + byteArray.setOriginalValue(new byte[] {1, 2}); + assertEquals(2, (int) lengthField.getOriginalValue()); + assertEquals(5, (int) lengthField.getValue()); + + assertEquals(5, (int) deserialized.getOriginalValue()); + assertEquals(8, (int) deserialized.getValue()); + } +} From 516c5c5250e97d912af9f4b08d6db7fb0ac22c32 Mon Sep 17 00:00:00 2001 From: Robert Merget Date: Thu, 19 Jun 2025 06:25:39 +0000 Subject: [PATCH 2/3] Add edge case test for equals with null value comparison - Test comparing fields where one has null value due to modification - Ensure proper handling of null values in equals method - Improve test coverage for edge cases --- .../length/ModifiableLengthFieldTest.java | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/src/test/java/de/rub/nds/modifiablevariable/length/ModifiableLengthFieldTest.java b/src/test/java/de/rub/nds/modifiablevariable/length/ModifiableLengthFieldTest.java index e31d2054..fa2ae496 100644 --- a/src/test/java/de/rub/nds/modifiablevariable/length/ModifiableLengthFieldTest.java +++ b/src/test/java/de/rub/nds/modifiablevariable/length/ModifiableLengthFieldTest.java @@ -544,4 +544,38 @@ public void testReferenceWithNoOriginalValue() { "Original value should still be array length"); assertEquals(13, (int) lengthField.getValue(), "Value should include modification"); } + + /** Test edge case: comparing fields where one has modifications resulting in null */ + @Test + public void testEqualsWithOneNullValue() { + // Create two fields with the same reference + ModifiableByteArray sharedArray = new ModifiableByteArray(); + sharedArray.setOriginalValue(new byte[] {1, 2, 3, 4}); + + ModifiableLengthField field1 = new ModifiableLengthField(sharedArray); + ModifiableLengthField field2 = new ModifiableLengthField(sharedArray); + + // Create a special modification that returns null + class NullResultModification extends IntegerAddModification { + public NullResultModification() { + super(0); + } + + @Override + protected Integer modifyImplementationHook(Integer input) { + return null; + } + } + + // Apply null modification to only one field + field1.setModifications(new NullResultModification()); + + // field1.getValue() is null, field2.getValue() is 4 + assertNull(field1.getValue()); + assertEquals(4, (int) field2.getValue()); + + // They should not be equal + assertNotEquals(field1, field2); + assertNotEquals(field2, field1); + } } From 47acd9dee08118c1584b3e143c427a8889df7858 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fabian=20B=C3=A4umer?= Date: Wed, 2 Jul 2025 18:43:26 +0200 Subject: [PATCH 3/3] test: Adapt LengthFieldSerializationTest to JSON serialization --- .../LengthFieldSerializationTest.java} | 72 +++++++------------ 1 file changed, 26 insertions(+), 46 deletions(-) rename src/test/java/de/rub/nds/modifiablevariable/{length/ModifiableLengthFieldSerializationTest.java => json/LengthFieldSerializationTest.java} (54%) diff --git a/src/test/java/de/rub/nds/modifiablevariable/length/ModifiableLengthFieldSerializationTest.java b/src/test/java/de/rub/nds/modifiablevariable/json/LengthFieldSerializationTest.java similarity index 54% rename from src/test/java/de/rub/nds/modifiablevariable/length/ModifiableLengthFieldSerializationTest.java rename to src/test/java/de/rub/nds/modifiablevariable/json/LengthFieldSerializationTest.java index 9739ac24..d02ba4b0 100644 --- a/src/test/java/de/rub/nds/modifiablevariable/length/ModifiableLengthFieldSerializationTest.java +++ b/src/test/java/de/rub/nds/modifiablevariable/json/LengthFieldSerializationTest.java @@ -5,60 +5,49 @@ * * Licensed under Apache License 2.0 http://www.apache.org/licenses/LICENSE-2.0 */ -package de.rub.nds.modifiablevariable.length; +package de.rub.nds.modifiablevariable.json; import static org.junit.jupiter.api.Assertions.*; +import com.fasterxml.jackson.databind.ObjectMapper; import de.rub.nds.modifiablevariable.bytearray.ModifiableByteArray; import de.rub.nds.modifiablevariable.integer.IntegerAddModification; -import jakarta.xml.bind.JAXBContext; -import jakarta.xml.bind.JAXBException; -import jakarta.xml.bind.Marshaller; -import jakarta.xml.bind.Unmarshaller; -import java.io.StringReader; -import java.io.StringWriter; +import de.rub.nds.modifiablevariable.length.ModifiableLengthField; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; +import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -public class ModifiableLengthFieldSerializationTest { +public class LengthFieldSerializationTest { - private static final Logger LOGGER = - LogManager.getLogger(ModifiableLengthFieldSerializationTest.class); + private static final Logger LOGGER = LogManager.getLogger(); + private static ObjectMapper mapper; private ModifiableLengthField lengthField; private ModifiableByteArray byteArray; - private JAXBContext context; - private Marshaller marshaller; - private Unmarshaller unmarshaller; + + @BeforeAll + public static void setUpClass() { + mapper = new ObjectMapper(); + mapper.registerModule(new ModifiableVariableModule()); + mapper.setVisibility(ModifiableVariableModule.getFieldVisibilityChecker()); + } @BeforeEach - public void setUp() throws JAXBException { + public void setUp() { byteArray = new ModifiableByteArray(); byteArray.setOriginalValue(new byte[] {1, 2, 3, 4, 5}); lengthField = new ModifiableLengthField(byteArray); - - context = - JAXBContext.newInstance( - ModifiableLengthField.class, - ModifiableByteArray.class, - IntegerAddModification.class); - marshaller = context.createMarshaller(); - marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); - unmarshaller = context.createUnmarshaller(); } @Test public void testSerializeDeserializeSimple() throws Exception { - StringWriter writer = new StringWriter(); - marshaller.marshal(lengthField, writer); - - String xmlString = writer.toString(); - LOGGER.debug("Serialized XML: {}", xmlString); + String jsonString = mapper.writeValueAsString(lengthField); + LOGGER.debug("Serialized JSON: {}", jsonString); ModifiableLengthField deserialized = - (ModifiableLengthField) unmarshaller.unmarshal(new StringReader(xmlString)); + mapper.readValue(jsonString, ModifiableLengthField.class); assertNotNull(deserialized); assertEquals(5, (int) deserialized.getOriginalValue()); @@ -70,14 +59,11 @@ public void testSerializeDeserializeWithModification() throws Exception { lengthField.setModifications(new IntegerAddModification(10)); assertEquals(15, (int) lengthField.getValue()); - StringWriter writer = new StringWriter(); - marshaller.marshal(lengthField, writer); - - String xmlString = writer.toString(); - LOGGER.debug("Serialized XML with modification: {}", xmlString); + String jsonString = mapper.writeValueAsString(lengthField); + LOGGER.debug("Serialized JSON with modification: {}", jsonString); ModifiableLengthField deserialized = - (ModifiableLengthField) unmarshaller.unmarshal(new StringReader(xmlString)); + mapper.readValue(jsonString, ModifiableLengthField.class); assertNotNull(deserialized); assertEquals(5, (int) deserialized.getOriginalValue()); @@ -90,14 +76,11 @@ public void testSerializeDeserializeWithNullByteArrayValue() throws Exception { assertNull(lengthField.getOriginalValue()); assertNull(lengthField.getValue()); - StringWriter writer = new StringWriter(); - marshaller.marshal(lengthField, writer); - - String xmlString = writer.toString(); - LOGGER.debug("Serialized XML with null byte array: {}", xmlString); + String jsonString = mapper.writeValueAsString(lengthField); + LOGGER.debug("Serialized JSON with null byte array: {}", jsonString); ModifiableLengthField deserialized = - (ModifiableLengthField) unmarshaller.unmarshal(new StringReader(xmlString)); + mapper.readValue(jsonString, ModifiableLengthField.class); assertNotNull(deserialized); assertNull(deserialized.getOriginalValue()); @@ -108,12 +91,9 @@ public void testSerializeDeserializeWithNullByteArrayValue() throws Exception { public void testSerializedFieldReferencesArePreserved() throws Exception { lengthField.setModifications(new IntegerAddModification(3)); - StringWriter writer = new StringWriter(); - marshaller.marshal(lengthField, writer); - String xmlString = writer.toString(); - + String jsonString = mapper.writeValueAsString(lengthField); ModifiableLengthField deserialized = - (ModifiableLengthField) unmarshaller.unmarshal(new StringReader(xmlString)); + mapper.readValue(jsonString, ModifiableLengthField.class); byteArray.setOriginalValue(new byte[] {1, 2}); assertEquals(2, (int) lengthField.getOriginalValue());