OSDN Git Service

original
[gb-231r1-is01/Gingerbread_2.3.3_r1_IS01.git] / libcore / luni / src / test / java / tests / security / spec / EncodedKeySpec2Test.java
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 package tests.security.spec;
19
20 import dalvik.annotation.TestTargets;
21 import dalvik.annotation.TestLevel;
22 import dalvik.annotation.TestTargetNew;
23 import dalvik.annotation.TestTargetClass;
24
25 import junit.framework.TestCase;
26
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;
36
37 @TestTargetClass(EncodedKeySpec.class)
38 public class EncodedKeySpec2Test extends TestCase {
39
40     /**
41      * @tests java.security.spec.EncodedKeySpec#getEncoded()
42      */
43     @TestTargetNew(
44         level = TestLevel.COMPLETE,
45         notes = "",
46         method = "getEncoded",
47         args = {}
48     )
49     public void test_getEncoded() throws Exception {
50
51                KeyPairGenerator keyGen = KeyPairGenerator.getInstance("DSA");
52
53                keyGen.initialize(1024);
54                KeyPair keys = keyGen.generateKeyPair();
55
56
57                KeyFactory fact = KeyFactory.getInstance("DSA");
58
59
60                // check public key encoding
61                byte[] encoded = keys.getPublic().getEncoded();
62                Key key = fact.generatePublic(new X509EncodedKeySpec(encoded));
63
64                assertTrue("public key encodings were different",
65                            isEqual(key, keys.getPublic()));
66
67                // check private key encoding
68                encoded = keys.getPrivate().getEncoded();
69                key = fact.generatePrivate(new PKCS8EncodedKeySpec(encoded));
70
71                assertTrue("private key encodings were different",
72                            isEqual(key, keys.getPrivate()));
73     }
74
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());
83
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());
92         } else {
93             return false;
94         }
95     }
96 }