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:
025: import junit.framework.TestCase;
026:
027: /**
028: * Tests for <code>Signature</code> constructor and methods
029: *
030: */
031: public class SignatureTest extends TestCase {
032:
033: /*
034: * Class under test for Object clone()
035: */
036: public void testClone() {
037: MySignature1 s = new MySignature1("ABC");
038: try {
039: s.clone();
040: fail("No expected CloneNotSupportedException");
041: } catch (CloneNotSupportedException e) {
042: }
043: }
044:
045: public void testGetProvider() {
046: MySignature1 s = new MySignature1("ABC");
047:
048: assertEquals("state", Signature.UNINITIALIZED, s.getState());
049: assertNull("provider", s.getProvider());
050: }
051:
052: public void testGetAlgorithm() {
053: MySignature1 s = new MySignature1("ABC");
054:
055: assertEquals("state", Signature.UNINITIALIZED, s.getState());
056: assertEquals("algorithm", "ABC", s.getAlgorithm());
057: }
058:
059: /*
060: * Class under test for void initVerify(PublicKey)
061: */
062: public void testInitVerifyPublicKey() throws InvalidKeyException {
063: MySignature1 s = new MySignature1("ABC");
064:
065: s.initVerify(new MyPublicKey());
066: assertEquals("state", Signature.VERIFY, s.getState());
067: assertTrue("initVerify() failed", s.runEngineInitVerify);
068: }
069:
070: /*
071: * Class under test for void initVerify(Certificate)
072: */
073: public void testInitVerifyCertificate() throws InvalidKeyException {
074: MySignature1 s = new MySignature1("ABC");
075:
076: s.initVerify(new MyCertificate());
077: assertEquals("state", Signature.VERIFY, s.getState());
078: assertTrue("initVerify() failed", s.runEngineInitVerify);
079: }
080:
081: /*
082: * Class under test for void initSign(PrivateKey)
083: */
084: public void testInitSignPrivateKey() throws InvalidKeyException {
085: MySignature1 s = new MySignature1("ABC");
086:
087: s.initSign(new MyPrivateKey());
088: assertEquals("state", Signature.SIGN, s.getState());
089: assertTrue("initSign() failed", s.runEngineInitSign);
090: }
091:
092: /*
093: * Class under test for void initSign(PrivateKey, SecureRandom)
094: */
095: public void testInitSignPrivateKeySecureRandom()
096: throws InvalidKeyException {
097: MySignature1 s = new MySignature1("ABC");
098:
099: s.initSign(new MyPrivateKey(), new SecureRandom());
100: assertEquals("state", Signature.SIGN, s.getState());
101: assertTrue("initSign() failed", s.runEngineInitSign);
102: }
103:
104: /*
105: * Class under test for byte[] sign()
106: */
107: public void testSign() throws Exception {
108: MySignature1 s = new MySignature1("ABC");
109: try {
110: s.sign();
111: fail("No expected SignatureException");
112: } catch (SignatureException e) {
113: }
114:
115: s.initVerify(new MyPublicKey());
116:
117: try {
118: s.sign();
119: fail("No expected SignatureException");
120: } catch (SignatureException e) {
121: }
122:
123: s.initSign(new MyPrivateKey());
124: s.sign();
125: assertEquals("state", Signature.SIGN, s.getState());
126: assertTrue("sign() failed", s.runEngineSign);
127: }
128:
129: /*
130: * Class under test for boolean verify(byte[])
131: */
132: public void testVerifybyteArray() throws Exception {
133: MySignature1 s = new MySignature1("ABC");
134: byte[] b = { 1, 2, 3, 4 };
135: try {
136: s.verify(b);
137: fail("No expected SignatureException");
138: } catch (SignatureException e) {
139: }
140:
141: s.initSign(new MyPrivateKey());
142: try {
143: s.verify(b);
144: fail("No expected SignatureException");
145: } catch (SignatureException e) {
146: }
147:
148: s.initVerify(new MyPublicKey());
149: s.verify(b);
150: assertEquals("state", Signature.VERIFY, s.getState());
151: assertTrue("verify() failed", s.runEngineVerify);
152: }
153:
154: /*
155: * Class under test for boolean verify(byte[], int, int)
156: */
157: public void testVerifybyteArrayintint() throws Exception {
158: MySignature1 s = new MySignature1("ABC");
159: byte[] b = { 1, 2, 3, 4 };
160: try {
161: s.verify(b, 0, 3);
162: fail("No expected SignatureException");
163: } catch (SignatureException e) {
164: }
165:
166: s.initSign(new MyPrivateKey());
167:
168: try {
169: s.verify(b, 0, 3);
170: fail("No expected SignatureException");
171: } catch (SignatureException e) {
172: }
173:
174: s.initVerify(new MyPublicKey());
175:
176: try {
177: s.verify(b, 0, 5);
178: fail("No expected IllegalArgumentException");
179: } catch (IllegalArgumentException e) {
180: }
181:
182: s.verify(b, 0, 3);
183: assertEquals("state", Signature.VERIFY, s.getState());
184: assertTrue("verify() failed", s.runEngineVerify);
185: }
186:
187: /*
188: * Class under test for void update(byte)
189: */
190: public void testUpdatebyte() throws Exception {
191: MySignature1 s = new MySignature1("ABC");
192: try {
193: s.update((byte) 1);
194: fail("No expected SignatureException");
195: } catch (SignatureException e) {
196: }
197:
198: s.initVerify(new MyPublicKey());
199: s.update((byte) 1);
200: s.initSign(new MyPrivateKey());
201: s.update((byte) 1);
202:
203: assertEquals("state", Signature.SIGN, s.getState());
204: assertTrue("update() failed", s.runEngineUpdate1);
205: }
206:
207: /*
208: * Class under test for void update(byte[])
209: */
210: public void testUpdatebyteArray() throws Exception {
211: MySignature1 s = new MySignature1("ABC");
212: byte[] b = { 1, 2, 3, 4 };
213: try {
214: s.update(b);
215: fail("No expected SignatureException");
216: } catch (SignatureException e) {
217: }
218:
219: s.initVerify(new MyPublicKey());
220: s.update(b);
221: s.initSign(new MyPrivateKey());
222: s.update(b);
223:
224: assertEquals("state", Signature.SIGN, s.getState());
225: assertTrue("update() failed", s.runEngineUpdate2);
226: }
227:
228: /*
229: * Class under test for void update(byte[], int, int)
230: */
231: public void testUpdatebyteArrayintint() throws Exception {
232: MySignature1 s = new MySignature1("ABC");
233: byte[] b = { 1, 2, 3, 4 };
234: try {
235: s.update(b, 0, 3);
236: fail("No expected SignatureException");
237: } catch (SignatureException e) {
238: }
239:
240: s.initVerify(new MyPublicKey());
241: s.update(b, 0, 3);
242: s.initSign(new MyPrivateKey());
243: s.update(b, 0, 3);
244:
245: assertEquals("state", Signature.SIGN, s.getState());
246: assertTrue("update() failed", s.runEngineUpdate2);
247: }
248:
249: /*
250: * Class under test for void setParameter(String, Object)
251: */
252: public void testSetParameterStringObject() {
253: MySignature1 s = new MySignature1("ABC");
254: s.setParameter("aaa", new Object());
255: }
256:
257: /*
258: * Class under test for void setParameter(AlgorithmParameterSpec)
259: */
260: public void testSetParameterAlgorithmParameterSpec()
261: throws InvalidAlgorithmParameterException {
262: MySignature1 s = new MySignature1("ABC");
263: try {
264: s
265: .setParameter((java.security.spec.AlgorithmParameterSpec) null);
266: fail("No expected UnsupportedOperationException");
267: } catch (UnsupportedOperationException e) {
268: }
269: }
270:
271: public void testGetParameter() {
272: MySignature1 s = new MySignature1("ABC");
273: s.getParameter("aaa");
274: }
275:
276: private class MyKey implements Key {
277: public String getFormat() {
278: return "123";
279: }
280:
281: public byte[] getEncoded() {
282: return null;
283: }
284:
285: public String getAlgorithm() {
286: return "aaa";
287: }
288: }
289:
290: private class MyPublicKey extends MyKey implements PublicKey {
291: }
292:
293: private class MyPrivateKey extends MyKey implements PrivateKey {
294: }
295:
296: private class MyCertificate extends java.security.cert.Certificate {
297: public MyCertificate() {
298: super ("MyCertificateType");
299: }
300:
301: public PublicKey getPublicKey() {
302: return new MyPublicKey();
303: }
304:
305: public byte[] getEncoded() {
306: return null;
307: }
308:
309: public void verify(PublicKey key) {
310: }
311:
312: public void verify(PublicKey key, String sigProvider) {
313: }
314:
315: public String toString() {
316: return "MyCertificate";
317: }
318: }
319: }
|