Skip to content

Commit 56d4f6f

Browse files
committed
removed Assert
1 parent b9cb354 commit 56d4f6f

File tree

2 files changed

+53
-7
lines changed

2 files changed

+53
-7
lines changed

core/src/main/java/org/bouncycastle/util/test/SimpleTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ protected void isTrue(
3131
}
3232
}
3333

34-
protected void isTrue(
34+
public void isTrue(
3535
String message,
3636
boolean value)
3737
{
@@ -273,7 +273,7 @@ void operation()
273273
throws Exception;
274274
}
275275

276-
protected Exception testException(String failMessage, String exceptionClass, TestExceptionOperation operation)
276+
public Exception testException(String failMessage, String exceptionClass, TestExceptionOperation operation)
277277
{
278278
try
279279
{
@@ -286,7 +286,7 @@ protected Exception testException(String failMessage, String exceptionClass, Tes
286286
{
287287
isTrue(e.getMessage(), e.getMessage().indexOf(failMessage) >= 0);
288288
}
289-
isTrue(e.getClass().getName().indexOf(exceptionClass) >= 0);
289+
isTrue(e.getMessage(),e.getClass().getName().indexOf(exceptionClass) >= 0);
290290
return e;
291291
}
292292
return null;

core/src/test/java/org/bouncycastle/crypto/test/CipherTest.java

Lines changed: 50 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@
1010
import org.bouncycastle.crypto.InvalidCipherTextException;
1111
import org.bouncycastle.crypto.KeyGenerationParameters;
1212
import org.bouncycastle.crypto.modes.AEADCipher;
13+
import org.bouncycastle.crypto.params.AEADParameters;
1314
import org.bouncycastle.crypto.params.KeyParameter;
1415
import org.bouncycastle.crypto.params.ParametersWithIV;
1516
import org.bouncycastle.util.Arrays;
1617
import org.bouncycastle.util.test.SimpleTest;
1718
import org.bouncycastle.util.test.SimpleTestResult;
1819
import org.bouncycastle.util.test.TestFailedException;
19-
import org.junit.Assert;
2020

2121
public abstract class CipherTest
2222
extends SimpleTest
@@ -255,7 +255,7 @@ static void isEqualTo(
255255
}
256256
}
257257

258-
static void checkCipher(final BlockCipher pCipher, final int datalen)
258+
void checkCipher(final BlockCipher pCipher, final int datalen)
259259
throws Exception
260260
{
261261
final SecureRandom random = new SecureRandom();
@@ -299,7 +299,53 @@ static void checkCipher(final BlockCipher pCipher, final int datalen)
299299
myCipher.doFinal(plaintext, myOutLen);
300300

301301
/* Check that the cipherTexts are identical */
302-
Assert.assertArrayEquals(myOutput, myOutput2);
303-
Assert.assertArrayEquals(myData, plaintext);
302+
isTrue(areEqual(myOutput, myOutput2));
303+
isTrue(areEqual(myData, plaintext));
304+
}
305+
306+
static void checkAEADParemeter(SimpleTest test, int keySize, int ivSize, final int macSize, int blockSize, final AEADCipher cipher)
307+
throws Exception
308+
{
309+
final SecureRandom random = new SecureRandom();
310+
final byte[] key = new byte[keySize];
311+
final byte[] iv = new byte[ivSize];
312+
int tmpLength = random.nextInt(blockSize - 1) + 1;
313+
final byte[] plaintext = new byte[blockSize * 2 + tmpLength];
314+
byte[] aad = new byte[random.nextInt(100) + 2];
315+
random.nextBytes(key);
316+
random.nextBytes(iv);
317+
random.nextBytes(plaintext);
318+
random.nextBytes(aad);
319+
cipher.init(true, new ParametersWithIV(new KeyParameter(key), iv));
320+
byte[] ciphertext1 = new byte[cipher.getOutputSize(plaintext.length)];
321+
for (int i = 0; i < aad.length; ++i)
322+
{
323+
cipher.processAADByte(aad[i]);
324+
}
325+
int len = cipher.processBytes(plaintext, 0, plaintext.length, ciphertext1, 0);
326+
len += cipher.doFinal(ciphertext1, len);
327+
int aadSplit = random.nextInt(aad.length) + 1;
328+
cipher.init(true, new AEADParameters(new KeyParameter(key), macSize * 8, iv, Arrays.copyOf(aad, aadSplit)));
329+
cipher.processAADBytes(aad, aadSplit, aad.length - aadSplit);
330+
byte[] ciphertext2 = new byte[cipher.getOutputSize(plaintext.length)];
331+
len = cipher.processBytes(plaintext, 0, plaintext.length, ciphertext2, 0);
332+
len += cipher.doFinal(ciphertext2, len);
333+
test.isTrue("cipher text check", Arrays.areEqual(ciphertext1, ciphertext2));
334+
335+
test.testException("Invalid value for MAC size: ", "IllegalArgumentException", new TestExceptionOperation()
336+
{
337+
@Override
338+
public void operation()
339+
throws Exception
340+
{
341+
int macSize2 = random.nextInt();
342+
while (macSize2 == macSize * 8)
343+
{
344+
macSize2 = random.nextInt();
345+
}
346+
cipher.init(true, new AEADParameters(new KeyParameter(key), macSize2, iv, null));
347+
}
348+
});
349+
304350
}
305351
}

0 commit comments

Comments
 (0)