Skip to content

Commit 6327357

Browse files
authored
Merge pull request #9 from RUB-NDS/mvToHex
Implemented a toString method for ModifiableByteArray and made a new …
2 parents 46dc6e0 + 5fe7ff6 commit 6327357

File tree

5 files changed

+93
-10
lines changed

5 files changed

+93
-10
lines changed

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

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +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;
1314
import de.rub.nds.modifiablevariable.util.ByteArrayAdapter;
1415
import java.io.Serializable;
1516
import java.util.Arrays;
@@ -19,8 +20,9 @@
1920
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
2021

2122
/**
22-
*
23+
*
2324
* @author Juraj Somorovsky - juraj.somorovsky@rub.de
25+
* @author Matthias Terlinde - <matthias.terlinde@rub.de>
2426
*/
2527
@XmlRootElement
2628
@XmlSeeAlso({ ByteArrayDeleteModification.class, ByteArrayExplicitValueModification.class,
@@ -71,4 +73,21 @@ public boolean validateAssertions() {
7173
}
7274
return valid;
7375
}
74-
}
76+
77+
@Override
78+
public String toString() {
79+
StringBuilder result = new StringBuilder();
80+
if (this.isOriginalValueModified()) {
81+
result.append("Actual byte value is: ");
82+
result.append(ArrayConverter.bytesToHexString(this));
83+
result.append("\nOriginal value was: ");
84+
result.append(ArrayConverter.bytesToHexString(this.getOriginalValue()));
85+
} else {
86+
result.append("Original byte value is: ");
87+
result.append(ArrayConverter.bytesToHexString(this.getOriginalValue()));
88+
}
89+
return result.toString();
90+
91+
}
92+
93+
}

src/main/java/de/rub/nds/modifiablevariable/util/ArrayConverter.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,14 @@
88
*/
99
package de.rub.nds.modifiablevariable.util;
1010

11+
import de.rub.nds.modifiablevariable.bytearray.ModifiableByteArray;
1112
import java.lang.reflect.Array;
1213
import java.math.BigInteger;
1314
import java.util.List;
1415

