Skip to content

Commit a5c27a4

Browse files
committed
Cleanup array comparisons in tests
- see #2012
1 parent 7dadecd commit a5c27a4

File tree

7 files changed

+36
-194
lines changed

7 files changed

+36
-194
lines changed

prov/src/test/java/org/bouncycastle/jce/provider/test/DESedeTest.java

Lines changed: 11 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import javax.crypto.spec.SecretKeySpec;
2020

2121
import org.bouncycastle.jce.provider.BouncyCastleProvider;
22+
import org.bouncycastle.util.Arrays;
2223
import org.bouncycastle.util.encoders.Hex;
2324
import org.bouncycastle.util.test.SimpleTest;
2425

@@ -79,50 +80,11 @@ public String getName()
7980
return "DESEDE";
8081
}
8182

82-
private boolean equalArray(
83-
byte[] a,
84-
byte[] b)
83+
private static boolean equalPrefix(byte[] a, byte[] b, int length)
8584
{
86-
if (a.length != b.length)
87-
{
88-
return false;
89-
}
90-
91-
for (int i = 0; i != a.length; i++)
92-
{
93-
if (a[i] != b[i])
94-
{
95-
return false;
96-
}
97-
}
98-
99-
return true;
100-
}
101-
102-
private boolean equalArray(
103-
byte[] a,
104-
byte[] b,
105-
int length)
106-
{
107-
if (a.length < length)
108-
{
109-
return false;
110-
}
111-
112-
if (b.length < length)
113-
{
114-
return false;
115-
}
116-
117-
for (int i = 0; i != length; i++)
118-
{
119-
if (a[i] != b[i])
120-
{
121-
return false;
122-
}
123-
}
124-
125-
return true;
85+
return a.length >= length
86+
&& b.length >= length
87+
&& Arrays.areEqual(a, 0, length, b, 0, length);
12688
}
12789

