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 2019/10/30 16:01:40 UTC

svn commit: r1869187 - in /jackrabbit/oak/trunk/oak-auth-external: ./ src/main/java/org/apache/jackrabbit/oak/spi/security/authentication/external/impl/principal/ src/test/java/org/apache/jackrabbit/oak/spi/security/authentication/external/impl/principal/

Author: angela
Date: Wed Oct 30 16:01:39 2019
New Revision: 1869187

URL: http://svn.apache.org/viewvc?rev=1869187&view=rev
Log:
OAK-8700 : ExternalIdentityConflictHandler prone to NPE

Added:
    jackrabbit/oak/trunk/oak-auth-external/src/test/java/org/apache/jackrabbit/oak/spi/security/authentication/external/impl/principal/ExternalIdentityConflictHandlerTest.java   (with props)
Modified:
    jackrabbit/oak/trunk/oak-auth-external/pom.xml
    jackrabbit/oak/trunk/oak-auth-external/src/main/java/org/apache/jackrabbit/oak/spi/security/authentication/external/impl/principal/ExternalIdentityConflictHandler.java

Modified: jackrabbit/oak/trunk/oak-auth-external/pom.xml
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-auth-external/pom.xml?rev=1869187&r1=1869186&r2=1869187&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-auth-external/pom.xml (original)
+++ jackrabbit/oak/trunk/oak-auth-external/pom.xml Wed Oct 30 16:01:39 2019
@@ -180,6 +180,11 @@
             <scope>test</scope>
         </dependency>
         <dependency>
+            <groupId>org.mockito</groupId>
+            <artifactId>mockito-core</artifactId>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
             <groupId>org.slf4j</groupId>
             <artifactId>jul-to-slf4j</artifactId>
             <scope>test</scope>

Modified: jackrabbit/oak/trunk/oak-auth-external/src/main/java/org/apache/jackrabbit/oak/spi/security/authentication/external/impl/principal/ExternalIdentityConflictHandler.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-auth-external/src/main/java/org/apache/jackrabbit/oak/spi/security/authentication/external/impl/principal/ExternalIdentityConflictHandler.java?rev=1869187&r1=1869186&r2=1869187&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-auth-external/src/main/java/org/apache/jackrabbit/oak/spi/security/authentication/external/impl/principal/ExternalIdentityConflictHandler.java (original)
+++ jackrabbit/oak/trunk/oak-auth-external/src/main/java/org/apache/jackrabbit/oak/spi/security/authentication/external/impl/principal/ExternalIdentityConflictHandler.java Wed Oct 30 16:01:39 2019
@@ -40,36 +40,41 @@ class ExternalIdentityConflictHandler im
 
     @NotNull
     @Override
