@Test public void testHashInner() { byte[] val = DUMMY; byte[] lchildval = hashed(val, 3, 3, 2); byte[] rchildval = hashed(val, 2, 2); byte[] fullval = hashed(val, 3, 3, 2, 2, 2); Range<Token> full = new Range<>(tok(-1), tok(-1)); Range<Token> lchild = new Range<>(tok(-1), tok(4)); Range<Token> rchild = new Range<>(tok(4), tok(-1)); Range<Token> invalid = new Range<>(tok(1), tok(-1)); // (zero,one] (one, two] (two,four] (four, six] (six, zero] mts.split(tok(4)); mts.split(tok(2)); mts.split(tok(6)); mts.split(tok(1)); assertNull(mts.hash(full)); assertNull(mts.hash(lchild)); assertNull(mts.hash(rchild)); assertNull(mts.hash(invalid)); // validate the range mts.get(tok(1)).hash(val); mts.get(tok(2)).hash(val); mts.get(tok(4)).hash(val); mts.get(tok(6)).hash(val); mts.get(tok(-1)).hash(val); assertHashEquals(fullval, mts.hash(full)); assertHashEquals(lchildval, mts.hash(lchild)); assertHashEquals(rchildval, mts.hash(rchild)); assertNull(mts.hash(invalid)); }
@Test public void testHashDegenerate() { TOKEN_SCALE = new BigInteger("32"); byte[] val = DUMMY; byte[] childfullval = hashed(val, 5, 5, 4); byte[] fullval = hashed(val, 5, 5, 4, 3, 2, 1); Range<Token> childfull = new Range<>(tok(-1), tok(4)); Range<Token> full = new Range<>(tok(-1), tok(-1)); Range<Token> invalid = new Range<>(tok(4), tok(-1)); mts.split(tok(16)); mts.split(tok(8)); mts.split(tok(4)); mts.split(tok(2)); mts.split(tok(1)); assertNull(mts.hash(full)); assertNull(mts.hash(childfull)); assertNull(mts.hash(invalid)); // validate the range mts.get(tok(1)).hash(val); mts.get(tok(2)).hash(val); mts.get(tok(4)).hash(val); mts.get(tok(8)).hash(val); mts.get(tok(16)).hash(val); mts.get(tok(-1)).hash(val); assertHashEquals(fullval, mts.hash(full)); assertHashEquals(childfullval, mts.hash(childfull)); assertNull(mts.hash(invalid)); }
@Test public void testHashPartial() { byte[] val = DUMMY; byte[] leftval = hashed(val, 1, 1); byte[] partialval = hashed(val, 1); Range<Token> left = new Range<>(tok(-1), tok(4)); Range<Token> partial = new Range<>(tok(2), tok(4)); Range<Token> right = new Range<>(tok(4), tok(-1)); Range<Token> linvalid = new Range<>(tok(1), tok(4)); Range<Token> rinvalid = new Range<>(tok(4), tok(6)); // (zero,two] (two,four] (four, zero] mts.split(tok(4)); mts.split(tok(2)); assertNull(mts.hash(left)); assertNull(mts.hash(partial)); assertNull(mts.hash(right)); assertNull(mts.hash(linvalid)); assertNull(mts.hash(rinvalid)); // validate the range mts.get(tok(2)).hash(val); mts.get(tok(4)).hash(val); mts.get(tok(-1)).hash(val); assertHashEquals(leftval, mts.hash(left)); assertHashEquals(partialval, mts.hash(partial)); assertHashEquals(val, mts.hash(right)); assertNull(mts.hash(linvalid)); assertNull(mts.hash(rinvalid)); }
@Test public void testSplitLimitSize() { mts = new MerkleTrees(partitioner); mts.addMerkleTree(2, fullRange()); assertTrue(mts.split(tok(4))); assertEquals(2, mts.size()); // should fail to split above maxsize assertFalse(mts.split(tok(2))); assertEquals(2, mts.size()); assertEquals(new Range<>(tok(4), tok(-1)), mts.get(tok(-1))); assertEquals(new Range<>(tok(-1), tok(4)), mts.get(tok(4))); }
@Test public void testSplitLimitDepth() { mts = new MerkleTrees(partitioner); mts.addMerkleTree(Integer.MAX_VALUE, (byte) 2, fullRange()); assertTrue(mts.split(tok(4))); assertTrue(mts.split(tok(2))); assertEquals(3, mts.size()); // should fail to split below hashdepth assertFalse(mts.split(tok(1))); assertEquals(3, mts.size()); assertEquals(new Range<>(tok(4), tok(-1)), mts.get(tok(-1))); assertEquals(new Range<>(tok(-1), tok(2)), mts.get(tok(2))); assertEquals(new Range<>(tok(2), tok(4)), mts.get(tok(4))); }
@Test public void testSplit() { // split the range (zero, zero] into: // (zero,four], (four,six], (six,seven] and (seven, zero] mts.split(tok(4)); mts.split(tok(6)); mts.split(tok(7)); assertEquals(4, mts.size()); assertEquals(new Range<>(tok(7), tok(-1)), mts.get(tok(-1))); assertEquals(new Range<>(tok(-1), tok(4)), mts.get(tok(3))); assertEquals(new Range<>(tok(-1), tok(4)), mts.get(tok(4))); assertEquals(new Range<>(tok(4), tok(6)), mts.get(tok(6))); assertEquals(new Range<>(tok(6), tok(7)), mts.get(tok(7))); // check depths assertEquals((byte) 1, mts.get(tok(4)).depth); assertEquals((byte) 2, mts.get(tok(6)).depth); assertEquals((byte) 3, mts.get(tok(7)).depth); assertEquals((byte) 3, mts.get(tok(-1)).depth); try { mts.split(tok(-1)); fail("Shouldn't be able to split outside the initial range."); } catch (AssertionError e) { // pass } }
@Test public void testHashFull() { byte[] val = DUMMY; Range<Token> range = new Range<>(tok(-1), tok(-1)); // (zero, zero] assertNull(mts.hash(range)); // validate the range mts.get(tok(-1)).hash(val); assertHashEquals(val, mts.hash(range)); }
@Test public void testDifference() { int maxsize = 16; mts = new MerkleTrees(partitioner); mts.addMerkleTree(32, fullRange()); MerkleTrees mts2 = new MerkleTrees(partitioner); mts2.addMerkleTree(32, fullRange()); mts.init(); mts2.init(); // add dummy hashes to both trees for (TreeRange range : mts.invalids()) range.addAll(new HIterator(range.right)); for (TreeRange range : mts2.invalids()) range.addAll(new HIterator(range.right)); TreeRange leftmost = null; TreeRange middle = null; mts.maxsize(fullRange(), maxsize + 2); // give some room for splitting // split the leftmost Iterator<TreeRange> ranges = mts.invalids(); leftmost = ranges.next(); mts.split(leftmost.right); // set the hashes for the leaf of the created split middle = mts.get(leftmost.right); middle.hash("arbitrary!".getBytes()); mts.get(partitioner.midpoint(leftmost.left, leftmost.right)) .hash("even more arbitrary!".getBytes()); // trees should disagree for (leftmost.left, middle.right] List<Range<Token>> diffs = MerkleTrees.difference(mts, mts2); assertEquals(diffs + " contains wrong number of differences:", 1, diffs.size()); assertTrue(diffs.contains(new Range<>(leftmost.left, middle.right))); }