Skip to content

Commit d6eba40

Browse files
authored
Merge pull request #6 from RUB-NDS/MoveTests
Move tests
2 parents 2c09d79 + 1e2a30e commit d6eba40

File tree

2 files changed

+200
-28
lines changed

2 files changed

+200
-28
lines changed

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

Lines changed: 11 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,8 @@ 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
59-
* integer value
60-
* @param size
61-
* byte size of the new integer byte array
58+
* @param value integer value
59+
* @param size byte size of the new integer byte array
6260
* @return
6361
*/
6462
public static byte[] intToBytes(int value, int size) {
@@ -78,10 +76,8 @@ public static byte[] intToBytes(int value, int size) {
7876
/**
7977
* Takes a long value and stores its last bytes into a byte array
8078
*
81-
* @param value
82-
* long value
83-
* @param size
84-
* byte size of the new integer byte array
79+
* @param value long value
80+
* @param size byte size of the new integer byte array
8581
* @return
8682
*/
8783
public static byte[] longToBytes(long value, int size) {
@@ -134,35 +130,23 @@ public static String bytesToHexString(byte[] array) {
134130
if (array == null) {
135131
array = new byte[0];
136132
}
137-
return bytesToHexString(array, array.length);
138-
}
139-
140-
public static String bytesToHexString(byte[] array, int byteSize) {
141-
boolean usePrettyPrinting = (byteSize > 15);
142-
return bytesToHexString(array, byteSize, usePrettyPrinting);
133+
boolean usePrettyPrinting = (array.length > 15);
134+
return bytesToHexString(array, usePrettyPrinting);
143135
}
144136

145137
public static String bytesToHexString(byte[] array, boolean usePrettyPrinting) {
146138
if (array == null) {
147139
array = new byte[0];
148140
}
149-
return bytesToHexString(array, array.length, usePrettyPrinting);
150-
}
151-
152-
public static String bytesToHexString(byte[] array, int byteSize, boolean usePrettyPrinting) {
153-
if (array == null) {
154-
array = new byte[0];
155-
}
156-
return bytesToHexString(array, array.length, usePrettyPrinting, true);
141+
return bytesToHexString(array, usePrettyPrinting, true);
157142
}
158143

159-
public static String bytesToHexString(byte[] array, int byteSize, boolean usePrettyPrinting, boolean initialNewLine) {
144+
public static String bytesToHexString(byte[] array, boolean usePrettyPrinting, boolean initialNewLine) {
160145
StringBuilder result = new StringBuilder();
161-
int bs = (byteSize < array.length) ? byteSize : array.length;
162146
if (initialNewLine && usePrettyPrinting) {
163147
result.append("\n");
164148
}
165-
for (int i = 0; i < bs; i++) {
149+
for (int i = 0; i < array.length; i++) {
166150
if (usePrettyPrinting && i != 0) {
167151
if (i % 16 == 0) {
168152
result.append("\n");
@@ -241,9 +225,8 @@ public static void makeArrayNonZero(final byte[] array) {
241225
*
242226
* @param value
243227
* @param blockSize
244-
* @param removeSignByte
245-
* in a case the removeSignByte is set, the sign byte is removed
246-
* (in case the byte array contains one)
228+
* @param removeSignByte in a case the removeSignByte is set, the sign byte
229+
* is removed (in case the byte array contains one)
247230
* @return
248231
*/
249232
public static byte[] bigIntegerToByteArray(BigInteger value, int blockSize, boolean removeSignByte) {
Lines changed: 189 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,189 @@
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+
import java.math.BigInteger;
11+
import static org.junit.Assert.assertArrayEquals;
12+
import static org.junit.Assert.assertEquals;
13+
import org.junit.Test;
14+
15+
/**
16+
* @author Juraj Somorovsky - juraj.somorovsky@rub.de
17+
* @author Florian Pfützenreuter <Florian.Pfuetzenreuter@rub.de>
18+
* @author Matthias Terlinde <matthias.terlinde@rub.de>
19+
*/
20+
public class ArrayConverterTest {
21+
22+
/**
23+
* Test of longToUint64Bytes method, of class ArrayConverter.
24+
*/
25+
@Test
26+
public void testLongToUint64Bytes() {
27+
}
28+
29+
/**
30+
* Test of longToUint32Bytes method, of class ArrayConverter.
31+
*/
32+
@Test
33+
public void testLongToUint32Bytes() {
34+
}
35+
36+
/**
37+
* Test of intToBytes method, of class ArrayConverter.
38+
*/
39+
@Test
40+
public void testIntToBytes() {
41+
int toParse = 5717;
42+
byte[] result = ArrayConverter.intToBytes(toParse, 2);
43+
assertArrayEquals("The conversion result of 5717 should be {0x16} {0x55}", new byte[]{0x16, 0x55}, result);
44+
}
45+
46+
/**
47+
* Test of bytesToInt method, of class ArrayConverter.
48+
*/
49+
@Test
50+
public void testBytesToInt() {
51+
byte[] toParse = {0x16, 0x55};
52+
int expectedResult = 5717;
53+
assertEquals("The conversion result of {0x16, 0x55} should be 5717", expectedResult, ArrayConverter.bytesToInt(toParse));
54+
}
55+
56+
/**
57+
* Test of bytesToLong method, of class ArrayConverter.
58+
*/
59+
@Test
60+
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+
*/
68+
69+
}
70+
71+
/**
72+
* Test of bytesToHexString method, of class ArrayConverter.
73+
*/
74+
@Test
75+
public void testBytesToHexString_byteArr() {
76+
byte[] toTest = new byte[]{0x00, 0x11, 0x22, 0x33, 0x44};
77+
assertEquals("00 11 22 33 44", ArrayConverter.bytesToHexString(toTest));
78+
toTest = new byte[]{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08};
79+
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};
81+
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+
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+
assertEquals(
88+
"\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",
89+
ArrayConverter.bytesToHexString(toTest));
90+
}
91+
92+
/**
93+
* Test of bytesToHexString method, of class ArrayConverter.
94+
*/
95+
@Test
96+
public void testBytesToHexString_byteArr_boolean() {
97+
}
98+
99+
/**
100+
* Test of bytesToHexString method, of class ArrayConverter.
101+
*/
102+
@Test
103+
public void testBytesToHexString_3args() {
104+
}
105+
106+
/**
107+
* Test of concatenate method, of class ArrayConverter.
108+
*/
109+
@Test
110+
public void testConcatenate_GenericType() {
111+
}
112+
113+
/**
114+
* Test of concatenate method, of class ArrayConverter.
115+
*/
116+
@Test
117+
public void testConcatenate_byteArrArr() {
118+
}
119+
120+
/**
121+
* Test of makeArrayNonZero method, of class ArrayConverter.
122+
*/
123+
@Test
124+
public void testMakeArrayNonZero() {
125+
}
126+
127+
/**
128+
* Test of bigIntegerToByteArray method, of class ArrayConverter.
129+
*/
130+
@Test
131+
public void testBigIntegerToByteArray_3args() {
132+
}
133+
134+
/**
135+
* Test of bigIntegerToByteArray method, of class ArrayConverter.
136+
*/
137+
@Test
138+
public void testBigIntegerToByteArray_BigInteger() {
139+
}
140+
141+
/**
142+
* Test of convertListToArray method, of class ArrayConverter.
143+
*/
144+
@Test
145+
public void testConvertListToArray() {
146+
}
147+
148+
/**
149+
* Test of hexStringToByteArray method, of class ArrayConverter.
150+
*/
151+
@Test
152+
public void testHexStringToByteArray() {
153+
String hex = "01";
154+
assertArrayEquals("Testing simple one byte hex value", new byte[]{0x01},
155+
ArrayConverter.hexStringToByteArray(hex));
156+
hex = "FF";
157+
assertArrayEquals("Testing one byte hex value > 0x7f", new byte[]{(byte) 0xff},
158+
ArrayConverter.hexStringToByteArray(hex));
159+
hex = "FFFFFF";
160+
assertArrayEquals("Testing one byte hex value > 0x7f", new byte[]{(byte) 0xff, (byte) 0xff, (byte) 0xff},
161+
ArrayConverter.hexStringToByteArray(hex));
162+
}
163+
164+
@Test
165+
public void testBigIntegerToNullPaddedByteArray() {
166+
BigInteger test = new BigInteger("1D42C86F7923DFEC", 16);
167+
168+
assertArrayEquals("Check zero output size", new byte[0],
169+
ArrayConverter.bigIntegerToNullPaddedByteArray(test, 0));
170+
assertArrayEquals("Check check output size smaller than input", new byte[]{(byte) 0xEC},
171+
ArrayConverter.bigIntegerToNullPaddedByteArray(test, 1));
172+
assertArrayEquals("Check output size bigger than input size",
173+
ArrayConverter.hexStringToByteArray("0000000000000000000000001D42C86F7923DFEC"),
174+
ArrayConverter.bigIntegerToNullPaddedByteArray(test, 20));
175+
}
176+
177+
@Test
178+
public void testLongToUint48Bytes() {
179+
long testValue = 0x0000123456789ABCL;
180+
byte[] expectedResult = ArrayConverter.hexStringToByteArray("123456789ABC");
181+
182+
assertArrayEquals("Assert correct output", expectedResult, ArrayConverter.longToUint48Bytes(testValue));
183+
184+
testValue = 0x0000000000000001L;
185+
expectedResult = ArrayConverter.hexStringToByteArray("000000000001");
186+
187+
assertArrayEquals("Assert correct output", expectedResult, ArrayConverter.longToUint48Bytes(testValue));
188+
}
189+
}

0 commit comments

Comments
 (0)