/* * Copyright 2019-2023 Amazon.com, Inc. or its affiliates. All Rights Reserved. * * Licensed under the Apache License, Version 2.0 (the "License"). * You may not use this file except in compliance with the License. * A copy of the License is located at * * http://aws.amazon.com/apache2.0 * * or in the "license" file accompanying this file. This file is distributed * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either * express or implied. See the License for the specific language governing * permissions and limitations under the License. */ package com.amazonaws.regions; import org.junit.Test; import utils.TestHttpServer; import javax.xml.parsers.ParserConfigurationException; import java.io.ByteArrayInputStream; import java.io.IOException; import static org.junit.Assert.*; public class RegionMetadataParserTest { @Test public void testExternalEntity() throws IOException { TestHttpServer server = new TestHttpServer("secret"); try { String xml = String.format( "\n" + "\n" + "]>\n" + "&foo;", server.url()); parse(xml); } catch (IOException e) { // okay } finally { server.stop(); } if (server.accepted()) { fail("Oops! The server has been reached!"); } } @Test public void testExternalSchema() throws IOException { TestHttpServer server = new TestHttpServer("secret"); try { String xml = String.format( "\n" + "\n" + "", server.url()); parse(xml); } catch (IOException e) { // okay } finally { server.stop(); } if (server.accepted()) { fail("Oops! The server has been reached!"); } } @Test public void testExternalEntityParameter() throws IOException { TestHttpServer server = new TestHttpServer("secret"); try { String xml = String.format( "\n" + "\n" + "%%sp;" + "]>\n" + "", server.url()); parse(xml); } catch (IOException e) { // okay } finally { server.stop(); } if (server.accepted()) { fail("Oops! The server has been reached!"); } } @Test(expected = IOException.class) public void billionLaughs() throws IOException { String xml = "\n" + "\n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + "]>\n" + "&lol9;"; parse(xml); } private static void parse(String xml) throws IOException { try { RegionMetadataParser.parse(new ByteArrayInputStream(xml.getBytes())); } catch (IOException e) { if (e.getCause() instanceof ParserConfigurationException) { e.printStackTrace(System.out); fail("Looks like parser configuration failed"); } throw e; } } }