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 org.apache.harmony.security.tests.support.MySignature1;
024: import org.apache.harmony.security.tests.support.MySignature2;
025:
026: import junit.framework.TestCase;
027:
028: /**
029: * Tests for <code>Signature</code> constructor and methods
030: *
031: */
032: public class Signature_Impl2Test extends TestCase {
033:
034: /**
035: * Provider
036: */
037: Provider p;
038:
039: /*
040: * @see TestCase#setUp()
041: */
042: protected void setUp() throws Exception {
043: super .setUp();
044: p = new MyProvider();
045: Security.insertProviderAt(p, 1);
046: }
047:
048: /*
049: * @see TestCase#tearDown()
050: */
051: protected void tearDown() throws Exception {
052: super .tearDown();
053: Security.removeProvider(p.getName());
054: }
055:
056: /*
057: * Class under test for Signature getInstance(String)
058: */
059: public void testGetInstanceString1() throws Exception {
060: Signature sig = Signature.getInstance("ABC");
061: checkSig1(sig, p);
062: }
063:
064: /*
065: * Class under test for Signature getInstance(String)
066: */
067: public void testGetInstanceString2() throws Exception {
068: Signature sig = Signature.getInstance("CBA");
069: checkSig2(sig, p);
070: }
071:
072: /*
073: * Class under test for Signature getInstance(String, String)
074: */
075: public void testGetInstanceStringString1() throws Exception {
076: Signature sig = Signature.getInstance("ABC", "MyProvider");
077: checkSig1(sig, p);
078: }
079:
080: /*
081: * Class under test for Signature getInstance(String, String)
082: */
083: public void testGetInstanceStringString2() throws Exception {
084: Signature sig = Signature.getInstance("CBA", "MyProvider");
085: checkSig2(sig, p);
086: }
087:
088: /*
089: * Class under test for Signature getInstance(String, Provider)
090: */
091: public void testGetInstanceStringProvider1() throws Exception {
092: Provider p1 = new MyProvider();
093: Signature sig = Signature.getInstance("ABC", p1);
094: checkSig1(sig, p1);
095: }
096:
097: /*
098: * Class under test for Signature getInstance(String, Provider)
099: */
100: public void testGetInstanceStringProvider2() throws Exception {
101: Provider p2 = new MyProvider();
102: Signature sig = Signature.getInstance("CBA", p2);
103: checkSig2(sig, p2);
104: }
105:
106: private void checkSig1(Signature s, Provider p) throws Exception {
107: byte[] b = { 1, 2, 3, 4 };
108: assertTrue("getInstance() failed", s instanceof MySignature1);
109: assertEquals("getProvider() failed", p, s.getProvider());
110: assertEquals("getAlgorithm() failed", "ABC", s.getAlgorithm());
111:
112: try {
113: s.sign();
114: fail("No expected SignatureException");
115: } catch (SignatureException e) {
116: }
117:
118: s.initVerify(new MyPublicKey());
119:
120: try {
121: s.sign();
122: fail("No expected SignatureException");
123: } catch (SignatureException e) {
124: }
125:
126: s.initSign(new MyPrivateKey());
127: s.sign();
128: assertEquals("Incorrect state", Signature.SIGN,
129: ((MySignature1) s).getState());
130: assertTrue("sign() failed", ((MySignature1) s).runEngineSign);
131:
132: s.initVerify(new MyPublicKey());
133: s.update((byte) 1);
134:
135: s.initSign(new MyPrivateKey());
136: s.update((byte) 1);
137:
138: assertEquals("Incorrect state", Signature.SIGN,
139: ((MySignature1) s).getState());
140: assertTrue("sign() failed", ((MySignature1) s).runEngineUpdate1);
141:
142: s.initSign(new MyPrivateKey());
143:
144: try {
145: s.verify(b);
146: fail("No expected SignatureException");
147: } catch (SignatureException e) {
148: }
149: s.initVerify(new MyPublicKey());
150: s.verify(b);
151: assertEquals("Incorrect state", Signature.VERIFY,
152: ((MySignature1) s).getState());
153: assertTrue("verify() failed",
154: ((MySignature1) s).runEngineVerify);
155: }
156:
157: private void checkSig2(Signature s, Provider p) throws Exception {
158: byte[] b = { 1, 2, 3, 4 };
159:
160: assertEquals("getProvider() failed", p, s.getProvider());
161: assertEquals("getAlgorithm() failed", "CBA", s.getAlgorithm());
162:
163: s.initVerify(new MyCertificate());
164:
165: try {
166: s.sign(b, 0, 5);
167: fail("No expected IllegalArgumentException 1");
168: } catch (IllegalArgumentException e) {
169: }
170:
171: s.initSign(new MyPrivateKey());
172: s.sign(b, 0, 3);
173:
174: assertTrue("sign() failed", MySignature2.runEngineSign);
175: s.update(b);
176: s.initSign(new MyPrivateKey());
177: s.update(b);
178: assertTrue("update() failed", MySignature2.runEngineUpdate2);
179:
180: s.initSign(new MyPrivateKey());
181: try {
182: s.verify(b, 0, 3);
183: fail("No expected SignatureException");
184: } catch (SignatureException e) {
185: }
186: s.initVerify(new MyPublicKey());
187:
188: try {
189: s.verify(b, 0, 5);
190: fail("No expected IllegalArgumentException");
191: } catch (IllegalArgumentException e) {
192: } catch (SignatureException e) {
193: }
194:
195: s.verify(b, 0, 3);
196: assertTrue("verify() failed", MySignature2.runEngineVerify);
197: }
198:
199: private class MyProvider extends Provider {
200: MyProvider() {
201: super ("MyProvider", 1.0, "Provider for testing");
202: put("Signature.ABC",
203: "org.apache.harmony.security.tests.support.MySignature1");
204: put("Signature.CBA",
205: "org.apache.harmony.security.tests.support.MySignature2");
206: }
207:
208: MyProvider(String name, double version, String info) {
209: super (name, version, info);
210: }
211: }
212:
213: private class MyKey implements Key {
214: public String getFormat() {
215: return "123";
216: }
217:
218: public byte[] getEncoded() {
219: return null;
220: }
221:
222: public String getAlgorithm() {
223: return "aaa";
224: }
225: }
226:
227: private class MyPublicKey extends MyKey implements PublicKey {
228: }
229:
230: private class MyPrivateKey extends MyKey implements PrivateKey {
231: }
232:
233: private class MyCertificate extends java.security.cert.Certificate {
234: public MyCertificate() {
235: super ("MyCertificateType");
236: }
237:
238: public PublicKey getPublicKey() {
239: return new MyPublicKey();
240: }
241:
242: public byte[] getEncoded() {
243: return null;
244: }
245:
246: public void verify(PublicKey key) {
247: }
248:
249: public void verify(PublicKey key, String sigProvider) {
250: }
251:
252: public String toString() {
253: return "MyCertificate";
254: }
255: }
256: }
|