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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 package tests.security.spec;
20 import dalvik.annotation.TestTargets;
21 import dalvik.annotation.TestLevel;
22 import dalvik.annotation.TestTargetNew;
23 import dalvik.annotation.TestTargetClass;
25 import junit.framework.TestCase;
27 import java.security.Key;
28 import java.security.KeyFactory;
29 import java.security.KeyPair;
30 import java.security.KeyPairGenerator;
31 import java.security.interfaces.DSAPrivateKey;
32 import java.security.interfaces.DSAPublicKey;
33 import java.security.spec.EncodedKeySpec;
34 import java.security.spec.PKCS8EncodedKeySpec;
35 import java.security.spec.X509EncodedKeySpec;
37 @TestTargetClass(EncodedKeySpec.class)
38 public class EncodedKeySpec2Test extends TestCase {
41 * @tests java.security.spec.EncodedKeySpec#getEncoded()
44 level = TestLevel.COMPLETE,
46 method = "getEncoded",
49 public void test_getEncoded() throws Exception {
51 KeyPairGenerator keyGen = KeyPairGenerator.getInstance("DSA");
53 keyGen.initialize(1024);
54 KeyPair keys = keyGen.generateKeyPair();
57 KeyFactory fact = KeyFactory.getInstance("DSA");
60 // check public key encoding
61 byte[] encoded = keys.getPublic().getEncoded();
62 Key key = fact.generatePublic(new X509EncodedKeySpec(encoded));
64 assertTrue("public key encodings were different",
65 isEqual(key, keys.getPublic()));
67 // check private key encoding
68 encoded = keys.getPrivate().getEncoded();
69 key = fact.generatePrivate(new PKCS8EncodedKeySpec(encoded));
71 assertTrue("private key encodings were different",
72 isEqual(key, keys.getPrivate()));
75 private boolean isEqual(Key key1, Key key2) {
76 if (key1 instanceof DSAPublicKey && key2 instanceof DSAPublicKey) {
77 DSAPublicKey dsa1 = ((DSAPublicKey) key1);
78 DSAPublicKey dsa2 = ((DSAPublicKey) key2);
79 return dsa1.getY().equals(dsa2.getY())
80 && dsa1.getParams().getG().equals(dsa2.getParams().getG())
81 && dsa1.getParams().getP().equals(dsa2.getParams().getP())
82 && dsa1.getParams().getQ().equals(dsa2.getParams().getQ());
84 } else if (key1 instanceof DSAPrivateKey
85 && key2 instanceof DSAPrivateKey) {
86 DSAPrivateKey dsa1 = ((DSAPrivateKey) key1);
87 DSAPrivateKey dsa2 = ((DSAPrivateKey) key2);
88 return dsa1.getX().equals(dsa2.getX())
89 && dsa1.getParams().getG().equals(dsa2.getParams().getG())
90 && dsa1.getParams().getP().equals(dsa2.getParams().getP())
91 && dsa1.getParams().getQ().equals(dsa2.getParams().getQ());