|
| 1 | +/* |
| 2 | + * Copyright (C) 2014, Evolved Binary Ltd |
| 3 | + * |
| 4 | + * This file was originally ported from FusionDB to eXist-db by |
| 5 | + * Evolved Binary, for the benefit of the eXist-db Open Source community. |
| 6 | + * Only the ported code as it appears in this file, at the time that |
| 7 | + * it was contributed to eXist-db, was re-licensed under The GNU |
| 8 | + * Lesser General Public License v2.1 only for use in eXist-db. |
| 9 | + * |
| 10 | + * This license grant applies only to a snapshot of the code as it |
| 11 | + * appeared when ported, it does not offer or infer any rights to either |
| 12 | + * updates of this source code or access to the original source code. |
| 13 | + * |
| 14 | + * The GNU Lesser General Public License v2.1 only license follows. |
| 15 | + * |
| 16 | + * --------------------------------------------------------------------- |
| 17 | + * |
| 18 | + * Copyright (C) 2014, Evolved Binary Ltd |
| 19 | + * |
| 20 | + * This library is free software; you can redistribute it and/or |
| 21 | + * modify it under the terms of the GNU Lesser General Public |
| 22 | + * License as published by the Free Software Foundation; version 2.1. |
| 23 | + * |
| 24 | + * This library is distributed in the hope that it will be useful, |
| 25 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 26 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 27 | + * Lesser General Public License for more details. |
| 28 | + * |
| 29 | + * You should have received a copy of the GNU Lesser General Public |
| 30 | + * License along with this library; if not, write to the Free Software |
| 31 | + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 32 | + */ |
| 33 | +package org.exist.xquery.functions.util; |
| 34 | + |
| 35 | +import org.exist.EXistException; |
| 36 | +import org.exist.ExistSystemProperties; |
| 37 | +import org.exist.security.PermissionDeniedException; |
| 38 | +import org.exist.storage.BrokerPool; |
| 39 | +import org.exist.storage.DBBroker; |
| 40 | +import org.exist.test.ExistEmbeddedServer; |
| 41 | +import org.exist.util.DatabaseConfigurationException; |
| 42 | +import org.exist.xquery.XPathException; |
| 43 | +import org.exist.xquery.XQuery; |
| 44 | +import org.exist.xquery.value.Sequence; |
| 45 | +import org.junit.After; |
| 46 | +import org.junit.Before; |
| 47 | +import org.junit.Test; |
| 48 | +import org.junit.runner.RunWith; |
| 49 | +import org.junit.runners.Parameterized; |
| 50 | + |
| 51 | +import java.io.IOException; |
| 52 | +import java.net.URISyntaxException; |
| 53 | +import java.nio.file.Path; |
| 54 | +import java.nio.file.Paths; |
| 55 | +import java.util.Arrays; |
| 56 | +import java.util.HashSet; |
| 57 | +import java.util.Optional; |
| 58 | +import java.util.Set; |
| 59 | + |
| 60 | +import static org.junit.Assert.*; |
| 61 | + |
| 62 | +@RunWith(Parameterized.class) |
| 63 | +public class SystemPropertyTest { |
| 64 | + |
| 65 | + @Parameterized.Parameters(name = "{0}") |
| 66 | + public static java.util.Collection<Object[]> data() { |
| 67 | + return Arrays.asList(new Object[][] { |
| 68 | + { "non-secure", null, false }, |
| 69 | + { "secure", "conf-sys-props-admins-only.xml", true } |
| 70 | + }); |
| 71 | + } |
| 72 | + |
| 73 | + @Parameterized.Parameter(value = 0) |
| 74 | + public String testTypeName; |
| 75 | + |
| 76 | + @Parameterized.Parameter(value = 1) |
| 77 | + public String confFileName; |
| 78 | + |
| 79 | + @Parameterized.Parameter(value = 2) |
| 80 | + public boolean shouldReturnEmptySequence; |
| 81 | + |
| 82 | + private ExistEmbeddedServer existEmbeddedServer = null; |
| 83 | + |
| 84 | + @Before |
| 85 | + public void setup() throws URISyntaxException, DatabaseConfigurationException, EXistException, IOException { |
| 86 | + if (confFileName == null) { |
| 87 | + existEmbeddedServer = new ExistEmbeddedServer(true, true); |
| 88 | + } else { |
| 89 | + final Path confFile = Paths.get(getClass().getResource(confFileName).toURI()); |
| 90 | + existEmbeddedServer = new ExistEmbeddedServer(null, confFile, null, true, true); |
| 91 | + } |
| 92 | + existEmbeddedServer.startDb(); |
| 93 | + } |
| 94 | + |
| 95 | + @After |
| 96 | + public void teardown() { |
| 97 | + if (existEmbeddedServer != null) { |
| 98 | + existEmbeddedServer.stopDb(); |
| 99 | + } |
| 100 | + existEmbeddedServer = null; |
| 101 | + } |
| 102 | + |
| 103 | + @Test |
| 104 | + public void availableSystemProperties() throws EXistException, XPathException, PermissionDeniedException { |
| 105 | + final BrokerPool pool = existEmbeddedServer.getBrokerPool(); |
| 106 | + final XQuery xqueryService = pool.getXQueryService(); |
| 107 | + try (final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().getSystemSubject()))) { |
| 108 | + |
| 109 | + final String query = "util:available-system-properties()"; |
| 110 | + final Sequence result = xqueryService.execute(broker, query, null); |
| 111 | + assertFalse(result.isEmpty()); |
| 112 | + |
| 113 | + final Set<String> set = new HashSet<>(result.getItemCount()); |
| 114 | + for (int i = 0; i < result.getItemCount(); i++) { |
| 115 | + set.add(result.itemAt(i).getStringValue()); |
| 116 | + } |
| 117 | + |
| 118 | + assertTrue(set.contains(ExistSystemProperties.PROP_PRODUCT_VERSION)); |
| 119 | + |
| 120 | + if (shouldReturnEmptySequence) { |
| 121 | + assertFalse(set.contains("os.name")); |
| 122 | + } else { |
| 123 | + assertTrue(set.contains("os.name")); |
| 124 | + } |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + @Test |
| 129 | + public void systemProperty() throws EXistException, XPathException, PermissionDeniedException { |
| 130 | + final BrokerPool pool = existEmbeddedServer.getBrokerPool(); |
| 131 | + final XQuery xqueryService = pool.getXQueryService(); |
| 132 | + try (final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().getSystemSubject()))) { |
| 133 | + |
| 134 | + String query = "util:system-property('" + ExistSystemProperties.PROP_PRODUCT_NAME + "')"; |
| 135 | + Sequence result = xqueryService.execute(broker, query, null); |
| 136 | + assertEquals(1, result.getItemCount()); |
| 137 | + |
| 138 | + query = "util:system-property('os.name')"; |
| 139 | + result = xqueryService.execute(broker, query, null); |
| 140 | + |
| 141 | + if (shouldReturnEmptySequence) { |
| 142 | + assertTrue(result.isEmpty()); |
| 143 | + } else { |
| 144 | + assertFalse(result.isEmpty()); |
| 145 | + } |
| 146 | + } |
| 147 | + } |
| 148 | +} |
0 commit comments