Path: blob/master/test/jdk/javax/management/loading/ParserInfiniteLoopTest.java
41149 views
/*1* Copyright (c) 2005, 2015, 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 504236426* @summary Malformed MLet text file causes infinite loop in parser.27* The MLetParser goes into an infinite loop when a tag is not28* terminated with the corresponding '>' and an opening '<' for29* the subsequent tag is encountered.30* @author Luis-Miguel Alventosa31*32* @run clean ParserInfiniteLoopTest33* @run build ParserInfiniteLoopTest34* @run main/othervm ParserInfiniteLoopTest mlet1.html35* @run main/othervm ParserInfiniteLoopTest mlet2.html36* @run main/othervm ParserInfiniteLoopTest mlet3.html37*/3839import java.io.File;40import java.io.IOException;41import javax.management.MBeanServer;42import javax.management.MBeanServerFactory;43import javax.management.ObjectName;44import javax.management.ServiceNotFoundException;45import javax.management.loading.MLet;4647public class ParserInfiniteLoopTest {4849public static void main(String[] args) throws Exception {5051boolean error = false;5253// Instantiate the MBean server54//55System.out.println("Create the MBean server");56MBeanServer mbs = MBeanServerFactory.createMBeanServer();5758// Instantiate an MLet59//60System.out.println("Create the MLet");61MLet mlet = new MLet();6263// Register the MLet MBean with the MBeanServer64//65System.out.println("Register the MLet MBean");66ObjectName mletObjectName = new ObjectName("Test:type=MLet");67mbs.registerMBean(mlet, mletObjectName);6869// Call getMBeansFromURL70//71System.out.println("Call mlet.getMBeansFromURL(<url>)");72String testSrc = System.getProperty("test.src");73System.out.println("test.src = " + testSrc);74String urlCodebase;75if (testSrc.startsWith("/")) {76urlCodebase =77"file:" + testSrc.replace(File.separatorChar, '/') + "/";78} else {79urlCodebase =80"file:/" + testSrc.replace(File.separatorChar, '/') + "/";81}82String mletFile = urlCodebase + args[0];83System.out.println("MLet File = " + mletFile);84try {85mlet.getMBeansFromURL(mletFile);86System.out.println(87"TEST FAILED: Expected ServiceNotFoundException not thrown");88error = true;89} catch (ServiceNotFoundException e) {90if (e.getCause() == null) {91System.out.println("TEST FAILED: Got unexpected null cause " +92"in ServiceNotFoundException");93error = true;94} else if (!(e.getCause() instanceof IOException)) {95System.out.println("TEST FAILED: Got unexpected non-null " +96"cause in ServiceNotFoundException");97error = true;98} else {99System.out.println("TEST PASSED: Got expected non-null " +100"cause in ServiceNotFoundException");101error = false;102}103e.printStackTrace(System.out);104}105106// Unregister the MLet MBean107//108System.out.println("Unregister the MLet MBean");109mbs.unregisterMBean(mletObjectName);110111// Release MBean server112//113System.out.println("Release the MBean server");114MBeanServerFactory.releaseMBeanServer(mbs);115116// End Test117//118System.out.println("Bye! Bye!");119if (error) System.exit(1);120}121}122123124