01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * 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: * @version $Revision$
21: */package org.apache.harmony.security.tests.java.security;
22:
23: import java.security.*;
24: import java.io.IOException;
25: import java.security.spec.InvalidKeySpecException;
26: import java.util.Properties;
27:
28: import org.apache.harmony.security.tests.support.TestKeyPair;
29:
30: import junit.framework.TestCase;
31:
32: /**
33: * Tests for <code>SignedObject</code> constructor and methods
34: *
35: */
36: public class SignedObjectTest extends TestCase {
37:
38: public void testSignedObject() {
39: Signature sig = null;
40: TestKeyPair tkp = null;
41: Properties prop;
42:
43: try {
44: sig = Signature.getInstance("SHA1withDSA");
45: } catch (NoSuchAlgorithmException e) {
46: fail(e.toString());
47: }
48:
49: try {
50: tkp = new TestKeyPair("DSA");
51: } catch (NoSuchAlgorithmException e) {
52: e.printStackTrace();
53: return;
54: }
55: prop = new Properties();
56: prop.put("aaa", "bbb");
57: SignedObject so = null;
58: try {
59: so = new SignedObject(prop, tkp.getPrivate(), sig);
60: } catch (IOException e) {
61: fail(e.toString());
62: } catch (SignatureException e) {
63: fail(e.toString());
64: } catch (InvalidKeyException e) {
65: fail(e.toString());
66: } catch (InvalidKeySpecException e) {
67: fail(e.toString());
68: }
69:
70: assertEquals("SHA1withDSA", so.getAlgorithm());
71:
72: try {
73: assertEquals(so.getObject(), prop);
74: } catch (ClassNotFoundException e) {
75: fail(e.toString());
76: } catch (IOException e) {
77: fail(e.toString());
78: }
79: try {
80: if (!so.verify(tkp.getPublic(), sig)) {
81: fail("verify() failed");
82: }
83: } catch (SignatureException e) {
84: fail(e.toString());
85: } catch (InvalidKeyException e) {
86: fail(e.toString());
87: } catch (InvalidKeySpecException e) {
88: fail(e.toString());
89: }
90:
91: if (so.getSignature() == null) {
92: fail("signature is null");
93: }
94: }
95:
96: }
|