Path: blob/master/test/jdk/java/security/SignedObject/Copy.java
41149 views
/*1* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import java.io.Serializable;24import java.security.KeyPair;25import java.security.KeyPairGenerator;26import java.security.Signature;27import java.security.SignedObject;2829/*30* @test31* @bug 805037432* @summary Checks if a signed object is a copy of an original object33*/34public class Copy {3536private static final String DSA = "DSA";37private static final int KEY_SIZE = 512;38private static final int MAGIC = 123;3940public static void main(String args[]) throws Exception {41KeyPairGenerator kg = KeyPairGenerator.getInstance(DSA);42kg.initialize(KEY_SIZE);43KeyPair kp = kg.genKeyPair();4445Signature signature = Signature.getInstance(DSA);46Test original = new Test();47SignedObject so = new SignedObject(original, kp.getPrivate(),48signature);49System.out.println("Signature algorithm: " + so.getAlgorithm());5051signature = Signature.getInstance(DSA, "SUN");52if (!so.verify(kp.getPublic(), signature)) {53throw new RuntimeException("Verification failed");54}5556kg = KeyPairGenerator.getInstance(DSA);57kg.initialize(KEY_SIZE);58kp = kg.genKeyPair();5960if (so.verify(kp.getPublic(), signature)) {61throw new RuntimeException("Unexpected success");62}6364Object copy = so.getObject();65if (!original.equals(copy)) {66throw new RuntimeException("Signed object is not equal "67+ "to original one: " + copy);68}6970/*71* The signed object is a copy of an original one.72* Once the copy is made, further manipulation73* of the original object shouldn't has any effect on the copy.74*/75original.set(MAGIC - 1);76copy = so.getObject();77if (original.equals(copy)) {78throw new RuntimeException("Signed object is not a copy "79+ "of original one: " + copy);80}8182System.out.println("Test passed");83}8485private static class Test implements Serializable {86private int number = MAGIC;8788public int get() {89return number;90}9192public void set(int magic) {93this.number = magic;94}9596@Override97public int hashCode() {98return number;99}100101@Override102public boolean equals(Object obj) {103if (obj == null) {104return false;105}106107if (!(obj instanceof Test)) {108return false;109}110111Test other = (Test) obj;112return number == other.number;113}114115@Override116public String toString() {117return "" + number;118}119}120}121122123