예제 #1
0
        public void GetFileHash_FailsForMissingFile()
        {
            GetFileHash task = new GetFileHash
            {
                Files       = new[] { new TaskItem(Path.Combine(AppContext.BaseDirectory, "this_does_not_exist.txt")) },
                BuildEngine = _mockEngine,
            };

            task.Execute().ShouldBeFalse();
            _mockEngine.Log.ShouldContain("MSB3954");
        }
예제 #2
0
        public void GetFileHash_FailsForUnknownHashEncoding()
        {
            GetFileHash task = new GetFileHash
            {
                Files        = new[] { new TaskItem(TestBinary.LoremFilePath) },
                BuildEngine  = _mockEngine,
                HashEncoding = "blue",
            };

            task.Execute().ShouldBeFalse();
            _mockEngine.Log.ShouldContain("MSB3951");
        }
예제 #3
0
        public void GetFileHash_FailsForUnknownAlgorithmName()
        {
            GetFileHash task = new GetFileHash
            {
                Files       = new[] { new TaskItem(TestBinary.LoremFilePath) },
                BuildEngine = _mockEngine,
                Algorithm   = "BANANA",
            };

            task.Execute().ShouldBeFalse();
            _mockEngine.Log.ShouldContain("MSB3953");
        }
예제 #4
0
        public void ComputesFileChecksum(string algoritm, string hash)
        {
            var task = new GetFileHash
            {
                Files       = new[] { new TaskItem(Path.Combine(AppContext.BaseDirectory, "TestResources", "lorem.bin")) },
                BuildEngine = new MockEngine(),
                Algorithm   = algoritm,
            };

            Assert.True(task.Execute(), "Task should pass");
            Assert.Equal(hash, task.Hash);
        }
예제 #5
0
        public void GetFileHash_ComputesCorrectChecksumForOneFile(TestBinary testBinary)
        {
            GetFileHash task = new GetFileHash
            {
                Files        = new[] { new TaskItem(testBinary.FilePath) },
                BuildEngine  = _mockEngine,
                Algorithm    = testBinary.HashAlgorithm,
                HashEncoding = testBinary.HashEncoding,
            };

            task.Execute().ShouldBeTrue();
            task.Hash.ShouldBe(testBinary.FileHash);
        }
예제 #6
0
        /// <summary>
        ///     获得文件哈希值
        /// </summary>
        /// <param name="filePath"></param>
        public void CalcFileHash(string filePath)
        {
            try
            {
                //显示路径
                TbFilePath.Text = filePath;
                //显示进度条
                Pbar.Visibility = Visibility.Visible;
                Pbar.Value      = 10;

                #region 异步计算哈希值

                var md5  = string.Empty;
                var sha1 = string.Empty;

                var bgw = new BackgroundWorker();
                bgw.WorkerReportsProgress = true;

                //要异步的内容
                bgw.DoWork += delegate
                {
                    md5 = GetFileHash.GetMd5(filePath);
                    bgw.ReportProgress(50);
                    sha1 = GetFileHash.GetSha1(filePath);
                    bgw.ReportProgress(100);
                };

                //进度报告
                bgw.ProgressChanged += (s, a) => { Pbar.Value = a.ProgressPercentage; };

                //后台工作完成后
                bgw.RunWorkerCompleted += delegate
                {
                    TbMd5.Text      = md5;
                    TbSha1.Text     = sha1;
                    Pbar.Visibility = Visibility.Hidden;
                };

                bgw.RunWorkerAsync();

                #endregion
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
예제 #7
0
        public void ComputesFileChecksumInGroup(string algoritm, string hash)
        {
            var task = new GetFileHash
            {
                Files = new[]
                {
                    new TaskItem(Path.Combine(AppContext.BaseDirectory, "TestResources", "lorem.bin")),
                    new TaskItem(Path.Combine(AppContext.BaseDirectory, "TestResources", "lorem.bin")),
                },
                BuildEngine = new MockEngine(),
                Algorithm   = algoritm,
            };

            Assert.True(task.Execute(), "Task should pass");
            Assert.Equal(2, task.Items.Length);
            Assert.All(task.Items, i => Assert.Equal(hash, i.GetMetadata("FileHash")));
        }
예제 #8
0
        public void GetFileHash_ComputesCorrectChecksumForManyFiles(TestBinary testBinary)
        {
            GetFileHash task = new GetFileHash
            {
                Files = new[]
                {
                    new TaskItem(testBinary.FilePath),
                    new TaskItem(testBinary.FilePath),
                },
                BuildEngine  = _mockEngine,
                Algorithm    = testBinary.HashAlgorithm,
                HashEncoding = testBinary.HashEncoding,
            };

            task.Execute().ShouldBeTrue();
            task.Items.Length.ShouldBe(2);
            task.Items.ShouldAllBe(i => string.Equals(testBinary.FileHash, i.GetMetadata("FileHash"), StringComparison.Ordinal));
        }