use of org.exist.xquery.XQueryContext in project exist by eXist-db.
the class PermissionsFunctionModeConversionTest method modeToOctal_invalidMode.
@Test(expected = XPathException.class)
public void modeToOctal_invalidMode() throws XPathException {
final XQueryContext mckContext = EasyMock.createMock(XQueryContext.class);
final PermissionsFunction permissionsFunctions = new PermissionsFunction(mckContext, PermissionsFunction.FNS_MODE_TO_OCTAL);
Sequence[] args = { new StringValue("invalid") };
permissionsFunctions.eval(args, null);
}
use of org.exist.xquery.XQueryContext in project exist by eXist-db.
the class PermissionsFunctionModeConversionTest method octalToMode.
@Test
public void octalToMode() throws XPathException {
final XQueryContext mckContext = EasyMock.createMock(XQueryContext.class);
final PermissionsFunction permissionsFunctions = new PermissionsFunction(mckContext, PermissionsFunction.FNS_OCTAL_TO_MODE);
Sequence[] args = { new StringValue("0750") };
final Sequence result = permissionsFunctions.eval(args, null);
assertEquals(1, result.getItemCount());
assertEquals("rwxr-x---", result.itemAt(0).toString());
}
use of org.exist.xquery.XQueryContext in project exist by eXist-db.
the class PermissionsFunctionModeConversionTest method modeToOctal.
/**
* Test of eval method, of class PermissionsFunctions.
*/
@Test
public void modeToOctal() throws XPathException {
final XQueryContext mckContext = EasyMock.createMock(XQueryContext.class);
final PermissionsFunction permissionsFunctions = new PermissionsFunction(mckContext, PermissionsFunction.FNS_MODE_TO_OCTAL);
Sequence[] args = { new StringValue("rwxr-x---") };
final Sequence result = permissionsFunctions.eval(args, null);
assertEquals(1, result.getItemCount());
assertEquals("0750", result.itemAt(0).toString());
}
use of org.exist.xquery.XQueryContext in project exist by eXist-db.
the class IdFunctionTest method differingRealAndEffectiveUsers.
/**
* Test of eval method, of class IdFunction.
* when real and effective users are different
*/
@Test
public void differingRealAndEffectiveUsers() throws XPathException, XpathException {
final XQueryContext mckContext = createMockBuilder(XQueryContext.class).addMockedMethod("pushDocumentContext").addMockedMethod("getDocumentBuilder", new Class[0]).addMockedMethod("popDocumentContext").addMockedMethod("getRealUser").addMockedMethod("getEffectiveUser").createMock();
final Subject mckRealUser = EasyMock.createMock(Subject.class);
final String realUsername = "real";
mckContext.pushDocumentContext();
expectLastCall().once();
expect(mckContext.getDocumentBuilder()).andReturn(new MemTreeBuilder());
mckContext.popDocumentContext();
expectLastCall().once();
expect(mckContext.getRealUser()).andReturn(mckRealUser).times(2);
expect(mckRealUser.getName()).andReturn(realUsername);
expect(mckRealUser.getGroups()).andReturn(new String[] { "realGroup1", "realGroup2" });
expect(mckRealUser.getId()).andReturn(1);
final Subject mckEffectiveUser = EasyMock.createMock(Subject.class);
final String effectiveUsername = "effective";
expect(mckContext.getEffectiveUser()).andReturn(mckEffectiveUser).times(2);
expect(mckEffectiveUser.getId()).andReturn(2);
expect(mckEffectiveUser.getName()).andReturn(effectiveUsername);
expect(mckEffectiveUser.getGroups()).andReturn(new String[] { "effectiveGroup1", "effectiveGroup2" });
replay(mckEffectiveUser, mckRealUser, mckContext);
final IdFunction idFunctions = new IdFunction(mckContext, IdFunction.FNS_ID);
final Sequence result = idFunctions.eval(new Sequence[] { Sequence.EMPTY_SEQUENCE }, null);
assertEquals(1, result.getItemCount());
final XpathEngine xpathEngine = XMLUnit.newXpathEngine();
final Map<String, String> namespaces = new HashMap<>();
namespaces.put("sm", "http://exist-db.org/xquery/securitymanager");
xpathEngine.setNamespaceContext(new SimpleNamespaceContext(namespaces));
final DocumentImpl resultDoc = (DocumentImpl) result.itemAt(0);
final String actualRealUsername = xpathEngine.evaluate("/sm:id/sm:real/sm:username", resultDoc);
assertEquals(realUsername, actualRealUsername);
final String actualEffectiveUsername = xpathEngine.evaluate("/sm:id/sm:effective/sm:username", resultDoc);
assertEquals(effectiveUsername, actualEffectiveUsername);
verify(mckEffectiveUser, mckRealUser, mckContext);
}
use of org.exist.xquery.XQueryContext in project exist by eXist-db.
the class LocalXPathQueryService method execute.
private ResourceSet execute(final DBBroker broker, final Txn transaction, XmldbURI[] docs, final Sequence contextSet, final CompiledExpression expression, final String sortExpr) throws XMLDBException {
final long start = System.currentTimeMillis();
final CompiledXQuery expr = (CompiledXQuery) expression;
Sequence result = null;
final XQueryContext context = expr.getContext();
try {
context.setStaticallyKnownDocuments(docs);
if (lockedDocuments != null) {
context.setProtectedDocs(lockedDocuments);
}
setupContext(null, context);
final XQuery xquery = brokerPool.getXQueryService();
result = xquery.execute(broker, expr, contextSet, properties);
} catch (final Exception e) {
// need to catch all runtime exceptions here to be able to release locked documents
throw new XMLDBException(ErrorCodes.VENDOR_ERROR, e.getMessage(), e);
} finally {
/*
* Run the cleanup tasks, but don't close BinaryValues which
* are in the result set as the user has not yet accessed them.
*
* Final cleanup of those BinaryValues is done by the user
* calling EXistResource#close(), ResourceSet#clear() or CompiledExpression#reset().
*/
final Sequence resSeq = result;
context.runCleanupTasks(o -> {
if (resSeq != null && o instanceof BinaryValue) {
for (int i = 0; i < resSeq.getItemCount(); i++) {
if (resSeq.itemAt(i) == o) {
return false;
}
}
}
return true;
});
}
LOG.debug("query took {} ms.", System.currentTimeMillis() - start);
if (result != null) {
final Properties resourceSetProperties = new Properties(properties);
resourceSetProperties.setProperty(EXistOutputKeys.XDM_SERIALIZATION, "yes");
return new LocalResourceSet(user, brokerPool, collection, resourceSetProperties, result, sortExpr);
} else {
return null;
}
}
Aggregations