public void AttemptsFiveTimesOnError()
        {
            var packageId       = "FakePackageId";
            var version         = new NuGetVersion(1, 2, 3);
            var feedUri         = new Uri("http://www.myget.org");
            var feedCredentials = new CredentialCache();
            var targetFilePath  = "FakeTargetFilePath";

            var calledCount = 0;

            Assert.Throws <Exception>(() =>
            {
                //total attempts is initial attempt + 4 retries
                var retryTracker = new RetryTracker(maxRetries: 4,
                                                    timeLimit: null,
                                                    retryInterval: new RetryInterval(100, 150, 1));
                var downloader = new NuGetPackageDownloader(retryTracker);
                downloader.DownloadPackage(packageId, version, feedUri, feedCredentials, targetFilePath,
                                           (arg1, arg2, arg3, arg4, arg5) =>
                {
                    calledCount++;
                    throw new ApplicationException("Expected exception from test");
                });
            });
            Assert.That(calledCount, Is.EqualTo(5));
        }
        public void ItShouldReturnListOfManifestFiles()
        {
            Initialize(nameof(ItShouldReturnListOfManifestFiles));
            NuGetPackageDownloader nuGetPackageDownloader = new NuGetPackageDownloader(new DirectoryPath(_updaterDir),
                                                                                       null,
                                                                                       new MockFirstPartyNuGetPackageSigningVerifier(),
                                                                                       new NullLogger(), restoreActionConfig: new RestoreActionConfig(NoCache: true));

            MockWorkloadResolver    mockWorkloadResolver    = new(Enumerable.Empty <WorkloadResolver.WorkloadInfo>());
            WorkloadManifestUpdater workloadManifestUpdater =
                new WorkloadManifestUpdater(new BufferedReporter(),
                                            mockWorkloadResolver, nuGetPackageDownloader,
                                            _updaterDir, _updaterDir, new MockInstallationRecordRepository());

            string package = DownloadSamplePackage(new PackageId("Microsoft.NET.Workload.Emscripten.Manifest-6.0.100"),
                                                   NuGetVersion.Parse("6.0.0-preview.7.21377.2"), nuGetPackageDownloader);

            workloadManifestUpdater.ExtractManifestPackagesToTempDirAsync(new List <string> {
                package
            },
                                                                          new DirectoryPath(_manifestDirectory)).GetAwaiter().GetResult();

            TempDirectoryWorkloadManifestProvider tempDirectoryWorkloadManifestProvider =
                new TempDirectoryWorkloadManifestProvider(_manifestDirectory, mockWorkloadResolver.GetSdkFeatureBand());
            IEnumerable <(string manifestId, string informationalPath, Func <Stream> openManifestStream, Func <Stream> openLocalizationStream)> manifest =
                tempDirectoryWorkloadManifestProvider.GetManifests();

            manifest.First().manifestId.Should()
            .NotBe("microsoft.net.workload.emscripten.manifest-6.0.100.6.0.0-preview.7.21377.2");
            manifest.First().manifestId.Should()
            .BeEquivalentTo("microsoft.net.workload.emscripten");
        }
Beispiel #3
0
        /// <summary>
        /// Creates a new <see cref="NetSdkMsiInstallerClient"/> instance. If the current host process is not elevated,
        /// the elevated server process will also be started by running an additional command.
        /// </summary>
        /// <param name="nugetPackageDownloader"></param>
        /// <param name="verbosity"></param>
        /// <param name="packageSourceLocation"></param>
        /// <returns></returns>
        public static NetSdkMsiInstallerClient Create(
            SdkFeatureBand sdkFeatureBand,
            IWorkloadResolver workloadResolver,
            INuGetPackageDownloader nugetPackageDownloader = null,
            VerbosityOptions verbosity = VerbosityOptions.normal,
            PackageSourceLocation packageSourceLocation = null,
            IReporter reporter = null,
            string tempDirPath = null,
            RestoreActionConfig restoreActionConfig = null)
        {
            TimestampedFileLogger         logger           = new(Path.Combine(Path.GetTempPath(), $"Microsoft.NET.Workload_{DateTime.Now:yyyyMMdd_HHmmss}.log"));
            InstallClientElevationContext elevationContext = new(logger);

            if (nugetPackageDownloader == null)
            {
                DirectoryPath tempPackagesDir = new(string.IsNullOrWhiteSpace(tempDirPath) ? Path.GetTempPath() : tempDirPath);

                nugetPackageDownloader = new NuGetPackageDownloader(tempPackagesDir,
                                                                    filePermissionSetter: null, new FirstPartyNuGetPackageSigningVerifier(tempPackagesDir),
                                                                    new NullLogger(), restoreActionConfig: restoreActionConfig);
            }

            return(new NetSdkMsiInstallerClient(elevationContext, logger, workloadResolver, sdkFeatureBand, nugetPackageDownloader,
                                                verbosity, packageSourceLocation, reporter));
        }
