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 fr...@apache.org on 2017/08/04 15:34:54 UTC

svn commit: r1804136 - in /jackrabbit/oak/trunk/oak-segment-tar/src: main/java/org/apache/jackrabbit/oak/segment/file/tar/ main/java/org/apache/jackrabbit/oak/segment/file/tar/index/ test/java/org/apache/jackrabbit/oak/segment/file/tar/index/

Author: frm
Date: Fri Aug  4 15:34:54 2017
New Revision: 1804136

URL: http://svn.apache.org/viewvc?rev=1804136&view=rev
Log:
OAK-6518 - Add tests for reading the individual index formats

Added:
    jackrabbit/oak/trunk/oak-segment-tar/src/test/java/org/apache/jackrabbit/oak/segment/file/tar/index/
    jackrabbit/oak/trunk/oak-segment-tar/src/test/java/org/apache/jackrabbit/oak/segment/file/tar/index/IndexLoaderV1Test.java   (with props)
    jackrabbit/oak/trunk/oak-segment-tar/src/test/java/org/apache/jackrabbit/oak/segment/file/tar/index/IndexLoaderV2Test.java   (with props)
    jackrabbit/oak/trunk/oak-segment-tar/src/test/java/org/apache/jackrabbit/oak/segment/file/tar/index/IndexV1Test.java   (with props)
    jackrabbit/oak/trunk/oak-segment-tar/src/test/java/org/apache/jackrabbit/oak/segment/file/tar/index/IndexV2Test.java   (with props)
Modified:
    jackrabbit/oak/trunk/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/file/tar/TarReader.java
    jackrabbit/oak/trunk/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/file/tar/index/Index.java
    jackrabbit/oak/trunk/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/file/tar/index/IndexLoaderV1.java
    jackrabbit/oak/trunk/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/file/tar/index/IndexLoaderV2.java
    jackrabbit/oak/trunk/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/file/tar/index/IndexV1.java
    jackrabbit/oak/trunk/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/file/tar/index/IndexV2.java

