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 Vladimir N. Molotkov 20*/ 21 22package org.apache.harmony.security.tests.java.security.serialization; 23 24import java.io.Serializable; 25import java.security.Key; 26import java.security.KeyPair; 27import java.util.Arrays; 28 29import junit.framework.TestCase; 30 31import org.apache.harmony.security.tests.support.PrivateKeyStub; 32import org.apache.harmony.security.tests.support.PublicKeyStub; 33import org.apache.harmony.testframework.serialization.SerializationTest; 34import org.apache.harmony.testframework.serialization.SerializationTest.SerializableAssert; 35 36 37/** 38 * Tests for <code>KeyPair</code> serialization 39 */ 40public class KeyPairTest extends TestCase { 41 42 // key pair for testing 43 private KeyPair keyPair; 44 45 protected void setUp() throws Exception { 46 PrivateKeyStub privateKey = new PrivateKeyStub("privateAlgorithm", 47 "privateFormat", new byte[] { 0x00, 0x05, 0x10 }); 48 PublicKeyStub publicKey = new PublicKeyStub("publicAlgorithm", 49 "publicFormat", new byte[] { 0x01, 0x02, 0x12 }); 50 51 keyPair = new KeyPair(publicKey, privateKey); 52 } 53 54 /** 55 * Tests serialization compatibility 56 */ 57 public void testSerializationCompatibility() throws Exception { 58 59 SerializationTest.verifyGolden(this, keyPair, comparator); 60 } 61 62 /** 63 * Tests serialization/deserialization 64 */ 65 public void testSerializationSelf() throws Exception { 66 67 SerializationTest.verifySelf(keyPair, comparator); 68 } 69 70 71 // comparator for KeyPair objects 72 private static SerializableAssert comparator = new SerializableAssert(){ 73 public void assertDeserialized(Serializable reference, Serializable test) { 74 75 // check result: compare public keys 76 Key key1 = ((KeyPair)test).getPublic(); 77 Key key2 = ((KeyPair)reference).getPublic(); 78 79 assertEquals("PublicKey class", key1.getClass(), key2.getClass()); 80 assertEquals("PublicKey algorithm", key1.getAlgorithm(), key2 81 .getAlgorithm()); 82 assertEquals("PublicKey format", key1.getFormat(), key2.getFormat()); 83 assertTrue("PublicKey encoded", Arrays.equals(key1.getEncoded(), 84 key2.getEncoded())); 85 86 // check result: compare private keys 87 key1 = ((KeyPair)test).getPrivate(); 88 key2 = ((KeyPair)reference).getPrivate(); 89 90 assertEquals("PrivateKey class", key1.getClass(), key2.getClass()); 91 assertEquals("PrivateKey algorithm", key1.getAlgorithm(), key2 92 .getAlgorithm()); 93 assertEquals("PrivateKey format", key1.getFormat(), key2.getFormat()); 94 assertTrue("PrivateKey encoded", Arrays.equals(key1.getEncoded(), 95 key2.getEncoded())); 96 } 97 }; 98} 99