Skip to content

Commit fa52b9f

Browse files
committed
Merge origin/master into mvToHex
Conflicts: src/main/java/de/rub/nds/modifiablevariable/util/ArrayConverter.java src/test/java/de/rub/nds/modifiablevariable/util/ArrayConverterTest.java
2 parents f4893f7 + 46dc6e0 commit fa52b9f

File tree

5 files changed

+122
-29
lines changed

5 files changed

+122
-29
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
@@ -57,8 +57,10 @@ public static byte[] longToUint32Bytes(long l) {
5757
/**
5858
* Takes an integer value and stores its last bytes into a byte array
5959
*
60-
* @param value integer value
61-
* @param size byte size of the new integer byte array
60+
* @param value
61+
* integer value
62+
* @param size
63+
* byte size of the new integer byte array
6264
* @return
6365
*/
6466
public static byte[] intToBytes(int value, int size) {
@@ -78,8 +80,10 @@ public static byte[] intToBytes(int value, int size) {
7880
/**
7981
* Takes a long value and stores its last bytes into a byte array
8082
*
81-
* @param value long value
82-
* @param size byte size of the new integer byte array
83+
* @param value
84+
* long value
85+
* @param size
86+
* byte size of the new integer byte array
8387
* @return
8488
*/
8589
public static byte[] longToBytes(long value, int size) {
@@ -149,16 +153,28 @@ public static String bytesToHexString(byte[] array, boolean usePrettyPrinting, b
149153
result.append("\n");
150154
}
151155
for (int i = 0; i < array.length; i++) {
152-
if (usePrettyPrinting && i != 0) {
153-
if (i % 16 == 0) {
156+
if (i != 0) {
157+
if (usePrettyPrinting && (i % 16 == 0)) {
154158
result.append("\n");
155-
} else if (i % 8 == 0) {
159+
} else {
160+
if (usePrettyPrinting && (i % 8 == 0)) {
161+
result.append(" ");
162+
}
156163
result.append(" ");
157164
}
158165
}
159-
if (i % 16 != 0) {
160-
result.append(" ");
161-
}
166+
byte b = array[i];
167+
result.append(String.format("%02X", b));
168+
}
169+
return result.toString();
170+
}
171+
172+
/**
173+
* Like bytesToHexString() without any formatting.
174+
*/
175+
public static String bytesToRawHexString(byte[] array) {
176+
StringBuilder result = new StringBuilder();
177+
for (int i = 0; i < array.length; i++) {
162178
byte b = array[i];
163179
result.append(String.format("%02X", b));
164180
}
@@ -240,8 +256,9 @@ public static void makeArrayNonZero(final byte[] array) {
240256
*
241257
* @param value
242258
* @param blockSize
243-
* @param removeSignByte in a case the removeSignByte is set, the sign byte
244-
* is removed (in case the byte array contains one)
259+
* @param removeSignByte
260+
* in a case the removeSignByte is set, the sign byte is removed
261+
* (in case the byte array contains one)
245262
* @return
246263
*/
247264
public static byte[] bigIntegerToByteArray(BigInteger value, int blockSize, boolean removeSignByte) {
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* TLS-Attacker - A Modular Penetration Testing Framework for TLS
3+
*
4+
* Copyright 2014-2016 Ruhr University Bochum / Hackmanit GmbH
5+
*
6+
* Licensed under Apache License 2.0
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*/
9+
package de.rub.nds.modifiablevariable.util;
10+
11+
import java.util.Random;
12+
13+
/**
14+
* A fake random number generator for testing.
15+
* This generator will always return the byte "retVal" passed to the constructor.
16+
*
17+
*/
18+
public class BadFixedRandom extends Random {
19+
20+
byte retVal;
21+
22+
public BadFixedRandom(byte retVal) {
23+
this.retVal = retVal;
24+
}
25+
26+
/**
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+
*/
30+
@Override
31+
public void nextBytes(byte[] bytes) {
32+
for (int i = 0, len = bytes.length; i < len;)
33+
bytes[i++] = retVal;
34+
}
35+
36+
}

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

Lines changed: 35 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -43,17 +43,18 @@ public void testLongToUint32Bytes() {
4343
public void testIntToBytes() {
4444
int toParse = 5717;
4545
byte[] result = ArrayConverter.intToBytes(toParse, 2);
46-
assertArrayEquals("The conversion result of 5717 should be {0x16} {0x55}", new byte[]{0x16, 0x55}, result);
46+
assertArrayEquals("The conversion result of 5717 should be {0x16} {0x55}", new byte[] { 0x16, 0x55 }, result);
4747
}
4848

4949
/**
5050
* Test of bytesToInt method, of class ArrayConverter.
5151
*/
5252
@Test
5353
public void testBytesToInt() {
54-
byte[] toParse = {0x16, 0x55};
54+
byte[] toParse = { 0x16, 0x55 };
5555
int expectedResult = 5717;
56-
assertEquals("The conversion result of {0x16, 0x55} should be 5717", expectedResult, ArrayConverter.bytesToInt(toParse));
56+
assertEquals("The conversion result of {0x16, 0x55} should be 5717", expectedResult,
57+
ArrayConverter.bytesToInt(toParse));
5758
}
5859

5960
/**
@@ -76,17 +77,17 @@ public void testBytesToLong() {
7677
*/
7778
@Test
7879
public void testBytesToHexString_byteArr() {
79-
byte[] toTest = new byte[]{0x00, 0x11, 0x22, 0x33, 0x44};
80+
byte[] toTest = new byte[] { 0x00, 0x11, 0x22, 0x33, 0x44 };
8081
assertEquals("00 11 22 33 44", ArrayConverter.bytesToHexString(toTest));
81-
toTest = new byte[]{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08};
82+
toTest = new byte[] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 };
8283
assertEquals("00 01 02 03 04 05 06 07 08", ArrayConverter.bytesToHexString(toTest));
83-
toTest = new byte[]{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10};
84+
toTest = new byte[] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10 };
8485
assertEquals("00 01 02 03 04 05 06 07 08 09 10", ArrayConverter.bytesToHexString(toTest));
85-
toTest = new byte[]{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
86-
0x07,};
86+
toTest = new byte[] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
87+
0x07, };
8788
assertEquals("\n00 01 02 03 04 05 06 07 00 01 02 03 04 05 06 07", ArrayConverter.bytesToHexString(toTest));
88-
toTest = new byte[]{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
89-
0x07, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,};
89+
toTest = new byte[] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
90+
0x07, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, };
9091
assertEquals(
9192
"\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",
9293
ArrayConverter.bytesToHexString(toTest));
@@ -97,6 +98,14 @@ public void testBytesToHexString_byteArr() {
9798
*/
9899
@Test
99100
public void testBytesToHexString_byteArr_boolean() {
101+
byte[] toTest = new byte[] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x00, 0x01, 0x02, 0x03, 0x04,
102+
0x05, 0x06, 0x07, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05,
103+
0x06, 0x07 };
104+
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",
105+
ArrayConverter.bytesToHexString(toTest, false));
106+
assertEquals(
107+
"\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",
108+
ArrayConverter.bytesToHexString(toTest, true));
100109
}
101110

102111
/**
@@ -106,6 +115,18 @@ public void testBytesToHexString_byteArr_boolean() {
106115
public void testBytesToHexString_3args() {
107116
}
108117

118+
/**
119+
* Test ArrayConverter.bytesToRawHexString().
120+
*/
121+
@Test
122+
public void testBytesToRawHexString() {
123+
byte[] toTest = new byte[] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x00, 0x01, 0x02, 0x03, 0x04,
124+
0x05, 0x06, 0x07, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05,
125+
0x06, 0x07 };
126+
assertEquals("0001020304050607000102030405060700010203040506070001020304050607",
127+
ArrayConverter.bytesToRawHexString(toTest));
128+
}
129+
109130
/**
110131
* Test of concatenate method, of class ArrayConverter.
111132
*/
@@ -154,13 +175,13 @@ public void testConvertListToArray() {
154175
@Test
155176
public void testHexStringToByteArray() {
156177
String hex = "01";
157-
assertArrayEquals("Testing simple one byte hex value", new byte[]{0x01},
178+
assertArrayEquals("Testing simple one byte hex value", new byte[] { 0x01 },
158179
ArrayConverter.hexStringToByteArray(hex));
159180
hex = "FF";
160-
assertArrayEquals("Testing one byte hex value > 0x7f", new byte[]{(byte) 0xff},
181+
assertArrayEquals("Testing one byte hex value > 0x7f", new byte[] { (byte) 0xff },
161182
ArrayConverter.hexStringToByteArray(hex));
162183
hex = "FFFFFF";
163-
assertArrayEquals("Testing one byte hex value > 0x7f", new byte[]{(byte) 0xff, (byte) 0xff, (byte) 0xff},
184+
assertArrayEquals("Testing one byte hex value > 0x7f", new byte[] { (byte) 0xff, (byte) 0xff, (byte) 0xff },
164185
ArrayConverter.hexStringToByteArray(hex));
165186
}
166187

@@ -170,7 +191,7 @@ public void testBigIntegerToNullPaddedByteArray() {
170191

171192
assertArrayEquals("Check zero output size", new byte[0],
172193
ArrayConverter.bigIntegerToNullPaddedByteArray(test, 0));
173-
assertArrayEquals("Check check output size smaller than input", new byte[]{(byte) 0xEC},
194+
assertArrayEquals("Check check output size smaller than input", new byte[] { (byte) 0xEC },
174195
ArrayConverter.bigIntegerToNullPaddedByteArray(test, 1));
175196
assertArrayEquals("Check output size bigger than input size",
176197
ArrayConverter.hexStringToByteArray("0000000000000000000000001D42C86F7923DFEC"),

0 commit comments

Comments
 (0)