CodeSignerTest.java revision 561ee011997c6c2f1befbfaa9d5f0a99771c1d63
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*/
21
22package org.apache.harmony.security.tests.java.security;
23import java.security.*;
24import java.security.cert.CertPath;
25import java.util.Date;
26
27import org.apache.harmony.security.tests.support.TestCertUtils;
28
29import junit.framework.TestCase;
30
31
32/**
33 * Unit test for CodeSigner.
34 */
35
36public class CodeSignerTest extends TestCase {
37
38    /**
39     * Entry point for standalone runs.
40     * @param args command line arguments
41     */
42    public static void main(String[] args) {
43        junit.textui.TestRunner.run(CodeSignerTest.class);
44    }
45
46    private CertPath cpath = TestCertUtils.genCertPath(3, 0);
47    private Date now = new Date();
48
49    private Timestamp ts = new Timestamp(now, cpath);
50
51    /**
52     * must throw NPE if signerCertPath is null
53     */
54    public void testCodeSigner_00() {
55        try {
56            new CodeSigner(null, ts);
57            fail("must not accept null");
58        } catch (NullPointerException ex) {
59            /* it's ok */
60        }
61    }
62
63    /**
64     * timestamp can be null
65     */
66    public final void testCodeSigner_01() {
67        new CodeSigner(cpath, null);
68    }
69
70    /**
71     * Test various assertions about equals()
72     */
73    public final void testEqualsObject() {
74
75        CodeSigner one = new CodeSigner(cpath, ts);
76        CodeSigner two = new CodeSigner(cpath, ts);
77        CodeSigner three = new CodeSigner(cpath, null);
78
79        CertPath cpath2 = TestCertUtils.genCertPath(5, 3);
80        CodeSigner four = new CodeSigner(cpath2, null);
81
82        assertTrue(one.equals(one));
83        assertTrue(one.equals(two));
84        assertTrue(two.equals(one));
85        assertFalse(one.equals(three));
86        assertFalse(three.equals(one));
87        assertTrue(three.equals(three));
88        // different CertPaths
89        assertFalse( three.equals(four));
90        // special cases
91        assertFalse( one.equals(null) );
92        assertFalse( one.equals(new Object()) );
93    }
94
95    /**
96     * Tests CodeSigner.getSignerCertPath()
97     */
98    public void testGetSignerCertPath() {
99        assertSame(new CodeSigner(cpath, null).getSignerCertPath(), cpath);
100    }
101
102    /**
103     * Tests CodeSigner.getTimeStamp()
104     */
105    public void testGetTimestamp() {
106        assertNull(new CodeSigner(cpath, null).getTimestamp());
107        assertSame(new CodeSigner(cpath, ts).getTimestamp(), ts);
108    }
109
110    /**
111     * Tests CodeSigner.toString()
112     */
113    public void testToString() {
114        new CodeSigner(cpath, null).toString();
115        new CodeSigner(cpath, ts).toString();
116    }
117
118}
119