ProviderServiceTest.java revision f33eae7e84eb6d3b0f4e86b59605bb3de73009f3
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 dalvik.annotation.TestTargetClass;
26import dalvik.annotation.TestTargets;
27import dalvik.annotation.TestLevel;
28import dalvik.annotation.TestTargetNew;
29
30import java.security.InvalidParameterException;
31import java.security.Provider;
32import java.security.Provider.Service;
33import java.security.NoSuchAlgorithmException;
34import java.util.HashMap;
35import java.util.List;
36import java.util.Map;
37
38import org.apache.harmony.security.tests.support.RandomImpl;
39
40import junit.framework.TestCase;
41@TestTargetClass(Service.class)
42/**
43 * Tests for <code>Provider.Service</code> constructor and methods
44 *
45 */
46public class ProviderServiceTest extends TestCase {
47
48    @TestTargetNew(
49        level = TestLevel.COMPLETE,
50        notes = "",
51        method = "Service",
52        args = {java.security.Provider.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.util.List.class, java.util.Map.class}
53    )
54    public void testService() {
55        Provider p = new MyProvider();
56        try {
57            new Provider.Service(null, "type", "algorithm", "className", null,
58                    null);
59            fail("provider is null: No expected NullPointerException");
60        } catch (NullPointerException e) {
61        }
62        try {
63            new Provider.Service(p, null, "algorithm", "className", null, null);
64            fail("type is null: No expected NullPointerException");
65        } catch (NullPointerException e) {
66        }
67        try {
68            new Provider.Service(p, "type", null, "className", null, null);
69            fail("algorithm is null: No expected NullPointerException");
70        } catch (NullPointerException e) {
71        }
72        try {
73            new Provider.Service(p, "type", "algorithm", null, null, null);
74            fail("className is null: No expected NullPointerException");
75        } catch (NullPointerException e) {
76        }
77
78        Provider.Service s = new Provider.Service(p, "type", "algorithm",
79                "className", null, null);
80
81        if (!s.getType().equals("type")) {
82            fail("getType() failed");
83        }
84        if (!s.getAlgorithm().equals("algorithm")) {
85            fail("getAlgorithm() failed");
86        }
87        if (s.getProvider() != p) {
88            fail("getProvider() failed");
89        }
90        if (!s.getClassName().equals("className")) {
91            fail("getClassName() failed");
92        }
93        if (!s.supportsParameter(new Object())) {
94            fail("supportsParameter() failed");
95        }
96    }
97
98    @TestTargetNew(
99        level = TestLevel.COMPLETE,
100        notes = "",
101        method = "getAttribute",
102        args = {java.lang.String.class}
103    )
104    public void testGetAttribute() {
105        Provider p = new MyProvider();
106        Provider.Service s = new Provider.Service(p, "type", "algorithm",
107                "className", null, null);
108        try {
109            s.getAttribute(null);
110            fail("No expected NullPointerException");
111        } catch (NullPointerException e) {
112        }
113
114        if (s.getAttribute("aaa") != null) {
115            fail("getAttribute(aaa) failed");
116        }
117
118        HashMap<String, String> hm = new HashMap<String, String>();
119        hm.put("attribute", "value");
120        hm.put("KeySize", "1024");
121        hm.put("AAA", "BBB");
122
123        s = new Provider.Service(p, "type", "algorithm", "className", null, hm);
124        if (s.getAttribute("bbb") != null) {
125            fail("getAttribute(bbb) failed");
126        }
127        if (!s.getAttribute("attribute").equals("value")) {
128            fail("getAttribute(attribute) failed");
129        }
130        if (!s.getAttribute("KeySize").equals("1024")) {
131            fail("getAttribute(KeySize) failed");
132        }
133    }
134
135    @TestTargetNew(
136        level = TestLevel.COMPLETE,
137        notes = "",
138        method = "newInstance",
139        args = {java.lang.Object.class}
140    )
141    public void testNewInstance() throws Exception {
142        Provider p = new MyProvider();
143        Provider.Service s = new Provider.Service(p, "SecureRandom",
144                "algorithm",
145                "org.apache.harmony.security.tests.support.RandomImpl",
146                null, null);
147
148        Object o = s.newInstance(null);
149        assertTrue("incorrect instance", o instanceof RandomImpl);
150
151        try {
152            o = s.newInstance(new Object());
153            fail("No expected NoSuchAlgorithmException");
154        } catch (NoSuchAlgorithmException e) {
155        }
156
157    }
158
159    @TestTargetNew(
160        level = TestLevel.COMPLETE,
161        notes = "",
162        method = "getAlgorithm",
163        args = {}
164    )
165    public void testGetAlgorithm() {
166        Provider p = new MyProvider();
167        Provider.Service s1 = new Provider.Service(p, "type", "algorithm",
168                "className", null, null);
169        assertTrue(s1.getAlgorithm().equals("algorithm"));
170
171        Provider.Service s2 = new Provider.Service(p, "SecureRandom",
172                "algorithm", "tests.java.security.support.RandomImpl", null,
173                null);
174        assertTrue(s2.getAlgorithm().equals("algorithm"));
175    }
176
177    @TestTargetNew(
178        level = TestLevel.COMPLETE,
179        notes = "",
180        method = "getClassName",
181        args = {}
182    )
183    public void testGetClassName() {
184        Provider p = new MyProvider();
185        Provider.Service s1 = new Provider.Service(p, "type", "algorithm",
186                "className", null, null);
187        assertTrue(s1.getClassName().equals("className"));
188
189        Provider.Service s2 = new Provider.Service(p, "SecureRandom",
190                "algorithm", "tests.java.security.support.RandomImpl", null,
191                null);
192        assertTrue(s2.getClassName().equals("tests.java.security.support.RandomImpl"));
193    }
194
195    @TestTargetNew(
196        level = TestLevel.COMPLETE,
197        notes = "",
198        method = "getProvider",
199        args = {}
200    )
201    public void testGetProvider() {
202        Provider p = new MyProvider();
203        Provider.Service s1 = new Provider.Service(p, "type", "algorithm",
204                "className", null, null);
205        assertTrue(s1.getProvider() == p);
206
207        Provider.Service s2 = new Provider.Service(p, "SecureRandom",
208                "algorithm", "tests.java.security.support.RandomImpl", null,
209                null);
210        assertTrue(s2.getProvider() == p);
211    }
212
213    @TestTargetNew(
214        level = TestLevel.COMPLETE,
215        notes = "",
216        method = "getType",
217        args = {}
218    )
219    public void testGetType() {
220        Provider p = new MyProvider();
221        Provider.Service s1 = new Provider.Service(p, "type", "algorithm",
222                "className", null, null);
223        assertTrue(s1.getType().equals("type"));
224
225        Provider.Service s2 = new Provider.Service(p, "SecureRandom",
226                "algorithm", "tests.java.security.support.RandomImpl", null,
227                null);
228        assertTrue(s2.getType().equals("SecureRandom"));
229    }
230
231    @TestTargetNew(
232        level = TestLevel.COMPLETE,
233        notes = "",
234        method = "supportsParameter",
235        args = {java.lang.Object.class}
236    )
237    public void testSupportsParameter() {
238        Provider p = new MyProvider();
239        Provider.Service s1 = new Provider.Service(p, "type", "algorithm",
240                "className", null, null);
241        assertTrue(s1.supportsParameter(null));
242        assertTrue(s1.supportsParameter(new Object()));
243    }
244
245    @TestTargetNew(
246        level = TestLevel.COMPLETE,
247        notes = "",
248        method = "toString",
249        args = {}
250    )
251    public void testToString() {
252        Provider p = new MyProvider();
253        Provider.Service s1 = new Provider.Service(p, "type", "algorithm",
254                "className", null, null);
255        s1.toString();
256
257        Provider.Service s2 = new Provider.Service(p, "SecureRandom",
258                "algorithm", "tests.java.security.support.RandomImpl", null,
259                null);
260        s2.toString();
261    }
262
263    class MyProvider extends Provider {
264        MyProvider() {
265            super("MyProvider", 1.0, "Provider for testing");
266            put("MessageDigest.SHA-1", "SomeClassName");
267        }
268
269    }
270
271    class MyService extends Provider.Service {
272
273        public MyService(Provider provider, String type, String algorithm,
274                String className, List<String> aliases,
275                Map<String, String> attributes) {
276            super(provider, type, algorithm, className, aliases, attributes);
277            // TODO Auto-generated constructor stub
278        }
279
280        @Override
281        public boolean supportsParameter(Object parameter) {
282            if (parameter.getClass() == String.class) {
283                return true;
284            }
285            return false;
286        }
287    }
288}
289