Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/security/x509/AlgorithmId/AlgorithmIdEqualsHashCode.java
38853 views
/*1* Copyright (c) 1999, 2021, 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 4170635 825824727* @summary Verify equals()/hashCode() contract honored28*/2930import java.io.*;31import java.security.AlgorithmParameters;32import java.security.spec.MGF1ParameterSpec;33import java.security.spec.PSSParameterSpec;3435import sun.security.util.DerValue;36import sun.security.x509.*;3738public class AlgorithmIdEqualsHashCode {3940public static void main(String[] args) throws Exception {4142AlgorithmId ai1 = AlgorithmId.get("DH");43AlgorithmId ai2 = AlgorithmId.get("DH");44AlgorithmId ai3 = AlgorithmId.get("DH");4546// supposedly transitivity is broken47// System.out.println(ai1.equals(ai2));48// System.out.println(ai2.equals(ai3));49// System.out.println(ai1.equals(ai3));5051if ( (ai1.equals(ai2)) == (ai2.equals(ai3)) == (ai1.equals(ai3)))52System.out.println("PASSED transitivity test");53else54throw new Exception("Failed equals transitivity() contract");5556if ( (ai1.equals(ai2)) == (ai1.hashCode()==ai2.hashCode()) )57System.out.println("PASSED equals()/hashCode() test");58else59throw new Exception("Failed equals()/hashCode() contract");6061// check that AlgorithmIds with same name but different params62// are not equal63AlgorithmParameters algParams1 =64AlgorithmParameters.getInstance("RSASSA-PSS");65AlgorithmParameters algParams2 =66AlgorithmParameters.getInstance("RSASSA-PSS");67algParams1.init(new PSSParameterSpec("SHA-1", "MGF1",68MGF1ParameterSpec.SHA1, 20, PSSParameterSpec.TRAILER_FIELD_BC));69algParams2.init(new PSSParameterSpec("SHA-256", "MGF1",70MGF1ParameterSpec.SHA1, 20, PSSParameterSpec.TRAILER_FIELD_BC));71ai1 = new AlgorithmId(AlgorithmId.RSASSA_PSS_oid, algParams1);72ai2 = new AlgorithmId(AlgorithmId.RSASSA_PSS_oid, algParams2);73if (ai1.equals(ai2)) {74throw new Exception("Failed equals() contract");75} else {76System.out.println("PASSED equals() test");77}7879// check that two AlgorithmIds created with the same parameters but80// one with DER encoded parameters and the other with81// AlgorithmParameters are equal82byte[] encoded = ai1.encode();83ai3 = AlgorithmId.parse(new DerValue(encoded));84if (!ai1.equals(ai3)) {85throw new Exception("Failed equals() contract");86} else {87System.out.println("PASSED equals() test");88}8990// check that two AlgorithmIds created with different parameters but91// one with DER encoded parameters and the other with92// AlgorithmParameters are not equal93if (ai2.equals(ai3)) {94throw new Exception("Failed equals() contract");95} else {96System.out.println("PASSED equals() test");97}98}99}100101102