|
| 1 | +/* |
| 2 | + * ModifiableVariable - A Variable Concept for Runtime Modifications |
| 3 | + * |
| 4 | + * Ruhr University Bochum, Paderborn University, Technology Innovation Institute, and Hackmanit GmbH |
| 5 | + * |
| 6 | + * Licensed under Apache License 2.0 http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | + */ |
| 8 | +package de.rub.nds.modifiablevariable.json; |
| 9 | + |
| 10 | +import static org.junit.jupiter.api.Assertions.*; |
| 11 | + |
| 12 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 13 | +import de.rub.nds.modifiablevariable.bytearray.ModifiableByteArray; |
| 14 | +import de.rub.nds.modifiablevariable.integer.IntegerAddModification; |
| 15 | +import de.rub.nds.modifiablevariable.length.ModifiableLengthField; |
| 16 | +import org.apache.logging.log4j.LogManager; |
| 17 | +import org.apache.logging.log4j.Logger; |
| 18 | +import org.junit.jupiter.api.BeforeAll; |
| 19 | +import org.junit.jupiter.api.BeforeEach; |
| 20 | +import org.junit.jupiter.api.Test; |
| 21 | + |
| 22 | +public class LengthFieldSerializationTest { |
| 23 | + |
| 24 | + private static final Logger LOGGER = LogManager.getLogger(); |
| 25 | + private static ObjectMapper mapper; |
| 26 | + |
| 27 | + private ModifiableLengthField lengthField; |
| 28 | + private ModifiableByteArray byteArray; |
| 29 | + |
| 30 | + @BeforeAll |
| 31 | + public static void setUpClass() { |
| 32 | + mapper = new ObjectMapper(); |
| 33 | + mapper.registerModule(new ModifiableVariableModule()); |
| 34 | + mapper.setVisibility(ModifiableVariableModule.getFieldVisibilityChecker()); |
| 35 | + } |
| 36 | + |
| 37 | + @BeforeEach |
| 38 | + public void setUp() { |
| 39 | + byteArray = new ModifiableByteArray(); |
| 40 | + byteArray.setOriginalValue(new byte[] {1, 2, 3, 4, 5}); |
| 41 | + lengthField = new ModifiableLengthField(byteArray); |
| 42 | + } |
| 43 | + |
| 44 | + @Test |
| 45 | + public void testSerializeDeserializeSimple() throws Exception { |
| 46 | + String jsonString = mapper.writeValueAsString(lengthField); |
| 47 | + LOGGER.debug("Serialized JSON: {}", jsonString); |
| 48 | + |
| 49 | + ModifiableLengthField deserialized = |
| 50 | + mapper.readValue(jsonString, ModifiableLengthField.class); |
| 51 | + |
| 52 | + assertNotNull(deserialized); |
| 53 | + assertEquals(5, (int) deserialized.getOriginalValue()); |
| 54 | + assertEquals(5, (int) deserialized.getValue()); |
| 55 | + } |
| 56 | + |
| 57 | + @Test |
| 58 | + public void testSerializeDeserializeWithModification() throws Exception { |
| 59 | + lengthField.setModifications(new IntegerAddModification(10)); |
| 60 | + assertEquals(15, (int) lengthField.getValue()); |
| 61 | + |
| 62 | + String jsonString = mapper.writeValueAsString(lengthField); |
| 63 | + LOGGER.debug("Serialized JSON with modification: {}", jsonString); |
| 64 | + |
| 65 | + ModifiableLengthField deserialized = |
| 66 | + mapper.readValue(jsonString, ModifiableLengthField.class); |
| 67 | + |
| 68 | + assertNotNull(deserialized); |
| 69 | + assertEquals(5, (int) deserialized.getOriginalValue()); |
| 70 | + assertEquals(15, (int) deserialized.getValue()); |
| 71 | + } |
| 72 | + |
| 73 | + @Test |
| 74 | + public void testSerializeDeserializeWithNullByteArrayValue() throws Exception { |
| 75 | + byteArray.setOriginalValue(null); |
| 76 | + assertNull(lengthField.getOriginalValue()); |
| 77 | + assertNull(lengthField.getValue()); |
| 78 | + |
| 79 | + String jsonString = mapper.writeValueAsString(lengthField); |
| 80 | + LOGGER.debug("Serialized JSON with null byte array: {}", jsonString); |
| 81 | + |
| 82 | + ModifiableLengthField deserialized = |
| 83 | + mapper.readValue(jsonString, ModifiableLengthField.class); |
| 84 | + |
| 85 | + assertNotNull(deserialized); |
| 86 | + assertNull(deserialized.getOriginalValue()); |
| 87 | + assertNull(deserialized.getValue()); |
| 88 | + } |
| 89 | + |
| 90 | + @Test |
| 91 | + public void testSerializedFieldReferencesArePreserved() throws Exception { |
| 92 | + lengthField.setModifications(new IntegerAddModification(3)); |
| 93 | + |
| 94 | + String jsonString = mapper.writeValueAsString(lengthField); |
| 95 | + ModifiableLengthField deserialized = |
| 96 | + mapper.readValue(jsonString, ModifiableLengthField.class); |
| 97 | + |
| 98 | + byteArray.setOriginalValue(new byte[] {1, 2}); |
| 99 | + assertEquals(2, (int) lengthField.getOriginalValue()); |
| 100 | + assertEquals(5, (int) lengthField.getValue()); |
| 101 | + |
| 102 | + assertEquals(5, (int) deserialized.getOriginalValue()); |
| 103 | + assertEquals(8, (int) deserialized.getValue()); |
| 104 | + } |
| 105 | +} |
0 commit comments