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; 25import org.apache.harmony.security.tests.support.MySignature2; 26 27import junit.framework.TestCase; 28 29/** 30 * Tests for <code>Signature</code> constructor and methods 31 * 32 */ 33public class Signature_Impl2Test extends TestCase { 34 35 /** 36 * Provider 37 */ 38 Provider p; 39 40 /* 41 * @see TestCase#setUp() 42 */ 43 protected void setUp() throws Exception { 44 super.setUp(); 45 p = new MyProvider(); 46 Security.insertProviderAt(p, 1); 47 } 48 49 /* 50 * @see TestCase#tearDown() 51 */ 52 protected void tearDown() throws Exception { 53 super.tearDown(); 54 Security.removeProvider(p.getName()); 55 } 56 57 /* 58 * Class under test for Signature getInstance(String) 59 */ 60 public void testGetInstanceString1() throws Exception { 61 Signature sig = Signature.getInstance("ABC"); 62 checkSig1(sig, p); 63 } 64 65 /* 66 * Class under test for Signature getInstance(String) 67 */ 68 public void testGetInstanceString2() throws Exception { 69 Signature sig = Signature.getInstance("CBA"); 70 checkSig2(sig, p); 71 } 72 73 /* 74 * Class under test for Signature getInstance(String, String) 75 */ 76 public void testGetInstanceStringString1() throws Exception { 77 Signature sig = Signature.getInstance("ABC", "MyProvider"); 78 checkSig1(sig, p); 79 } 80 81 /* 82 * Class under test for Signature getInstance(String, String) 83 */ 84 public void testGetInstanceStringString2() throws Exception { 85 Signature sig = Signature.getInstance("CBA", "MyProvider"); 86 checkSig2(sig, p); 87 } 88 89 90 /* 91 * Class under test for Signature getInstance(String, Provider) 92 */ 93 public void testGetInstanceStringProvider1() throws Exception { 94 Provider p1 = new MyProvider(); 95 Signature sig = Signature.getInstance("ABC", p1); 96 checkSig1(sig, p1); 97 } 98 99 /* 100 * Class under test for Signature getInstance(String, Provider) 101 */ 102 public void testGetInstanceStringProvider2() throws Exception { 103 Provider p2 = new MyProvider(); 104 Signature sig = Signature.getInstance("CBA", p2); 105 checkSig2(sig, p2); 106 } 107 108 private void checkSig1(Signature s, Provider p) throws Exception { 109 byte[] b = { 1, 2, 3, 4 }; 110 assertTrue("getInstance() failed", s instanceof MySignature1); 111 assertEquals("getProvider() failed", p, s.getProvider()); 112 assertEquals("getAlgorithm() failed", "ABC", s.getAlgorithm()); 113 114 try { 115 s.sign(); 116 fail("No expected SignatureException"); 117 } catch (SignatureException e) { 118 } 119 120 s.initVerify(new MyPublicKey()); 121 122 try { 123 s.sign(); 124 fail("No expected SignatureException"); 125 } catch (SignatureException e) { 126 } 127 128 s.initSign(new MyPrivateKey()); 129 s.sign(); 130 assertEquals("Incorrect state", Signature.SIGN, ((MySignature1) s) 131 .getState()); 132 assertTrue("sign() failed", ((MySignature1) s).runEngineSign); 133 134 s.initVerify(new MyPublicKey()); 135 s.update((byte) 1); 136 137 s.initSign(new MyPrivateKey()); 138 s.update((byte) 1); 139 140 assertEquals("Incorrect state", Signature.SIGN, ((MySignature1) s) 141 .getState()); 142 assertTrue("sign() failed", ((MySignature1) s).runEngineUpdate1); 143 144 s.initSign(new MyPrivateKey()); 145 146 try { 147 s.verify(b); 148 fail("No expected SignatureException"); 149 } catch (SignatureException e) { 150 } 151 s.initVerify(new MyPublicKey()); 152 s.verify(b); 153 assertEquals("Incorrect state", Signature.VERIFY, ((MySignature1) s) 154 .getState()); 155 assertTrue("verify() failed", ((MySignature1) s).runEngineVerify); 156 } 157 158 private void checkSig2(Signature s, Provider p) throws Exception { 159 byte[] b = { 1, 2, 3, 4 }; 160 161 assertEquals("getProvider() failed", p, s.getProvider()); 162 assertEquals("getAlgorithm() failed", "CBA", s.getAlgorithm()); 163 164 s.initVerify(new MyCertificate()); 165 166 try { 167 s.sign(b, 0, 5); 168 fail("No expected IllegalArgumentException 1"); 169 } catch (IllegalArgumentException e) { 170 } 171 172 s.initSign(new MyPrivateKey()); 173 s.sign(b, 0, 3); 174 175 assertTrue("sign() failed", MySignature2.runEngineSign); 176 s.update(b); 177 s.initSign(new MyPrivateKey()); 178 s.update(b); 179 assertTrue("update() failed", MySignature2.runEngineUpdate2); 180 181 s.initSign(new MyPrivateKey()); 182 try { 183 s.verify(b, 0, 3); 184 fail("No expected SignatureException"); 185 } catch (SignatureException e) { 186 } 187 s.initVerify(new MyPublicKey()); 188 189 try { 190 s.verify(b, 0, 5); 191 fail("No expected IllegalArgumentException"); 192 } catch (IllegalArgumentException e) { 193 } catch (SignatureException e) { 194 } 195 196 s.verify(b, 0, 3); 197 assertTrue("verify() failed", MySignature2.runEngineVerify); 198 } 199 200 private class MyProvider extends Provider { 201 MyProvider() { 202 super("MyProvider", 1.0, "Provider for testing"); 203 put("Signature.ABC", "org.apache.harmony.security.tests.support.MySignature1"); 204 put("Signature.CBA", "org.apache.harmony.security.tests.support.MySignature2"); 205 } 206 207 MyProvider(String name, double version, String info) { 208 super(name, version, info); 209 } 210 } 211 private class MyKey implements Key { 212 public String getFormat() { 213 return "123"; 214 } 215 public byte[] getEncoded() { 216 return null; 217 } 218 public String getAlgorithm() { 219 return "aaa"; 220 } 221 } 222 223 private class MyPublicKey extends MyKey implements PublicKey {} 224 225 private class MyPrivateKey extends MyKey implements PrivateKey {} 226 227 private class MyCertificate extends java.security.cert.Certificate { 228 public MyCertificate() { 229 super("MyCertificateType"); 230 } 231 232 public PublicKey getPublicKey() { 233 return new MyPublicKey(); 234 } 235 236 public byte[] getEncoded() { 237 return null; 238 } 239 public void verify(PublicKey key) {} 240 241 public void verify(PublicKey key, String sigProvider) {} 242 243 public String toString() { 244 return "MyCertificate"; 245 } 246 } 247} 248