-    public Resolution addExistingProperty(NodeBuilder parent, PropertyState ours, PropertyState theirs) {
+    public Resolution addExistingProperty(@NotNull NodeBuilder parent, @NotNull PropertyState ours, @NotNull PropertyState theirs) {
         if (ExternalIdentityConstants.REP_LAST_SYNCED.equals(ours.getName())) {
-            merge(parent, ours, theirs);
-            return Resolution.MERGED;
+            return merge(parent, ours, theirs);
         }
         return Resolution.IGNORED;
     }
 
     @NotNull
     @Override
-    public Resolution changeChangedProperty(NodeBuilder parent, PropertyState ours, PropertyState theirs,
-            PropertyState base) {
+    public Resolution changeChangedProperty(@NotNull NodeBuilder parent, @NotNull PropertyState ours, @NotNull PropertyState theirs,
+                                            @NotNull PropertyState base) {
         if (ExternalIdentityConstants.REP_LAST_SYNCED.equals(ours.getName())) {
-            merge(parent, ours, theirs);
-            return Resolution.MERGED;
+            return merge(parent, ours, theirs);
         }
         return Resolution.IGNORED;
     }
 
-    private static void merge(NodeBuilder parent, PropertyState ours, PropertyState theirs) {
+    private static Resolution merge(@NotNull NodeBuilder parent, @NotNull PropertyState ours, @NotNull PropertyState theirs) {
         Calendar o = parse(ours.getValue(Type.DATE));
         Calendar t = parse(theirs.getValue(Type.DATE));
-        Calendar v = o.before(t) ? t : o;
-        parent.setProperty(ours.getName(), v);
+        if (o != null) {
+            Calendar v = o.before(t) ? t : o;
+            parent.setProperty(ours.getName(), v);
+            return Resolution.MERGED;
+        } else if (t != null) {
+            parent.setProperty(ours.getName(), t);
+            return Resolution.MERGED;
+        } else {
+            return Resolution.IGNORED;
+        }
     }
 
     @Override
     @NotNull
-    public Resolution changeDeletedProperty(@NotNull NodeBuilder parent, @NotNull PropertyState ours,
-            @NotNull PropertyState base) {
+    public Resolution changeDeletedProperty(@NotNull NodeBuilder parent, @NotNull PropertyState ours, @NotNull PropertyState base) {
         return Resolution.IGNORED;
     }
 

Added: jackrabbit/oak/trunk/oak-auth-external/src/test/java/org/apache/jackrabbit/oak/spi/security/authentication/external/impl/principal/ExternalIdentityConflictHandlerTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-auth-external/src/test/java/org/apache/jackrabbit/oak/spi/security/authentication/external/impl/principal/ExternalIdentityConflictHandlerTest.java?rev=1869187&view=auto
==============================================================================
--- jackrabbit/oak/trunk/oak-auth-external/src/test/java/org/apache/jackrabbit/oak/spi/security/authentication/external/impl/principal/ExternalIdentityConflictHandlerTest.java (added)
+++ jackrabbit/oak/trunk/oak-auth-external/src/test/java/org/apache/jackrabbit/oak/spi/security/authentication/external/impl/principal/ExternalIdentityConflictHandlerTest.java Wed Oct 30 16:01:39 2019
@@ -0,0 +1,151 @@
+/*
+ * 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.spi.security.authentication.external.impl.principal;
+
+import org.apache.jackrabbit.oak.api.PropertyState;
+import org.apache.jackrabbit.oak.api.Type;
+import org.apache.jackrabbit.oak.spi.commit.ThreeWayConflictHandler;
+import org.apache.jackrabbit.oak.spi.state.NodeBuilder;
+import org.apache.jackrabbit.oak.spi.state.NodeState;
+import org.apache.jackrabbit.util.ISO8601;
+import org.junit.Test;
+
+import java.util.Calendar;
+
+import static org.apache.jackrabbit.oak.spi.security.authentication.external.impl.ExternalIdentityConstants.REP_LAST_SYNCED;
+import static org.junit.Assert.assertSame;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.when;
+import static org.mockito.Mockito.verify;
+
+public class ExternalIdentityConflictHandlerTest {
+
+    private ExternalIdentityConflictHandler handler = new ExternalIdentityConflictHandler();
+
+    @Test
+    public void testAddExistingProperty() {
+        assertSame(ThreeWayConflictHandler.Resolution.IGNORED, handler.addExistingProperty(mock(NodeBuilder.class), mock(PropertyState.class), mock(PropertyState.class)));
+    }
+
+    @Test
+    public void testAddExistingPropertyRepLastSynced() {
+        Calendar cal = Calendar.getInstance();
+        String calStr = ISO8601.format(cal);
+        cal.set(Calendar.YEAR, 2000);
+        String calStr2 = ISO8601.format(cal);
+
+        PropertyState ours = when(mock(PropertyState.class).getName()).thenReturn(REP_LAST_SYNCED).getMock();
+        when(ours.getValue(Type.DATE)).thenReturn(calStr);
+        PropertyState theirs = when(mock(PropertyState.class).getValue(Type.DATE)).thenReturn(calStr2).getMock();
+
+        assertSame(ThreeWayConflictHandler.Resolution.MERGED, handler.addExistingProperty(mock(NodeBuilder.class), ours, theirs));
+    }
+
+    @Test
+    public void testChangeChangedProperty() {
+        assertSame(ThreeWayConflictHandler.Resolution.IGNORED, handler.changeChangedProperty(mock(NodeBuilder.class), mock(PropertyState.class), mock(PropertyState.class), mock(PropertyState.class)));
+    }
+
+    @Test
+    public void testChangeChangedPropertyRepLastSynced() {
+        Calendar cal = Calendar.getInstance();
+        String calStr = ISO8601.format(cal);
+        cal.set(Calendar.YEAR, 2000);
+        String calStr2 = ISO8601.format(cal);
+
+        PropertyState ours = when(mock(PropertyState.class).getName()).thenReturn(REP_LAST_SYNCED).getMock();
+        when(ours.getValue(Type.DATE)).thenReturn(calStr2);
+        PropertyState theirs = when(mock(PropertyState.class).getValue(Type.DATE)).thenReturn(calStr).getMock();
+
+        assertSame(ThreeWayConflictHandler.Resolution.MERGED, handler.changeChangedProperty(mock(NodeBuilder.class), ours, theirs, mock(PropertyState.class)));
+    }
+
+    @Test
+    public void testChangeDeletedProperty() {
+        assertSame(ThreeWayConflictHandler.Resolution.IGNORED, handler.changeDeletedProperty(mock(NodeBuilder.class), mock(PropertyState.class), mock(PropertyState.class)));
+    }
+
+    @Test
+    public void testDeleteDeletedProperty() {
+        assertSame(ThreeWayConflictHandler.Resolution.IGNORED, handler.deleteDeletedProperty(mock(NodeBuilder.class), mock(PropertyState.class)));
+    }
+
+    @Test
+    public void testDeleteChangedProperty() {
+        assertSame(ThreeWayConflictHandler.Resolution.IGNORED, handler.deleteChangedProperty(mock(NodeBuilder.class), mock(PropertyState.class), mock(PropertyState.class)));
+    }
+
+    @Test
+    public void testAddExistingNode() {
+        assertSame(ThreeWayConflictHandler.Resolution.IGNORED, handler.addExistingNode(mock(NodeBuilder.class), "name", mock(NodeState.class), mock(NodeState.class)));
+    }
+
+    @Test
+    public void testChangeDeletedNode() {
+        assertSame(ThreeWayConflictHandler.Resolution.IGNORED, handler.changeDeletedNode(mock(NodeBuilder.class), "name", mock(NodeState.class), mock(NodeState.class)));
+    }
+
+    @Test
+    public void testDeleteChangedNode() {
+        assertSame(ThreeWayConflictHandler.Resolution.IGNORED, handler.deleteChangedNode(mock(NodeBuilder.class), "name", mock(NodeState.class), mock(NodeState.class)));
+    }
+
+    @Test
+    public void testDeleteDeletedNode() {
+        assertSame(ThreeWayConflictHandler.Resolution.IGNORED, handler.deleteDeletedNode(mock(NodeBuilder.class), "name", mock(NodeState.class)));
+    }
+
+    @Test
+    public void testMergeOursCannotBeParsed() {
+        Calendar cal = Calendar.getInstance();
+        String calStr = ISO8601.format(cal);
+
+        PropertyState ours = when(mock(PropertyState.class).getName()).thenReturn(REP_LAST_SYNCED).getMock();
+        when(ours.getValue(Type.DATE)).thenReturn("notParseable");
+        PropertyState theirs = when(mock(PropertyState.class).getValue(Type.DATE)).thenReturn(calStr).getMock();
+
+        NodeBuilder parent = mock(NodeBuilder.class);
+        assertSame(ThreeWayConflictHandler.Resolution.MERGED, handler.changeChangedProperty(parent, ours, theirs, mock(PropertyState.class)));
+        verify(parent, times(1)).setProperty(REP_LAST_SYNCED, ISO8601.parse(theirs.getValue(Type.DATE)));
+    }
+
+    @Test
+    public void testMergeTheirsCannotBeParsed() {
+        Calendar cal = Calendar.getInstance();
+        String calStr = ISO8601.format(cal);
+
+        PropertyState ours = when(mock(PropertyState.class).getName()).thenReturn(REP_LAST_SYNCED).getMock();
+        when(ours.getValue(Type.DATE)).thenReturn(calStr);
+        PropertyState theirs = when(mock(PropertyState.class).getValue(Type.DATE)).thenReturn("notParseable").getMock();
+
+        NodeBuilder parent = mock(NodeBuilder.class);
+        assertSame(ThreeWayConflictHandler.Resolution.MERGED, handler.changeChangedProperty(parent, ours, theirs, mock(PropertyState.class)));
+        verify(parent, times(1)).setProperty(REP_LAST_SYNCED, ISO8601.parse(ours.getValue(Type.DATE)));
+    }
+
+    @Test
+    public void testMergeNoneCannotBeParsed() {
+        PropertyState ours = when(mock(PropertyState.class).getName()).thenReturn(REP_LAST_SYNCED).getMock();
+        when(ours.getValue(Type.DATE)).thenReturn("notParseable1");
+        PropertyState theirs = when(mock(PropertyState.class).getValue(Type.DATE)).thenReturn("notParseable2").getMock();
+
+        NodeBuilder parent = mock(NodeBuilder.class);
+        assertSame(ThreeWayConflictHandler.Resolution.IGNORED, handler.changeChangedProperty(parent, ours, theirs, mock(PropertyState.class)));
+        assertSame(ThreeWayConflictHandler.Resolution.IGNORED, handler.addExistingProperty(parent, ours, theirs));
+    }
+}
\ No newline at end of file

Propchange: jackrabbit/oak/trunk/oak-auth-external/src/test/java/org/apache/jackrabbit/oak/spi/security/authentication/external/impl/principal/ExternalIdentityConflictHandlerTest.java
------------------------------------------------------------------------------
    svn:eol-style = native