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: /**
019: * @author Boris V. Kuznetsov
020: * @version $Revision$
021: */package java.security;
022:
023: import java.io.FileInputStream;
024: import java.io.FileNotFoundException;
025: import java.io.IOException;
026: import java.util.HashMap;
027: import java.util.Iterator;
028: import java.util.Set;
029:
030: import junit.framework.TestCase;
031:
032: import org.apache.harmony.security.tests.support.SpiEngUtils;
033: import org.apache.harmony.security.tests.support.TestUtils;
034:
035: import tests.support.resource.Support_Resources;
036:
037: /**
038: * Tests for <code>Provider</code> constructor and methods
039: *
040: */
041: public class ProviderTest extends TestCase {
042:
043: Provider p;
044:
045: /*
046: * @see TestCase#setUp()
047: */
048: protected void setUp() throws Exception {
049: super .setUp();
050: p = new MyProvider();
051: }
052:
053: /*
054: * Class under test for void load(InputStream)
055: */
056: public final void testLoadInputStream() throws IOException {
057:
058: p.load(Support_Resources
059: .getResourceStream("java/security/Provider.prop.dat"));
060:
061: if (!"value 1".equals(p.getProperty("Property 1").trim())
062: || !"className".equals(p.getProperty(
063: "serviceName.algName").trim())
064: || !"attrValue".equals(p.getProperty(
065: "serviceName.algName attrName").trim())
066: || !"standardName".equals(p.getProperty(
067: "Alg.Alias.engineClassName.aliasName").trim())
068: || !String.valueOf(p.getName()).equals(
069: p.getProperty("Provider.id name").trim())
070: || !String.valueOf(p.getVersion()).equals(
071: p.getProperty("Provider.id version").trim())
072: || !String.valueOf(p.getInfo()).equals(
073: p.getProperty("Provider.id info").trim())
074: || !p.getClass().getName().equals(
075: p.getProperty("Provider.id className").trim())
076: || !"SomeClassName".equals(p.getProperty(
077: "MessageDigest.SHA-1").trim())) {
078: fail("Incorrect property value");
079: }
080: }
081:
082: public final void testGetService() {
083: try {
084: p.getService(null, "algorithm");
085: fail("No expected NullPointerException");
086: } catch (NullPointerException e) {
087: }
088: try {
089: p.getService("type", null);
090: fail("No expected NullPointerException");
091: } catch (NullPointerException e) {
092: }
093:
094: Provider.Service s = new Provider.Service(p, "Type",
095: "Algorithm", "className", null, null);
096: p.putService(s);
097:
098: if (p.getService("Type", "AlgoRithM") != s) {
099: fail("Case 1. getService() failed");
100: }
101:
102: Provider.Service s1 = p.getService("MessageDigest", "AbC");
103: if (s1 == null) {
104: fail("Case 2. getService() failed");
105: }
106:
107: s = new Provider.Service(p, "MessageDigest", "SHA-1",
108: "className", null, null);
109: p.putService(s);
110: if (s1 == p.getService("MessageDigest", "SHA-1")) {
111: fail("Case 3. getService() failed");
112: }
113:
114: if (p.getService("MessageDigest", "SHA1") == null) {
115: fail("Case 4. getService() failed");
116: }
117: }
118:
119: public final void testGetServices() {
120: Provider.Service s = new Provider.Service(p, "Type",
121: "Algorithm", "className", null, null);
122:
123: // incomplete services should be removed
124: p.put("serv.alg", "aaaaaaaaaaaaa");
125: p.put("serv.alg KeySize", "11111");
126: p.put("serv1.alg1 KeySize", "222222");
127: p.remove("serv.alg");
128:
129: p.putService(s);
130: Set services = p.getServices();
131: if (services.size() != 3) {
132: fail("incorrect size");
133: }
134: for (Iterator it = services.iterator(); it.hasNext();) {
135: s = (Provider.Service) it.next();
136: if ("Type".equals(s.getType())
137: && "Algorithm".equals(s.getAlgorithm())
138: && "className".equals(s.getClassName())) {
139: continue;
140: }
141: if ("MessageDigest".equals(s.getType())
142: && "SHA-1".equals(s.getAlgorithm())
143: && "SomeClassName".equals(s.getClassName())) {
144: continue;
145: }
146: if ("MessageDigest".equals(s.getType())
147: && "abc".equals(s.getAlgorithm())
148: && "SomeClassName".equals(s.getClassName())) {
149: continue;
150: }
151: fail("Incorrect service");
152: }
153: }
154:
155: public final void testPutService() {
156: HashMap hm = new HashMap();
157: hm.put("KeySize", "1024");
158: hm.put("AAA", "BBB");
159: Provider.Service s = new Provider.Service(p, "Type",
160: "Algorithm", "className", null, hm);
161: p.putService(s);
162: if (s != p.getService("Type", "Algorithm")) {
163: fail("putService failed");
164: }
165: if (!"className".equals(p.getProperty("Type.Algorithm"))) {
166: fail("incorrect className");
167: }
168: if (!"1024".equals(p.getProperty("Type.Algorithm KeySize"))) {
169: fail("incorrect attribute");
170: }
171: }
172:
173: public final void testRemoveService() {
174: Provider.Service s = new Provider.Service(p, "Type",
175: "Algorithm", "className", null, null);
176: p.putService(s);
177: p.removeService(s);
178: Set services = p.getServices();
179: if (services.size() != 2) {
180: fail("incorrect size");
181: }
182:
183: for (Iterator it = services.iterator(); it.hasNext();) {
184: s = (Provider.Service) it.next();
185: if ("MessageDigest".equals(s.getType())
186: && "SHA-1".equals(s.getAlgorithm())
187: && "SomeClassName".equals(s.getClassName())) {
188: continue;
189: }
190: if ("MessageDigest".equals(s.getType())
191: && "abc".equals(s.getAlgorithm())
192: && "SomeClassName".equals(s.getClassName())) {
193: continue;
194: }
195: fail("Incorrect service");
196: }
197:
198: if (p.getProperty("Type.Algorithm") != null) {
199: fail("incorrect property");
200: }
201: }
202:
203: class MyProvider extends Provider {
204: MyProvider() {
205: super ("MyProvider", 1.0, "Provider for testing");
206: put("MessageDigest.SHA-1", "SomeClassName");
207: put("MessageDigest.abc", "SomeClassName");
208: put("Alg.Alias.MessageDigest.SHA1", "SHA-1");
209: }
210:
211: MyProvider(String name, double version, String info) {
212: super(name, version, info);
213: }
214: }
215: }
|