Search in sources :

Example 31 with AlgorithmParameters

use of java.security.AlgorithmParameters in project j2objc by google.

the class AlgorithmParametersTest method test_getParameterSpecLjava_lang_Class.

/**
     * java.security.AlgorithmParameters#getParameterSpec(Class)
     */
public void test_getParameterSpecLjava_lang_Class() throws Exception {
    final MyAlgorithmParameterSpec myParamSpec = new MyAlgorithmParameterSpec();
    MyAlgorithmParameters paramSpi = new MyAlgorithmParameters() {

        protected AlgorithmParameterSpec engineGetParameterSpec(Class paramSpec) {
            return myParamSpec;
        }
    };
    AlgorithmParameters params = new DummyAlgorithmParameters(paramSpi, p, "algorithm");
    //
    try {
        params.getParameterSpec(null);
        fail("No expected InvalidParameterSpecException");
    } catch (InvalidParameterSpecException e) {
    // expected
    }
    try {
        params.getParameterSpec(MyAlgorithmParameterSpec.class);
        fail("No expected InvalidParameterSpecException");
    } catch (InvalidParameterSpecException e) {
    // expected
    }
    //
    // test: corresponding spi method is invoked
    //
    params.init(new MyAlgorithmParameterSpec());
    assertSame(myParamSpec, params.getParameterSpec(MyAlgorithmParameterSpec.class));
    //
    // test: if paramSpec is null
    // Regression test for HARMONY-2733
    //
    paramSpi = new MyAlgorithmParameters() {

        protected AlgorithmParameterSpec engineGetParameterSpec(Class paramSpec) {
            // null is passed to spi-provider
            assertNull(paramSpec);
            return null;
        }
    };
    params = new DummyAlgorithmParameters(paramSpi, p, "algorithm");
    params.init(new MyAlgorithmParameterSpec());
    assertNull(params.getParameterSpec(null));
}
Also used : InvalidParameterSpecException(java.security.spec.InvalidParameterSpecException) AlgorithmParameterSpec(java.security.spec.AlgorithmParameterSpec) AlgorithmParameters(java.security.AlgorithmParameters)

Example 32 with AlgorithmParameters

use of java.security.AlgorithmParameters in project j2objc by google.

the class AlgorithmParametersTest method test_getInstanceLjava_lang_StringLjava_security_Provider.

/**
     * java.security.AlgorithmParameters#getInstance(String, Provider)
     */
