Skip to content

Commit 2819933

Browse files
authored
Merge pull request #11 from RUB-NDS/issue7
Issue7
2 parents bca9930 + f953346 commit 2819933

File tree

4 files changed

+95
-37
lines changed

4 files changed

+95
-37
lines changed

pom.xml

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,27 @@
6868
<version>2.8.2</version>
6969
</dependency>
7070
</dependencies>
71-
71+
<build>
72+
<plugins>
73+
<plugin>
74+
<groupId>net.revelc.code</groupId>
75+
<artifactId>formatter-maven-plugin</artifactId>
76+
<version>0.5.2</version>
77+
<configuration>
78+
<configFile>maven-eclipse-codestyle.xml</configFile>
79+
<lineEnding>LF</lineEnding>
80+
</configuration>
81+
<executions>
82+
<execution>
83+
<phase>process-sources</phase>
84+
<goals>
85+
<goal>format</goal>
86+
</goals>
87+
</execution>
88+
</executions>
89+
</plugin>
90+
</plugins>
91+
</build>
7292
<properties>
7393
<maven.compiler.source>1.7</maven.compiler.source>
7494
<maven.compiler.target>1.7</maven.compiler.target>

src/main/java/de/rub/nds/modifiablevariable/length/ModifiableLengthField.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@
2020

