Path: blob/master/test/jdk/java/rmi/registry/reexport/Reexport.java
41153 views
/*1* Copyright (c) 1999, 2018, 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/* @test24* @bug 412032925* @summary RMI registry creation is impossible if first attempt fails.26* @library ../../testlibrary27* @modules java.rmi/sun.rmi.registry28* java.rmi/sun.rmi.server29* java.rmi/sun.rmi.transport30* java.rmi/sun.rmi.transport.tcp31* @build TestLibrary32* @run main/othervm Reexport33*/3435/*36* If a VM could not create an RMI registry because the registry port37* was already occupied by this or other processes, the next38* time the VM tried to create a registry (after the other registry39* was brought down) the attempt would fail. The second try to create40* a registry would fail because the registry ObjID would still be in41* use when it should never have been allocated.42*43* The test creates this conflict starting a dummy tcp server and ensures44* that a registry can still be created after the conflict is resolved.45*/4647import java.io.IOException;48import java.net.InetSocketAddress;49import java.nio.channels.ServerSocketChannel;50import java.rmi.registry.LocateRegistry;51import java.rmi.registry.Registry;5253public class Reexport {54static public void main(String[] argv) throws IOException {5556for (int loop = 0; loop < 10; loop++) {57System.err.println("\nat loop: " + loop);58int port = -1;59try (ServerSocketChannel server = ServerSocketChannel.open();) {60server.bind(null);61InetSocketAddress addr = (InetSocketAddress)server.getLocalAddress();62port = addr.getPort();6364System.err.println("Creating duplicate registry, this should fail...");65createReg(port, true);66}67try {68if (createReg(port, false) == null) {69TestLibrary.bomb("Could not create registry on second try");70}71System.err.println("Test passed");72return;73} catch (Exception e) {74String err = e.getMessage();75if (err.contains("Address already in use")76|| err.contains("Port already in use")) {77continue;78}79TestLibrary.bomb(e);80}81}82TestLibrary.bomb("Test failed");83}8485static Registry createReg(int port, boolean expectException) {86Registry reg = null;8788try {89reg = LocateRegistry.createRegistry(port);90if (expectException) {91TestLibrary.bomb("Registry is up, an Exception is expected!");92}93} catch (Throwable e) {94if (expectException) {95System.err.println("EXPECTING PORT IN USE EXCEPTION:");96System.err.println(e.getMessage());97e.printStackTrace();98} else {99TestLibrary.bomb((Exception) e);100}101}102return reg;103}104}105106107