SignedObjectTest.java revision 561ee011997c6c2f1befbfaa9d5f0a99771c1d63
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 org.apache.harmony.security.tests.java.security.serialization;
23
24import java.io.IOException;
25import java.io.Serializable;
26import java.security.InvalidKeyException;
27import java.security.NoSuchAlgorithmException;
28import java.security.Signature;
29import java.security.SignatureException;
30import java.security.SignedObject;
31import java.security.spec.InvalidKeySpecException;
32import java.util.Properties;
33
34import org.apache.harmony.testframework.serialization.SerializationTest;
35import org.apache.harmony.security.tests.support.TestKeyPair;
36
37
38/**
39 * Tests for SignedObject serialization
40 *
41 */
42public class SignedObjectTest extends SerializationTest implements
43        SerializationTest.SerializableAssert {
44
45	private Signature sig;
46	private TestKeyPair tkp = null;
47    private Properties prop;
48
49    protected Object[] getData() {
50    	try {
51        	sig = Signature.getInstance("SHA1withDSA");
52    	} catch (NoSuchAlgorithmException e) {
53    		fail(e.toString());
54    	}
55    	try {
56			tkp = new TestKeyPair("DSA");
57		} catch (NoSuchAlgorithmException e1) {
58			fail(e1.toString());
59		}
60    	prop = new Properties();
61    	prop.put("aaa", "bbb");
62    	Object o = null;
63    	try {
64    		o = new SignedObject(prop, tkp.getPrivate(), sig);
65    	} catch (IOException e) {
66           	fail(e.toString());
67    	} catch (SignatureException e) {
68           	fail(e.toString());
69    	} catch (InvalidKeyException e) {
70           	fail(e.toString());
71    	} catch (InvalidKeySpecException e) {
72          	fail(e.toString());
73		}
74       return new Object[] { o };
75    }
76
77    public void assertDeserialized(Serializable oref, Serializable otest) {
78    	SignedObject ref = (SignedObject) oref;
79    	SignedObject test = (SignedObject) otest;
80
81    	assertEquals(test.getAlgorithm(), ref.getAlgorithm());
82
83        try {
84            assertEquals(test.getObject(), prop);
85        } catch (ClassNotFoundException e) {
86           	fail(e.toString());
87        } catch (IOException e) {
88           	fail(e.toString());
89        }
90        try {
91        	if (!test.verify(tkp.getPublic(), sig)) {
92            	fail("verify() failed");
93            }
94        } catch (SignatureException e) {
95        	fail(e.toString());
96        } catch (InvalidKeyException e) {
97           	fail(e.toString());
98        } catch (InvalidKeySpecException e) {
99           	fail(e.toString());
100		}
101    }
102
103    public static void main(String[] args) {
104        junit.textui.TestRunner.run(DigestExceptionTest.class);
105    }
106}
107