Beispiel #4
0
        public async static Task BackgroundUpdateAdvertisingManifestsAsync()
        {
            try
            {
                var reporter   = new NullReporter();
                var dotnetPath = Path.GetDirectoryName(Environment.ProcessPath);
                var sdkVersion = Product.Version;
                var workloadManifestProvider = new SdkDirectoryWorkloadManifestProvider(dotnetPath, sdkVersion);
                var workloadResolver         = WorkloadResolver.Create(workloadManifestProvider, dotnetPath, sdkVersion);
                var tempPackagesDir          = new DirectoryPath(Path.Combine(Path.GetTempPath(), "dotnet-sdk-advertising-temp"));
                var nugetPackageDownloader   = new NuGetPackageDownloader(tempPackagesDir,
                                                                          filePermissionSetter: null,
                                                                          new FirstPartyNuGetPackageSigningVerifier(tempPackagesDir, new NullLogger()),
                                                                          new NullLogger(),
                                                                          reporter);
                var userHome = CliFolderPathCalculator.DotnetHomePath;

                var manifestUpdater = new WorkloadManifestUpdater(reporter, workloadManifestProvider, workloadResolver, nugetPackageDownloader, userHome, tempPackagesDir.Value);
                await manifestUpdater.BackgroundUpdateAdvertisingManifestsWhenRequiredAsync();
            }
            catch (Exception)
            {
                // Never surface messages on background updates
            }
        }
Beispiel #5
0
        private string DownloadSamplePackage(PackageId packageId)
        {
            NuGetPackageDownloader nuGetPackageDownloader = new NuGetPackageDownloader(_tempDirectory, null,
                                                                                       new MockFirstPartyNuGetPackageSigningVerifier(),
                                                                                       _logger, restoreActionConfig: new RestoreActionConfig(NoCache: true));

            return(ExponentialRetry.ExecuteWithRetry <string>(
                       action: DownloadMostRecentSamplePackageFromPublicFeed,
                       shouldStopRetry: result => result != null,
                       maxRetryCount: 3,
                       timer: () => ExponentialRetry.Timer(ExponentialRetry.Intervals),
                       taskDescription: "Run command while retry transient restore error")
                   .ConfigureAwait(false).GetAwaiter().GetResult());

            string DownloadMostRecentSamplePackageFromPublicFeed()
            {
                try
                {
                    return(nuGetPackageDownloader.DownloadPackageAsync(
                               new PackageId("Microsoft.iOS.Ref"), null, includePreview: true,
                               packageSourceLocation: new PackageSourceLocation(
                                   sourceFeedOverrides: new[] { "https://api.nuget.org/v3/index.json" })).GetAwaiter()
                           .GetResult());
                }
                catch (Exception)
                {
                    return(null);
                }
            }
        }
        private string DownloadSamplePackage(PackageId packageId, NuGetVersion version,
                                             NuGetPackageDownloader nuGetPackageDownloader)
        {
            return(ExponentialRetry.ExecuteWithRetry(
                       DownloadMostRecentSamplePackageFromPublicFeed,
                       result => result != null,
                       3,
                       () => ExponentialRetry.Timer(ExponentialRetry.Intervals),
                       "Run command while retry transient restore error")
                   .ConfigureAwait(false).GetAwaiter().GetResult());

            string DownloadMostRecentSamplePackageFromPublicFeed()
            {
                try
                {
                    return(nuGetPackageDownloader.DownloadPackageAsync(
                               packageId, version, includePreview: true,
                               packageSourceLocation: new PackageSourceLocation(
                                   sourceFeedOverrides: new[] { "https://api.nuget.org/v3/index.json" })).GetAwaiter()
                           .GetResult());
                }
                catch (Exception)
                {
                    return(null);
                }
            }
        }
