|
| 1 | +/* |
| 2 | + * Elemental |
| 3 | + * Copyright (C) 2024, Evolved Binary Ltd |
| 4 | + * |
| 5 | + |
| 6 | + * https://www.evolvedbinary.com | https://www.elemental.xyz |
| 7 | + * |
| 8 | + * This library is free software; you can redistribute it and/or |
| 9 | + * modify it under the terms of the GNU Lesser General Public |
| 10 | + * License as published by the Free Software Foundation; version 2.1. |
| 11 | + * |
| 12 | + * This library is distributed in the hope that it will be useful, |
| 13 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | + * Lesser General Public License for more details. |
| 16 | + * |
| 17 | + * You should have received a copy of the GNU Lesser General Public |
| 18 | + * License along with this library; if not, write to the Free Software |
| 19 | + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 20 | + */ |
| 21 | +package org.exist.xquery; |
| 22 | + |
| 23 | +import org.exist.test.ExistXmldbEmbeddedServer; |
| 24 | +import org.junit.BeforeClass; |
| 25 | +import org.junit.ClassRule; |
| 26 | +import org.junit.Test; |
| 27 | +import org.xmldb.api.base.Collection; |
| 28 | +import org.xmldb.api.base.Resource; |
| 29 | +import org.xmldb.api.base.XMLDBException; |
| 30 | + |
| 31 | +import static org.junit.Assert.*; |
| 32 | + |
| 33 | +/** |
| 34 | + * @author <a href="mailto:[email protected]">Adam Retter</a> |
| 35 | + */ |
| 36 | +public class WatchdogTest { |
| 37 | + |
| 38 | + @ClassRule |
| 39 | + public static final ExistXmldbEmbeddedServer existEmbeddedServer = new ExistXmldbEmbeddedServer(false, true, true); |
| 40 | + |
| 41 | + @BeforeClass |
| 42 | + public static void setup() throws XMLDBException { |
| 43 | + final String queryModule = "module namespace nodes = \"http://nodes\";\n" + |
| 44 | + "\n" + |
| 45 | + "declare function nodes:many()\n" + |
| 46 | + "{\n" + |
| 47 | + " <a id=\"too-many\" output-size-limit=\"{util:get-option('exist:output-size-limit')}\">\n" + |
| 48 | + " <b><c/><d/><e><f/></e><g/></b>\n" + |
| 49 | + " <b><c/><d/><e><f/></e><g/></b>\n" + |
| 50 | + " <b><c/><d/><e><f/></e><g/></b>\n" + |
| 51 | + " </a>\n" + |
| 52 | + "};"; |
| 53 | + |
| 54 | + try (final Collection dbCollection = existEmbeddedServer.getRoot(); |
| 55 | + final Collection watchdogTestCollection = existEmbeddedServer.createCollection(dbCollection, "watchdog-test")) { |
| 56 | + final Resource nodesModule = watchdogTestCollection.createResource("nodes.xqm", "BinaryResource"); |
| 57 | + nodesModule.setContent(queryModule); |
| 58 | + watchdogTestCollection.storeResource(nodesModule); |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + @Test |
| 63 | + public void outputSizeLimitUnderInImportModule() throws XMLDBException { |
| 64 | + final String query = |
| 65 | + "declare option exist:output-size-limit \"100\";\n" + |
| 66 | + "import module namespace nodes = \"http://nodes\" at \"/db/watchdog-test/nodes.xqm\";\n" + |
| 67 | + "nodes:many()"; |
| 68 | + |
| 69 | + final String result = existEmbeddedServer.executeOneValue(query); |
| 70 | + assertNotNull(result); |
| 71 | + } |
| 72 | + |
| 73 | + @Test |
| 74 | + public void outputSizeLimitOverInImportModule() { |
| 75 | + final String query = |
| 76 | + "declare option exist:output-size-limit \"4\";\n" + |
| 77 | + "import module namespace nodes = \"http://nodes\" at \"/db/watchdog-test/nodes.xqm\";\n" + |
| 78 | + "nodes:many()"; |
| 79 | + |
| 80 | + try { |
| 81 | + existEmbeddedServer.executeOneValue(query); |
| 82 | + } catch (final XMLDBException e) { |
| 83 | + assertTrue(e.getMessage().startsWith("exerr:ERROR The constructed document fragment exceeded the predefined output-size-limit")); |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + @Test |
| 88 | + public void outputSizeLimitUnderInLoadXQueryModule() throws XMLDBException { |
| 89 | + final String query = |
| 90 | + "declare option exist:output-size-limit \"100\";\n" + |
| 91 | + "let $nodes-mod := fn:load-xquery-module(\"http://nodes\", map{ \"location-hints\": \"/db/watchdog-test/nodes.xqm\" })\n" + |
| 92 | + "let $nodes-mod-many := $nodes-mod?functions?(QName(\"http://nodes\", \"many\"))?0\n" + |
| 93 | + "return\n" + |
| 94 | + " $nodes-mod-many()"; |
| 95 | + |
| 96 | + final String result = existEmbeddedServer.executeOneValue(query); |
| 97 | + assertNotNull(result); |
| 98 | + } |
| 99 | + |
| 100 | + @Test |
| 101 | + public void outputSizeLimitOverInLoadXQueryModule() throws XMLDBException { |
| 102 | + final String query = |
| 103 | + "declare option exist:output-size-limit \"4\";\n" + |
| 104 | + "let $nodes-mod := fn:load-xquery-module(\"http://nodes\", map{ \"location-hints\": \"/db/watchdog-test/nodes.xqm\" })\n" + |
| 105 | + "let $nodes-mod-many := $nodes-mod?functions?(QName(\"http://nodes\", \"many\"))?0\n" + |
| 106 | + "return\n" + |
| 107 | + " $nodes-mod-many()"; |
| 108 | + |
| 109 | + try { |
| 110 | + existEmbeddedServer.executeOneValue(query); |
| 111 | + } catch (final XMLDBException e) { |
| 112 | + assertTrue(e.getMessage().startsWith("exerr:ERROR The constructed document fragment exceeded the predefined output-size-limit")); |
| 113 | + } |
| 114 | + } |
| 115 | +} |
0 commit comments