Skip to content

Commit 31130ab

Browse files
authored
Merge pull request #233 from tls-attacker/length-package-tests-100-coverage
Achieve 100% test coverage for de.rub.nds.modifiablevariable.length package
2 parents 0c917e1 + 47acd9d commit 31130ab

File tree

2 files changed

+139
-0
lines changed

2 files changed

+139
-0
lines changed
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
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+
}

src/test/java/de/rub/nds/modifiablevariable/length/ModifiableLengthFieldTest.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -544,4 +544,38 @@ void testReferenceWithNoOriginalValue() {
544544
"Original value should still be array length");
545545
assertEquals(13, (int) lengthField.getValue(), "Value should include modification");
546546
}
547+
548+
/** Test edge case: comparing fields where one has modifications resulting in null */
549+
@Test
550+
public void testEqualsWithOneNullValue() {
551+
// Create two fields with the same reference
552+
ModifiableByteArray sharedArray = new ModifiableByteArray();
553+
sharedArray.setOriginalValue(new byte[] {1, 2, 3, 4});
554+
555+
ModifiableLengthField field1 = new ModifiableLengthField(sharedArray);
556+
ModifiableLengthField field2 = new ModifiableLengthField(sharedArray);
557+
558+
// Create a special modification that returns null
559+
class NullResultModification extends IntegerAddModification {
560+
public NullResultModification() {
561+
super(0);
562+
}
563+
564+
@Override
565+
protected Integer modifyImplementationHook(Integer input) {
566+
return null;
567+
}
568+
}
569+
570+
// Apply null modification to only one field
571+
field1.setModifications(new NullResultModification());
572+
573+
// field1.getValue() is null, field2.getValue() is 4
574+
assertNull(field1.getValue());
575+
assertEquals(4, (int) field2.getValue());
576+
577+
// They should not be equal
578+
assertNotEquals(field1, field2);
579+
assertNotEquals(field2, field1);
580+
}
547581
}

0 commit comments

Comments
 (0)