http://git-wip-us.apache.org/repos/asf/jena/blob/c4b0113d/jena-permissions/src/test/java/org/apache/jena/permissions/model/SecuredPropertyTest.java ---------------------------------------------------------------------- diff --git a/jena-permissions/src/test/java/org/apache/jena/permissions/model/SecuredPropertyTest.java b/jena-permissions/src/test/java/org/apache/jena/permissions/model/SecuredPropertyTest.java new file mode 100644 index 0000000..affa7df --- /dev/null +++ b/jena-permissions/src/test/java/org/apache/jena/permissions/model/SecuredPropertyTest.java @@ -0,0 +1,79 @@ +/* + * 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.permissions.model; + +import org.apache.jena.permissions.AccessDeniedException; +import org.apache.jena.permissions.MockSecurityEvaluator; +import org.apache.jena.permissions.SecurityEvaluatorParameters; +import org.apache.jena.permissions.SecurityEvaluator.Action; +import org.apache.jena.permissions.model.SecuredProperty; +import org.apache.jena.permissions.model.impl.SecuredPropertyImpl; +import org.apache.jena.rdf.model.Property ; +import org.apache.jena.rdf.model.ResourceFactory ; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; + +@RunWith( value = SecurityEvaluatorParameters.class ) +public class SecuredPropertyTest extends SecuredResourceTest +{ + + public SecuredPropertyTest( final MockSecurityEvaluator securityEvaluator ) + { + super(securityEvaluator); + } + + private SecuredProperty getSecuredProperty() + { + return (SecuredProperty) getSecuredRDFNode(); + } + + @Override + @Before + public void setup() + { + super.setup(); + final Property p = ResourceFactory + .createProperty("http://example.com/testProperty"); + setSecuredRDFNode(SecuredPropertyImpl.getInstance(securedModel, p), p); + } + + @Test + public void testGetOrdinal() + { + try + { + getSecuredProperty().getOrdinal(); + if (!securityEvaluator.evaluate(Action.Read)) + { + Assert.fail("Should have thrown AccessDenied Exception"); + } + } + catch (final AccessDeniedException e) + { + if (securityEvaluator.evaluate(Action.Read)) + { + Assert.fail(String + .format("Should not have thrown AccessDenied Exception: %s - %s", + e, e.getTriple())); + } + } + } + +}
http://git-wip-us.apache.org/repos/asf/jena/blob/c4b0113d/jena-permissions/src/test/java/org/apache/jena/permissions/model/SecuredRDFListTest.java ---------------------------------------------------------------------- diff --git a/jena-permissions/src/test/java/org/apache/jena/permissions/model/SecuredRDFListTest.java b/jena-permissions/src/test/java/org/apache/jena/permissions/model/SecuredRDFListTest.java new file mode 100644 index 0000000..b83fd41 --- /dev/null +++ b/jena-permissions/src/test/java/org/apache/jena/permissions/model/SecuredRDFListTest.java @@ -0,0 +1,970 @@ +/* + * 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.permissions.model; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; +import java.util.Set; + +import org.apache.jena.permissions.AccessDeniedException; +import org.apache.jena.permissions.MockSecurityEvaluator; +import org.apache.jena.permissions.SecurityEvaluator; +import org.apache.jena.permissions.SecurityEvaluatorParameters; +import org.apache.jena.permissions.SecurityEvaluator.Action; +import org.apache.jena.permissions.model.SecuredRDFList; +import org.apache.jena.permissions.model.impl.SecuredRDFListImpl; +import org.apache.jena.permissions.utils.RDFListIterator; +import org.apache.jena.permissions.utils.RDFListSecFilter; +import org.apache.jena.rdf.model.* ; +import org.apache.jena.rdf.model.RDFList.ApplyFn ; +import org.apache.jena.rdf.model.RDFList.ReduceFn ; +import org.apache.jena.util.iterator.WrappedIterator ; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; + +@RunWith( value = SecurityEvaluatorParameters.class ) +public class SecuredRDFListTest extends SecuredResourceTest +{ + private RDFList baseList; + + public SecuredRDFListTest( final MockSecurityEvaluator securityEvaluator ) + { + super(securityEvaluator); + } + + private int count( final Action action ) + { + final Iterator<RDFList> iter = new RDFListIterator( + (RDFList) getBaseRDFNode()); + return WrappedIterator.create(iter) + .filterKeep(new RDFListSecFilter<RDFList>(getSecuredRDFList(), action)) + .toList().size(); + } + + private int count( final Set<Action> action ) + { + final Iterator<RDFList> iter = new RDFListIterator( + (RDFList) getBaseRDFNode()); + return WrappedIterator.create(iter) + .filterKeep(new RDFListSecFilter<RDFList>(getSecuredRDFList(), action)) + .toList().size(); + } + + private SecuredRDFList getSecuredRDFList() + { + return (SecuredRDFList) getSecuredRDFNode(); + } + + @Override + @Before + public void setup() + { + super.setup(); + final RDFNode[] listElements = { + ResourceFactory.createResource("http://example.com/ListNode1"), + ResourceFactory.createResource("http://example.com/ListNode2"), + ResourceFactory.createResource("http://example.com/ListNode3"), + ResourceFactory.createResource("http://example.com/ListNode4") }; + baseList = baseModel.createList(listElements); + setSecuredRDFNode(SecuredRDFListImpl.getInstance(securedModel, baseList), baseList); + } + + @Test + public void testAdd() + { + final Set<Action> perms = SecurityEvaluator.Util.asSet(new Action[] { + Action.Update, Action.Create }); + try + { + getSecuredRDFList().add(baseModel.createResource()); + if (!securityEvaluator.evaluate(perms)) + { + Assert.fail("Should have thrown AccessDenied Exception"); + } + } + catch (final AccessDeniedException e) + { + if (securityEvaluator.evaluate(perms)) + { + Assert.fail(String + .format("Should not have thrown AccessDenied Exception: %s - %s", + e, e.getTriple())); + } + } + + } + + @Test + public void testAppendNodeIterator() + { + final Set<Action> perms = SecurityEvaluator.Util.asSet(new Action[] { + Action.Update, Action.Create }); + try + { + getSecuredRDFList().append(baseModel.listObjects()); + if (!securityEvaluator.evaluate(perms)) + { + Assert.fail("Should have thrown AccessDenied Exception"); + } + } + catch (final AccessDeniedException e) + { + if (securityEvaluator.evaluate(perms)) + { + Assert.fail(String + .format("Should not have thrown AccessDenied Exception: %s - %s", + e, e.getTriple())); + } + } + } + + @Test + public void testAppendRDFList() + { + final Set<Action> perms = SecurityEvaluator.Util.asSet(new Action[] { + Action.Update, Action.Create }); + + try { + getSecuredRDFList().append(baseModel.createList()); + if (!securityEvaluator.evaluate(Action.Update)) + { + Assert.fail("Should have thrown AccessDenied Exception"); + } + if (!securityEvaluator.evaluate(Action.Create) && (baseList.size()>0 && securityEvaluator.evaluate(Action.Read) )) + { + Assert.fail("Should have thrown AccessDenied Exception"); + } + + } + catch (final AccessDeniedException e) + { + if (securityEvaluator.evaluate(perms)) + { + Assert.fail(String + .format("Should not have thrown AccessDenied Exception: %s - %s", + e, e.getTriple())); + } + } + } + + @Test + public void testApply() + { + + final ApplyFn fn = new ApplyFn() { + + @Override + public void apply( final RDFNode node ) + { + // do nothing + } + }; + + try + { + getSecuredRDFList().apply(fn); + if (!securityEvaluator.evaluate(Action.Read)) + { + Assert.fail("Should have thrown AccessDenied Exception"); + } + } + catch (final AccessDeniedException e) + { + if (securityEvaluator.evaluate(Action.Read)) + { + Assert.fail(String + .format("Should not have thrown AccessDenied Exception: %s - %s", + e, e.getTriple())); + } + } + + final Set<Action> perms = SecurityEvaluator.Util.asSet(new Action[] { + Action.Update, Action.Read }); + try + { + getSecuredRDFList().apply(perms, fn); + if (!securityEvaluator.evaluate(Action.Read)) + { + Assert.fail("Should have thrown AccessDenied Exception"); + } + } + catch (final AccessDeniedException e) + { + if (securityEvaluator.evaluate(Action.Read)) + { + Assert.fail(String + .format("Should not have thrown AccessDenied Exception: %s - %s", + e, e.getTriple())); + } + } + } + + @Test + public void testAsJaveList() + { + try + { + getSecuredRDFList().asJavaList(); + if (!securityEvaluator.evaluate(Action.Read)) + { + Assert.fail("Should have thrown AccessDenied Exception"); + } + } + catch (final AccessDeniedException e) + { + if (securityEvaluator.evaluate(Action.Read)) + { + Assert.fail(String + .format("Should not have thrown AccessDenied Exception: %s - %s", + e, e.getTriple())); + } + } + + } + + @Test + public void testConcatenate() + { + final Set<Action> perms = SecurityEvaluator.Util.asSet(new Action[] { + Action.Update, Action.Create }); + try + { + getSecuredRDFList().concatenate(baseModel.listObjects()); + if (!securityEvaluator.evaluate(perms)) + { + Assert.fail("Should have thrown AccessDenied Exception"); + } + } + catch (final AccessDeniedException e) + { + if (securityEvaluator.evaluate(perms)) + { + Assert.fail(String + .format("Should not have thrown AccessDenied Exception: %s - %s", + e, e.getTriple())); + } + } + + try + { + final List<Resource> lst = new ArrayList<Resource>(); + lst.add(ResourceFactory + .createResource("http://example.com/dummyList")); + getSecuredRDFList().concatenate( + baseModel.createList(lst.iterator())); + if (!securityEvaluator.evaluate(perms)) + { + Assert.fail("Should have thrown AccessDenied Exception"); + } + } + catch (final AccessDeniedException e) + { + if (securityEvaluator.evaluate(perms)) + { + Assert.fail(String + .format("Should not have thrown AccessDenied Exception: %s - %s", + e, e.getTriple())); + } + } + } + + @Test + public void testCons() + { + final Set<Action> perms = SecurityEvaluator.Util.asSet(new Action[] { + Action.Update, Action.Create }); + try + { + getSecuredRDFList().cons(SecuredRDFNodeTest.s); + if (!securityEvaluator.evaluate(perms)) + { + Assert.fail("Should have thrown AccessDenied Exception"); + } + } + catch (final AccessDeniedException e) + { + if (securityEvaluator.evaluate(perms)) + { + Assert.fail(String + .format("Should not have thrown AccessDenied Exception: %s - %s", + e, e.getTriple())); + } + } + } + + @Test + public void testContains() + { + try + { + getSecuredRDFList().contains(SecuredRDFNodeTest.s); + if (!securityEvaluator.evaluate(Action.Read)) + { + Assert.fail("Should have thrown AccessDenied Exception"); + } + } + catch (final AccessDeniedException e) + { + if (securityEvaluator.evaluate(Action.Read)) + { + Assert.fail(String + .format("Should not have thrown AccessDenied Exception: %s - %s", + e, e.getTriple())); + } + } + } + + @Test + public void testCopy() + { + final Set<Action> perms = SecurityEvaluator.Util.asSet(new Action[] { + Action.Read, Action.Update, Action.Create }); + try + { + getSecuredRDFList().copy(); + } + catch (final AccessDeniedException e) + { + if (securityEvaluator.evaluate(perms) ) + { + Assert.fail(String + .format("Should not have thrown AccessDenied Exception: %s - %s", + e, e.getTriple())); + } + } + } + + @Test + public void testGet() + { + try + { + getSecuredRDFList().get(0); + if (!securityEvaluator.evaluate(Action.Read)) + { + Assert.fail("Should have thrown AccessDenied Exception"); + } + } + catch (final AccessDeniedException e) + { + if (securityEvaluator.evaluate(Action.Read)) + { + Assert.fail(String + .format("Should not have thrown AccessDenied Exception: %s - %s", + e, e.getTriple())); + } + } + catch (final ListIndexException e) + { + if (((RDFList) getBaseRDFNode()).size() < 0) + { + // acceptable exception + } + else + { + throw e; + } + } + } + + @Test + public void testGetHead() + { + try + { + getSecuredRDFList().getHead(); + if (!securityEvaluator.evaluate(Action.Read)) + { + Assert.fail("Should have thrown AccessDenied Exception"); + } + } + catch (final AccessDeniedException e) + { + if (securityEvaluator.evaluate(Action.Read)) + { + Assert.fail(String + .format("Should not have thrown AccessDenied Exception: %s - %s", + e, e.getTriple())); + } + } + catch (final ListIndexException e) + { + if (((RDFList) getBaseRDFNode()).size() == 0) + { + // acceptable exception + } + else + { + throw e; + } + } + } + + @Test + public void testGetTail() + { + try + { + getSecuredRDFList().getTail(); + if (!securityEvaluator.evaluate(Action.Read)) + { + Assert.fail("Should have thrown AccessDenied Exception"); + } + } + catch (final AccessDeniedException e) + { + if (securityEvaluator.evaluate(Action.Read)) + { + Assert.fail(String + .format("Should not have thrown AccessDenied Exception: %s - %s", + e, e.getTriple())); + } + } + catch (final ListIndexException e) + { + if (((RDFList) getBaseRDFNode()).size() == 0) + { + // acceptable exception + } + else + { + throw e; + } + } + } + + @Test + public void testGetValidityErrorMessage() + { + try + { + getSecuredRDFList().getValidityErrorMessage(); + if (!securityEvaluator.evaluate(Action.Read)) + { + Assert.fail("Should have thrown AccessDenied Exception"); + } + } + catch (final AccessDeniedException e) + { + if (securityEvaluator.evaluate(Action.Read)) + { + Assert.fail(String + .format("Should not have thrown AccessDenied Exception: %s - %s", + e, e.getTriple())); + } + } + } + + @Test + public void testIndexOf() + { + try + { + getSecuredRDFList().indexOf(SecuredRDFNodeTest.s); + if (!securityEvaluator.evaluate(Action.Read)) + { + Assert.fail("Should have thrown AccessDenied Exception"); + } + } + catch (final AccessDeniedException e) + { + if (securityEvaluator.evaluate(Action.Read)) + { + Assert.fail(String + .format("Should not have thrown AccessDenied Exception: %s - %s", + e, e.getTriple())); + } + } + catch (final ListIndexException e) + { + if (((RDFList) getBaseRDFNode()).size() == 0) + { + // acceptable exception + } + else + { + throw e; + } + } + + try + { + getSecuredRDFList().indexOf(SecuredRDFNodeTest.s, 1); + if (!securityEvaluator.evaluate(Action.Read)) + { + Assert.fail("Should have thrown AccessDenied Exception"); + } + } + catch (final AccessDeniedException e) + { + if (securityEvaluator.evaluate(Action.Read)) + { + Assert.fail(String + .format("Should not have thrown AccessDenied Exception: %s - %s", + e, e.getTriple())); + } + } + catch (final ListIndexException e) + { + if (((RDFList) getBaseRDFNode()).size() <= 0) + + { + // acceptable exception + } + else + { + throw e; + } + } + } + + @Test + public void testIsEmpty() + { + try + { + getSecuredRDFList().isEmpty(); + if (!securityEvaluator.evaluate(Action.Read)) + { + Assert.fail("Should have thrown AccessDenied Exception"); + } + } + catch (final AccessDeniedException e) + { + if (securityEvaluator.evaluate(Action.Read)) + { + Assert.fail(String + .format("Should not have thrown AccessDenied Exception: %s - %s", + e, e.getTriple())); + } + } + } + + @Test + public void testIterator() + { + try + { + getSecuredRDFList().iterator(); + if (!securityEvaluator.evaluate(Action.Read)) + { + Assert.fail("Should have thrown AccessDenied Exception"); + } + } + catch (final AccessDeniedException e) + { + if (securityEvaluator.evaluate(Action.Read)) + { + Assert.fail(String + .format("Should not have thrown AccessDenied Exception: %s - %s", + e, e.getTriple())); + } + } + + final Set<Action> perms = SecurityEvaluator.Util.asSet(new Action[] { + Action.Update, Action.Create }); + + try + { + getSecuredRDFList().iterator(perms); + if (!securityEvaluator.evaluate(Action.Read)) + { + Assert.fail("Should have thrown AccessDenied Exception"); + } + } + catch (final AccessDeniedException e) + { + if (securityEvaluator.evaluate(Action.Read)) + { + Assert.fail(String + .format("Should not have thrown AccessDenied Exception: %s - %s", + e, e.getTriple())); + } + } + } + + @Test + public void testReduce() + { + final ReduceFn fn = new ReduceFn() { + + @Override + public Object reduce( final RDFNode node, final Object accumulator ) + { + return accumulator; + } + }; + + try + { + getSecuredRDFList().reduce(fn, "Hello"); + if (!securityEvaluator.evaluate(Action.Read)) + { + Assert.fail("Should have thrown AccessDenied Exception"); + } + } + catch (final AccessDeniedException e) + { + if (securityEvaluator.evaluate(Action.Read)) + { + Assert.fail(String + .format("Should not have thrown AccessDenied Exception: %s - %s", + e, e.getTriple())); + } + } + + final Set<Action> perms = SecurityEvaluator.Util.asSet(new Action[] { + Action.Update, Action.Create }); + + try + { + getSecuredRDFList().reduce(perms, fn, "Hello"); + if (!securityEvaluator.evaluate(Action.Read)) + { + Assert.fail("Should have thrown AccessDenied Exception"); + } + } + catch (final AccessDeniedException e) + { + if (securityEvaluator.evaluate(Action.Read)) + { + Assert.fail(String + .format("Should not have thrown AccessDenied Exception: %s - %s", + e, e.getTriple())); + } + } + } + + @Test + public void testRemove() + { + final Set<Action> perms = SecurityEvaluator.Util.asSet(new Action[] { + Action.Update, Action.Delete }); + + try + { + final int count = count(Action.Delete); + getSecuredRDFList().remove(SecuredRDFNodeTest.s); + if (!securityEvaluator.evaluate(Action.Update) + || ((count > 0) && !securityEvaluator + .evaluate(Action.Delete))) + { + Assert.fail("Should have thrown AccessDenied Exception"); + } + } + catch (final AccessDeniedException e) + { + if (securityEvaluator.evaluate(perms)) + { + Assert.fail(String + .format("Should not have thrown AccessDenied Exception: %s - %s", + e, e.getTriple())); + } + } + } + + @SuppressWarnings("deprecation") + @Override + @Test + public void testRemoveAll() + { + final Set<Action> perms = SecurityEvaluator.Util.asSet(new Action[] { + Action.Update, Action.Delete }); + + try + { + final int count = count(SecurityEvaluator.Util.asSet(new Action[] { + Action.Delete, Action.Read })); + getSecuredRDFList().removeAll(); + if (!securityEvaluator.evaluate(Action.Update) + || ((count > 0) && !securityEvaluator + .evaluate(Action.Delete))) + { + Assert.fail("Should have thrown AccessDenied Exception"); + } + } + catch (final AccessDeniedException e) + { + if (securityEvaluator.evaluate(perms)) + { + Assert.fail(String + .format("Should not have thrown AccessDenied Exception: %s - %s", + e, e.getTriple())); + } + } + catch (final EmptyListException e) + { + if (count(Action.Read) == 0) + { + // expected. + } + else + { + throw e; + } + } + } + + @Test + public void testRemoveHead() + { + final Set<Action> perms = SecurityEvaluator.Util.asSet(new Action[] { + Action.Update, Action.Delete }); + + try + { + getSecuredRDFList().removeHead(); + if (!securityEvaluator.evaluate(perms)) + { + Assert.fail("Should have thrown AccessDenied Exception"); + } + } + catch (final AccessDeniedException e) + { + if (securityEvaluator.evaluate(perms)) + { + Assert.fail(String + .format("Should not have thrown AccessDenied Exception: %s - %s", + e, e.getTriple())); + } + } + catch (final EmptyListException e) + { + if (count(Action.Read) == 0) + { + // expected. + } + else + { + throw e; + } + } + } + + @Test + public void testRemoveList() + { + final Set<Action> perms = SecurityEvaluator.Util.asSet(new Action[] { + Action.Update, Action.Delete }); + + try + { + final int count = count(Action.Delete); + getSecuredRDFList().removeList(); + if (!securityEvaluator.evaluate(Action.Update) + || ((count > 0) && !securityEvaluator + .evaluate(Action.Delete))) + { + Assert.fail("Should have thrown AccessDenied Exception"); + } + } + catch (final AccessDeniedException e) + { + if (securityEvaluator.evaluate(perms)) + { + Assert.fail(String + .format("Should not have thrown AccessDenied Exception: %s - %s", + e, e.getTriple())); + } + } + } + + @Test + public void testReplace() + { + try + { + getSecuredRDFList().replace(1, SecuredRDFNodeTest.s); + if (!securityEvaluator.evaluate(Action.Update)) + { + Assert.fail("Should have thrown AccessDenied Exception"); + } + } + catch (final AccessDeniedException e) + { + if (securityEvaluator.evaluate(Action.Update)) + { + Assert.fail(String + .format("Should not have thrown AccessDenied Exception: %s - %s", + e, e.getTriple())); + } + } + catch (final ListIndexException e) + { + if (count(Action.Read) == 0) + { + // expected. + } + else + { + throw e; + } + } + } + + @Test + public void testSameListAs() + { + try + { + getSecuredRDFList().sameListAs(baseModel.createList()); + if (!securityEvaluator.evaluate(Action.Read)) + { + Assert.fail("Should have thrown AccessDenied Exception"); + } + } + catch (final AccessDeniedException e) + { + if (securityEvaluator.evaluate(Action.Read)) + { + Assert.fail(String + .format("Should not have thrown AccessDenied Exception: %s - %s", + e, e.getTriple())); + } + } + } + + @Test + public void testSetHead() + { + + try + { + getSecuredRDFList().setHead(SecuredRDFNodeTest.s); + if (!securityEvaluator.evaluate(Action.Update)) + { + Assert.fail("Should have thrown AccessDenied Exception"); + } + } + catch (final AccessDeniedException e) + { + if (securityEvaluator.evaluate(Action.Update)) + { + Assert.fail(String + .format("Should not have thrown AccessDenied Exception: %s - %s", + e, e.getTriple())); + } + } + catch (final EmptyListException e) + { + if (count(Action.Read) == 0) + { + // expected. + } + else + { + throw e; + } + } + } + + @Test + public void testSetStrict() + { + try + { + getSecuredRDFList().setStrict(true); + if (!securityEvaluator.evaluate(Action.Update)) + { + Assert.fail("Should have thrown AccessDenied Exception"); + } + } + catch (final AccessDeniedException e) + { + if (securityEvaluator.evaluate(Action.Update)) + { + Assert.fail(String + .format("Should not have thrown AccessDenied Exception: %s - %s", + e, e.getTriple())); + } + } + } + + @Test + public void testSize() + { + try + { + getSecuredRDFList().size(); + if (!securityEvaluator.evaluate(Action.Read)) + { + Assert.fail("Should have thrown AccessDenied Exception"); + } + } + catch (final AccessDeniedException e) + { + if (securityEvaluator.evaluate(Action.Read)) + { + Assert.fail(String + .format("Should not have thrown AccessDenied Exception: %s - %s", + e, e.getTriple())); + } + } + } + + @Test + public void testValid() + { + try + { + getSecuredRDFList().isValid(); + if (!securityEvaluator.evaluate(Action.Read)) + { + Assert.fail("Should have thrown AccessDenied Exception"); + } + } + catch (final AccessDeniedException e) + { + if (securityEvaluator.evaluate(Action.Read)) + { + Assert.fail(String + .format("Should not have thrown AccessDenied Exception: %s - %s", + e, e.getTriple())); + } + } + } + + @Test + public void testWith() + { + final Set<Action> perms = SecurityEvaluator.Util.asSet(new Action[] { + Action.Update, Action.Create }); + + try + { + getSecuredRDFList().with(SecuredRDFNodeTest.s); + if (!securityEvaluator.evaluate(perms)) + { + Assert.fail("Should have thrown AccessDenied Exception"); + } + } + catch (final AccessDeniedException e) + { + if (securityEvaluator.evaluate(perms)) + { + Assert.fail(String + .format("Should not have thrown AccessDenied Exception: %s - %s", + e, e.getTriple())); + } + } + } + +} http://git-wip-us.apache.org/repos/asf/jena/blob/c4b0113d/jena-permissions/src/test/java/org/apache/jena/permissions/model/SecuredRDFNodeTest.java ---------------------------------------------------------------------- diff --git a/jena-permissions/src/test/java/org/apache/jena/permissions/model/SecuredRDFNodeTest.java b/jena-permissions/src/test/java/org/apache/jena/permissions/model/SecuredRDFNodeTest.java new file mode 100644 index 0000000..3c1f3db --- /dev/null +++ b/jena-permissions/src/test/java/org/apache/jena/permissions/model/SecuredRDFNodeTest.java @@ -0,0 +1,209 @@ +/* + * 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.permissions.model; + +import org.apache.jena.permissions.AccessDeniedException; +import org.apache.jena.permissions.Factory; +import org.apache.jena.permissions.MockSecurityEvaluator; +import org.apache.jena.permissions.SecurityEvaluatorParameters; +import org.apache.jena.permissions.SecurityEvaluator.Action; +import org.apache.jena.permissions.model.SecuredModel; +import org.apache.jena.permissions.model.SecuredRDFNode; +import org.apache.jena.permissions.model.impl.SecuredRDFNodeImpl; +import org.apache.jena.rdf.model.* ; +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; + +@RunWith( value = SecurityEvaluatorParameters.class ) +public class SecuredRDFNodeTest +{ + protected final MockSecurityEvaluator securityEvaluator; + protected Model baseModel; + protected SecuredModel securedModel; + private SecuredRDFNode securedRDFNode; + private RDFNode baseRDFNode; + + public static Resource s = ResourceFactory + .createResource("http://example.com/graph/s"); + public static Property p = ResourceFactory + .createProperty("http://example.com/graph/p"); + public static Property p2 = ResourceFactory + .createProperty("http://example.com/graph/p2"); + public static Resource o = ResourceFactory + .createResource("http://example.com/graph/o"); + + public SecuredRDFNodeTest( final MockSecurityEvaluator securityEvaluator ) + { + this.securityEvaluator = securityEvaluator; + } + + protected Model createModel() + { + return ModelFactory.createDefaultModel(); + } + + protected RDFNode getBaseRDFNode() + { + return baseRDFNode; + } + + protected SecuredRDFNode getSecuredRDFNode() + { + return securedRDFNode; + } + + protected void setSecuredRDFNode( final SecuredRDFNode securedRDFNode, + final RDFNode baseRDFNode ) + { + this.securedRDFNode = securedRDFNode; + this.baseRDFNode = baseRDFNode; + } + + @Before + public void setup() + { + baseModel = createModel(); + baseModel.removeAll(); + baseModel.add(SecuredRDFNodeTest.s, SecuredRDFNodeTest.p, + SecuredRDFNodeTest.o); + baseModel.add(SecuredRDFNodeTest.s, SecuredRDFNodeTest.p2, "yeehaw"); + securedModel = Factory.getInstance(securityEvaluator, + "http://example.com/securedGraph", baseModel); + securedRDFNode = SecuredRDFNodeImpl.getInstance( + securedModel, + baseModel.listObjectsOfProperty(SecuredRDFNodeTest.s, + SecuredRDFNodeTest.p).next()); + } + + @After + public void teardown() + { + securedModel.close(); + securedModel = null; + } + + @Test + public void testAsNode() + { + try + { + securedRDFNode.asNode(); + if (!securityEvaluator.evaluate(Action.Read)) + { + Assert.fail("Should have thrown AccessDenied Exception"); + } + } + catch (final AccessDeniedException e) + { + if (securityEvaluator.evaluate(Action.Read)) + { + Assert.fail(String + .format("Should not have thrown AccessDenied Exception: %s - %s", + e, e.getTriple())); + } + } + } + + @Test + public void testCanAs() + { + try + { + securedRDFNode.canAs(Resource.class); + if (!securityEvaluator.evaluate(Action.Read)) + { + Assert.fail("Should have thrown AccessDenied Exception"); + } + } + catch (final AccessDeniedException e) + { + if (securityEvaluator.evaluate(Action.Read)) + { + Assert.fail(String + .format("Should not have thrown AccessDenied Exception: %s - %s", + e, e.getTriple())); + } + } + } + + @Test + public void testGetModel() + { + final Model m2 = securedRDFNode.getModel(); + Assert.assertTrue("Model should have been secured", + m2 instanceof SecuredModel); + } + + @Test + public void testInModel() + { + final Model m2 = ModelFactory.createDefaultModel(); + try + { + final RDFNode n2 = securedRDFNode.inModel(m2); + if (!securityEvaluator.evaluate(Action.Read)) + { + Assert.fail("Should have thrown AccessDenied Exception"); + } + Assert.assertFalse("RDFNode should not have been secured", + n2 instanceof SecuredRDFNode); + Assert.assertEquals("Wrong securedModel returned", n2.getModel(), + m2); + } + catch (final AccessDeniedException e) + { + if (securityEvaluator.evaluate(Action.Read)) + { + Assert.fail(String + .format("Should not have thrown AccessDenied Exception: %s - %s", + e, e.getTriple())); + } + } + + m2.removeAll(); + final SecuredModel m3 = Factory.getInstance(securityEvaluator, + "http://example.com/securedGraph2", m2); + + try + { + final RDFNode n2 = securedRDFNode.inModel(m3); + if (!securityEvaluator.evaluate(Action.Read)) + { + Assert.fail("Should have thrown AccessDenied Exception"); + } + Assert.assertTrue("RDFNode should have been secured", + n2 instanceof SecuredRDFNode); + Assert.assertEquals("Wrong securedModel returned", n2.getModel(), + m3); + } + catch (final AccessDeniedException e) + { + if (securityEvaluator.evaluate(Action.Read)) + { + Assert.fail(String + .format("Should not have thrown AccessDenied Exception: %s - %s", + e, e.getTriple())); + } + } + + } + +} http://git-wip-us.apache.org/repos/asf/jena/blob/c4b0113d/jena-permissions/src/test/java/org/apache/jena/permissions/model/SecuredReifiedStatementTest.java ---------------------------------------------------------------------- diff --git a/jena-permissions/src/test/java/org/apache/jena/permissions/model/SecuredReifiedStatementTest.java b/jena-permissions/src/test/java/org/apache/jena/permissions/model/SecuredReifiedStatementTest.java new file mode 100644 index 0000000..73055fe --- /dev/null +++ b/jena-permissions/src/test/java/org/apache/jena/permissions/model/SecuredReifiedStatementTest.java @@ -0,0 +1,84 @@ +/* + * 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.permissions.model; + +import org.apache.jena.permissions.AccessDeniedException; +import org.apache.jena.permissions.MockSecurityEvaluator; +import org.apache.jena.permissions.SecurityEvaluatorParameters; +import org.apache.jena.permissions.SecurityEvaluator.Action; +import org.apache.jena.permissions.model.SecuredReifiedStatement; +import org.apache.jena.permissions.model.impl.SecuredReifiedStatementImpl; +import org.apache.jena.rdf.model.ReifiedStatement ; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; + +@RunWith( value = SecurityEvaluatorParameters.class ) +public class SecuredReifiedStatementTest extends SecuredResourceTest +{ + + public SecuredReifiedStatementTest( + final MockSecurityEvaluator securityEvaluator ) + { + super(securityEvaluator); + } + + private SecuredReifiedStatement getSecuredReifiedStatement() + { + return (SecuredReifiedStatement) getSecuredRDFNode(); + } + + @Override + @Before + public void setup() + { + super.setup(); + final ReifiedStatement stmt = baseModel.listStatements().next() + .createReifiedStatement(); + setSecuredRDFNode( + SecuredReifiedStatementImpl.getInstance(securedModel, stmt), + stmt); + } + + /** + * @sec.graph Read + * @throws AccessDeniedException + */ + @Test + public void testGetStatement() + { + try + { + getSecuredReifiedStatement().getStatement(); + if (!securityEvaluator.evaluate(Action.Read)) + { + Assert.fail("Should have thrown AccessDenied Exception"); + } + } + catch (final AccessDeniedException e) + { + if (securityEvaluator.evaluate(Action.Read)) + { + Assert.fail(String + .format("Should not have thrown AccessDenied Exception: %s - %s", + e, e.getTriple())); + } + } + } +} http://git-wip-us.apache.org/repos/asf/jena/blob/c4b0113d/jena-permissions/src/test/java/org/apache/jena/permissions/model/SecuredResourceTest.java ---------------------------------------------------------------------- diff --git a/jena-permissions/src/test/java/org/apache/jena/permissions/model/SecuredResourceTest.java b/jena-permissions/src/test/java/org/apache/jena/permissions/model/SecuredResourceTest.java new file mode 100644 index 0000000..c893f4e --- /dev/null +++ b/jena-permissions/src/test/java/org/apache/jena/permissions/model/SecuredResourceTest.java @@ -0,0 +1,827 @@ +/* + * 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.permissions.model; + +import java.util.Set; + +import org.apache.jena.permissions.AccessDeniedException; +import org.apache.jena.permissions.MockSecurityEvaluator; +import org.apache.jena.permissions.SecurityEvaluator; +import org.apache.jena.permissions.SecurityEvaluatorParameters; +import org.apache.jena.permissions.SecurityEvaluator.Action; +import org.apache.jena.permissions.model.SecuredResource; +import org.apache.jena.permissions.model.impl.SecuredResourceImpl; +import org.apache.jena.rdf.model.Literal ; +import org.apache.jena.rdf.model.RDFNode ; +import org.apache.jena.rdf.model.ResourceFactory ; +import org.apache.jena.rdf.model.StmtIterator ; +import org.apache.jena.shared.PropertyNotFoundException ; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; + +@RunWith( value = SecurityEvaluatorParameters.class ) +public class SecuredResourceTest extends SecuredRDFNodeTest +{ + + public SecuredResourceTest( final MockSecurityEvaluator securityEvaluator ) + { + super(securityEvaluator); + } + + private SecuredResource getSecuredResource() + { + return (SecuredResource) getSecuredRDFNode(); + } + + @Override + @Before + public void setup() + { + super.setup(); + setSecuredRDFNode(SecuredResourceImpl.getInstance(securedModel, + SecuredRDFNodeTest.s), SecuredRDFNodeTest.s); + } + + /** + * @sec.graph Update + * @sec.triple Create (this, p, o ) + * @throws AccessDeniedException + */ + @Test + public void testAddLiteralBoolean() + { + final Set<Action> perms = SecurityEvaluator.Util.asSet(new Action[] { + Action.Update, Action.Create }); + try + { + getSecuredResource().addLiteral(SecuredRDFNodeTest.p, true); + if (!securityEvaluator.evaluate(perms)) + { + Assert.fail("Should have thrown AccessDenied Exception"); + } + } + catch (final AccessDeniedException e) + { + if (securityEvaluator.evaluate(perms)) + { + Assert.fail(String + .format("Should not have thrown AccessDenied Exception: %s - %s", + e, e.getTriple())); + } + } + } + + public void testAddLiteralChar() + { + final Set<Action> perms = SecurityEvaluator.Util.asSet(new Action[] { + Action.Update, Action.Create }); + try + { + getSecuredResource().addLiteral(SecuredRDFNodeTest.p, 'c'); + if (!securityEvaluator.evaluate(perms)) + { + Assert.fail("Should have thrown AccessDenied Exception"); + } + } + catch (final AccessDeniedException e) + { + if (securityEvaluator.evaluate(perms)) + { + Assert.fail(String + .format("Should not have thrown AccessDenied Exception: %s - %s", + e, e.getTriple())); + } + } +} + + public void testAddLiteralDouble() + { + final Set<Action> perms = SecurityEvaluator.Util.asSet(new Action[] { + Action.Update, Action.Create }); + + try + { + getSecuredResource().addLiteral(SecuredRDFNodeTest.p, 3.14D); + if (!securityEvaluator.evaluate(perms)) + { + Assert.fail("Should have thrown AccessDenied Exception"); + } + } + catch (final AccessDeniedException e) + { + if (securityEvaluator.evaluate(perms)) + { + Assert.fail(String + .format("Should not have thrown AccessDenied Exception: %s - %s", + e, e.getTriple())); + } + } +} + + public void testAddLiteralFloat() + { + final Set<Action> perms = SecurityEvaluator.Util.asSet(new Action[] { + Action.Update, Action.Create }); + + try + { + getSecuredResource().addLiteral(SecuredRDFNodeTest.p, 3.14F); + if (!securityEvaluator.evaluate(perms)) + { + Assert.fail("Should have thrown AccessDenied Exception"); + } + } + catch (final AccessDeniedException e) + { + if (securityEvaluator.evaluate(perms)) + { + Assert.fail(String + .format("Should not have thrown AccessDenied Exception: %s - %s", + e, e.getTriple())); + } + } +} + + public void testAddLiteral() + { + final Set<Action> perms = SecurityEvaluator.Util.asSet(new Action[] { + Action.Update, Action.Create }); + + try + { + getSecuredResource().addLiteral(SecuredRDFNodeTest.p, + ResourceFactory.createTypedLiteral("Yee haw")); + if (!securityEvaluator.evaluate(perms)) + { + Assert.fail("Should have thrown AccessDenied Exception"); + } + } + catch (final AccessDeniedException e) + { + if (securityEvaluator.evaluate(perms)) + { + Assert.fail(String + .format("Should not have thrown AccessDenied Exception: %s - %s", + e, e.getTriple())); + } + } +} + + public void testAddLiteralLong() + { + final Set<Action> perms = SecurityEvaluator.Util.asSet(new Action[] { + Action.Update, Action.Create }); + + try + { + getSecuredResource().addLiteral(SecuredRDFNodeTest.p, 1L); + if (!securityEvaluator.evaluate(perms)) + { + Assert.fail("Should have thrown AccessDenied Exception"); + } + } + catch (final AccessDeniedException e) + { + if (securityEvaluator.evaluate(perms)) + { + Assert.fail(String + .format("Should not have thrown AccessDenied Exception: %s - %s", + e, e.getTriple())); + } + } +} + + public void testAddLiteralObject() + { + final Set<Action> perms = SecurityEvaluator.Util.asSet(new Action[] { + Action.Update, Action.Create }); + + final Object o = Integer.valueOf("1234"); + try + { + getSecuredResource().addLiteral(SecuredRDFNodeTest.p, o); + if (!securityEvaluator.evaluate(perms)) + { + Assert.fail("Should have thrown AccessDenied Exception"); + } + } + catch (final AccessDeniedException e) + { + if (securityEvaluator.evaluate(perms)) + { + Assert.fail(String + .format("Should not have thrown AccessDenied Exception: %s - %s", + e, e.getTriple())); + } + } + + } + + @Test + public void testAddProperty() + { + final Set<Action> perms = SecurityEvaluator.Util.asSet(new Action[] { + Action.Update, Action.Create }); + + final RDFNode rdfNode = ResourceFactory + .createResource("http://example.com/newResource"); + try + { + getSecuredResource().addLiteral(SecuredRDFNodeTest.p, rdfNode); + if (!securityEvaluator.evaluate(perms)) + { + Assert.fail("Should have thrown AccessDenied Exception"); + } + } + catch (final AccessDeniedException e) + { + if (securityEvaluator.evaluate(perms)) + { + Assert.fail(String + .format("Should not have thrown AccessDenied Exception: %s - %s", + e, e.getTriple())); + } + } + + try + { + getSecuredResource().addLiteral(SecuredRDFNodeTest.p, "string"); + if (!securityEvaluator.evaluate(perms)) + { + Assert.fail("Should have thrown AccessDenied Exception"); + } + } + catch (final AccessDeniedException e) + { + if (securityEvaluator.evaluate(perms)) + { + Assert.fail(String + .format("Should not have thrown AccessDenied Exception: %s - %s", + e, e.getTriple())); + } + } + + final Literal l = ResourceFactory.createTypedLiteral( 3.14F ); + try + { + getSecuredResource().addProperty(SecuredRDFNodeTest.p, + l.getLexicalForm(), l.getDatatype()); + if (!securityEvaluator.evaluate(perms)) + { + Assert.fail("Should have thrown AccessDenied Exception"); + } + } + catch (final AccessDeniedException e) + { + if (securityEvaluator.evaluate(perms)) + { + Assert.fail(String + .format("Should not have thrown AccessDenied Exception: %s - %s", + e, e.getTriple())); + } + } + + try + { + getSecuredResource().addProperty(SecuredRDFNodeTest.p, "dos", "sp"); + if (!securityEvaluator.evaluate(perms)) + { + Assert.fail("Should have thrown AccessDenied Exception"); + } + } + catch (final AccessDeniedException e) + { + if (securityEvaluator.evaluate(perms)) + { + Assert.fail(String + .format("Should not have thrown AccessDenied Exception: %s - %s", + e, e.getTriple())); + } + } + } + + @Test + public void testAnonFuncs() + { + + final SecuredResource anonResource = securedModel.createResource(); + setSecuredRDFNode(anonResource, null); + + try + { + getSecuredResource().getId(); + if (!securityEvaluator.evaluate(Action.Read)) + { + Assert.fail("Should have thrown AccessDenied Exception"); + } + } + catch (final AccessDeniedException e) + { + if (securityEvaluator.evaluate(Action.Read)) + { + Assert.fail(String + .format("Should not have thrown AccessDenied Exception: %s - %s", + e, e.getTriple())); + } + } + } + + @Test + public void testAsResource() + { + getSecuredResource().asResource(); + } + + @Test + public void testEquals() + { + try + { + getSecuredResource().equals(SecuredRDFNodeTest.s); + if (!securityEvaluator.evaluate(Action.Read)) + { + Assert.fail("Should have thrown AccessDenied Exception"); + } + } + catch (final AccessDeniedException e) + { + if (securityEvaluator.evaluate(Action.Read)) + { + Assert.fail(String + .format("Should not have thrown AccessDenied Exception: %s - %s", + e, e.getTriple())); + } + } + } + + public void testGetLocalName() + { + try + { + getSecuredResource().getLocalName(); + if (!securityEvaluator.evaluate(Action.Read)) + { + Assert.fail("Should have thrown AccessDenied Exception"); + } + } + catch (final AccessDeniedException e) + { + if (securityEvaluator.evaluate(Action.Read)) + { + Assert.fail(String + .format("Should not have thrown AccessDenied Exception: %s - %s", + e, e.getTriple())); + } + } + } + + public void testGetNameSpace() + { + try + { + getSecuredResource().getNameSpace(); + if (!securityEvaluator.evaluate(Action.Read)) + { + Assert.fail("Should have thrown AccessDenied Exception"); + } + } + catch (final AccessDeniedException e) + { + if (securityEvaluator.evaluate(Action.Read)) + { + Assert.fail(String + .format("Should not have thrown AccessDenied Exception: %s - %s", + e, e.getTriple())); + } + } + } + + @Test + public void testGetProperty() + { + try + { + getSecuredResource().getProperty(SecuredRDFNodeTest.p); + if (!securityEvaluator.evaluate(Action.Read)) + { + Assert.fail("Should have thrown AccessDenied Exception"); + } + } + catch (final AccessDeniedException e) + { + if (securityEvaluator.evaluate(Action.Read)) + { + Assert.fail(String + .format("Should not have thrown AccessDenied Exception: %s - %s", + e, e.getTriple())); + } + } + + try + { + getSecuredResource().getPropertyResourceValue(SecuredRDFNodeTest.p); + if (!securityEvaluator.evaluate(Action.Read)) + { + Assert.fail("Should have thrown AccessDenied Exception"); + } + } + catch (final AccessDeniedException e) + { + if (securityEvaluator.evaluate(Action.Read)) + { + Assert.fail(String + .format("Should not have thrown AccessDenied Exception: %s - %s", + e, e.getTriple())); + } + } + + try + { + getSecuredResource().getRequiredProperty(SecuredRDFNodeTest.p); + if (!securityEvaluator.evaluate(Action.Read)) + { + Assert.fail("Should have thrown AccessDenied Exception"); + } + } + catch (final AccessDeniedException e) + { + if (securityEvaluator.evaluate(Action.Read)) + { + Assert.fail(String + .format("Should not have thrown AccessDenied Exception: %s - %s", + e, e.getTriple())); + } + } + catch (final PropertyNotFoundException e) + { + // expected if (this, "p", ANY) is not in the base securedModel. + final StmtIterator iter = baseModel.listStatements( + getSecuredResource(), SecuredRDFNodeTest.p, (RDFNode) null); + try + { + if (iter.hasNext()) + { + throw e; + } + } + finally + { + iter.close(); + } + } + } + + @Test + public void testGetURI() + { + try + { + getSecuredResource().getURI(); + if (!securityEvaluator.evaluate(Action.Read)) + { + Assert.fail("Should have thrown AccessDenied Exception"); + } + } + catch (final AccessDeniedException e) + { + if (securityEvaluator.evaluate(Action.Read)) + { + Assert.fail(String + .format("Should not have thrown AccessDenied Exception: %s - %s", + e, e.getTriple())); + } + } + } + + @Test + public void testHasLiteral() + { + try + { + getSecuredResource().hasLiteral(SecuredRDFNodeTest.p, true); + if (!securityEvaluator.evaluate(Action.Read)) + { + Assert.fail("Should have thrown AccessDenied Exception"); + } + } + catch (final AccessDeniedException e) + { + if (securityEvaluator.evaluate(Action.Read)) + { + Assert.fail(String + .format("Should not have thrown AccessDenied Exception: %s - %s", + e, e.getTriple())); + } + } + + try + { + getSecuredResource().hasLiteral(SecuredRDFNodeTest.p, 'c'); + if (!securityEvaluator.evaluate(Action.Read)) + { + Assert.fail("Should have thrown AccessDenied Exception"); + } + } + catch (final AccessDeniedException e) + { + if (securityEvaluator.evaluate(Action.Read)) + { + Assert.fail(String + .format("Should not have thrown AccessDenied Exception: %s - %s", + e, e.getTriple())); + } + } + + try + { + getSecuredResource().hasLiteral(SecuredRDFNodeTest.p, 3.14d); + if (!securityEvaluator.evaluate(Action.Read)) + { + Assert.fail("Should have thrown AccessDenied Exception"); + } + } + catch (final AccessDeniedException e) + { + if (securityEvaluator.evaluate(Action.Read)) + { + Assert.fail(String + .format("Should not have thrown AccessDenied Exception: %s - %s", + e, e.getTriple())); + } + } + + try + { + getSecuredResource().hasLiteral(SecuredRDFNodeTest.p, 3.14f); + if (!securityEvaluator.evaluate(Action.Read)) + { + Assert.fail("Should have thrown AccessDenied Exception"); + } + } + catch (final AccessDeniedException e) + { + if (securityEvaluator.evaluate(Action.Read)) + { + Assert.fail(String + .format("Should not have thrown AccessDenied Exception: %s - %s", + e, e.getTriple())); + } + } + + try + { + getSecuredResource().hasLiteral(SecuredRDFNodeTest.p, 6l); + if (!securityEvaluator.evaluate(Action.Read)) + { + Assert.fail("Should have thrown AccessDenied Exception"); + } + } + catch (final AccessDeniedException e) + { + if (securityEvaluator.evaluate(Action.Read)) + { + Assert.fail(String + .format("Should not have thrown AccessDenied Exception: %s - %s", + e, e.getTriple())); + } + } + + final Object o = 6; + try + { + getSecuredResource().hasLiteral(SecuredRDFNodeTest.p, o); + if (!securityEvaluator.evaluate(Action.Read)) + { + Assert.fail("Should have thrown AccessDenied Exception"); + } + } + catch (final AccessDeniedException e) + { + if (securityEvaluator.evaluate(Action.Read)) + { + Assert.fail(String + .format("Should not have thrown AccessDenied Exception: %s - %s", + e, e.getTriple())); + } + } + } + + @Test + public void testHasProperty() + { + + try + { + getSecuredResource().hasProperty(SecuredRDFNodeTest.p); + if (!securityEvaluator.evaluate(Action.Read)) + { + Assert.fail("Should have thrown AccessDenied Exception"); + } + } + catch (final AccessDeniedException e) + { + if (securityEvaluator.evaluate(Action.Read)) + { + Assert.fail(String + .format("Should not have thrown AccessDenied Exception: %s - %s", + e, e.getTriple())); + } + } + + try + { + getSecuredResource().hasProperty(SecuredRDFNodeTest.p, + SecuredRDFNodeTest.o); + if (!securityEvaluator.evaluate(Action.Read)) + { + Assert.fail("Should have thrown AccessDenied Exception"); + } + } + catch (final AccessDeniedException e) + { + if (securityEvaluator.evaluate(Action.Read)) + { + Assert.fail(String + .format("Should not have thrown AccessDenied Exception: %s - %s", + e, e.getTriple())); + } + } + + try + { + getSecuredResource().hasProperty(SecuredRDFNodeTest.p, "yeee haw"); + if (!securityEvaluator.evaluate(Action.Read)) + { + Assert.fail("Should have thrown AccessDenied Exception"); + } + } + catch (final AccessDeniedException e) + { + if (securityEvaluator.evaluate(Action.Read)) + { + Assert.fail(String + .format("Should not have thrown AccessDenied Exception: %s - %s", + e, e.getTriple())); + } + } + + try + { + getSecuredResource().hasProperty(SecuredRDFNodeTest.p, "dos", "sp"); + if (!securityEvaluator.evaluate(Action.Read)) + { + Assert.fail("Should have thrown AccessDenied Exception"); + } + } + catch (final AccessDeniedException e) + { + if (securityEvaluator.evaluate(Action.Read)) + { + Assert.fail(String + .format("Should not have thrown AccessDenied Exception: %s - %s", + e, e.getTriple())); + } + } + } + + @Test + public void testHasURI() + { + try + { + getSecuredResource().hasURI("http://example.com/yeeHaw"); + if (!securityEvaluator.evaluate(Action.Read)) + { + Assert.fail("Should have thrown AccessDenied Exception"); + } + } + catch (final AccessDeniedException e) + { + if (securityEvaluator.evaluate(Action.Read)) + { + Assert.fail(String + .format("Should not have thrown AccessDenied Exception: %s - %s", + e, e.getTriple())); + } + } + } + + @Test + public void testListProperties() + { + try + { + getSecuredResource().listProperties(); + if (!securityEvaluator.evaluate(Action.Read)) + { + Assert.fail("Should have thrown AccessDenied Exception"); + } + } + catch (final AccessDeniedException e) + { + if (securityEvaluator.evaluate(Action.Read)) + { + Assert.fail(String + .format("Should not have thrown AccessDenied Exception: %s - %s", + e, e.getTriple())); + } + } + + try + { + getSecuredResource().listProperties(SecuredRDFNodeTest.p); + if (!securityEvaluator.evaluate(Action.Read)) + { + Assert.fail("Should have thrown AccessDenied Exception"); + } + } + catch (final AccessDeniedException e) + { + if (securityEvaluator.evaluate(Action.Read)) + { + Assert.fail(String + .format("Should not have thrown AccessDenied Exception: %s - %s", + e, e.getTriple())); + } + } + } + + @Test + public void testRemoveAll() + { + final Set<Action> perms = SecurityEvaluator.Util.asSet(new Action[] { + Action.Update, Action.Delete }); + final int count = baseModel + .listStatements(getBaseRDFNode().asResource(), + SecuredRDFNodeTest.p, (RDFNode) null).toSet().size(); + + try + { + getSecuredResource().removeAll(SecuredRDFNodeTest.p); + // only throw on delete if count > 0 + if (!securityEvaluator.evaluate(Action.Update) + || ((count > 0) && !securityEvaluator + .evaluate(Action.Delete))) + { + Assert.fail("Should have thrown AccessDenied Exception"); + } + } + catch (final AccessDeniedException e) + { + if (securityEvaluator.evaluate(perms)) + { + Assert.fail(String + .format("Should not have thrown AccessDenied Exception: %s - %s", + e, e.getTriple())); + } + } + } + + @Test + public void testRemoveProperties() + { + final Set<Action> perms = SecurityEvaluator.Util.asSet(new Action[] { + Action.Update, Action.Delete }); + final int count = baseModel + .listStatements(getBaseRDFNode().asResource(), + SecuredRDFNodeTest.p, (RDFNode) null).toSet().size(); + + try + { + getSecuredResource().removeProperties(); + // only throw on delete if count > 0 + if (!securityEvaluator.evaluate(Action.Update) + || ((count > 0) && !securityEvaluator + .evaluate(Action.Delete))) + { + Assert.fail("Should have thrown AccessDenied Exception"); + } + } + catch (final AccessDeniedException e) + { + if (securityEvaluator.evaluate(perms)) + { + Assert.fail(String + .format("Should not have thrown AccessDenied Exception: %s - %s", + e, e.getTriple())); + } + } + } +} http://git-wip-us.apache.org/repos/asf/jena/blob/c4b0113d/jena-permissions/src/test/java/org/apache/jena/permissions/model/SecuredSeqTest.java ---------------------------------------------------------------------- diff --git a/jena-permissions/src/test/java/org/apache/jena/permissions/model/SecuredSeqTest.java b/jena-permissions/src/test/java/org/apache/jena/permissions/model/SecuredSeqTest.java new file mode 100644 index 0000000..54c365a --- /dev/null +++ b/jena-permissions/src/test/java/org/apache/jena/permissions/model/SecuredSeqTest.java @@ -0,0 +1,1013 @@ +/* + * 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.permissions.model; + +import java.util.Set; + +import org.apache.jena.permissions.AccessDeniedException; +import org.apache.jena.permissions.MockSecurityEvaluator; +import org.apache.jena.permissions.SecurityEvaluator; +import org.apache.jena.permissions.SecurityEvaluatorParameters; +import org.apache.jena.permissions.SecurityEvaluator.Action; +import org.apache.jena.permissions.model.SecuredAlt; +import org.apache.jena.permissions.model.SecuredBag; +import org.apache.jena.permissions.model.SecuredSeq; +import org.apache.jena.permissions.model.impl.SecuredSeqImpl; +import org.apache.jena.rdf.model.Alt ; +import org.apache.jena.rdf.model.Bag ; +import org.apache.jena.rdf.model.ResourceFactory ; +import org.apache.jena.rdf.model.Seq ; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; + +@RunWith( value = SecurityEvaluatorParameters.class ) +public class SecuredSeqTest extends SecuredContainerTest +{ + private Seq seq; + + public SecuredSeqTest( final MockSecurityEvaluator securityEvaluator ) + { + super(securityEvaluator); + // TODO Auto-generated constructor stub + } + + private SecuredSeq getSecuredSeq() + { + return (SecuredSeq) getSecuredRDFNode(); + } + + @Override + @Before + public void setup() + { + super.setup(); + seq = baseModel.getSeq("http://example.com/testContainer"); + setSecuredRDFNode(SecuredSeqImpl.getInstance(securedModel, seq), seq); + } + + @Override + @Test + public void testAdd() + { + final Set<Action> perms = SecurityEvaluator.Util.asSet(new Action[] { + Action.Update, Action.Create }); + try + { + getSecuredSeq().add(2, true); + if (!securityEvaluator.evaluate(perms)) + { + Assert.fail("Should have thrown AccessDenied Exception"); + } + } + catch (final AccessDeniedException e) + { + if (securityEvaluator.evaluate(perms)) + { + Assert.fail(String + .format("Should not have thrown AccessDenied Exception: %s - %s", + e, e.getTriple())); + } + } + + try + { + getSecuredSeq().add(2, 'c'); + if (!securityEvaluator.evaluate(perms)) + { + Assert.fail("Should have thrown AccessDenied Exception"); + } + } + catch (final AccessDeniedException e) + { + if (securityEvaluator.evaluate(perms)) + { + Assert.fail(String + .format("Should not have thrown AccessDenied Exception: %s - %s", + e, e.getTriple())); + } + } + + try + { + getSecuredSeq().add(2, 3.14d); + if (!securityEvaluator.evaluate(perms)) + { + Assert.fail("Should have thrown AccessDenied Exception"); + } + } + catch (final AccessDeniedException e) + { + if (securityEvaluator.evaluate(perms)) + { + Assert.fail(String + .format("Should not have thrown AccessDenied Exception: %s - %s", + e, e.getTriple())); + } + } + + try + { + getSecuredSeq().add(2, 3.14F); + if (!securityEvaluator.evaluate(perms)) + { + Assert.fail("Should have thrown AccessDenied Exception"); + } + } + catch (final AccessDeniedException e) + { + if (securityEvaluator.evaluate(perms)) + { + Assert.fail(String + .format("Should not have thrown AccessDenied Exception: %s - %s", + e, e.getTriple())); + } + } + + try + { + getSecuredSeq().add(2, 3L); + if (!securityEvaluator.evaluate(perms)) + { + Assert.fail("Should have thrown AccessDenied Exception"); + } + } + catch (final AccessDeniedException e) + { + if (securityEvaluator.evaluate(perms)) + { + Assert.fail(String + .format("Should not have thrown AccessDenied Exception: %s - %s", + e, e.getTriple())); + } + } + + try + { + final Object o = Integer.MAX_VALUE; + getSecuredSeq().add(2, o); + if (!securityEvaluator.evaluate(perms)) + { + Assert.fail("Should have thrown AccessDenied Exception"); + } + } + catch (final AccessDeniedException e) + { + if (securityEvaluator.evaluate(perms)) + { + Assert.fail(String + .format("Should not have thrown AccessDenied Exception: %s - %s", + e, e.getTriple())); + } + } + + try + { + getSecuredSeq().add(2, ResourceFactory.createResource()); + if (!securityEvaluator.evaluate(perms)) + { + Assert.fail("Should have thrown AccessDenied Exception"); + } + } + catch (final AccessDeniedException e) + { + if (securityEvaluator.evaluate(perms)) + { + Assert.fail(String + .format("Should not have thrown AccessDenied Exception: %s - %s", + e, e.getTriple())); + } + } + + try + { + getSecuredSeq().add(2, "Waa hoo"); + if (!securityEvaluator.evaluate(perms)) + { + Assert.fail("Should have thrown AccessDenied Exception"); + } + } + catch (final AccessDeniedException e) + { + if (securityEvaluator.evaluate(perms)) + { + Assert.fail(String + .format("Should not have thrown AccessDenied Exception: %s - %s", + e, e.getTriple())); + } + } + + try + { + getSecuredSeq().add(2, "dos", "es"); + if (!securityEvaluator.evaluate(perms)) + { + Assert.fail("Should have thrown AccessDenied Exception"); + } + } + catch (final AccessDeniedException e) + { + if (securityEvaluator.evaluate(perms)) + { + Assert.fail(String + .format("Should not have thrown AccessDenied Exception: %s - %s", + e, e.getTriple())); + } + } + } + + @Test + public void testGetAlt() + { + try + { + final Alt a = getSecuredSeq().getAlt(1); + Assert.assertTrue("Should be a secured Alt", + a instanceof SecuredAlt); + if (!securityEvaluator.evaluate(Action.Read)) + { + Assert.fail("Should have thrown AccessDenied Exception"); + } + } + catch (final AccessDeniedException e) + { + if (securityEvaluator.evaluate(Action.Read)) + { + Assert.fail(String + .format("Should not have thrown AccessDenied Exception: %s - %s", + e, e.getTriple())); + } + } + } + + @Test + public void testGetBag() + { + try + { + final Bag a = getSecuredSeq().getBag(1); + Assert.assertTrue("Should be a secured Bag", + a instanceof SecuredBag); + if (!securityEvaluator.evaluate(Action.Read)) + { + Assert.fail("Should have thrown AccessDenied Exception"); + } + } + catch (final AccessDeniedException e) + { + if (securityEvaluator.evaluate(Action.Read)) + { + Assert.fail(String + .format("Should not have thrown AccessDenied Exception: %s - %s", + e, e.getTriple())); + } + } + + } + + @Test + public void testGetBoolean() + { + seq.add(2, true); + try + { + getSecuredSeq().getBoolean(2); + if (!securityEvaluator.evaluate(Action.Read)) + { + Assert.fail("Should have thrown AccessDenied Exception"); + } + } + catch (final AccessDeniedException e) + { + if (securityEvaluator.evaluate(Action.Read)) + { + Assert.fail(String + .format("Should not have thrown AccessDenied Exception: %s - %s", + e, e.getTriple())); + } + } + + } + + @Test + public void testGetByte() + { + seq.add(2, Byte.MAX_VALUE); + try + { + getSecuredSeq().getByte(2); + if (!securityEvaluator.evaluate(Action.Read)) + { + Assert.fail("Should have thrown AccessDenied Exception"); + } + } + catch (final AccessDeniedException e) + { + if (securityEvaluator.evaluate(Action.Read)) + { + Assert.fail(String + .format("Should not have thrown AccessDenied Exception: %s - %s", + e, e.getTriple())); + } + } + + } + + @Test + public void testGetChar() + { + seq.add(2, 'c'); + try + { + getSecuredSeq().getChar(2); + if (!securityEvaluator.evaluate(Action.Read)) + { + Assert.fail("Should have thrown AccessDenied Exception"); + } + } + catch (final AccessDeniedException e) + { + if (securityEvaluator.evaluate(Action.Read)) + { + Assert.fail(String + .format("Should not have thrown AccessDenied Exception: %s - %s", + e, e.getTriple())); + } + } + + } + + @Test + public void testGetDouble() + { + seq.add(2, 3.14D); + try + { + getSecuredSeq().getDouble(2); + if (!securityEvaluator.evaluate(Action.Read)) + { + Assert.fail("Should have thrown AccessDenied Exception"); + } + } + catch (final AccessDeniedException e) + { + if (securityEvaluator.evaluate(Action.Read)) + { + Assert.fail(String + .format("Should not have thrown AccessDenied Exception: %s - %s", + e, e.getTriple())); + } + } + + } + + @Test + public void testGetFloat() + { + seq.add(2, 3.14F); + try + { + getSecuredSeq().getFloat(2); + if (!securityEvaluator.evaluate(Action.Read)) + { + Assert.fail("Should have thrown AccessDenied Exception"); + } + } + catch (final AccessDeniedException e) + { + if (securityEvaluator.evaluate(Action.Read)) + { + Assert.fail(String + .format("Should not have thrown AccessDenied Exception: %s - %s", + e, e.getTriple())); + } + } + + } + + @Test + public void testGetInt() + { + seq.add(2, 2); + try + { + getSecuredSeq().getInt(2); + if (!securityEvaluator.evaluate(Action.Read)) + { + Assert.fail("Should have thrown AccessDenied Exception"); + } + } + catch (final AccessDeniedException e) + { + if (securityEvaluator.evaluate(Action.Read)) + { + Assert.fail(String + .format("Should not have thrown AccessDenied Exception: %s - %s", + e, e.getTriple())); + } + } + + } + + @Test + public void testGetLanguage() + { + seq.add(2, "foo"); + try + { + getSecuredSeq().getLanguage(2); + if (!securityEvaluator.evaluate(Action.Read)) + { + Assert.fail("Should have thrown AccessDenied Exception"); + } + } + catch (final AccessDeniedException e) + { + if (securityEvaluator.evaluate(Action.Read)) + { + Assert.fail(String + .format("Should not have thrown AccessDenied Exception: %s - %s", + e, e.getTriple())); + } + } + + } + + @Test + public void testGetLiteral() + { + seq.add(2, "foo"); + try + { + getSecuredSeq().getLiteral(2); + if (!securityEvaluator.evaluate(Action.Read)) + { + Assert.fail("Should have thrown AccessDenied Exception"); + } + } + catch (final AccessDeniedException e) + { + if (securityEvaluator.evaluate(Action.Read)) + { + Assert.fail(String + .format("Should not have thrown AccessDenied Exception: %s - %s", + e, e.getTriple())); + } + } + + } + + @Test + public void testGetLong() + { + seq.add(2, 2L); + try + { + getSecuredSeq().getLong(2); + if (!securityEvaluator.evaluate(Action.Read)) + { + Assert.fail("Should have thrown AccessDenied Exception"); + } + } + catch (final AccessDeniedException e) + { + if (securityEvaluator.evaluate(Action.Read)) + { + Assert.fail(String + .format("Should not have thrown AccessDenied Exception: %s - %s", + e, e.getTriple())); + } + } + + } + + @Test + public void testGetObject() + { + final Object o = Integer.MAX_VALUE; + seq.add(2, o); + try + { + getSecuredSeq().getObject(2); + if (!securityEvaluator.evaluate(Action.Read)) + { + Assert.fail("Should have thrown AccessDenied Exception"); + } + } + catch (final AccessDeniedException e) + { + if (securityEvaluator.evaluate(Action.Read)) + { + Assert.fail(String + .format("Should not have thrown AccessDenied Exception: %s - %s", + e, e.getTriple())); + } + } + + } + + @Test + public void testGetResource() + { + seq.add(2, ResourceFactory.createResource()); + try + { + getSecuredSeq().getResource(2); + if (!securityEvaluator.evaluate(Action.Read)) + { + Assert.fail("Should have thrown AccessDenied Exception"); + } + } + catch (final AccessDeniedException e) + { + if (securityEvaluator.evaluate(Action.Read)) + { + Assert.fail(String + .format("Should not have thrown AccessDenied Exception: %s - %s", + e, e.getTriple())); + } + } + + } + + /* + * @Test + * public void testGetChar() + * { + * ResourceF f; + * seq.add( 2, 'c' ); + * try + * { + * getSecuredSeq().getResource(2, f ); + * if (!securityEvaluator.evaluate(Action.Read)) + * { + * Assert.fail("Should have thrown AccessDenied Exception"); + * } + * } + * catch (final AccessDeniedException e) + * { + * if (securityEvaluator.evaluate(Action.Read)) + * { + * Assert.fail(String + * .format("Should not have thrown AccessDenied Exception: %s - %s", + * e, e.getTriple())); + * } + * } + * + * } + */ + @Test + public void testGetSeq() + { + seq.add(2, 'c'); + try + { + getSecuredSeq().getSeq(2); + if (!securityEvaluator.evaluate(Action.Read)) + { + Assert.fail("Should have thrown AccessDenied Exception"); + } + } + catch (final AccessDeniedException e) + { + if (securityEvaluator.evaluate(Action.Read)) + { + Assert.fail(String + .format("Should not have thrown AccessDenied Exception: %s - %s", + e, e.getTriple())); + } + } + + } + + @Test + public void testGetShort() + { + seq.add(2, Short.MAX_VALUE); + try + { + getSecuredSeq().getShort(2); + if (!securityEvaluator.evaluate(Action.Read)) + { + Assert.fail("Should have thrown AccessDenied Exception"); + } + } + catch (final AccessDeniedException e) + { + if (securityEvaluator.evaluate(Action.Read)) + { + Assert.fail(String + .format("Should not have thrown AccessDenied Exception: %s - %s", + e, e.getTriple())); + } + } + + } + + @Test + public void testGetString() + { + seq.add(2, "Waaa hoo"); + try + { + getSecuredSeq().getString(2); + if (!securityEvaluator.evaluate(Action.Read)) + { + Assert.fail("Should have thrown AccessDenied Exception"); + } + } + catch (final AccessDeniedException e) + { + if (securityEvaluator.evaluate(Action.Read)) + { + Assert.fail(String + .format("Should not have thrown AccessDenied Exception: %s - %s", + e, e.getTriple())); + } + } + + } + + @Test + public void testIndexOf() + { + try + { + getSecuredSeq().indexOf(true); + if (!securityEvaluator.evaluate(Action.Read)) + { + Assert.fail("Should have thrown AccessDenied Exception"); + } + } + catch (final AccessDeniedException e) + { + if (securityEvaluator.evaluate(Action.Read)) + { + Assert.fail(String + .format("Should not have thrown AccessDenied Exception: %s - %s", + e, e.getTriple())); + } + } + + try + { + getSecuredSeq().indexOf('c'); + if (!securityEvaluator.evaluate(Action.Read)) + { + Assert.fail("Should have thrown AccessDenied Exception"); + } + } + catch (final AccessDeniedException e) + { + if (securityEvaluator.evaluate(Action.Read)) + { + Assert.fail(String + .format("Should not have thrown AccessDenied Exception: %s - %s", + e, e.getTriple())); + } + } + + try + { + getSecuredSeq().indexOf(3.14D); + if (!securityEvaluator.evaluate(Action.Read)) + { + Assert.fail("Should have thrown AccessDenied Exception"); + } + } + catch (final AccessDeniedException e) + { + if (securityEvaluator.evaluate(Action.Read)) + { + Assert.fail(String + .format("Should not have thrown AccessDenied Exception: %s - %s", + e, e.getTriple())); + } + } + + try + { + getSecuredSeq().indexOf(3.14F); + if (!securityEvaluator.evaluate(Action.Read)) + { + Assert.fail("Should have thrown AccessDenied Exception"); + } + } + catch (final AccessDeniedException e) + { + if (securityEvaluator.evaluate(Action.Read)) + { + Assert.fail(String + .format("Should not have thrown AccessDenied Exception: %s - %s", + e, e.getTriple())); + } + } + + try + { + getSecuredSeq().indexOf(3L); + if (!securityEvaluator.evaluate(Action.Read)) + { + Assert.fail("Should have thrown AccessDenied Exception"); + } + } + catch (final AccessDeniedException e) + { + if (securityEvaluator.evaluate(Action.Read)) + { + Assert.fail(String + .format("Should not have thrown AccessDenied Exception: %s - %s", + e, e.getTriple())); + } + } + + try + { + final Object o = Integer.MAX_VALUE; + getSecuredSeq().indexOf(o); + if (!securityEvaluator.evaluate(Action.Read)) + { + Assert.fail("Should have thrown AccessDenied Exception"); + } + } + catch (final AccessDeniedException e) + { + if (securityEvaluator.evaluate(Action.Read)) + { + Assert.fail(String + .format("Should not have thrown AccessDenied Exception: %s - %s", + e, e.getTriple())); + } + } + + try + { + + getSecuredSeq() + .indexOf( + ResourceFactory + .createResource("http://example.com/exampleResource")); + if (!securityEvaluator.evaluate(Action.Read)) + { + Assert.fail("Should have thrown AccessDenied Exception"); + } + } + catch (final AccessDeniedException e) + { + if (securityEvaluator.evaluate(Action.Read)) + { + Assert.fail(String + .format("Should not have thrown AccessDenied Exception: %s - %s", + e, e.getTriple())); + } + } + + try + { + getSecuredSeq().indexOf("waaa hooo"); + if (!securityEvaluator.evaluate(Action.Read)) + { + Assert.fail("Should have thrown AccessDenied Exception"); + } + } + catch (final AccessDeniedException e) + { + if (securityEvaluator.evaluate(Action.Read)) + { + Assert.fail(String + .format("Should not have thrown AccessDenied Exception: %s - %s", + e, e.getTriple())); + } + } + + try + { + getSecuredSeq().indexOf("dos", "es"); + if (!securityEvaluator.evaluate(Action.Read)) + { + Assert.fail("Should have thrown AccessDenied Exception"); + } + } + catch (final AccessDeniedException e) + { + if (securityEvaluator.evaluate(Action.Read)) + { + Assert.fail(String + .format("Should not have thrown AccessDenied Exception: %s - %s", + e, e.getTriple())); + } + } + + } + + @Override + @Test + public void testRemove() + { + final Set<Action> perms = SecurityEvaluator.Util.asSet(new Action[] { + Action.Update, Action.Delete }); + try + { + getSecuredSeq().remove(1); + if (!securityEvaluator.evaluate(perms)) + { + Assert.fail("Should have thrown AccessDenied Exception"); + } + } + catch (final AccessDeniedException e) + { + if (securityEvaluator.evaluate(perms)) + { + Assert.fail(String + .format("Should not have thrown AccessDenied Exception: %s - %s", + e, e.getTriple())); + } + } + } + + @Test + public void testSet() + { + final Set<Action> perms = SecurityEvaluator.Util + .asSet(new Action[] { Action.Update }); + try + { + getSecuredSeq().set(1, true); + if (!securityEvaluator.evaluate(perms)) + { + Assert.fail("Should have thrown AccessDenied Exception"); + } + } + catch (final AccessDeniedException e) + { + if (securityEvaluator.evaluate(perms)) + { + Assert.fail(String + .format("Should not have thrown AccessDenied Exception: %s - %s", + e, e.getTriple())); + } + } + + try + { + getSecuredSeq().set(1, 'c'); + if (!securityEvaluator.evaluate(perms)) + { + Assert.fail("Should have thrown AccessDenied Exception"); + } + } + catch (final AccessDeniedException e) + { + if (securityEvaluator.evaluate(perms)) + { + Assert.fail(String + .format("Should not have thrown AccessDenied Exception: %s - %s", + e, e.getTriple())); + } + } + + try + { + getSecuredSeq().set(1, 3.14d); + if (!securityEvaluator.evaluate(perms)) + { + Assert.fail("Should have thrown AccessDenied Exception"); + } + } + catch (final AccessDeniedException e) + { + if (securityEvaluator.evaluate(perms)) + { + Assert.fail(String + .format("Should not have thrown AccessDenied Exception: %s - %s", + e, e.getTriple())); + } + } + + try + { + getSecuredSeq().set(1, 3.14F); + if (!securityEvaluator.evaluate(perms)) + { + Assert.fail("Should have thrown AccessDenied Exception"); + } + } + catch (final AccessDeniedException e) + { + if (securityEvaluator.evaluate(perms)) + { + Assert.fail(String + .format("Should not have thrown AccessDenied Exception: %s - %s", + e, e.getTriple())); + } + } + + try + { + getSecuredSeq().set(1, 3L); + if (!securityEvaluator.evaluate(perms)) + { + Assert.fail("Should have thrown AccessDenied Exception"); + } + } + catch (final AccessDeniedException e) + { + if (securityEvaluator.evaluate(perms)) + { + Assert.fail(String + .format("Should not have thrown AccessDenied Exception: %s - %s", + e, e.getTriple())); + } + } + + try + { + final Object o = Integer.MAX_VALUE; + getSecuredSeq().set(1, o); + if (!securityEvaluator.evaluate(perms)) + { + Assert.fail("Should have thrown AccessDenied Exception"); + } + } + catch (final AccessDeniedException e) + { + if (securityEvaluator.evaluate(perms)) + { + Assert.fail(String + .format("Should not have thrown AccessDenied Exception: %s - %s", + e, e.getTriple())); + } + } + + try + { + getSecuredSeq().set(1, ResourceFactory.createResource()); + if (!securityEvaluator.evaluate(perms)) + { + Assert.fail("Should have thrown AccessDenied Exception"); + } + } + catch (final AccessDeniedException e) + { + if (securityEvaluator.evaluate(perms)) + { + Assert.fail(String + .format("Should not have thrown AccessDenied Exception: %s - %s", + e, e.getTriple())); + } + } + + try + { + getSecuredSeq().set(1, "Waa hoo"); + if (!securityEvaluator.evaluate(perms)) + { + Assert.fail("Should have thrown AccessDenied Exception"); + } + } + catch (final AccessDeniedException e) + { + if (securityEvaluator.evaluate(perms)) + { + Assert.fail(String + .format("Should not have thrown AccessDenied Exception: %s - %s", + e, e.getTriple())); + } + } + + try + { + getSecuredSeq().set(1, "dos", "es"); + if (!securityEvaluator.evaluate(perms)) + { + Assert.fail("Should have thrown AccessDenied Exception"); + } + } + catch (final AccessDeniedException e) + { + if (securityEvaluator.evaluate(perms)) + { + Assert.fail(String + .format("Should not have thrown AccessDenied Exception: %s - %s", + e, e.getTriple())); + } + } + } + +}
