Index: modules/security/src/test/java/common/tests/api/java/security/AlgorithmParametersTest.java =================================================================== --- modules/security/src/test/java/common/tests/api/java/security/AlgorithmParametersTest.java (revision 398875) +++ modules/security/src/test/java/common/tests/api/java/security/AlgorithmParametersTest.java (working copy) @@ -62,24 +62,24 @@ /** * @tests java.security.AlgorithmParameters#getEncoded(java.lang.String) */ - public void test_getEncodedLjava_lang_String() throws Exception { - // Test for method byte [] - // java.security.AlgorithmParameters.getEncoded(java.lang.String) - AlgorithmParameters params = AlgorithmParameters.getInstance("DSA"); + public void test_getEncodedLjava_lang_String() throws Exception { + // Test for method byte [] + // java.security.AlgorithmParameters.getEncoded(java.lang.String) + AlgorithmParameters params = AlgorithmParameters.getInstance("DSA"); - byte[] enc = null; - try { - params.init(new DSAParameterSpec(BigInteger.ONE, BigInteger.ONE, - BigInteger.ONE)); - enc = params.getEncoded("JUNK"); - fail("bogus format should have resulted in IOException"); - } catch (IOException e) { - // expected - } + params.init(new DSAParameterSpec(BigInteger.ONE, BigInteger.ONE, + BigInteger.ONE)); + + // getEncoded behavior is not specified for unknown 'format' + // different providers work in different manner: + // 1.5 RI provider uses primary encoding format, + // but BC provider returns null. + // As it is provider specific behavior - it is not tested. + //byte[] enc = params.getEncoded("JUNK"); - enc = params.getEncoded("ASN.1"); - assertNotNull("ANS.1 should be supported", enc); - } + byte[] enc = params.getEncoded("ASN.1"); + assertNotNull("ANS.1 should be supported", enc); + } /** * @tests java.security.AlgorithmParameters#getInstance(java.lang.String) @@ -133,83 +133,58 @@ /** * @tests java.security.AlgorithmParameters#getProvider() */ - public void test_getProvider() { + public void test_getProvider() throws Exception { // Test for method java.security.Provider // java.security.AlgorithmParameters.getProvider() - try { - Provider p = AlgorithmParameters.getInstance("DSA").getProvider(); - assertNotNull("provider is null", p); - } catch (NoSuchAlgorithmException e) { - fail("getInstance did not find algorithm"); - } + Provider p = AlgorithmParameters.getInstance("DSA").getProvider(); + assertNotNull("provider is null", p); + } /** * @tests java.security.AlgorithmParameters#init(byte[]) */ - public void test_init$B() { + public void test_init$B() throws Exception { // Test for method void java.security.AlgorithmParameters.init(byte []) - try { - AlgorithmParameters params = AlgorithmParameters.getInstance("DSA"); - params.init(new DSAParameterSpec(BigInteger.ONE, BigInteger.ONE, - BigInteger.ONE)); - byte[] encoded = params.getEncoded(); - assertNotNull("encoded spec is null", encoded); - AlgorithmParameters params2 = AlgorithmParameters - .getInstance("DSA"); - params2.init(encoded); - byte[] encodedAfter = params2.getEncoded(); - assertTrue("param encoded is different", Arrays.equals(encoded, - encodedAfter)); - } catch (NoSuchAlgorithmException e) { - fail("getInstance did not find algorithm"); - } catch (IOException e) { - fail("IOException getting encoded"); - } catch (InvalidParameterSpecException e) { - fail("cant pass DSAParameterSpec"); - } + AlgorithmParameters params = AlgorithmParameters.getInstance("DSA"); + params.init(new DSAParameterSpec(BigInteger.ONE, BigInteger.ONE, + BigInteger.ONE)); + byte[] encoded = params.getEncoded(); + assertNotNull("encoded spec is null", encoded); + AlgorithmParameters params2 = AlgorithmParameters.getInstance("DSA"); + params2.init(encoded); + byte[] encodedAfter = params2.getEncoded(); + assertTrue("param encoded is different", Arrays.equals(encoded, + encodedAfter)); } /** * @tests java.security.AlgorithmParameters#init(byte[], java.lang.String) */ - public void test_init$BLjava_lang_String() { + public void test_init$BLjava_lang_String() throws Exception { // Test for method void java.security.AlgorithmParameters.init(byte [], // java.lang.String) - try { - AlgorithmParameters params = AlgorithmParameters.getInstance("DSA"); - params.init(new DSAParameterSpec(BigInteger.ONE, BigInteger.ONE, - BigInteger.ONE)); - byte[] encoded = params.getEncoded(); - assertNotNull("encoded spec is null", encoded); - AlgorithmParameters params2 = AlgorithmParameters - .getInstance("DSA"); - params2.init(encoded, "ASN.1"); - byte[] encodedAfter = params2.getEncoded(); - assertTrue("param encoded is different", Arrays.equals(encoded, - encodedAfter)); - } catch (NoSuchAlgorithmException e) { - fail("getInstance did not find algorithm"); - } catch (IOException e) { - fail("IOException getting encoded"); - } catch (InvalidParameterSpecException e) { - fail("cant pass DSAParameterSpec"); - } + AlgorithmParameters params = AlgorithmParameters.getInstance("DSA"); + params.init(new DSAParameterSpec(BigInteger.ONE, BigInteger.ONE, + BigInteger.ONE)); + byte[] encoded = params.getEncoded(); + assertNotNull("encoded spec is null", encoded); + AlgorithmParameters params2 = AlgorithmParameters.getInstance("DSA"); + params2.init(encoded, "ASN.1"); + byte[] encodedAfter = params2.getEncoded(); + assertTrue("param encoded is different", Arrays.equals(encoded, + encodedAfter)); try { - AlgorithmParameters params = AlgorithmParameters.getInstance("DSA"); + params = AlgorithmParameters.getInstance("DSA"); params.init(new DSAParameterSpec(BigInteger.ONE, BigInteger.ONE, BigInteger.ONE)); - byte[] encoded = params.getEncoded(); + encoded = params.getEncoded(); assertNotNull("encoded spec is null", encoded); params.init(encoded, "DOUGLASMAWSON"); fail("unsupported format should have raised IOException"); - } catch (NoSuchAlgorithmException e) { - fail("getInstance did not find algorithm"); } catch (IOException e) { // expected - } catch (InvalidParameterSpecException e) { - fail("cant pass DSAParameterSpec"); } } @@ -217,36 +192,23 @@ /** * @tests java.security.AlgorithmParameters#init(java.security.spec.AlgorithmParameterSpec) */ - public void test_initLjava_security_spec_AlgorithmParameterSpec() { + public void test_initLjava_security_spec_AlgorithmParameterSpec() throws Exception { // Test for method void // java.security.AlgorithmParameters.init(java.security.spec.AlgorithmParameterSpec) AlgorithmParameters params = null; - try { - params = AlgorithmParameters.getInstance("DSA"); - } catch (NoSuchAlgorithmException e) { - fail("getInstance did not find algorithm"); - } - - try { - params.init(new DSAParameterSpec(BigInteger.ONE, BigInteger.ONE, - BigInteger.ONE)); - } catch (InvalidParameterSpecException e) { - fail("cant pass DSAParameterSpec"); - } + params = AlgorithmParameters.getInstance("DSA"); + params.init(new DSAParameterSpec(BigInteger.ONE, BigInteger.ONE, + BigInteger.ONE)); } /** * @tests java.security.AlgorithmParameters#toString() */ - public void test_toString() { + public void test_toString() throws Exception { // Test for method java.lang.String // java.security.AlgorithmParameters.toString() - try { - String str = AlgorithmParameters.getInstance("DSA").toString(); - assertNull("toString should be null", str); - } catch (NoSuchAlgorithmException e) { - e.printStackTrace(); - assertTrue("getInstance did not find algorithm", false); - } + String str = AlgorithmParameters.getInstance("DSA").toString(); + assertNull("toString should be null", str); + } -} \ No newline at end of file +} Index: modules/security/src/test/java/common/tests/api/java/security/SecurityTest.java =================================================================== --- modules/security/src/test/java/common/tests/api/java/security/SecurityTest.java (revision 398875) +++ modules/security/src/test/java/common/tests/api/java/security/SecurityTest.java (working copy) @@ -38,7 +38,6 @@ // java.security.Security.getProperty(java.lang.String) Security.setProperty("keyTestAlternate", "testing a property set"); assertEquals("the property value returned for keyTestAlternate was incorrect", - "testing a property set", Security.getProperty("keyTestAlternate")); } @@ -53,7 +52,6 @@ Security.setProperty("keyTest", "permission to set property"); assertEquals("the property value returned for keyTest was not correct", - "permission to set property", Security.getProperty("keyTest")); } @@ -80,8 +78,6 @@ // providers. assertTrue("provider is not found at the expected position", provTest[prefPos - 1].getName().equals(rsaName)); - } catch (Exception e) { - fail("Unexpected exception : " + e); } finally { // Now remove it - does nothing if provider not actually installed Security.removeProvider(rsaName); @@ -98,8 +94,6 @@ result2.getName().equals(test.getName()) && result2.getInfo().equals(test.getInfo()) && result2.getVersion() == test.getVersion()); - } catch (Exception e) { - fail("Unexpected exception : " + e); } finally { // Now remove it - does nothing if provider not actually installed Security.removeProvider(test.getName()); @@ -126,8 +120,6 @@ Security.getProviders(); assertEquals("addProvider method did not return a -1 for " + "a provider already added", -1, prefPosEntAdded); - } catch (Exception e) { - fail("Unexpected exception : " + e); } finally { // Now remove it - does nothing if provider not actually installed Security.removeProvider(entrust.getName()); @@ -251,9 +243,6 @@ fail("InvalidParameterException should be thrown "); } catch (InvalidParameterException e) { // Expected - } catch (Exception e) { - fail(" - Expected InvalidParameterException but got a " - + e); } } @@ -415,13 +404,8 @@ filter = new Hashtable(); filter.put("Provider.id name", "DRLCertFactory"); provTest = Security.getProviders(filter); - if (provTest == null) { - assertEquals("Filter : ", - 0, getProvidersCount(filter)); - } else { - assertEquals("Filter : ", - getProvidersCount(filter), provTest.length); - } + assertEquals("Filter : ", + null, provTest); // exception - no attribute name after the service.algorithm yet we // still supply an expected value. This is not valid. @@ -432,9 +416,6 @@ fail("InvalidParameterException should be thrown <512>"); } catch (InvalidParameterException e) { // Expected - } catch (Exception e) { - fail("InvalidParameterException was expected for" - + " <512> " + " but we got a " + e); } // exception - space character in the service.algorithm pair. Not valid. @@ -445,9 +426,6 @@ fail("InvalidParameterException should be thrown <512>"); } catch (InvalidParameterException e) { // Expected - } catch (Exception e) { - fail("InvalidParameterException was expected for" - + " <512> " + " but we got a " + e); } } @@ -478,8 +456,6 @@ int referAdded = Security.insertProviderAt(jceProvider, 3); assertEquals("the method insertProviderAt did not " + "return a -1 for providers already added", -1, referAdded); - } catch (Exception e) { - fail("Unexpected exception : " + e); } finally { // Tidy up before we leave Security.removeProvider(jceProvider.getName()); @@ -523,8 +499,6 @@ "the provider entrust is found after it was removed", provTest[i].getName() != entrust.getName()); } - } catch (Exception e) { - fail("Unexpected exception : " + e); } finally { // Tidy up - the following calls do nothing if the providers were // already removed above. Index: modules/security/src/test/java/common/tests/api/java/security/AlgorithmParameterGeneratorTest.java =================================================================== --- modules/security/src/test/java/common/tests/api/java/security/AlgorithmParameterGeneratorTest.java (revision 398875) +++ modules/security/src/test/java/common/tests/api/java/security/AlgorithmParameterGeneratorTest.java (working copy) @@ -18,6 +18,7 @@ import java.math.BigInteger; import java.security.AlgorithmParameterGenerator; import java.security.AlgorithmParameters; +import java.security.InvalidAlgorithmParameterException; import java.security.NoSuchProviderException; import java.security.Provider; import java.security.SecureRandom; @@ -151,14 +152,19 @@ * @tests java.security.AlgorithmParameterGenerator#init(java.security.spec.AlgorithmParameterSpec) */ public void test_initLjava_security_spec_AlgorithmParameterSpec() throws Exception { - // Test for method void - // java.security.AlgorithmParameterGenerator.init(java.security.spec.AlgorithmParameterSpec) - // checks that no exception is thrown - DSAParameterSpec spec = new DSAParameterSpec(BigInteger.ONE, - BigInteger.ONE, BigInteger.ONE); - AlgorithmParameterGenerator gen = AlgorithmParameterGenerator - .getInstance("DSA"); - gen.init(spec); + // Test for method void + // java.security.AlgorithmParameterGenerator.init(java.security.spec.AlgorithmParameterSpec) + // checks that InvalidAlgorithmParameterException is thrown + DSAParameterSpec spec = new DSAParameterSpec(BigInteger.ONE, + BigInteger.ONE, BigInteger.ONE); + AlgorithmParameterGenerator gen = AlgorithmParameterGenerator + .getInstance("DSA"); + try { + gen.init(spec); + fail("No expected InvalidAlgorithmParameterException"); + } catch (InvalidAlgorithmParameterException e) { + //expected + } } /** @@ -166,14 +172,19 @@ * java.security.SecureRandom) */ public void test_initLjava_security_spec_AlgorithmParameterSpecLjava_security_SecureRandom() throws Exception { - // Test for method void - // java.security.AlgorithmParameterGenerator.init(java.security.spec.AlgorithmParameterSpec, - // java.security.SecureRandom) - // checks that no exception is thrown - DSAParameterSpec spec = new DSAParameterSpec(BigInteger.ONE, - BigInteger.ONE, BigInteger.ONE); - AlgorithmParameterGenerator gen = AlgorithmParameterGenerator - .getInstance("DSA"); - gen.init(spec, new SecureRandom()); + // Test for method void + // java.security.AlgorithmParameterGenerator.init(java.security.spec.AlgorithmParameterSpec, + // java.security.SecureRandom) + // checks that InvalidAlgorithmParameterException is thrown + DSAParameterSpec spec = new DSAParameterSpec(BigInteger.ONE, + BigInteger.ONE, BigInteger.ONE); + AlgorithmParameterGenerator gen = AlgorithmParameterGenerator + .getInstance("DSA"); + try { + gen.init(spec, new SecureRandom()); + fail("No expected InvalidAlgorithmParameterException"); + } catch (InvalidAlgorithmParameterException e) { + //expected + } } -} \ No newline at end of file +} Index: modules/security/src/main/java/common/java/security/Security.java =================================================================== --- modules/security/src/main/java/common/java/security/Security.java (revision 398875) +++ modules/security/src/main/java/common/java/security/Security.java (working copy) @@ -306,10 +306,18 @@ "The fillter is not in the required format"); } attribute = key.substring(i + 1); + if (attribute.trim().length() == 0) { + throw new InvalidParameterException( + "The fillter is not in the required format"); + } key = key.substring(0, i); } String serv = key.substring(0, j); String alg = key.substring(j + 1); + if (serv.length() == 0 || alg.length() == 0) { + throw new InvalidParameterException( + "The fillter is not in the required format"); + } Provider p; for (int k = 0; k < result.size(); k++) { try {