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 22 package java.security; 23 24 import org.apache.harmony.security.tests.support.MySignature1; 25 26 import junit.framework.TestCase; 27 28 /** 29 * Tests for <code>Signature</code> constructor and methods 30 */ 31 public class SignatureTest extends TestCase { 32 33 /* 34 * Class under test for Object clone() 35 */ 36 public void testClone() { 37 MySignature1 s = new MySignature1("ABC"); 38 try { 39 s.clone(); 40 fail("No expected CloneNotSupportedException"); 41 } catch (CloneNotSupportedException e) { 42 } 43 } 44 45 public void testGetProvider() { 46 MySignature1 s = new MySignature1("ABC"); 47 48 assertEquals("state", Signature.UNINITIALIZED, s.getState()); 49 assertNull("provider", s.getProvider()); 50 } 51 52 public void testGetAlgorithm() { 53 MySignature1 s = new MySignature1("ABC"); 54 55 assertEquals("state", Signature.UNINITIALIZED, s.getState()); 56 assertEquals("algorithm", "ABC", s.getAlgorithm()); 57 } 58 59 /* 60 * Class under test for void initVerify(PublicKey) 61 */ 62 public void testInitVerifyPublicKey() throws InvalidKeyException { 63 MySignature1 s = new MySignature1("ABC"); 64 65 s.initVerify(new MyPublicKey()); 66 assertEquals("state", Signature.VERIFY, s.getState()); 67 assertTrue("initVerify() failed", s.runEngineInitVerify); 68 } 69 70 /* 71 * Class under test for void initVerify(Certificate) 72 */ 73 public void testInitVerifyCertificate() throws InvalidKeyException { 74 MySignature1 s = new MySignature1("ABC"); 75 76 s.initVerify(new MyCertificate()); 77 assertEquals("state", Signature.VERIFY, s.getState()); 78 assertTrue("initVerify() failed", s.runEngineInitVerify); 79 } 80 81 /* 82 * Class under test for void initSign(PrivateKey) 83 */ 84 public void testInitSignPrivateKey() throws InvalidKeyException { 85 MySignature1 s = new MySignature1("ABC"); 86 87 s.initSign(new MyPrivateKey()); 88 assertEquals("state", Signature.SIGN, s.getState()); 89 assertTrue("initSign() failed", s.runEngineInitSign); 90 } 91 92 /* 93 * Class under test for void initSign(PrivateKey, SecureRandom) 94 */ 95 public void testInitSignPrivateKeySecureRandom() throws InvalidKeyException { 96 MySignature1 s = new MySignature1("ABC"); 97 98 s.initSign(new MyPrivateKey(), new SecureRandom()); 99 assertEquals("state", Signature.SIGN, s.getState()); 100 assertTrue("initSign() failed", s.runEngineInitSign); 101 } 102 103 /* 104 * Class under test for byte[] sign() 105 */ 106 public void testSign() throws Exception { 107 MySignature1 s = new MySignature1("ABC"); 108 try { 109 s.sign(); 110 fail("No expected SignatureException"); 111 } catch (SignatureException e) { 112 } 113 114 s.initVerify(new MyPublicKey()); 115 116 try { 117 s.sign(); 118 fail("No expected SignatureException"); 119 } catch (SignatureException e) { 120 } 121 122 s.initSign(new MyPrivateKey()); 123 s.sign(); 124 assertEquals("state", Signature.SIGN, s.getState()); 125 assertTrue("sign() failed", s.runEngineSign); 126 } 127 128 /* 129 * Class under test for boolean verify(byte[]) 130 */ 131 public void testVerifybyteArray() throws Exception { 132 MySignature1 s = new MySignature1("ABC"); 133 byte[] b = { 1, 2, 3, 4 }; 134 try { 135 s.verify(b); 136 fail("No expected SignatureException"); 137 } catch (SignatureException e) { 138 } 139 140 s.initSign(new MyPrivateKey()); 141 try { 142 s.verify(b); 143 fail("No expected SignatureException"); 144 } catch (SignatureException e) { 145 } 146 147 s.initVerify(new MyPublicKey()); 148 s.verify(b); 149 assertEquals("state", Signature.VERIFY, s.getState()); 150 assertTrue("verify() failed", s.runEngineVerify); 151 } 152 153 /* 154 * Class under test for boolean verify(byte[], int, int) 155 */ 156 public void testVerifybyteArrayintint() throws Exception { 157 MySignature1 s = new MySignature1("ABC"); 158 byte[] b = { 1, 2, 3, 4 }; 159 try { 160 s.verify(b, 0, 3); 161 fail("No expected SignatureException"); 162 } catch (SignatureException e) { 163 } 164 165 s.initSign(new MyPrivateKey()); 166 167 try { 168 s.verify(b, 0, 3); 169 fail("No expected SignatureException"); 170 } catch (SignatureException e) { 171 } 172 173 s.initVerify(new MyPublicKey()); 174 175 try { 176 s.verify(b, 0, 5); 177 fail("No expected IllegalArgumentException"); 178 } catch (IllegalArgumentException e) { 179 } 180 181 s.verify(b, 0, 3); 182 assertEquals("state", Signature.VERIFY, s.getState()); 183 assertTrue("verify() failed", s.runEngineVerify); 184 } 185 186 /* 187 * Class under test for void update(byte) 188 */ 189 public void testUpdatebyte() throws Exception { 190 MySignature1 s = new MySignature1("ABC"); 191 try { 192 s.update((byte) 1); 193 fail("No expected SignatureException"); 194 } catch (SignatureException e) { 195 } 196 197 s.initVerify(new MyPublicKey()); 198 s.update((byte) 1); 199 s.initSign(new MyPrivateKey()); 200 s.update((byte) 1); 201 202 assertEquals("state", Signature.SIGN, s.getState()); 203 assertTrue("update() failed", s.runEngineUpdate1); 204 } 205 206 /* 207 * Class under test for void update(byte[]) 208 */ 209 public void testUpdatebyteArray() throws Exception { 210 MySignature1 s = new MySignature1("ABC"); 211 byte[] b = { 1, 2, 3, 4 }; 212 try { 213 s.update(b); 214 fail("No expected SignatureException"); 215 } catch (SignatureException e) { 216 } 217 218 s.initVerify(new MyPublicKey()); 219 s.update(b); 220 s.initSign(new MyPrivateKey()); 221 s.update(b); 222 223 assertEquals("state", Signature.SIGN, s.getState()); 224 assertTrue("update() failed", s.runEngineUpdate2); 225 } 226 227 /* 228 * Class under test for void update(byte[], int, int) 229 */ 230 public void testUpdatebyteArrayintint() throws Exception { 231 MySignature1 s = new MySignature1("ABC"); 232 byte[] b = { 1, 2, 3, 4 }; 233 try { 234 s.update(b, 0, 3); 235 fail("No expected SignatureException"); 236 } catch (SignatureException e) { 237 } 238 239 s.initVerify(new MyPublicKey()); 240 s.update(b, 0, 3); 241 s.initSign(new MyPrivateKey()); 242 s.update(b, 0, 3); 243 244 assertEquals("state", Signature.SIGN, s.getState()); 245 assertTrue("update() failed", s.runEngineUpdate2); 246 } 247 248 /* 249 * Class under test for void setParameter(String, Object) 250 */ 251 public void testSetParameterStringObject() { 252 MySignature1 s = new MySignature1("ABC"); 253 s.setParameter("aaa", new Object()); 254 } 255 256 /* 257 * Class under test for void setParameter(AlgorithmParameterSpec) 258 */ 259 public void testSetParameterAlgorithmParameterSpec() throws InvalidAlgorithmParameterException { 260 MySignature1 s = new MySignature1("ABC"); 261 try { 262 s.setParameter((java.security.spec.AlgorithmParameterSpec) null); 263 fail("No expected UnsupportedOperationException"); 264 } catch (UnsupportedOperationException e) { 265 } 266 } 267 268 public void testGetParameter() { 269 MySignature1 s = new MySignature1("ABC"); 270 s.getParameter("aaa"); 271 } 272 273 private class MyKey implements Key { 274 public String getFormat() { 275 return "123"; 276 } 277 278 public byte[] getEncoded() { 279 return null; 280 } 281 282 public String getAlgorithm() { 283 return "aaa"; 284 } 285 } 286 287 private class MyPublicKey extends MyKey implements PublicKey { 288 } 289 290 private class MyPrivateKey extends MyKey implements PrivateKey { 291 } 292 293 private class MyCertificate extends java.security.cert.Certificate { 294 public MyCertificate() { 295 super("MyCertificateType"); 296 } 297 298 public PublicKey getPublicKey() { 299 return new MyPublicKey(); 300 } 301 302 public byte[] getEncoded() { 303 return null; 304 } 305 306 public void verify(PublicKey key) { 307 } 308 309 public void verify(PublicKey key, String sigProvider) { 310 } 311 312 public String toString() { 313 return "MyCertificate"; 314 } 315 } 316 } 317