DHPrivateKeyTest.java revision f33eae7e84eb6d3b0f4e86b59605bb3de73009f3
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 Vera Y. Petrashkova
20* @version $Revision$
21*/
22
23package org.apache.harmony.crypto.tests.javax.crypto.interfaces;
24
25import java.math.BigInteger;
26import java.security.KeyPair;
27import java.security.KeyPairGenerator;
28
29import javax.crypto.interfaces.DHKey;
30import javax.crypto.interfaces.DHPrivateKey;
31import javax.crypto.spec.DHParameterSpec;
32
33import junit.framework.TestCase;
34import dalvik.annotation.TestLevel;
35import dalvik.annotation.TestTargetClass;
36import dalvik.annotation.TestTargetNew;
37import dalvik.annotation.TestTargets;
38import dalvik.annotation.BrokenTest;
39
40
41/**
42 * Tests for <code>DHPrivateKey</code> class field
43 *
44 */
45@TestTargetClass(DHPrivateKey.class)
46public class DHPrivateKeyTest extends TestCase {
47
48    /**
49     * Test for <code>serialVersionUID</code> field
50     */
51    @TestTargetNew(
52        level = TestLevel.COMPLETE,
53        notes = "tests serialVersionUID for a fixed value",
54        method = "!field:serialVersionUID"
55    )
56    public void testField() {
57        checkDHPrivateKey key = new checkDHPrivateKey();
58        assertEquals("Incorrect serialVersionUID",
59                key.getSerVerUID(), //DHPrivateKey.serialVersionUID
60                2211791113380396553L);
61    }
62
63@TestTargets({
64    @TestTargetNew(
65          level = TestLevel.COMPLETE,
66          method = "getX",
67          args = {}
68        ),
69    @TestTargetNew(
70          level = TestLevel.COMPLETE,
71          clazz = DHKey.class,
72          method = "getParams",
73          args = {}
74        )
75    })
76    @BrokenTest("Too slow - disabling for now")
77    public void test_getParams() throws Exception {
78        KeyPairGenerator kg = KeyPairGenerator.getInstance("DH");
79        kg.initialize(1024);
80        KeyPair kp1 = kg.genKeyPair();
81        KeyPair kp2 = kg.genKeyPair();
82        DHPrivateKey pk1 = (DHPrivateKey) kp1.getPrivate();
83        DHPrivateKey pk2 = (DHPrivateKey) kp2.getPrivate();
84
85        assertTrue(pk1.getX().getClass().getCanonicalName().equals("java.math.BigInteger"));
86        assertTrue(pk1.getParams().getClass().getCanonicalName().equals("javax.crypto.spec.DHParameterSpec"));
87        assertFalse(pk1.equals(pk2));
88        assertTrue(pk1.getX().equals(pk1.getX()));
89    }
90
91    public class checkDHPrivateKey implements DHPrivateKey {
92        public String getAlgorithm() {
93            return "SecretKey";
94        }
95        public String getFormat() {
96            return "Format";
97        }
98        public byte[] getEncoded() {
99            return new byte[0];
100        }
101        public long getSerVerUID() {
102            return serialVersionUID;
103        }
104        public BigInteger getX() {
105            return null;
106        }
107        public DHParameterSpec getParams() {
108            return null;
109        }
110    }
111}
112