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