Path: blob/master/test/jdk/java/security/Identity/EqualsHashCodeContract.java
41149 views
/*1* Copyright (c) 1999, 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*/2223/*24* @test25* @author Gary Ellison26* @bug 418313627* @summary java.security.Identity violates equals/hashCode contract28*/29import java.security.*;30import java.io.*;3132public class EqualsHashCodeContract33{34public static void main(String args[]) throws Exception {3536Identity i1=new MyIdentity("identity",37new MyIdentityScope("IdentityScope"));38Identity i2=new MyIdentity("identity",39new MyIdentityScope("IdentityScope"));40Identity i3=new MyIdentity("identity",41new MyIdentityScope(""));4243PublicKey pk1=new MyPublicKey();44PublicKey pk2=new MyPublicKey();4546if ( !(i1.equals(i2)) == (i1.hashCode()==i2.hashCode()) ) {47System.err.println("FAILED");48Exception up = new49Exception("Contract violated -- same name and same scope");50throw up;51}52System.out.println("Test same name, same scope........... PASSED");5354i1.setPublicKey(pk1);55i3.setPublicKey(pk1);56if ( !((i1.equals(i3)) && (i1.hashCode()==i3.hashCode()))) {57System.err.println("FAILED");58Exception up = new Exception("Contract violated -- PublicKeys do not differ");59throw up;60}61System.out.println("Test same name, same PublicKeys ..... PASSED");6263System.out.println("TEST PASSED");6465}66}6768class MyIdentity extends Identity {69public MyIdentity(String name, IdentityScope is) throws KeyManagementException {70super(name, is);71}72}7374class MyPublicKey implements PublicKey, Certificate {75private byte e[] = null;76public String getAlgorithm() {77return null;78}79public String getFormat() {80return new String("PKCS15");81}8283public byte[] getEncoded() {84if (e == null) {85ByteArrayOutputStream bs = new ByteArrayOutputStream();86DataOutputStream ds = new DataOutputStream(bs);87try {88ds.writeLong(System.currentTimeMillis());89} catch (IOException ioe) {90ioe.printStackTrace();91}92e = bs.toByteArray();93}9495return e;96}9798public void decode(InputStream stream) {99}100public void encode(OutputStream stream) {101}102public Principal getGuarantor() {103return null;104}105public Principal getPrincipal() {106return null;107}108public PublicKey getPublicKey() {109return this;110}111public String toString(boolean detailed) {112return null;113}114}115116117class MyIdentityScope extends IdentityScope {118public MyIdentityScope(String name) {119super(name);120}121122public int size() {123return 0;124}125126public Identity getIdentity(String name) {127return null;128}129130public Identity getIdentity(PublicKey key) {131return null;132}133134public void addIdentity(Identity identity) {135}136137public void removeIdentity(Identity identity) {138}139140public java.util.Enumeration identities() {141return null;142}143144145}146147148