Beispiel #7
0
 public NuGetPackageInstallerTests(ITestOutputHelper log) : base(log)
 {
     _tempDirectory = GetUniqueTempProjectPathEachTest();
     _logger        = new NuGetTestLogger();
     _installer     =
         new NuGetPackageDownloader(_tempDirectory, null, new MockFirstPartyNuGetPackageSigningVerifier(),
                                    _logger, restoreActionConfig: new RestoreActionConfig(NoCache: true));
 }
        public async Task TestDownloadAllPackagesAsync01Async()
        {
            var packages = await nugetHelper.QueryNuGetAsync(httpClient, "dec");

            var downloader = new NuGetPackageDownloader(nugetHelper, remoteFile);

            var result = await downloader.DownloadAllPackagesAsync(packages);

            Assert.NotNull(result);
            Assert.Equal(packages.Count, result.Count);
            Assert.True(!string.IsNullOrEmpty(packages[0].LocalFilepath));
            Assert.True(File.Exists(packages[0].LocalFilepath));
        }
Beispiel #9
0
        public async Task It_installs_nuget_package()
        {
            var packageId      = "Humanizer";
            var packageVersion = "2.6.2";
            var logger         = new NuGetTestLogger();
            var installer      = new NuGetPackageDownloader(Directory.GetCurrentDirectory(), logger: logger);
            var packagePath    = await installer.DownloadPackageAsync(new PackageId(packageId), new NuGetVersion(packageVersion));

            logger.Errors.Should().Be(0);
            logger.Warnings.Should().Be(0);
            packagePath.Should().ContainEquivalentOf(packageId);
            packagePath.Should().Contain(packageVersion);
            File.Exists(packagePath).Should().BeTrue();
        }
Beispiel #10
0
        public async Task WhenCalledWithNotSignedPackageItShouldThrowWithCommandOutput()
        {
            string commandOutput = "COMMAND OUTPUT";
            NuGetPackageDownloader nuGetPackageDownloader = new NuGetPackageDownloader(_tempDirectory, null,
                                                                                       new MockFirstPartyNuGetPackageSigningVerifier(verifyResult: false, commandOutput: commandOutput),
                                                                                       _logger, restoreActionConfig: new RestoreActionConfig(NoCache: true));

            NuGetPackageInstallerException ex = await Assert.ThrowsAsync <NuGetPackageInstallerException>(() =>
                                                                                                          nuGetPackageDownloader.DownloadPackageAsync(
                                                                                                              TestPackageId,
                                                                                                              new NuGetVersion(TestPackageVersion),
                                                                                                              new PackageSourceLocation(sourceFeedOverrides: new[] { GetTestLocalFeedPath() })));

            ex.Message.Should().Contain(commandOutput);
        }
Beispiel #11
0
        public async Task It_extracts_nuget_package()
        {
            var packageId      = "Newtonsoft.Json";
            var packageVersion = "12.0.3";
            var logger         = new NuGetTestLogger();
            var installer      = new NuGetPackageDownloader(Directory.GetCurrentDirectory(), logger: logger);
            var packagePath    = await installer.DownloadPackageAsync(new PackageId(packageId), new NuGetVersion(packageVersion));

            var targetPath = Path.Combine(Directory.GetCurrentDirectory(), "ExtractedPackage");
            var result     = await installer.ExtractPackageAsync(packagePath, targetPath);

            Directory.Exists(targetPath).Should().BeTrue();
            var extractedFiles = Directory.GetFiles(targetPath, "*", SearchOption.AllDirectories);

            extractedFiles.Should().Contain(Path.Combine(targetPath, $"{packageId}.nuspec"));
            extractedFiles.Should().BeEquivalentTo(result);
        }
        public void GivenPackageNotInAllowListItCannotGetAllFilesNeedToSetExecutablePermission()
        {
            NuGetTestLogger        logger    = new NuGetTestLogger(Log);
            NuGetPackageDownloader installer =
                new NuGetPackageDownloader(new DirectoryPath(Directory.GetCurrentDirectory()), null, logger);
            var allFiles = new List <string>()
            {
                "/ExtractedPackage/Not.In.Allow.List.nuspec",
                "/ExtractedPackage/LICENSE",
                "/ExtractedPackage/PreserveLists/Java.Interop.xml",
                "/ExtractedPackage/tools/javadoc-to-mdoc.dll",
                "/ExtractedPackage/tools/javadoc-to-mdoc.runtimeconfig.json",
                "/ExtractedPackage/tools/Darwin/mono",
            };
            var result = installer.FindAllFilesNeedExecutablePermission(allFiles, "/ExtractedPackage");

            result.Should().BeEmpty("Not in allow list package");
        }
