You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@sling.apache.org by GitBox <gi...@apache.org> on 2018/02/16 15:57:10 UTC

[GitHub] cziegeler closed pull request #3: SLING-7502 Resource Digest Calculation returns different values for the same data

cziegeler closed pull request #3: SLING-7502 Resource Digest Calculation returns different values for the same data
URL: https://github.com/apache/sling-org-apache-sling-installer-core/pull/3
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/src/main/java/org/apache/sling/installer/core/impl/FileDataStore.java b/src/main/java/org/apache/sling/installer/core/impl/FileDataStore.java
index f7e2990..3b5242a 100644
--- a/src/main/java/org/apache/sling/installer/core/impl/FileDataStore.java
+++ b/src/main/java/org/apache/sling/installer/core/impl/FileDataStore.java
@@ -29,6 +29,7 @@
 import java.io.OutputStream;
 import java.math.BigInteger;
 import java.security.MessageDigest;
+import java.util.Collection;
 import java.util.Dictionary;
 import java.util.Enumeration;
 import java.util.HashMap;
@@ -46,7 +47,7 @@
 public class FileDataStore {
 
     private static final Logger log = LoggerFactory.getLogger(FileDataStore.class);
-    
+
     /**
      * The name of the bundle context property defining the location for the
      * installer files (value is "sling.installer.dir").
@@ -159,7 +160,7 @@ public File createNewDataFile(final InputStream stream,
                 final CacheEntry storedDigest = this.digestCache.get(url);
                 if ( storedDigest != null && storedDigest.digest.equals(digest) ) {
                     log.debug(
-                            "File {} with digest {} found, returning {}", 
+                            "File {} with digest {} found, returning {}",
                             url, digest, safePath(storedDigest.file));
                     return storedDigest.file;
                 }
@@ -280,12 +281,7 @@ public static String computeDigest(Dictionary<String, Object> data) {
             }
             for(final String key : sortedKeys) {
                 oos.writeObject(key);
-                final Object val = data.get(key);
-                if ( val instanceof Number ) {
-                    oos.writeObject(String.valueOf(val));
-                } else {
-                    oos.writeObject(val);
-                }
+                writeValue(data.get(key), oos);
             }
 
             oos.flush();
@@ -299,7 +295,21 @@ public static String computeDigest(Dictionary<String, Object> data) {
             return result;
         }
     }
-    
+
+    private static void writeValue(final Object val, final ObjectOutputStream oos) throws IOException {
+        if ( val instanceof Number ) {
+            writeValue(String.valueOf(val), oos);
+        } else if ( val instanceof Collection ) {
+            for (Object o : (Collection<?>) val) {
+                writeValue(o, oos);
+            }
+        } else if ( val instanceof String ) {
+            oos.writeUTF((String) val);
+        } else {
+            oos.writeObject(val);
+        }
+    }
+
     private static final String safePath(File f) {
         return f == null ? null : f.getAbsolutePath();
     }
diff --git a/src/test/java/org/apache/sling/installer/core/impl/FileDataStoreTest.java b/src/test/java/org/apache/sling/installer/core/impl/FileDataStoreTest.java
new file mode 100644
index 0000000..3c00352
--- /dev/null
+++ b/src/test/java/org/apache/sling/installer/core/impl/FileDataStoreTest.java
@@ -0,0 +1,92 @@
+/*
+ * 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.sling.installer.core.impl;
+
+import java.util.ArrayList;
+import java.util.Dictionary;
+import java.util.Hashtable;
+import java.util.LinkedList;
+import java.util.List;
+
+import org.junit.Test;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotEquals;
+
+public class FileDataStoreTest {
+    @Test
+    public void testDigestFromDictionary() {
+        Dictionary<String,Object> dict1 = new Hashtable<>();
+        List<String> l1 = new ArrayList<>();
+        l1.add("x");
+        l1.add("y");
+        dict1.put("la", l1);
+        dict1.put("foo", new int[] {3, 2, 7});
+        String dig1 = FileDataStore.computeDigest(dict1);
+
+        Dictionary<String,Object> dict2 = new Hashtable<>();
+        List<String> l2 = new LinkedList<>();
+        l2.add("x");
+        l2.add("y");
+        dict2.put("la", l2);
+        dict2.put("foo", new int[] {3, 2, 7});
+        String dig2 = FileDataStore.computeDigest(dict2);
+        assertEquals(dig1, dig2);
+    }
+
+    @Test
+    public void testDigestFromDictionaryNegative1() {
+        Dictionary<String,Object> dict1 = new Hashtable<>();
+        List<String> l1 = new ArrayList<>();
+        l1.add("y");
+        l1.add("x");
+        dict1.put("la", l1);
+        dict1.put("foo", new int[] {3, 2, 7});
+        String dig1 = FileDataStore.computeDigest(dict1);
+
+        Dictionary<String,Object> dict2 = new Hashtable<>();
+        List<String> l2 = new LinkedList<>();
+        l2.add("x");
+        l2.add("y");
+        dict2.put("la", l2);
+        dict2.put("foo", new int[] {3, 2, 7});
+        String dig2 = FileDataStore.computeDigest(dict2);
+        assertNotEquals(dig1, dig2);
+    }
+
+    @Test
+    public void testDigestFromDictionaryNegative2() {
+        Dictionary<String,Object> dict1 = new Hashtable<>();
+        List<String> l1 = new ArrayList<>();
+        l1.add("x");
+        l1.add("y");
+        dict1.put("la", l1);
+        dict1.put("foo", new int[] {7, 2, 3});
+        String dig1 = FileDataStore.computeDigest(dict1);
+
+        Dictionary<String,Object> dict2 = new Hashtable<>();
+        List<String> l2 = new LinkedList<>();
+        l2.add("x");
+        l2.add("y");
+        dict2.put("la", l2);
+        dict2.put("foo", new int[] {3, 2, 7});
+        String dig2 = FileDataStore.computeDigest(dict2);
+        assertNotEquals(dig1, dig2);
+    }
+}


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services