Exemplo n.º 1
0
        public void TestBlockReadError()
        {
            // Create a memory stream to generate the data to work with
            var memStream = new MemoryStream();
            var writer    = new BinaryWriter(memStream);

            byte[] contents    = { 0xFF, 0xFE, 0xFD, 0xFC };
            byte[] offsetBytes = { 0, 0, 0, 0 };

            // Write some data off so the memory stream starts with an offset
            writer.Write(offsetBytes, 0, 4);

            // Block ID
            writer.Write(FileBlock.BLOCKID_NULL);
            // Block length
            writer.Write(contents.LongLength + 1); // Write a block length that is larger than the real block
            // Block version
            writer.Write((short)1);

            // Block contents
            memStream.Write(contents, 0, 4);

            // Reset memory stream position and read a block
            // We reset to just past the offset so the block offset property can be tested as well
            memStream.Position = offsetBytes.Length;

            FileBlock.FromStream(memStream, null);
        }
Exemplo n.º 2
0
        public void TestBlockRead()
        {
            // Create a memory stream to generate the data to work with
            var memStream = new MemoryStream();
            var writer    = new BinaryWriter(memStream);

            byte[] contents    = { 0xFF, 0xFE, 0xFD, 0xFC };
            byte[] offsetBytes = { 0, 0, 0, 0 };

            // Write some data off so the memory stream starts with an offset
            writer.Write(offsetBytes, 0, 4);

            // Block ID
            writer.Write(FileBlock.BLOCKID_NULL);
            // Block length
            writer.Write(contents.LongLength);
            // Block version
            writer.Write((short)1);

            // Block contents
            memStream.Write(contents, 0, 4);

            // Reset memory stream position and read a block
            // We reset to just past the offset so the block offset property can be tested as well
            memStream.Position = offsetBytes.Length;

            var block = FileBlock.FromStream(memStream, null);

            Assert.AreEqual(FileBlock.BLOCKID_NULL, block.BlockID, "The block's ID must match the data that was read off the stream");
            Assert.AreEqual(offsetBytes.Length, block.BlockOffset, "The block's length must match the block content's size in bytes");
            Assert.AreEqual(contents.Length, block.BlockLength, "The block's length must match the block content's size in bytes");
            Assert.AreEqual(1, block.BlockVersion, "The block's ID must match the data that was read off the stream");
            Assert.IsTrue(contents.SequenceEqual(block.GetBlockBuffer()), "The buffered data on the block must match the data that was read off the stream");
        }
Exemplo n.º 3
0
        public void TestBlockWrite()
        {
            // Create a memory stream to generate the data to work with
            var memStream      = new MemoryStream();
            var writeMemStream = new MemoryStream();
            var writer         = new BinaryWriter(memStream);

            byte[] contents    = { 0xFF, 0xFE, 0xFD, 0xFC };
            byte[] offsetBytes = { 0, 0, 0, 0 };

            // Write some data off so the memory stream starts with an offset
            writer.Write(offsetBytes, 0, 4);
            writeMemStream.Write(offsetBytes, 0, 4);

            // Block ID
            writer.Write(FileBlock.BLOCKID_NULL);
            // Block length
            writer.Write(contents.LongLength);
            // Block version
            writer.Write((short)1);

            // Block contents
            memStream.Write(contents, 0, 4);

            // Reset memory stream position and read a block
            // We reset to just past the offset so the block offset property can be tested as well
            memStream.Position = offsetBytes.Length;

            var block = FileBlock.FromStream(memStream, null);

            block.SaveToStream(writeMemStream);

            Assert.IsTrue(memStream.GetBuffer().SequenceEqual(writeMemStream.GetBuffer()),
                          "After writing the contents of a block back to a stream, its contents must exactly match the original contents that were read");
        }