Beispiel #13
0
        private static WorkloadManifestUpdater GetInstance(string userProfileDir)
        {
            var reporter   = new NullReporter();
            var dotnetPath = Path.GetDirectoryName(Environment.ProcessPath);
            var sdkVersion = Product.Version;
            var workloadManifestProvider = new SdkDirectoryWorkloadManifestProvider(dotnetPath, sdkVersion, userProfileDir);
            var workloadResolver         = WorkloadResolver.Create(workloadManifestProvider, dotnetPath, sdkVersion, userProfileDir);
            var tempPackagesDir          = new DirectoryPath(Path.Combine(Path.GetTempPath(), "dotnet-sdk-advertising-temp"));
            var nugetPackageDownloader   = new NuGetPackageDownloader(tempPackagesDir,
                                                                      filePermissionSetter: null,
                                                                      new FirstPartyNuGetPackageSigningVerifier(tempPackagesDir, new NullLogger()),
                                                                      new NullLogger(),
                                                                      reporter);
            var workloadRecordRepo = WorkloadInstallerFactory.GetWorkloadInstaller(reporter, new SdkFeatureBand(sdkVersion), workloadResolver, Cli.VerbosityOptions.normal, userProfileDir)
                                     .GetWorkloadInstallationRecordRepository();

            return(new WorkloadManifestUpdater(reporter, workloadResolver, nugetPackageDownloader, userProfileDir, tempPackagesDir.Value, workloadRecordRepo));
        }
Beispiel #14
0
        public async Task GivenAFailedSourceAndIgnoreFailedSourcesItShouldNotThrowFatalProtocolException()
        {
            var installer =
                new NuGetPackageDownloader(_tempDirectory, null, new MockFirstPartyNuGetPackageSigningVerifier(),
                                           _logger, restoreActionConfig: new RestoreActionConfig(IgnoreFailedSources: true, NoCache: true));

            // should not throw FatalProtocolException
            // when there is at least one valid source, it should pass.
            // but it is hard to set up that in unit test
            await Assert.ThrowsAsync <NuGetPackageInstallerException>(() =>
                                                                      installer.DownloadPackageAsync(
                                                                          TestPackageId,
                                                                          new NuGetVersion(TestPackageVersion),
                                                                          new PackageSourceLocation(sourceFeedOverrides: new[]
            {
                "https://nonexist.nuget.source/F/nonexist/api/v3/index.json"
            })));
        }
        public void AttemptsOnlyOnceIfSuccessful()
        {
            var packageId       = "FakePackageId";
            var version         = new NuGetVersion(1, 2, 3);
            var feedUri         = new Uri("http://www.myget.org");
            var feedCredentials = new CredentialCache();
            var targetFilePath  = "FakeTargetFilePath";

            var calledCount = 0;
            var downloader  = new NuGetPackageDownloader();

            downloader.DownloadPackage(packageId, version, feedUri, feedCredentials, targetFilePath, (arg1, arg2, arg3, arg4, arg5) =>
            {
                calledCount++;
            });

            Assert.That(calledCount, Is.EqualTo(1));
        }
        public async Task ItCanExtractNugetPackage()
        {
            string                 packageId      = "Newtonsoft.Json";
            string                 packageVersion = "12.0.3";
            NuGetTestLogger        logger         = new NuGetTestLogger();
            NuGetPackageDownloader installer      =
                new NuGetPackageDownloader(new DirectoryPath(Directory.GetCurrentDirectory()), logger);
            string packagePath =
                await installer.DownloadPackageAsync(new PackageId(packageId), new NuGetVersion(packageVersion));

            string targetPath           = Path.Combine(Directory.GetCurrentDirectory(), "ExtractedPackage");
            IEnumerable <string> result = await installer.ExtractPackageAsync(packagePath, targetPath);

            Directory.Exists(targetPath).Should().BeTrue();
            string[] extractedFiles = Directory.GetFiles(targetPath, "*", SearchOption.AllDirectories);
            extractedFiles.Should().Contain(Path.Combine(targetPath, $"{packageId}.nuspec"));
            extractedFiles.Should().BeEquivalentTo(result);
        }
