Exemple #1
0
        public void GetCRC32Name()
        {
            string name = CheckSumImplList.GetImplementationName(
                CheckSumType.CRC32);

            Assert.IsNotNull(name);
            Assert.AreEqual("CRC32", name);
        }
Exemple #2
0
        public void GetMD5Name()
        {
            string name = CheckSumImplList.GetImplementationName(
                CheckSumType.MD5);

            Assert.IsNotNull(name);
            Assert.AreEqual("MD5", name);
        }
Exemple #3
0
        public void GetSHA1Name()
        {
            string name = CheckSumImplList.GetImplementationName(
                CheckSumType.SHA1);

            Assert.IsNotNull(name);
            Assert.AreEqual("SHA-1", name);
        }
Exemple #4
0
        public void GetCRC32Imp()
        {
            ICheckSum impl = CheckSumImplList.GetImplementation(
                CheckSumType.CRC32);

            Assert.IsNotNull(impl);
            Assert.IsInstanceOf <CRC32Sum>(impl);
        }
Exemple #5
0
        public void GetMD5Imp()
        {
            ICheckSum impl = CheckSumImplList.GetImplementation(
                CheckSumType.MD5);

            Assert.IsNotNull(impl);
            Assert.IsInstanceOf <Md5Sum>(impl);
        }
Exemple #6
0
        public void GetSHA1Imp()
        {
            ICheckSum impl = CheckSumImplList.GetImplementation(
                CheckSumType.SHA1);

            Assert.IsNotNull(impl);
            Assert.IsInstanceOf <Sha1Sum>(impl);
        }
Exemple #7
0
        /// <summary>
        /// Calculate checksums.
        /// </summary>
        /// <param name="itemList">List of items for which to calculate.</param>
        /// <param name="progressInfo">Returns information about calculation
        /// progress.</param>
        public void Calculate(List <CheckSumItem> itemList,
                              ref ProgressInfo progressInfo)
        {
            ICheckSum sumImpl = CheckSumImplList.GetImplementation(_sumType);
            int       i       = 0;

            foreach (CheckSumItem ci in itemList)
            {
                progressInfo.Filename = ci.FullPath;

                // Check if fhe file is found and accessible
                try
                {
                    FileInfo fi = new FileInfo(ci.FullPath);
                    if (!fi.Exists)
                    {
                        continue;
                    }
                }
                catch
                {
                    // Ignore non-existing and non-accessible files
                    // TODO: Should we inform user?
                    continue;
                }

                try
                {
                    using (FileStream filestream = new FileStream(ci.FullPath, FileMode.Open, FileAccess.Read))
                    {
                        byte[] hash = sumImpl.Calculate(filestream);
                        filestream.Close();
                        ci.SetSum(hash);
                    }
                }
                catch (IOException)
                {
                    //TODO: Set failure status to checksum item.
                }

                // Update progress information after item completed
                i++;
                progressInfo.Now = i;

                if (progressInfo.IsStopping())
                {
                    progressInfo.ActivityEnded();
                    break;
                }
            }
        }
Exemple #8
0
        /// <summary>
        /// Verify checksums.
        /// </summary>
        /// <param name="itemList">List of items to verify.</param>
        /// <param name="progressInfo">Returns information about calculation
        /// progress.</param>
        /// <returns>
        /// true if all items succeeded the verification, false if
        /// one or more items failed verification.
        /// </returns>
        public bool Verify(List <CheckSumItem> itemList,
                           ref ProgressInfo progressInfo)
        {
            bool      verifysucceeded = true;
            ICheckSum sumImpl         = CheckSumImplList.GetImplementation(_sumType);
            int       i = 0;

            foreach (CheckSumItem ci in itemList)
            {
                progressInfo.Filename = ci.FullPath;

                // If verification finds an item which does not have checksum
                // (not calculated yet?) just ignore the item. Or maybe we
                // should calculate checksum first?
                if (ci.CheckSum == null)
                {
                    verifysucceeded = false;
                    continue;
                }

                // Check if fhe file is found and accessible
                try
                {
                    FileInfo fi = new FileInfo(ci.FullPath);
                    if (!fi.Exists)
                    {
                        verifysucceeded = false;
                        continue;
                    }
                }
                catch
                {
                    // Ignore non-existing and non-accessible files
                    // TODO: Should we inform user?
                    verifysucceeded = false;
                    continue;
                }

                bool verified = false;

                try
                {
                    using (FileStream filestream = new FileStream(ci.FullPath, FileMode.Open, FileAccess.Read))
                    {
                        verified = sumImpl.Verify(filestream, ci.CheckSum.GetAsByteArray());
                        filestream.Close();
                    }
                }
                catch (IOException)
                {
                    // TODO: Set failure status to checksum item.

                    // Let verification just fail when not implemented

                    // TODO: Need to inform the user, but how?
                    // For debug builds just throw the exception for reminder..
                    #if DEBUG
                    throw;
                    #endif
                }

                if (verified)
                {
                    ci.Verified = VerificationState.VerifyOK;
                }
                else
                {
                    ci.Verified = VerificationState.VerifyFailed;
                    if (verifysucceeded == true)
                    {
                        // Set to false when we find first failed item
                        verifysucceeded = false;
                    }
                }

                // Update progress information after item completed
                i++;
                progressInfo.Now = i;

                if (progressInfo.IsStopping())
                {
                    progressInfo.ActivityEnded();
                    break;
                }
            }
            return(verifysucceeded);
        }