Beispiel #1
0
        public void ReposignCommandArgParsing_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 v3serviceIndexUrl      = "https://v3serviceIndex.test/api/index.json";

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

                reposignCommand.Arguments.Add(packagePath);

                // Act & Assert
                var ex = Assert.Throws <ArgumentException>(() => reposignCommand.GetRepositorySignRequest());
                Assert.Equal(string.Format(_invalidArgException, nameof(reposignCommand.CSPName)), ex.Message);
            }
        }
Beispiel #2
0
        public void ReposignCommandArgParsing_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 serviceIndex    = "https://v3serviceindex.test/api/index.json";
                var parsable        = Enum.TryParse(timestampHashAlgorithm, ignoreCase: true, result: out Common.HashAlgorithmName parsedHashAlgorithm);
                var mockConsole     = new Mock <IConsole>();
                var reposignCommand = new RepoSignCommand
                {
                    Console                = mockConsole.Object,
                    Timestamper            = timestamper,
                    CertificateFile        = certFile,
                    CertificateFingerprint = certificateFingerprint,
                    KeyContainer           = keyContainer,
                    CSPName                = cspName,
                    V3ServiceIndexUrl      = serviceIndex,
                    TimestampHashAlgorithm = timestampHashAlgorithm
                };

                reposignCommand.Arguments.Add(packagePath);

                // Act & Assert
                Assert.True(parsable);
                var ex = Assert.Throws <CryptographicException>(() => reposignCommand.GetRepositorySignRequest());
                Assert.NotEqual(string.Format(_invalidArgException, nameof(reposignCommand.TimestampHashAlgorithm)), ex.Message);
            }
        }
Beispiel #3
0
        public void ReposignCommandArgParsing_NoPackagePath()
        {
            using (var dir = TestDirectory.Create())
            {
                // Arrange
                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 v3serviceIndexUrl = "https://v3serviceIndex.test/api/index.json";

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

                // Act & Assert
                var ex = Assert.Throws <ArgumentException>(() => reposignCommand.GetRepositorySignRequest());
                Assert.Equal(_noPackageException, ex.Message);
            }
        }
Beispiel #4
0
        public void GetRepositorySignRequest_InvalidCSPName()
        {
            var mockConsole    = new Mock <IConsole>();
            var v3serviceIndex = "https://v3serviceindex.test/api/index.json";
            var timestampUri   = "http://timestamp.test/url";

            // Arrange
            using (var dir = TestDirectory.Create())
                using (var test = new MSSignCommandTestContext(_trustedTestCertWithoutPrivateKey.TrustedCert, exportPfx: false))
                {
                    var reposignCommand = new RepoSignCommand
                    {
                        Console                = mockConsole.Object,
                        Timestamper            = timestampUri,
                        CertificateFile        = test.CertificatePath,
                        CSPName                = "random nonexistant csp name",
                        KeyContainer           = test.CertificateKeyContainer,
                        CertificateFingerprint = test.Cert.Thumbprint,
                        V3ServiceIndexUrl      = v3serviceIndex,
                    };
                    reposignCommand.Arguments.Add(Path.Combine(dir, "package.nupkg"));

                    // Act & Assert
                    var ex = Assert.Throws <InvalidOperationException>(() => reposignCommand.GetRepositorySignRequest());
                    Assert.Equal("Can't find cng key.", ex.Message);
                }
        }
Beispiel #5
0
        public void GetRepositorySignRequest_InvalidCertificateFile()
        {
            var mockConsole    = new Mock <IConsole>();
            var v3serviceIndex = "https://v3serviceindex.test/api/index.json";
            var timestampUri   = "http://timestamp.test/url";

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

                    // Act & Assert
                    var ex = Assert.Throws <CryptographicException>(() => reposignCommand.GetRepositorySignRequest());
                    Assert.Contains("The system cannot find the file specified.", ex.Message);
                }
        }
Beispiel #6
0
        public void GetRepositorySignRequest_Success()
        {
            var mockConsole    = new Mock <IConsole>();
            var v3serviceIndex = "https://v3serviceindex.test/api/index.json";
            var timestampUri   = "http://timestamp.test/url";

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

                    // Act
                    var signRequest = reposignCommand.GetRepositorySignRequest();

                    // Assert
                    Assert.Equal(v3serviceIndex, signRequest.V3ServiceIndexUrl.AbsoluteUri, StringComparer.Ordinal);
                    Assert.Equal(SignatureType.Repository, signRequest.SignatureType);
                    Assert.NotNull(signRequest.Certificate);
                    Assert.Equal(signRequest.Certificate.Thumbprint, test.Cert.Thumbprint, StringComparer.Ordinal);
                    Assert.NotNull(signRequest.PrivateKey);
                }
        }