12890
private void wrapTest(
@@ -142,7 +104,7 @@ private void wrapTest(
142104
try
143105
{
144106
byte[] cText = wrapper.wrap(new SecretKeySpec(in, alg));
145-
if (!equalArray(cText, out))
107+
if (!Arrays.areEqual(cText, out))
146108
{
147109
fail("failed wrap test " + id + " expected " + new String(Hex.encode(out)) + " got " + new String(Hex.encode(cText)));
148110
}
@@ -157,7 +119,7 @@ private void wrapTest(
157119
try
158120
{
159121
Key pText = wrapper.unwrap(out, alg, Cipher.SECRET_KEY);
160-
if (!equalArray(pText.getEncoded(), in))
122+
if (!Arrays.areEqual(pText.getEncoded(), in))
161123
{
162124
fail("failed unwrap test " + id + " expected " + new String(Hex.encode(in)) + " got " + new String(Hex.encode(pText.getEncoded())));
163125
}
@@ -242,7 +204,7 @@ public void test(
242204

243205
bytes = bOut.toByteArray();
244206

245-
if (!equalArray(bytes, output))
207+
if (!Arrays.areEqual(bytes, output))
246208
{
247209
fail(alg + " failed encryption - expected " + new String(Hex.encode(output)) + " got " + new String(Hex.encode(bytes)));
248210
}
@@ -265,13 +227,14 @@ public void test(
265227
bytes[i] = (byte)dIn.read();
266228
}
267229
dIn.readFully(bytes, input.length / 2, bytes.length - input.length / 2);
230+
dIn.close();
268231
}
269232
catch (Exception e)
270233
{
271234
fail(alg + " failed encryption - " + e.toString());
272235
}
273236

274-
if (!equalArray(bytes, input))
237+
if (!Arrays.areEqual(bytes, input))
275238
{
276239
fail(alg + " failed decryption - expected " + new String(Hex.encode(input)) + " got " + new String(Hex.encode(bytes)));
277240
}
@@ -284,7 +247,7 @@ public void test(
284247
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(alg, "BC");
285248
DESedeKeySpec keySpec = (DESedeKeySpec)keyFactory.getKeySpec((SecretKey)key, DESedeKeySpec.class);
286249

287-
if (!equalArray(key.getEncoded(), keySpec.getKey(), 16))
250+
if (!equalPrefix(key.getEncoded(), keySpec.getKey(), 16))
288251
{
289252
fail(alg + " KeySpec does not match key.");
290253
}

prov/src/test/java/org/bouncycastle/jce/provider/test/FIPSDESTest.java

Lines changed: 4 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import javax.crypto.spec.SecretKeySpec;
1717

1818
import org.bouncycastle.jce.provider.BouncyCastleProvider;
19+
import org.bouncycastle.util.Arrays;
1920
import org.bouncycastle.util.encoders.Hex;
2021
import org.bouncycastle.util.test.SimpleTestResult;
2122
import org.bouncycastle.util.test.Test;
@@ -54,26 +55,6 @@ public String getName()
5455
return "FIPSDESTest";
5556
}
5657

57-
private boolean equalArray(
58-
byte[] a,
59-
byte[] b)
60-
{
61-
if (a.length != b.length)
62-
{
63-
return false;
64-
}
65-
66-
for (int i = 0; i != a.length; i++)
67-
{
68-
if (a[i] != b[i])
69-
{
70-
return false;
71-
}
72-
}
73-
74-
return true;
75-
}
76-
7758
public TestResult test(
7859
String algorithm,
7960
byte[] input,
@@ -89,8 +70,6 @@ public TestResult test(
8970

9071
try
9172
{
92-
String baseAlgorithm;
93-
9473
key = new SecretKeySpec(Hex.decode("0123456789abcdef"), "DES");
9574

9675
in = Cipher.getInstance(algorithm, "BC");
@@ -151,7 +130,7 @@ public TestResult test(
151130

152131
bytes = bOut.toByteArray();
153132

154-
if (!equalArray(bytes, output))
133+
if (!Arrays.areEqual(bytes, output))
155134
{
156135
return new SimpleTestResult(false, getName() + ": " + algorithm + " failed encryption - expected " + new String(Hex.encode(output)) + " got " + new String(Hex.encode(bytes)));
157136
}
@@ -174,13 +153,14 @@ public TestResult test(
174153
bytes[i] = (byte)dIn.read();
175154
}
176155
dIn.readFully(bytes, input.length / 2, bytes.length - input.length / 2);
156+
dIn.close();
177157
}
178158
catch (Exception e)
179159
{
180160
return new SimpleTestResult(false, getName() + ": " + algorithm + " failed encryption - " + e.toString());
181161
}
182162

183-
if (!equalArray(bytes, input))
163+
if (!Arrays.areEqual(bytes, input))
184164
{
185165
return new SimpleTestResult(false, getName() + ": " + algorithm + " failed decryption - expected " + new String(Hex.encode(input)) + " got " + new String(Hex.encode(bytes)));
186166
}

prov/src/test/java/org/bouncycastle/jce/provider/test/PSSTest.java

Lines changed: 4 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -47,27 +47,6 @@ public void nextBytes(
4747
}
4848
}
4949

50-
private boolean arrayEquals(
51-
byte[] a,
52-
byte[] b)
53-
{
54-
if (a.length != b.length)
55-
{
56-
return false;
57-
}
58-
59-
for (int i = 0; i != a.length; i++)
60-
{
61-
if (a[i] != b[i])
62-
{
63-
return false;
64-
}
65-
}
66-
67-
return true;
68-
}
69-
70-
7150
private RSAPublicKeySpec pubKeySpec = new RSAPublicKeySpec(
7251
new BigInteger("a56e4a0e701017589a5187dc7ea841d156f2ec0e36ad52a44dfeb1e61f7ad991d8c51056ffedb162b4c0f283a12a88a394dff526ab7291cbb307ceabfce0b1dfd5cd9508096d5b2b8b6df5d671ef6377c0921cb23c270a70e2598e6ff89d19f105acc2d3f0cb35f29280e1386b6f64c4ef22e1e1f20d0ce8cffb2249bd9a2137",16),
7352
new BigInteger("010001",16));
@@ -109,7 +88,7 @@ public void performTest() throws Exception
10988
s.update(msg1a);
11089
byte[] sig = s.sign();
11190

112-
if (!arrayEquals(sig1a, sig))
91+
if (!Arrays.areEqual(sig1a, sig))
11392
{
11493
fail("PSS Sign test expected " + new String(Hex.encode(sig1a)) + " got " + new String(Hex.encode(sig)));
11594
}
@@ -135,7 +114,7 @@ public void performTest() throws Exception
135114
}
136115

137116
AlgorithmParameters pss = s.getParameters();
138-
if (!arrayEquals(pss.getEncoded(), new byte[] { 0x30, 0x00 }))
117+
if (!Arrays.areEqual(pss.getEncoded(), new byte[]{ 0x30, 0x00 }))
139118
{
140119
fail("failed default encoding test.");
141120
}
@@ -148,7 +127,7 @@ public void performTest() throws Exception
148127

149128
pss = s.getParameters();
150129

151-
if (!arrayEquals(sig1b, sig))
130+
if (!Arrays.areEqual(sig1b, sig))
152131
{
153132
fail("PSS Sign test expected " + new String(Hex.encode(sig1b)) + " got " + new String(Hex.encode(sig)));
154133
}
@@ -251,7 +230,7 @@ public void performTest() throws Exception
251230

252231
pss = s.getParameters();
253232

254-
if (!arrayEquals(sig1c, sig))
233+
if (!Arrays.areEqual(sig1c, sig))
255234
{
256235
fail("PSS Sign test expected " + new String(Hex.encode(sig1c)) + " got " + new String(Hex.encode(sig)));
257236
}

prov/src/test/jdk1.1/org/bouncycastle/jce/provider/test/RSATest.java

Lines changed: 7 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import javax.crypto.Cipher;
1313

1414
import org.bouncycastle.jce.provider.BouncyCastleProvider;
15+
import org.bouncycastle.util.Arrays;
1516
import org.bouncycastle.util.encoders.Hex;
1617
import org.bouncycastle.util.test.SimpleTestResult;
1718
import org.bouncycastle.util.test.Test;
@@ -50,27 +51,6 @@ public void nextBytes(
5051
}
5152
}
5253

53-
private boolean arrayEquals(
54-
byte[] a,
55-
byte[] b)
56-
{
57-
if (a.length != b.length)
58-
{
59-
return false;
60-
}
61-
62-
for (int i = 0; i != a.length; i++)
63-
{
64-
if (a[i] != b[i])
65-
{
66-
return false;
67-
}
68-
}
69-
70-
return true;
71-
}
72-
73-
7454
private RSAPublicKeySpec pubKeySpec = new RSAPublicKeySpec(
7555
new BigInteger("b4a7e46170574f16a97082b22be58b6a2a629798419be12872a4bdba626cfae9900f76abfb12139dce5de56564fab2b6543165a040c606887420e33d91ed7ed7", 16),
7656
new BigInteger("11", 16));
@@ -115,7 +95,7 @@ public TestResult perform()
11595

11696
byte[] out = c.doFinal(input);
11797

118-
if (!arrayEquals(out, output[0]))
98+
if (!Arrays.areEqual(out, output[0]))
11999
{
120100
return new SimpleTestResult(false, "NoPadding test failed on encrypt expected " + new String(Hex.encode(output[0])) + " got " + new String(Hex.encode(out)));
121101
}
@@ -124,7 +104,7 @@ public TestResult perform()
124104

125105
out = c.doFinal(out);
126106

127-
if (!arrayEquals(out, input))
107+
if (!Arrays.areEqual(out, input))
128108
{
129109
return new SimpleTestResult(false, "NoPadding test failed on decrypt expected " + new String(Hex.encode(input)) + " got " + new String(Hex.encode(out)));
130110
}
@@ -138,7 +118,7 @@ public TestResult perform()
138118

139119
out = c.doFinal(input);
140120

141-
if (!arrayEquals(out, output[1]))
121+
if (!Arrays.areEqual(out, output[1]))
142122
{
143123
return new SimpleTestResult(false, "PKCS1 test failed on encrypt expected " + new String(Hex.encode(output[1])) + " got " + new String(Hex.encode(out)));
144124
}
@@ -147,7 +127,7 @@ public TestResult perform()
147127

148128
out = c.doFinal(out);
149129

150-
if (!arrayEquals(out, input))
130+
if (!Arrays.areEqual(out, input))
151131
{
152132
return new SimpleTestResult(false, "PKCS1 test failed on decrypt expected " + new String(Hex.encode(input)) + " got " + new String(Hex.encode(out)));
153133
}
@@ -161,7 +141,7 @@ public TestResult perform()
161141

162142
out = c.doFinal(input);
163143

164-
if (!arrayEquals(out, output[2]))
144+
if (!Arrays.areEqual(out, output[2]))
165145
{
166146
return new SimpleTestResult(false, "OAEP test failed on encrypt expected " + new String(Hex.encode(output[2])) + " got " + new String(Hex.encode(out)));
167147
}
@@ -170,7 +150,7 @@ public TestResult perform()
170150

171151
out = c.doFinal(out);
172152

173-
if (!arrayEquals(out, input))
153+
if (!Arrays.areEqual(out, input))
174154
{
175155
return new SimpleTestResult(false, "OAEP test failed on decrypt expected " + new String(Hex.encode(input)) + " got " + new String(Hex.encode(out)));
176156
}

prov/src/test/jdk1.3/org/bouncycastle/jce/provider/test/PSSTest.java

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import java.security.spec.RSAPublicKeySpec;
1212

1313
import org.bouncycastle.jce.provider.BouncyCastleProvider;
14+
import org.bouncycastle.util.Arrays;
1415
import org.bouncycastle.util.encoders.Hex;
1516
import org.bouncycastle.util.test.SimpleTestResult;
1617
import org.bouncycastle.util.test.Test;
@@ -38,27 +39,6 @@ public void nextBytes(
3839
}
3940
}
4041

41-
private boolean arrayEquals(
42-
byte[] a,
43-
byte[] b)
44-
{
45-
if (a.length != b.length)
46-
{
47-
return false;
48-
}
49-
50-
for (int i = 0; i != a.length; i++)
51-
{
52-
if (a[i] != b[i])
53-
{
54-
return false;
55-
}
56-
}
57-
58-
return true;
59-
}
60-
61-
6242
private RSAPublicKeySpec pubKeySpec = new RSAPublicKeySpec(
6343
new BigInteger("a56e4a0e701017589a5187dc7ea841d156f2ec0e36ad52a44dfeb1e61f7ad991d8c51056ffedb162b4c0f283a12a88a394dff526ab7291cbb307ceabfce0b1dfd5cd9508096d5b2b8b6df5d671ef6377c0921cb23c270a70e2598e6ff89d19f105acc2d3f0cb35f29280e1386b6f64c4ef22e1e1f20d0ce8cffb2249bd9a2137",16),
6444
new BigInteger("010001",16));
@@ -98,7 +78,7 @@ public TestResult perform()
9878
s.update(msg1a);
9979
byte[] sig = s.sign();
10080

101-
if (!arrayEquals(sig1a, sig))
81+
if (!Arrays.areEqual(sig1a, sig))
10282
{
10383
return new SimpleTestResult(false, "PSS Sign test expected " + new String(Hex.encode(sig1a)) + " got " + new String(Hex.encode(sig)));
10484
}
@@ -118,7 +98,7 @@ public TestResult perform()
11898
s.update(msg1a);
11999
sig = s.sign();
120100

121-
if (!arrayEquals(sig1b, sig))
101+
if (!Arrays.areEqual(sig1b, sig))
122102
{
123103
return new SimpleTestResult(false, "PSS Sign test expected " + new String(Hex.encode(sig1b)) + " got " + new String(Hex.encode(sig)));
124104
}

0 commit comments

Comments
 (0)