RSAMultiPrimePrivateCrtKeyTest.java revision adc854b798c1cfe3bfd4c27d68d5cee38ca617da
1/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package tests.security.interfaces;
18
19import dalvik.annotation.TestTargets;
20import dalvik.annotation.TestLevel;
21import dalvik.annotation.TestTargetNew;
22import dalvik.annotation.TestTargetClass;
23
24import junit.framework.TestCase;
25
26import java.math.BigInteger;
27import java.security.interfaces.RSAMultiPrimePrivateCrtKey;
28import java.security.spec.RSAOtherPrimeInfo;
29
30import org.apache.harmony.security.tests.support.interfaces.RSAMultiPrimePrivateCrtKeyImpl;
31
32@TestTargetClass(RSAMultiPrimePrivateCrtKey.class)
33public class RSAMultiPrimePrivateCrtKeyTest extends TestCase {
34
35    /**
36     * Reference array of RSAOtherPrimeInfo. DO NOT MODIFY
37     */
38    private static final RSAOtherPrimeInfo[] opi = new RSAOtherPrimeInfo[] {
39            new RSAOtherPrimeInfo(BigInteger.ONE, BigInteger.ONE, BigInteger.ONE),
40            new RSAOtherPrimeInfo(BigInteger.ONE, BigInteger.ONE, BigInteger.ONE),
41            new RSAOtherPrimeInfo(BigInteger.ONE, BigInteger.ONE, BigInteger.ONE)
42    };
43
44    private final BigInteger publicExponent = BigInteger.ONE;
45    private final BigInteger primeExponentP = BigInteger.ONE;
46    private final BigInteger primeExponentQ = BigInteger.ONE;
47    private final BigInteger primeP = BigInteger.ONE;
48    private final BigInteger primeQ = BigInteger.ONE;
49    private final BigInteger crtCoefficient = BigInteger.ONE;
50
51    class RSAMulti extends RSAMultiPrimePrivateCrtKeyImpl {
52        public RSAMulti(BigInteger publicExp,
53                        BigInteger primeExpP,
54                        BigInteger primeExpQ,
55                        BigInteger prP,
56                        BigInteger prQ,
57                        BigInteger crtCft,
58                        RSAOtherPrimeInfo[] otherPrmInfo) {
59            super(publicExp, primeExpP, primeExpQ, prP, prQ, crtCft, otherPrmInfo);
60        }
61    }
62
63    /**
64     * @tests java.security.interfaces.RSAMultiPrimePrivateCrtKey#getCrtCoefficient()
65     * @tests java.security.interfaces.RSAMultiPrimePrivateCrtKey#getPrimeExponentP()
66     * @tests java.security.interfaces.RSAMultiPrimePrivateCrtKey#getPrimeExponentQ()
67     * @tests java.security.interfaces.RSAMultiPrimePrivateCrtKey#getPrimeP()
68     * @tests java.security.interfaces.RSAMultiPrimePrivateCrtKey#getPrimeQ()
69     * @tests java.security.interfaces.RSAMultiPrimePrivateCrtKey#getPublicExponent()
70     */
71    @TestTargets({
72        @TestTargetNew(
73            level = TestLevel.COMPLETE,
74            notes = "",
75            method = "getCrtCoefficient",
76            args = {}
77        ),
78        @TestTargetNew(
79            level = TestLevel.COMPLETE,
80            notes = "",
81            method = "getPrimeExponentP",
82            args = {}
83        ),
84        @TestTargetNew(
85            level = TestLevel.COMPLETE,
86            notes = "",
87            method = "getPrimeExponentQ",
88            args = {}
89        ),
90        @TestTargetNew(
91            level = TestLevel.COMPLETE,
92            notes = "",
93            method = "getPrimeP",
94            args = {}
95        ),
96        @TestTargetNew(
97            level = TestLevel.COMPLETE,
98            notes = "",
99            method = "getPrimeQ",
100            args = {}
101        ),
102        @TestTargetNew(
103            level = TestLevel.COMPLETE,
104            notes = "",
105            method = "getPublicExponent",
106            args = {}
107        )
108    })
109    public void test_RSAMultiPrimePrivateCrtKey() {
110        RSAMulti rsam = new RSAMulti(publicExponent, primeExponentP, primeExponentQ,
111                                     primeP, primeQ, crtCoefficient, opi);
112        try {
113            assertEquals(rsam.getCrtCoefficient(), crtCoefficient);
114            assertEquals(rsam.getPrimeExponentP(), primeExponentP);
115            assertEquals(rsam.getPrimeExponentQ(), primeExponentQ);
116            assertEquals(rsam.getPrimeP(), primeP);
117            assertEquals(rsam.getPrimeQ(), primeQ);
118            assertEquals(rsam.getPublicExponent(), publicExponent);
119        } catch (Exception e) {
120            fail("Unexpected exception: " + e);
121        }
122    }
123
124    /**
125     * @tests java.security.interfaces.RSAMultiPrimePrivateCrtKey#getOtherPrimeInfo()
126     */
127    @TestTargetNew(
128        level = TestLevel.COMPLETE,
129        notes = "",
130        method = "getOtherPrimeInfo",
131        args = {}
132    )
133    public void test_getOtherPrimeInfo() {
134        RSAMulti rsam = new RSAMulti(publicExponent, primeExponentP, primeExponentQ,
135                                     primeP, primeQ, crtCoefficient, null);
136        try {
137            assertNull("Object RSAOtherPrimeInfo is not NULL", rsam.getOtherPrimeInfo());
138        } catch (Exception e) {
139            fail("Unexpected exception: " + e);
140        }
141        rsam = new RSAMulti(publicExponent, primeExponentP, primeExponentQ,
142                            primeP, primeQ, crtCoefficient, opi);
143
144        try {
145            assertEquals(rsam.getOtherPrimeInfo(), opi);
146        } catch (Exception e) {
147            fail("Unexpected exception: " + e);
148        }
149    }
150}