Modified: jackrabbit/oak/trunk/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/file/tar/TarReader.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/file/tar/TarReader.java?rev=1804136&r1=1804135&r2=1804136&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/file/tar/TarReader.java (original)
+++ jackrabbit/oak/trunk/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/file/tar/TarReader.java Fri Aug  4 15:34:54 2017
@@ -475,7 +475,7 @@ class TarReader implements Closeable {
      * otherwise.
      */
     boolean containsEntry(long msb, long lsb) {
-        return findEntry(msb, lsb) != null;
+        return findEntry(msb, lsb) != -1;
     }
 
     /**
@@ -490,10 +490,14 @@ class TarReader implements Closeable {
      * @return the byte buffer, or null if not in this file.
      */
     ByteBuffer readEntry(long msb, long lsb) throws IOException {
-        IndexEntry entry = findEntry(msb, lsb);
-        if (entry == null) {
+        int idx = findEntry(msb, lsb);
+        if (idx == -1) {
             return null;
         }
+        return readEntry(msb, lsb, index.entry(idx));
+    }
+
+    private ByteBuffer readEntry(long msb, long lsb, IndexEntry entry) throws IOException {
         return readSegment(msb, lsb, entry.getPosition(), entry.getLength());
     }
 
@@ -505,7 +509,7 @@ class TarReader implements Closeable {
      * @return The position of the entry in the TAR file, or {@code -1} if the
      * entry is not found.
      */
-    private IndexEntry findEntry(long msb, long lsb) {
+    private int findEntry(long msb, long lsb) {
         return index.findEntry(msb, lsb);
     }
 
@@ -516,7 +520,7 @@ class TarReader implements Closeable {
      */
     @Nonnull
     TarEntry[] getEntries() {
-        TarEntry[] entries = new TarEntry[index.entryCount()];
+        TarEntry[] entries = new TarEntry[index.count()];
         for (int i = 0; i < entries.length; i++) {
             IndexEntry e = index.entry(i);
             entries[i]  = new TarEntry(

Modified: jackrabbit/oak/trunk/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/file/tar/index/Index.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/file/tar/index/Index.java?rev=1804136&r1=1804135&r2=1804136&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/file/tar/index/Index.java (original)
+++ jackrabbit/oak/trunk/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/file/tar/index/Index.java Fri Aug  4 15:34:54 2017
@@ -24,11 +24,11 @@ public interface Index {
 
     Set<UUID> getUUIDs();
 
-    IndexEntry findEntry(long msb, long lsb);
+    int findEntry(long msb, long lsb);
 
     int size();
 
-    int entryCount();
+    int count();
 
     IndexEntry entry(int i);
 

Modified: jackrabbit/oak/trunk/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/file/tar/index/IndexLoaderV1.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/file/tar/index/IndexLoaderV1.java?rev=1804136&r1=1804135&r2=1804136&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/file/tar/index/IndexLoaderV1.java (original)
+++ jackrabbit/oak/trunk/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/file/tar/index/IndexLoaderV1.java Fri Aug  4 15:34:54 2017
@@ -23,6 +23,8 @@ import java.io.IOException;
 import java.nio.ByteBuffer;
 import java.util.zip.CRC32;
 
+import org.apache.commons.io.HexDump;
+
 class IndexLoaderV1 {
 
     static final int MAGIC = ('\n' << 24) + ('0' << 16) + ('K' << 8) + '\n';
@@ -33,8 +35,9 @@ class IndexLoaderV1 {
         this.blockSize = blockSize;
     }
 
-    Index loadIndex(ReaderAtEnd reader) throws InvalidIndexException, IOException {
+    IndexV1 loadIndex(ReaderAtEnd reader) throws InvalidIndexException, IOException {
         ByteBuffer meta = reader.readAtEnd(IndexV1.FOOTER_SIZE, IndexV1.FOOTER_SIZE);
+
         int crc32 = meta.getInt();
         int count = meta.getInt();
         int bytes = meta.getInt();
@@ -43,16 +46,21 @@ class IndexLoaderV1 {
         if (magic != MAGIC) {
             throw new InvalidIndexException("Magic number mismatch");
         }
-
-        if (count < 1 || bytes < count * IndexEntryV1.SIZE + IndexV1.FOOTER_SIZE || bytes % blockSize != 0) {
-            throw new InvalidIndexException("Invalid metadata");
+        if (count < 1) {
+            throw new InvalidIndexException("Invalid entry count");
+        }
+        if (bytes < count * IndexEntryV1.SIZE + IndexV1.FOOTER_SIZE) {
+            throw new InvalidIndexException("Invalid size");
+        }
+        if (bytes % blockSize != 0) {
+            throw new InvalidIndexException("Invalid size alignment");
         }
 
-        ByteBuffer index = reader.readAtEnd(IndexV1.FOOTER_SIZE + count * IndexEntryV1.SIZE, count * IndexEntryV1.SIZE);
-        index.mark();
+        ByteBuffer entries = reader.readAtEnd(IndexV1.FOOTER_SIZE + count * IndexEntryV1.SIZE, count * IndexEntryV1.SIZE);
+        entries.mark();
 
         CRC32 checksum = new CRC32();
-        checksum.update(index.array());
+        checksum.update(entries.array(), entries.position(), entries.remaining());
         if (crc32 != (int) checksum.getValue()) {
             throw new InvalidIndexException("Invalid checksum");
         }
@@ -61,7 +69,7 @@ class IndexLoaderV1 {
         long lastLsb = Long.MIN_VALUE;
         byte[] entry = new byte[IndexEntryV1.SIZE];
         for (int i = 0; i < count; i++) {
-            index.get(entry);
+            entries.get(entry);
 
             ByteBuffer buffer = wrap(entry);
             long msb = buffer.getLong();
@@ -75,9 +83,12 @@ class IndexLoaderV1 {
             if (lastMsb == msb && lastLsb == lsb && i > 0) {
                 throw new InvalidIndexException("Duplicate entry");
             }
-            if (offset < 0 || offset % blockSize != 0) {
+            if (offset < 0) {
                 throw new InvalidIndexException("Invalid entry offset");
             }
+            if (offset % blockSize != 0) {
+                throw new InvalidIndexException("Invalid entry offset alignment");
+            }
             if (size < 1) {
                 throw new InvalidIndexException("Invalid entry size");
             }
@@ -86,9 +97,9 @@ class IndexLoaderV1 {
             lastLsb = lsb;
         }
 
-        index.reset();
+        entries.reset();
 
-        return new IndexV1(index);
+        return new IndexV1(entries);
     }
 
 }

Modified: jackrabbit/oak/trunk/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/file/tar/index/IndexLoaderV2.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/file/tar/index/IndexLoaderV2.java?rev=1804136&r1=1804135&r2=1804136&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/file/tar/index/IndexLoaderV2.java (original)
+++ jackrabbit/oak/trunk/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/file/tar/index/IndexLoaderV2.java Fri Aug  4 15:34:54 2017
@@ -33,8 +33,9 @@ class IndexLoaderV2 {
         this.blockSize = blockSize;
     }
 
-    Index loadIndex(ReaderAtEnd reader) throws InvalidIndexException, IOException {
+    IndexV2 loadIndex(ReaderAtEnd reader) throws InvalidIndexException, IOException {
         ByteBuffer meta = reader.readAtEnd(IndexV2.FOOTER_SIZE, IndexV2.FOOTER_SIZE);
+
         int crc32 = meta.getInt();
         int count = meta.getInt();
         int bytes = meta.getInt();
@@ -43,17 +44,23 @@ class IndexLoaderV2 {
         if (magic != MAGIC) {
             throw new InvalidIndexException("Magic number mismatch");
         }
-
-        if (count < 1 || bytes < count * IndexEntryV2.SIZE + IndexV2.FOOTER_SIZE || bytes % blockSize != 0) {
-            throw new InvalidIndexException("Invalid metadata");
+        if (count < 1) {
+            throw new InvalidIndexException("Invalid entry count");
+        }
+        if (bytes < count * IndexEntryV1.SIZE + IndexV1.FOOTER_SIZE) {
+            throw new InvalidIndexException("Invalid size");
+        }
+        if (bytes % blockSize != 0) {
+            throw new InvalidIndexException("Invalid size alignment");
         }
 
-        ByteBuffer index = reader.readAtEnd(IndexV2.FOOTER_SIZE + count * IndexEntryV2.SIZE, count * IndexEntryV2.SIZE);
-        index.mark();
+        ByteBuffer entries = reader.readAtEnd(IndexV2.FOOTER_SIZE + count * IndexEntryV2.SIZE, count * IndexEntryV2.SIZE);
+        entries.mark();
 
         CRC32 checksum = new CRC32();
-        checksum.update(index.array());
+        checksum.update(entries.array(), entries.position(), entries.remaining());
         if (crc32 != (int) checksum.getValue()) {
+            System.out.printf("0x%X\n", (int) checksum.getValue());
             throw new InvalidIndexException("Invalid checksum");
         }
 
@@ -61,7 +68,7 @@ class IndexLoaderV2 {
         long lastLsb = Long.MIN_VALUE;
         byte[] entry = new byte[IndexEntryV2.SIZE];
         for (int i = 0; i < count; i++) {
-            index.get(entry);
+            entries.get(entry);
 
             ByteBuffer buffer = wrap(entry);
             long msb = buffer.getLong();
@@ -75,9 +82,12 @@ class IndexLoaderV2 {
             if (lastMsb == msb && lastLsb == lsb && i > 0) {
                 throw new InvalidIndexException("Duplicate entry");
             }
-            if (offset < 0 || offset % blockSize != 0) {
+            if (offset < 0) {
                 throw new InvalidIndexException("Invalid entry offset");
             }
+            if (offset % blockSize != 0) {
+                throw new InvalidIndexException("Invalid entry offset alignment");
+            }
             if (size < 1) {
                 throw new InvalidIndexException("Invalid entry size");
             }
@@ -86,9 +96,9 @@ class IndexLoaderV2 {
             lastLsb = lsb;
         }
 
-        index.reset();
+        entries.reset();
 
-        return new IndexV2(index);
+        return new IndexV2(entries);
     }
 
 }

Modified: jackrabbit/oak/trunk/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/file/tar/index/IndexV1.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/file/tar/index/IndexV1.java?rev=1804136&r1=1804135&r2=1804136&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/file/tar/index/IndexV1.java (original)
+++ jackrabbit/oak/trunk/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/file/tar/index/IndexV1.java Fri Aug  4 15:34:54 2017
@@ -17,6 +17,7 @@
 
 package org.apache.jackrabbit.oak.segment.file.tar.index;
 
+import static com.google.common.base.Preconditions.checkElementIndex;
 import static com.google.common.collect.Sets.newHashSetWithExpectedSize;
 
 import java.nio.ByteBuffer;
@@ -27,19 +28,19 @@ class IndexV1 implements Index {
 
     static final int FOOTER_SIZE = 16;
 
-    private final ByteBuffer index;
+    private final ByteBuffer entries;
 
-    IndexV1(ByteBuffer index) {
-        this.index = index;
+    IndexV1(ByteBuffer entries) {
+        this.entries = entries;
     }
 
     @Override
     public Set<UUID> getUUIDs() {
-        Set<UUID> uuids = newHashSetWithExpectedSize(index.remaining() / IndexEntryV1.SIZE);
-        int position = index.position();
-        while (position < index.limit()) {
-            long msb = index.getLong(position);
-            long lsb = index.getLong(position + 8);
+        Set<UUID> uuids = newHashSetWithExpectedSize(entries.remaining() / IndexEntryV1.SIZE);
+        int position = entries.position();
+        while (position < entries.limit()) {
+            long msb = entries.getLong(position);
+            long lsb = entries.getLong(position + 8);
             uuids.add(new UUID(msb, lsb));
             position += IndexEntryV1.SIZE;
         }
@@ -47,13 +48,13 @@ class IndexV1 implements Index {
     }
 
     @Override
-    public IndexEntryV1 findEntry(long msb, long lsb) {
+    public int findEntry(long msb, long lsb) {
         // The segment identifiers are randomly generated with uniform
         // distribution, so we can use interpolation search to find the
         // matching entry in the index. The average runtime is O(log log n).
 
         int lowIndex = 0;
-        int highIndex = index.remaining() / IndexEntryV1.SIZE - 1;
+        int highIndex = entries.remaining() / IndexEntryV1.SIZE - 1;
         float lowValue = Long.MIN_VALUE;
         float highValue = Long.MAX_VALUE;
         float targetValue = msb;
@@ -63,8 +64,8 @@ class IndexV1 implements Index {
                     (highIndex - lowIndex)
                             * (targetValue - lowValue)
                             / (highValue - lowValue));
-            int position = index.position() + guessIndex * IndexEntryV1.SIZE;
-            long m = index.getLong(position);
+            int position = entries.position() + guessIndex * IndexEntryV1.SIZE;
+            long m = entries.getLong(position);
             if (msb < m) {
                 highIndex = guessIndex - 1;
                 highValue = m;
@@ -73,7 +74,7 @@ class IndexV1 implements Index {
                 lowValue = m;
             } else {
                 // getting close...
-                long l = index.getLong(position + 8);
+                long l = entries.getLong(position + 8);
                 if (lsb < l) {
                     highIndex = guessIndex - 1;
                     highValue = m;
@@ -81,27 +82,27 @@ class IndexV1 implements Index {
                     lowIndex = guessIndex + 1;
                     lowValue = m;
                 } else {
-                    return new IndexEntryV1(index, position);
+                    return position / IndexEntryV1.SIZE;
                 }
             }
         }
 
-        return null;
+        return -1;
     }
 
     @Override
     public int size() {
-        return index.remaining() + FOOTER_SIZE;
+        return entries.remaining() + FOOTER_SIZE;
     }
 
     @Override
-    public int entryCount() {
-        return index.remaining() / IndexEntryV1.SIZE;
+    public int count() {
+        return entries.remaining() / IndexEntryV1.SIZE;
     }
 
     @Override
     public IndexEntryV1 entry(int i) {
-        return new IndexEntryV1(index, index.position() + i * IndexEntryV1.SIZE);
+        return new IndexEntryV1(entries, checkElementIndex(i, count()) * IndexEntryV1.SIZE);
     }
 
 }

Modified: jackrabbit/oak/trunk/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/file/tar/index/IndexV2.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/file/tar/index/IndexV2.java?rev=1804136&r1=1804135&r2=1804136&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/file/tar/index/IndexV2.java (original)
+++ jackrabbit/oak/trunk/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/file/tar/index/IndexV2.java Fri Aug  4 15:34:54 2017
@@ -17,29 +17,32 @@
 
 package org.apache.jackrabbit.oak.segment.file.tar.index;
 
+import static com.google.common.base.Preconditions.checkElementIndex;
 import static com.google.common.collect.Sets.newHashSetWithExpectedSize;
 
 import java.nio.ByteBuffer;
 import java.util.Set;
 import java.util.UUID;
 
+import com.google.common.base.Preconditions;
+
 class IndexV2 implements Index {
 
     static final int FOOTER_SIZE = 16;
 
-    private final ByteBuffer index;
+    private final ByteBuffer entries;
 
-    IndexV2(ByteBuffer index) {
-        this.index = index;
+    IndexV2(ByteBuffer entries) {
+        this.entries = entries;
     }
 
     @Override
     public Set<UUID> getUUIDs() {
-        Set<UUID> uuids = newHashSetWithExpectedSize(index.remaining() / IndexEntryV2.SIZE);
-        int position = index.position();
-        while (position < index.limit()) {
-            long msb = index.getLong(position);
-            long lsb = index.getLong(position + 8);
+        Set<UUID> uuids = newHashSetWithExpectedSize(entries.remaining() / IndexEntryV2.SIZE);
+        int position = entries.position();
+        while (position < entries.limit()) {
+            long msb = entries.getLong(position);
+            long lsb = entries.getLong(position + 8);
             uuids.add(new UUID(msb, lsb));
             position += IndexEntryV2.SIZE;
         }
@@ -47,13 +50,13 @@ class IndexV2 implements Index {
     }
 
     @Override
-    public IndexEntryV2 findEntry(long msb, long lsb) {
+    public int findEntry(long msb, long lsb) {
         // The segment identifiers are randomly generated with uniform
         // distribution, so we can use interpolation search to find the
         // matching entry in the index. The average runtime is O(log log n).
 
         int lowIndex = 0;
-        int highIndex = index.remaining() / IndexEntryV2.SIZE - 1;
+        int highIndex = entries.remaining() / IndexEntryV2.SIZE - 1;
         float lowValue = Long.MIN_VALUE;
         float highValue = Long.MAX_VALUE;
         float targetValue = msb;
@@ -63,8 +66,8 @@ class IndexV2 implements Index {
                     (highIndex - lowIndex)
                             * (targetValue - lowValue)
                             / (highValue - lowValue));
-            int position = index.position() + guessIndex * IndexEntryV2.SIZE;
-            long m = index.getLong(position);
+            int position = entries.position() + guessIndex * IndexEntryV2.SIZE;
+            long m = entries.getLong(position);
             if (msb < m) {
                 highIndex = guessIndex - 1;
                 highValue = m;
@@ -73,7 +76,7 @@ class IndexV2 implements Index {
                 lowValue = m;
             } else {
                 // getting close...
-                long l = index.getLong(position + 8);
+                long l = entries.getLong(position + 8);
                 if (lsb < l) {
                     highIndex = guessIndex - 1;
                     highValue = m;
@@ -81,27 +84,27 @@ class IndexV2 implements Index {
                     lowIndex = guessIndex + 1;
                     lowValue = m;
                 } else {
-                    return new IndexEntryV2(index, position);
+                    return position / IndexEntryV2.SIZE;
                 }
             }
         }
 
-        return null;
+        return -1;
     }
 
     @Override
     public int size() {
-        return index.remaining() + FOOTER_SIZE;
+        return entries.remaining() + FOOTER_SIZE;
     }
 
     @Override
-    public int entryCount() {
-        return index.remaining() / IndexEntryV2.SIZE;
+    public int count() {
+        return entries.remaining() / IndexEntryV2.SIZE;
     }
 
     @Override
     public IndexEntryV2 entry(int i) {
-        return new IndexEntryV2(index, index.position() + i * IndexEntryV2.SIZE);
+        return new IndexEntryV2(entries, checkElementIndex(i, count()) * IndexEntryV2.SIZE);
     }
 
 }

Added: jackrabbit/oak/trunk/oak-segment-tar/src/test/java/org/apache/jackrabbit/oak/segment/file/tar/index/IndexLoaderV1Test.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-segment-tar/src/test/java/org/apache/jackrabbit/oak/segment/file/tar/index/IndexLoaderV1Test.java?rev=1804136&view=auto
==============================================================================
--- jackrabbit/oak/trunk/oak-segment-tar/src/test/java/org/apache/jackrabbit/oak/segment/file/tar/index/IndexLoaderV1Test.java (added)
+++ jackrabbit/oak/trunk/oak-segment-tar/src/test/java/org/apache/jackrabbit/oak/segment/file/tar/index/IndexLoaderV1Test.java Fri Aug  4 15:34:54 2017
@@ -0,0 +1,238 @@
+/*
+ * 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.segment.file.tar.index;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+
+import java.nio.ByteBuffer;
+
+import org.junit.Test;
+
+public class IndexLoaderV1Test {
+
+    private static IndexV1 loadIndex(ByteBuffer buffer) throws Exception {
+        return loadIndex(1, buffer);
+    }
+
+    private static IndexV1 loadIndex(int blockSize, ByteBuffer buffer) throws Exception {
+        return new IndexLoaderV1(blockSize).loadIndex((whence, length) -> {
+            ByteBuffer slice = buffer.duplicate();
+            slice.position(slice.limit() - whence);
+            slice.limit(slice.position() + length);
+            return slice.slice();
+        });
+    }
+
+    @Test(expected = InvalidIndexException.class)
+    public void testInvalidMagic() throws Exception {
+        ByteBuffer buffer = ByteBuffer.allocate(IndexV1.FOOTER_SIZE);
+        try {
+            loadIndex(buffer);
+        } catch (InvalidIndexException e) {
+            assertEquals("Magic number mismatch", e.getMessage());
+            throw e;
+        }
+    }
+
+    @Test(expected = InvalidIndexException.class)
+    public void testInvalidCount() throws Exception {
+        ByteBuffer buffer = ByteBuffer.allocate(IndexV1.FOOTER_SIZE);
+        buffer.duplicate()
+                .putInt(0)
+                .putInt(0)
+                .putInt(0)
+                .putInt(IndexLoaderV1.MAGIC);
+        try {
+            loadIndex(buffer);
+        } catch (InvalidIndexException e) {
+            assertEquals("Invalid entry count", e.getMessage());
+            throw e;
+        }
+    }
+
+    @Test(expected = InvalidIndexException.class)
+    public void testInvalidSize() throws Exception {
+        ByteBuffer buffer = ByteBuffer.allocate(IndexV1.FOOTER_SIZE);
+        buffer.duplicate()
+                .putInt(0)
+                .putInt(1)
+                .putInt(0)
+                .putInt(IndexLoaderV1.MAGIC);
+        try {
+            loadIndex(buffer);
+        } catch (InvalidIndexException e) {
+            assertEquals("Invalid size", e.getMessage());
+            throw e;
+        }
+    }
+
+    @Test(expected = InvalidIndexException.class)
+    public void testInvalidSizeAlignment() throws Exception {
+        ByteBuffer buffer = ByteBuffer.allocate(IndexV1.FOOTER_SIZE);
+        buffer.duplicate()
+                .putInt(0)
+                .putInt(1)
+                .putInt(IndexEntryV1.SIZE + IndexV1.FOOTER_SIZE)
+                .putInt(IndexLoaderV1.MAGIC);
+        try {
+            loadIndex(IndexEntryV1.SIZE + IndexV1.FOOTER_SIZE + 1, buffer);
+        } catch (InvalidIndexException e) {
+            assertEquals("Invalid size alignment", e.getMessage());
+            throw e;
+        }
+    }
+
+    @Test(expected = InvalidIndexException.class)
+    public void testInvalidChecksum() throws Exception {
+        ByteBuffer buffer = ByteBuffer.allocate(IndexEntryV1.SIZE + IndexV1.FOOTER_SIZE);
+        buffer.duplicate()
+                .putLong(1).putLong(2).putInt(3).putInt(4).putInt(5)
+                .putInt(0)
+                .putInt(1)
+                .putInt(IndexEntryV1.SIZE + IndexV1.FOOTER_SIZE)
+                .putInt(IndexLoaderV1.MAGIC);
+        try {
+            loadIndex(buffer);
+        } catch (InvalidIndexException e) {
+            assertEquals("Invalid checksum", e.getMessage());
+            throw e;
+        }
+    }
+
+    @Test(expected = InvalidIndexException.class)
+    public void testIncorrectEntryOrderingByMsb() throws Exception {
+        ByteBuffer buffer = ByteBuffer.allocate(2 * IndexEntryV1.SIZE + IndexV1.FOOTER_SIZE);
+        buffer.duplicate()
+                .putLong(1).putLong(0).putInt(0).putInt(1).putInt(0)
+                .putLong(0).putLong(0).putInt(1).putInt(1).putInt(0)
+                .putInt(0x4079275A)
+                .putInt(2)
+                .putInt(2 * IndexEntryV1.SIZE + IndexV1.FOOTER_SIZE)
+                .putInt(IndexLoaderV1.MAGIC);
+        try {
+            loadIndex(buffer);
+        } catch (InvalidIndexException e) {
+            assertEquals("Incorrect entry ordering", e.getMessage());
+            throw e;
+        }
+    }
+
+    @Test(expected = InvalidIndexException.class)
+    public void testIncorrectEntryOrderingByLsb() throws Exception {
+        ByteBuffer buffer = ByteBuffer.allocate(2 * IndexEntryV1.SIZE + IndexV1.FOOTER_SIZE);
+        buffer.duplicate()
+                .putLong(0).putLong(1).putInt(0).putInt(1).putInt(0)
+                .putLong(0).putLong(0).putInt(1).putInt(1).putInt(0)
+                .putInt(0x273698E8)
+                .putInt(2)
+                .putInt(2 * IndexEntryV1.SIZE + IndexV1.FOOTER_SIZE)
+                .putInt(IndexLoaderV1.MAGIC);
+        try {
+            loadIndex(buffer);
+        } catch (InvalidIndexException e) {
+            assertEquals("Incorrect entry ordering", e.getMessage());
+            throw e;
+        }
+    }
+
+    @Test(expected = InvalidIndexException.class)
+    public void testDuplicateEntry() throws Exception {
+        ByteBuffer buffer = ByteBuffer.allocate(2 * IndexEntryV1.SIZE + IndexV1.FOOTER_SIZE);
+        buffer.duplicate()
+                .putLong(0).putLong(0).putInt(0).putInt(1).putInt(0)
+                .putLong(0).putLong(0).putInt(1).putInt(1).putInt(0)
+                .putInt(0xCF210849)
+                .putInt(2)
+                .putInt(2 * IndexEntryV1.SIZE + IndexV1.FOOTER_SIZE)
+                .putInt(IndexLoaderV1.MAGIC);
+        try {
+            loadIndex(buffer);
+        } catch (InvalidIndexException e) {
+            assertEquals("Duplicate entry", e.getMessage());
+            throw e;
+        }
+    }
+
+    @Test(expected = InvalidIndexException.class)
+    public void testInvalidEntryOffset() throws Exception {
+        ByteBuffer buffer = ByteBuffer.allocate(IndexEntryV1.SIZE + IndexV1.FOOTER_SIZE);
+        buffer.duplicate()
+                .putLong(0).putLong(0).putInt(-1).putInt(1).putInt(0)
+                .putInt(0x393A67C9)
+                .putInt(1)
+                .putInt(IndexEntryV1.SIZE + IndexV1.FOOTER_SIZE)
+                .putInt(IndexLoaderV1.MAGIC);
+        try {
+            loadIndex(buffer);
+        } catch (InvalidIndexException e) {
+            assertEquals("Invalid entry offset", e.getMessage());
+            throw e;
+        }
+    }
+
+    @Test(expected = InvalidIndexException.class)
+    public void testInvalidEntryOffsetAlignment() throws Exception {
+        ByteBuffer buffer = ByteBuffer.allocate(2 * (IndexEntryV1.SIZE + IndexV1.FOOTER_SIZE));
+        ByteBuffer duplicate = buffer.duplicate();
+        duplicate.position(duplicate.limit() - IndexEntryV1.SIZE - IndexV1.FOOTER_SIZE);
+        duplicate
+                .putLong(0).putLong(0).putInt(1).putInt(1).putInt(0)
+                .putInt(0x807077E9)
+                .putInt(1)
+                .putInt(2 * (IndexEntryV1.SIZE + IndexV1.FOOTER_SIZE))
+                .putInt(IndexLoaderV1.MAGIC);
+        try {
+            loadIndex(2, buffer);
+        } catch (InvalidIndexException e) {
+            assertEquals("Invalid entry offset alignment", e.getMessage());
+            throw e;
+        }
+    }
+
+    @Test(expected = InvalidIndexException.class)
+    public void testInvalidEntrySize() throws Exception {
+        ByteBuffer buffer = ByteBuffer.allocate(IndexEntryV1.SIZE + IndexV1.FOOTER_SIZE);
+        buffer.duplicate()
+                .putLong(0).putLong(0).putInt(0).putInt(0).putInt(0)
+                .putInt(0x807077E9)
+                .putInt(1)
+                .putInt(IndexEntryV1.SIZE + IndexV1.FOOTER_SIZE)
+                .putInt(IndexLoaderV1.MAGIC);
+        try {
+            loadIndex(buffer);
+        } catch (InvalidIndexException e) {
+            assertEquals("Invalid entry size", e.getMessage());
+            throw e;
+        }
+    }
+
+    @Test
+    public void testLoadIndex() throws Exception {
+        ByteBuffer buffer = ByteBuffer.allocate(2 * IndexEntryV1.SIZE + IndexV1.FOOTER_SIZE);
+        buffer.duplicate()
+                .putLong(0).putLong(0).putInt(0).putInt(1).putInt(0)
+                .putLong(0).putLong(1).putInt(1).putInt(1).putInt(0)
+                .putInt(0x12B7D1CC)
+                .putInt(2)
+                .putInt(2 * IndexEntryV1.SIZE + IndexV1.FOOTER_SIZE)
+                .putInt(IndexLoaderV1.MAGIC);
+        assertNotNull(loadIndex(buffer));
+    }
+
+}

Propchange: jackrabbit/oak/trunk/oak-segment-tar/src/test/java/org/apache/jackrabbit/oak/segment/file/tar/index/IndexLoaderV1Test.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: jackrabbit/oak/trunk/oak-segment-tar/src/test/java/org/apache/jackrabbit/oak/segment/file/tar/index/IndexLoaderV2Test.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-segment-tar/src/test/java/org/apache/jackrabbit/oak/segment/file/tar/index/IndexLoaderV2Test.java?rev=1804136&view=auto
==============================================================================
--- jackrabbit/oak/trunk/oak-segment-tar/src/test/java/org/apache/jackrabbit/oak/segment/file/tar/index/IndexLoaderV2Test.java (added)
+++ jackrabbit/oak/trunk/oak-segment-tar/src/test/java/org/apache/jackrabbit/oak/segment/file/tar/index/IndexLoaderV2Test.java Fri Aug  4 15:34:54 2017
@@ -0,0 +1,238 @@
+/*
+ * 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.segment.file.tar.index;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+
+import java.nio.ByteBuffer;
+
+import org.junit.Test;
+
+public class IndexLoaderV2Test {
+
+    private static IndexV2 loadIndex(ByteBuffer buffer) throws Exception {
+        return loadIndex(1, buffer);
+    }
+
+    private static IndexV2 loadIndex(int blockSize, ByteBuffer buffer) throws Exception {
+        return new IndexLoaderV2(blockSize).loadIndex((whence, length) -> {
+            ByteBuffer slice = buffer.duplicate();
+            slice.position(slice.limit() - whence);
+            slice.limit(slice.position() + length);
+            return slice.slice();
+        });
+    }
+
+    @Test(expected = InvalidIndexException.class)
+    public void testInvalidMagic() throws Exception {
+        ByteBuffer buffer = ByteBuffer.allocate(IndexV2.FOOTER_SIZE);
+        try {
+            loadIndex(buffer);
+        } catch (InvalidIndexException e) {
+            assertEquals("Magic number mismatch", e.getMessage());
+            throw e;
+        }
+    }
+
+    @Test(expected = InvalidIndexException.class)
+    public void testInvalidCount() throws Exception {
+        ByteBuffer buffer = ByteBuffer.allocate(IndexV2.FOOTER_SIZE);
+        buffer.duplicate()
+                .putInt(0)
+                .putInt(0)
+                .putInt(0)
+                .putInt(IndexLoaderV2.MAGIC);
+        try {
+            loadIndex(buffer);
+        } catch (InvalidIndexException e) {
+            assertEquals("Invalid entry count", e.getMessage());
+            throw e;
+        }
+    }
+
+    @Test(expected = InvalidIndexException.class)
+    public void testInvalidSize() throws Exception {
+        ByteBuffer buffer = ByteBuffer.allocate(IndexV2.FOOTER_SIZE);
+        buffer.duplicate()
+                .putInt(0)
+                .putInt(1)
+                .putInt(0)
+                .putInt(IndexLoaderV2.MAGIC);
+        try {
+            loadIndex(buffer);
+        } catch (InvalidIndexException e) {
+            assertEquals("Invalid size", e.getMessage());
+            throw e;
+        }
+    }
+
+    @Test(expected = InvalidIndexException.class)
+    public void testInvalidSizeAlignment() throws Exception {
+        ByteBuffer buffer = ByteBuffer.allocate(IndexV2.FOOTER_SIZE);
+        buffer.duplicate()
+                .putInt(0)
+                .putInt(1)
+                .putInt(IndexEntryV2.SIZE + IndexV2.FOOTER_SIZE)
+                .putInt(IndexLoaderV2.MAGIC);
+        try {
+            loadIndex(IndexEntryV2.SIZE + IndexV2.FOOTER_SIZE + 1, buffer);
+        } catch (InvalidIndexException e) {
+            assertEquals("Invalid size alignment", e.getMessage());
+            throw e;
+        }
+    }
+
+    @Test(expected = InvalidIndexException.class)
+    public void testInvalidChecksum() throws Exception {
+        ByteBuffer buffer = ByteBuffer.allocate(IndexEntryV2.SIZE + IndexV2.FOOTER_SIZE);
+        buffer.duplicate()
+                .putLong(1).putLong(2).putInt(3).putInt(4).putInt(5).putInt(6).put((byte) 0)
+                .putInt(0)
+                .putInt(1)
+                .putInt(IndexEntryV2.SIZE + IndexV2.FOOTER_SIZE)
+                .putInt(IndexLoaderV2.MAGIC);
+        try {
+            loadIndex(buffer);
+        } catch (InvalidIndexException e) {
+            assertEquals("Invalid checksum", e.getMessage());
+            throw e;
+        }
+    }
+
+    @Test(expected = InvalidIndexException.class)
+    public void testIncorrectEntryOrderingByMsb() throws Exception {
+        ByteBuffer buffer = ByteBuffer.allocate(2 * IndexEntryV2.SIZE + IndexV2.FOOTER_SIZE);
+        buffer.duplicate()
+                .putLong(1).putLong(0).putInt(0).putInt(1).putInt(0).putInt(0).put((byte) 0)
+                .putLong(0).putLong(0).putInt(1).putInt(1).putInt(0).putInt(0).put((byte) 0)
+                .putInt(0x40ECBFA7)
+                .putInt(2)
+                .putInt(2 * IndexEntryV2.SIZE + IndexV2.FOOTER_SIZE)
+                .putInt(IndexLoaderV2.MAGIC);
+        try {
+            loadIndex(buffer);
+        } catch (InvalidIndexException e) {
+            assertEquals("Incorrect entry ordering", e.getMessage());
+            throw e;
+        }
+    }
+
+    @Test(expected = InvalidIndexException.class)
+    public void testIncorrectEntryOrderingByLsb() throws Exception {
+        ByteBuffer buffer = ByteBuffer.allocate(2 * IndexEntryV2.SIZE + IndexV2.FOOTER_SIZE);
+        buffer.duplicate()
+                .putLong(0).putLong(1).putInt(0).putInt(1).putInt(0).putInt(0).put((byte) 0)
+                .putLong(0).putLong(0).putInt(1).putInt(1).putInt(0).putInt(0).put((byte) 0)
+                .putInt(0xE39E49C5)
+                .putInt(2)
+                .putInt(2 * IndexEntryV2.SIZE + IndexV2.FOOTER_SIZE)
+                .putInt(IndexLoaderV2.MAGIC);
+        try {
+            loadIndex(buffer);
+        } catch (InvalidIndexException e) {
+            assertEquals("Incorrect entry ordering", e.getMessage());
+            throw e;
+        }
+    }
+
+    @Test(expected = InvalidIndexException.class)
+    public void testDuplicateEntry() throws Exception {
+        ByteBuffer buffer = ByteBuffer.allocate(2 * IndexEntryV2.SIZE + IndexV2.FOOTER_SIZE);
+        buffer.duplicate()
+                .putLong(0).putLong(0).putInt(0).putInt(1).putInt(0).putInt(0).put((byte) 0)
+                .putLong(0).putLong(0).putInt(1).putInt(1).putInt(0).putInt(0).put((byte) 0)
+                .putInt(0x29A0BA56)
+                .putInt(2)
+                .putInt(2 * IndexEntryV2.SIZE + IndexV2.FOOTER_SIZE)
+                .putInt(IndexLoaderV2.MAGIC);
+        try {
+            loadIndex(buffer);
+        } catch (InvalidIndexException e) {
+            assertEquals("Duplicate entry", e.getMessage());
+            throw e;
+        }
+    }
+
+    @Test(expected = InvalidIndexException.class)
+    public void testInvalidEntryOffset() throws Exception {
+        ByteBuffer buffer = ByteBuffer.allocate(IndexEntryV2.SIZE + IndexV2.FOOTER_SIZE);
+        buffer.duplicate()
+                .putLong(0).putLong(0).putInt(-1).putInt(1).putInt(0).putInt(0).put((byte) 0)
+                .putInt(0xA3AE5FF1)
+                .putInt(1)
+                .putInt(IndexEntryV2.SIZE + IndexV2.FOOTER_SIZE)
+                .putInt(IndexLoaderV2.MAGIC);
+        try {
+            loadIndex(buffer);
+        } catch (InvalidIndexException e) {
+            assertEquals("Invalid entry offset", e.getMessage());
+            throw e;
+        }
+    }
+
+    @Test(expected = InvalidIndexException.class)
+    public void testInvalidEntryOffsetAlignment() throws Exception {
+        ByteBuffer buffer = ByteBuffer.allocate(2 * (IndexEntryV2.SIZE + IndexV2.FOOTER_SIZE));
+        ByteBuffer duplicate = buffer.duplicate();
+        duplicate.position(duplicate.limit() - IndexEntryV2.SIZE - IndexV2.FOOTER_SIZE);
+        duplicate
+                .putLong(0).putLong(0).putInt(1).putInt(1).putInt(0).putInt(0).put((byte) 0)
+                .putInt(0x7A7C3A8D)
+                .putInt(1)
+                .putInt(2 * (IndexEntryV2.SIZE + IndexV2.FOOTER_SIZE))
+                .putInt(IndexLoaderV2.MAGIC);
+        try {
+            loadIndex(2, buffer);
+        } catch (InvalidIndexException e) {
+            assertEquals("Invalid entry offset alignment", e.getMessage());
+            throw e;
+        }
+    }
+
+    @Test(expected = InvalidIndexException.class)
+    public void testInvalidEntrySize() throws Exception {
+        ByteBuffer buffer = ByteBuffer.allocate(IndexEntryV2.SIZE + IndexV2.FOOTER_SIZE);
+        buffer.duplicate()
+                .putLong(0).putLong(0).putInt(0).putInt(0).putInt(0).putInt(0).put((byte) 0)
+                .putInt(0x7A7C3A8D)
+                .putInt(1)
+                .putInt(IndexEntryV2.SIZE + IndexV2.FOOTER_SIZE)
+                .putInt(IndexLoaderV2.MAGIC);
+        try {
+            loadIndex(buffer);
+        } catch (InvalidIndexException e) {
+            assertEquals("Invalid entry size", e.getMessage());
+            throw e;
+        }
+    }
+
+    @Test
+    public void testLoadIndex() throws Exception {
+        ByteBuffer buffer = ByteBuffer.allocate(2 * IndexEntryV2.SIZE + IndexV2.FOOTER_SIZE);
+        buffer.duplicate()
+                .putLong(0).putLong(0).putInt(0).putInt(1).putInt(0).putInt(0).put((byte) 0)
+                .putLong(0).putLong(1).putInt(1).putInt(1).putInt(0).putInt(0).put((byte) 0)
+                .putInt(0xC6F20CB7)
+                .putInt(2)
+                .putInt(2 * IndexEntryV2.SIZE + IndexV2.FOOTER_SIZE)
+                .putInt(IndexLoaderV2.MAGIC);
+        assertNotNull(loadIndex(buffer));
+    }
+
+}

Propchange: jackrabbit/oak/trunk/oak-segment-tar/src/test/java/org/apache/jackrabbit/oak/segment/file/tar/index/IndexLoaderV2Test.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: jackrabbit/oak/trunk/oak-segment-tar/src/test/java/org/apache/jackrabbit/oak/segment/file/tar/index/IndexV1Test.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-segment-tar/src/test/java/org/apache/jackrabbit/oak/segment/file/tar/index/IndexV1Test.java?rev=1804136&view=auto
==============================================================================
--- jackrabbit/oak/trunk/oak-segment-tar/src/test/java/org/apache/jackrabbit/oak/segment/file/tar/index/IndexV1Test.java (added)
+++ jackrabbit/oak/trunk/oak-segment-tar/src/test/java/org/apache/jackrabbit/oak/segment/file/tar/index/IndexV1Test.java Fri Aug  4 15:34:54 2017
@@ -0,0 +1,94 @@
+/*
+ * 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.segment.file.tar.index;
+
+import static org.junit.Assert.assertEquals;
+
+import java.nio.ByteBuffer;
+import java.util.HashSet;
+import java.util.Set;
+import java.util.UUID;
+
+import org.junit.Test;
+
+public class IndexV1Test {
+
+    @Test
+    public void testGetUUIDs() throws Exception {
+        ByteBuffer buffer = ByteBuffer.allocate(2 * IndexEntryV1.SIZE);
+        buffer.duplicate()
+                .putLong(1).putLong(2).putInt(3).putInt(4).putInt(5)
+                .putLong(6).putLong(7).putInt(8).putInt(9).putInt(10);
+        Set<UUID> expected = new HashSet<>();
+        expected.add(new UUID(1, 2));
+        expected.add(new UUID(6, 7));
+        assertEquals(expected, new IndexV1(buffer).getUUIDs());
+    }
+
+    @Test
+    public void testFindEntry() throws Exception {
+        ByteBuffer buffer = ByteBuffer.allocate(4 * IndexEntryV1.SIZE);
+        buffer.duplicate()
+                .putLong(1).putLong(1).putInt(0).putInt(0).putInt(0)
+                .putLong(1).putLong(3).putInt(0).putInt(0).putInt(0)
+                .putLong(3).putLong(1).putInt(0).putInt(0).putInt(0)
+                .putLong(3).putLong(3).putInt(0).putInt(0).putInt(0);
+        IndexV1 index = new IndexV1(buffer);
+        assertEquals(-1, index.findEntry(1, 0));
+        assertEquals(0, index.findEntry(1, 1));
+        assertEquals(-1, index.findEntry(1, 2));
+        assertEquals(1, index.findEntry(1, 3));
+        assertEquals(-1, index.findEntry(3, 0));
+        assertEquals(2, index.findEntry(3, 1));
+        assertEquals(-1, index.findEntry(3, 2));
+        assertEquals(3, index.findEntry(3, 3));
+    }
+
+    @Test
+    public void testSize() throws Exception {
+        ByteBuffer buffer = ByteBuffer.allocate(IndexEntryV1.SIZE);
+        buffer.duplicate()
+                .putLong(1).putLong(2).putInt(3).putInt(4).putInt(5);
+        assertEquals(IndexEntryV1.SIZE + IndexV1.FOOTER_SIZE, new IndexV1(buffer).size());
+    }
+
+    @Test
+    public void testCount() throws Exception {
+        ByteBuffer buffer = ByteBuffer.allocate(2 * IndexEntryV1.SIZE);
+        buffer.duplicate()
+                .putLong(1).putLong(2).putInt(3).putInt(4).putInt(5)
+                .putLong(6).putLong(7).putInt(8).putInt(9).putInt(10);
+        assertEquals(2, new IndexV1(buffer).count());
+    }
+
+    @Test
+    public void testEntry() throws Exception {
+        ByteBuffer buffer = ByteBuffer.allocate(IndexEntryV1.SIZE);
+        buffer.duplicate()
+                .putLong(1).putLong(2).putInt(3).putInt(4).putInt(5);
+        IndexEntryV1 entry = new IndexV1(buffer).entry(0);
+        assertEquals(1, entry.getMsb());
+        assertEquals(2, entry.getLsb());
+        assertEquals(3, entry.getPosition());
+        assertEquals(4, entry.getLength());
+        assertEquals(5, entry.getFullGeneration());
+        assertEquals(0, entry.getTailGeneration());
+        assertEquals(false, entry.isTail());
+    }
+
+}

Propchange: jackrabbit/oak/trunk/oak-segment-tar/src/test/java/org/apache/jackrabbit/oak/segment/file/tar/index/IndexV1Test.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: jackrabbit/oak/trunk/oak-segment-tar/src/test/java/org/apache/jackrabbit/oak/segment/file/tar/index/IndexV2Test.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-segment-tar/src/test/java/org/apache/jackrabbit/oak/segment/file/tar/index/IndexV2Test.java?rev=1804136&view=auto
==============================================================================
--- jackrabbit/oak/trunk/oak-segment-tar/src/test/java/org/apache/jackrabbit/oak/segment/file/tar/index/IndexV2Test.java (added)
+++ jackrabbit/oak/trunk/oak-segment-tar/src/test/java/org/apache/jackrabbit/oak/segment/file/tar/index/IndexV2Test.java Fri Aug  4 15:34:54 2017
@@ -0,0 +1,94 @@
+/*
+ * 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.segment.file.tar.index;
+
+import static org.junit.Assert.assertEquals;
+
+import java.nio.ByteBuffer;
+import java.util.HashSet;
+import java.util.Set;
+import java.util.UUID;
+
+import org.junit.Test;
+
+public class IndexV2Test {
+
+    @Test
+    public void testGetUUIDs() throws Exception {
+        ByteBuffer buffer = ByteBuffer.allocate(2 * IndexEntryV2.SIZE);
+        buffer.duplicate()
+                .putLong(1).putLong(2).putInt(3).putInt(4).putInt(5).putInt(6).put((byte) 0)
+                .putLong(7).putLong(8).putInt(9).putInt(10).putInt(11).putInt(12).put((byte) 1);
+        Set<UUID> expected = new HashSet<>();
+        expected.add(new UUID(1, 2));
+        expected.add(new UUID(7, 8));
+        assertEquals(expected, new IndexV2(buffer).getUUIDs());
+    }
+
+    @Test
+    public void testFindEntry() throws Exception {
+        ByteBuffer buffer = ByteBuffer.allocate(4 * IndexEntryV2.SIZE);
+        buffer.duplicate()
+                .putLong(1).putLong(1).putInt(0).putInt(0).putInt(0).putInt(0).put((byte) 0)
+                .putLong(1).putLong(3).putInt(0).putInt(0).putInt(0).putInt(0).put((byte) 0)
+                .putLong(3).putLong(1).putInt(0).putInt(0).putInt(0).putInt(0).put((byte) 0)
+                .putLong(3).putLong(3).putInt(0).putInt(0).putInt(0).putInt(0).put((byte) 0);
+        IndexV2 index = new IndexV2(buffer);
+        assertEquals(-1, index.findEntry(1, 0));
+        assertEquals(0, index.findEntry(1, 1));
+        assertEquals(-1, index.findEntry(1, 2));
+        assertEquals(1, index.findEntry(1, 3));
+        assertEquals(-1, index.findEntry(3, 0));
+        assertEquals(2, index.findEntry(3, 1));
+        assertEquals(-1, index.findEntry(3, 2));
+        assertEquals(3, index.findEntry(3, 3));
+    }
+
+    @Test
+    public void testSize() throws Exception {
+        ByteBuffer buffer = ByteBuffer.allocate(IndexEntryV2.SIZE);
+        buffer.duplicate()
+                .putLong(1).putLong(2).putInt(3).putInt(4).putInt(5).putInt(6).put((byte) 0);
+        assertEquals(IndexEntryV2.SIZE + IndexV2.FOOTER_SIZE, new IndexV2(buffer).size());
+    }
+
+    @Test
+    public void testCount() throws Exception {
+        ByteBuffer buffer = ByteBuffer.allocate(2 * IndexEntryV2.SIZE);
+        buffer.duplicate()
+                .putLong(1).putLong(2).putInt(3).putInt(4).putInt(5).putInt(6).put((byte) 0)
+                .putLong(7).putLong(8).putInt(9).putInt(10).putInt(11).putInt(12).put((byte) 1);
+        assertEquals(2, new IndexV2(buffer).count());
+    }
+
+    @Test
+    public void testEntry() throws Exception {
+        ByteBuffer buffer = ByteBuffer.allocate(IndexEntryV2.SIZE);
+        buffer.duplicate()
+                .putLong(1).putLong(2).putInt(3).putInt(4).putInt(5).putInt(6).put((byte) 1);
+        IndexEntryV2 entry = new IndexV2(buffer).entry(0);
+        assertEquals(1, entry.getMsb());
+        assertEquals(2, entry.getLsb());
+        assertEquals(3, entry.getPosition());
+        assertEquals(4, entry.getLength());
+        assertEquals(5, entry.getFullGeneration());
+        assertEquals(6, entry.getTailGeneration());
+        assertEquals(true, entry.isTail());
+    }
+
+}

Propchange: jackrabbit/oak/trunk/oak-segment-tar/src/test/java/org/apache/jackrabbit/oak/segment/file/tar/index/IndexV2Test.java
------------------------------------------------------------------------------
    svn:eol-style = native