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