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 Alexander V. Astapchuk
20* @version $Revision$
21*/
22
23package org.apache.harmony.security.tests.java.security;
24
25import java.security.CodeSigner;
26import java.security.Timestamp;
27import java.security.cert.CertPath;
28import java.util.Date;
29
30import org.apache.harmony.security.tests.support.TestCertUtils;
31
32import junit.framework.TestCase;
33
34/**
35 * Unit test for CodeSigner.
36 */
37public class CodeSignerTest extends TestCase {
38    private CertPath cpath = TestCertUtils.genCertPath(3, 0);
39    private Date now = new Date();
40
41    private Timestamp ts = new Timestamp(now, cpath);
42
43    /**
44     * must throw NPE if signerCertPath is null
45     */
46    public void testCodeSigner_00() {
47        try {
48            new CodeSigner(null, ts);
49            fail("must not accept null");
50        } catch (NullPointerException ex) {
51            /* it's ok */
52        }
53    }
54
55    /**
56     * timestamp can be null
57     */
58    public final void testCodeSigner_01() {
59        try {
60            CodeSigner cs = new CodeSigner(cpath, null);
61            assertNotNull(cs);
62        } catch (Exception e) {
63            fail("Unexpected exception");
64        }
65    }
66
67    /**
68     * Not null parameters
69     */
70    public final void testCodeSigner_02() {
71        try {
72            CodeSigner cs = new CodeSigner(cpath, ts);
73            assertNotNull(cs);
74        } catch (Exception e) {
75            fail("Unexpected exception");
76        }
77    }
78
79    /**
80     * Test various assertions about equals()
81     */
82    public final void testEqualsObject() {
83
84        CodeSigner one = new CodeSigner(cpath, ts);
85        CodeSigner two = new CodeSigner(cpath, ts);
86        CodeSigner three = new CodeSigner(cpath, null);
87
88        CertPath cpath2 = TestCertUtils.genCertPath(5, 3);
89        CodeSigner four = new CodeSigner(cpath2, null);
90
91        assertTrue(one.equals(one));
92        assertTrue(one.equals(two));
93        assertTrue(two.equals(one));
94        assertFalse(one.equals(three));
95        assertFalse(three.equals(one));
96        assertTrue(three.equals(three));
97        // different CertPaths
98        assertFalse( three.equals(four));
99        // special cases
100        assertFalse( one.equals(null) );
101        assertFalse( one.equals(new Object()) );
102    }
103
104    /**
105     * Tests CodeSigner.getSignerCertPath()
106     */
107    public void testGetSignerCertPath() {
108        assertSame(new CodeSigner(cpath, null).getSignerCertPath(), cpath);
109    }
110
111    /**
112     * Tests CodeSigner.getTimeStamp()
113     */
114    public void testGetTimestamp() {
115        assertNull(new CodeSigner(cpath, null).getTimestamp());
116        assertSame(new CodeSigner(cpath, ts).getTimestamp(), ts);
117    }
118
119    /**
120     * Tests CodeSigner.toString()
121     */
122    public void testToString() {
123        assertTrue(new CodeSigner(cpath, null).toString().contains(""));
124        assertTrue(new CodeSigner(cpath, ts).toString().contains(""));
125
126        assertTrue(new CodeSigner(cpath, null).toString().contains("Signer"));
127        assertTrue(new CodeSigner(cpath, ts).toString().contains(ts.toString()));
128    }
129
130    /**
131     * Tests CodeSigner.hashCode()
132     */
133    public void testHashCode() {
134        CodeSigner cs1 = new CodeSigner(cpath, ts);
135        CodeSigner cs2 = new CodeSigner(cpath, ts);
136        CodeSigner cs3 = new CodeSigner(cpath, null);
137
138        assertTrue(cs1.hashCode() == cs2.hashCode());
139        assertTrue(cs2.hashCode() != cs3.hashCode());
140    }
141
142}
143