EncodedKeySpec2Test.java revision 561ee011997c6c2f1befbfaa9d5f0a99771c1d63
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
18package org.apache.harmony.security.tests.java.security.spec;
19
20import java.security.Key;
21import java.security.KeyFactory;
22import java.security.KeyPair;
23import java.security.KeyPairGenerator;
24import java.security.interfaces.DSAPrivateKey;
25import java.security.interfaces.DSAPublicKey;
26import java.security.spec.PKCS8EncodedKeySpec;
27import java.security.spec.X509EncodedKeySpec;
28
29import junit.framework.TestCase;
30
31public class EncodedKeySpec2Test extends TestCase {
32
33	/**
34	 * @tests java.security.spec.EncodedKeySpec#getEncoded()
35	 */
36	public void test_getEncoded() throws Exception {
37
38       		KeyPairGenerator keyGen = KeyPairGenerator.getInstance("DSA");
39
40       		keyGen.initialize(1024);
41       		KeyPair keys = keyGen.generateKeyPair();
42
43
44       		KeyFactory fact = KeyFactory.getInstance("DSA");
45
46
47       		// check public key encoding
48       		byte[] encoded = keys.getPublic().getEncoded();
49       		Key key = fact.generatePublic(new X509EncodedKeySpec(encoded));
50
51       		assertTrue("public key encodings were different",
52       					isEqual(key, keys.getPublic()));
53
54       		// check private key encoding
55       		encoded = keys.getPrivate().getEncoded();
56       		key = fact.generatePrivate(new PKCS8EncodedKeySpec(encoded));
57
58       		assertTrue("private key encodings were different",
59       					isEqual(key, keys.getPrivate()));
60	}
61
62	private boolean isEqual(Key key1, Key key2) {
63		if (key1 instanceof DSAPublicKey && key2 instanceof DSAPublicKey) {
64			DSAPublicKey dsa1 = ((DSAPublicKey) key1);
65			DSAPublicKey dsa2 = ((DSAPublicKey) key2);
66			return dsa1.getY().equals(dsa2.getY())
67					&& dsa1.getParams().getG().equals(dsa2.getParams().getG())
68					&& dsa1.getParams().getP().equals(dsa2.getParams().getP())
69					&& dsa1.getParams().getQ().equals(dsa2.getParams().getQ());
70
71		} else if (key1 instanceof DSAPrivateKey
72				&& key2 instanceof DSAPrivateKey) {
73			DSAPrivateKey dsa1 = ((DSAPrivateKey) key1);
74			DSAPrivateKey dsa2 = ((DSAPrivateKey) key2);
75			return dsa1.getX().equals(dsa2.getX())
76					&& dsa1.getParams().getG().equals(dsa2.getParams().getG())
77					&& dsa1.getParams().getP().equals(dsa2.getParams().getP())
78					&& dsa1.getParams().getQ().equals(dsa2.getParams().getQ());
79		} else {
80			return false;
81		}
82	}
83}