2 * Copyright (C) 2009 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package tests.security;
18 import dalvik.annotation.TestLevel;
19 import dalvik.annotation.TestTargetNew;
20 import dalvik.annotation.TestTargets;
22 import junit.framework.TestCase;
24 import java.security.InvalidKeyException;
25 import java.security.KeyPair;
26 import java.security.KeyPairGenerator;
27 import java.security.NoSuchAlgorithmException;
28 import java.security.PrivateKey;
29 import java.security.PublicKey;
30 import java.security.Signature;
31 import java.security.SignatureException;
32 public abstract class SignatureTest extends TestCase {
34 private final String algorithmName;
35 private final String keyAlgorithmName;
36 private static final String signData = "some data to sign an"; //d verify";
37 KeyPairGenerator generator;
40 public SignatureTest(String algorithmName, String keyAlgorithmName) {
41 this.algorithmName = algorithmName;
42 this.keyAlgorithmName = keyAlgorithmName;
45 protected void setUp() throws Exception {
47 generator = getGenerator();
48 keyPair = getKeyPair();
51 private KeyPair getKeyPair() {
52 return generator.generateKeyPair();
55 private KeyPairGenerator getGenerator() {
57 return KeyPairGenerator.getInstance(keyAlgorithmName);
58 } catch (NoSuchAlgorithmException e) {
66 level = TestLevel.ADDITIONAL,
67 method = "getInstance",
71 level = TestLevel.ADDITIONAL,
73 args = {PrivateKey.class}
76 level = TestLevel.ADDITIONAL,
81 level = TestLevel.ADDITIONAL,
86 level = TestLevel.ADDITIONAL,
87 method = "initVerify",
88 args = {PublicKey.class}
91 level = TestLevel.ADDITIONAL,
96 level = TestLevel.COMPLETE,
101 public void testSignature() {
102 Signature signature = null;
104 signature = Signature.getInstance(algorithmName);
105 } catch (NoSuchAlgorithmException e) {
106 fail(e.getMessage());
110 signature.initSign(keyPair.getPrivate());
111 } catch (InvalidKeyException e) {
112 fail(e.getMessage());
116 signature.update(signData.getBytes());
117 } catch (SignatureException e) {
118 fail(e.getMessage());
123 sign = signature.sign();
124 } catch (SignatureException e) {
125 fail(e.getMessage());
129 signature.initVerify(keyPair.getPublic());
130 } catch (InvalidKeyException e) {
131 fail(e.getMessage());
135 signature.update(signData.getBytes());
136 } catch (SignatureException e) {
137 fail(e.getMessage());
141 assertTrue(signature.verify(sign));
142 } catch (SignatureException e) {
143 fail(e.getMessage());