Beispiel #17
0
        public void AttemptsOnlyOnceIfSuccessful()
        {
            var packageId       = "FakePackageId";
            var version         = VersionFactory.CreateSemanticVersion(1, 2, 3);
            var feedUri         = new Uri("http://www.myget.org");
            var feedCredentials = new CredentialCache();
            var targetFilePath  = "FakeTargetFilePath";
            var filesystem      = Substitute.For <ICalamariFileSystem>();

            var calledCount = 0;
            var downloader  = new NuGetPackageDownloader(filesystem);

            downloader.DownloadPackage(packageId, version, feedUri, feedCredentials, targetFilePath, maxDownloadAttempts: 5, downloadAttemptBackoff: TimeSpan.Zero, action: (arg1, arg2, arg3, arg4, arg5) =>
            {
                calledCount++;
            });

            Assert.That(calledCount, Is.EqualTo(1));
        }
        public void ItCanGetAllFilesNeedToSetExecutablePermission()
        {
            NuGetTestLogger        logger    = new NuGetTestLogger(Log);
            NuGetPackageDownloader installer =
                new NuGetPackageDownloader(new DirectoryPath(Directory.GetCurrentDirectory()), null, logger);
            var allFiles = new List <string>()
            {
                "/ExtractedPackage/Microsoft.Android.Sdk.Darwin.nuspec",
                "/ExtractedPackage/LICENSE",
                "/ExtractedPackage/PreserveLists/Java.Interop.xml",
                "/ExtractedPackage/tools/javadoc-to-mdoc.dll",
                "/ExtractedPackage/tools/javadoc-to-mdoc.runtimeconfig.json",
                "/ExtractedPackage/tools/Darwin/mono",
            };
            var result = installer.FindAllFilesNeedExecutablePermission(allFiles, "/ExtractedPackage");

            result.Should().HaveCount(1);
            result.First().Value.Should().Be("/ExtractedPackage/tools/Darwin/mono",
                                             "file without extension under tools folder");
        }
        public async Task ItCanExtractNugetPackage()
        {
            string                 packageId      = "Newtonsoft.Json";
            string                 packageVersion = "12.0.3";
            NuGetTestLogger        logger         = new NuGetTestLogger(Log);
            NuGetPackageDownloader installer      =
                new NuGetPackageDownloader(new DirectoryPath(Directory.GetCurrentDirectory()), null,
                                           new MockFirstPartyNuGetPackageSigningVerifier(), logger, restoreActionConfig: new RestoreActionConfig(NoCache: true));
            string packagePath =
                await installer.DownloadPackageAsync(new PackageId(packageId), new NuGetVersion(packageVersion));

            string targetPath = Path.Combine(Path.Combine(Path.GetTempPath(), Path.GetRandomFileName()),
                                             "ExtractedPackage");
            IEnumerable <string> result = await installer.ExtractPackageAsync(packagePath, new DirectoryPath(targetPath));

            Directory.Exists(targetPath).Should().BeTrue();
            string[] extractedFiles = Directory.GetFiles(targetPath, "*", SearchOption.AllDirectories);
            extractedFiles.Should().Contain(Path.Combine(targetPath, $"{packageId}.nuspec"));
            extractedFiles.Should().BeEquivalentTo(result);
        }
