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 java.security;
23
24
25import org.apache.harmony.security.tests.support.CertificateStub;
26import org.apache.harmony.security.tests.support.IdentityStub;
27import org.apache.harmony.security.tests.support.PublicKeyStub;
28
29import junit.framework.TestCase;
30
31/**
32 * Tests for class Identity
33 */
34
35public class IdentityTest extends TestCase {
36
37    /**
38     * Constructor for IdentityTest.
39     *
40     * @param name
41     */
42    public IdentityTest(String name) {
43        super(name);
44    }
45
46    public void testHashCode() {
47        new IdentityStub("testHashCode").hashCode();
48    }
49
50    public void testEquals() throws Exception {
51        Identity i1 = new IdentityStub("testEquals");
52        Object value[] = {
53                null, Boolean.FALSE,
54                new Object(), Boolean.FALSE,
55                i1, Boolean.TRUE,
56                new IdentityStub(i1.getName()), Boolean.TRUE
57        };
58
59        for (int k = 0; k < value.length; k += 2) {
60            assertEquals(value[k + 1], new Boolean(i1.equals(value[k])));
61            if (Boolean.TRUE.equals(value[k + 1]))
62                assertEquals(i1.hashCode(), value[k].hashCode());
63        }
64        // check other cases
65        Identity i2 = new IdentityStub("testEquals", IdentityScope.getSystemScope());
66        assertEquals(i1.identityEquals(i2), i1.equals(i2));
67        Identity i3 = new IdentityStub("testEquals3");
68        assertEquals(i1.identityEquals(i3), i1.equals(i3));
69
70    }
71
72    /**
73     * verify Identity.toString()
74     */
75    public void testToString2() {
76        assertNotNull(new IdentityStub("testToString2").toString());
77    }
78
79    /**
80     * verify Identity() creates instance
81     */
82    public void testIdentity() {
83        assertNotNull(new IdentityStub());
84    }
85
86    /*
87     * verify Identity(String) creates instance with given name
88     */
89    public void testIdentityString() {
90        Identity i = new IdentityStub("iii");
91        assertNotNull(i);
92        assertEquals("iii", i.getName());
93        i = new IdentityStub(null);
94        assertNotNull(i);
95        assertNull(i.getName());
96    }
97
98    /**
99     * verify Identity(String, IdentityScope) creates instance with given name and in give scope
100     */
101    public void testIdentityStringIdentityScope() throws Exception {
102        IdentityScope s = IdentityScope.getSystemScope();
103        Identity i = new IdentityStub("iii2", s);
104        assertNotNull(i);
105        assertEquals("iii2", i.getName());
106        assertSame(s, i.getScope());
107        assertSame(i, s.getIdentity(i.getName()));
108    }
109
110    /**
111     * verify addCertificate(Certificate certificate) adds a certificate for this identity.
112     * If the identity has a public key, the public key in the certificate must be the same
113     */
114    public void testAddCertificate1() throws Exception {
115        Identity i = new IdentityStub("iii");
116        PublicKeyStub pk1 = new PublicKeyStub("kkk", "fff", new byte[] { 1, 2, 3, 4, 5 });
117        i.setPublicKey(pk1);
118        // try with the same key
119        CertificateStub c1 = new CertificateStub("fff", null, null, pk1);
120        i.addCertificate(c1);
121        assertSame(c1, i.certificates()[0]);
122        // try Certificate with different key
123        try {
124            i.addCertificate(new CertificateStub("ccc", null, null, new PublicKeyStub("k2", "fff", new byte[] { 6, 7, 8, 9, 0 })));
125            fail("KeyManagementException should be thrown");
126        } catch (KeyManagementException ok) {
127        }
128    }
129
130    /**
131     * verify addCertificate(Certificate certificate) adds a certificate for this identity.
132     * if the identity does not have a public key, the identity's public key is set to be that specified in the certificate.
133     */
134    public void testAddCertificate2() throws Exception {
135        Identity i = new IdentityStub("iii");
136        PublicKeyStub pk1 = new PublicKeyStub("kkk", "fff", null);
137        CertificateStub c1 = new CertificateStub("fff", null, null, pk1);
138        i.addCertificate(c1);
139        assertSame(c1, i.certificates()[0]);
140        assertSame(pk1, i.getPublicKey());
141
142    }
143
144    /**
145     * verify addCertificate(Certificate certificate) throws KeyManagementException if certificate is null
146     */
147    public void testAddCertificate4() throws Exception {
148        try {
149            new IdentityStub("aaa").addCertificate(null);
150            fail("KeyManagementException should be thrown");
151        } catch (KeyManagementException ok) {
152        } catch (NullPointerException ok) {
153        }
154
155    }
156//
157//  Commented out since there will no be fix for the test failure
158//    /**
159//     * verify removeCertificate(Certificate certificate) removes certificate
160//     */
161//    public void testRemoveCertificate1() throws Exception{
162//        Identity i = new IdentityStub("iii");
163//        PublicKeyStub pk1 = new PublicKeyStub("kkk", "fff", null);
164//        CertificateStub c1 = new CertificateStub("fff", null, null, pk1);
165//        i.addCertificate(c1);
166//        assertSame(c1, i.certificates()[0]);
167//        i.removeCertificate(c1);
168//        assertEquals(0, i.certificates().length);
169//        // throw KeyManagementException if certificate not found
170//        try {
171//            i.removeCertificate(c1);
172//            fail("KeyManagementException should be thrown");
173//        } catch (KeyManagementException ok) {
174//        }
175//        try {
176//            i.removeCertificate(null);
177//            fail("KeyManagementException should be thrown");
178//        } catch (KeyManagementException ok) {
179//
180//        }
181//    }
182
183    /**
184     * verify certificates() returns a copy of all certificates for this identity
185     */
186    public void testCertificates() throws Exception {
187        Identity i = new IdentityStub("iii");
188        PublicKeyStub pk1 = new PublicKeyStub("kkk", "fff", null);
189        CertificateStub c1 = new CertificateStub("fff", null, null, pk1);
190        CertificateStub c2 = new CertificateStub("zzz", null, null, pk1);
191        i.addCertificate(c1);
192        i.addCertificate(c2);
193        Certificate[] s = i.certificates();
194        assertEquals(2, s.length);
195        assertTrue(c1.equals(s[0]) || c2.equals(s[0]));
196        assertTrue(c1.equals(s[1]) || c2.equals(s[1]));
197        s[0] = null;
198        s[1] = null;
199        // check that the copy was modified
200        s = i.certificates();
201        assertEquals(2, s.length);
202        assertTrue(c1.equals(s[0]) || c2.equals(s[0]));
203        assertTrue(c1.equals(s[1]) || c2.equals(s[1]));
204    }
205
206    /**
207     * verify Identity.identityEquals(Identity) return true, only if names and public keys are equal
208     */
209
210    public void testIdentityEquals() throws Exception {
211        String name = "nnn";
212        PublicKey pk = new PublicKeyStub("aaa", "fff", new byte[] { 1, 2, 3, 4, 5 });
213        Identity i = new IdentityStub(name);
214        i.setPublicKey(pk);
215        Object[] value = {
216                //null, Boolean.FALSE,
217                //new Object(), Boolean.FALSE,
218                new IdentityStub("111"), Boolean.FALSE,
219                new IdentityStub(name), Boolean.FALSE,
220                new IdentityStub(name, IdentityScope.getSystemScope()), Boolean.FALSE,
221                i, Boolean.TRUE,
222                new IdentityStub(name, pk), Boolean.TRUE
223        };
224        for (int k = 0; k < value.length; k += 2) {
225            assertEquals(value[k + 1], new Boolean(i.identityEquals((Identity) value[k])));
226            if (Boolean.TRUE.equals(value[k + 1]))
227                assertEquals(i.hashCode(), value[k].hashCode());
228        }
229        Identity i2 = IdentityScope.getSystemScope().getIdentity(name);
230        i2.setPublicKey(pk);
231        assertTrue(i.identityEquals(i2));
232    }
233
234    /**
235     * verify Identity.toString(boolean) return string representation of identity
236     */
237    public void testToStringboolean() throws Exception {
238        new IdentityStub("aaa").toString(false);
239        new IdentityStub("aaa2", IdentityScope.getSystemScope()).toString(false);
240        new IdentityStub("bbb").toString(true);
241        new IdentityStub("bbb2", IdentityScope.getSystemScope()).toString(true);
242    }
243
244    /**
245     * verify Identity.getScope() returns identity's scope
246     */
247    public void testGetScope() throws Exception {
248        Identity i = new IdentityStub("testGetScope");
249        assertNull(i.getScope());
250        IdentityScope s = IdentityScope.getSystemScope();
251
252        Identity i2 = new IdentityStub("testGetScope2", s);
253        assertSame(s, i2.getScope());
254
255    }
256
257    /**
258     * verify Identity.setPublicKey() throws KeyManagementException if key is invalid
259     */
260    public void testSetPublicKey2() throws Exception {
261        Identity i2 = new IdentityStub("testSetPublicKey2_2", IdentityScope.getSystemScope());
262        new PublicKeyStub("kkk", "testSetPublicKey2", new byte[] { 1, 2, 3, 4, 5 });
263        try {
264            i2.setPublicKey(null);
265            //fail("KeyManagementException should be thrown - key is null");
266        } catch (KeyManagementException ok) {
267        }
268    }
269
270//
271//  Commented out since there will no be fix for the test failure
272//    /**
273//     *
274//     * verify Identity.setPublicKey() throws KeyManagementException if key is already used
275//     *
276//     */
277//    public void testSetPublicKey3() throws Exception {
278//        Identity i1 = new IdentityStub("testSetPublicKey3_1", IdentityScope.getSystemScope());
279//        Identity i2 = new IdentityStub("testSetPublicKey3_2", IdentityScope.getSystemScope());
280//        PublicKey pk = new PublicKeyStub("kkk", "fff", new byte[]{1,2,3,4,5});
281//        i1.setPublicKey(pk);
282//        try {
283//            i2.setPublicKey(pk);
284//            fail("KeyManagementException should be thrown - key already used");
285//        } catch (KeyManagementException ok) {};
286//    }
287
288    /**
289     * verify Identity.setPublicKey()  removes old key and all identity's certificates
290     */
291    public void testSetPublicKey4() throws Exception {
292        Identity i = new IdentityStub("testSetPublicKey4");
293        PublicKeyStub pk1 = new PublicKeyStub("kkk", "Identity.testSetPublicKey4", null);
294        CertificateStub c1 = new CertificateStub("fff", null, null, pk1);
295        CertificateStub c2 = new CertificateStub("zzz", null, null, pk1);
296        i.addCertificate(c1);
297        i.addCertificate(c2);
298        assertEquals(2, i.certificates().length);
299        assertSame(pk1, i.getPublicKey());
300
301        PublicKeyStub pk2 = new PublicKeyStub("zzz", "Identity.testSetPublicKey4", null);
302        i.setPublicKey(pk2);
303        assertSame(pk2, i.getPublicKey());
304        assertEquals(0, i.certificates().length);
305    }
306
307    /**
308     * verify Identity.getPublicKey() returns public key
309     */
310
311    public void testGetPublicKey() throws Exception {
312        Identity i = new IdentityStub("testGetPublicKey");
313        assertNull(i.getPublicKey());
314        PublicKey pk = new PublicKeyStub("kkk", "Identity.testGetPublicKey", null);
315        i.setPublicKey(pk);
316        assertSame(pk, i.getPublicKey());
317    }
318
319    public void testGetInfo() {
320
321        Identity i = new IdentityStub("testGetInfo");
322        i.setInfo("some info");
323        assertEquals("some info", i.getInfo());
324    }
325
326    public void testGetName() {
327        Identity i = new IdentityStub("testGetName");
328        assertEquals("testGetName", i.getName());
329    }
330
331}
332