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 Aleksei Y. Semenov
20 */
21
22package org.apache.harmony.security.tests.java.security;
23
24import java.security.*;
25import org.apache.harmony.security.tests.support.PrivateKeyStub;
26import org.apache.harmony.security.tests.support.PublicKeyStub;
27import org.apache.harmony.security.tests.support.SignerStub;
28
29import junit.framework.TestCase;
30
31
32/**
33 * tests for class Signer
34 */
35
36public class SignerTest extends TestCase {
37
38    /**
39     * Constructor for SignerTest.
40     *
41     * @param arg0
42     */
43    public SignerTest(String arg0) {
44        super(arg0);
45    }
46
47    /**
48     * @tests java.security.Signer#toString()
49     */
50    public void test_toString() throws Exception {
51        Signer s1 = new SignerStub("testToString1");
52        assertEquals("[Signer]testToString1", s1.toString());
53
54        Signer s2 = new SignerStub("testToString2", IdentityScope.getSystemScope());
55        s2.toString();
56
57        KeyPair kp = new KeyPair(new PublicKeyStub("public", "SignerTest.testToString", null), new PrivateKeyStub("private", "SignerTest.testToString", null));
58        s1.setKeyPair(kp);
59        s1.toString();
60
61        s2.setKeyPair(kp);
62        s2.toString();
63    }
64
65    /**
66     * verify Signer() creates instance
67     */
68    public void testSigner() {
69        Signer s = new SignerStub();
70        assertNotNull(s);
71        //assertNull(s.getName(), s.getName());
72        assertNull(s.getPrivateKey());
73    }
74
75    /**
76     * verify Signer(String) creates instance
77     */
78    public void testSignerString() throws Exception {
79        Signer s = new SignerStub("sss3");
80        assertNotNull(s);
81        assertEquals("sss3", s.getName());
82        assertNull(s.getPrivateKey());
83    }
84
85    /**
86     * verify  Signer(String, IdentityScope) creates instance
87     */
88    public void testSignerStringIdentityScope() throws Exception {
89        Signer s = new SignerStub("sss4", IdentityScope.getSystemScope());
90        assertNotNull(s);
91        assertEquals("sss4", s.getName());
92        assertSame(IdentityScope.getSystemScope(), s.getScope());
93        assertNull(s.getPrivateKey());
94    }
95
96    /**
97     * verify Signer.getPrivateKey() returns null or private key
98     */
99    public void testGetPrivateKey() throws Exception {
100        byte[] privateKeyData = { 1, 2, 3, 4, 5 };
101        PrivateKeyStub privateKey = new PrivateKeyStub("private", "fff", privateKeyData);
102        PublicKeyStub publicKey = new PublicKeyStub("public", "fff", null);
103        KeyPair kp = new KeyPair(publicKey, privateKey);
104
105        Signer s = new SignerStub("sss5");
106
107        assertNull(s.getPrivateKey());
108
109        s.setKeyPair(kp);
110        assertSame(privateKey, s.getPrivateKey());
111    }
112
113    /**
114     * @tests java.security.Signer#setKeyPair(java.security.KeyPair)
115     */
116    public void test_setKeyPairLjava_security_KeyPair() throws Exception {
117
118        // Regression for HARMONY-2408
119        // test: NullPointerException if pair is null
120        try {
121            new SignerStub("name").setKeyPair(null);
122            fail("No expected NullPointerException");
123        } catch (NullPointerException e) {
124        }
125    }
126
127}
128