Repository: jena Updated Branches: refs/heads/master ba660daaa -> 0e135b58a
Changes to test JENA-861 Quick fix for same. Project: http://git-wip-us.apache.org/repos/asf/jena/repo Commit: http://git-wip-us.apache.org/repos/asf/jena/commit/02af44d5 Tree: http://git-wip-us.apache.org/repos/asf/jena/tree/02af44d5 Diff: http://git-wip-us.apache.org/repos/asf/jena/diff/02af44d5 Branch: refs/heads/master Commit: 02af44d5082493ff404c3d60f9b1b0b72e0ef258 Parents: d1b5e21 Author: Claude Warren <[email protected]> Authored: Sat Jan 24 11:16:12 2015 +0000 Committer: Claude Warren <[email protected]> Committed: Sat Jan 24 11:16:12 2015 +0000 ---------------------------------------------------------------------- .../jena/security/impl/SecuredItemInvoker.java | 5 +- .../security/model/impl/SecuredModelImpl.java | 5 +- .../graph/SecuredTDBGraphContractTests.java | 52 ++++ .../apache/jena/security/query/DataSetTest.java | 250 +++++++++++++++++++ .../jena/security/query/QueryEngineTest.java | 83 +++++- 5 files changed, 388 insertions(+), 7 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/jena/blob/02af44d5/jena-security/src/main/java/org/apache/jena/security/impl/SecuredItemInvoker.java ---------------------------------------------------------------------- diff --git a/jena-security/src/main/java/org/apache/jena/security/impl/SecuredItemInvoker.java b/jena-security/src/main/java/org/apache/jena/security/impl/SecuredItemInvoker.java index c066d88..9e9149d 100644 --- a/jena-security/src/main/java/org/apache/jena/security/impl/SecuredItemInvoker.java +++ b/jena-security/src/main/java/org/apache/jena/security/impl/SecuredItemInvoker.java @@ -39,6 +39,8 @@ public class SecuredItemInvoker implements InvocationHandler // package-private for ItemHolder use. /* package-private */final SecuredItem securedItem; + final Class<?> securedClass; + // populate the static fields. static { @@ -71,6 +73,7 @@ public class SecuredItemInvoker implements InvocationHandler final SecuredItem securedItem ) { this.securedItem = securedItem; + this.securedClass = securedClass; } @Override @@ -112,7 +115,7 @@ public class SecuredItemInvoker implements InvocationHandler SecuredItemImpl.incrementUse(); try { - return method.invoke(securedItem, args); + return m.invoke(securedItem, args); } finally { http://git-wip-us.apache.org/repos/asf/jena/blob/02af44d5/jena-security/src/main/java/org/apache/jena/security/model/impl/SecuredModelImpl.java ---------------------------------------------------------------------- diff --git a/jena-security/src/main/java/org/apache/jena/security/model/impl/SecuredModelImpl.java b/jena-security/src/main/java/org/apache/jena/security/model/impl/SecuredModelImpl.java index 9d02138..d061361 100644 --- a/jena-security/src/main/java/org/apache/jena/security/model/impl/SecuredModelImpl.java +++ b/jena-security/src/main/java/org/apache/jena/security/model/impl/SecuredModelImpl.java @@ -347,10 +347,9 @@ public class SecuredModelImpl extends SecuredItemImpl implements SecuredModel final String modelURI, final ItemHolder<Model, SecuredModel> holder ) { super(securityEvaluator, modelURI, holder); - this.graph = org.apache.jena.security.Factory.getInstance(this - .getSecurityEvaluator(), this.getModelIRI(), holder + this.graph = org.apache.jena.security.Factory.getInstance(securityEvaluator, modelURI, holder .getBaseItem().getGraph()); - this.holder = holder; + this.holder = holder; // FIXME -- this should just access the super holder. } private RDFNode asObject( Object o ) http://git-wip-us.apache.org/repos/asf/jena/blob/02af44d5/jena-security/src/test/java/org/apache/jena/security/contract/graph/SecuredTDBGraphContractTests.java ---------------------------------------------------------------------- diff --git a/jena-security/src/test/java/org/apache/jena/security/contract/graph/SecuredTDBGraphContractTests.java b/jena-security/src/test/java/org/apache/jena/security/contract/graph/SecuredTDBGraphContractTests.java new file mode 100644 index 0000000..c95f398 --- /dev/null +++ b/jena-security/src/test/java/org/apache/jena/security/contract/graph/SecuredTDBGraphContractTests.java @@ -0,0 +1,52 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License 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 org.apache.jena.security.contract.graph; + +import com.hp.hpl.jena.graph.Graph; +import com.hp.hpl.jena.graph.test.MetaTestGraph; +import com.hp.hpl.jena.tdb.TDBFactory; + +import org.apache.jena.security.MockSecurityEvaluator; +import org.apache.jena.security.SecurityEvaluator; + +public class SecuredTDBGraphContractTests extends MetaTestGraph +{ + + private final SecurityEvaluator eval; + + public SecuredTDBGraphContractTests( final Class<? extends Graph> graphClass, + final String name ) + { + super(graphClass, name); + eval = new MockSecurityEvaluator(true, true, true, true, true, true); + } + + public SecuredTDBGraphContractTests( final String name ) + { + super(name); + eval = new MockSecurityEvaluator(true, true, true, true, true, true); + } + + @Override + public Graph getGraph() + { + return org.apache.jena.security.Factory.getInstance(eval, getName(), + TDBFactory.createDatasetGraph().getDefaultGraph()); + } + +} http://git-wip-us.apache.org/repos/asf/jena/blob/02af44d5/jena-security/src/test/java/org/apache/jena/security/query/DataSetTest.java ---------------------------------------------------------------------- diff --git a/jena-security/src/test/java/org/apache/jena/security/query/DataSetTest.java b/jena-security/src/test/java/org/apache/jena/security/query/DataSetTest.java new file mode 100644 index 0000000..4cb30aa --- /dev/null +++ b/jena-security/src/test/java/org/apache/jena/security/query/DataSetTest.java @@ -0,0 +1,250 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License 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 org.apache.jena.security.query; + +import org.apache.jena.security.Factory; +import org.apache.jena.security.MockSecurityEvaluator; +import org.apache.jena.security.SecurityEvaluator.SecNode.Type; +import org.apache.jena.security.model.SecuredModel; +import org.junit.AfterClass; +import org.junit.Assert; +import org.junit.BeforeClass; +import org.junit.Test; + +import com.hp.hpl.jena.query.Dataset; +import com.hp.hpl.jena.query.DatasetFactory; +import com.hp.hpl.jena.query.QueryExecution; +import com.hp.hpl.jena.query.QueryExecutionFactory; +import com.hp.hpl.jena.query.QuerySolution; +import com.hp.hpl.jena.query.ResultSet; +import com.hp.hpl.jena.rdf.model.Model; +import com.hp.hpl.jena.sparql.core.DatasetGraph; +import com.hp.hpl.jena.tdb.TDB; +import com.hp.hpl.jena.tdb.TDBFactory; + +public class DataSetTest { + private Dataset dataset; + private Model baseModel; + private MockSecurityEvaluator eval; + private SecuredModel dftModel; + + @BeforeClass + public static void setupFactory() + { + SecuredQueryEngineFactory.register(); + } + + @AfterClass + public static void teardownFactory() + { + SecuredQueryEngineFactory.unregister(); + } + + + public void setup() { + + DatasetGraph dsg = TDBFactory.createDatasetGraph() ; + + + dsg.getContext().set(TDB.symUnionDefaultGraph, true) ; + Dataset myDataset = DatasetFactory.create(dsg) ; + +// DatasetGraph dsg = DatasetGraphFactory.createMem() ; +// +// Dataset myDataset = TDBFactory.createDataset(); + baseModel = myDataset.getNamedModel( "http://example.com/baseModel"); + //baseModel = myDataset.getDefaultModel(); + baseModel = QueryEngineTest.populateModel( baseModel ); + + dftModel = Factory.getInstance(eval, + "http://example.com/securedModel", baseModel); + + dataset = DatasetFactory.createMem() ; + dataset.setDefaultModel(dftModel) ; + +// // dataset.addNamedModel( dftModel.getModelIRI(), dftModel); + + + + } + + @Test + public void testOpenQueryType() + { + eval = new MockSecurityEvaluator(true, true, + true, true, true, true); + + setup(); + + try + { + final String query = "prefix fn: <http://www.w3.org/2005/xpath-functions#> " + + " SELECT ?foo ?bar WHERE " + + " { ?foo a <http://example.com/class> ; " + + "?bar [] ." + + " } "; + final QueryExecution qexec = QueryExecutionFactory.create(query, + dataset); + try + { + final ResultSet results = qexec.execSelect(); + int count = 0; + for (; results.hasNext();) + { + count++; + final QuerySolution soln = results.nextSolution(); + } + Assert.assertEquals(8, count); + } + finally + { + qexec.close(); + } + } + finally + { + dataset.close(); + } + } + + @Test + public void testRestrictedQueryType() + { + eval = new MockSecurityEvaluator(true, true, + true, true, true, true) { + + @Override + public boolean evaluate( final Action action, + final SecNode graphIRI, final SecTriple triple ) + { + if (triple.getSubject().equals( + new SecNode(Type.URI, "http://example.com/resource/1"))) + { + return false; + } + return super.evaluate(action, graphIRI, triple); + } + }; + + setup(); + + try + { + final String query = "prefix fn: <http://www.w3.org/2005/xpath-functions#> " + + " SELECT ?foo ?bar WHERE " + + " { ?foo a <http://example.com/class> ; " + + "?bar [] ." + + " } "; + final QueryExecution qexec = QueryExecutionFactory.create(query, + dataset); + try + { + final ResultSet results = qexec.execSelect(); + int count = 0; + for (; results.hasNext();) + { + count++; + results.nextSolution(); + } + Assert.assertEquals(4, count); + } + finally + { + qexec.close(); + } + } + finally + { + dataset.close(); + } + } + + @Test + public void testSelectAllType() + { + eval = new MockSecurityEvaluator(true, true, + true, true, true, true) { + + @Override + public boolean evaluate( final Action action, + final SecNode graphIRI, final SecTriple triple ) + { + if (triple.getSubject().equals( + new SecNode(Type.URI, "http://example.com/resource/1"))) + { + return false; + } + return super.evaluate(action, graphIRI, triple); + } + }; + + setup(); + + try + { + String query = "SELECT ?s ?p ?o WHERE " + + " { ?s ?p ?o } "; + QueryExecution qexec = QueryExecutionFactory.create(query, + dataset); + try + { + final ResultSet results = qexec.execSelect(); + int count = 0; + for (; results.hasNext();) + { + count++; + final QuerySolution soln = results.nextSolution(); + System.out.println( soln ); + } + // 2x 3 values + type triple + Assert.assertEquals(8, count); + } + finally + { + qexec.close(); + } + + query = "SELECT ?g ?s ?p ?o WHERE " + + " { GRAPH ?g {?s ?p ?o } }"; + qexec = QueryExecutionFactory.create(query, + dataset); + try + { + final ResultSet results = qexec.execSelect(); + int count = 0; + for (; results.hasNext();) + { + count++; + final QuerySolution soln = results.nextSolution(); + System.out.println( soln ); + } + // 2x 3 values + type triple + // all are in the base graph so no named graphs + Assert.assertEquals(0, count); + } + finally + { + qexec.close(); + } + } + finally + { + dataset.close(); + } + } +} http://git-wip-us.apache.org/repos/asf/jena/blob/02af44d5/jena-security/src/test/java/org/apache/jena/security/query/QueryEngineTest.java ---------------------------------------------------------------------- diff --git a/jena-security/src/test/java/org/apache/jena/security/query/QueryEngineTest.java b/jena-security/src/test/java/org/apache/jena/security/query/QueryEngineTest.java index 0b783c7..b332bc7 100644 --- a/jena-security/src/test/java/org/apache/jena/security/query/QueryEngineTest.java +++ b/jena-security/src/test/java/org/apache/jena/security/query/QueryEngineTest.java @@ -63,10 +63,9 @@ public class QueryEngineTest } - @Before - public void setUp() + public static Model populateModel(Model baseModel) { - baseModel = ModelFactory.createDefaultModel(); + Resource r = ResourceFactory .createResource("http://example.com/resource/1"); final Resource o = ResourceFactory @@ -105,6 +104,13 @@ public class QueryEngineTest baseModel.add(r, ResourceFactory .createProperty("http://example.com/property/_3"), ResourceFactory.createTypedLiteral(9.42)); + return baseModel; + } + + @Before + public void setUp() + { + baseModel = populateModel( ModelFactory.createDefaultModel()); } @After @@ -202,4 +208,75 @@ public class QueryEngineTest } } + @Test + public void testSelectAllType() + { + final SecurityEvaluator eval = new MockSecurityEvaluator(true, true, + true, true, true, true) { + + @Override + public boolean evaluate( final Action action, + final SecNode graphIRI, final SecTriple triple ) + { + if (triple.getSubject().equals( + new SecNode(Type.URI, "http://example.com/resource/1"))) + { + return false; + } + return super.evaluate(action, graphIRI, triple); + } + }; + final SecuredModel model = Factory.getInstance(eval, + "http://example.com/securedModel", baseModel); + try + { + String query = "SELECT ?s ?p ?o WHERE " + + " { ?s ?p ?o } "; + QueryExecution qexec = QueryExecutionFactory.create(query, + model); + try + { + final ResultSet results = qexec.execSelect(); + int count = 0; + for (; results.hasNext();) + { + count++; + final QuerySolution soln = results.nextSolution(); + System.out.println( soln ); + } + // 2x 3 values + type triple + Assert.assertEquals(8, count); + } + finally + { + qexec.close(); + } + + query = "SELECT ?s ?p ?o WHERE " + + " { GRAPH ?g {?s ?p ?o } }"; + qexec = QueryExecutionFactory.create(query, + model); + try + { + final ResultSet results = qexec.execSelect(); + int count = 0; + for (; results.hasNext();) + { + count++; + final QuerySolution soln = results.nextSolution(); + System.out.println( soln ); + } + // 2x 3 values + type triple + Assert.assertEquals(8, count); + } + finally + { + qexec.close(); + } + } + finally + { + model.close(); + } + } }
