1/*
2 *  Licensed to the Apache Software Foundation (ASF) under one or more
3 *  contributor license agreements.  See the NOTICE file distributed with
4 *  this work for additional information regarding copyright ownership.
5 *  The ASF licenses this file to You under the Apache License, Version 2.0
6 *  (the "License"); you may not use this file except in compliance with
7 *  the License.  You may obtain a copy of the License at
8 *
9 *     http://www.apache.org/licenses/LICENSE-2.0
10 *
11 *  Unless required by applicable law or agreed to in writing, software
12 *  distributed under the License is distributed on an "AS IS" BASIS,
13 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 *  See the License for the specific language governing permissions and
15 *  limitations under the License.
16 */
17
18/**
19* @author Boris V. Kuznetsov
20*/
21
22package java.security;
23
24import org.apache.harmony.security.tests.support.MySignature1;
25
26import junit.framework.TestCase;
27
28/**
29 * Tests for <code>Signature</code> constructor and methods
30 *
31 */
32public class SignatureTest extends TestCase {
33
34	/*
35	 * Class under test for Object clone()
36	 */
37	public void testClone() {
38		MySignature1 s = new MySignature1("ABC");
39		try {
40			s.clone();
41			fail("No expected CloneNotSupportedException");
42		} catch (CloneNotSupportedException e) {
43		}
44	}
45
46	public void testGetProvider() {
47		MySignature1 s = new MySignature1("ABC");
48
49        assertEquals("state", Signature.UNINITIALIZED, s.getState());
50        assertNull("provider", s.getProvider());
51	}
52
53	public void testGetAlgorithm() {
54		MySignature1 s = new MySignature1("ABC");
55
56        assertEquals("state", Signature.UNINITIALIZED, s.getState());
57        assertEquals("algorithm", "ABC", s.getAlgorithm());
58	}
59
60	/*
61	 * Class under test for void initVerify(PublicKey)
62	 */
63	public void testInitVerifyPublicKey() throws InvalidKeyException {
64		MySignature1 s = new MySignature1("ABC");
65
66        s.initVerify(new MyPublicKey());
67        assertEquals("state", Signature.VERIFY, s.getState());
68        assertTrue("initVerify() failed", s.runEngineInitVerify);
69	}
70
71	/*
72	 * Class under test for void initVerify(Certificate)
73	 */
74	public void testInitVerifyCertificate() throws InvalidKeyException {
75		MySignature1 s = new MySignature1("ABC");
76
77        s.initVerify(new MyCertificate());
78        assertEquals("state", Signature.VERIFY, s.getState());
79        assertTrue("initVerify() failed", s.runEngineInitVerify);
80	}
81
82	/*
83	 * Class under test for void initSign(PrivateKey)
84	 */
85	public void testInitSignPrivateKey() throws InvalidKeyException {
86		MySignature1 s = new MySignature1("ABC");
87
88        s.initSign(new MyPrivateKey());
89        assertEquals("state", Signature.SIGN, s.getState());
90        assertTrue("initSign() failed", s.runEngineInitSign);
91	}
92
93	/*
94	 * Class under test for void initSign(PrivateKey, SecureRandom)
95	 */
96	public void testInitSignPrivateKeySecureRandom() throws InvalidKeyException {
97		MySignature1 s = new MySignature1("ABC");
98
99        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() throws InvalidAlgorithmParameterException {
261		MySignature1 s = new MySignature1("ABC");
262		try {
263			s.setParameter((java.security.spec.AlgorithmParameterSpec)null);
264			fail("No expected UnsupportedOperationException");
265		} catch (UnsupportedOperationException e){
266		}
267	}
268
269	public void testGetParameter() {
270		MySignature1 s = new MySignature1("ABC");
271        s.getParameter("aaa");
272	}
273
274	private class MyKey implements Key {
275		public String getFormat() {
276			return "123";
277		}
278		public byte[] getEncoded() {
279			return null;
280		}
281		public String getAlgorithm() {
282			return "aaa";
283		}
284	}
285
286	private class MyPublicKey extends MyKey implements PublicKey {}
287
288	private class MyPrivateKey extends MyKey implements PrivateKey {}
289
290	private class MyCertificate extends java.security.cert.Certificate {
291		public  MyCertificate() {
292			super("MyCertificateType");
293		}
294
295		public PublicKey getPublicKey() {
296			return new MyPublicKey();
297		}
298
299		public byte[] getEncoded() {
300			return null;
301		}
302		public void verify(PublicKey key) {}
303
304		public void verify(PublicKey key, String sigProvider) {}
305
306		public String toString() {
307			return "MyCertificate";
308		}
309	}
310}
311