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 26 import junit.framework.TestCase; 27 28 /** 29 * Tests for <code>Provider</code> constructor and methods 30 */ 31 public class Provider_ImplTest extends TestCase { 32 33 Provider p; 34 35 /* 36 * @see TestCase#setUp() 37 */ setUp()38 protected void setUp() throws Exception { 39 super.setUp(); 40 p = new MyProvider(); 41 } 42 43 /* 44 * Class under test for String toString() 45 */ testToString()46 public final void testToString() { 47 assertEquals("Incorrect provider.toString()", "MyProvider version 1.0", 48 p.toString()); 49 } 50 testImplementsAlg()51 public final void testImplementsAlg() { 52 HashMap hm = new HashMap(); 53 hm.put("KeySize", "1024"); 54 hm.put("AAA", "BBB"); 55 Provider.Service s = new Provider.Service(p, "Type", "Algorithm", 56 "className", null, hm); 57 p.putService(s); 58 if (!p.implementsAlg("Type", "Algorithm", null, null) || 59 !p.implementsAlg("MessageDigest", "SHA-1", null, null)) { 60 fail("Case 1. implementsAlg failed"); 61 } 62 if (!p.implementsAlg("Type", "Algorithm", "KeySize", "512")) { 63 fail("Case 2. implementsAlg failed"); 64 } 65 if (p.implementsAlg("Type", "Algorithm", "KeySize", "1025")) { 66 fail("Case 3. implementsAlg failed"); 67 } 68 if (!p.implementsAlg("Type", "Algorithm", "AAA", "BBB")) { 69 fail("Case 3. implementsAlg failed"); 70 } 71 } 72 testSetProviderNumber()73 public final void testSetProviderNumber() { 74 p.setProviderNumber(100); 75 assertEquals("Incorrect ProviderNumber", 100, p.getProviderNumber()); 76 } 77 testGetProviderNumber()78 public final void testGetProviderNumber() { 79 assertEquals("Incorrect ProviderNumber", -1, p.getProviderNumber()); 80 81 int i = Security.addProvider(p); 82 assertEquals("Incorrect ProviderNumber", i, p.getProviderNumber()); 83 Security.removeProvider(p.getName()); // clean up 84 } 85 86 class MyProvider extends Provider { MyProvider()87 MyProvider() { 88 super("MyProvider", 1.0, "Provider for testing"); 89 put("MessageDigest.SHA-1", "SomeClassName"); 90 put("MessageDigest.abc", "SomeClassName"); 91 put("Alg.Alias.MessageDigest.SHA1", "SHA-1"); 92 } 93 MyProvider(String name, double version, String info)94 MyProvider(String name, double version, String info) { 95 super(name, version, info); 96 } 97 } 98 } 99