public void test_getInstanceLjava_lang_StringLjava_security_Provider() {
    String[] alg = { "", "qwertyu", "!@#$%^&*()" };
    Provider pp = null;
    try {
        AlgorithmParameters ap = AlgorithmParameters.getInstance("ABC", p);
        checkUnititialized(ap);
        ap.init(new byte[6], "aaa");
        checkAP(ap, p);
    } catch (Exception e) {
        fail("Unexpected exception");
    }
    for (int i = 0; i < alg.length; i++) {
        try {
            AlgorithmParameters ap = AlgorithmParameters.getInstance(alg[i], p);
            fail("NoSuchAlgorithmException was not thrown for parameter " + alg[i]);
        } catch (NoSuchAlgorithmException nsae) {
        //expected
        } catch (Exception e) {
            fail("Incorrect exception " + e + " was thrown for " + alg[i]);
        }
    }
    try {
        AlgorithmParameters ap = AlgorithmParameters.getInstance("ABC", pp);
        fail("IllegalArgumentException was not thrown for NULL provider");
    } catch (IllegalArgumentException iae) {
    //expected
    } catch (Exception e) {
        fail("Incorrect exception " + e + " was thrown");
    }
}
Also used : NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) IOException(java.io.IOException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidParameterSpecException(java.security.spec.InvalidParameterSpecException) NoSuchProviderException(java.security.NoSuchProviderException) Provider(java.security.Provider) AlgorithmParameters(java.security.AlgorithmParameters)

Example 33 with AlgorithmParameters

use of java.security.AlgorithmParameters in project j2objc by google.

the class AlgorithmParametersTest method test_getEncodedLjava_lang_String.

/**
     * java.security.AlgorithmParameters#getEncoded(String)
     */
public void test_getEncodedLjava_lang_String() throws Exception {
    final byte[] enc = new byte[] { 0x02, 0x01, 0x03 };
    final String strFormatParam = "format";
    MyAlgorithmParameters paramSpi = new MyAlgorithmParameters() {

        protected byte[] engineGetEncoded(String format) throws IOException {
            assertEquals(strFormatParam, format);
            return enc;
        }
    };
    AlgorithmParameters params = new DummyAlgorithmParameters(paramSpi, p, "algorithm");
    //
    try {
        params.getEncoded(strFormatParam);
        fail("should not get encoded from un-initialized instance");
    } catch (IOException e) {
    // expected
    }
    //
    // test: corresponding spi method is invoked
    //
    params.init(new MyAlgorithmParameterSpec());
    assertSame(enc, params.getEncoded(strFormatParam));
    //
    // test: if format param is null
    // Regression test for HARMONY-2680
    //
    paramSpi = new MyAlgorithmParameters() {

        protected byte[] engineGetEncoded(String format) throws IOException {
            // null is passed to spi-provider
            assertNull(format);
            return enc;
        }
    };
    params = new DummyAlgorithmParameters(paramSpi, p, "algorithm");
    params.init(new MyAlgorithmParameterSpec());
    assertSame(enc, params.getEncoded(null));
}
Also used : IOException(java.io.IOException) AlgorithmParameters(java.security.AlgorithmParameters)

Example 34 with AlgorithmParameters

use of java.security.AlgorithmParameters in project j2objc by google.

the class AlgorithmParametersTest method test_getInstanceLjava_lang_StringLjava_lang_String.

/**
     * java.security.AlgorithmParameters#getInstance(String, String)
     */
public void test_getInstanceLjava_lang_StringLjava_lang_String() {
    String[] alg = { "", "qwertyu", "!@#$%^&*()" };
    String[] prv = { "", null };
    String[] prv1 = { "1234567890", "qwertyu", "!@#$%^&*()" };
    try {
        AlgorithmParameters ap = AlgorithmParameters.getInstance("ABC", "MyProvider");
        checkUnititialized(ap);
        ap.init(new byte[6]);
        checkAP(ap, p);
    } catch (Exception e) {
        fail("Unexpected exception");
    }
    for (int i = 0; i < alg.length; i++) {
        try {
            AlgorithmParameters ap = AlgorithmParameters.getInstance(alg[i], "MyProvider");
            fail("NoSuchAlgorithmException was not thrown for parameter " + alg[i]);
        } catch (NoSuchAlgorithmException nsae) {
        //expected
        } catch (Exception e) {
            fail("Incorrect exception " + e + " was thrown for " + alg[i]);
        }
    }
    for (int i = 0; i < prv.length; i++) {
        try {
            AlgorithmParameters ap = AlgorithmParameters.getInstance("ABC", prv[i]);
            fail("IllegalArgumentException was not thrown for parameter " + prv[i]);
        } catch (IllegalArgumentException iae) {
        //expected
        } catch (Exception e) {
            fail("Incorrect exception " + e + " was thrown for " + prv[i]);
        }
    }
    for (int i = 0; i < prv1.length; i++) {
        try {
            AlgorithmParameters ap = AlgorithmParameters.getInstance("ABC", prv1[i]);
            fail("NoSuchProviderException was not thrown for parameter " + prv1[i]);
        } catch (NoSuchProviderException nspe) {
        //expected
        } catch (Exception e) {
            fail("Incorrect exception " + e + " was thrown for " + prv1[i]);
        }
    }
}
Also used : NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) NoSuchProviderException(java.security.NoSuchProviderException) IOException(java.io.IOException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidParameterSpecException(java.security.spec.InvalidParameterSpecException) NoSuchProviderException(java.security.NoSuchProviderException) AlgorithmParameters(java.security.AlgorithmParameters)

Example 35 with AlgorithmParameters

use of java.security.AlgorithmParameters in project j2objc by google.

the class AlgorithmParametersTest method test_getInstanceLjava_lang_String.

/**
     * java.security.AlgorithmParameters#getInstance(String)
     */
public void test_getInstanceLjava_lang_String() {
    String[] str = { "", "qwertyu", "!@#$%^&*()" };
    try {
        AlgorithmParameters ap = AlgorithmParameters.getInstance("ABC");
        checkUnititialized(ap);
        ap.init(new MyAlgorithmParameterSpec());
        checkAP(ap, p);
    } catch (Exception e) {
        fail("Unexpected exception");
    }
    for (int i = 0; i < str.length; i++) {
        try {
            AlgorithmParameters ap = AlgorithmParameters.getInstance(str[i]);
            fail("NoSuchAlgorithmException was not thrown for parameter " + str[i]);
        } catch (NoSuchAlgorithmException nsae) {
        //expected
        }
    }
}
Also used : NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) IOException(java.io.IOException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidParameterSpecException(java.security.spec.InvalidParameterSpecException) NoSuchProviderException(java.security.NoSuchProviderException) AlgorithmParameters(java.security.AlgorithmParameters)

Aggregations

AlgorithmParameters (java.security.AlgorithmParameters)107 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)39 IOException (java.io.IOException)31 InvalidParameterSpecException (java.security.spec.InvalidParameterSpecException)22 Cipher (javax.crypto.Cipher)22 SecretKey (javax.crypto.SecretKey)18 AlgorithmParameterSpec (java.security.spec.AlgorithmParameterSpec)13 NoSuchProviderException (java.security.NoSuchProviderException)12 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)11 Key (java.security.Key)11 SecureRandom (java.security.SecureRandom)10 EncryptedPrivateKeyInfo (javax.crypto.EncryptedPrivateKeyInfo)10 InvalidKeyException (java.security.InvalidKeyException)8 PBEParameterSpec (javax.crypto.spec.PBEParameterSpec)8 UnrecoverableKeyException (java.security.UnrecoverableKeyException)7 KeyPair (java.security.KeyPair)6 KeyPairGenerator (java.security.KeyPairGenerator)6 AlgorithmId (sun.security.x509.AlgorithmId)6 Nullable (android.annotation.Nullable)5 Asn1Integer (com.android.hotspot2.asn1.Asn1Integer)5