summaryrefslogtreecommitdiff
path: root/security/src/test/api/java/org/apache/harmony/security/tests/java/security/ProviderServiceTest.java
blob: 97de2b899fc13cdd629b3e0e5e4106a1bd9c9757 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/*
 *  Licensed to the Apache Software Foundation (ASF) under one or more
 *  contributor license agreements.  See the NOTICE file distributed with
 *  this work for additional information regarding copyright ownership.
 *  The ASF licenses this file to You under the Apache License, Version 2.0
 *  (the "License"); you may not use this file except in compliance with
 *  the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

/**
 * @author Boris V. Kuznetsov
 */

package org.apache.harmony.security.tests.java.security;

import java.security.Provider;
import java.security.NoSuchAlgorithmException;
import java.util.HashMap;

import org.apache.harmony.security.tests.support.RandomImpl;

import junit.framework.TestCase;


/**
 * Tests for <code>Provider.Service</code> constructor and methods
 */
public class ProviderServiceTest extends TestCase {

    public void testService() {
        Provider p = new MyProvider();
        try {
            new Provider.Service(null, "type", "algorithm",
                    "className", null, null);
            fail("provider is null: No expected NullPointerException");
        } catch (NullPointerException e) {
        }
        try {
            new Provider.Service(p, null, "algorithm",
                    "className", null, null);
            fail("type is null: No expected NullPointerException");
        } catch (NullPointerException e) {
        }
        try {
            new Provider.Service(p, "type", null,
                    "className", null, null);
            fail("algorithm is null: No expected NullPointerException");
        } catch (NullPointerException e) {
        }
        try {
            new Provider.Service(p, "type", "algorithm",
                    null, null, null);
            fail("className is null: No expected NullPointerException");
        } catch (NullPointerException e) {
        }

        Provider.Service s = new Provider.Service(p,
                "type", "algorithm", "className", null, null);

        assertEquals("getType() failed", "type", s.getType());
        assertEquals("getAlgorithm() failed", "algorithm", s.getAlgorithm());
        assertSame("getProvider() failed", p, s.getProvider());
        assertEquals("getClassName() failed", "className", s.getClassName());
        assertTrue("supportsParameter() failed", s
                .supportsParameter(new Object()));
    }

    public void testGetAttribute() {
        Provider p = new MyProvider();
        Provider.Service s = new Provider.Service(p,
                "type", "algorithm", "className", null, null);
        try {
            s.getAttribute(null);
            fail("No expected NullPointerException");
        } catch (NullPointerException e) {
        }

        assertNull("getAttribute(aaa) failed", s.getAttribute("aaa"));

        HashMap hm = new HashMap();
        hm.put("attribute", "value");
        hm.put("KeySize", "1024");
        hm.put("AAA", "BBB");

        s = new Provider.Service(p, "type", "algorithm", "className",
                null, hm);
        assertNull("getAttribute(bbb) failed", s.getAttribute("bbb"));
        assertEquals("getAttribute(attribute) failed", "value", s
                .getAttribute("attribute"));
        assertEquals("getAttribute(KeySize) failed", "1024", s
                .getAttribute("KeySize"));
    }

    public void testNewInstance() throws NoSuchAlgorithmException {
        Provider p = new MyProvider();
        Provider.Service s = new Provider.Service(p, "SecureRandom",
                "algorithm",
                "org.apache.harmony.security.tests.support.RandomImpl",
                null, null);

        Object o = s.newInstance(null);
        assertTrue("incorrect instance", o instanceof RandomImpl);

        try {
            o = s.newInstance(new Object());
            fail("No expected NoSuchAlgorithmException");
        } catch (NoSuchAlgorithmException e) {
        }
    }

    class MyProvider extends Provider {
        MyProvider() {
            super("MyProvider", 1.0, "Provider for testing");
            put("MessageDigest.SHA-1", "SomeClassName");
        }
    }
}