Security_ImplTest.java revision 561ee011997c6c2f1befbfaa9d5f0a99771c1d63
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*/
21
22package java.security;
23
24import java.util.HashMap;
25import java.util.Map;
26import java.util.Set;
27
28import junit.framework.TestCase;
29
30
31/**
32 * Tests for <code>Security</code> constructor and methods
33 *
34 */
35public class Security_ImplTest extends TestCase {
36
37    public final void testGetAlgorithmProperty() {
38        assertNull("Incorrect result on null parameter", Security
39                .getAlgorithmProperty(null, "MyService"));
40        assertNull("Incorrect result on null parameter", Security
41                .getAlgorithmProperty("MyAlgorithm", null));
42        assertNull("Incorrect result (provider not added)", Security
43                .getAlgorithmProperty("MyAlgorithm", "MyService"));
44
45        Provider p = new MyProvider();
46        Security.addProvider(p);
47        try {
48            assertEquals("Incorrect result (provider added)",
49                    "SomeClassName", Security.getAlgorithmProperty("MyAlGoriThm", "MySerVicE"));
50        } finally { //clean up
51            Security.removeProvider(p.getName());
52        }
53    }
54
55    /*
56     * Class under test for Provider[] getProviders()
57     */
58    public final void testGetProviders() {
59        Provider[] providers;
60
61        providers = Security.getProviders();
62        for (int i = 0; i < providers.length; i++) {
63            // position is 1-based
64            assertEquals("Incorrect provider number", i + 1, providers[i]
65                    .getProviderNumber());
66        }
67    }
68
69    /**
70     * @tests java.security.Security#getProviders(String)
71     */
72    public final void test_getProvidersLjava_lang_String() {
73        // Regression for Harmony-3154 (non-bug difference)
74        try {
75            Security.getProviders("AAA.BBB CCC");
76            fail("AAA.BBB CCC: No expected InvalidParameterException");
77        } catch (InvalidParameterException e) {
78        }
79    }
80
81    /**
82     * @tests java.security.Security#getProviders(Map)
83     */
84    public final void test_getProvidersLjava_util_Map() {
85        // Regression for Harmony-3154 (non-bug difference)
86        Map<String, String> m = new HashMap<String, String>();
87        m.put("AAA.BBB CCC", "");
88        m.put("AAA.BBB", "");
89        try {
90            Security.getProviders(m);
91            fail("attribute value is empty string: No expected InvalidParameterException");
92        } catch (InvalidParameterException  e) {
93        }
94    }
95
96    public final void testGetAlgorithms() {
97        Set alg1;
98        Set alg2;
99
100        alg1 = Security.getAlgorithms("AAAAAAAAAAAAAAA");
101        assertTrue("failed for non-existent service", alg1 != null);
102        assertEquals("failed for non-existent service", 0, alg1.size());
103
104        alg1 = Security.getAlgorithms("SecureRandom");
105        alg2 = Security.getAlgorithms("seCuReranDom");
106        assertEquals("different size", alg1.size(), alg2.size());
107        assertTrue("different content", alg2.containsAll(alg1));
108
109        Provider p = new MyProvider();
110
111        try {
112            Security.addProvider(p);
113            alg1 = Security.getAlgorithms("MyService");
114            assertEquals("failed for MyService", 1, alg1.size());
115            assertTrue("failed for MyService", alg1.contains("MyAlgorithm"));
116        } finally { //clean up
117            Security.removeProvider(p.getName());
118        }
119    }
120
121    @SuppressWarnings("serial")
122    class MyProvider extends Provider {
123        MyProvider() {
124            super("MyProvider", 1.0, "Provider for testing");
125            put("MessageDigest.SHA-1", "SomeClassName");
126            put("MyService.MyAlgorithm", "SomeClassName");
127            put("MyService.MyAlgorithm KeySize", "1024");
128        }
129    }
130
131}
132