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 */ 41public class SignedObjectTest extends SerializationTest implements 42 SerializationTest.SerializableAssert { 43 44 private Signature sig; 45 private TestKeyPair tkp = null; 46 private Properties prop; 47 48 protected Object[] getData() { 49 try { 50 sig = Signature.getInstance("SHA1withDSA"); 51 } catch (NoSuchAlgorithmException e) { 52 fail(e.toString()); 53 } 54 try { 55 tkp = new TestKeyPair("DSA"); 56 } catch (NoSuchAlgorithmException e1) { 57 fail(e1.toString()); 58 } 59 prop = new Properties(); 60 prop.put("aaa", "bbb"); 61 Object o = null; 62 try { 63 o = new SignedObject(prop, tkp.getPrivate(), sig); 64 } catch (IOException e) { 65 fail(e.toString()); 66 } catch (SignatureException e) { 67 fail(e.toString()); 68 } catch (InvalidKeyException e) { 69 fail(e.toString()); 70 } catch (InvalidKeySpecException e) { 71 fail(e.toString()); 72 } 73 return new Object[] { o }; 74 } 75 76 public void assertDeserialized(Serializable oref, Serializable otest) { 77 SignedObject ref = (SignedObject) oref; 78 SignedObject test = (SignedObject) otest; 79 80 assertEquals(test.getAlgorithm(), ref.getAlgorithm()); 81 82 try { 83 assertEquals(test.getObject(), prop); 84 } catch (ClassNotFoundException e) { 85 fail(e.toString()); 86 } catch (IOException e) { 87 fail(e.toString()); 88 } 89 try { 90 if (!test.verify(tkp.getPublic(), sig)) { 91 fail("verify() failed"); 92 } 93 } catch (SignatureException e) { 94 fail(e.toString()); 95 } catch (InvalidKeyException e) { 96 fail(e.toString()); 97 } catch (InvalidKeySpecException e) { 98 fail(e.toString()); 99 } 100 } 101 102} 103