Path: blob/master/test/jdk/sun/security/x509/AlgorithmId/AlgorithmIdEqualsHashCode.java
41153 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* @modules java.base/sun.security.x509 java.base/sun.security.util29*/3031import java.io.*;32import java.security.AlgorithmParameters;33import java.security.spec.MGF1ParameterSpec;34import java.security.spec.PSSParameterSpec;3536import sun.security.util.DerValue;37import sun.security.x509.*;3839public class AlgorithmIdEqualsHashCode {4041public static void main(String[] args) throws Exception {4243AlgorithmId ai1 = AlgorithmId.get("DH");44AlgorithmId ai2 = AlgorithmId.get("DH");45AlgorithmId ai3 = AlgorithmId.get("DH");4647// supposedly transitivity is broken48// System.out.println(ai1.equals(ai2));49// System.out.println(ai2.equals(ai3));50// System.out.println(ai1.equals(ai3));5152if ( (ai1.equals(ai2)) == (ai2.equals(ai3)) == (ai1.equals(ai3)))53System.out.println("PASSED transitivity test");54else55throw new Exception("Failed equals transitivity() contract");5657if ( (ai1.equals(ai2)) == (ai1.hashCode()==ai2.hashCode()) )58System.out.println("PASSED equals()/hashCode() test");59else60throw new Exception("Failed equals()/hashCode() contract");6162// check that AlgorithmIds with same name but different params63// are not equal64AlgorithmParameters algParams1 =65AlgorithmParameters.getInstance("RSASSA-PSS");66AlgorithmParameters algParams2 =67AlgorithmParameters.getInstance("RSASSA-PSS");68algParams1.init(new PSSParameterSpec("SHA-1", "MGF1",69MGF1ParameterSpec.SHA1, 20, PSSParameterSpec.TRAILER_FIELD_BC));70algParams2.init(new PSSParameterSpec("SHA-256", "MGF1",71MGF1ParameterSpec.SHA1, 20, PSSParameterSpec.TRAILER_FIELD_BC));72ai1 = new AlgorithmId(AlgorithmId.RSASSA_PSS_oid, algParams1);73ai2 = new AlgorithmId(AlgorithmId.RSASSA_PSS_oid, algParams2);74if (ai1.equals(ai2)) {75throw new Exception("Failed equals() contract");76} else {77System.out.println("PASSED equals() test");78}7980// check that two AlgorithmIds created with the same parameters but81// one with DER encoded parameters and the other with82// AlgorithmParameters are equal83byte[] encoded = ai1.encode();84ai3 = AlgorithmId.parse(new DerValue(encoded));85if (!ai1.equals(ai3)) {86throw new Exception("Failed equals() contract");87} else {88System.out.println("PASSED equals() test");89}9091// check that two AlgorithmIds created with different parameters but92// one with DER encoded parameters and the other with93// AlgorithmParameters are not equal94if (ai2.equals(ai3)) {95throw new Exception("Failed equals() contract");96} else {97System.out.println("PASSED equals() test");98}99}100}101102103