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        try {
129            o = s.newInstance(new Object());
130            fail("No expected NoSuchAlgorithmException");
131        } catch (NoSuchAlgorithmException e) {
132        }
133
134    }
135
136    public void testGetAlgorithm() {
137        Provider p = new MyProvider();
138        Provider.Service s1 = new Provider.Service(p, "type", "algorithm",
139                "className", null, null);
140        assertTrue(s1.getAlgorithm().equals("algorithm"));
141
142        Provider.Service s2 = new Provider.Service(p, "SecureRandom",
143                "algorithm", "tests.java.security.support.RandomImpl", null,
144                null);
145        assertTrue(s2.getAlgorithm().equals("algorithm"));
146    }
147
148    public void testGetClassName() {
149        Provider p = new MyProvider();
150        Provider.Service s1 = new Provider.Service(p, "type", "algorithm",
151                "className", null, null);
152        assertTrue(s1.getClassName().equals("className"));
153
154        Provider.Service s2 = new Provider.Service(p, "SecureRandom",
155                "algorithm", "tests.java.security.support.RandomImpl", null,
156                null);
157        assertTrue(s2.getClassName().equals("tests.java.security.support.RandomImpl"));
158    }
159
160    public void testGetProvider() {
161        Provider p = new MyProvider();
162        Provider.Service s1 = new Provider.Service(p, "type", "algorithm",
163                "className", null, null);
164        assertTrue(s1.getProvider() == p);
165
166        Provider.Service s2 = new Provider.Service(p, "SecureRandom",
167                "algorithm", "tests.java.security.support.RandomImpl", null,
168                null);
169        assertTrue(s2.getProvider() == p);
170    }
171
172    public void testGetType() {
173        Provider p = new MyProvider();
174        Provider.Service s1 = new Provider.Service(p, "type", "algorithm",
175                "className", null, null);
176        assertTrue(s1.getType().equals("type"));
177
178        Provider.Service s2 = new Provider.Service(p, "SecureRandom",
179                "algorithm", "tests.java.security.support.RandomImpl", null,
180                null);
181        assertTrue(s2.getType().equals("SecureRandom"));
182    }
183
184    public void testSupportsParameter() {
185        Provider p = new MyProvider();
186        Provider.Service s1 = new Provider.Service(p, "type", "algorithm",
187                "className", null, null);
188        assertTrue(s1.supportsParameter(null));
189        assertTrue(s1.supportsParameter(new Object()));
190    }
191
192    public void testToString() {
193        Provider p = new MyProvider();
194        Provider.Service s1 = new Provider.Service(p, "type", "algorithm",
195                "className", null, null);
196        s1.toString();
197
198        Provider.Service s2 = new Provider.Service(p, "SecureRandom",
199                "algorithm", "tests.java.security.support.RandomImpl", null,
200                null);
201        s2.toString();
202    }
203
204    class MyProvider extends Provider {
205        MyProvider() {
206            super("MyProvider", 1.0, "Provider for testing");
207            put("MessageDigest.SHA-1", "SomeClassName");
208        }
209
210    }
211
212    class MyService extends Provider.Service {
213
214        public MyService(Provider provider, String type, String algorithm,
215                String className, List<String> aliases,
216                Map<String, String> attributes) {
217            super(provider, type, algorithm, className, aliases, attributes);
218            // TODO Auto-generated constructor stub
219        }
220
221        @Override
222        public boolean supportsParameter(Object parameter) {
223            if (parameter.getClass() == String.class) {
224                return true;
225            }
226            return false;
227        }
228    }
229}
230