use of java.security.InvalidParameterException in project robovm by robovm.
the class KeyPairGenerator2Test method checkResult.
private void checkResult(KeyPairGenerator keyPairGen, int mode) throws InvalidAlgorithmParameterException {
AlgorithmParameterSpec pp = null;
switch(mode) {
case 1:
try {
keyPairGen.initialize(pp, new SecureRandom());
fail("InvalidAlgorithmParameterException must be thrown");
} catch (InvalidAlgorithmParameterException e) {
}
keyPairGen.initialize(1000, new SecureRandom());
try {
keyPairGen.initialize(-1024, new SecureRandom());
fail("InvalidParameterException must be thrown");
} catch (InvalidParameterException e) {
assertEquals("Incorrect exception", e.getMessage(), "Incorrect keysize parameter");
}
try {
keyPairGen.initialize(100, null);
fail("InvalidParameterException must be thrown");
} catch (InvalidParameterException e) {
assertEquals("Incorrect exception", e.getMessage(), "Incorrect random");
}
keyPairGen.generateKeyPair();
keyPairGen.genKeyPair();
break;
case 2:
try {
keyPairGen.initialize(pp, new SecureRandom());
} catch (UnsupportedOperationException e) {
// js2e does not throw this exception
}
keyPairGen.initialize(1000, new SecureRandom());
try {
keyPairGen.initialize(63, new SecureRandom());
fail("InvalidParameterException must be thrown");
} catch (InvalidParameterException e) {
}
keyPairGen.initialize(100, null);
assertNull("Not null KeyPair", keyPairGen.generateKeyPair());
assertNull("Not null KeyPair", keyPairGen.genKeyPair());
break;
case 3:
keyPairGen.initialize(pp, new SecureRandom());
keyPairGen.initialize(pp);
keyPairGen.initialize(1000, new SecureRandom());
keyPairGen.initialize(100);
assertNotNull("Null KeyPair", keyPairGen.generateKeyPair());
assertNotNull("Null KeyPair", keyPairGen.genKeyPair());
break;
case 4:
try {
keyPairGen.initialize(pp, null);
fail("UnsupportedOperationException must be thrown");
} catch (UnsupportedOperationException e) {
}
keyPairGen.initialize(pp, new SecureRandom());
keyPairGen.initialize(101, new SecureRandom());
keyPairGen.initialize(10000);
try {
keyPairGen.initialize(101, null);
fail("IllegalArgumentException must be thrown for null random");
} catch (IllegalArgumentException e) {
}
try {
keyPairGen.initialize(99, new SecureRandom());
fail("InvalidParameterException must be thrown for invalid key");
} catch (InvalidParameterException e) {
}
try {
keyPairGen.initialize(99);
fail("InvalidParameterException must be thrown for invalid key");
} catch (InvalidParameterException e) {
}
try {
keyPairGen.initialize(199, null);
fail("IllegalArgumentException must be thrown for null random");
} catch (IllegalArgumentException e) {
}
assertNull("Not null KeyPair", keyPairGen.generateKeyPair());
assertNull("Not null KeyPair", keyPairGen.genKeyPair());
break;
}
}
use of java.security.InvalidParameterException in project robovm by robovm.
the class InvalidParameterExceptionTest method testInvalidParameterException02.
/**
* Test for <code>InvalidParameterException(String)</code> constructor
* Assertion: constructs InvalidParameterException with detail message msg.
* Parameter <code>msg</code> is not null.
*/
public void testInvalidParameterException02() {
InvalidParameterException tE;
for (int i = 0; i < msgs.length; i++) {
tE = new InvalidParameterException(msgs[i]);
assertEquals("getMessage() must return: ".concat(msgs[i]), tE.getMessage(), msgs[i]);
assertNull("getCause() must return null", tE.getCause());
}
}
use of java.security.InvalidParameterException in project robovm by robovm.
the class DSAKeyPairGeneratorTest method test_DSAKeyPairGenerator01.
/**
* java.security.interfaces.DSAKeyPairGenerator
* #initialize(DSAParams params, SecureRandom random)
*/
public void test_DSAKeyPairGenerator01() {
DSAParams dsaParams = new DSAParameterSpec(p, q, g);
SecureRandom random = null;
MyDSA dsa = new MyDSA(dsaParams);
try {
random = SecureRandom.getInstance("SHA1PRNG");
} catch (Exception e) {
fail("Unexpected exception for SecureRandom: " + e);
}
try {
dsa.initialize(dsaParams, random);
} catch (Exception e) {
fail("Unexpected exception: " + e);
}
try {
dsa.initialize(dsaParams, null);
fail("InvalidParameterException was not thrown");
} catch (InvalidParameterException ipe) {
//expected
} catch (Exception e) {
fail(e + " was thrown instead of InvalidParameterException");
}
try {
dsa.initialize(null, random);
fail("InvalidParameterException was not thrown");
} catch (InvalidParameterException ipe) {
//expected
} catch (Exception e) {
fail(e + " was thrown instead of InvalidParameterException");
}
}
use of java.security.InvalidParameterException in project robovm by robovm.
the class DSAKeyPairGeneratorTest method test_DSAKeyPairGenerator02.
/**
* java.security.interfaces.DSAKeyPairGenerator
* #initialize(int modlen, boolean genParams, SecureRandom randomm)
*/
public void test_DSAKeyPairGenerator02() {
int[] invalidLen = { -1, 0, 511, 513, 650, 1023, 1025 };
DSAParams dsaParams = new DSAParameterSpec(p, q, g);
SecureRandom random = null;
MyDSA dsa = new MyDSA(null);
try {
random = SecureRandom.getInstance("SHA1PRNG");
} catch (Exception e) {
fail("Unexpected exception for SecureRandom: " + e);
}
//exception case
try {
dsa.initialize(520, false, random);
fail("InvalidParameterException was not thrown");
} catch (InvalidParameterException ipe) {
String str = ipe.getMessage();
if (!str.equals("there are not precomputed parameters")) {
fail("Incorrect exception's message: " + str);
}
} catch (Exception e) {
fail(e + " was thrown instead of InvalidParameterException");
}
//exception case
for (int i = 0; i < invalidLen.length; i++) {
try {
dsa.initialize(invalidLen[i], true, random);
fail("InvalidParameterException was not thrown");
} catch (InvalidParameterException ipe) {
String str = ipe.getMessage();
if (!str.equals("Incorrect modlen")) {
fail("Incorrect exception's message: " + str);
}
} catch (Exception e) {
fail(e + " was thrown instead of InvalidParameterException");
}
}
//positive case
dsa = new MyDSA(dsaParams);
try {
dsa.initialize(520, true, random);
} catch (Exception e) {
fail(e + " was thrown for subcase 1");
}
//positive case
try {
dsa.initialize(520, false, random);
} catch (Exception e) {
fail(e + " was thrown for subcase 1");
}
}
use of java.security.InvalidParameterException in project robovm by robovm.
the class myKeyGenerator method test_initILjava_security_SecureRandom.
public void test_initILjava_security_SecureRandom() throws NoSuchAlgorithmException {
SecureRandom random = null;
KeyGenerator kg = null;
for (int i = 0; i < validAlgorithmsKeyGenerator.length; i++) {
kg = KeyGenerator.getInstance(validAlgorithmsKeyGenerator[i]);
random = new SecureRandom();
kg.init(validKeySizes[i], random);
assertNotNull(kg.getProvider());
}
kg = KeyGenerator.getInstance(validAlgorithmsKeyGenerator[0]);
try {
kg.init(5, random);
fail("InvalidParameterException expected");
} catch (InvalidParameterException e) {
//expected
}
}
Aggregations