Example #1
0
        /// <summary>
        /// Parses the decrypted content.
        /// </summary>
        /// <param name="decrypted">The input stream.</param>
        /// <param name="useGZip">Set to <c>true</c> to decompress the input stream before parsing.</param>
        /// <returns>The decrypted content.</returns>
        /// <param name="headers">The database file headers.</param>
        /// <exception cref="System.ArgumentNullException">
        /// The <paramref name="decrypted"/> or <paramref name="headers"/> parameter cannot be <c>null</c>.
        /// </exception>
        public static async Task <XDocument> ParseContent(
            IInputStream decrypted, bool useGZip, FileHeaders headers)
        {
            if (decrypted == null)
            {
                throw new ArgumentNullException("decrypted");
            }
            if (headers == null)
            {
                throw new ArgumentNullException("headers");
            }

            var deHashed = await HashedBlockFileFormat.Read(decrypted);

            var input = deHashed;

            try
            {
                if (useGZip)
                {
                    input = new GZipStream(input,
                                           CompressionMode.Decompress);
                }

                var doc = XDocument.Load(input);
                Decrypt(headers, doc);

                return(doc);
            }
            finally
            {
                deHashed.Dispose();
                input.Dispose();
            }
        }
        public async Task Should_read_correctly_formatted_stream()
        {
            using (var input = new InMemoryRandomAccessStream())
                using (var expectedData = TestFiles.Read("IO.HashedBlockStream.Content.bin"))
                {
                    await CopyData(input, "IO.HashedBlockStream.bin");

                    input.Seek(0);
                    expectedData.Seek(0);

                    using (var actualData = await HashedBlockFileFormat.Read(input))
                    {
                        Assert.Equal(expectedData.Size, (ulong)actualData.Length);

                        var actual   = new byte[1024];
                        var expected = WindowsRuntimeBuffer.Create(actual.Length);

                        while (true)
                        {
                            expected = await expectedData.ReadAsync(expected);

                            var read = await actualData.ReadAsync(actual, 0, actual.Length);

                            Assert.Equal(expected.Length, (uint)read);

                            if (read == 0)
                            {
                                break;
                            }

                            Assert.Equal(expected.ToArray(), actual.Take(read));
                        }
                    }
                }
        }
        public async Task Should_detect_truncated_stream()
        {
            using (var input = new InMemoryRandomAccessStream())
            {
                await CopyData(input, "IO.HashedBlockStream.bin");

                input.Size -= 8;

                input.Seek(0);
                await Assert.ThrowsAsync <InvalidDataException>(
                    () => HashedBlockFileFormat.Read(input));
            }
        }
        public async Task Should_detect_corrupt_data()
        {
            using (var input = new InMemoryRandomAccessStream())
            {
                await CopyData(input, "IO.HashedBlockStream.bin");

                input.Seek(200);
                await input.WriteAsync(CryptographicBuffer.GenerateRandom(8));

                input.Seek(0);
                await Assert.ThrowsAsync <InvalidDataException>(
                    () => HashedBlockFileFormat.Read(input));
            }
        }
        public async Task Should_verify_block_index()
        {
            using (var input = new InMemoryRandomAccessStream())
            {
                await CopyData(input, "IO.HashedBlockStream.bin");

                input.Seek(97390);
                var writer = new DataWriter(input);
                writer.WriteInt32(5);
                await writer.StoreAsync();

                input.Seek(0);
                await Assert.ThrowsAsync <InvalidDataException>(
                    () => HashedBlockFileFormat.Read(input));
            }
        }
        public async Task Should_detect_corrupt_block_length()
        {
            using (var input = new InMemoryRandomAccessStream())
            {
                await CopyData(input, "IO.HashedBlockStream.bin");

                input.Seek(36);

                var writer = new DataWriter(input)
                {
                    ByteOrder = ByteOrder.LittleEndian,
                };
                writer.WriteInt32(-100);
                await writer.StoreAsync();

                input.Seek(0);
                await Assert.ThrowsAsync <InvalidDataException>(
                    () => HashedBlockFileFormat.Read(input));
            }
        }