public void Destream_can_read_streamed_blocks()
 {
     var blocks = new HashBlock[5];
     for (int index = 0; index < blocks.Length; index++)
     {
         blocks[index] = new HashBlock
                             {
                                 Checksum = (uint) index,
                                 Length = index * 2,
                                 Offset = 9223372036854775807L,
                                 Hash = new byte[16]
                             };
         new Random().NextBytes(blocks[index].Hash);
     }
     var ms = new MemoryStream();
     HashBlockStreamer.Stream(blocks, ms);
     ms.Seek(0, SeekOrigin.Begin);
     HashBlock[] newBlocks = HashBlockStreamer.Destream(ms);
     Assert.AreEqual(blocks.Length, newBlocks.Length);
     for (int index = 0; index < newBlocks.Length; index++)
     {
         HashBlock newBlock = newBlocks[index];
         HashBlock oldBlock = blocks[index];
         Assert.AreEqual(oldBlock.Checksum, newBlock.Checksum);
         Assert.AreEqual(oldBlock.Length, newBlock.Length);
         Assert.AreEqual(oldBlock.Offset, newBlock.Offset);
         Assert.IsTrue(oldBlock.Hash.SequenceEqual(newBlock.Hash));
     }
 }
Example #2
0
 public void GetDeltas_returns_two_CopyDelta_objects_and_a_middle_ByteDelta_for_interspaced_new_data()
 {
     var checksumMock = new Mock<IRollingChecksum>();
     int checksumCallNumber = 0;
     // Returns "1" for calls number 2 through 11 inclusive.
     checksumMock.Setup(x => x.Value)
         .Returns(() => (uint) (++checksumCallNumber > 1 && checksumCallNumber <= 11 ? 1 : 0));
     var hashMock = new Mock<IHashAlgorithm>();
     byte[] dummyBytes = InitializeDummyMD5Hash(hashMock);
     var gen = new DeltaGenerator(checksumMock.Object, hashMock.Object);
     int blockSize = 10;
     var magicHashBlock = new HashBlock {Checksum = 0, Hash = dummyBytes, Offset = 42, Length = 43};
     gen.Initialize(blockSize, new[] {magicHashBlock});
     var bytes = new byte[blockSize*3];
     for (int i = 0; i < bytes.Length; ++i) bytes[i] = 0;
     // Should immediately get a match for an entire block.
     // Then upon trying to match a new one get a mismatch for the next
     // 10 bytes
     IEnumerable<IDelta> deltas = gen.GetDeltas(new MemoryStream(bytes));
     Assert.AreEqual(3, deltas.Count());
     Assert.IsTrue(
         deltas.ElementAt(0) is CopyDelta &&
         deltas.ElementAt(1) is ByteDelta &&
         deltas.ElementAt(2) is CopyDelta);
     Assert.AreEqual(10, (deltas.ElementAt(1) as ByteDelta).Offset);
     Assert.AreEqual(10, (deltas.ElementAt(1) as ByteDelta).Length);
 }
 public void Stream_writes_valid_byte_length()
 {
     var blocks = new HashBlock[5];
     for (int index = 0; index < blocks.Length; index++)
     {
         blocks[index] = new HashBlock {Hash = new byte[16]};
     }
     var ms = new MemoryStream();
     HashBlockStreamer.Stream(blocks, ms);
     Assert.AreEqual(blocks.Length*32 + 4, ms.Length);
 }
Example #4
0
 public static HashBlock[] Destream(Stream inputStream)
 {
     uint count = inputStream.ReadUInt();
     var hashBlocks = new HashBlock[count];
     for (int i = 0; i < count; ++i)
     {
         hashBlocks[i] = new HashBlock {Hash = new byte[16]};
         inputStream.Read(hashBlocks[i].Hash, 0, 16);
         hashBlocks[i].Length = inputStream.ReadInt();
         hashBlocks[i].Offset = inputStream.ReadLong();
         hashBlocks[i].Checksum = inputStream.ReadUInt();
     }
     return hashBlocks;
 }
 public void Stream_writes_valid_length()
 {
     var blocks = new HashBlock[5];
     for (int index = 0; index < blocks.Length; index++)
     {
         blocks[index] = new HashBlock {Hash = new byte[16]};
     }
     var ms = new MemoryStream();
     HashBlockStreamer.Stream(blocks, ms);
     ms.Seek(0, SeekOrigin.Begin);
     var buf = new byte[4];
     ms.Read(buf, 0, 4);
     Assert.AreEqual((uint) blocks.Length, BitConverter.ToUInt32(buf, 0));
 }
Example #6
0
 public void GetDeltas_returns_three_CopyDeltas_object_when_matching_hash_is_provided_for_triple_repeat()
 {
     var checksumMock = new Mock<IRollingChecksum>();
     checksumMock.Setup(x => x.Value).Returns(0);
     var hashMock = new Mock<IHashAlgorithm>();
     byte[] dummyBytes = InitializeDummyMD5Hash(hashMock);
     var gen = new DeltaGenerator(checksumMock.Object, hashMock.Object);
     int blockSize = 10;
     var magicHashBlock = new HashBlock {Checksum = 0, Hash = dummyBytes, Offset = 42, Length = 43};
     gen.Initialize(blockSize, new[] {magicHashBlock});
     var bytes = new byte[blockSize*3];
     for (int i = 0; i < bytes.Length; ++i) bytes[i] = 0;
     IEnumerable<IDelta> deltas = gen.GetDeltas(new MemoryStream(bytes));
     Assert.AreEqual(3, deltas.Count());
     Assert.IsTrue(deltas.All(d => d is CopyDelta));
     Assert.IsTrue(deltas.All(d => (d as CopyDelta).Offset == magicHashBlock.Offset));
 }
Example #7
0
        public static HashBlock[] Destream(Stream inputStream)
        {
            uint count      = inputStream.ReadUInt();
            var  hashBlocks = new HashBlock[count];

            for (int i = 0; i < count; ++i)
            {
                hashBlocks[i] = new HashBlock {
                    Hash = new byte[16]
                };
                inputStream.Read(hashBlocks[i].Hash, 0, 16);
                hashBlocks[i].Length   = inputStream.ReadInt();
                hashBlocks[i].Offset   = inputStream.ReadLong();
                hashBlocks[i].Checksum = inputStream.ReadUInt();
            }
            return(hashBlocks);
        }
Example #8
0
 public void GetDeltas_throws_for_null_stream()
 {
     var dummyBlock =
         new HashBlock
             {
                 Checksum = 0,
                 Length = 0,
                 Offset = 0,
                 Hash = new byte[] {1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7, 8}
             };
     DeltaGenerator gen = GetValidDeltaGenerator();
     gen.Initialize(10, new[] {dummyBlock});
     gen.GetDeltas(null).Count();
 }