Search in sources :

Example 26 with XPathQueryService

use of org.xmldb.api.modules.XPathQueryService in project exist by eXist-db.

the class BackupRestoreSecurityPrincipalsTest method restoreConflictingUsername.

/**
 * 1. With an empty database we create three
 *    users: 'frank', 'joe', and 'jack'.
 *
 * 2. We create a backup of the database which contains
 *    the three users from (1).
 *
 * 3. We destroy the database, restart the server,
 *    and start again with a clean database.
 *
 * 4. With an (again) empty database we create two
 *    users: 'frank', and 'jack'.
 *
 * 5. We then try and restore the database backup from (2), which
 *    contains the original 'frank', 'joe', and 'jack' users.
 *
 * frank will have the same username and user id in the current
 * database and the backup we are trying to restore.
 *
 * joe does not exist in the current database, but his user id
 * in the backup will collide with that of jack in the current database.
 *
 * jack will have a different user id in the backup when compared to the current
 * database, however he will have the same username.
 *
 * We want to make sure that after the restore, all three users are present
 * that they have distinct and expected user ids and that any resources
 * that were owned by them are still correctly owner by them (and not some other user).
 */
@Test
public void restoreConflictingUsername() throws PermissionDeniedException, EXistException, SAXException, ParserConfigurationException, IOException, URISyntaxException, XMLDBException, IllegalAccessException, ClassNotFoundException, InstantiationException {
    // creates a database with new users: 'frank(id=11)', 'joe(id=12)', and 'jack(id=13)'
    createInitialUsers(FRANK_USER, JOE_USER, JACK_USER);
    // create a backup of the database (which has the initial users)
    final Path backupFile = backupDatabase();
    // reset database to empty
    server.restart(true);
    // create new users: 'frank(id=11)' and 'jack(id=12)'
    createInitialUsers(FRANK_USER, JACK_USER);
    final String accountQuery = "declare namespace c = 'http://exist-db.org/Configuration';\n" + "for $account in //c:account\n" + "return\n" + "<user id='{$account/@id}' name='{$account/c:name}'/>";
    final XPathQueryService xqs = (XPathQueryService) server.getRoot().getService("XPathQueryService", "1.0");
    final SecurityManagerImpl sm = (SecurityManagerImpl) BrokerPool.getInstance().getSecurityManager();
    // check the current user accounts
    ResourceSet result = xqs.query(accountQuery);
    assertUser(RealmImpl.ADMIN_ACCOUNT_ID, SecurityManager.DBA_USER, ((XMLResource) result.getResource(0)).getContentAsDOM());
    assertUser(RealmImpl.GUEST_ACCOUNT_ID, SecurityManager.GUEST_USER, ((XMLResource) result.getResource(1)).getContentAsDOM());
    assertUser(SecurityManagerImpl.INITIAL_LAST_ACCOUNT_ID + 1, "frank", ((XMLResource) result.getResource(2)).getContentAsDOM());
    assertUser(SecurityManagerImpl.INITIAL_LAST_ACCOUNT_ID + 2, "jack", ((XMLResource) result.getResource(3)).getContentAsDOM());
    // check the last user id
    // last account id should be that of 'jack'
    assertEquals(SecurityManagerImpl.INITIAL_LAST_ACCOUNT_ID + 2, sm.getLastAccountId());
    // create a test collection and give everyone access
    final CollectionManagementService cms = (CollectionManagementService) server.getRoot().getService("CollectionManagementService", "1.0");
    final Collection test = cms.createCollection("test");
    final UserManagementService testUms = (UserManagementService) test.getService("UserManagementService", "1.0");
    testUms.chmod("rwxrwxrwx");
    // create and store a new document as 'frank'
    final Collection frankTest = DatabaseManager.getCollection("xmldb:exist:///db/test", FRANK_USER, FRANK_USER);
    final String FRANKS_DOCUMENT = "franks-document.xml";
    final Resource frankDoc = frankTest.createResource(FRANKS_DOCUMENT, XMLResource.RESOURCE_TYPE);
    frankDoc.setContent("<hello>frank</hello>");
    frankTest.storeResource(frankDoc);
    // create and store a new document as 'jack'
    final Collection jackTest = DatabaseManager.getCollection("xmldb:exist:///db/test", JACK_USER, JACK_USER);
    final String JACKS_DOCUMENT = "jacks-document.xml";
    final Resource jackDoc = jackTest.createResource(JACKS_DOCUMENT, XMLResource.RESOURCE_TYPE);
    jackDoc.setContent("<hello>jack</hello>");
    jackTest.storeResource(jackDoc);
    // restore the database backup
    final EXistRestoreService service = (EXistRestoreService) server.getRoot().getService("RestoreService", "1.0");
    service.restore(backupFile.normalize().toAbsolutePath().toString(), null, new NullRestoreServiceTaskListener(), false);
    // check the current user accounts after the restore
    result = xqs.query(accountQuery);
    assertUser(RealmImpl.ADMIN_ACCOUNT_ID, SecurityManager.DBA_USER, ((XMLResource) result.getResource(0)).getContentAsDOM());
    assertUser(RealmImpl.GUEST_ACCOUNT_ID, SecurityManager.GUEST_USER, ((XMLResource) result.getResource(1)).getContentAsDOM());
    assertUser(SecurityManagerImpl.INITIAL_LAST_ACCOUNT_ID + 1, FRANK_USER, ((XMLResource) result.getResource(2)).getContentAsDOM());
    assertUser(SecurityManagerImpl.INITIAL_LAST_ACCOUNT_ID + 2, JACK_USER, ((XMLResource) result.getResource(3)).getContentAsDOM());
    assertUser(SecurityManagerImpl.INITIAL_LAST_ACCOUNT_ID + 3, JOE_USER, ((XMLResource) result.getResource(4)).getContentAsDOM());
    // check the last user id after the restore
    // last account id should be that of 'joe'
    assertEquals(SecurityManagerImpl.INITIAL_LAST_ACCOUNT_ID + 3, sm.getLastAccountId());
    // check the owner of frank's document after restore
    final Resource fDoc = test.getResource(FRANKS_DOCUMENT);
    final Permission franksDocPermissions = testUms.getPermissions(fDoc);
    assertEquals(FRANK_USER, franksDocPermissions.getOwner().getName());
    // check the owner of jack's document after restore
    final Resource jDoc = test.getResource(JACKS_DOCUMENT);
    final Permission jacksDocPermissions = testUms.getPermissions(jDoc);
    assertEquals(JACK_USER, jacksDocPermissions.getOwner().getName());
}
Also used : Path(java.nio.file.Path) CollectionManagementService(org.xmldb.api.modules.CollectionManagementService) EXistRestoreService(org.exist.xmldb.EXistRestoreService) XPathQueryService(org.xmldb.api.modules.XPathQueryService) XMLResource(org.xmldb.api.modules.XMLResource) Resource(org.xmldb.api.base.Resource) NullRestoreServiceTaskListener(org.exist.xmldb.NullRestoreServiceTaskListener) Collection(org.xmldb.api.base.Collection) UserManagementService(org.exist.xmldb.UserManagementService) ResourceSet(org.xmldb.api.base.ResourceSet)

