Home | History | Annotate | Download | only in security
      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.io.FileInputStream;
     25 import java.io.FileNotFoundException;
     26 import java.io.IOException;
     27 import java.util.HashMap;
     28 import java.util.Iterator;
     29 import java.util.Set;
     30 
     31 import junit.framework.TestCase;
     32 
     33 import org.apache.harmony.security.tests.support.SpiEngUtils;
     34 import org.apache.harmony.security.tests.support.TestUtils;
     35 
     36 import tests.support.resource.Support_Resources;
     37 
     38 
     39 /**
     40  * Tests for <code>Provider</code> constructor and methods
     41  *
     42  */
     43 public class ProviderTest extends TestCase {
     44 
     45     Provider p;
     46 
     47     /*
     48      * @see TestCase#setUp()
     49      */
     50     protected void setUp() throws Exception {
     51         super.setUp();
     52         p = new MyProvider();
     53     }
     54 
     55     /*
     56      * Class under test for void load(InputStream)
     57      */
     58     public final void testLoadInputStream() throws IOException {
     59 
     60         p.load(Support_Resources
     61                 .getResourceStream("java/security/Provider.prop.dat"));
     62 
     63         if (!"value 1".equals(p.getProperty("Property 1").trim()) ||
     64                 !"className".equals(p.getProperty("serviceName.algName").trim()) ||
     65                 !"attrValue".equals(p.getProperty("serviceName.algName attrName").trim()) ||
     66                 !"standardName".equals(p.getProperty("Alg.Alias.engineClassName.aliasName").trim()) ||
     67                 !String.valueOf(p.getName()).equals(p.getProperty("Provider.id name").trim()) ||
     68                 !String.valueOf(p.getVersion()).equals(p.getProperty("Provider.id version").trim()) ||
     69                 !String.valueOf(p.getInfo()).equals(p.getProperty("Provider.id info").trim()) ||
     70                 !p.getClass().getName().equals(p.getProperty("Provider.id className").trim()) ||
     71                 !"SomeClassName".equals(p.getProperty("MessageDigest.SHA-1").trim()) ) {
     72             fail("Incorrect property value");
     73         }
     74     }
     75 
     76     public final void testGetService() {
     77         try {
     78             p.getService(null, "algorithm");
     79             fail("No expected NullPointerException");
     80         } catch (NullPointerException e) {
     81         }
     82         try {
     83             p.getService("type", null);
     84             fail("No expected NullPointerException");
     85         } catch (NullPointerException e) {
     86         }
     87 
     88         Provider.Service s = new Provider.Service(p, "Type", "Algorithm",
     89                 "className", null, null);
     90         p.putService(s);
     91 
     92         if (p.getService("Type", "AlgoRithM") != s) {
     93             fail("Case 1. getService() failed");
     94         }
     95 
     96         Provider.Service s1 = p.getService("MessageDigest", "AbC");
     97         if (s1 == null) {
     98             fail("Case 2. getService() failed");
     99         }
    100 
    101         s = new Provider.Service(p, "MessageDigest", "SHA-1",
    102                 "className", null, null);
    103         p.putService(s);
    104         if (s1 == p.getService("MessageDigest", "SHA-1")) {
    105             fail("Case 3. getService() failed");
    106         }
    107 
    108         if (p.getService("MessageDigest", "SHA1") == null) {
    109             fail("Case 4. getService() failed");
    110         }
    111     }
    112 
    113     public final void testGetServices() {
    114         Provider.Service s = new Provider.Service(p, "Type", "Algorithm",
    115                 "className", null, null);
    116 
    117         // incomplete services should be removed
    118         p.put("serv.alg", "aaaaaaaaaaaaa");
    119         p.put("serv.alg KeySize", "11111");
    120         p.put("serv1.alg1 KeySize", "222222");
    121         p.remove("serv.alg");
    122 
    123         p.putService(s);
    124         Set services = p.getServices();
    125         if (services.size() != 3) {
    126             fail("incorrect size");
    127         }
    128         for (Iterator it = services.iterator(); it.hasNext();) {
    129             s = (Provider.Service)it.next();
    130             if ("Type".equals(s.getType()) &&
    131                     "Algorithm".equals(s.getAlgorithm()) &&
    132                     "className".equals(s.getClassName())) {
    133                 continue;
    134             }
    135             if ("MessageDigest".equals(s.getType()) &&
    136                     "SHA-1".equals(s.getAlgorithm()) &&
    137                     "SomeClassName".equals(s.getClassName())) {
    138                 continue;
    139             }
    140             if ("MessageDigest".equals(s.getType()) &&
    141                     "abc".equals(s.getAlgorithm()) &&
    142                     "SomeClassName".equals(s.getClassName())) {
    143                 continue;
    144             }
    145             fail("Incorrect service");
    146         }
    147     }
    148 
    149     public final void testPutService() {
    150         HashMap hm = new HashMap();
    151         hm.put("KeySize", "1024");
    152         hm.put("AAA", "BBB");
    153         Provider.Service s = new Provider.Service(p, "Type", "Algorithm",
    154                 "className", null, hm);
    155         p.putService(s);
    156         if (s != p.getService("Type", "Algorithm")){
    157             fail("putService failed");
    158         }
    159         if (!"className".equals(p.getProperty("Type.Algorithm"))) {
    160             fail("incorrect className");
    161         }
    162         if (!"1024".equals(p.getProperty("Type.Algorithm KeySize"))) {
    163             fail("incorrect attribute");
    164         }
    165     }
    166 
    167     public final void testRemoveService() {
    168         Provider.Service s = new Provider.Service(p, "Type", "Algorithm",
    169                 "className", null, null);
    170         p.putService(s);
    171         p.removeService(s);
    172         Set services = p.getServices();
    173         if (services.size() != 2) {
    174             fail("incorrect size");
    175         }
    176 
    177         for (Iterator it = services.iterator(); it.hasNext();) {
    178             s = (Provider.Service)it.next();
    179             if ("MessageDigest".equals(s.getType()) &&
    180                     "SHA-1".equals(s.getAlgorithm()) &&
    181                     "SomeClassName".equals(s.getClassName())) {
    182                 continue;
    183             }
    184             if ("MessageDigest".equals(s.getType()) &&
    185                     "abc".equals(s.getAlgorithm()) &&
    186                     "SomeClassName".equals(s.getClassName())) {
    187                 continue;
    188             }
    189             fail("Incorrect service");
    190         }
    191 
    192         if (p.getProperty("Type.Algorithm") != null) {
    193             fail("incorrect property");
    194         }
    195     }
    196 
    197     class MyProvider extends Provider {
    198         MyProvider() {
    199             super("MyProvider", 1.0, "Provider for testing");
    200             put("MessageDigest.SHA-1", "SomeClassName");
    201             put("MessageDigest.abc", "SomeClassName");
    202             put("Alg.Alias.MessageDigest.SHA1", "SHA-1");
    203         }
    204 
    205         MyProvider(String name, double version, String info) {
    206             super(name, version, info);
    207         }
    208     }
    209 }
    210