2121
/**
2222
*
23-
* @author Robert Merget - robert.merget@rub.de
24-
* Highly Experimental
23+
* @author Robert Merget - robert.merget@rub.de Highly Experimental
2524
*/
2625
@XmlRootElement
2726
@XmlSeeAlso({ IntegerAddModification.class, IntegerExplicitValueModification.class, IntegerSubtractModification.class,

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

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,10 @@ public static byte[] longToUint32Bytes(long l) {
5555
/**
5656
* Takes an integer value and stores its last bytes into a byte array
5757
*
58-
* @param value integer value
59-
* @param size byte size of the new integer byte array
58+
* @param value
59+
* integer value
60+
* @param size
61+
* byte size of the new integer byte array
6062
* @return
6163
*/
6264
public static byte[] intToBytes(int value, int size) {
@@ -76,8 +78,10 @@ public static byte[] intToBytes(int value, int size) {
7678
/**
7779
* Takes a long value and stores its last bytes into a byte array
7880
*
79-
* @param value long value
80-
* @param size byte size of the new integer byte array
81+
* @param value
82+
* long value
83+
* @param size
84+
* byte size of the new integer byte array
8185
* @return
8286
*/
8387
public static byte[] longToBytes(long value, int size) {
@@ -147,16 +151,28 @@ public static String bytesToHexString(byte[] array, boolean usePrettyPrinting, b
147151
result.append("\n");
148152
}
149153
for (int i = 0; i < array.length; i++) {
150-
if (usePrettyPrinting && i != 0) {
151-
if (i % 16 == 0) {
154+
if (i != 0) {
155+
if (usePrettyPrinting && (i % 16 == 0)) {
152156
result.append("\n");
153-
} else if (i % 8 == 0) {
157+
} else {
158+
if (usePrettyPrinting && (i % 8 == 0)) {
159+
result.append(" ");
160+
}
154161
result.append(" ");
155162
}
156163
}
157-
if (i % 16 != 0) {
158-
result.append(" ");
159-
}
164+
byte b = array[i];
165+
result.append(String.format("%02X", b));
166+
}
167+
return result.toString();
168+
}
169+
170+
/**
171+
* Like bytesToHexString() without any formatting.
172+
*/
173+
public static String bytesToRawHexString(byte[] array) {
174+
StringBuilder result = new StringBuilder();
175+
for (int i = 0; i < array.length; i++) {
160176
byte b = array[i];
161177
result.append(String.format("%02X", b));
162178
}
@@ -225,8 +241,9 @@ public static void makeArrayNonZero(final byte[] array) {
225241
*
226242
* @param value
227243
* @param blockSize
228-
* @param removeSignByte in a case the removeSignByte is set, the sign byte
229-
* is removed (in case the byte array contains one)
244+
* @param removeSignByte
245+
* in a case the removeSignByte is set, the sign byte is removed
246+
* (in case the byte array contains one)
230247
* @return
231248
*/
232249
public static byte[] bigIntegerToByteArray(BigInteger value, int blockSize, boolean removeSignByte) {

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

Lines changed: 44 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,15 @@
77
* http://www.apache.org/licenses/LICENSE-2.0
88
*/
99
package de.rub.nds.modifiablevariable.util;
10+
1011
import java.math.BigInteger;
1112
import static org.junit.Assert.assertArrayEquals;
1213
import static org.junit.Assert.assertEquals;
1314
import org.junit.Test;
1415

1516
/**
1617
* @author Juraj Somorovsky - juraj.somorovsky@rub.de
17-
* @author Florian Pfützenreuter <Florian.Pfuetzenreuter@rub.de>
18+
* @author Florian Pfützenreuter <Florian.Pfuetzenreuter@rub.de>
1819
* @author Matthias Terlinde <matthias.terlinde@rub.de>
1920
*/
2021
public class ArrayConverterTest {
@@ -40,31 +41,32 @@ public void testLongToUint32Bytes() {
4041
public void testIntToBytes() {
4142
int toParse = 5717;
4243
byte[] result = ArrayConverter.intToBytes(toParse, 2);
43-
assertArrayEquals("The conversion result of 5717 should be {0x16} {0x55}", new byte[]{0x16, 0x55}, result);
44+
assertArrayEquals("The conversion result of 5717 should be {0x16} {0x55}", new byte[] { 0x16, 0x55 }, result);
4445
}
4546

4647
/**
4748
* Test of bytesToInt method, of class ArrayConverter.
4849
*/
4950
@Test
5051
public void testBytesToInt() {
51-
byte[] toParse = {0x16, 0x55};
52+
byte[] toParse = { 0x16, 0x55 };
5253
int expectedResult = 5717;
53-
assertEquals("The conversion result of {0x16, 0x55} should be 5717", expectedResult, ArrayConverter.bytesToInt(toParse));
54+
assertEquals("The conversion result of {0x16, 0x55} should be 5717", expectedResult,
55+
ArrayConverter.bytesToInt(toParse));
5456
}
5557

5658
/**
5759
* Test of bytesToLong method, of class ArrayConverter.
5860
*/
5961
@Test
6062
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-
*/
63+
/**
64+
* TODO get the casting correct. long result = 571714867398058L; byte[]
65+
* toParse = {0x02, 0x07, (byte) 0xf8, (byte) 0xbd, (byte) 0x95, (byte)
66+
* 0x85, (byte) 0xaa}; byte[] test = ArrayConverter.longToBytes(result,
67+
* 7); int a = 0; //assertEquals(result,
68+
* ArrayConverter.bytesToLong(toParse));
69+
*/
6870

6971
}
7072

@@ -73,17 +75,17 @@ public void testBytesToLong() {
7375
*/
7476
@Test
7577
public void testBytesToHexString_byteArr() {
76-
byte[] toTest = new byte[]{0x00, 0x11, 0x22, 0x33, 0x44};
78+
byte[] toTest = new byte[] { 0x00, 0x11, 0x22, 0x33, 0x44 };
7779
assertEquals("00 11 22 33 44", ArrayConverter.bytesToHexString(toTest));
78-
toTest = new byte[]{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08};
80+
toTest = new byte[] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 };
7981
assertEquals("00 01 02 03 04 05 06 07 08", ArrayConverter.bytesToHexString(toTest));
80-
toTest = new byte[]{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10};
82+
toTest = new byte[] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10 };
8183
assertEquals("00 01 02 03 04 05 06 07 08 09 10", ArrayConverter.bytesToHexString(toTest));
82-
toTest = new byte[]{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
83-
0x07,};
84+
toTest = new byte[] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
85+
0x07, };
8486
assertEquals("\n00 01 02 03 04 05 06 07 00 01 02 03 04 05 06 07", ArrayConverter.bytesToHexString(toTest));
85-
toTest = new byte[]{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
86-
0x07, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,};
87+
toTest = new byte[] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
88+
0x07, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, };
8789
assertEquals(
8890
"\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",
8991
ArrayConverter.bytesToHexString(toTest));
@@ -94,6 +96,14 @@ public void testBytesToHexString_byteArr() {
9496
*/
9597
@Test
9698
public void testBytesToHexString_byteArr_boolean() {
99+
byte[] toTest = new byte[] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x00, 0x01, 0x02, 0x03, 0x04,
100+
0x05, 0x06, 0x07, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05,
101+
0x06, 0x07 };
102+
assertEquals("00 01 02 03 04 05 06 07 00 01 02 03 04 05 06 07 00 01 02 03 04 05 06 07 00 01 02 03 04 05 06 07",
103+
ArrayConverter.bytesToHexString(toTest, false));
104+
assertEquals(
105+
"\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",
106+
ArrayConverter.bytesToHexString(toTest, true));
97107
}
98108

99109
/**
@@ -103,6 +113,18 @@ public void testBytesToHexString_byteArr_boolean() {
103113
public void testBytesToHexString_3args() {
104114
}
105115

116+
/**
117+
* Test ArrayConverter.bytesToRawHexString().
118+
*/
119+
@Test
120+
public void testBytesToRawHexString() {
121+
byte[] toTest = new byte[] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x00, 0x01, 0x02, 0x03, 0x04,
122+
0x05, 0x06, 0x07, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05,
123+
0x06, 0x07 };
124+
assertEquals("0001020304050607000102030405060700010203040506070001020304050607",
125+
ArrayConverter.bytesToRawHexString(toTest));
126+
}
127+
106128
/**
107129
* Test of concatenate method, of class ArrayConverter.
108130
*/
@@ -151,13 +173,13 @@ public void testConvertListToArray() {
151173
@Test
152174
public void testHexStringToByteArray() {
153175
String hex = "01";
154-
assertArrayEquals("Testing simple one byte hex value", new byte[]{0x01},
176+
assertArrayEquals("Testing simple one byte hex value", new byte[] { 0x01 },
155177
ArrayConverter.hexStringToByteArray(hex));
156178
hex = "FF";
157-
assertArrayEquals("Testing one byte hex value > 0x7f", new byte[]{(byte) 0xff},
179+
assertArrayEquals("Testing one byte hex value > 0x7f", new byte[] { (byte) 0xff },
158180
ArrayConverter.hexStringToByteArray(hex));
159181
hex = "FFFFFF";
160-
assertArrayEquals("Testing one byte hex value > 0x7f", new byte[]{(byte) 0xff, (byte) 0xff, (byte) 0xff},
182+
assertArrayEquals("Testing one byte hex value > 0x7f", new byte[] { (byte) 0xff, (byte) 0xff, (byte) 0xff },
161183
ArrayConverter.hexStringToByteArray(hex));
162184
}
163185

@@ -167,7 +189,7 @@ public void testBigIntegerToNullPaddedByteArray() {
167189

168190
assertArrayEquals("Check zero output size", new byte[0],
169191
ArrayConverter.bigIntegerToNullPaddedByteArray(test, 0));
170-
assertArrayEquals("Check check output size smaller than input", new byte[]{(byte) 0xEC},
192+
assertArrayEquals("Check check output size smaller than input", new byte[] { (byte) 0xEC },
171193
ArrayConverter.bigIntegerToNullPaddedByteArray(test, 1));
172194
assertArrayEquals("Check output size bigger than input size",
173195
ArrayConverter.hexStringToByteArray("0000000000000000000000001D42C86F7923DFEC"),

0 commit comments

Comments
 (0)