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 Boris V. Kuznetsov
20 * @version $Revision$
21 */
22
23package org.apache.harmony.security.tests.java.security;
24
25import java.security.InvalidParameterException;
26import java.security.Provider;
27import java.security.Provider.Service;
28import java.security.NoSuchAlgorithmException;
29import java.util.HashMap;
30import java.util.List;
31import java.util.Map;
32
33import org.apache.harmony.security.tests.support.RandomImpl;
34
35import junit.framework.TestCase;
36
37/**
38 * Tests for <code>Provider.Service</code> constructor and methods
39 *
40 */
41public class ProviderServiceTest extends TestCase {
42
43    public void testService() {
44        Provider p = new MyProvider();
45        try {
46            new Provider.Service(null, "type", "algorithm", "className", null,
47                    null);
48            fail("provider is null: No expected NullPointerException");
49        } catch (NullPointerException e) {
50        }
51        try {
52            new Provider.Service(p, null, "algorithm", "className", null, null);
53            fail("type is null: No expected NullPointerException");
54        } catch (NullPointerException e) {
55        }
56        try {
57            new Provider.Service(p, "type", null, "className", null, null);
58            fail("algorithm is null: No expected NullPointerException");
59        } catch (NullPointerException e) {
60        }
61        try {
62            new Provider.Service(p, "type", "algorithm", null, null, null);
63            fail("className is null: No expected NullPointerException");
64        } catch (NullPointerException e) {
65        }
66
67        Provider.Service s = new Provider.Service(p, "type", "algorithm",
68                "className", null, null);
69
70        if (!s.getType().equals("type")) {
71            fail("getType() failed");
72        }
73        if (!s.getAlgorithm().equals("algorithm")) {
74            fail("getAlgorithm() failed");
75        }
76        if (s.getProvider() != p) {
77            fail("getProvider() failed");
78        }
79        if (!s.getClassName().equals("className")) {
80            fail("getClassName() failed");
81        }
82        if (!s.supportsParameter(new Object())) {
83            fail("supportsParameter() failed");
84        }
85    }
86
87    public void testGetAttribute() {
88        Provider p = new MyProvider();
89        Provider.Service s = new Provider.Service(p, "type", "algorithm",
90                "className", null, null);
91        try {
92            s.getAttribute(null);
93            fail("No expected NullPointerException");
94        } catch (NullPointerException e) {
95        }
96
97        if (s.getAttribute("aaa") != null) {
98            fail("getAttribute(aaa) failed");
99        }
100
101        HashMap<String, String> hm = new HashMap<String, String>();
102        hm.put("attribute", "value");
103        hm.put("KeySize", "1024");
104        hm.put("AAA", "BBB");
105
106        s = new Provider.Service(p, "type", "algorithm", "className", null, hm);
107        if (s.getAttribute("bbb") != null) {
108            fail("getAttribute(bbb) failed");
109        }
110        if (!s.getAttribute("attribute").equals("value")) {
111            fail("getAttribute(attribute) failed");
112        }
113        if (!s.getAttribute("KeySize").equals("1024")) {
114            fail("getAttribute(KeySize) failed");
115        }
116    }
117
118    public void testNewInstance() throws Exception {
119        Provider p = new MyProvider();
120        Provider.Service s = new Provider.Service(p, "SecureRandom",
121                "algorithm",
122                "org.apache.harmony.security.tests.support.RandomImpl",
123                null, null);
124
125        Object o = s.newInstance(null);
126        assertTrue("incorrect instance", o instanceof RandomImpl);
127    }
128
129    public void testGetAlgorithm() {
130        Provider p = new MyProvider();
131        Provider.Service s1 = new Provider.Service(p, "type", "algorithm",
132                "className", null, null);
133        assertTrue(s1.getAlgorithm().equals("algorithm"));
134
135        Provider.Service s2 = new Provider.Service(p, "SecureRandom",
136                "algorithm", "tests.java.security.support.RandomImpl", null,
137                null);
138        assertTrue(s2.getAlgorithm().equals("algorithm"));
139    }
140
141    public void testGetClassName() {
142        Provider p = new MyProvider();
143        Provider.Service s1 = new Provider.Service(p, "type", "algorithm",
144                "className", null, null);
145        assertTrue(s1.getClassName().equals("className"));
146
147        Provider.Service s2 = new Provider.Service(p, "SecureRandom",
148                "algorithm", "tests.java.security.support.RandomImpl", null,
149                null);
150        assertTrue(s2.getClassName().equals("tests.java.security.support.RandomImpl"));
151    }
152
153    public void testGetProvider() {
154        Provider p = new MyProvider();
155        Provider.Service s1 = new Provider.Service(p, "type", "algorithm",
156                "className", null, null);
157        assertTrue(s1.getProvider() == p);
158
159        Provider.Service s2 = new Provider.Service(p, "SecureRandom",
160                "algorithm", "tests.java.security.support.RandomImpl", null,
161                null);
162        assertTrue(s2.getProvider() == p);
163    }
164
165    public void testGetType() {
166        Provider p = new MyProvider();
167        Provider.Service s1 = new Provider.Service(p, "type", "algorithm",
168                "className", null, null);
169        assertTrue(s1.getType().equals("type"));
170
171        Provider.Service s2 = new Provider.Service(p, "SecureRandom",
172                "algorithm", "tests.java.security.support.RandomImpl", null,
173                null);
174        assertTrue(s2.getType().equals("SecureRandom"));
175    }
176
177    public void testSupportsParameter() {
178        Provider p = new MyProvider();
179        Provider.Service s1 = new Provider.Service(p, "type", "algorithm",
180                "className", null, null);
181        assertTrue(s1.supportsParameter(null));
182        assertTrue(s1.supportsParameter(new Object()));
183    }
184
185    public void testToString() {
186        Provider p = new MyProvider();
187        Provider.Service s1 = new Provider.Service(p, "type", "algorithm",
188                "className", null, null);
189        s1.toString();
190
191        Provider.Service s2 = new Provider.Service(p, "SecureRandom",
192                "algorithm", "tests.java.security.support.RandomImpl", null,
193                null);
194        s2.toString();
195    }
196
197    class MyProvider extends Provider {
198        MyProvider() {
199            super("MyProvider", 1.0, "Provider for testing");
200            put("MessageDigest.SHA-1", "SomeClassName");
201        }
202
203    }
204
205    class MyService extends Provider.Service {
206
207        public MyService(Provider provider, String type, String algorithm,
208                String className, List<String> aliases,
209                Map<String, String> attributes) {
210            super(provider, type, algorithm, className, aliases, attributes);
211            // TODO Auto-generated constructor stub
212        }
213
214        @Override
215        public boolean supportsParameter(Object parameter) {
216            if (parameter.getClass() == String.class) {
217                return true;
218            }
219            return false;
220        }
221    }
222}
223