Пример #1
0
        /// <summary>
        ///     Executes the task to validate strong name signature.
        /// </summary>
        /// <returns>
        ///     True if validation succeeded; otherwise False.
        /// </returns>
        public override bool Execute()
        {
            try
            {
                var utility = new StrongNameUtility();
                if (!utility.ValidateStrongNameToolExistance(SdkPath))
                {
                    Log.LogError(
                        "The strong name tool (sn.exe) could not be located within the provided SDK directory ({0})).",
                        SdkPath);
                    return(false);
                }

                string path = Assembly.ItemSpec;

                // Check the public key token of the assembly.
                // -q -T: Display token for public key.
                string output;
                string arguments = "-q -T \"" + path + "\"";
                bool   success   = utility.Execute(arguments, out output);

                if (!success)
                {
                    Log.LogError("The assembly \"" + path + "\" has not been strong named signed.");
                    Log.LogError(output);

                    return(false);
                }

                // Read the public key token.
                int lastSpace = output.LastIndexOf(' ');
                if (lastSpace >= 0)
                {
                    output = output.Substring(lastSpace + 1).Trim();
                }

                if (output != ExpectedTokenSignature)
                {
                    Log.LogError(
                        "The assembly \"{0}\" had the strong name token of \"{1}\", but was expected to have the token \"{2}\"",
                        path,
                        output,
                        ExpectedTokenSignature);
                    return(false);
                }

                Log.LogMessage("The assembly \"{0}\" had the expected strong name token of \"{1}\"",
                               path,
                               output);

                // Validate that it is or is not delay signed.
                // -q -v[f]: Verify <assembly> for strong name signature self
                // consistency. If -vf is specified, force verification even if
                // disabled in the registry.
                output    = null;
                arguments = "-q -vf \"" + path + "\"";
                success   = utility.Execute(arguments, out output);

                success = (success == (!ExpectedDelaySigned));

                string message;
                if (ExpectedDelaySigned && success || !ExpectedDelaySigned && !success)
                {
                    message = "The assembly \"{0}\" was delay signed.";
                }
                else if (ExpectedDelaySigned && !success)
                {
                    message = "The assembly \"{0}\" was not delay signed.";
                }
                else
                {
                    message = "The assembly \"{0}\" has been fully signed.";
                }

                if (success)
                {
                    Log.LogMessage(MessageImportance.High, message, path);
                }
                else
                {
                    Log.LogError(message, path);
                }

                return(success);
            }
            catch (Exception ex)
            {
                Log.LogErrorFromException(ex);
                return(false);
            }
        }
        /// <summary>
        ///     Executes the task to validate strong name signature. 
        /// </summary>
        /// <returns>
        ///     True if validation succeeded; otherwise False.
        /// </returns>
        public override bool Execute()
        {
            try
            {
                var utility = new StrongNameUtility();
                if (!utility.ValidateStrongNameToolExistance(SdkPath))
                {
                    Log.LogError(
                        "The strong name tool (sn.exe) could not be located within the provided SDK directory ({0})).",
                        SdkPath);
                    return false;
                }

                string path = Assembly.ItemSpec;

                // Check the public key token of the assembly.
                // -q -T: Display token for public key.
                string output;
                string arguments = "-q -T \"" + path + "\"";
                bool success = utility.Execute(arguments, out output);

                if (!success)
                {
                    Log.LogError("The assembly \"" + path + "\" has not been strong named signed.");
                    Log.LogError(output);

                    return false;
                }

                // Read the public key token.
                int lastSpace = output.LastIndexOf(' ');
                if (lastSpace >= 0)
                {
                    output = output.Substring(lastSpace + 1).Trim();
                }

                if (output != ExpectedTokenSignature)
                {
                    Log.LogError(
                        "The assembly \"{0}\" had the strong name token of \"{1}\", but was expected to have the token \"{2}\"",
                        path,
                        output,
                        ExpectedTokenSignature);
                    return false;
                }

                Log.LogMessage("The assembly \"{0}\" had the expected strong name token of \"{1}\"",
                    path,
                    output);

                // Validate that it is or is not delay signed.
                // -q -v[f]: Verify <assembly> for strong name signature self 
                // consistency. If -vf is specified, force verification even if
                // disabled in the registry.
                output = null;
                arguments = "-q -vf \"" + path + "\"";
                success = utility.Execute(arguments, out output);

                success = (success == (!ExpectedDelaySigned));

                string message;
                if (ExpectedDelaySigned && success || !ExpectedDelaySigned && !success)
                {
                    message = "The assembly \"{0}\" was delay signed.";
                }
                else if (ExpectedDelaySigned && !success)
                {
                    message = "The assembly \"{0}\" was not delay signed.";
                }
                else
                {
                    message = "The assembly \"{0}\" has been fully signed.";
                }

                if (success)
                {
                    Log.LogMessage(MessageImportance.High, message, path);
                }
                else
                {
                    Log.LogError(message, path);
                }

                return success;
            }
            catch (Exception ex)
            {
                Log.LogErrorFromException(ex);
                return false;
            }
        }