Skip to content

Commit 4c604d8

Browse files
committed
The toString method of the mvByteArray now respects original values.
1 parent 8d1a8d0 commit 4c604d8

File tree

2 files changed

+42
-42
lines changed

2 files changed

+42
-42
lines changed

src/main/java/de/rub/nds/modifiablevariable/bytearray/ModifiableByteArray.java

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
import de.rub.nds.modifiablevariable.ModifiableVariable;
1212
import de.rub.nds.modifiablevariable.VariableModification;
13-
import de.rub.nds.modifiablevariable.util.ArrayConverter;
13+
import static de.rub.nds.modifiablevariable.util.ArrayConverter.bytesToHexString;
1414
import de.rub.nds.modifiablevariable.util.ByteArrayAdapter;
1515
import java.io.Serializable;
1616
import java.util.Arrays;
@@ -20,13 +20,14 @@
2020
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
2121

2222
/**
23-
*
23+
*
2424
* @author Juraj Somorovsky - juraj.somorovsky@rub.de
25+
* @author Matthias Terlinde - <matthias.terlinde@rub.de>
2526
*/
2627
@XmlRootElement
27-
@XmlSeeAlso({ ByteArrayDeleteModification.class, ByteArrayExplicitValueModification.class,
28-
ByteArrayInsertModification.class, ByteArrayXorModification.class, ByteArrayDuplicateModification.class })
29-
@XmlType(propOrder = { "originalValue", "modification", "assertEquals" })
28+
@XmlSeeAlso({ByteArrayDeleteModification.class, ByteArrayExplicitValueModification.class,
29+
ByteArrayInsertModification.class, ByteArrayXorModification.class, ByteArrayDuplicateModification.class})
30+
@XmlType(propOrder = {"originalValue", "modification", "assertEquals"})
3031
public class ModifiableByteArray extends ModifiableVariable<byte[]> implements Serializable {
3132

3233
private byte[] originalValue;
@@ -75,8 +76,18 @@ public boolean validateAssertions() {
7576

7677
@Override
7778
public String toString() {
78-
return ArrayConverter.bytesToHexString(this);
79+
StringBuilder result = new StringBuilder();
80+
if (this.isOriginalValueModified()) {
81+
result.append("Actual byte value is: ");
82+
result.append(bytesToHexString(this));
83+
result.append("\nOriginal value was: ");
84+
result.append(bytesToHexString(this.getOriginalValue()));
85+
} else {
86+
result.append("Original byte value is: ");
87+
result.append(bytesToHexString(this.getOriginalValue()));
88+
}
89+
return result.toString();
90+
7991
}
80-
81-
82-
}
92+
93+
}

src/test/java/de/rub/nds/modifiablevariable/bytearray/ModifiableByteArrayTest.java

