1/*
2 *  Licensed to the Apache Software Foundation (ASF) under one or more
3 *  contributor license agreements.  See the NOTICE file distributed with
4 *  this work for additional information regarding copyright ownership.
5 *  The ASF licenses this file to You under the Apache License, Version 2.0
6 *  (the "License"); you may not use this file except in compliance with
7 *  the License.  You may obtain a copy of the License at
8 *
9 *     http://www.apache.org/licenses/LICENSE-2.0
10 *
11 *  Unless required by applicable law or agreed to in writing, software
12 *  distributed under the License is distributed on an "AS IS" BASIS,
13 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 *  See the License for the specific language governing permissions and
15 *  limitations under the License.
16 */
17
18/**
19* @author Vladimir N. Molotkov
20*/
21
22package org.apache.harmony.crypto.tests.javax.crypto;
23
24import java.io.IOException;
25import java.security.AlgorithmParameters;
26import java.security.NoSuchAlgorithmException;
27
28import javax.crypto.EncryptedPrivateKeyInfo;
29
30import org.apache.harmony.crypto.tests.support.EncryptedPrivateKeyInfoData;
31
32import junit.framework.TestCase;
33
34/**
35 * Test for EncryptedPrivateKeyInfo class.
36 *
37 * All binary data for this test were generated using
38 * BEA JRockit j2sdk1.4.2_04 (http://www.bea.com) with
39 * security providers list extended by Bouncy Castle's one
40 * (http://www.bouncycastle.org)
41 */
42public class EncryptedPrivateKeyInfo_ImplTest extends TestCase {
43
44    /**
45     * Test #1 for <code>getAlgName()</code> method <br>
46     * Assertion: Returns the encryption algorithm name <br>
47     * Test preconditions: test object created using ctor which takes encoded
48     * form as the only parameter <br>
49     * Expected: corresponding algorithm name must be returned
50     *
51     * @throws IOException
52     */
53    public final void testGetAlgName01() throws IOException {
54        boolean performed = false;
55        for (int i = 0; i < EncryptedPrivateKeyInfoData.algName0.length; i++) {
56            try {
57                EncryptedPrivateKeyInfo epki = new EncryptedPrivateKeyInfo(
58                        EncryptedPrivateKeyInfoData
59                                .getValidEncryptedPrivateKeyInfoEncoding(
60                                        EncryptedPrivateKeyInfoData.algName0[i][0]));
61                assertEquals(EncryptedPrivateKeyInfoData.algName0[i][1], epki
62                        .getAlgName());
63                performed = true;
64            } catch (NoSuchAlgorithmException allowed) {
65            }
66        }
67        assertTrue("Test not performed", performed);
68    }
69
70    /**
71     * Test #2 for <code>getAlgName()</code> method <br>
72     * Assertion: Returns the encryption algorithm name <br>
73     * Test preconditions: test object created using ctor which takes algorithm
74     * name and encrypted data as a parameters <br>
75     * Expected: corresponding algorithm name must be returned
76     *
77     * @throws IOException
78     */
79    public final void testGetAlgName02() {
80        boolean performed = false;
81        for (int i = 0; i < EncryptedPrivateKeyInfoData.algName0.length; i++) {
82            try {
83                EncryptedPrivateKeyInfo epki = new EncryptedPrivateKeyInfo(
84                        EncryptedPrivateKeyInfoData.algName0[i][0],
85                        EncryptedPrivateKeyInfoData.encryptedData);
86                assertEquals(EncryptedPrivateKeyInfoData.algName0[i][1], epki
87                        .getAlgName());
88                performed = true;
89            } catch (NoSuchAlgorithmException allowedFailure) {
90            }
91        }
92        assertTrue("Test not performed", performed);
93    }
94
95    /**
96     * Test #3 for <code>getAlgName()</code> method <br>
97     * Assertion: Returns the encryption algorithm name <br>
98     * Test preconditions: test object created using ctor which takes
99     * AlgorithmParameters and encrypted data as a parameters <br>
100     * Expected: corresponding algorithm name must be returned
101     *
102     * @throws IOException
103     */
104    public final void testGetAlgName03() throws IOException {
105        boolean performed = false;
106        for (int i = 0; i < EncryptedPrivateKeyInfoData.algName0.length; i++) {
107            try {
108                AlgorithmParameters ap = AlgorithmParameters
109                        .getInstance(EncryptedPrivateKeyInfoData.algName0[i][0]);
110                // use pregenerated AlgorithmParameters encodings
111                ap.init(EncryptedPrivateKeyInfoData.getParametersEncoding(
112                        EncryptedPrivateKeyInfoData.algName0[i][0]));
113                EncryptedPrivateKeyInfo epki = new EncryptedPrivateKeyInfo(ap,
114                        EncryptedPrivateKeyInfoData.encryptedData);
115                assertEquals(EncryptedPrivateKeyInfoData.algName0[i][1], epki
116                        .getAlgName());
117                performed = true;
118            } catch (NoSuchAlgorithmException allowedFailure) {
119            }
120        }
121        assertTrue("Test not performed", performed);
122    }
123}
124