/* * Copyright (c) 2016, 2018, 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.
*/
// Check default timeout is 30s. Use real KDC only, otherwise too // slow to wait for timeout. Each request (without preauth and with // preauth) might be retried 3 times, and could fail if one fails for // all 3 times.
writeConf(-1, -1, p3);
test("(c30000){2,6}|(c30000){3,6}-");
// 1. Default policy is tryLast //Security.setProperty("krb5.kdc.bad.policy", "tryLast");
// Need a real KDC, otherwise there is no last good. // This test waste 3 seconds waiting for d1 to timeout. // It is possible the real KDC cannot fulfil the request // in 3s, so it might fail (either 1st time or 2nd time).
writeConf(1, 3000, p1, p3);
test("a3000c3000c3000|a3000c3000-|a3000c3000c3000a3000-");
// If a test case won't use a real KDC, it can be sped up.
writeConf(3, 5, p1, p2);
test("a5a5a5b5b5b5-"); // default max_retries == 3
test("a5a5a5b5b5b5-"); // all bad means no bad
// 2. No policy.
Security.setProperty("krb5.kdc.bad.policy", "");
Config.refresh();
// This case needs a real KDC, otherwise, all bad means no // bad and we cannot tell the difference. This case waste 3 // seconds on d1 to timeout twice. It is possible the real KDC // cannot fulfil the request within 3s, so it might fail // (either 1st time or 2nd time).
writeConf(1, 3000, p1, p3);
test("a3000c3000a3000c3000|a3000c3000-|a3000c3000a3000c3000-");
// 3. tryLess with no argument means tryLess:1,5000
Security.setProperty("krb5.kdc.bad.policy", "tryLess");
// This case will waste 11s. We are checking that the default // value of 5000 in tryLess is only used if it's less than timeout // in krb5.conf
writeConf(1, 6000, p1);
test("a6000-"); // timeout in krb5.conf is 6s
test("a5000-"); // tryLess to 5s. This line can be made faster if // d1 is a read KDC, but we have no existing method // to start KDC on an existing ServerSocket (port).
writeConf(-1, 4, p1, p2);
test("a4a4a4b4b4b4-"); // default max_retries == 3
test("a4b4-"); // tryLess to 1. And since 4 < 5000, use 4.
Config.refresh();
test("a4a4a4b4b4b4-");
/** * Writes a krb5.conf file. * @param max max_retries, -1 if not set * @param to kdc_timeout, -1 if not set * @param ports where KDCs listen on
*/ staticvoid writeConf(int max, int to, int... ports) throws Exception {
// content of krb5.conf
String conf = "";
// Extra settings in [libdefaults]
String inDefaults = "";
// Extra settings in [realms]
String inRealm = "";
// We will randomly put extra settings only in [libdefaults], // or in [realms] but with different values in [libdefaults], // to prove that settings in [realms] override those in [libdefaults].
Random r = new Random();
/** * One call of krb5 login. As long as the result matches one of expected, * the test is considered as success. The grammar of expected is * * kdc#, timeout, kdc#, timeout, ..., optional "-" for failure
*/ staticvoid test(String... expected) throws Exception {
String[] lines = new String(bo.toByteArray()).split("\n");
StringBuilder sb = new StringBuilder(); for (String line: lines) { if (cm.match(line)) { if (udp != cm.isUDP()) {
sb.append("x");
}
sb.append(cm.kdc()).append(cm.timeout());
}
} if (failed) sb.append('-');
String output = sb.toString();
boolean found = false; for (String ex : expected) { if (output.matches(ex)) {
System.out.println("Expected: " + ex + ", actual " + output);
found = true; break;
}
}
if (!found) {
System.out.println("--------------- ERROR START -------------");
System.out.println(new String(bo.toByteArray()));
System.out.println("--------------- ERROR END ---------------"); thrownew Exception("Does not match. Output is " + output);
}
}
/** * A helper class to match the krb5 debug output: * >>> KDCCommunication: kdc=host UDP:11555, timeout=200,Attempt =1, #bytes=138 * * Example: * DebugMatcher cm = new DebugMatcher(); * cm.addPort(12345).addPort(11555); * for (String line : debugOutput) { * if (cm.match(line)) { * System.out.printf("%c%d\n", cm.kdc(), cm.timeout()); * // shows b200 for the example above * } * }
*/ staticclass DebugMatcher {
List<Integer> kdcPorts = new ArrayList<>();
Matcher matcher;
/** * Add KDC ports one by one. See {@link #kdc()}.
*/
DebugMatcher addPort(int port) { if (port > 0) {
kdcPorts.add(port);
} else {
kdcPorts.clear();
} returnthis;
}
/** * When a line matches the ">>> KDCCommunication:" pattern. After a * match, the getters below can be called on this match.
*/ boolean match(String line) {
matcher = re.matcher(line); return matcher.find();
}
/** * Protocol of this match, "UDP" or "TCP".
*/ boolean isUDP() { return matcher.group(1).equals("UDP");
}
/** * KDC for this match, "a" for the one 1st added bt addPort(), "b" * for second, etc. Undefined for not added.
*/ char kdc() { int port = Integer.parseInt(matcher.group(2)); return (char) (kdcPorts.indexOf(port) + 'a');
}
/** * Timeout value for this match.
*/ int timeout() { return Integer.parseInt(matcher.group(3));
}
}
}
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 und die Messung sind noch experimentell.