/* * Copyright (c) 2018, 2020, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions.
*/
/** * This test checks if the strings in various Resources files are used * properly. Each string must be used somewhere, and each getString() call * must use an existing string. * <p> * For each Resources file, the test maintains a list of where the strings are * used (a file or a directory) and how they are used (one or more patterns). * <p> * If this test fails, there can be several reasons: * <p> * 1. If a string is not found, it has not been added to a Resources file. * <p> * 2. If a string is not used, maybe the call was removed earlier but the * Resources file was not updated. Or, the file is not listed or the * pattern is not correct and the usage is not found. * <p> * Because of #2 above, this test might not be complete. If a getString() * is called but either the file and calling pattern is not listed here, * we cannot guarantee it exists in a Resources file.
*/ publicclass Usages {
// For each Resources file, where and how the strings are used. static Map<ListResourceBundle, List<Pair>> MAP = Map.of( new sun.security.tools.keytool.Resources(), List.of( new Pair("java.base/share/classes/sun/security/tools/keytool/Main.java",
List.of(RB_GETSTRING, KT_ENUM)), new Pair("java.base/share/classes/sun/security/tools/KeyStoreUtil.java",
List.of(RB_GETSTRING))), new sun.security.util.AuthResources(), List.of( new Pair("java.base/share/classes/sun/security/provider/ConfigFile.java",
List.of(GETAUTHSTRING, IOEXCEPTION)), new Pair("jdk.security.auth/share/classes/com/sun/security/auth/",
List.of(GETAUTHSTRING))), new sun.security.tools.jarsigner.Resources(), List.of( new Pair("jdk.jartool/share/classes/sun/security/tools/jarsigner/Main.java",
List.of(RB_GETSTRING)), new Pair("java.base/share/classes/sun/security/provider/certpath/OCSP.java",
List.of(EVENT_OCSP_CRL)), new Pair("java.base/share/classes/sun/security/provider/certpath/DistributionPointFetcher.java",
List.of(EVENT_OCSP_CRL)), new Pair("java.base/share/classes/sun/security/tools/KeyStoreUtil.java",
List.of(RB_GETSTRING))), new sun.security.util.Resources(), List.of( new Pair("jdk.crypto.cryptoki/share/classes/sun/security/pkcs11/SunPKCS11.java",
List.of(MGR_GETSTRING)), new Pair("java.base/share/classes/sun/security/provider/PolicyParser.java",
List.of(LOC_GETNONLOC, NEW_LOC)), new Pair("java.base/share/classes/sun/security/provider/PolicyFile.java",
List.of(MGR_GETSTRING, LOC_GETNONLOC, LOC_GETNONLOC_POLICY)), new Pair("java.base/share/classes/javax/security/auth/",
List.of(MGR_GETSTRING)))
);
publicstaticvoid main(String[] args) { if (Files.exists(SRC)) {
MAP.forEach(Usages::check);
} else {
System.out.println("No src directory. Test skipped.");
}
}
// Initialize unused to be all keys. Each time a key is used it // is removed. We cannot reuse keys because a key might be used // multiple times. Make it a Set so we can check duplicates.
Set<String> unused = new HashSet<>(keys);
keys.forEach(Usages::checkKeyFormat); if (keys.size() != unused.size()) { thrownew RuntimeException("Duplicates found");
}
for (Pair fnp : fnps) {
Files.find(SRC.resolve(fnp.path), Integer.MAX_VALUE,
(p, attr) -> p.toString().endsWith(".java"))
.forEach(pa -> { try {
String content = Files.readString(pa); for (Pattern p : fnp.patterns) {
Matcher m = p.matcher(content); while (m.find()) {
String arg = m.group(1); // Special case in PolicyFile.java: if (arg.startsWith("POLICY + \"")) {
arg = "java.security.policy"
+ arg.substring(10);
} if (!keys.contains(arg)) { thrownew RuntimeException( "Not found: " + arg);
}
unused.remove(arg);
}
}
} catch (IOException e) { thrownew UncheckedIOException(e);
}
});
} if (!unused.isEmpty()) { thrownew RuntimeException("Unused keys: " + unused);
}
} catch (Exception e) { thrownew RuntimeException(e);
}
}
privatestaticvoid checkKeyFormat(String key) { for (char c : key.toCharArray()) { if (Character.isLetter(c) || Character.isDigit(c) ||
c == '{' || c == '}' || c == '.') { // OK
} else { thrownew RuntimeException( "Illegal char [" + c + "] in key: " + key);
}
}
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.