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 18package org.apache.harmony.security.tests.java.security; 19 20import java.math.BigInteger; 21import java.security.InvalidParameterException; 22import java.security.KeyPair; 23import java.security.KeyPairGenerator; 24import java.security.Provider; 25import java.security.Security; 26import java.security.Signature; 27import java.security.spec.DSAParameterSpec; 28import java.util.Locale; 29 30public class Signature2Test extends junit.framework.TestCase { 31 32 private static final String MESSAGE = "abc"; 33 static KeyPair keys; 34 35 static { 36 try { 37 KeyPairGenerator keyGen = KeyPairGenerator.getInstance("DSA"); 38 keyGen.initialize(1024); 39 keys = keyGen.generateKeyPair(); 40 } catch (Exception e) { 41 fail(e.toString()); 42 } 43 } 44 45 /** 46 * @tests java.security.Signature#clone() 47 */ 48 public void test_clone() throws Exception { 49 Signature s = Signature.getInstance("DSA"); 50 try { 51 s.clone(); 52 fail("A Signature may not be cloneable"); 53 } catch (CloneNotSupportedException e) { 54 // Expected - a Signature may not be cloneable 55 } 56 } 57 58 /** 59 * @tests java.security.Signature#getAlgorithm() 60 */ 61 public void test_getAlgorithm() throws Exception { 62 String alg = Signature.getInstance("DSA").getAlgorithm(); 63 assertTrue("getAlgorithm did not get DSA (" + alg + ")", alg 64 .indexOf("DSA") != -1); 65 } 66 67 /** 68 * @tests java.security.Signature#getInstance(java.lang.String) 69 */ 70 public void test_getInstanceLjava_lang_String() throws Exception { 71 Signature.getInstance("DSA"); 72 } 73 74 /** 75 * @tests java.security.Signature#getInstance(java.lang.String, 76 *java.lang.String) 77 */ 78 public void test_getInstanceLjava_lang_StringLjava_lang_String() throws Exception { 79 Provider[] providers = Security.getProviders("Signature.DSA"); 80 81 for (int i = 0; i < providers.length; i++) { 82 Signature.getInstance("DSA", providers[i].getName()); 83 }// end for 84 } 85 86 /** 87 * @tests java.security.Signature#getParameters() 88 */ 89 public void test_getParameters() throws Exception { 90 Signature sig = Signature.getInstance("DSA"); 91 try { 92 sig.getParameters(); 93 } catch (UnsupportedOperationException e) { 94 // Could be that the operation is not supported 95 } 96 } 97 98 /** 99 * @tests java.security.Signature#getParameter(java.lang.String) 100 */ 101 public void test_getParameterLjava_lang_String() throws Exception { 102 Signature sig = Signature.getInstance("DSA"); 103 104 try { 105 sig.getParameter("r"); 106 sig.getParameter("s"); 107 } catch (UnsupportedOperationException e) { 108 } 109 } 110 111 /** 112 * @tests java.security.Signature#getProvider() 113 */ 114 public void test_getProvider() throws Exception { 115 Provider p = Signature.getInstance("DSA").getProvider(); 116 assertNotNull("provider is null", p); 117 } 118 119 /** 120 * @tests java.security.Signature#initSign(java.security.PrivateKey) 121 */ 122 public void test_initSignLjava_security_PrivateKey() throws Exception { 123 Signature.getInstance("DSA").initSign(keys.getPrivate()); 124 } 125 126 /** 127 * @tests java.security.Signature#initVerify(java.security.PublicKey) 128 */ 129 public void test_initVerifyLjava_security_PublicKey() throws Exception { 130 Signature.getInstance("DSA").initVerify(keys.getPublic()); 131 } 132 133 /** 134 * @tests java.security.Signature#setParameter(java.lang.String, 135 *java.lang.Object) 136 */ 137 public void test_setParameterLjava_lang_StringLjava_lang_Object() throws Exception { 138 Signature sig = Signature.getInstance("DSA"); 139 140 try { 141 sig.setParameter("r", BigInteger.ONE); 142 sig.setParameter("s", BigInteger.ONE); 143 } catch (InvalidParameterException e) { 144 // Could be that it's an invalid param for the found algorithm 145 } catch (UnsupportedOperationException e) { 146 // Could be that the operation is not supported 147 } 148 } 149 150 /** 151 * @tests java.security.Signature#setParameter(java.security.spec.AlgorithmParameterSpec) 152 */ 153 public void test_setParameterLjava_security_spec_AlgorithmParameterSpec() throws Exception { 154 Signature sig = Signature.getInstance("DSA"); 155 156 try { 157 DSAParameterSpec spec = new DSAParameterSpec(BigInteger.ONE, 158 BigInteger.ONE, BigInteger.ONE); 159 sig.setParameter(spec); 160 } catch (InvalidParameterException e) { 161 // Could be that it's an invalid param for the found algorithm 162 } catch (UnsupportedOperationException e) { 163 // Could be that the operation is not supported 164 } 165 } 166 167 /** 168 * @tests java.security.Signature#sign() 169 */ 170 public void test_sign() throws Exception { 171 Signature sig = Signature.getInstance("DSA"); 172 sig.initSign(keys.getPrivate()); 173 sig.update(MESSAGE.getBytes()); 174 sig.sign(); 175 } 176 177 /** 178 * @tests java.security.Signature#toString() 179 */ 180 public void test_toString() throws Exception { 181 String str = Signature.getInstance("DSA").toString(); 182 assertNotNull("toString is null", str); 183 } 184 185 /** 186 * @tests java.security.Signature#update(byte[]) 187 */ 188 public void test_update$B() throws Exception { 189 Signature sig = Signature.getInstance("DSA"); 190 sig.initSign(keys.getPrivate()); 191 192 byte[] bytes = MESSAGE.getBytes(); 193 sig.update(bytes); 194 } 195 196 /** 197 * @tests java.security.Signature#update(byte[], int, int) 198 */ 199 public void test_update$BII() throws Exception { 200 Signature sig = Signature.getInstance("DSA"); 201 sig.initSign(keys.getPrivate()); 202 203 byte[] bytes = MESSAGE.getBytes(); 204 sig.update(bytes, 0, bytes.length); 205 } 206 207 /** 208 * @tests java.security.Signature#update(byte) 209 */ 210 public void test_updateB() throws Exception { 211 Signature sig = Signature.getInstance("DSA"); 212 sig.initSign(keys.getPrivate()); 213 214 sig.update(MESSAGE.getBytes()[0]); 215 } 216 217 /** 218 * @tests java.security.Signature#verify(byte[]) 219 */ 220 public void test_verify$B() throws Exception { 221 Signature sig = Signature.getInstance("DSA"); 222 sig.initSign(keys.getPrivate()); 223 sig.update(MESSAGE.getBytes()); 224 byte[] signature = sig.sign(); 225 226 227 sig.initVerify(keys.getPublic()); 228 sig.update(MESSAGE.getBytes()); 229 assertTrue("Sign/Verify does not pass", sig.verify(signature)); 230 } 231 232 //Regression Test for HARMONY-4916 233 public void test_getInstance_withI18n() throws Exception { 234 // Enfore that providers information has been loaded. 235 Signature.getInstance("DSA"); 236 Locale defaultLocale = Locale.getDefault(); 237 try { 238 /** 239 * In locale("tr"), char 'i' will be transferred to an upper case 240 * other char than 'I'. Thus in security architecture, all 241 * manipulation to the string representing an algorithm name or 242 * standard property shall be treated as locale neutral 243 */ 244 Locale.setDefault(new Locale("tr")); 245 Signature.getInstance("MD5withRSA"); 246 } finally { 247 Locale.setDefault(defaultLocale); 248 } 249 } 250} 251