public void ReadHeader_CheckHashCode()
        {
            var fileName = Path.Combine(testFolder, "file.bin");
            var bytes    = new byte[] {
                0,                                 // version
                4, 0,                              // length of blocks
                1, 0, 0, 0,                        // length of source file
                0, 0, 0, 0,                        // length of source file
                1, 2, 3, 4,                        // hash code
                5, 6, 7, 8,                        // hash code
                9, 10, 11, 12,                     // hash code
                13, 14, 15, 16,                    // hash code
                0, 0, 0, 0                         // first block
            };

            File.WriteAllBytes(fileName, bytes);
            using (reader = new RsaFileReader(fileName)) {
                reader.ReadHeader();
                var expected = new byte[] {
                    1, 2, 3, 4,
                    5, 6, 7, 8,
                    9, 10, 11, 12,
                    13, 14, 15, 16
                };
                TestHelper.CheckArray(expected, reader.HashCode);
            }
        }
        public void ReadHeader_FileTooSmall()
        {
            var fileName = Path.Combine(testFolder, "file.h");
            var bytes    = new byte[26];

            File.WriteAllBytes(fileName, bytes);
            using (reader = new RsaFileReader(fileName)) {
                Assert.Throws(typeof(SourceFileException), () => reader.ReadHeader());
            }
        }
        public void ReadHeader_CheckVersion()
        {
            var fileName = Path.Combine(testFolder, "file.bin");
            var bytes    = new byte[27];

            bytes[0] = 12;
            File.WriteAllBytes(fileName, bytes);
            using (reader = new RsaFileReader(fileName)) {
                reader.ReadHeader();
                Assert.AreEqual(12, reader.Version);
            }
        }
        public void ReadHeader_CheckBlockLength()
        {
            var fileName = Path.Combine(testFolder, "file.bin");
            var bytes    = new byte[27];

            bytes[1] = 23;
            bytes[2] = 1;
            File.WriteAllBytes(fileName, bytes);
            using (reader = new RsaFileReader(fileName)) {
                reader.ReadHeader();
                Assert.AreEqual(279, reader.BlockLength);
            }
        }
        public void ReadNextBlock_ZeroBeforeNumber()
        {
            var fileName = Path.Combine(testFolder, "file.bin");
            var bytes    = new byte[29];

            bytes[1]  = 2;
            bytes[27] = 45;
            bytes[28] = 0;
            File.WriteAllBytes(fileName, bytes);
            using (reader = new RsaFileReader(fileName)) {
                reader.ReadHeader();
                var number         = reader.ReadNextBlock();
                var expectedNumber = BigNumber.FromBytes(new[] { 45 });
                Assert.AreEqual(CompareResult.Equal, expectedNumber.Compare(number));
            }
        }
        public void ReadNextBlock_BlockLengthIs5()
        {
            var fileName = Path.Combine(testFolder, "file.bin");
            var bytes    = new byte[32];

            bytes[1]  = 5;
            bytes[27] = 23;
            bytes[28] = 67;
            bytes[29] = 12;
            bytes[30] = 149;
            bytes[31] = 253;
            File.WriteAllBytes(fileName, bytes);
            using (reader = new RsaFileReader(fileName)) {
                reader.ReadHeader();
                var number         = reader.ReadNextBlock();
                var expectedNumber = BigNumber.FromBytes(new[] { 23, 67, 12, 149, 253 });
                Assert.AreEqual(CompareResult.Equal, expectedNumber.Compare(number));
            }
        }
        public void ReadHeader_CheckSourceFileLength()
        {
            var fileName = Path.Combine(testFolder, "file.bin");
            var bytes    = new byte[27];

            // 10 FF FF FF FF FF FF FF
            bytes[3] = 255;
            bytes[4] = 255;
            bytes[5] = 255;
            bytes[6] = 255;

            bytes[7]  = 255;
            bytes[8]  = 255;
            bytes[9]  = 255;
            bytes[10] = 16;
            File.WriteAllBytes(fileName, bytes);
            using (reader = new RsaFileReader(fileName)) {
                reader.ReadHeader();
                Assert.AreEqual(1224979098644774911, reader.SourceFileLength);
            }
        }
        public void ReadNextBlock_BlockContainsOnlyZeros()
        {
            var fileName = Path.Combine(testFolder, "file.bin");
            var bytes    = new byte[] {
                0,                                 // version
                4, 0,                              // length of blocks
                1, 0, 0, 0,                        // length of source file
                0, 0, 0, 0,                        // length of source file
                0, 0, 0, 0,                        // hash code
                0, 0, 0, 0,                        // hash code
                0, 0, 0, 0,                        // hash code
                0, 0, 0, 0,                        // hash code
                0, 0, 0, 0                         // first block
            };

            File.WriteAllBytes(fileName, bytes);
            using (reader = new RsaFileReader(fileName)) {
                reader.ReadHeader();
                var number = reader.ReadNextBlock();
                Assert.IsTrue(number.IsZero);
            }
        }