public async Task AddPkg_V3LocalSourceFeed_WithRelativePath_NoVersionSpecified_Fail() { using (var pathContext = new SimpleTestPathContext()) { var projectName = "projectA"; var targetFrameworks = "net5.0"; XPlatTestUtils.CreateProject(projectName, pathContext, targetFrameworks); var packageX = "packageX"; var packageY = "packageY"; var packageY_V1 = new PackageIdentity(packageY, new NuGetVersion("1.0.0")); var packageFrameworks = "net472; netcoreapp2.0"; var packageY_V1_Context = XPlatTestUtils.CreatePackage(packageY_V1.Id, packageY_V1.Version.Version.ToString(), frameworkString: packageFrameworks); var customSourcePath = Path.Combine(pathContext.WorkingDirectory, "Custompackages"); var sourceRelativePath = Path.Combine("..", "..", "Custompackages"); // Generate Package await SimpleTestPackageUtility.CreateFolderFeedV3Async( customSourcePath, PackageSaveMode.Defaultv3, new SimpleTestPackageContext[] { packageY_V1_Context }); var projectDirectory = Path.Combine(pathContext.SolutionRoot, projectName); var projectFilePath = Path.Combine(projectDirectory, $"{projectName}.csproj"); // Act CommandRunnerResult result = _fixture.RunDotnet(projectDirectory, $"add {projectFilePath} package {packageX} -s {sourceRelativePath}", ignoreExitCode: true); // Assert result.Success.Should().BeFalse(because: result.AllOutput); } }
public async Task GetTimestampCertificateChain_WithNoSigningCertificateUsage_Throws() { ISigningTestServer testServer = await _testFixture.GetSigningTestServerAsync(); CertificateAuthority rootCa = await _testFixture.GetDefaultTrustedCertificateAuthorityAsync(); var options = new TimestampServiceOptions() { SigningCertificateUsage = SigningCertificateUsage.None }; TimestampService timestampService = TimestampService.Create(rootCa, options); using (testServer.RegisterResponder(timestampService)) { var nupkg = new SimpleTestPackageContext(); using (var certificate = new X509Certificate2(_testFixture.TrustedTestCertificate.Source.Cert)) using (TestDirectory directory = TestDirectory.Create()) { string signedPackagePath = await SignedArchiveTestUtility.AuthorSignPackageAsync( certificate, nupkg, directory, timestampService.Url); // Act CommandRunnerResult result = RunVerifyCommand(signedPackagePath); // Assert result.Success.Should().BeFalse(because: result.AllOutput); result.AllOutput.Should().Contain("Either the signing-certificate or signing-certificate-v2 attribute must be present."); result.AllOutput.Should().NotContain(_successfullyVerified); } } }
public async Task DotnetTrust_AuthorAction_RelativePathConfileFile_Succeeds(bool allowUntrustedRoot) { // Arrange var nugetConfigFileName = "NuGet.Config"; var package = new SimpleTestPackageContext(); using (SimpleTestPathContext pathContext = _msbuildFixture.CreateSimpleTestPathContext()) using (MemoryStream zipStream = await package.CreateAsStreamAsync()) using (TrustedTestCert <TestCertificate> trustedTestCert = SigningTestUtility.GenerateTrustedTestCertificate()) { string certFingerprint = SignatureTestUtility.GetFingerprint(trustedTestCert.Source.Cert, HashAlgorithmName.SHA256); string signedPackagePath = await SignedArchiveTestUtility.AuthorSignPackageAsync(trustedTestCert.Source.Cert, package, pathContext.PackageSource); var config = $@"<?xml version=""1.0"" encoding=""utf-8""?> <configuration> <packageSources> <!--To inherit the global NuGet package sources remove the <clear/> line below --> <clear /> <add key=""NuGetSource"" value=""{pathContext.PackageSource}"" /> </packageSources> <config> <add key=""signaturevalidationmode"" value=""accept"" /> </config> <trustedSigners> </trustedSigners> </configuration>"; SettingsTestUtils.CreateConfigurationFile(nugetConfigFileName, pathContext.WorkingDirectory, config); var nugetConfigPath = Path.Combine(pathContext.WorkingDirectory, nugetConfigFileName); var allowUntrustedRootArg = allowUntrustedRoot ? "--allow-untrusted-root" : string.Empty; var allowUntruestedRootValue = allowUntrustedRoot ? "true" : "false"; // Act CommandRunnerResult resultAdd = _msbuildFixture.RunDotnet( pathContext.SolutionRoot, $"nuget trust author nuget {signedPackagePath} {allowUntrustedRootArg} --configfile ..{Path.DirectorySeparatorChar}{nugetConfigFileName}"); // Assert resultAdd.Success.Should().BeTrue(); resultAdd.AllOutput.Should().Contain(string.Format(CultureInfo.CurrentCulture, _successfulAddTrustedSigner, "author", "nuget")); string expectedResult = SettingsTestUtils.RemoveWhitespace($@"<?xml version=""1.0"" encoding=""utf-8""?> <configuration> <packageSources> <!--To inherit the global NuGet package sources remove the < clear /> line below--> <clear/> <add key = ""NuGetSource"" value = ""{pathContext.PackageSource}""/> </packageSources > <config> <add key = ""signaturevalidationmode"" value = ""accept""/> </config> < trustedSigners> <author name = ""nuget""> <certificate fingerprint = ""{certFingerprint}"" hashAlgorithm = ""SHA256"" allowUntrustedRoot = ""{allowUntruestedRootValue}""/> </author> </trustedSigners> </configuration>"); SettingsTestUtils.RemoveWhitespace(File.ReadAllText(nugetConfigPath)).Should().Be(expectedResult); } }
public void DotnetTrust_ListAction_Succeeds() { using (SimpleTestPathContext pathContext = _msbuildFixture.CreateSimpleTestPathContext()) { // Arrange var nugetConfigFileName = "NuGet.Config"; var nugetConfigPath = Path.Combine(pathContext.WorkingDirectory, nugetConfigFileName); var nugetConfigContent = $@"<?xml version=""1.0"" encoding=""utf-8""?> <configuration> <trustedSigners> <author name=""signer""> <certificate fingerprint=""abcdef"" hashAlgorithm=""SHA256"" allowUntrustedRoot=""false"" /> </author> </trustedSigners> </configuration>"; var expectedAuthorContent = $@"Registered trusted signers: 1. signer [author] Certificate fingerprint(s): SHA256 - abcdef "; File.WriteAllText(nugetConfigPath, nugetConfigContent); //Act CommandRunnerResult result = _msbuildFixture.RunDotnet( pathContext.WorkingDirectory, $"nuget trust list --configfile {nugetConfigPath}"); // Assert result.Success.Should().BeTrue(); SettingsTestUtils.RemoveWhitespace(result.Output).Should().Contain(SettingsTestUtils.RemoveWhitespace(expectedAuthorContent)); } }
public async Task DotnetSign_ResignPackageWithOverwrite_SuccessAsync() { // Arrange using (var pathContext = _msbuildFixture.CreateSimpleTestPathContext()) { await SimpleTestPackageUtility.CreatePackagesAsync( pathContext.PackageSource, new SimpleTestPackageContext("PackageA", "1.0.0")); var packageFilePath = Path.Combine(pathContext.PackageSource, "PackageA.1.0.0.nupkg"); TrustedTestCert <TestCertificate> trustedCert = _signFixture.TrustedTestCertificateChain.Leaf; //Act CommandRunnerResult firstResult = _msbuildFixture.RunDotnet( pathContext.PackageSource, $"nuget sign {packageFilePath} --certificate-fingerprint {trustedCert.Source.Cert.Thumbprint} --certificate-store-name {trustedCert.StoreName} --certificate-store-location {trustedCert.StoreLocation}", ignoreExitCode: true); CommandRunnerResult secondResult = _msbuildFixture.RunDotnet( pathContext.PackageSource, $"nuget sign {packageFilePath} --certificate-fingerprint {trustedCert.Source.Cert.Thumbprint} --certificate-store-name {trustedCert.StoreName} --certificate-store-location {trustedCert.StoreLocation} --overwrite", ignoreExitCode: true); // Assert firstResult.Success.Should().BeTrue(because: firstResult.AllOutput); firstResult.AllOutput.Should().Contain(_noTimestamperWarningCode); secondResult.Success.Should().BeTrue(); secondResult.AllOutput.Should().Contain(_noTimestamperWarningCode); } }
public async Task DotnetSign_SignPackageWithPfxFile_SuccessAsync() { // Arrange using (var pathContext = _msbuildFixture.CreateSimpleTestPathContext()) { await SimpleTestPackageUtility.CreatePackagesAsync( pathContext.PackageSource, new SimpleTestPackageContext("PackageA", "1.0.0")); var packageFilePath = Path.Combine(pathContext.PackageSource, "PackageA.1.0.0.nupkg"); TrustedTestCert <TestCertificate> trustedCert = _signFixture.TrustedTestCertificateChain.Leaf; var pfxPath = Path.Combine(pathContext.WorkingDirectory, Guid.NewGuid().ToString()); var password = Guid.NewGuid().ToString(); var pfxBytes = trustedCert.Source.Cert.Export(X509ContentType.Pfx, password); File.WriteAllBytes(pfxPath, pfxBytes); //Act CommandRunnerResult result = _msbuildFixture.RunDotnet( pathContext.PackageSource, $"nuget sign {packageFilePath} --certificate-path {pfxPath} --certificate-password {password}", ignoreExitCode: true); // Assert result.Success.Should().BeTrue(because: result.AllOutput); result.AllOutput.Should().Contain(_noTimestamperWarningCode); } }
public async Task DotnetSign_SignPackageWithOutputDirectory_SucceedsAsync() { // Arrange using (var pathContext = _msbuildFixture.CreateSimpleTestPathContext()) { await SimpleTestPackageUtility.CreatePackagesAsync( pathContext.PackageSource, new SimpleTestPackageContext("PackageA", "1.0.0")); var packageFilePath = Path.Combine(pathContext.PackageSource, "PackageA.1.0.0.nupkg"); var outputDir = Path.Combine(pathContext.WorkingDirectory, "Output"); Directory.CreateDirectory(outputDir); TrustedTestCert <TestCertificate> trustedCert = _signFixture.TrustedTestCertificateChain.Leaf; //Act CommandRunnerResult result = _msbuildFixture.RunDotnet( pathContext.PackageSource, $"nuget sign {packageFilePath} --certificate-fingerprint {trustedCert.Source.Cert.Thumbprint} --certificate-store-name {trustedCert.StoreName} --certificate-store-location {trustedCert.StoreLocation} --output {outputDir}", ignoreExitCode: true); var signedPackagePath = Path.Combine(outputDir, "PackageA.1.0.0.nupkg"); // Assert result.Success.Should().BeTrue(because: result.AllOutput); result.AllOutput.Should().Contain(_noTimestamperWarningCode); File.Exists(signedPackagePath).Should().BeTrue(); } }
public async Task DotnetSign_SignPackageWithUnknownRevocationCertChain_SucceedsAsync() { // Arrange using (var pathContext = _msbuildFixture.CreateSimpleTestPathContext()) { await SimpleTestPackageUtility.CreatePackagesAsync( pathContext.PackageSource, new SimpleTestPackageContext("PackageA", "1.0.0")); var packageFilePath = Path.Combine(pathContext.PackageSource, "PackageA.1.0.0.nupkg"); TrustedTestCert <TestCertificate> revocationUnknownCert = _signFixture.RevocationUnknownTestCertificateWithChain; //Act CommandRunnerResult result = _msbuildFixture.RunDotnet( pathContext.PackageSource, $"nuget sign {packageFilePath} --certificate-fingerprint {revocationUnknownCert.Source.Cert.Thumbprint} --certificate-store-name {revocationUnknownCert.StoreName} --certificate-store-location {revocationUnknownCert.StoreLocation}", ignoreExitCode: true); // Assert result.Success.Should().BeTrue(because: result.AllOutput); result.AllOutput.Should().Contain(_noTimestamperWarningCode); result.AllOutput.Should().Contain(_chainBuildFailureErrorCode); result.AllOutput.Should().Contain(X509ChainStatusFlags.RevocationStatusUnknown.ToString()); } }
public async Task DotnetSign_SignPackageWithTimestamping_SucceedsAsync() { // Arrange using (var pathContext = _msbuildFixture.CreateSimpleTestPathContext()) { await SimpleTestPackageUtility.CreatePackagesAsync( pathContext.PackageSource, new SimpleTestPackageContext("PackageA", "1.0.0")); var packageFilePath = Path.Combine(pathContext.PackageSource, "PackageA.1.0.0.nupkg"); var timestampService = await _signFixture.GetDefaultTrustedTimestampServiceAsync(); TrustedTestCert <TestCertificate> trustedCert = _signFixture.TrustedTestCertificateChain.Leaf; //Act CommandRunnerResult result = _msbuildFixture.RunDotnet( pathContext.PackageSource, $"nuget sign {packageFilePath} --certificate-fingerprint {trustedCert.Source.Cert.Thumbprint} --certificate-store-name {trustedCert.StoreName} --certificate-store-location {trustedCert.StoreLocation} --timestamper {timestampService.Url.OriginalString}", ignoreExitCode: true); // Assert result.Success.Should().BeTrue(because: result.AllOutput); result.AllOutput.Should().NotContain(_noTimestamperWarningCode); } }
public async Task Verify_AuthorSignedTimestampedPackage_SuccessAsync() { // Arrange var nupkg = new SimpleTestPackageContext(); X509Certificate2 defaultAuthorCertificate = await _testFixture.GetDefaultAuthorSigningCertificateAsync(); TimestampService timestampService = await _testFixture.GetDefaultTrustedTimestampServiceAsync(); using (TestDirectory dir = TestDirectory.Create()) using (var authorCertificate = new X509Certificate2(defaultAuthorCertificate)) { string signedPackagePath = await SignedArchiveTestUtility.AuthorSignPackageAsync( authorCertificate, nupkg, dir, timestampService.Url); // Act CommandRunnerResult result = RunVerifyCommand(signedPackagePath); // Assert result.Success.Should().BeTrue(because: result.AllOutput); result.AllOutput.Should().Contain(_successfullyVerified); Regex.Matches(result.AllOutput, _noTimestamperWarning).Count.Should().Be(0); } }
public async Task DotnetSign_SignPackageWithTrustedCertificateWithRelativePath_SucceedsAsync() { // Arrange using (SimpleTestPathContext pathContext = _msbuildFixture.CreateSimpleTestPathContext()) { await SimpleTestPackageUtility.CreatePackagesAsync( pathContext.PackageSource, new SimpleTestPackageContext("PackageA", "1.0.0")); var packageFileName = "PackageA.1.0.0.nupkg"; IX509StoreCertificate storeCertificate = _signFixture.DefaultCertificate; // Act CommandRunnerResult result = _msbuildFixture.RunDotnet( pathContext.PackageSource, $"nuget sign .{Path.DirectorySeparatorChar}{packageFileName} " + $"--certificate-fingerprint {storeCertificate.Certificate.Thumbprint} " + $"--certificate-store-name {storeCertificate.StoreName} " + $"--certificate-store-location {storeCertificate.StoreLocation}", ignoreExitCode: true); // Assert result.Success.Should().BeTrue(because: result.AllOutput); result.AllOutput.Should().Contain(_noTimestamperWarningCode); } }
public void PushCommand_Server_Snupkg_ByFilename_SnupkgExists_SkipDuplicate_ServerMessage() { // Arrange using (var packageDirectory = TestDirectory.Create()) { var nuget = Util.GetNuGetExePath(); string snupkgFileName = "fileName.snupkg"; string snupkgFullPath = Path.Combine(packageDirectory, snupkgFileName); //Create snupkg in test directory. WriteSnupkgFile(snupkgFullPath); CommandRunnerResult result = null; using (var server = CreateAndStartMockV3Server(packageDirectory, out string sourceName)) { SetupMockServerAlwaysDuplicate(server); // Act //Since this is V3, this will trigger 2 pushes: one for nupkgs, and one for snupkgs. result = CommandRunner.Run( nuget, packageDirectory, $"push {snupkgFileName} -Source {sourceName} -Timeout 110", waitForExit: true, timeOutInMilliseconds: 120000); // 120 seconds } // Assert Assert.False(result.Success, "Expected a Duplicate response to fail the push."); Assert.Contains("Conflict", result.AllOutput); } }
public async Task DotnetSign_ResignPackageWithOverwrite_SuccessAsync() { // Arrange using (SimpleTestPathContext pathContext = _msbuildFixture.CreateSimpleTestPathContext()) { await SimpleTestPackageUtility.CreatePackagesAsync( pathContext.PackageSource, new SimpleTestPackageContext("PackageA", "1.0.0")); string packageFilePath = Path.Combine(pathContext.PackageSource, "PackageA.1.0.0.nupkg"); IX509StoreCertificate storeCertificate = _signFixture.DefaultCertificate; string args = GetDefaultArgs(packageFilePath, storeCertificate); // Act CommandRunnerResult firstResult = _msbuildFixture.RunDotnet( pathContext.PackageSource, args, ignoreExitCode: true); CommandRunnerResult secondResult = _msbuildFixture.RunDotnet( pathContext.PackageSource, args + " --overwrite", ignoreExitCode: true); // Assert firstResult.Success.Should().BeTrue(because: firstResult.AllOutput); firstResult.AllOutput.Should().Contain(_noTimestamperWarningCode); secondResult.Success.Should().BeTrue(); secondResult.AllOutput.Should().Contain(_noTimestamperWarningCode); } }
public async Task DotnetSign_SignPackageWithUnknownRevocationCertChain_SucceedsAsync() { // Arrange using (SimpleTestPathContext pathContext = _msbuildFixture.CreateSimpleTestPathContext()) { await SimpleTestPackageUtility.CreatePackagesAsync( pathContext.PackageSource, new SimpleTestPackageContext("PackageA", "1.0.0")); string packageFilePath = Path.Combine(pathContext.PackageSource, "PackageA.1.0.0.nupkg"); IX509StoreCertificate storeCertificate = _signFixture.RevocationUnknownCertificate; // Act CommandRunnerResult result = _msbuildFixture.RunDotnet( pathContext.PackageSource, GetDefaultArgs(packageFilePath, storeCertificate), ignoreExitCode: true); // Assert result.Success.Should().BeTrue(because: result.AllOutput); result.AllOutput.Should().Contain(_noTimestamperWarningCode); result.AllOutput.Should().Contain(_chainBuildFailureErrorCode); result.AllOutput.Should().Contain(X509ChainStatusFlags.RevocationStatusUnknown.ToString()); } }
public async Task DotnetSign_SignPackageWithOutputDirectory_SucceedsAsync() { // Arrange using (SimpleTestPathContext pathContext = _msbuildFixture.CreateSimpleTestPathContext()) { await SimpleTestPackageUtility.CreatePackagesAsync( pathContext.PackageSource, new SimpleTestPackageContext("PackageA", "1.0.0")); string packageFilePath = Path.Combine(pathContext.PackageSource, "PackageA.1.0.0.nupkg"); string outputDir = Path.Combine(pathContext.WorkingDirectory, "Output"); IX509StoreCertificate storeCertificate = _signFixture.DefaultCertificate; Directory.CreateDirectory(outputDir); // Act CommandRunnerResult result = _msbuildFixture.RunDotnet( pathContext.PackageSource, GetDefaultArgs(packageFilePath, storeCertificate) + $" --output {outputDir}", ignoreExitCode: true); string signedPackagePath = Path.Combine(outputDir, "PackageA.1.0.0.nupkg"); // Assert result.Success.Should().BeTrue(because: result.AllOutput); result.AllOutput.Should().Contain(_noTimestamperWarningCode); File.Exists(signedPackagePath).Should().BeTrue(); } }
public async Task DotnetSign_SignPackageWithPfxFileWithoutPasswordAndWithNonInteractive_FailsAsync() { // Arrange using (SimpleTestPathContext pathContext = _msbuildFixture.CreateSimpleTestPathContext()) { await SimpleTestPackageUtility.CreatePackagesAsync( pathContext.PackageSource, new SimpleTestPackageContext("PackageA", "1.0.0")); string packageFilePath = Path.Combine(pathContext.PackageSource, "PackageA.1.0.0.nupkg"); IX509StoreCertificate storeCertificate = _signFixture.DefaultCertificate; string pfxPath = Path.Combine(pathContext.WorkingDirectory, Guid.NewGuid().ToString()); string password = Guid.NewGuid().ToString(); byte[] pfxBytes = storeCertificate.Certificate.Export(X509ContentType.Pfx, password); File.WriteAllBytes(pfxPath, pfxBytes); // Act CommandRunnerResult result = _msbuildFixture.RunDotnet( pathContext.PackageSource, $"nuget sign {packageFilePath} --certificate-path {pfxPath}", ignoreExitCode: true); // Assert result.Success.Should().BeFalse(because: result.AllOutput); result.AllOutput.Should().Contain(string.Format(_invalidPasswordError, pfxPath)); } }
public async Task DotnetSign_SignPackageWithTimestamping_SucceedsAsync() { // Arrange using (SimpleTestPathContext pathContext = _msbuildFixture.CreateSimpleTestPathContext()) { await SimpleTestPackageUtility.CreatePackagesAsync( pathContext.PackageSource, new SimpleTestPackageContext("PackageA", "1.0.0")); string packageFilePath = Path.Combine(pathContext.PackageSource, "PackageA.1.0.0.nupkg"); TimestampService timestampService = await _signFixture.GetDefaultTrustedTimestampServiceAsync(); IX509StoreCertificate storeCertificate = _signFixture.DefaultCertificate; // Act CommandRunnerResult result = _msbuildFixture.RunDotnet( pathContext.PackageSource, GetDefaultArgs(packageFilePath, storeCertificate) + $" --timestamper {timestampService.Url.OriginalString}", ignoreExitCode: true); // Assert result.Success.Should().BeTrue(because: result.AllOutput); result.AllOutput.Should().NotContain(_noTimestamperWarningCode); } }
public async Task DotnetSign_SignPackageWithUntrustedSelfIssuedCertificateInCertificateStore_SuccessAsync() { // Arrange using (SimpleTestPathContext pathContext = _msbuildFixture.CreateSimpleTestPathContext()) { await SimpleTestPackageUtility.CreatePackagesAsync( pathContext.PackageSource, new SimpleTestPackageContext("PackageA", "1.0.0")); string packageFilePath = Path.Combine(pathContext.PackageSource, "PackageA.1.0.0.nupkg"); IX509StoreCertificate storeCertificate = _signFixture.UntrustedSelfIssuedCertificateInCertificateStore; // Act CommandRunnerResult result = _msbuildFixture.RunDotnet( pathContext.PackageSource, $"nuget sign {packageFilePath} " + $"--certificate-fingerprint {storeCertificate.Certificate.Thumbprint}", ignoreExitCode: true); // Assert result.Success.Should().BeTrue(because: result.AllOutput); result.AllOutput.Should().Contain(_noTimestamperWarningCode); result.AllOutput.Should().Contain(_chainBuildFailureErrorCode); } }
public void PushCommand_Server_Nupkg_ByWildcard_FindsNothing_FileNotFoundError() { // Arrange using (var packageDirectory = TestDirectory.Create()) { var nuget = Util.GetNuGetExePath(); string nupkgToPush = "*.nupkg"; CommandRunnerResult result = null; using (var server = CreateAndStartMockV3Server(packageDirectory, out string sourceName)) { // Act result = CommandRunner.Run( nuget, packageDirectory, $"push {nupkgToPush} -Source {sourceName} -Timeout 110", waitForExit: true, timeOutInMilliseconds: 120000); // 120 seconds } //Assert string expectedFileNotFoundErrorMessage = string.Format(MESSAGE_FILE_DOES_NOT_EXIST, nupkgToPush); Assert.False(result.Success, "File did not exist and should fail."); Assert.DoesNotContain(MESSAGE_PACKAGE_PUSHED, result.Output); Assert.Contains(expectedFileNotFoundErrorMessage, result.Errors); } }
public void Sources_WarnWhenAdding(string source, bool shouldWarn) { using (SimpleTestPathContext pathContext = new SimpleTestPathContext()) { TestDirectory workingPath = pathContext.WorkingDirectory; SimpleTestSettingsContext settings = pathContext.Settings; // Arrange var args = new string[] { "nuget", "add", "source", source, "--name", "test_source", "--configfile", settings.ConfigPath }; // Act CommandRunnerResult result = _fixture.RunDotnet(workingPath, string.Join(" ", args), ignoreExitCode: true); // Assert Assert.True(result.Success, result.Output + " " + result.Errors); ISettings loadedSettings = Settings.LoadDefaultSettings(root: workingPath, configFileName: null, machineWideSettings: null); SettingSection packageSourcesSection = loadedSettings.GetSection("packageSources"); SourceItem sourceItem = packageSourcesSection?.GetFirstItemWithAttribute <SourceItem>("key", "test_source"); Assert.Equal(source, sourceItem.GetValueAsPath()); Assert.Equal(shouldWarn, result.Output.Contains("warn : You are running the 'add source' operation with an 'HTTP' source")); } }
public void SourcesCommandTest_AddSource(string source, bool shouldWarn) { using (SimpleTestPathContext pathContext = new SimpleTestPathContext()) { TestDirectory workingPath = pathContext.WorkingDirectory; SimpleTestSettingsContext settings = pathContext.Settings; // Arrange string nugetexe = Util.GetNuGetExePath(); var args = new string[] { "sources", "Add", "-Name", "test_source", "-Source", source, "-ConfigFile", settings.ConfigPath }; // Act CommandRunnerResult result = CommandRunner.Run(nugetexe, workingPath, string.Join(" ", args), true); // Assert Assert.Equal(0, result.ExitCode); ISettings loadedSettings = Configuration.Settings.LoadDefaultSettings(workingPath, null, null); SettingSection packageSourcesSection = loadedSettings.GetSection("packageSources"); SourceItem sourceItem = packageSourcesSection?.GetFirstItemWithAttribute <SourceItem>("key", "test_source"); Assert.Equal(source, sourceItem.GetValueAsPath()); Assert.Equal(shouldWarn, result.Output.Contains("WARNING: You are running the 'add source' operation with an 'HTTP' source")); } }
public void ListCommand_InvalidInput_V3_NotFound(string invalidInput) { // Arrange string nugetexe = Util.GetNuGetExePath(); // Act using (var pathContext = new SimpleTestPathContext()) { var args = "list test -Source " + invalidInput; CommandRunnerResult result = CommandRunner.Run( process: nugetexe, workingDirectory: pathContext.SolutionRoot, arguments: args, waitForExit: true); // Assert Assert.False( result.Success, "The run did not fail as desired. Simply got this output:" + result.Output); Assert.True( result.Errors.Contains("Response status code does not indicate success"), "Expected error message not found in " + result.Errors ); } }
public async Task DotnetSign_SignPackageWithPfxFileOfRelativePath_SuccessAsync() { // Arrange using (SimpleTestPathContext pathContext = _msbuildFixture.CreateSimpleTestPathContext()) { await SimpleTestPackageUtility.CreatePackagesAsync( pathContext.PackageSource, new SimpleTestPackageContext("PackageA", "1.0.0")); string packageFilePath = Path.Combine(pathContext.PackageSource, "PackageA.1.0.0.nupkg"); IX509StoreCertificate storeCertificate = _signFixture.DefaultCertificate; string pfxName = Guid.NewGuid().ToString() + ".pfx"; string pfxPath = Path.Combine(pathContext.PackageSource, pfxName); string password = Guid.NewGuid().ToString(); byte[] pfxBytes = storeCertificate.Certificate.Export(X509ContentType.Pfx, password); File.WriteAllBytes(pfxPath, pfxBytes); // Act CommandRunnerResult result = _msbuildFixture.RunDotnet( pathContext.PackageSource, $"nuget sign {packageFilePath} " + $"--certificate-path .{Path.DirectorySeparatorChar}{pfxName} " + $"--certificate-password {password}", ignoreExitCode: true); // Assert result.Success.Should().BeTrue(because: result.AllOutput); result.AllOutput.Should().Contain(_noTimestamperWarningCode); } }
public void PushCommand_WhenPushingToAnHttpServerV3_WithSymbols_Warns() { // Arrange using var packageDirectory = TestDirectory.Create(); var nuget = Util.GetNuGetExePath(); string snupkgFileName = "fileName.snupkg"; string snupkgFullPath = Path.Combine(packageDirectory, snupkgFileName); //Create snupkg in test directory. WriteSnupkgFile(snupkgFullPath); CommandRunnerResult result = null; using var server = CreateAndStartMockV3Server(packageDirectory, out string sourceName); SetupMockServerAlwaysCreate(server); // Act result = CommandRunner.Run( nuget, packageDirectory, $"push {snupkgFileName} -Source {sourceName} -Timeout 110 -Verbosity detailed", waitForExit: true, timeOutInMilliseconds: 120000); // 120 seconds // Assert Assert.True(result.Success, result.AllOutput); Assert.Contains("WARNING: You are running the 'push' operation with an 'HTTP' source", result.AllOutput); }
public void SourcesCommandTest_EnableSource_WarnWhenUsingHttp() { // Arrange string nugetexe = Util.GetNuGetExePath(); using (TestDirectory configFileDirectory = TestDirectory.Create()) { string configFileName = "nuget.config"; string configFilePath = Path.Combine(configFileDirectory, configFileName); Util.CreateFile(configFileDirectory, configFileName, @"<?xml version=""1.0"" encoding=""utf-8""?> <configuration> <packageSources> <add key=""test_source"" value=""http://test_source"" /> </packageSources> <disabledPackageSources> <add key=""test_source"" value=""true"" /> <add key=""Microsoft and .NET"" value=""true"" /> </disabledPackageSources> </configuration>"); var args = new string[] { "sources", "Enable", "-Name", "test_source", "-ConfigFile", configFilePath }; // Act CommandRunnerResult result = CommandRunner.Run( nugetexe, Directory.GetCurrentDirectory(), string.Join(" ", args), true); // Assert Util.VerifyResultSuccess(result); ISettings settings = Configuration.Settings.LoadDefaultSettings( configFileDirectory, configFileName, null); PackageSourceProvider packageSourceProvider = new Configuration.PackageSourceProvider(settings); var sources = packageSourceProvider.LoadPackageSources().ToList(); var testSources = sources.Where(s => s.Name == "test_source"); Assert.Single(testSources); PackageSource source = testSources.Single(); Assert.Equal("test_source", source.Name); Assert.Equal("http://test_source", source.Source); Assert.True(source.IsEnabled, "Source is not enabled"); Assert.True(result.Output.Contains("WARNING: You are running the 'enable source' operation with an 'HTTP' source, 'http://test_source'. Non-HTTPS access will be removed in a future version. Consider migrating to an 'HTTPS' source.")); } }
public void Sources_WarnWhenUpdatingHttpSource(string updateSource, bool shouldWarn) { using (TestDirectory configFileDirectory = _fixture.CreateTestDirectory()) { string configFileName = "nuget.config"; string configFilePath = Path.Combine(configFileDirectory, configFileName); var nugetConfig = @"<?xml version=""1.0"" encoding=""utf-8""?> <configuration> <packageSources> <add key=""test_source"" value=""http://source.test.initial"" /> </packageSources> </configuration>"; CreateXmlFile(configFilePath, nugetConfig); ISettings settings = Settings.LoadDefaultSettings( configFileDirectory, configFileName, null); PackageSourceProvider packageSourceProvider = new PackageSourceProvider(settings); var sources = packageSourceProvider.LoadPackageSources().ToList(); Assert.Single(sources); PackageSource source = sources.Single(); Assert.Equal("test_source", source.Name); Assert.Equal("http://source.test.initial", source.Source); // Arrange var args = new string[] { "nuget", "update", "source", "test_source", "--source", updateSource, "--configfile", configFilePath }; // Act CommandRunnerResult result = _fixture.RunDotnet(configFileDirectory, string.Join(" ", args), ignoreExitCode: true); // Assert Assert.True(result.Success, result.Output + " " + result.Errors); ISettings loadedSettings = Settings.LoadDefaultSettings(root: configFileDirectory, configFileName: null, machineWideSettings: null); SettingSection packageSourcesSection = loadedSettings.GetSection("packageSources"); SourceItem sourceItem = packageSourcesSection?.GetFirstItemWithAttribute <SourceItem>("key", "test_source"); Assert.Equal(updateSource, sourceItem.GetValueAsPath()); Assert.Equal(shouldWarn, result.Output.Contains("warn : You are running the 'update source' operation with an 'HTTP' source")); } }
public void SourcesCommandTest_DisableSource_NoWarnWhenUsingHttp() { // Arrange string nugetexe = Util.GetNuGetExePath(); using (var configFileDirectory = TestDirectory.Create()) { string configFileName = "nuget.config"; string configFilePath = Path.Combine(configFileDirectory, configFileName); Util.CreateFile(configFileDirectory, configFileName, @"<?xml version=""1.0"" encoding=""utf-8""?> <configuration> <packageSources> <add key=""test_source"" value=""http://test_source"" /> </packageSources> </configuration>"); var args = new string[] { "sources", "Disable", "-Name", "test_source", "-ConfigFile", configFilePath }; // Act CommandRunnerResult result = CommandRunner.Run( nugetexe, Directory.GetCurrentDirectory(), string.Join(" ", args), true); // Assert Util.VerifyResultSuccess(result); ISettings settings = Configuration.Settings.LoadDefaultSettings( configFileDirectory, configFileName, null); PackageSourceProvider packageSourceProvider = new Configuration.PackageSourceProvider(settings); var sources = packageSourceProvider.LoadPackageSources().ToList(); var testSources = sources.Where(s => s.Name == "test_source"); Assert.Single(testSources); PackageSource source = testSources.Single(); Assert.Equal("test_source", source.Name); Assert.Equal("http://test_source", source.Source); Assert.False(source.IsEnabled, "Source is not disabled"); Assert.False(result.Output.Contains("WARNING:")); } }
/// <summary> /// Utility for asserting faulty executions of dotnet.exe /// /// Asserts a non-zero status code and a message on stderr. /// </summary> /// <param name="result">An instance of <see cref="CommandRunnerResult"/> with command execution results</param> /// <param name="expectedErrorMessage">A portion of the error message to be sent</param> public static void VerifyResultFailure(CommandRunnerResult result, string expectedErrorMessage) { Assert.False( result.Success, "dotnet.exe nuget DID NOT FAIL: Output is " + result.Output + ". Error is " + result.Errors); Assert.True( result.Output.Contains(expectedErrorMessage), "Expected error is " + expectedErrorMessage + ". Actual error is " + result.Output); }
/// <summary> /// Used to verify the failure of negitive test cases /// </summary> /// <param name="result">The actual result of the test</param> /// <param name="expectedOutputMessage"> The expected result of the test</param> public static void VerifyResultFailure(CommandRunnerResult result, string expectedErrorMessage) { Assert.True( result.Item1 != 0, $"Command DID NOT FAIL. Ouput is: \"{result.Item2}\". Error is: \"{result.Item3}\""); Assert.True( result.Item2.Contains(expectedErrorMessage), $"Expected error is: \"{expectedErrorMessage}\". Actual error is: \"{result.Item3}\". Ouput is: \"{result.Item2}\"."); }
public static void VerifyResultFailure(CommandRunnerResult result, string expectedErrorMessage) { Assert.True( result.Item1 != 0, "nuget.exe DID NOT FAIL: Ouput is " + result.Item2 + ". Error is " + result.Item3); Assert.True( result.Item3.Contains(expectedErrorMessage), "Expected error is " + expectedErrorMessage + ". Actual error is " + result.Item3); }