Lines changed: 22 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
* @author Juraj Somorovsky <juraj.somorovsky@rub.de>
2727
*/
2828
public class ModifiableByteArrayTest {
29+
2930
private static final Logger LOGGER = LogManager.getLogger(ModifiableByteArray.class);
3031

3132
private ModifiableByteArray start;
@@ -38,10 +39,10 @@ public class ModifiableByteArrayTest {
3839

3940
@Before
4041
public void setUp() {
41-
originalValue = new byte[] { (byte) 0, (byte) 1, (byte) 2, (byte) 3, (byte) 4, (byte) 5, (byte) 6 };
42-
modification1 = new byte[] { (byte) 2, (byte) 3 };
43-
modification2 = new byte[] { (byte) 2, (byte) 1, (byte) 0, (byte) 1, (byte) 2, (byte) 3, (byte) 4, (byte) 5,
44-
(byte) 6 };
42+
originalValue = new byte[]{(byte) 0, (byte) 1, (byte) 2, (byte) 3, (byte) 4, (byte) 5, (byte) 6};
43+
modification1 = new byte[]{(byte) 2, (byte) 3};
44+
modification2 = new byte[]{(byte) 2, (byte) 1, (byte) 0, (byte) 1, (byte) 2, (byte) 3, (byte) 4, (byte) 5,
45+
(byte) 6};
4546
start = new ModifiableByteArray();
4647
start.setOriginalValue(originalValue);
4748
}
@@ -361,13 +362,13 @@ public void testInsertBytes() {
361362
@Test
362363
public void testIsOriginalValueModified() {
363364
assertFalse(start.isOriginalValueModified());
364-
VariableModification<byte[]> modifier = ByteArrayModificationFactory.xor(new byte[] {}, 0);
365+
VariableModification<byte[]> modifier = ByteArrayModificationFactory.xor(new byte[]{}, 0);
365366
start.setModification(modifier);
366367
assertFalse(start.isOriginalValueModified());
367-
modifier = ByteArrayModificationFactory.xor(new byte[] { 1 }, 0);
368+
modifier = ByteArrayModificationFactory.xor(new byte[]{1}, 0);
368369
start.setModification(modifier);
369370
assertTrue(start.isOriginalValueModified());
370-
modifier = ByteArrayModificationFactory.xor(new byte[] { 0, 0 }, originalValue.length - 2);
371+
modifier = ByteArrayModificationFactory.xor(new byte[]{0, 0}, originalValue.length - 2);
371372
start.setModification(modifier);
372373
assertFalse(start.isOriginalValueModified());
373374
}
@@ -398,13 +399,13 @@ public void testExplicitValueFromFile() {
398399

399400
modifier = ByteArrayModificationFactory.explicitValueFromFile(1);
400401
start.setModification(modifier);
401-
expectedResult = new byte[] { 00 };
402+
expectedResult = new byte[]{00};
402403
result = start.getValue();
403404
assertArrayEquals(expectedResult, result);
404405

405406
modifier = ByteArrayModificationFactory.explicitValueFromFile(17);
406407
start.setModification(modifier);
407-
expectedResult = new byte[] { (byte) 255 };
408+
expectedResult = new byte[]{(byte) 255};
408409
result = start.getValue();
409410
assertArrayEquals(expectedResult, result);
410411
}
@@ -415,42 +416,30 @@ public void testExplicitValueFromFile() {
415416
@Test
416417
public void testShuffle() {
417418
LOGGER.info("testShuffle");
418-
VariableModification<byte[]> modifier = ByteArrayModificationFactory.shuffle(new byte[] { 0, 1 });
419+
VariableModification<byte[]> modifier = ByteArrayModificationFactory.shuffle(new byte[]{0, 1});
419420
start.setModification(modifier);
420-
byte[] result = { 1, 0, 2, 3, 4, 5, 6 };
421+
byte[] result = {1, 0, 2, 3, 4, 5, 6};
421422
assertArrayEquals(result, start.getValue());
422423

423-
modifier = ByteArrayModificationFactory.shuffle(new byte[] { 0, 1, 2, 3, 4, 5, 6 });
424+
modifier = ByteArrayModificationFactory.shuffle(new byte[]{0, 1, 2, 3, 4, 5, 6});
424425
start.setModification(modifier);
425-
result = new byte[] { 1, 0, 3, 2, 5, 4, 6 };
426+
result = new byte[]{1, 0, 3, 2, 5, 4, 6};
426427
assertArrayEquals(result, start.getValue());
427428

428-
modifier = ByteArrayModificationFactory.shuffle(new byte[] { 0, 1, 2, 3, 4, 5, 6, 7 });
429+
modifier = ByteArrayModificationFactory.shuffle(new byte[]{0, 1, 2, 3, 4, 5, 6, 7});
429430
start.setModification(modifier);
430-
result = new byte[] { 6, 0, 3, 2, 5, 4, 1 };
431+
result = new byte[]{6, 0, 3, 2, 5, 4, 1};
431432
assertArrayEquals(result, start.getValue());
432433
}
433-
434+
434435
@Test
435436
public void toStringTest() {
436-
ModifiableByteArray toTest = new ModifiableByteArray();
437+
ModifiableByteArray toTest = new ModifiableByteArray();
437438
toTest = ModifiableVariableFactory.safelySetValue(toTest, new byte[]{0x00, 0x11, 0x22, 0x33, 0x44});
438-
assertEquals("00 11 22 33 44", toTest.toString());
439-
440-
toTest = ModifiableVariableFactory.safelySetValue(toTest, new byte[]{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08});
441-
assertEquals("00 01 02 03 04 05 06 07 08", toTest.toString());
442-
443-
toTest = ModifiableVariableFactory.safelySetValue(toTest, new byte[]{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10});
444-
assertEquals("00 01 02 03 04 05 06 07 08 09 10", toTest.toString());
445-
446-
toTest = ModifiableVariableFactory.safelySetValue(toTest, new byte[]{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
447-
0x07,});
448-
assertEquals("\n00 01 02 03 04 05 06 07 00 01 02 03 04 05 06 07", toTest.toString());
439+
assertEquals("Original byte value is: 00 11 22 33 44", toTest.toString());
449440

450-
toTest = ModifiableVariableFactory.safelySetValue(toTest, new byte[]{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
451-
0x07, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,});
452-
assertEquals(
453-
"\n00 01 02 03 04 05 06 07 00 01 02 03 04 05 06 07\n00 01 02 03 04 05 06 07 00 01 02 03 04 05 06 07",
454-
toTest.toString());
441+
VariableModification modificatoin = new ByteArrayExplicitValueModification(new byte[]{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08});
442+
toTest.setModification(modificatoin);
443+
assertEquals("Actual byte value is: 00 01 02 03 04 05 06 07 08\nOriginal value was: 00 11 22 33 44", toTest.toString());
455444
}
456445
}

0 commit comments

Comments
 (0)