/*
 * 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.security.authorization.evaluation;

import org.apache.jackrabbit.JcrConstants;
import org.apache.jackrabbit.oak.api.CommitFailedException;
import org.apache.jackrabbit.oak.api.Root;
import org.apache.jackrabbit.oak.api.Tree;
import org.apache.jackrabbit.oak.spi.security.privilege.PrivilegeConstants;
import org.apache.jackrabbit.oak.util.NodeUtil;
import org.junit.Ignore;
import org.junit.Test;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

public class ShadowInvisibleContentTest extends AbstractOakCoreTest {
     
    @Test
    public void testShadowInvisibleNode() throws Exception {
        setupPermission("/a", testPrincipal, true, PrivilegeConstants.JCR_ALL);
        setupPermission("/a/b", testPrincipal, false, PrivilegeConstants.JCR_ALL);
        setupPermission("/a/b/c", testPrincipal, true, PrivilegeConstants.JCR_ALL);

        Root testRoot = getTestRoot();
        Tree a = testRoot.getTree("/a");

        // /b not visible to this session
        assertFalse(a.hasChild("b"));

        // shadow /b with transient node of the same name
        Tree b = a.addChild("b");
        assertTrue(a.hasChild("b"));
        assertFalse(b.hasChild("c"));

        try {
            testRoot.commit();
            fail();
        } catch (CommitFailedException e) {
            assertTrue(e.isAccessViolation());
        }
    }

    @Test
    public void testShadowInvisibleProperty() throws Exception {
        setupPermission("/a", testPrincipal, true, PrivilegeConstants.JCR_ALL);
        setupPermission("/a", testPrincipal, false, PrivilegeConstants.REP_READ_PROPERTIES);

        Root testRoot = getTestRoot();
        Tree a = testRoot.getTree("/a");

        // /a/x not visible to this session
        assertNull(a.getProperty("aProp"));
        assertFalse(a.hasProperty("aProp"));

        // shadow /a/x with transient property of the same name
        a.setProperty("aProp", "aValue1");
        assertNotNull(a.getProperty("aProp"));
        assertTrue(a.hasProperty("aProp"));

        // after commit() normal access control again takes over!
        testRoot.commit(); // does not fail since only read access is denied
        assertNull(a.getProperty("aProp"));
        assertFalse(a.hasProperty("aProp"));
    }

    @Test
    @Ignore // FIXME how do we handle the case where the shadowing item is the same as the shadowing item?
    public void testShadowInvisibleProperty2() throws Exception {
        setupPermission("/a", testPrincipal, true, PrivilegeConstants.JCR_ALL);
        setupPermission("/a", testPrincipal, false, PrivilegeConstants.REP_READ_PROPERTIES);

        Root testRoot = getTestRoot();
        Tree a = testRoot.getTree("/a");

        // /a/x not visible to this session
        assertNull(a.getProperty("x"));

        // shadow /a/x with transient property of the same name
        a.setProperty("x", "xValue");
        assertNotNull(a.getProperty("x"));

        try {
            testRoot.commit();
            fail();
        } catch (CommitFailedException e) {
            assertTrue(e.isAccessViolation());
        }
    }

    @Ignore("OAK-869") // FIXME: OAK-869
    @Test
    public void testAddNodeCollidingWithInvisibleNode() throws Exception {
        setupPermission("/a", testPrincipal, true, PrivilegeConstants.JCR_ALL);
        setupPermission("/a/b", testPrincipal, false, PrivilegeConstants.JCR_READ);

        Root testRoot = getTestRoot();
        Tree a = testRoot.getTree("/a");

        assertFalse(a.getChild("b").exists());
        new NodeUtil(a).addChild("b", JcrConstants.NT_UNSTRUCTURED);

        try {
            testRoot.commit();
            fail();
        } catch (CommitFailedException e) {
            assertTrue(e.isConstraintViolation());
        }
    }

}

@@ -2681,33 +2681,37 @@
 %0A%0A        // /a/
-x
+aProp
  not visible to 
@@ -2826,33 +2826,37 @@
    // shadow /a/
-x
+aProp
  with transient 
@@ -3269,115 +3269,8 @@
 est%0A
-    @Ignore // FIXME how do we handle the case where the shadowing item is the same as the shadowing item?%0A
     
@@ -3495,32 +3495,126 @@
 EAD_PROPERTIES);
+%0A        setupPermission(%22/a%22, testPrincipal, false, PrivilegeConstants.REP_ALTER_PROPERTIES);
 %0A%0A        Root t
@@ -3694,17 +3694,21 @@
   // /a/
-x
+aProp
  not vis
@@ -3754,33 +3754,82 @@
 (a.getProperty(%22
-x
+aProp%22));%0A        assertFalse(a.hasProperty(%22aProp
 %22));%0A%0A        //
@@ -3839,17 +3839,21 @@
 adow /a/
-x
+aProp
  with tr
@@ -3877,32 +3877,44 @@
 of the same name
+ *and value*
 %0A        a.setPr
@@ -3925,14 +3925,18 @@
 ty(%22
-x
+aProp
 %22, %22
-x
+a
 Valu
@@ -3981,23 +3981,26 @@
 ty(%22
-x
+aProp
 %22));%0A
-%0A
         
 try 
@@ -3999,165 +3999,306 @@
     
-try %7B%0A            testRoot.commit();%0A            fail();%0A        %7D catch (CommitFailedException e) %7B%0A            assertTrue(e.isAccessViolation());%0A        %7D
+assertTrue(a.hasProperty(%22aProp%22));%0A%0A        // after commit() normal access control again takes over%0A        testRoot.commit(); // does not fail since no changes are detected, even when write access is denied%0A        assertNull(a.getProperty(%22aProp%22));%0A        assertFalse(a.hasProperty(%22aProp%22));
 %0A   

Read access is enforced on NEW items