Example 27 with XPathQueryService

use of org.xmldb.api.modules.XPathQueryService in project exist by eXist-db.

the class AnnotationsTest method annotationInXMLNamespaceFails.

@Test(expected = XMLDBException.class)
public void annotationInXMLNamespaceFails() throws XMLDBException {
    final String TEST_VALUE_CONSTANT = "hello world";
    final String query = "declare namespace hello = 'http://www.w3.org/XML/1998/namespace';\n" + "declare\n" + "%hello:world\n" + "function local:hello() {\n" + "'" + TEST_VALUE_CONSTANT + "'\n" + "};\n" + "local:hello()";
    final XPathQueryService service = getQueryService();
    service.query(query);
}
Also used : XPathQueryService(org.xmldb.api.modules.XPathQueryService) Test(org.junit.Test)

Example 28 with XPathQueryService

use of org.xmldb.api.modules.XPathQueryService in project exist by eXist-db.

the class AnnotationsTest method annotationWithLiterals.

@Test
public void annotationWithLiterals() throws XMLDBException {
    final String TEST_VALUE_CONSTANT = "hello world";
    final String query = "declare namespace hello = 'http://world.com';\n" + "declare\n" + "%hello:world('a=b', 'b=c')\n" + "function local:hello() {\n" + "'" + TEST_VALUE_CONSTANT + "'\n" + "};\n" + "local:hello()";
    final XPathQueryService service = getQueryService();
    final ResourceSet result = service.query(query);
    assertEquals(1, result.getSize());
    Resource res = result.getIterator().nextResource();
    assertEquals(TEST_VALUE_CONSTANT, res.getContent());
}
Also used : XPathQueryService(org.xmldb.api.modules.XPathQueryService) Resource(org.xmldb.api.base.Resource) ResourceSet(org.xmldb.api.base.ResourceSet) Test(org.junit.Test)

