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.MessageDigest;
021: import java.security.NoSuchAlgorithmException;
022: import java.security.Provider;
023: import java.security.Security;
024:
025: public class Provider2Test extends junit.framework.TestCase {
026:
027: class TestProvider extends Provider {
028: TestProvider(String name, double version, String info) {
029: super (name, version, info);
030: }
031: }
032:
033: class MyEntry implements java.util.Map.Entry {
034: public Object getKey() {
035: return null;
036: }
037:
038: public Object getValue() {
039: return null;
040: }
041:
042: public Object setValue(Object value) {
043: return null;
044: }
045: }
046:
047: TestProvider provTest = new TestProvider("provTest", 1.2,
048: "contains nothings, purely for testing the class");
049:
050: /**
051: * @tests java.security.Provider#entrySet()
052: */
053: public void test_entrySet() {
054: // test method of java.security.provider.entrySet
055: provTest.put("test.prop", "this is a test property");
056: try {
057: //make it compilable on 1.5
058: provTest.entrySet().add(new MyEntry());
059: fail("was able to modify the entrySet");
060: } catch (UnsupportedOperationException e) {
061: // expected
062: }
063: }
064:
065: /**
066: * @tests java.security.Provider#getInfo()
067: */
068: public void test_getInfo() {
069: // test method of java.security.provider.getInfo
070: assertEquals(
071: "the information of the provider is not stored properly",
072: "contains nothings, purely for testing the class",
073: provTest.getInfo());
074: }
075:
076: /**
077: * @tests java.security.Provider#getName()
078: */
079: public void test_getName() {
080: // test method of java.security.provider.getName
081: assertEquals("the name of the provider is not stored properly",
082: "provTest", provTest.getName());
083: }
084:
085: /**
086: * @tests java.security.Provider#getVersion()
087: */
088: public void test_getVersion() {
089: // test method of java.security.provider.getVersion
090: assertEquals(
091: "the version of the provider is not stored properly",
092: 1.2, provTest.getVersion(), 0);
093: }
094:
095: /**
096: * @tests java.security.Provider#keySet()
097: */
098: public void test_keySet() {
099: // test method of java.security.provider.keySet
100: provTest.put("test.prop", "this is a test property");
101: try {
102: provTest.keySet().add("another property key");
103: fail("was able to modify the keySet");
104: } catch (UnsupportedOperationException e) {
105: // expected
106: }
107: }
108:
109: /**
110: * @tests java.security.Provider#values()
111: */
112: public void test_values() {
113: // test method of java.security.provider.values
114: provTest.put("test.prop", "this is a test property");
115: try {
116: provTest.values().add("another property value");
117: fail("was able to modify the values collection");
118: } catch (UnsupportedOperationException e) {
119: // expected
120: }
121: }
122:
123: /**
124: * @tests java.security.Provider#toString()
125: */
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
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,
139: null);
140: mockProvider.putService(service);
141: Security.addProvider(mockProvider);
142: try {
143: MessageDigest messageDigest = MessageDigest
144: .getInstance("NOTEXISTS");
145: }
146:
147: catch (NoSuchAlgorithmException e) {
148: // expected
149: } finally {
150: Security.removeProvider("MOCKNAME");
151: }
152: }
153:
154: public static class MockProvider extends Provider {
155:
156: private static final long serialVersionUID = 1L;
157:
158: public MockProvider(String name, double version, String info) {
159: super (name, version, info);
160:
161: }
162:
163: public void putService(Provider.Service service) {
164: super .putService(service);
165: }
166:
167: public void removeService(Provider.Service service) {
168: super.removeService(service);
169: }
170: }
171: }
|