Beispiel #20
0
        public async Task GivenANonWindowsMachineItShouldPrintMessageOnce()
        {
            BufferedReporter       bufferedReporter       = new BufferedReporter();
            NuGetPackageDownloader nuGetPackageDownloader = new NuGetPackageDownloader(_tempDirectory, null,
                                                                                       new MockFirstPartyNuGetPackageSigningVerifier(isExecutableIsFirstPartySignedWithoutValidation: false),
                                                                                       _logger, bufferedReporter, restoreActionConfig: new RestoreActionConfig(NoCache: true));
            await nuGetPackageDownloader.DownloadPackageAsync(
                TestPackageId,
                new NuGetVersion(TestPackageVersion),
                new PackageSourceLocation(sourceFeedOverrides : new[] { GetTestLocalFeedPath() }));

            // download 2 packages should only print the message once
            string packagePath = await nuGetPackageDownloader.DownloadPackageAsync(
                TestPackageId,
                new NuGetVersion(TestPackageVersion),
                new PackageSourceLocation(sourceFeedOverrides : new[] { GetTestLocalFeedPath() }));

            bufferedReporter.Lines.Should()
            .ContainSingle(
                LocalizableStrings.SkipNuGetpackageSigningValidationmacOSLinux);
            File.Exists(packagePath).Should().BeTrue();
        }
Beispiel #21
0
        public int AttemptsTheRightNumberOfTimesOnError(int maxDownloadAttempts)
        {
            var packageId       = "FakePackageId";
            var version         = new NuGetVersion(1, 2, 3);
            var feedUri         = new Uri("http://www.myget.org");
            var feedCredentials = new CredentialCache();
            var targetFilePath  = "FakeTargetFilePath";

            var calledCount = 0;

            Assert.Throws <Exception>(() =>
            {
                var downloader = new NuGetPackageDownloader();
                downloader.DownloadPackage(packageId, version, feedUri, feedCredentials, targetFilePath, maxDownloadAttempts: maxDownloadAttempts, downloadAttemptBackoff: TimeSpan.Zero,
                                           action: (arg1, arg2, arg3, arg4, arg5) =>
                {
                    calledCount++;
                    throw new Exception("Expected exception from test: simulate download failing");
                });
            });

            return(calledCount);
        }
Beispiel #22
0
        // https://aka.ms/netsdkinternal-certificate-rotate
        public void ItShouldHaveUpdateToDateCertificateSha()
        {
            NuGetPackageDownloader nuGetPackageDownloader = new NuGetPackageDownloader(_tempDirectory, null,
                                                                                       new MockFirstPartyNuGetPackageSigningVerifier(),
                                                                                       _logger, restoreActionConfig: new RestoreActionConfig(NoCache: true));

            var samplePackage = ExponentialRetry.ExecuteWithRetry <string>(
                action: DownloadMostRecentSamplePackageFromPublicFeed,
                shouldStopRetry: result => result != null,
                maxRetryCount: 3,
                timer: () => ExponentialRetry.Timer(ExponentialRetry.Intervals),
                taskDescription: "Run command while retry transient restore error")
                                .ConfigureAwait(false).GetAwaiter().GetResult();

            string DownloadMostRecentSamplePackageFromPublicFeed()
            {
                try
                {
                    return(nuGetPackageDownloader.DownloadPackageAsync(
                               new PackageId("Microsoft.iOS.Ref"), null, includePreview: true,
                               packageSourceLocation: new PackageSourceLocation(
                                   sourceFeedOverrides: new[] { "https://api.nuget.org/v3/index.json" })).GetAwaiter()
                           .GetResult());
                }
                catch (Exception)
                {
                    return(null);
                }
            }

            var    firstPartyNuGetPackageSigningVerifier = new FirstPartyNuGetPackageSigningVerifier();
            string shaFromPackage = GetShaFromSamplePackage(samplePackage);

            firstPartyNuGetPackageSigningVerifier._firstPartyCertificateThumbprints.Contains(shaFromPackage).Should()
            .BeTrue(
                $"Add {shaFromPackage} to the _firstPartyCertificateThumbprints of FirstPartyNuGetPackageSigningVerifier class. More info https://aka.ms/netsdkinternal-certificate-rotate");
        }
Beispiel #23
0
 public NuGetPackageInstallerTests(ITestOutputHelper log) : base(log)
 {
     _tempDirectory = GetUniqueTempProjectPathEachTest();
     _logger        = new NuGetTestLogger();
     _installer     = new NuGetPackageDownloader(_tempDirectory, null, _logger);
 }