Example 29 with XPathQueryService

use of org.xmldb.api.modules.XPathQueryService in project exist by eXist-db.

the class AnnotationsTest method annotationInXMLSchemaNamespaceFails.

@Test(expected = XMLDBException.class)
public void annotationInXMLSchemaNamespaceFails() throws XMLDBException {
    final String TEST_VALUE_CONSTANT = "hello world";
    final String query = "declare namespace hello = 'http://www.w3.org/2001/XMLSchema';\n" + "declare\n" + "%hello:world\n" + "function local:hello() {\n" + "'" + TEST_VALUE_CONSTANT + "'\n" + "};\n" + "local:hello()";
    final XPathQueryService service = getQueryService();
    service.query(query);
}
Also used : XPathQueryService(org.xmldb.api.modules.XPathQueryService) Test(org.junit.Test)

Example 30 with XPathQueryService

use of org.xmldb.api.modules.XPathQueryService in project exist by eXist-db.

the class AnnotationsTest method annotationInXPathFunctionsMathNamespaceFails.

@Test(expected = XMLDBException.class)
public void annotationInXPathFunctionsMathNamespaceFails() throws XMLDBException {
    final String TEST_VALUE_CONSTANT = "hello world";
    final String query = "declare namespace hello = 'http://www.w3.org/2005/xpath-functions/math';\n" + "declare\n" + "%hello:world\n" + "function local:hello() {\n" + "'" + TEST_VALUE_CONSTANT + "'\n" + "};\n" + "local:hello()";
    final XPathQueryService service = getQueryService();
    service.query(query);
}
Also used : XPathQueryService(org.xmldb.api.modules.XPathQueryService) Test(org.junit.Test)

Aggregations

XPathQueryService (org.xmldb.api.modules.XPathQueryService)148 ResourceSet (org.xmldb.api.base.ResourceSet)123 EXistXPathQueryService (org.exist.xmldb.EXistXPathQueryService)92 XMLResource (org.xmldb.api.modules.XMLResource)60 Collection (org.xmldb.api.base.Collection)31 Test (org.junit.Test)22 Resource (org.xmldb.api.base.Resource)18 XMLDBException (org.xmldb.api.base.XMLDBException)14 EXistResource (org.exist.xmldb.EXistResource)10 CollectionManagementService (org.xmldb.api.modules.CollectionManagementService)9 XUpdateQueryService (org.xmldb.api.modules.XUpdateQueryService)8 IndexQueryService (org.exist.xmldb.IndexQueryService)6 IOException (java.io.IOException)3 ResourceIterator (org.xmldb.api.base.ResourceIterator)3 BinaryResource (org.xmldb.api.modules.BinaryResource)3 MalformedURLException (java.net.MalformedURLException)2 Path (java.nio.file.Path)2 Node (org.w3c.dom.Node)2 HttpURLConnection (java.net.HttpURLConnection)1 URL (java.net.URL)1