Exemple #1
0
        public async Task TestHashCalculationAsync()
        {
            FileInfo fileInfo = new FileInfo(GetType().Assembly.Location);

            byte[] hash;
            using (FileStream fileStream = fileInfo.OpenRead())
                using (HashAlgorithm hashCalculator = HashAlgorithm.Create(HashAlgorithmName.Name))
                {
                    hash = hashCalculator.ComputeHash(fileStream);
                }

            HashStream hashStream;

            using (FileStream fileStream = fileInfo.OpenRead())
                using (RandomReadNumberStream randomReadStream = new RandomReadNumberStream(fileStream))
                    using (hashStream = new HashStream(randomReadStream, CryptoStreamMode.Read, HashAlgorithmName))
                    {
                        await hashStream.CopyToAsync(Stream.Null);
                    }
            Assert.AreEqual(hash, hashStream.HashResult.HashBytes);

            using (FileStream fileStream = fileInfo.OpenRead())
                using (RandomReadNumberStream randomReadStream = new RandomReadNumberStream(fileStream))
                    using (hashStream = new HashStream(Stream.Null, CryptoStreamMode.Write, HashAlgorithmName))
                    {
                        await randomReadStream.CopyToAsync(hashStream);
                    }
            Assert.AreEqual(hash, hashStream.HashResult.HashBytes);
        }
Exemple #2
0
        public async Task TestWriteAsync()
        {
            byte[] wrapContent, plainContent;
            using (FileStream fileStream = readFileInfo.OpenRead())
                using (RandomReadNumberStream randomReadNumberStream = new RandomReadNumberStream(fileStream))
                {
                    using (MemoryStream memoryStream = new MemoryStream())
                        using (WrapStream wrapStream = new WrapStreamImpl(memoryStream))
                        {
                            await randomReadNumberStream.CopyToAsync(wrapStream);

                            wrapContent = memoryStream.ToArray();
                        }

                    fileStream.Seek(0, SeekOrigin.Begin);
                    using (MemoryStream memoryStream = new MemoryStream())
                    {
                        await randomReadNumberStream.CopyToAsync(memoryStream);

                        plainContent = memoryStream.ToArray();
                    }
                }

            Assert.AreEqual(plainContent, wrapContent);
        }
Exemple #3
0
        public void TestRead()
        {
            using (FileStream fileStream = readFileInfo.OpenRead())
                using (RandomReadNumberStream randomReadNumberStream = new RandomReadNumberStream(fileStream))
                    using (WrapStream wrapStream = new WrapStreamImpl(randomReadNumberStream))
                    {
                        byte[] wrapContent = wrapStream.ReadToEnd();
                        fileStream.Seek(0, SeekOrigin.Begin);
                        byte[] fileContent = fileStream.ReadToEnd();

                        Assert.AreEqual(wrapContent, fileContent);
                    }
        }
Exemple #4
0
        public async Task TestReadAsync()
        {
            using (FileStream fileStream = readFileInfo.OpenRead())
                using (RandomReadNumberStream randomReadNumberStream = new RandomReadNumberStream(fileStream))
                    using (WrapStream wrapStream = new WrapStreamImpl(randomReadNumberStream))
                    {
                        byte[] wrapContent = await wrapStream.ReadToEndAsync();

                        fileStream.Seek(0, SeekOrigin.Begin);
                        byte[] plainContent = await fileStream.ReadToEndAsync();

                        Assert.AreEqual(plainContent, wrapContent);
                    }
        }