001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: /**
019: * @author Boris V. Kuznetsov
020: * @version $Revision$
021: */package java.security;
022:
023: import java.util.HashMap;
024: import java.util.Map;
025: import java.util.Set;
026:
027: import junit.framework.TestCase;
028:
029: /**
030: * Tests for <code>Security</code> constructor and methods
031: *
032: */
033: public class Security_ImplTest extends TestCase {
034:
035: public final void testGetAlgorithmProperty() {
036: assertNull("Incorrect result on null parameter", Security
037: .getAlgorithmProperty(null, "MyService"));
038: assertNull("Incorrect result on null parameter", Security
039: .getAlgorithmProperty("MyAlgorithm", null));
040: assertNull("Incorrect result (provider not added)", Security
041: .getAlgorithmProperty("MyAlgorithm", "MyService"));
042:
043: Provider p = new MyProvider();
044: Security.addProvider(p);
045: try {
046: assertEquals("Incorrect result (provider added)",
047: "SomeClassName", Security.getAlgorithmProperty(
048: "MyAlGoriThm", "MySerVicE"));
049: } finally { //clean up
050: Security.removeProvider(p.getName());
051: }
052: }
053:
054: /*
055: * Class under test for Provider[] getProviders()
056: */
057: public final void testGetProviders() {
058: Provider[] providers;
059:
060: providers = Security.getProviders();
061: for (int i = 0; i < providers.length; i++) {
062: // position is 1-based
063: assertEquals("Incorrect provider number", i + 1,
064: providers[i].getProviderNumber());
065: }
066: }
067:
068: /**
069: * @tests java.security.Security#getProviders(String)
070: */
071: public final void test_getProvidersLjava_lang_String() {
072: // Regression for Harmony-3154 (non-bug difference)
073: try {
074: Security.getProviders("AAA.BBB CCC");
075: fail("AAA.BBB CCC: No expected InvalidParameterException");
076: } catch (InvalidParameterException e) {
077: }
078: }
079:
080: /**
081: * @tests java.security.Security#getProviders(Map)
082: */
083: public final void test_getProvidersLjava_util_Map() {
084: // Regression for Harmony-3154 (non-bug difference)
085: Map<String, String> m = new HashMap<String, String>();
086: m.put("AAA.BBB CCC", "");
087: m.put("AAA.BBB", "");
088: try {
089: Security.getProviders(m);
090: fail("attribute value is empty string: No expected InvalidParameterException");
091: } catch (InvalidParameterException e) {
092: }
093: }
094:
095: public final void testGetAlgorithms() {
096: Set alg1;
097: Set alg2;
098:
099: 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
115: .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: }
|