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 Vera Y. Petrashkova
20* @version $Revision$
21*/
22
23package org.apache.harmony.security.tests.java.security;
24
25import java.security.KeyStore;
26import java.util.HashSet;
27import java.util.Set;
28
29import javax.crypto.SecretKey;
30
31import junit.framework.TestCase;
32
33/**
34 * Tests for <code>KeyStore.SecretKeyEntry</code> class constructor and methods
35 *
36 */
37public class KSSecretKeyEntryTest extends TestCase {
38
39    /**
40     * Test for <code>SecretKeyEntry(SecretKey secretKey)</code> constructor
41     * Assertion: throws NullPointerException when secretKey is null
42     */
43    public void testSecretKeyEntry() {
44        SecretKey sk = null;
45        try {
46            new KeyStore.SecretKeyEntry(sk);
47            fail("NullPointerException must be thrown when secretKey is null");
48        } catch(NullPointerException e) {
49            //expected
50        }
51
52        sk = new tmpSecretKey();
53        try {
54            KeyStore.SecretKeyEntry ske = new KeyStore.SecretKeyEntry(sk);
55            assertNotNull(ske);
56            assertTrue(ske instanceof KeyStore.SecretKeyEntry);
57        } catch(Exception e) {
58            fail("Unexpected exception was thrown when secretKey is not null");
59        }
60    }
61
62    /**
63     * Test for
64     * <code>SecretKeyEntry(SecretKey secretKey, Set<Attribute> attribute)</code>
65     * constructor
66     * Assertion: throws NullPointerException when attributes is null
67     */
68    public void testSecretKeyEntry_nullAttributes() {
69        SecretKey sk = new tmpSecretKey();
70        try {
71            new KeyStore.SecretKeyEntry(sk, null /* attributes */);
72            fail("NullPointerException must be thrown when attributes is null");
73        } catch(NullPointerException expected) {
74        }
75    }
76
77    /**
78     * Test for <code>getSecretKey()</code> method
79     * Assertion: returns SecretKey from the given entry
80     */
81    public void testGetSecretKey() {
82        SecretKey sk = new tmpSecretKey();
83        KeyStore.SecretKeyEntry ske = new KeyStore.SecretKeyEntry(sk);
84        assertEquals("Incorrect SecretKey", sk, ske.getSecretKey());
85    }
86
87    /**
88     * Test for <code>getAttributes()</code> method
89     * Assertion: returns the attributes specified in the constructor, as an unmodifiable set
90     */
91    public void testGetAttributes() {
92        SecretKey sk = new tmpSecretKey();
93        final String attributeName = "theAttributeName";
94        KeyStore.Entry.Attribute myAttribute = new KeyStore.Entry.Attribute() {
95            @Override
96            public String getName() {
97                return attributeName;
98            }
99
100            @Override
101            public String getValue() {
102                return null;
103            }
104        };
105        Set<KeyStore.Entry.Attribute> attributeSet = new HashSet<KeyStore.Entry.Attribute>();
106        attributeSet.add(myAttribute);
107
108        KeyStore.SecretKeyEntry ksSKE = new KeyStore.SecretKeyEntry(sk, attributeSet);
109        Set<KeyStore.Entry.Attribute> returnedAttributeSet = ksSKE.getAttributes();
110        assertEquals(attributeSet, returnedAttributeSet);
111        // Adding an element to the original set is OK.
112        attributeSet.add(myAttribute);
113        // The returned set is unmodifiabled.
114        try {
115            returnedAttributeSet.add(myAttribute);
116            fail("The returned set of attributed should be unmodifiable");
117        } catch (UnsupportedOperationException expected) {
118        }
119    }
120
121    /**
122     * Test for <code>toString()</code> method
123     * Assertion: returns non null string
124     */
125    public void testToString() {
126        SecretKey sk = new tmpSecretKey();
127        KeyStore.SecretKeyEntry ske = new KeyStore.SecretKeyEntry(sk);
128        assertNotNull("toString() returns null string", ske.toString());
129    }
130}
131
132class tmpSecretKey implements SecretKey {
133    public String getAlgorithm() {
134        return "My algorithm";
135    }
136    public String getFormat() {
137        return "My Format";
138    }
139    public byte[] getEncoded() {
140        return new byte[1];
141    }
142}
143