Path: blob/master/test/jdk/sun/security/util/Resources/Usages.java
41152 views
/*1* Copyright (c) 2018, 2020, 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* @bug 821593726* @modules java.base/sun.security.util27* java.base/sun.security.tools.keytool28* jdk.jartool/sun.security.tools.jarsigner29* @summary Check usages of security-related Resources files30*/3132import java.io.IOException;33import java.io.UncheckedIOException;34import java.nio.file.Files;35import java.nio.file.Path;36import java.util.Collections;37import java.util.HashSet;38import java.util.List;39import java.util.ListResourceBundle;40import java.util.Map;41import java.util.Set;42import java.util.regex.Matcher;43import java.util.regex.Pattern;4445/**46* This test checks if the strings in various Resources files are used47* properly. Each string must be used somewhere, and each getString() call48* must use an existing string.49* <p>50* For each Resources file, the test maintains a list of where the strings are51* used (a file or a directory) and how they are used (one or more patterns).52* <p>53* If this test fails, there can be several reasons:54* <p>55* 1. If a string is not found, it has not been added to a Resources file.56* <p>57* 2. If a string is not used, maybe the call was removed earlier but the58* Resources file was not updated. Or, the file is not listed or the59* pattern is not correct and the usage is not found.60* <p>61* Because of #2 above, this test might not be complete. If a getString()62* is called but either the file and calling pattern is not listed here,63* we cannot guarantee it exists in a Resources file.64*/65public class Usages {6667// src folder68static Path SRC = Path.of(69System.getProperty("test.src"), "../../../../../../src/")70.normalize();7172// rb.getString(). Used by keytool, jarsigner, and KeyStoreUtil.73static Pattern RB_GETSTRING = Pattern.compile(74"(?m)rb[ \\n]*\\.getString[ \\n]*\\([ \\n]*\"(.*?)\"\\)");7576static Pattern EVENT_OCSP_CRL = Pattern.compile(77"Event\\.report\\(.*, \"(.*?)\",");7879// Command and Option enums in keytool80static Pattern KT_ENUM = Pattern.compile("\\n +[A-Z]+\\(.*\"(.*)\"");8182// ResourceMgr.getAuthResourceString83static Pattern GETAUTHSTRING = Pattern.compile(84"getAuthResourceString[ \\n]*\\([ \\n]*\"(.*?)\"\\)");8586// ResourceMgr.getString87static Pattern MGR_GETSTRING = Pattern.compile(88"ResourcesMgr\\.getString[ \\n]*\\([ \\n]*\"(.*?)\"\\)");8990// LocalizedMessage.getNonlocalized("...")91static Pattern LOC_GETNONLOC = Pattern.compile(92"LocalizedMessage\\.getNonlocalized[ \\n]*\\([ \\n]*\"(.*?)\"");9394// LocalizedMessage.getNonlocalized(POLICY + "...")95static Pattern LOC_GETNONLOC_POLICY = Pattern.compile(96"LocalizedMessage\\.getNonlocalized[ \\n]*\\([ \\n]*(POLICY \\+ \".*?)\"");9798// new LocalizedMessage("...")99static Pattern NEW_LOC = Pattern.compile(100"new LocalizedMessage[ \\n]*\\([ \\n]*\"(.*?)\"");101102// ioException in ConfigFile.java103static Pattern IOEXCEPTION = Pattern.compile(104"ioException[ \\n]*\\([ \\n]*\"(.*?)\",");105106// For each Resources file, where and how the strings are used.107static Map<ListResourceBundle, List<Pair>> MAP = Map.of(108new sun.security.tools.keytool.Resources(), List.of(109new Pair("java.base/share/classes/sun/security/tools/keytool/Main.java",110List.of(RB_GETSTRING, KT_ENUM)),111new Pair("java.base/share/classes/sun/security/tools/KeyStoreUtil.java",112List.of(RB_GETSTRING))),113new sun.security.util.AuthResources(), List.of(114new Pair("java.base/share/classes/sun/security/provider/ConfigFile.java",115List.of(GETAUTHSTRING, IOEXCEPTION)),116new Pair("jdk.security.auth/share/classes/com/sun/security/auth/",117List.of(GETAUTHSTRING))),118new sun.security.tools.jarsigner.Resources(), List.of(119new Pair("jdk.jartool/share/classes/sun/security/tools/jarsigner/Main.java",120List.of(RB_GETSTRING)),121new Pair("java.base/share/classes/sun/security/provider/certpath/OCSP.java",122List.of(EVENT_OCSP_CRL)),123new Pair("java.base/share/classes/sun/security/provider/certpath/DistributionPointFetcher.java",124List.of(EVENT_OCSP_CRL)),125new Pair("java.base/share/classes/sun/security/tools/KeyStoreUtil.java",126List.of(RB_GETSTRING))),127new sun.security.util.Resources(), List.of(128new Pair("jdk.crypto.cryptoki/share/classes/sun/security/pkcs11/SunPKCS11.java",129List.of(MGR_GETSTRING)),130new Pair("java.base/share/classes/sun/security/provider/PolicyParser.java",131List.of(LOC_GETNONLOC, NEW_LOC)),132new Pair("java.base/share/classes/sun/security/provider/PolicyFile.java",133List.of(MGR_GETSTRING, LOC_GETNONLOC, LOC_GETNONLOC_POLICY)),134new Pair("java.base/share/classes/javax/security/auth/",135List.of(MGR_GETSTRING)))136);137138public static void main(String[] args) {139if (Files.exists(SRC)) {140MAP.forEach(Usages::check);141} else {142System.out.println("No src directory. Test skipped.");143}144}145146private static void check(ListResourceBundle res, List<Pair> fnps) {147try {148System.out.println(">>>> Checking " + res.getClass().getName());149150List<String> keys = Collections.list(res.getKeys());151152// Initialize unused to be all keys. Each time a key is used it153// is removed. We cannot reuse keys because a key might be used154// multiple times. Make it a Set so we can check duplicates.155Set<String> unused = new HashSet<>(keys);156157keys.forEach(Usages::checkKeyFormat);158if (keys.size() != unused.size()) {159throw new RuntimeException("Duplicates found");160}161162for (Pair fnp : fnps) {163Files.find(SRC.resolve(fnp.path), Integer.MAX_VALUE,164(p, attr) -> p.toString().endsWith(".java"))165.forEach(pa -> {166try {167String content = Files.readString(pa);168for (Pattern p : fnp.patterns) {169Matcher m = p.matcher(content);170while (m.find()) {171String arg = m.group(1);172// Special case in PolicyFile.java:173if (arg.startsWith("POLICY + \"")) {174arg = "java.security.policy"175+ arg.substring(10);176}177if (!keys.contains(arg)) {178throw new RuntimeException(179"Not found: " + arg);180}181unused.remove(arg);182}183}184} catch (IOException e) {185throw new UncheckedIOException(e);186}187});188}189if (!unused.isEmpty()) {190throw new RuntimeException("Unused keys: " + unused);191}192} catch (Exception e) {193throw new RuntimeException(e);194}195}196197private static void checkKeyFormat(String key) {198for (char c : key.toCharArray()) {199if (Character.isLetter(c) || Character.isDigit(c) ||200c == '{' || c == '}' || c == '.') {201// OK202} else {203throw new RuntimeException(204"Illegal char [" + c + "] in key: " + key);205}206}207}208209static class Pair {210211public final String path;212public final List<Pattern> patterns;213214public Pair(String path, List<Pattern> patterns) {215this.path = path;216this.patterns = patterns;217}218}219}220221222