0N/A/*
2362N/A * Copyright (c) 1999, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A/*
0N/A * @test
0N/A * @author Gary Ellison
0N/A * @bug 4183136
0N/A * @summary java.security.Identity violates equals/hashCode contract
0N/A */
0N/Aimport java.security.*;
0N/Aimport java.io.*;
0N/A
0N/Apublic class EqualsHashCodeContract
0N/A{
0N/A public static void main(String args[]) throws Exception {
0N/A
0N/A Identity i1=new MyIdentity("identity",
0N/A new MyIdentityScope("IdentityScope"));
0N/A Identity i2=new MyIdentity("identity",
0N/A new MyIdentityScope("IdentityScope"));
0N/A Identity i3=new MyIdentity("identity",
0N/A new MyIdentityScope(""));
0N/A
0N/A PublicKey pk1=new MyPublicKey();
0N/A PublicKey pk2=new MyPublicKey();
0N/A
0N/A if ( !(i1.equals(i2)) == (i1.hashCode()==i2.hashCode()) ) {
0N/A System.err.println("FAILED");
0N/A Exception up = new
0N/A Exception("Contract violated -- same name and same scope");
0N/A throw up;
0N/A }
0N/A System.out.println("Test same name, same scope........... PASSED");
0N/A
0N/A i1.setPublicKey(pk1);
0N/A i3.setPublicKey(pk1);
0N/A if ( !((i1.equals(i3)) && (i1.hashCode()==i3.hashCode()))) {
0N/A System.err.println("FAILED");
0N/A Exception up = new Exception("Contract violated -- PublicKeys do not differ");
0N/A throw up;
0N/A }
0N/A System.out.println("Test same name, same PublicKeys ..... PASSED");
0N/A
0N/A System.out.println("TEST PASSED");
0N/A
0N/A }
0N/A}
0N/A
0N/Aclass MyIdentity extends Identity {
0N/A public MyIdentity(String name, IdentityScope is) throws KeyManagementException {
0N/A super(name, is);
0N/A }
0N/A}
0N/A
0N/Aclass MyPublicKey implements PublicKey, Certificate {
0N/A private byte e[] = null;
0N/A public String getAlgorithm() {
0N/A return null;
0N/A }
0N/A public String getFormat() {
0N/A return new String("PKCS15");
0N/A }
0N/A
0N/A public byte[] getEncoded() {
0N/A if (e == null) {
0N/A ByteArrayOutputStream bs = new ByteArrayOutputStream();
0N/A DataOutputStream ds = new DataOutputStream(bs);
0N/A try {
0N/A ds.writeLong(System.currentTimeMillis());
0N/A } catch (IOException ioe) {
0N/A ioe.printStackTrace();
0N/A }
0N/A e = bs.toByteArray();
0N/A }
0N/A
0N/A return e;
0N/A }
0N/A
0N/A public void decode(InputStream stream) {
0N/A }
0N/A public void encode(OutputStream stream) {
0N/A }
0N/A public Principal getGuarantor() {
0N/A return null;
0N/A }
0N/A public Principal getPrincipal() {
0N/A return null;
0N/A }
0N/A public PublicKey getPublicKey() {
0N/A return this;
0N/A }
0N/A public String toString(boolean detailed) {
0N/A return null;
0N/A }
0N/A}
0N/A
0N/A
0N/Aclass MyIdentityScope extends IdentityScope {
0N/A public MyIdentityScope(String name) {
0N/A super(name);
0N/A }
0N/A
0N/A public int size() {
0N/A return 0;
0N/A }
0N/A
0N/A public Identity getIdentity(String name) {
0N/A return null;
0N/A }
0N/A
0N/A public Identity getIdentity(PublicKey key) {
0N/A return null;
0N/A }
0N/A
0N/A public void addIdentity(Identity identity) {
0N/A }
0N/A
0N/A public void removeIdentity(Identity identity) {
0N/A }
0N/A
0N/A public java.util.Enumeration identities() {
0N/A return null;
0N/A }
0N/A
0N/A
0N/A}