/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* @author Vladimir N. Molotkov
* @version $Revision$
*/
package tests.security.spec;
import junit.framework.TestCase;
import java.math.BigInteger;
import java.security.spec.KeySpec;
import java.security.spec.RSAMultiPrimePrivateCrtKeySpec;
import java.security.spec.RSAOtherPrimeInfo;
import java.security.spec.RSAPrivateKeySpec;
/**
* Tests for RSAMultiPrimePrivateCrtKeySpec
class fields and methods.
*
*/
public class RSAMultiPrimePrivateCrtKeySpecTest extends TestCase {
/**
* Reference array of RSAOtherPrimeInfo. DO NOT MODIFY
*/
private static final RSAOtherPrimeInfo[] opi = new RSAOtherPrimeInfo[] {
new RSAOtherPrimeInfo(BigInteger.ONE, BigInteger.ONE, BigInteger.ONE),
new RSAOtherPrimeInfo(BigInteger.ONE, BigInteger.ONE, BigInteger.ONE),
new RSAOtherPrimeInfo(BigInteger.ONE, BigInteger.ONE, BigInteger.ONE)
};
// Test-cases:
/**
* Test #1 for
* RSAMultiPrimePrivateCrtKeySpec(BigInteger modulus,
* BigInteger publicExponent,
* BigInteger privateExponent,
* BigInteger primeP,
* BigInteger primeQ,
* BigInteger primeExponentP,
* BigInteger primeExponentQ,
* BigInteger crtCoefficient,
* RSAOtherPrimeInfo[] otherPrimeInfo)
*
ctor
* Assertion: constructs RSAMultiPrimePrivateCrtKeySpec
* object using valid parameters
*/
public final void testRSAMultiPrimePrivateCrtKeySpec01() {
KeySpec ks = new RSAMultiPrimePrivateCrtKeySpec(
BigInteger.ONE,
BigInteger.ONE,
BigInteger.ONE,
BigInteger.ONE,
BigInteger.ONE,
BigInteger.ONE,
BigInteger.ONE,
BigInteger.ONE,
opi);
assertTrue(ks instanceof RSAMultiPrimePrivateCrtKeySpec);
}
/**
* Test #2 for
* RSAMultiPrimePrivateCrtKeySpec(BigInteger modulus,
* BigInteger publicExponent,
* BigInteger privateExponent,
* BigInteger primeP,
* BigInteger primeQ,
* BigInteger primeExponentP,
* BigInteger primeExponentQ,
* BigInteger crtCoefficient,
* RSAOtherPrimeInfo[] otherPrimeInfo)
*
ctor
* Assertion: NullPointerException if modulus is null
*/
public final void testRSAMultiPrimePrivateCrtKeySpec02() {
try {
new RSAMultiPrimePrivateCrtKeySpec(
null,
BigInteger.ONE,
BigInteger.ONE,
BigInteger.ONE,
BigInteger.ONE,
BigInteger.ONE,
BigInteger.ONE,
BigInteger.ONE,
opi);
fail("Expected NPE not thrown");
} catch (NullPointerException e) {
}
}
/**
* Test #3 for
* RSAMultiPrimePrivateCrtKeySpec(BigInteger modulus,
* BigInteger publicExponent,
* BigInteger privateExponent,
* BigInteger primeP,
* BigInteger primeQ,
* BigInteger primeExponentP,
* BigInteger primeExponentQ,
* BigInteger crtCoefficient,
* RSAOtherPrimeInfo[] otherPrimeInfo)
*
ctor
* Assertion: NullPointerException if publicExponent is null
*/
public final void testRSAMultiPrimePrivateCrtKeySpec03() {
try {
new RSAMultiPrimePrivateCrtKeySpec(
BigInteger.ONE,
null,
BigInteger.ONE,
BigInteger.ONE,
BigInteger.ONE,
BigInteger.ONE,
BigInteger.ONE,
BigInteger.ONE,
opi);
fail("Expected NPE not thrown");
} catch (NullPointerException e) {
}
}
/**
* Test #4 for
* RSAMultiPrimePrivateCrtKeySpec(BigInteger modulus,
* BigInteger publicExponent,
* BigInteger privateExponent,
* BigInteger primeP,
* BigInteger primeQ,
* BigInteger primeExponentP,
* BigInteger primeExponentQ,
* BigInteger crtCoefficient,
* RSAOtherPrimeInfo[] otherPrimeInfo)
*
ctor
* Assertion: NullPointerException if privateExponent is null
*/
public final void testRSAMultiPrimePrivateCrtKeySpec04() {
try {
new RSAMultiPrimePrivateCrtKeySpec(
BigInteger.ONE,
BigInteger.ONE,
null,
BigInteger.ONE,
BigInteger.ONE,
BigInteger.ONE,
BigInteger.ONE,
BigInteger.ONE,
opi);
fail("Expected NPE not thrown");
} catch (NullPointerException e) {
}
}
/**
* Test #5 for
* RSAMultiPrimePrivateCrtKeySpec(BigInteger modulus,
* BigInteger publicExponent,
* BigInteger privateExponent,
* BigInteger primeP,
* BigInteger primeQ,
* BigInteger primeExponentP,
* BigInteger primeExponentQ,
* BigInteger crtCoefficient,
* RSAOtherPrimeInfo[] otherPrimeInfo)
*
ctor
* Assertion: NullPointerException if primeP is null
*/
public final void testRSAMultiPrimePrivateCrtKeySpec05() {
try {
new RSAMultiPrimePrivateCrtKeySpec(
BigInteger.ONE,
BigInteger.ONE,
BigInteger.ONE,
null,
BigInteger.ONE,
BigInteger.ONE,
BigInteger.ONE,
BigInteger.ONE,
opi);
fail("Expected NPE not thrown");
} catch (NullPointerException e) {
}
}
/**
* Test #6 for
* RSAMultiPrimePrivateCrtKeySpec(BigInteger modulus,
* BigInteger publicExponent,
* BigInteger privateExponent,
* BigInteger primeP,
* BigInteger primeQ,
* BigInteger primeExponentP,
* BigInteger primeExponentQ,
* BigInteger crtCoefficient,
* RSAOtherPrimeInfo[] otherPrimeInfo)
*
ctor
* Assertion: NullPointerException if primeQ is null
*/
public final void testRSAMultiPrimePrivateCrtKeySpec06() {
try {
new RSAMultiPrimePrivateCrtKeySpec(
BigInteger.ONE,
BigInteger.ONE,
BigInteger.ONE,
BigInteger.ONE,
null,
BigInteger.ONE,
BigInteger.ONE,
BigInteger.ONE,
opi);
fail("Expected NPE not thrown");
} catch (NullPointerException e) {
}
}
/**
* Test #7 for
* RSAMultiPrimePrivateCrtKeySpec(BigInteger modulus,
* BigInteger publicExponent,
* BigInteger privateExponent,
* BigInteger primeP,
* BigInteger primeQ,
* BigInteger primeExponentP,
* BigInteger primeExponentQ,
* BigInteger crtCoefficient,
* RSAOtherPrimeInfo[] otherPrimeInfo)
*
ctor
* Assertion: NullPointerException if primeExponentP is null
*/
public final void testRSAMultiPrimePrivateCrtKeySpec07() {
try {
new RSAMultiPrimePrivateCrtKeySpec(
BigInteger.ONE,
BigInteger.ONE,
BigInteger.ONE,
BigInteger.ONE,
BigInteger.ONE,
null,
BigInteger.ONE,
BigInteger.ONE,
opi);
fail("Expected NPE not thrown");
} catch (NullPointerException e) {
}
}
/**
* Test #8 for
* RSAMultiPrimePrivateCrtKeySpec(BigInteger modulus,
* BigInteger publicExponent,
* BigInteger privateExponent,
* BigInteger primeP,
* BigInteger primeQ,
* BigInteger primeExponentP,
* BigInteger primeExponentQ,
* BigInteger crtCoefficient,
* RSAOtherPrimeInfo[] otherPrimeInfo)
*
ctor
* Assertion: NullPointerException if primeExponentQ is null
*/
public final void testRSAMultiPrimePrivateCrtKeySpec08() {
try {
new RSAMultiPrimePrivateCrtKeySpec(
BigInteger.ONE,
BigInteger.ONE,
BigInteger.ONE,
BigInteger.ONE,
BigInteger.ONE,
BigInteger.ONE,
null,
BigInteger.ONE,
opi);
fail("Expected NPE not thrown");
} catch (NullPointerException e) {
}
}
/**
* Test #9 for
* RSAMultiPrimePrivateCrtKeySpec(BigInteger modulus,
* BigInteger publicExponent,
* BigInteger privateExponent,
* BigInteger primeP,
* BigInteger primeQ,
* BigInteger primeExponentP,
* BigInteger primeExponentQ,
* BigInteger crtCoefficient,
* RSAOtherPrimeInfo[] otherPrimeInfo)
*
ctor
* Assertion: NullPointerException if crtCoefficient is null
*/
public final void testRSAMultiPrimePrivateCrtKeySpec09() {
try {
new RSAMultiPrimePrivateCrtKeySpec(
BigInteger.ONE,
BigInteger.ONE,
BigInteger.ONE,
BigInteger.ONE,
BigInteger.ONE,
BigInteger.ONE,
BigInteger.ONE,
null,
opi);
fail("Expected NPE not thrown");
} catch (NullPointerException e) {
}
}
/**
* Test #10 for
* RSAMultiPrimePrivateCrtKeySpec(BigInteger modulus,
* BigInteger publicExponent,
* BigInteger privateExponent,
* BigInteger primeP,
* BigInteger primeQ,
* BigInteger primeExponentP,
* BigInteger primeExponentQ,
* BigInteger crtCoefficient,
* RSAOtherPrimeInfo[] otherPrimeInfo)
*
ctor
* Assertion: otherPrimeInfo can be null
*/
public final void testRSAMultiPrimePrivateCrtKeySpec10() {
try {
new RSAMultiPrimePrivateCrtKeySpec(
BigInteger.ONE,
BigInteger.ONE,
BigInteger.ONE,
BigInteger.ONE,
BigInteger.ONE,
BigInteger.ONE,
BigInteger.ONE,
BigInteger.ONE,
null);
} catch (Exception e) {
fail("Unexpected exception is thrown");
}
}
/**
* Test #11 for
* RSAMultiPrimePrivateCrtKeySpec(BigInteger modulus,
* BigInteger publicExponent,
* BigInteger privateExponent,
* BigInteger primeP,
* BigInteger primeQ,
* BigInteger primeExponentP,
* BigInteger primeExponentQ,
* BigInteger crtCoefficient,
* RSAOtherPrimeInfo[] otherPrimeInfo)
*
ctor
* Assertion: IllegalArgumentException if otherPrimeInfo length is 0
*/
public final void testRSAMultiPrimePrivateCrtKeySpec11() {
try {
new RSAMultiPrimePrivateCrtKeySpec(
BigInteger.ONE,
BigInteger.ONE,
BigInteger.ONE,
BigInteger.ONE,
BigInteger.ONE,
BigInteger.ONE,
BigInteger.ONE,
BigInteger.ONE,
new RSAOtherPrimeInfo[0]);
fail("Expected IAE not thrown");
} catch (IllegalArgumentException e) {
}
}
/**
* Test #12 for
* RSAMultiPrimePrivateCrtKeySpec(BigInteger modulus,
* BigInteger publicExponent,
* BigInteger privateExponent,
* BigInteger primeP,
* BigInteger primeQ,
* BigInteger primeExponentP,
* BigInteger primeExponentQ,
* BigInteger crtCoefficient,
* RSAOtherPrimeInfo[] otherPrimeInfo)
*
ctor
* Assertion: constructs RSAMultiPrimePrivateCrtKeySpec
* object using valid parameters. Constructed object must be
* instance of RSAPrivateKeySpec.
*/
public final void testRSAMultiPrimePrivateCrtKeySpec12() {
KeySpec ks = new RSAMultiPrimePrivateCrtKeySpec(
BigInteger.ONE,
BigInteger.ONE,
BigInteger.ONE,
BigInteger.ONE,
BigInteger.ONE,
BigInteger.ONE,
BigInteger.ONE,
BigInteger.ONE,
opi);
assertTrue(ks instanceof RSAPrivateKeySpec);
}
/**
* Test for getCrtCoefficient()
method
* Assertion: returns crt coefficient
*/
public final void testGetCrtCoefficient() {
RSAMultiPrimePrivateCrtKeySpec ks =
new RSAMultiPrimePrivateCrtKeySpec(
BigInteger.ONE,
BigInteger.ONE,
BigInteger.ONE,
BigInteger.ONE,
BigInteger.ONE,
BigInteger.ONE,
BigInteger.ONE,
BigInteger.ONE,
opi);
assertTrue(BigInteger.ONE.equals(ks.getCrtCoefficient()));
}
/**
* Test for getPrimeExponentP()
method
* Assertion: returns prime exponent P
*/
public final void testGetPrimeExponentP() {
RSAMultiPrimePrivateCrtKeySpec ks =
new RSAMultiPrimePrivateCrtKeySpec(
BigInteger.ONE,
BigInteger.ONE,
BigInteger.ONE,
BigInteger.ONE,
BigInteger.ONE,
BigInteger.ONE,
BigInteger.ONE,
BigInteger.ONE,
opi);
assertTrue(BigInteger.ONE.equals(ks.getPrimeExponentP()));
}
/**
* Test for getPrimeExponentQ()
method
* Assertion: returns prime exponent Q
*/
public final void testGetPrimeExponentQ() {
RSAMultiPrimePrivateCrtKeySpec ks =
new RSAMultiPrimePrivateCrtKeySpec(
BigInteger.ONE,
BigInteger.ONE,
BigInteger.ONE,
BigInteger.ONE,
BigInteger.ONE,
BigInteger.ONE,
BigInteger.ONE,
BigInteger.ONE,
opi);
assertTrue(BigInteger.ONE.equals(ks.getPrimeExponentQ()));
}
/**
* Test for getPrimeP()
method
* Assertion: returns prime P
*/
public final void testGetPrimeP() {
RSAMultiPrimePrivateCrtKeySpec ks =
new RSAMultiPrimePrivateCrtKeySpec(
BigInteger.ONE,
BigInteger.ONE,
BigInteger.ONE,
BigInteger.ONE,
BigInteger.ONE,
BigInteger.ONE,
BigInteger.ONE,
BigInteger.ONE,
opi);
assertTrue(BigInteger.ONE.equals(ks.getPrimeP()));
}
/**
* Test for getPrimeQ()
method
* Assertion: returns prime Q
*/
public final void testGetPrimeQ() {
RSAMultiPrimePrivateCrtKeySpec ks =
new RSAMultiPrimePrivateCrtKeySpec(
BigInteger.ONE,
BigInteger.ONE,
BigInteger.ONE,
BigInteger.ONE,
BigInteger.ONE,
BigInteger.ONE,
BigInteger.ONE,
BigInteger.ONE,
opi);
assertTrue(BigInteger.ONE.equals(ks.getPrimeQ()));
}
/**
* Test for getPublicExponent()
method
* Assertion: returns public exponent
*/
public final void testGetPublicExponent() {
RSAMultiPrimePrivateCrtKeySpec ks =
new RSAMultiPrimePrivateCrtKeySpec(
BigInteger.ONE,
BigInteger.ONE,
BigInteger.ONE,
BigInteger.ONE,
BigInteger.ONE,
BigInteger.ONE,
BigInteger.ONE,
BigInteger.ONE,
opi);
assertTrue(BigInteger.ONE.equals(ks.getPublicExponent()));
}
/**
* Test #1 for getOtherPrimeInfo()
method
* Assertion: returns array of RSAOtherPrimeInfo
*/
public final void testGetOtherPrimeInfo01() {
RSAMultiPrimePrivateCrtKeySpec ks =
new RSAMultiPrimePrivateCrtKeySpec(
BigInteger.ONE,
BigInteger.ONE,
BigInteger.ONE,
BigInteger.ONE,
BigInteger.ONE,
BigInteger.ONE,
BigInteger.ONE,
BigInteger.ONE,
opi);
assertTrue(checkOtherPrimeInfo(ks.getOtherPrimeInfo()));
}
/**
* Test #2 for getOtherPrimeInfo()
method
* Assertion: returns null if null has been passed to the
* constructor as otherPrimeInfo parameter
*/
public final void testGetOtherPrimeInfo02() {
RSAMultiPrimePrivateCrtKeySpec ks =
new RSAMultiPrimePrivateCrtKeySpec(
BigInteger.ONE,
BigInteger.ONE,
BigInteger.ONE,
BigInteger.ONE,
BigInteger.ONE,
BigInteger.ONE,
BigInteger.ONE,
BigInteger.ONE,
null);
assertNull(ks.getOtherPrimeInfo());
}
//
// immutability tests
//
/**
* Tests that internal state of the object
* can not be modified by modifying initial array
*/
public final void testIsStatePreserved1() {
// Create initial array
RSAOtherPrimeInfo[] opi1 = opi.clone();
RSAMultiPrimePrivateCrtKeySpec ks = new RSAMultiPrimePrivateCrtKeySpec(
BigInteger.ONE,
BigInteger.ONE,
BigInteger.ONE,
BigInteger.ONE,
BigInteger.ONE,
BigInteger.ONE,
BigInteger.ONE,
BigInteger.ONE,
opi1);
// Modify initial array
opi1[2] = new RSAOtherPrimeInfo(BigInteger.ZERO,
BigInteger.ZERO,
BigInteger.ZERO);
// Check that above modification
// does not affect internal state
assertTrue(checkOtherPrimeInfo(ks.getOtherPrimeInfo()));
}
/**
* Tests that internal state of the object
* can not be modified using array reference
* returned by getOtherPrimeInfo()
* method
*/
public final void testIsStatePreserved2() {
// Create initial array
RSAOtherPrimeInfo[] opi1 = opi.clone();
RSAMultiPrimePrivateCrtKeySpec ks = new RSAMultiPrimePrivateCrtKeySpec(
BigInteger.ONE,
BigInteger.ONE,
BigInteger.ONE,
BigInteger.ONE,
BigInteger.ONE,
BigInteger.ONE,
BigInteger.ONE,
BigInteger.ONE,
opi1);
RSAOtherPrimeInfo[] ret = ks.getOtherPrimeInfo();
// Modify returned array
ret[2] = new RSAOtherPrimeInfo(BigInteger.ZERO,
BigInteger.ZERO,
BigInteger.ZERO);
// Check that above modification
// does not affect internal state
assertTrue(checkOtherPrimeInfo(ks.getOtherPrimeInfo()));
}
//
// Tests for inherited methods
//
/**
* Test for getModulus()
method
* Assertion: returns modulus
*/
public final void testGetModulus() {
RSAMultiPrimePrivateCrtKeySpec ks =
new RSAMultiPrimePrivateCrtKeySpec(
BigInteger.ONE,
BigInteger.ONE,
BigInteger.ONE,
BigInteger.ONE,
BigInteger.ONE,
BigInteger.ONE,
BigInteger.ONE,
BigInteger.ONE,
opi);
assertTrue(BigInteger.ONE.equals(ks.getModulus()));
}
/**
* Test for getPrivateExponent()
method
* Assertion: returns private exponent
*/
public final void testGetPrivateExponent() {
RSAMultiPrimePrivateCrtKeySpec ks =
new RSAMultiPrimePrivateCrtKeySpec(
BigInteger.ONE,
BigInteger.ONE,
BigInteger.ONE,
BigInteger.ONE,
BigInteger.ONE,
BigInteger.ONE,
BigInteger.ONE,
BigInteger.ONE,
opi);
assertTrue(BigInteger.ONE.equals(ks.getPrivateExponent()));
}
// private stuff
//
/**
* Compares array passed as a parameter with reference one
*
* private static final RSAOtherPrimeInfo[] opi
*
* @param toBeChecked
* Array to be compared
* @return
* true if arrays are equal
*/
private boolean checkOtherPrimeInfo(RSAOtherPrimeInfo[] toBeChecked) {
if (toBeChecked == null || toBeChecked.length != opi.length) {
return false;
}
for (int i=0; i