/// <summary>
        /// Executes the task to validate the strong name information for the
        /// assembly using the input values expected by the task.
        /// </summary>
        /// <returns>Returns a value indicating whether the task has been
        /// successful and the build should continue.</returns>
        public override bool Execute()
        {
            try
            {
                StrongNameUtility utility = new StrongNameUtility();
                if (!utility.ValidateStrongNameToolExistance(WindowsSdkPath))
                {
                    Log.LogError("The strong name tool (sn.exe) could not be located within the Windows SDK directory structure ({0})).", WindowsSdkPath);
                    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;
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Executes the task to validate the strong name information for the
        /// assembly using the input values expected by the task.
        /// </summary>
        /// <returns>Returns a value indicating whether the task has been
        /// successful and the build should continue.</returns>
        public override bool Execute()
        {
            try
            {
                StrongNameUtility utility = new StrongNameUtility();
                if (!utility.ValidateStrongNameToolExistance(WindowsSdkPath))
                {
                    Log.LogError("The strong name tool (sn.exe) could not be located within the Windows SDK directory structure ({0})).", WindowsSdkPath);
                    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);
            }
        }