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;
26
27import javax.crypto.SecretKey;
28
29import junit.framework.TestCase;
30
31/**
32 * Tests for <code>KeyStore.SecretKeyEntry</code> class constructor and methods
33 *
34 */
35public class KSSecretKeyEntryTest extends TestCase {
36
37    /**
38     * Test for <code>SecretKeyEntry(SecretKey secretKey)</code> constructor
39     * Assertion: throws NullPointerException when secretKey is null
40     */
41    public void testSecretKeyEntry() {
42        SecretKey sk = null;
43        try {
44            new KeyStore.SecretKeyEntry(sk);
45            fail("NullPointerException must be thrown when secretKey is null");
46        } catch(NullPointerException e) {
47            //expected
48        }
49
50        sk = new tmpSecretKey();
51        try {
52            KeyStore.SecretKeyEntry ske = new KeyStore.SecretKeyEntry(sk);
53            assertNotNull(ske);
54            assertTrue(ske instanceof KeyStore.SecretKeyEntry);
55        } catch(Exception e) {
56            fail("Unexpected exception was thrown when secretKey is not null");
57        }
58    }
59
60    /**
61     * Test for <code>getSecretKey()</code> method
62     * Assertion: returns SecretKey from the given entry
63     */
64    public void testGetSecretKey() {
65        SecretKey sk = new tmpSecretKey();
66        KeyStore.SecretKeyEntry ske = new KeyStore.SecretKeyEntry(sk);
67        assertEquals("Incorrect SecretKey", sk, ske.getSecretKey());
68    }
69
70    /**
71     * Test for <code>toString()</code> method
72     * Assertion: returns non null string
73     */
74    public void testToString() {
75        SecretKey sk = new tmpSecretKey();
76        KeyStore.SecretKeyEntry ske = new KeyStore.SecretKeyEntry(sk);
77        assertNotNull("toString() returns null string", ske.toString());
78    }
79}
80
81class tmpSecretKey implements SecretKey {
82    public String getAlgorithm() {
83        return "My algorithm";
84    }
85    public String getFormat() {
86        return "My Format";
87    }
88    public byte[] getEncoded() {
89        return new byte[1];
90    }
91}
92