Skip to content

Commit 8d1a8d0

Browse files
committed
Implemented a toString method for ModifiableByteArray and made a new hexToString method which handles ModifiableByteArrays.
1 parent bca9930 commit 8d1a8d0

File tree

4 files changed

+89
-11
lines changed

4 files changed

+89
-11
lines changed

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

Lines changed: 8 additions & 0 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;
@@ -71,4 +72,11 @@ public boolean validateAssertions() {
7172
}
7273
return valid;
7374
}
75+
76+
@Override
77+
public String toString() {
78+
return ArrayConverter.bytesToHexString(this);
79+
}
80+
81+
7482
}

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

Lines changed: 17 additions & 2 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

@@ -56,7 +58,7 @@ public static byte[] longToUint32Bytes(long l) {
5658
* Takes an integer value and stores its last bytes into a byte array
5759
*
5860
* @param value integer value
59-
* @param size byte size of the new integer byte array
61+
* @param size byte size of the new integer byte array
6062
* @return
6163
*/
6264
public static byte[] intToBytes(int value, int size) {
@@ -77,7 +79,7 @@ public static byte[] intToBytes(int value, int size) {
7779
* Takes a long value and stores its last bytes into a byte array
7880
*
7981
* @param value long value
80-
* @param size byte size of the new integer byte array
82+
* @param size byte size of the new integer byte array
8183
* @return
8284
*/
8385
public static byte[] longToBytes(long value, int size) {
@@ -163,6 +165,19 @@ public static String bytesToHexString(byte[] array, boolean usePrettyPrinting, b
163165
return result.toString();
164166
}
165167

168+
public static String bytesToHexString(ModifiableByteArray array) {
169+
return bytesToHexString(array.getValue());
170+
}
171+
172+
public static String bytesToHexString(ModifiableByteArray array, boolean usePrettyPrinting) {
173+
174+
return bytesToHexString(array.getValue(), usePrettyPrinting, true);
175+
}
176+
177+
public static String bytesToHexString(ModifiableByteArray array, boolean usePrettyPrinting, boolean initialNewLine) {
178+
return bytesToHexString(array.getValue(), usePrettyPrinting, initialNewLine);
179+
}
180+
166181
@SafeVarargs
167182
public static <T> T[] concatenate(final T[]... arrays) {
168183
if (arrays == null || arrays.length == 0) {

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

Lines changed: 25 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;
@@ -430,4 +430,27 @@ public void testShuffle() {
430430
result = new byte[] { 6, 0, 3, 2, 5, 4, 1 };
431431
assertArrayEquals(result, start.getValue());
432432
}
433+
434+
@Test
435+
public void toStringTest() {
436+
ModifiableByteArray toTest = new ModifiableByteArray();
437+
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());
449+
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());
455+
}
433456
}

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

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
* http://www.apache.org/licenses/LICENSE-2.0
88
*/
99
package de.rub.nds.modifiablevariable.util;
10+
11+
import de.rub.nds.modifiablevariable.ModifiableVariableFactory;
12+
import de.rub.nds.modifiablevariable.bytearray.ModifiableByteArray;
1013
import java.math.BigInteger;
1114
import static org.junit.Assert.assertArrayEquals;
1215
import static org.junit.Assert.assertEquals;
@@ -58,13 +61,13 @@ public void testBytesToInt() {
5861
*/
5962
@Test
6063
public void testBytesToLong() {
61-
/** TODO get the casting correct.
62-
long result = 571714867398058L;
63-
byte[] toParse = {0x02, 0x07, (byte) 0xf8, (byte) 0xbd, (byte) 0x95, (byte) 0x85, (byte) 0xaa};
64-
byte[] test = ArrayConverter.longToBytes(result, 7);
65-
int a = 0;
66-
//assertEquals(result, ArrayConverter.bytesToLong(toParse));
67-
*/
64+
/**
65+
* TODO get the casting correct. long result = 571714867398058L; byte[]
66+
* toParse = {0x02, 0x07, (byte) 0xf8, (byte) 0xbd, (byte) 0x95, (byte)
67+
* 0x85, (byte) 0xaa}; byte[] test = ArrayConverter.longToBytes(result,
68+
* 7); int a = 0; //assertEquals(result,
69+
* ArrayConverter.bytesToLong(toParse));
70+
*/
6871

6972
}
7073

@@ -186,4 +189,33 @@ public void testLongToUint48Bytes() {
186189

187190
assertArrayEquals("Assert correct output", expectedResult, ArrayConverter.longToUint48Bytes(testValue));
188191
}
192+
193+
/**
194+
* Test of bytesToHexString method, of class ArrayConverter. Differences in
195+
* the overloaded methods should be visible in the other tests since the
196+
* methods just pass the .getValue().
197+
*/
198+
@Test
199+
public void testBytesToHexString_ModifiableByteArray() {
200+
ModifiableByteArray toTest = new ModifiableByteArray();
201+
toTest = ModifiableVariableFactory.safelySetValue(toTest, new byte[]{0x00, 0x11, 0x22, 0x33, 0x44});
202+
assertEquals("00 11 22 33 44", ArrayConverter.bytesToHexString(toTest));
203+
204+
toTest = ModifiableVariableFactory.safelySetValue(toTest, new byte[]{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08});
205+
assertEquals("00 01 02 03 04 05 06 07 08", ArrayConverter.bytesToHexString(toTest));
206+
207+
toTest = ModifiableVariableFactory.safelySetValue(toTest, new byte[]{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10});
208+
assertEquals("00 01 02 03 04 05 06 07 08 09 10", ArrayConverter.bytesToHexString(toTest));
209+
210+
toTest = ModifiableVariableFactory.safelySetValue(toTest, new byte[]{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
211+
0x07,});
212+
assertEquals("\n00 01 02 03 04 05 06 07 00 01 02 03 04 05 06 07", ArrayConverter.bytesToHexString(toTest));
213+
214+
toTest = ModifiableVariableFactory.safelySetValue(toTest, new byte[]{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
215+
0x07, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,});
216+
assertEquals(
217+
"\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",
218+
ArrayConverter.bytesToHexString(toTest));
219+
}
220+
189221
}

0 commit comments

Comments
 (0)