• 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 package org.apache.harmony.security.tests.java.security;
19 
20 import java.security.MessageDigest;
21 import java.security.NoSuchAlgorithmException;
22 import java.security.Provider;
23 import java.security.Security;
24 
25 public class Provider2Test extends junit.framework.TestCase {
26 
27     class TestProvider extends Provider {
TestProvider(String name, double version, String info)28         TestProvider(String name, double version, String info) {
29             super(name, version, info);
30         }
31     }
32 
33     class MyEntry implements java.util.Map.Entry {
getKey()34          public Object getKey() {
35              return null;
36          }
37 
getValue()38          public Object getValue() {
39              return null;
40          }
41 
setValue(Object value)42          public Object setValue(Object value) {
43              return null;
44          }
45     }
46 
47     TestProvider provTest = new TestProvider("provTest", 1.2,
48             "contains nothings, purely for testing the class");
49 
50 
51     /**
52      * java.security.Provider#entrySet()
53      */
test_entrySet()54     public void test_entrySet() {
55         // test method of java.security.provider.entrySet
56         provTest.put("test.prop", "this is a test property");
57         try {
58             //make it compilable on 1.5
59             provTest.entrySet().add(new MyEntry());
60             fail("was able to modify the entrySet");
61         } catch (UnsupportedOperationException e) {
62             // expected
63         }
64     }
65 
66     /**
67      * java.security.Provider#getInfo()
68      */
test_getInfo()69     public void test_getInfo() {
70         // test method of java.security.provider.getInfo
71         assertEquals("the information of the provider is not stored properly",
72                 "contains nothings, purely for testing the class", provTest
73                         .getInfo());
74     }
75 
76     /**
77      * java.security.Provider#getName()
78      */
test_getName()79     public void test_getName() {
80         // test method of java.security.provider.getName
81         assertEquals("the name of the provider is not stored properly",
82                 "provTest", provTest.getName());
83     }
84 
85     /**
86      * java.security.Provider#getVersion()
87      */
test_getVersion()88     public void test_getVersion() {
89         // test method of java.security.provider.getVersion
90         assertEquals("the version of the provider is not stored properly",
91                 1.2, provTest.getVersion(), 0);
92     }
93 
94     /**
95      * java.security.Provider#keySet()
96      */
test_keySet()97     public void test_keySet() {
98         // test method of java.security.provider.keySet
99         provTest.put("test.prop", "this is a test property");
100         try {
101             provTest.keySet().add("another property key");
102             fail("was able to modify the keySet");
103         } catch (UnsupportedOperationException e) {
104             // expected
105         }
106     }
107 
108     /**
109      * java.security.Provider#values()
110      */
test_values()111     public void test_values() {
112         // test method of java.security.provider.values
113         provTest.put("test.prop", "this is a test property");
114         try {
115             provTest.values().add("another property value");
116             fail("was able to modify the values collection");
117         } catch (UnsupportedOperationException e) {
118             // expected
119         }
120     }
121 
122 
123     /**
124      * java.security.Provider#toString()
125      */
test_toString()126     public void test_toString() {
127         // Regression for HARMONY-3734
128         assertEquals("provTest version 1.2", provTest.toString());
129     }
130 
131     // Regression Test for Provider.Service.getAlias(), which is an package
132     // private method but will lead to NPE thrown at
133     // Secure.SecurityDorr.getAliases
test_getAlias()134     public void test_getAlias() throws Exception {
135         MockProvider mockProvider = new MockProvider("MOCKNAME", 1.0,
136                 "MOCKINFO");
137         Provider.Service service = new Provider.Service(mockProvider,
138                 "MOCKTYPE", "MOCKALGORITHM", "TESTCLASSNAME", null, null);
139         mockProvider.putService(service);
140         Security.addProvider(mockProvider);
141         try {
142             MessageDigest messageDigest = MessageDigest
143                     .getInstance("NOTEXISTS");
144         }
145 
146         catch (NoSuchAlgorithmException e) {
147             // expected
148         } finally {
149             Security.removeProvider("MOCKNAME");
150         }
151     }
152 
153     public static class MockProvider extends Provider {
154 
155         private static final long serialVersionUID = 1L;
156 
MockProvider(String name, double version, String info)157         public MockProvider(String name, double version, String info) {
158             super(name, version, info);
159 
160         }
161 
putService(Provider.Service service)162         public void putService(Provider.Service service) {
163             super.putService(service);
164         }
165 
removeService(Provider.Service service)166         public void removeService(Provider.Service service) {
167             super.removeService(service);
168         }
169     }
170 }
171