Path: blob/master/test/jdk/javax/net/ssl/compatibility/JdkInfo.java
66646 views
/*1* Copyright (c) 2017, 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*/2223import java.nio.file.Path;24import java.util.Arrays;25import java.util.LinkedHashMap;26import java.util.List;27import java.util.Map;2829/*30* It represents a JDK with some specific attributes.31* If two JdkInfo instances have the same version value, the instances are32* regarded as equivalent.33*/34public class JdkInfo {3536public static final JdkInfo DEFAULT = new JdkInfo(Jdk.DEFAULT.getPath());3738public final Path javaPath;3940public final String version;41public final List<String> supportedProtocols;42public final List<String> enabledProtocols;43public final List<String> supportedCipherSuites;44public final List<String> enabledCipherSuites;45public final boolean supportsSNI;46public final boolean supportsALPN;4748public JdkInfo(Path javaPath) {49this.javaPath = javaPath;5051String output = jdkAttributes(javaPath);52if (output == null || output.trim().isEmpty()) {53throw new RuntimeException(54"Cannot determine the JDK attributes: " + javaPath);55}5657String[] attributes = Utilities.split(output, Utilities.PARAM_DELIMITER);58version = parseAttribute(attributes[0]);59supportedProtocols = parseListAttribute(attributes[1]);60enabledProtocols = parseListAttribute(attributes[2]);61supportedCipherSuites = parseListAttribute(attributes[3]);62enabledCipherSuites = parseListAttribute(attributes[4]);63supportsSNI = parseBooleanAttribute(attributes[5]);64supportsALPN = parseBooleanAttribute(attributes[6]);65}6667private List<String> parseListAttribute(String attribute) {68attribute = parseAttribute(attribute);69return Arrays.asList(attribute.split(","));70}7172private boolean parseBooleanAttribute(String attribute) {73attribute = parseAttribute(attribute);74return Boolean.parseBoolean(attribute);75}76private String parseAttribute(String attribute) {77return attribute.replaceAll(".*=", "");78}7980// Determines the specific attributes for the specified JDK.81private static String jdkAttributes(Path javaPath) {82Map<String, String> props = new LinkedHashMap<>();83props.put("java.security.properties", Utils.SEC_PROPS_FILE);84return ProcUtils.java(javaPath, JdkInfoUtils.class, props).getOutput();85}8687@Override88public int hashCode() {89return version == null ? 0 : version.hashCode();90}9192@Override93public boolean equals(Object obj) {94if (this == obj) {95return true;96}97if (obj == null) {98return false;99}100if (getClass() != obj.getClass()) {101return false;102}103JdkInfo other = (JdkInfo) obj;104if (version == null) {105if (other.version != null) {106return false;107}108} else if (!version.equals(other.version)) {109return false;110}111return true;112}113114public boolean supportsProtocol(Protocol protocol) {115return supportedProtocols.contains(protocol.name);116}117118public boolean enablesProtocol(Protocol protocol) {119return enabledProtocols.contains(protocol.name);120}121122public boolean supportsCipherSuite(CipherSuite cipherSuite) {123return supportedCipherSuites.contains(cipherSuite.name());124}125126public boolean enablesCipherSuite(CipherSuite cipherSuite) {127return enabledCipherSuites.contains(cipherSuite.name());128}129}130131132