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: package org.apache.harmony.security.tests.java.security;
019:
020: import java.security.InvalidParameterException;
021: import java.security.Provider;
022: import java.security.Security;
023: import java.util.Hashtable;
024: import java.util.Iterator;
025: import java.util.Map;
026: import java.util.Set;
027:
028: import tests.support.Support_ProviderTrust;
029: import tests.support.Support_TestProvider;
030:
031: public class Security2Test extends junit.framework.TestCase {
032:
033: /**
034: * @tests java.security.Security#getProviders(java.lang.String)
035: */
036: public void test_getProvidersLjava_lang_String() {
037: // Test for method void
038: // java.security.Security.getProviders(java.lang.String)
039:
040: Hashtable allSupported = new Hashtable();
041: Provider[] allProviders = Security.getProviders();
042:
043: // Add all non-alias entries to allSupported
044: for (int i = 0; i < allProviders.length; i++) {
045: Provider provider = allProviders[i];
046: Iterator it = provider.entrySet().iterator();
047: while (it.hasNext()) {
048: Map.Entry entry = (Map.Entry) it.next();
049: String key = (String) entry.getKey();
050: // No aliases and no provider data
051: if (!isAlias(key) && !isProviderData(key)) {
052: addOrIncrementTable(allSupported, key);
053: }
054: }// end while more entries
055: }// end for all providers
056:
057: // Now walk through aliases. If an alias has actually been added
058: // to the allSupported table then increment the count of the
059: // entry that is being aliased.
060: for (int i = 0; i < allProviders.length; i++) {
061: Provider provider = allProviders[i];
062: Iterator it = provider.entrySet().iterator();
063: while (it.hasNext()) {
064: Map.Entry entry = (Map.Entry) it.next();
065: String key = (String) entry.getKey();
066: if (isAlias(key)) {
067: String aliasVal = key.substring("ALG.ALIAS."
068: .length());
069: String aliasKey = aliasVal.substring(0, aliasVal
070: .indexOf(".") + 1)
071: + entry.getValue();
072: // Skip over nonsense alias declarations where alias and
073: // aliased are identical. Such entries can occur.
074: if (!aliasVal.equals(aliasKey)) {
075: // Has a real entry been added for aliasValue ?
076: if (allSupported.containsKey(aliasVal)) {
077: // Add 1 to the provider count of the thing being
078: // aliased
079: addOrIncrementTable(allSupported, aliasKey);
080: }
081: }
082: }
083: }// end while more entries
084: }// end for all providers
085:
086: Provider provTest[] = null;
087: Iterator it = allSupported.keySet().iterator();
088: while (it.hasNext()) {
089: String filterString = (String) it.next();
090: try {
091: provTest = Security.getProviders(filterString);
092: int expected = ((Integer) allSupported
093: .get(filterString)).intValue();
094: assertEquals(
095: "Unexpected number of providers returned for filter "
096: + filterString, expected,
097: provTest.length);
098: } catch (InvalidParameterException e) {
099: // NO OP
100: }
101: }// end while
102:
103: // exception
104: try {
105: provTest = Security
106: .getProviders("Signature.SHA1withDSA :512");
107: fail("InvalidParameterException should be thrown <Signature.SHA1withDSA :512>");
108: } catch (InvalidParameterException e) {
109: // Expected
110: }
111: }
112:
113: /**
114: * @param key
115: * @return
116: */
117: private boolean isProviderData(String key) {
118: return key.toUpperCase().startsWith("PROVIDER.");
119: }
120:
121: /**
122: * @param key
123: * @return
124: */
125: private boolean isAlias(String key) {
126: return key.toUpperCase().startsWith("ALG.ALIAS.");
127: }
128:
129: /**
130: * @param table
131: * @param key
132: */
133: private void addOrIncrementTable(Hashtable table, String key) {
134: if (table.containsKey(key)) {
135: Integer before = (Integer) table.get(key);
136: table.put(key, new Integer(before.intValue() + 1));
137: } else {
138: table.put(key, new Integer(1));
139: }
140: }
141:
142: /**
143: * @param filterMap
144: * @return
145: */
146: private int getProvidersCount(Map filterMap) {
147: int result = 0;
148: Provider[] allProviders = Security.getProviders();
149:
150: // for each provider
151: for (int i = 0; i < allProviders.length; i++) {
152: Provider provider = allProviders[i];
153: Set allProviderKeys = provider.keySet();
154: boolean noMatchFoundForFilterEntry = false;
155:
156: // for each filter item
157: Set allFilterKeys = filterMap.keySet();
158: Iterator fkIter = allFilterKeys.iterator();
159: while (fkIter.hasNext()) {
160: String filterString = ((String) fkIter.next()).trim();
161:
162: // Remove any "=" characters that may be on the end of the
163: // map keys (no, I don't know why they might be there either
164: // but I have seen them)
165: if (filterString.endsWith("=")) {
166: filterString = filterString.substring(0,
167: filterString.length() - 1);
168: }
169:
170: if (filterString != null) {
171: if (filterString.indexOf(" ") == -1) {
172: // Is this filter string in the keys of the
173: // current provider ?
174: if (!allProviderKeys.contains(filterString)) {
175: // Check that the key is not contained as an
176: // alias.
177: if (!allProviderKeys.contains("Alg.Alias."
178: + filterString)) {
179: noMatchFoundForFilterEntry = true;
180: break; // out of while loop
181: }
182: }
183: } else {
184: // handle filter strings with attribute names
185: if (allProviderKeys.contains(filterString)) {
186: // Does the corresponding values match ?
187: String filterVal = (String) filterMap
188: .get(filterString);
189: String providerVal = (String) provider
190: .get(filterString);
191: if (providerVal == null
192: || !providerVal.equals(filterVal)) {
193: noMatchFoundForFilterEntry = true;
194: break; // out of while loop
195: }
196: }// end if filter string with named attribute is
197: // found
198: }// end else
199: }// end if non-null key
200: }// end while there are more filter strings for current map
201:
202: if (!noMatchFoundForFilterEntry) {
203: // Current provider is a match for the filterMap
204: result++;
205: }
206: }// end for each provider
207:
208: return result;
209: }
210:
211: /**
212: * @tests java.security.Security#getProviders(java.util.Map)
213: */
214: public void test_getProvidersLjava_util_Map() {
215: // Test for method void
216: // java.security.Security.getProviders(java.util.Map)
217:
218: Map filter = new Hashtable();
219: filter.put("KeyStore.BKS", "");
220: filter.put("Signature.SHA1withDSA", "");
221: Provider provTest[] = Security.getProviders(filter);
222: if (provTest == null) {
223: assertEquals(
224: "Filter : <KeyStore.BKS>,<Signature.SHA1withDSA>",
225: 0, getProvidersCount(filter));
226: } else {
227: assertEquals(
228: "Filter : <KeyStore.BKS>,<Signature.SHA1withDSA>",
229: getProvidersCount(filter), provTest.length);
230: }
231:
232: filter = new Hashtable();
233: filter.put("MessageDigest.MD2", "");
234: filter.put("CertificateFactory.X.509", "");
235: filter.put("KeyFactory.RSA", "");
236: provTest = Security.getProviders(filter);
237: if (provTest == null) {
238: assertEquals(
239: "Filter : <MessageDigest.MD2>,<CertificateFactory.X.509>,<KeyFactory.RSA>",
240: 0, getProvidersCount(filter));
241: } else {
242: assertEquals(
243: "Filter : <MessageDigest.MD2>,<CertificateFactory.X.509>,<KeyFactory.RSA>",
244: getProvidersCount(filter), provTest.length);
245: }
246:
247: filter = new Hashtable();
248: filter.put("MessageDigest.SHA", "");
249: filter.put("CertificateFactory.X.509", "");
250: provTest = Security.getProviders(filter);
251: if (provTest == null) {
252: assertEquals(
253: "Filter : <MessageDigest.SHA><CertificateFactory.X.509>",
254: 0, getProvidersCount(filter));
255: } else {
256: assertEquals(
257: "Filter : <MessageDigest.SHA><CertificateFactory.X.509>",
258: getProvidersCount(filter), provTest.length);
259: }
260:
261: filter = new Hashtable();
262: filter.put("CertificateFactory.X509", "");
263: provTest = Security.getProviders(filter);
264: if (provTest == null) {
265: assertEquals("Filter : <CertificateFactory.X509>", 0,
266: getProvidersCount(filter));
267: } else {
268: assertEquals("Filter : <CertificateFactory.X509>",
269: getProvidersCount(filter), provTest.length);
270: }
271:
272: filter = new Hashtable();
273: filter.put("Provider.id name", "DRLCertFactory");
274: provTest = Security.getProviders(filter);
275: assertNull("Filter : <Provider.id name, DRLCertFactory >",
276: provTest);
277:
278: // exception - no attribute name after the service.algorithm yet we
279: // still supply an expected value. This is not valid.
280: try {
281: filter = new Hashtable();
282: filter.put("Signature.SHA1withDSA", "512");
283: provTest = Security.getProviders(filter);
284: fail("InvalidParameterException should be thrown <Signature.SHA1withDSA><512>");
285: } catch (InvalidParameterException e) {
286: // Expected
287: }
288:
289: // exception - space character in the service.algorithm pair. Not valid.
290: try {
291: filter = new Hashtable();
292: filter.put("Signature. KeySize", "512");
293: provTest = Security.getProviders(filter);
294: fail("InvalidParameterException should be thrown <Signature. KeySize><512>");
295: } catch (InvalidParameterException e) {
296: // Expected
297: }
298: }
299:
300: /**
301: * @tests java.security.Security#removeProvider(java.lang.String)
302: */
303: public void test_removeProviderLjava_lang_String() {
304: // Test for method void
305: // java.security.Security.removeProvider(java.lang.String)
306: Provider test = new Support_TestProvider();
307: Provider entrust = new Support_ProviderTrust();
308: try {
309: // Make sure provider not already loaded. Should do nothing
310: // if not already loaded.
311: Security.removeProvider(test.getName());
312:
313: // Now add it
314: int addResult = Security.addProvider(test);
315: assertTrue("Failed to add provider", addResult != -1);
316:
317: Security.removeProvider(test.getName());
318: assertNull(
319: "the provider TestProvider is found after it was removed",
320: Security.getProvider(test.getName()));
321:
322: // Make sure entrust provider not already loaded. Should do nothing
323: // if not already loaded.
324: Security.removeProvider(entrust.getName());
325:
326: // Now add entrust
327: addResult = Security.addProvider(entrust);
328: assertTrue("Failed to add provider", addResult != -1);
329:
330: Security.removeProvider(entrust.getName());
331: Provider provTest[] = Security.getProviders();
332: for (int i = 0; i < provTest.length; i++) {
333: assertTrue(
334: "the provider entrust is found after it was removed",
335: provTest[i].getName() != entrust.getName());
336: }
337: } finally {
338: // Tidy up - the following calls do nothing if the providers were
339: // already removed above.
340: Security.removeProvider(test.getName());
341: Security.removeProvider(entrust.getName());
342: }
343: }
344: }
|