Index: modules/crypto/src/test/impl/java/org/apache/harmony/crypto/tests/javax/crypto/ExemptionMechanism_ImplTest.java =================================================================== --- modules/crypto/src/test/impl/java/org/apache/harmony/crypto/tests/javax/crypto/ExemptionMechanism_ImplTest.java (revision 427444) +++ modules/crypto/src/test/impl/java/org/apache/harmony/crypto/tests/javax/crypto/ExemptionMechanism_ImplTest.java (working copy) @@ -14,11 +14,6 @@ * limitations under the License. */ -/** -* @author Vera Y. Petrashkova -* @version $Revision$ -*/ - package org.apache.harmony.crypto.tests.javax.crypto; import java.security.AlgorithmParameters; @@ -42,18 +37,17 @@ import junit.framework.TestCase; - /** * Tests for ExemptionMechanism class constructors and methods * */ public class ExemptionMechanism_ImplTest extends TestCase { - + public static final String srvExemptionMechanism = "ExemptionMechanism"; - + private static final String defaultAlg = "EMech"; - + private static final String ExemptionMechanismProviderClass = "org.apache.harmony.crypto.tests.support.MyExemptionMechanismSpi"; private static final String[] invalidValues = SpiEngUtils.invalidValues; @@ -72,12 +66,13 @@ protected void setUp() throws Exception { super.setUp(); - mProv = (new SpiEngUtils()).new MyProvider("MyExMechProvider", "Provider for ExemptionMechanism testing", - srvExemptionMechanism.concat(".").concat(defaultAlg), + mProv = (new SpiEngUtils()).new MyProvider("MyExMechProvider", + "Provider for ExemptionMechanism testing", + srvExemptionMechanism.concat(".").concat(defaultAlg), ExemptionMechanismProviderClass); Security.insertProviderAt(mProv, 1); } - + /* * @see TestCase#tearDown() */ @@ -86,55 +81,40 @@ Security.removeProvider(mProv.getName()); } - private void checkResult(ExemptionMechanism exMech) + private void checkResult(ExemptionMechanism exMech) throws ExemptionMechanismException, ShortBufferException, - InvalidKeyException, InvalidAlgorithmParameterException { + InvalidKeyException, InvalidAlgorithmParameterException { Key key = new MyExemptionMechanismSpi().new tmpKey("Proba", new byte[0]); - byte [] emptyA = new byte[0]; + byte[] emptyA = new byte[0]; int len = MyExemptionMechanismSpi.getLength(); - byte [] byteA = new byte[len]; + byte[] byteA = new byte[len]; try { exMech.genExemptionBlob(); fail("IllegalStateException must be thrown"); - } catch (IllegalStateException e) { + } catch (IllegalStateException e) { } try { exMech.genExemptionBlob(byteA); fail("IllegalStateException must be thrown"); - } catch (IllegalStateException e) { + } catch (IllegalStateException e) { } try { exMech.genExemptionBlob(byteA, 1); fail("IllegalStateException must be thrown"); - } catch (IllegalStateException e) { - } + } catch (IllegalStateException e) { + } try { exMech.getOutputSize(0); fail("IllegalStateException must be thrown"); - } catch (IllegalStateException e) { - } - + } catch (IllegalStateException e) { + } + exMech.init(key); - byte [] bbRes = exMech.genExemptionBlob(); + byte[] bbRes = exMech.genExemptionBlob(); assertEquals("Incorrect length", bbRes.length, len); assertEquals("Incorrect result", exMech.genExemptionBlob(new byte[5]), 5); assertEquals("Incorrect result", exMech.genExemptionBlob(bbRes), len); - try { - exMech.genExemptionBlob(new byte[1], len); - fail("ShortBufferException must be thrown"); - } catch (ShortBufferException e) { - } - try { - exMech.genExemptionBlob(emptyA); - fail("ShortBufferException must be thrown"); - } catch (ShortBufferException e) { - } - - assertEquals("Incorrect result", exMech.genExemptionBlob(byteA, 1), 9); - assertEquals("Incorrect result", exMech.genExemptionBlob(new byte[20], (len - 2)), len); - - assertEquals("Incorrect output size", exMech.getOutputSize(100), 5); - + AlgorithmParameters params = null; exMech.init(key, params); AlgorithmParameterSpec parSpec = null; @@ -159,7 +139,7 @@ assertTrue("Empty message", (e.getMessage().length() > 0)); } } - + /** * Test for getInstance(String algorithm) method * Assertions: @@ -207,7 +187,7 @@ public void testGetInstance02() throws NoSuchAlgorithmException, NoSuchProviderException, IllegalArgumentException, ExemptionMechanismException, InvalidAlgorithmParameterException, - ShortBufferException, InvalidKeyException { + ShortBufferException, InvalidKeyException { try { ExemptionMechanism.getInstance(null, mProv.getName()); fail("NullPointerException or NoSuchAlgorithmException should be thrown if algorithm is null"); Index: modules/crypto/src/test/api/java/org/apache/harmony/crypto/tests/javax/crypto/ExemptionMechanismTest.java =================================================================== --- modules/crypto/src/test/api/java/org/apache/harmony/crypto/tests/javax/crypto/ExemptionMechanismTest.java (revision 427444) +++ modules/crypto/src/test/api/java/org/apache/harmony/crypto/tests/javax/crypto/ExemptionMechanismTest.java (working copy) @@ -14,25 +14,22 @@ * limitations under the License. */ -/** -* @author Vera Y. Petrashkova -* @version $Revision$ -*/ - package org.apache.harmony.crypto.tests.javax.crypto; import java.security.InvalidKeyException; +import java.security.Key; import java.security.NoSuchProviderException; import java.security.Provider; import javax.crypto.ExemptionMechanism; import javax.crypto.ExemptionMechanismSpi; -import junit.framework.TestCase; - import org.apache.harmony.crypto.tests.support.MyExemptionMechanismSpi; +import org.apache.harmony.crypto.tests.support.MyExemptionMechanismSpi.tmpKey; import org.apache.harmony.security.tests.support.SpiEngUtils; +import junit.framework.TestCase; + /** * Tests for ExemptionMechanism class constructors and methods * @@ -98,4 +95,64 @@ //expected } } + + /** + * Test for isCryptoAllowed(Key key) method + */ + public void testIsCryptoAllowed() throws Exception { + + //Regression for HARMONY-1029 + Provider mProv = (new SpiEngUtils()).new MyProvider("MyExMechProvider", + "Provider for ExemptionMechanism testing", + srvExemptionMechanism.concat(".").concat(defaultAlg), + ExemptionMechanismProviderClass); + + ExemptionMechanism em = new ExemptionMechanism( + new MyExemptionMechanismSpi(), mProv, defaultAlg) { + }; + + Key key = new MyExemptionMechanismSpi().new tmpKey("Proba", new byte[0]); + + assertFalse(em.isCryptoAllowed(key)); + + em.init(key); + assertFalse(em.isCryptoAllowed(key)); + + em.genExemptionBlob(); + assertTrue(em.isCryptoAllowed(key)); + + Key key1 = new MyExemptionMechanismSpi().new tmpKey("Proba", + new byte[] { 1 }); + assertFalse(em.isCryptoAllowed(key1)); + + em.init(key1); + assertFalse(em.isCryptoAllowed(key)); + } + + /** + * Test for genExemptionBlob((byte[] output, int outputOffset) method + */ + public void testGenExemptionBlob() throws Exception { + + //Regression for HARMONY-1029 + Provider mProv = (new SpiEngUtils()).new MyProvider("MyExMechProvider", + "Provider for ExemptionMechanism testing", + srvExemptionMechanism.concat(".").concat(defaultAlg), + ExemptionMechanismProviderClass); + + ExemptionMechanism em = new ExemptionMechanism( + new MyExemptionMechanismSpi(), mProv, defaultAlg) { + }; + + Key key = new MyExemptionMechanismSpi().new tmpKey("Proba", new byte[0]); + + em.init(key); + // ExemptionMechanism doesn't check parameters + // it is a responsibility of ExemptionMechanismSpi + em.genExemptionBlob(null, 0); + em.genExemptionBlob(new byte[0], 0); + em.genExemptionBlob(new byte[10], -5); + + } + } Index: modules/crypto/src/test/api/java.injected/javax/crypto/ExemptionMechanismSpiTest.java =================================================================== --- modules/crypto/src/test/api/java.injected/javax/crypto/ExemptionMechanismSpiTest.java (revision 427444) +++ modules/crypto/src/test/api/java.injected/javax/crypto/ExemptionMechanismSpiTest.java (working copy) @@ -62,7 +62,7 @@ assertEquals("Incorrect length", bbRes.length, len); assertEquals("Incorrect result", emSpi.engineGenExemptionBlob(new byte[1], len), len); - assertEquals("Incorrect output size", emSpi.engineGetOutputSize(100), 5); + assertEquals("Incorrect output size", 10, emSpi.engineGetOutputSize(100)); Key key = null; AlgorithmParameters params = null; AlgorithmParameterSpec parSpec = null; @@ -102,6 +102,6 @@ emSpi.engineInit(key, params); emSpi.engineInit(key, parSpec); - assertEquals("Incorrect result", emSpi.engineGetOutputSize(100),5); + assertEquals("Incorrect result", 10, emSpi.engineGetOutputSize(100)); } } Index: modules/crypto/src/test/support/common/java/org/apache/harmony/crypto/tests/support/MyExemptionMechanismSpi.java =================================================================== --- modules/crypto/src/test/support/common/java/org/apache/harmony/crypto/tests/support/MyExemptionMechanismSpi.java (revision 427444) +++ modules/crypto/src/test/support/common/java/org/apache/harmony/crypto/tests/support/MyExemptionMechanismSpi.java (working copy) @@ -39,7 +39,7 @@ public class MyExemptionMechanismSpi extends ExemptionMechanismSpi { - private static final int byteArrayLength = 10; + private static final int byteArrayLength = 5; public static final int getLength() { return byteArrayLength; @@ -55,7 +55,7 @@ } protected int engineGetOutputSize(int inputLen) { - return 5; + return 10; } protected void engineInit(Key key) throws InvalidKeyException, Index: modules/crypto/src/main/java/javax/crypto/ExemptionMechanism.java =================================================================== --- modules/crypto/src/main/java/javax/crypto/ExemptionMechanism.java (revision 427444) +++ modules/crypto/src/main/java/javax/crypto/ExemptionMechanism.java (working copy) @@ -14,11 +14,6 @@ * limitations under the License. */ -/** -* @author Vera Y. Petrashkova -* @version $Revision$ -*/ - package javax.crypto; import java.security.AlgorithmParameters; @@ -30,14 +25,10 @@ import java.security.Provider; import java.security.Security; import java.security.spec.AlgorithmParameterSpec; +import java.util.Arrays; import org.apache.harmony.security.fortress.Engine; - -/** - * @com.intel.drl.spec_ref - * - */ public class ExemptionMechanism { // Store spi implementation service name @@ -64,10 +55,9 @@ // Store initKey value private Key initKey; - /** - * @com.intel.drl.spec_ref - * - */ + // Indicates if blob generated successfully + private boolean generated; + protected ExemptionMechanism(ExemptionMechanismSpi exmechSpi, Provider provider, String mechanism) { this.mechanism = mechanism; @@ -76,18 +66,10 @@ isInit = false; } - /** - * @com.intel.drl.spec_ref - * - */ public final String getName() { return mechanism; } - /** - * @com.intel.drl.spec_ref - * - */ public static final ExemptionMechanism getInstance(String algorithm) throws NoSuchAlgorithmException { if (algorithm == null) { @@ -100,10 +82,6 @@ } } - /** - * @com.intel.drl.spec_ref - * - */ public static final ExemptionMechanism getInstance(String algorithm, String provider) throws NoSuchAlgorithmException, NoSuchProviderException { @@ -120,10 +98,6 @@ return getInstance(algorithm, impProvider); } - /** - * @com.intel.drl.spec_ref - * - */ public static final ExemptionMechanism getInstance(String algorithm, Provider provider) throws NoSuchAlgorithmException { if (algorithm == null) { @@ -139,41 +113,21 @@ } } - /** - * @com.intel.drl.spec_ref - * - */ public final Provider getProvider() { return provider; } - /** - * @com.intel.drl.spec_ref - * - * FIXME: check this method - */ public final boolean isCryptoAllowed(Key key) throws ExemptionMechanismException { - Key initK = initKey; - try { - initKey = key; - spiImpl.engineInit(key); - spiImpl.engineGenExemptionBlob(); - if (initK != null) { - spiImpl.engineInit(initK); - } + + if (generated + && (initKey.equals(key) || Arrays.equals(initKey.getEncoded(), + key.getEncoded()))) { return true; - } catch (InvalidKeyException e) { - return false; - } finally { - initKey = initK; } + return false; } - /** - * @com.intel.drl.spec_ref - * - */ public final int getOutputSize(int inputLen) throws IllegalStateException { if (!isInit) { throw new IllegalStateException(NOTINITEMECH); @@ -181,115 +135,62 @@ return spiImpl.engineGetOutputSize(inputLen); } - /** - * @com.intel.drl.spec_ref - * - */ public final void init(Key key) throws InvalidKeyException, ExemptionMechanismException { + generated = false; spiImpl.engineInit(key); initKey = key; isInit = true; } - /** - * @com.intel.drl.spec_ref - * - */ public final void init(Key key, AlgorithmParameters param) throws InvalidKeyException, InvalidAlgorithmParameterException, ExemptionMechanismException { + generated = false; spiImpl.engineInit(key, param); initKey = key; isInit = true; } - /** - * @com.intel.drl.spec_ref - * - */ public final void init(Key key, AlgorithmParameterSpec param) throws InvalidKeyException, InvalidAlgorithmParameterException, ExemptionMechanismException { + generated = false; spiImpl.engineInit(key, param); initKey = key; isInit = true; } - /** - * @com.intel.drl.spec_ref - * - */ public final byte[] genExemptionBlob() throws IllegalStateException, ExemptionMechanismException { if (!isInit) { throw new IllegalStateException(NOTINITEMECH); } - return spiImpl.engineGenExemptionBlob(); + generated = false; + byte[] result = spiImpl.engineGenExemptionBlob(); + generated = true; + return result; } - /** - * @com.intel.drl.spec_ref - * - * FIXME: about ShortBufferException (output: null, byte[0],... - */ public final int genExemptionBlob(byte[] output) throws IllegalStateException, ShortBufferException, ExemptionMechanismException { - if (!isInit) { - throw new IllegalStateException(NOTINITEMECH); - } - byte[] result = spiImpl.engineGenExemptionBlob(); - if (output.length == 0) { - throw new ShortBufferException( - "Output buffer is too small to hold the result"); - } - int len = (result.length < output.length ? result.length - : output.length); - for (int i = 0; i < len; i++) { - output[i] = result[i]; - } - return len; + return genExemptionBlob(output, 0); } - /** - * @com.intel.drl.spec_ref - * - * - * FIXME: about ShortBufferException (output: null, byte[0],... and - * outputOffset - */ public final int genExemptionBlob(byte[] output, int outputOffset) throws IllegalStateException, ShortBufferException, ExemptionMechanismException { if (!isInit) { throw new IllegalStateException(NOTINITEMECH); } - byte[] result = spiImpl.engineGenExemptionBlob(); - if ((output.length == 0) || (outputOffset < 0) - || (outputOffset >= output.length)) { - throw new ShortBufferException( - "Output buffer is too small to hold the result"); - } - int len = (result.length < (output.length - outputOffset) ? result.length - : (output.length - outputOffset)); - for (int i = 0; i < len; i++) { - output[i] = result[i]; - } + generated = false; + int len = spiImpl.engineGenExemptionBlob(output, outputOffset); + generated = true; return len; } - /** - * @com.intel.drl.spec_ref - * - * FIXME: check this method - */ protected void finalize() { - try { - initKey = null; - super.finalize(); - } catch (Throwable e) { - new RuntimeException(e); - } + initKey = null; } } \ No newline at end of file