Esempio n. 1
0
        public void GetAuthorSignRequest_InvalidKeyContainer()
        {
            var mockConsole  = new Mock <IConsole>();
            var timestampUri = "http://timestamp.test/url";

            // Arrange
            using (var dir = TestDirectory.Create())
                using (var test = new MSSignCommandTestContext(_trustedTestCertWithoutPrivateKey.TrustedCert, exportPfx: false))
                {
                    var signCommand = new MSSignCommand
                    {
                        Console                = mockConsole.Object,
                        Timestamper            = timestampUri,
                        CertificateFile        = test.CertificatePath,
                        CSPName                = test.CertificateCSPName,
                        KeyContainer           = "invalid-key-container",
                        CertificateFingerprint = test.Cert.Thumbprint,
                    };
                    signCommand.Arguments.Add(Path.Combine(dir, "package.nupkg"));

                    // Act & Assert
                    var ex = Assert.Throws <InvalidOperationException>(() => signCommand.GetAuthorSignRequest());
                    Assert.Equal("Can't find cng key.", ex.Message);
                }
        }
Esempio n. 2
0
        public void GetAuthorSignRequest_InvalidCertificateFile()
        {
            var mockConsole  = new Mock <IConsole>();
            var timestampUri = "http://timestamp.test/url";

            // Arrange
            using (var dir = TestDirectory.Create())
                using (var test = new MSSignCommandTestContext(_trustedTestCertWithPrivateKey.TrustedCert))
                {
                    var signCommand = new MSSignCommand
                    {
                        Console                = mockConsole.Object,
                        Timestamper            = timestampUri,
                        CertificateFile        = Path.Combine(dir, "non-existant-cert.pfx"),
                        CSPName                = test.CertificateCSPName,
                        KeyContainer           = test.CertificateKeyContainer,
                        CertificateFingerprint = test.Cert.Thumbprint,
                    };
                    signCommand.Arguments.Add(Path.Combine(dir, "package.nupkg"));

                    // Act & Assert
                    var ex = Assert.Throws <CryptographicException>(() => signCommand.GetAuthorSignRequest());
                    Assert.Contains("The system cannot find the file specified.", ex.Message);
                }
        }
Esempio n. 3
0
        public void GetAuthorSignRequest_Success()
        {
            var mockConsole  = new Mock <IConsole>();
            var timestampUri = "http://timestamp.test/url";

            // Arrange
            using (var dir = TestDirectory.Create())
                using (var test = new MSSignCommandTestContext(_trustedTestCertWithPrivateKey.TrustedCert))
                {
                    var signCommand = new MSSignCommand
                    {
                        Console                = mockConsole.Object,
                        Timestamper            = timestampUri,
                        CertificateFile        = test.CertificatePath,
                        CSPName                = test.CertificateCSPName,
                        KeyContainer           = test.CertificateKeyContainer,
                        CertificateFingerprint = test.Cert.Thumbprint,
                    };
                    signCommand.Arguments.Add(Path.Combine(dir, "package.nupkg"));

                    // Act
                    var signRequest = signCommand.GetAuthorSignRequest();

                    // Assert
                    Assert.Equal(SignatureType.Author, signRequest.SignatureType);
                    Assert.NotNull(signRequest.Certificate);
                    Assert.Equal(signRequest.Certificate.Thumbprint, test.Cert.Thumbprint, StringComparer.Ordinal);
                    Assert.NotNull(signRequest.PrivateKey);
                }
        }
        public void MSSignCommandArgParsing_NoCSPName()
        {
            using (var dir = TestDirectory.Create())
            {
                // Arrange
                var packagePath            = Path.Combine(dir, "package.nupkg");
                var timestamper            = "https://timestamper.test";
                var certFile               = Path.Combine(dir, "cert.p7b");
                var certificateFingerprint = new Guid().ToString();
                var keyContainer           = new Guid().ToString();

                var mockConsole = new Mock <IConsole>();
                var signCommand = new MSSignCommand
                {
                    Console                = mockConsole.Object,
                    Timestamper            = timestamper,
                    CertificateFile        = certFile,
                    KeyContainer           = keyContainer,
                    CertificateFingerprint = certificateFingerprint,
                };

                signCommand.Arguments.Add(packagePath);

                // Act & Assert
                var ex = Assert.Throws <ArgumentException>(() => signCommand.GetAuthorSignRequest());
                Assert.Equal(string.Format(_invalidArgException, nameof(signCommand.CSPName)), ex.Message);
            }
        }
        public void MSSignCommandArgParsing_ValidTimestampHashAlgorithm(string timestampHashAlgorithm)
        {
            using (var dir = TestDirectory.Create())
            {
                // Arrange
                var packagePath            = Path.Combine(dir, "package.nupkg");
                var timestamper            = "https://timestamper.test";
                var certFile               = Path.Combine(dir, "cert.p7b");
                var certificateFingerprint = new Guid().ToString();
                var keyContainer           = new Guid().ToString();
                var cspName     = "cert provider";
                var parsable    = Enum.TryParse(timestampHashAlgorithm, ignoreCase: true, result: out Common.HashAlgorithmName parsedHashAlgorithm);
                var mockConsole = new Mock <IConsole>();
                var signCommand = new MSSignCommand
                {
                    Console                = mockConsole.Object,
                    Timestamper            = timestamper,
                    CertificateFile        = certFile,
                    CertificateFingerprint = certificateFingerprint,
                    KeyContainer           = keyContainer,
                    CSPName                = cspName,
                    TimestampHashAlgorithm = timestampHashAlgorithm
                };

                signCommand.Arguments.Add(packagePath);

                // Act & Assert
                Assert.True(parsable);
                var ex = Assert.Throws <CryptographicException>(() => signCommand.GetAuthorSignRequest());
                Assert.NotEqual(string.Format(_invalidArgException, nameof(signCommand.TimestampHashAlgorithm)), ex.Message);
            }
        }
        public void MSSignCommandArgParsing_NoPackagePath()
        {
            // Arrange

            using (var dir = TestDirectory.Create())
            {
                var timestamper            = "https://timestamper.test";
                var certFile               = Path.Combine(dir, "cert.p7b");
                var certificateFingerprint = new Guid().ToString();
                var keyContainer           = new Guid().ToString();
                var cspName = "cert provider";

                var mockConsole = new Mock <IConsole>();
                var signCommand = new MSSignCommand
                {
                    Console                = mockConsole.Object,
                    Timestamper            = timestamper,
                    CertificateFile        = certFile,
                    CSPName                = cspName,
                    KeyContainer           = keyContainer,
                    CertificateFingerprint = certificateFingerprint,
                };

                // Act & Assert
                var ex = Assert.Throws <ArgumentException>(() => signCommand.GetAuthorSignRequest());
                Assert.Equal(_noPackageException, ex.Message);
            }
        }