/* * Copyright (c) 2012, 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.
*/
// - Only PortUnreachableException will allow to continue execution. // - SocketTimeoutException may occur on Mac because it will not throw // PortUnreachableException for unreachable port in which case the Test // execution will be skipped. // - For Reachable port, the Test execution will get skipped. // - Any other Exception will be treated as Test failure. if (!findPortUnreachableExc()) {
System.out.println(String.format("WARNING: Either a reachable "
+ "connection found to %s:%s or SocketTimeoutException "
+ "occured which means PortUnreachableException not thrown"
+ " by the platform.", HOST, PORT)); return;
}
KDC kdc = KDC.existing(REALM, HOST, PORT);
KDC.saveConfig(KRB_CONF, kdc);
ExecutorService executor = Executors.newSingleThreadExecutor();
Future<Exception> future = executor.submit(new Callable<Exception>() {
@Override public Exception call() {
System.setProperty("java.security.krb5.conf", KRB_CONF); try {
Config.refresh(); // If PortUnreachableException is not received, the login // will consume about 3*3*30 seconds and the test will // timeout. try {
Context.fromUserPass("name", "pass".toCharArray(), true);
} catch (LoginException le) { // This is OK
}
System.out.println("Execution successful.");
} catch (Exception e) { return e;
} returnnull;
}
}); try {
Exception ex = null; if ((ex = future.get(TIMEOUT, TimeUnit.SECONDS)) != null) { thrownew RuntimeException(ex);
}
} catch (TimeoutException e) {
future.cancel(true); thrownew RuntimeException("PortUnreachableException not thrown.");
} finally {
executor.shutdownNow();
}
}
/** * If the remote destination to which the socket is connected does not * exist, or is otherwise unreachable, and if an ICMP destination unreachable * packet has been received for that address, then a subsequent call to * send or receive may throw a PortUnreachableException. Note, there is no * guarantee that the exception will be thrown.
*/ privatestaticboolean findPortUnreachableExc() throws Exception { try {
InetSocketAddress iaddr = new InetSocketAddress(HOST, PORT);
DatagramSocket dgSocket = new DatagramSocket();
dgSocket.setSoTimeout(5000);
dgSocket.connect(iaddr); byte[] data = newbyte[]{};
dgSocket.send(new DatagramPacket(data, data.length, iaddr));
dgSocket.receive(new DatagramPacket(data, data.length));
} catch (PortUnreachableException e) { returntrue;
} catch (SocketTimeoutException e) { returnfalse;
} returnfalse;
}
}
Messung V0.5
¤ Dauer der Verarbeitung: 0.14 Sekunden
(vorverarbeitet)
¤
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.