You are viewing a plain text version of this content. The canonical link for it is here.
Posted to oak-commits@jackrabbit.apache.org by an...@apache.org on 2013/12/10 19:08:36 UTC
svn commit: r1549912 - in
/jackrabbit/oak/trunk/oak-jcr/src/test/java/org/apache/jackrabbit/oak/jcr/random:
./ AbstractRandomizedTest.java SimpleAclRandomizedTest.java
Author: angela
Date: Tue Dec 10 18:08:35 2013
New Revision: 1549912
URL: http://svn.apache.org/r1549912
Log:
OAK-884 : Add simple acl randomized test (slightly modified patch provided by asanso)
Added:
jackrabbit/oak/trunk/oak-jcr/src/test/java/org/apache/jackrabbit/oak/jcr/random/
jackrabbit/oak/trunk/oak-jcr/src/test/java/org/apache/jackrabbit/oak/jcr/random/AbstractRandomizedTest.java
jackrabbit/oak/trunk/oak-jcr/src/test/java/org/apache/jackrabbit/oak/jcr/random/SimpleAclRandomizedTest.java
Added: jackrabbit/oak/trunk/oak-jcr/src/test/java/org/apache/jackrabbit/oak/jcr/random/AbstractRandomizedTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-jcr/src/test/java/org/apache/jackrabbit/oak/jcr/random/AbstractRandomizedTest.java?rev=1549912&view=auto
==============================================================================
--- jackrabbit/oak/trunk/oak-jcr/src/test/java/org/apache/jackrabbit/oak/jcr/random/AbstractRandomizedTest.java (added)
+++ jackrabbit/oak/trunk/oak-jcr/src/test/java/org/apache/jackrabbit/oak/jcr/random/AbstractRandomizedTest.java Tue Dec 10 18:08:35 2013
@@ -0,0 +1,170 @@
+/*
+ * 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.jackrabbit.oak.jcr.random;
+
+import java.security.Principal;
+import java.util.ArrayList;
+import java.util.List;
+import javax.jcr.Repository;
+import javax.jcr.Session;
+import javax.jcr.SimpleCredentials;
+import javax.jcr.security.AccessControlManager;
+
+import org.apache.jackrabbit.api.JackrabbitSession;
+import org.apache.jackrabbit.api.security.JackrabbitAccessControlList;
+import org.apache.jackrabbit.api.security.user.Authorizable;
+import org.apache.jackrabbit.api.security.user.Group;
+import org.apache.jackrabbit.api.security.user.User;
+import org.apache.jackrabbit.api.security.user.UserManager;
+import org.apache.jackrabbit.commons.JcrUtils;
+import org.apache.jackrabbit.commons.jackrabbit.authorization.AccessControlUtils;
+import org.apache.jackrabbit.oak.jcr.Jcr;
+import org.junit.After;
+import org.junit.Before;
+
+
+/**
+ * Base class for randomized tests.
+ */
+public abstract class AbstractRandomizedTest {
+
+ protected Repository jackrabbitRepository;
+ protected Repository oakRepository;
+
+ protected Session jackrabbitWriterSession;
+ protected Session oakWriterSession;
+
+ protected Session jackrabbitReaderSession;
+ protected Session oakReaderSession;
+
+ protected Principal jackrabbitPrincipal;
+ protected Principal oakPrincipal;
+
+ protected String userId = "testuser";
+
+ protected List<Principal> jackrabbitPrincipals = new ArrayList<Principal>();
+ protected List<Principal> oakPrincipals = new ArrayList<Principal>();
+
+ @Before
+ public void setUp() throws Exception {
+ jackrabbitRepository = JcrUtils.getRepository();
+ jackrabbitWriterSession = jackrabbitRepository.login(new SimpleCredentials("admin", "admin".toCharArray()));
+ oakRepository = new Jcr().createRepository();
+ oakWriterSession = oakRepository.login(new SimpleCredentials("admin", "admin".toCharArray()));
+
+ jackrabbitPrincipal = setUpUser(jackrabbitWriterSession, userId);
+ jackrabbitPrincipals.add(jackrabbitPrincipal);
+
+ oakPrincipal = setUpUser(oakWriterSession, userId);
+ oakPrincipals.add(oakPrincipal);
+
+ Principal groupJR = setUpGroup(jackrabbitWriterSession, "group1", jackrabbitPrincipal);
+ jackrabbitPrincipals.add(groupJR);
+
+ Principal groupJR2 = setUpGroup(jackrabbitWriterSession, "group2", jackrabbitPrincipal);
+ jackrabbitPrincipals.add(groupJR2);
+
+ Principal groupOAK = setUpGroup(oakWriterSession, "group1", oakPrincipal);
+ oakPrincipals.add(groupOAK);
+
+ Principal groupOAK2 = setUpGroup(oakWriterSession, "group2", oakPrincipal);
+ oakPrincipals.add(groupOAK2);
+
+ oakReaderSession = oakRepository.login(new SimpleCredentials(userId, userId.toCharArray()));
+ jackrabbitReaderSession = jackrabbitRepository.login(new SimpleCredentials(userId, userId.toCharArray()));
+
+ setupTree(jackrabbitWriterSession);
+ setupTree(oakWriterSession);
+ }
+
+ @After
+ public void tearDown() throws Exception {
+
+ clearTree(jackrabbitWriterSession);
+ clearTree(oakWriterSession);
+
+ removAuthorizable(jackrabbitWriterSession, userId);
+ removAuthorizable(oakWriterSession, userId);
+
+ removAuthorizable(jackrabbitWriterSession, "group1");
+ removAuthorizable(oakWriterSession, "group1");
+ removAuthorizable(jackrabbitWriterSession, "group2");
+ removAuthorizable(oakWriterSession, "group2");
+
+ oakPrincipals.clear();
+ jackrabbitPrincipals.clear();
+
+ if (jackrabbitWriterSession.isLive()) {
+ jackrabbitWriterSession.logout();
+ }
+
+ if (oakWriterSession.isLive()) {
+ oakWriterSession.logout();
+ }
+
+ if (jackrabbitReaderSession.isLive()) {
+ jackrabbitReaderSession.logout();
+ }
+
+ if (oakReaderSession.isLive()) {
+ oakReaderSession.logout();
+ }
+
+ jackrabbitRepository = null;
+ oakRepository = null;
+ }
+
+ protected Principal setUpUser(Session session, String userId)
+ throws Exception {
+ UserManager userManager = ((JackrabbitSession) session).getUserManager();
+ User user = userManager.createUser(userId, userId);
+ session.save();
+ return user.getPrincipal();
+ }
+
+ protected Principal setUpGroup(Session session, String groupId,
+ Principal userPrincipal) throws Exception {
+ UserManager userManager = ((JackrabbitSession) session).getUserManager();
+ Group group = userManager.createGroup(groupId);
+ group.addMember(userManager.getAuthorizable(userPrincipal));
+ session.save();
+ return group.getPrincipal();
+ }
+
+ protected void removAuthorizable(Session session, String id)
+ throws Exception {
+ UserManager userManager = ((JackrabbitSession) session).getUserManager();
+ Authorizable authorizable = userManager.getAuthorizable(id);
+ if (id != null) {
+ authorizable.remove();
+ }
+ session.save();
+ }
+
+ protected void setupPermission(Session session, Principal principal, String path,
+ boolean allow, String... privilegeNames) throws Exception {
+ AccessControlManager acm = session.getAccessControlManager();
+ JackrabbitAccessControlList acl = AccessControlUtils.getAccessControlList(acm, path);
+ acl.addEntry(principal, AccessControlUtils.privilegesFromNames(acm, privilegeNames), allow);
+ acm.setPolicy(path, acl);
+ session.save();
+ }
+
+ protected abstract void setupTree(Session session) throws Exception;
+
+ protected abstract void clearTree(Session session) throws Exception;
+}
Added: jackrabbit/oak/trunk/oak-jcr/src/test/java/org/apache/jackrabbit/oak/jcr/random/SimpleAclRandomizedTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-jcr/src/test/java/org/apache/jackrabbit/oak/jcr/random/SimpleAclRandomizedTest.java?rev=1549912&view=auto
==============================================================================
--- jackrabbit/oak/trunk/oak-jcr/src/test/java/org/apache/jackrabbit/oak/jcr/random/SimpleAclRandomizedTest.java (added)
+++ jackrabbit/oak/trunk/oak-jcr/src/test/java/org/apache/jackrabbit/oak/jcr/random/SimpleAclRandomizedTest.java Tue Dec 10 18:08:35 2013
@@ -0,0 +1,154 @@
+/*
+ * 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.jackrabbit.oak.jcr.random;
+
+
+import java.util.Random;
+import javax.jcr.Node;
+import javax.jcr.PathNotFoundException;
+import javax.jcr.Session;
+import org.apache.jackrabbit.oak.spi.security.privilege.PrivilegeConstants;
+import org.junit.Assert;
+import org.junit.Test;
+import com.google.common.collect.HashBasedTable;
+import com.google.common.collect.Table;
+
+public class SimpleAclRandomizedTest extends AbstractRandomizedTest {
+
+ private int depth;
+
+ private Table<Integer, Integer, String> tree = HashBasedTable.create();
+ {
+ tree.put(0, 0, "/");
+ tree.put(1, 0, "/n1");
+ tree.put(1, 1, "/n2");
+ tree.put(2, 0, "/n1/n3");
+ tree.put(2, 1, "/n1/n4");
+ tree.put(2, 2, "/n1/n5");
+ tree.put(3, 0, "/n1/n3/n6");
+ tree.put(3, 1, "/n1/n3/n7");
+ tree.put(3, 2, "/n1/n3/n8");
+ tree.put(3, 3, "/n1/n3/n9");
+ }
+
+ protected void setupTree(Session session) throws Exception {
+ depth = 4;
+ Node n1 = session.getRootNode().addNode("n1");
+ session.getRootNode().addNode("n2");
+ Node n3 = n1.addNode("n3");
+ n1.addNode("n4");
+ n1.addNode("n5");
+ n3.addNode("n6");
+ n3.addNode("n7");
+ n3.addNode("n8");
+ n3.addNode("n9");
+ session.save();
+ }
+
+ protected void clearTree(Session session) throws Exception {
+ session.getRootNode().getNode("n1").remove();
+ session.getRootNode().getNode("n2").remove();
+ session.save();
+ }
+
+ @Test
+ public void testReadAcl() throws Exception {
+
+ setupPermission(jackrabbitWriterSession, jackrabbitPrincipal, "/", true, PrivilegeConstants.JCR_READ);
+ setupPermission(oakWriterSession, oakPrincipal, "/", true,PrivilegeConstants.JCR_READ);
+
+ for (int j = 0; j < 1; j++) {
+
+ Random r = new Random(j);
+ int operations = 1000;
+ int depthToApply;
+ int index;
+ boolean allow;
+ int principalIndex;
+
+ for (int i = 0; i < operations; i++) {
+ allow = r.nextBoolean();
+ depthToApply = r.nextInt(depth);
+ String path;
+ principalIndex = r.nextInt(jackrabbitPrincipals.size());
+
+ if (depthToApply == 0) {
+ path = "/";
+ continue;
+ } else {
+ index = r.nextInt(depthToApply + 1);
+ path = getPath(depthToApply, index);
+ }
+
+ setupPermission(jackrabbitWriterSession,
+ jackrabbitPrincipals.get(principalIndex), path, allow, PrivilegeConstants.JCR_READ);
+
+ setupPermission(oakWriterSession, oakPrincipals.get(principalIndex),
+ path, allow, PrivilegeConstants.JCR_READ);
+
+ check();
+ }
+
+ }
+ }
+
+ private String getPath(int depth, int index) throws Exception {
+ if (depth == 0) {
+ return "/";
+ }
+ return tree.get(depth, index);
+ }
+
+ public void check() throws Exception {
+ boolean mustThrow;
+ boolean thrown;
+
+ try {
+ for (String path : tree.values()) {
+ mustThrow = false;
+ thrown = false;
+ Node njr = null, noak = null;
+ try {
+ njr = jackrabbitReaderSession.getNode(path);
+ } catch (PathNotFoundException pnf) {
+ mustThrow = true;
+ }
+
+ try {
+ noak = oakReaderSession.getNode(path);
+ } catch (PathNotFoundException pnf) {
+ thrown = true;
+ }
+
+ if (mustThrow != thrown) {
+ Assert.fail("did not throw for both for path " + path);
+ }
+
+ if (!mustThrow) {
+ if (!path.equals(njr.getPath())
+ || !njr.getPath().equals(noak.getPath())) {
+ Assert.fail("did not resolved the same node");
+ }
+ }
+ }
+
+ } catch (Exception e) {
+ throw new Exception(e);
+ }
+ }
+
+}