1516
/**
1617
* @author Juraj Somorovsky <juraj.somorovsky@rub.de>
18+
* @author Matthias Terlinde <matthias.terlinde@rub.de>
1719
*/
1820
public class ArrayConverter {
1921

@@ -179,6 +181,19 @@ public static String bytesToRawHexString(byte[] array) {
179181
return result.toString();
180182
}
181183

184+
public static String bytesToHexString(ModifiableByteArray array) {
185+
return bytesToHexString(array.getValue());
186+
}
187+
188+
public static String bytesToHexString(ModifiableByteArray array, boolean usePrettyPrinting) {
189+
190+
return bytesToHexString(array.getValue(), usePrettyPrinting, true);
191+
}
192+
193+
public static String bytesToHexString(ModifiableByteArray array, boolean usePrettyPrinting, boolean initialNewLine) {
194+
return bytesToHexString(array.getValue(), usePrettyPrinting, initialNewLine);
195+
}
196+
182197
@SafeVarargs
183198
public static <T> T[] concatenate(final T[]... arrays) {
184199
if (arrays == null || arrays.length == 0) {

src/main/java/de/rub/nds/modifiablevariable/util/BadFixedRandom.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
import java.util.Random;
1212

1313
/**
14-
* A fake random number generator for testing.
15-
* This generator will always return the byte "retVal" passed to the constructor.
14+
* A fake random number generator for testing. This generator will always return
15+
* the byte "retVal" passed to the constructor.
1616
*
1717
*/
1818
public class BadFixedRandom extends Random {
@@ -24,9 +24,10 @@ public BadFixedRandom(byte retVal) {
2424
}
2525

2626
/**
27-
* Fills a user-supplied byte array with the fixed byte given at object initialization.
28-
* The number of "random" bytes produced is equal to the length of the byte array.
29-
*/
27+
* Fills a user-supplied byte array with the fixed byte given at object
28+
* initialization. The number of "random" bytes produced is equal to the
29+
* length of the byte array.
30+
*/
3031
@Override
3132
public void nextBytes(byte[] bytes) {
3233
for (int i = 0, len = bytes.length; i < len;)

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

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@
88
*/
99
package de.rub.nds.modifiablevariable.bytearray;
1010

11-
import de.rub.nds.modifiablevariable.bytearray.ModifiableByteArray;
12-
import de.rub.nds.modifiablevariable.bytearray.ByteArrayModificationFactory;
11+
import de.rub.nds.modifiablevariable.ModifiableVariableFactory;
1312
import de.rub.nds.modifiablevariable.VariableModification;
1413
import de.rub.nds.modifiablevariable.util.ArrayConverter;
1514
import org.apache.logging.log4j.LogManager;
1615
import org.apache.logging.log4j.Logger;
1716
import static org.junit.Assert.assertArrayEquals;
17+
import static org.junit.Assert.assertEquals;
1818
import static org.junit.Assert.assertFalse;
1919
import static org.junit.Assert.assertNotNull;
2020
import static org.junit.Assert.assertTrue;
@@ -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;
@@ -430,4 +431,17 @@ public void testShuffle() {
430431
result = new byte[] { 6, 0, 3, 2, 5, 4, 1 };
431432
assertArrayEquals(result, start.getValue());
432433
}
434+
435+
@Test
436+
public void toStringTest() {
437+
ModifiableByteArray toTest = new ModifiableByteArray();
438+
toTest = ModifiableVariableFactory.safelySetValue(toTest, new byte[] { 0x00, 0x11, 0x22, 0x33, 0x44 });
439+
assertEquals("Original byte value is: 00 11 22 33 44", toTest.toString());
440+
441+
VariableModification modificatoin = new ByteArrayExplicitValueModification(new byte[] { 0x00, 0x01, 0x02, 0x03,
442+
0x04, 0x05, 0x06, 0x07, 0x08 });
443+
toTest.setModification(modificatoin);
444+
assertEquals("Actual byte value is: 00 01 02 03 04 05 06 07 08\nOriginal value was: 00 11 22 33 44",
445+
toTest.toString());
446+
}
433447
}

src/test/java/de/rub/nds/modifiablevariable/util/ArrayConverterTest.java

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,16 @@
88
*/
99
package de.rub.nds.modifiablevariable.util;
1010

11+
import de.rub.nds.modifiablevariable.ModifiableVariableFactory;
12+
import de.rub.nds.modifiablevariable.bytearray.ModifiableByteArray;
1113
import java.math.BigInteger;
1214
import static org.junit.Assert.assertArrayEquals;
1315
import static org.junit.Assert.assertEquals;
1416
import org.junit.Test;
1517

1618
/**
1719
* @author Juraj Somorovsky - juraj.somorovsky@rub.de
18-
* @author Florian Pfützenreuter <Florian.Pfuetzenreuter@rub.de>
20+
* @author Florian Pfuetzenreuter <Florian.Pfuetzenreuter@rub.de>
1921
* @author Matthias Terlinde <matthias.terlinde@rub.de>
2022
*/
2123
public class ArrayConverterTest {
@@ -208,4 +210,36 @@ public void testLongToUint48Bytes() {
208210

209211
assertArrayEquals("Assert correct output", expectedResult, ArrayConverter.longToUint48Bytes(testValue));
210212
}
213+
214+
/**
215+
* Test of bytesToHexString method, of class ArrayConverter. Differences in
216+
* the overloaded methods should be visible in the other tests since the
217+
* methods just pass the .getValue().
218+
*/
219+
@Test
220+
public void testBytesToHexString_ModifiableByteArray() {
221+
ModifiableByteArray toTest = new ModifiableByteArray();
222+
toTest = ModifiableVariableFactory.safelySetValue(toTest, new byte[] { 0x00, 0x11, 0x22, 0x33, 0x44 });
223+
assertEquals("00 11 22 33 44", ArrayConverter.bytesToHexString(toTest));
224+
225+
toTest = ModifiableVariableFactory.safelySetValue(toTest, new byte[] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05,
226+
0x06, 0x07, 0x08 });
227+
assertEquals("00 01 02 03 04 05 06 07 08", ArrayConverter.bytesToHexString(toTest));
228+
229+
toTest = ModifiableVariableFactory.safelySetValue(toTest, new byte[] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05,
230+
0x06, 0x07, 0x08, 0x09, 0x10 });
231+
assertEquals("00 01 02 03 04 05 06 07 08 09 10", ArrayConverter.bytesToHexString(toTest));
232+
233+
toTest = ModifiableVariableFactory.safelySetValue(toTest, new byte[] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05,
234+
0x06, 0x07, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, });
235+
assertEquals("\n00 01 02 03 04 05 06 07 00 01 02 03 04 05 06 07", ArrayConverter.bytesToHexString(toTest));
236+
237+
toTest = ModifiableVariableFactory.safelySetValue(toTest, new byte[] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05,
238+
0x06, 0x07, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
239+
0x07, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, });
240+
assertEquals(
241+
"\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",
242+
ArrayConverter.bytesToHexString(toTest));
243+
}
244+
211245
}

0 commit comments

Comments
 (0)