public void DownloadPackageWithUnverifiedPackageThrowsInvalidDataException() { // Arrange var response = new Mock <WebResponse>(); response.Setup(r => r.GetResponseStream()).Returns(new MemoryStream(new byte[] { 123 })); var request = new Mock <WebRequest>(); request.Setup(r => r.GetResponse()).Returns(response.Object); var httpClient = new Mock <IHttpClient>(); httpClient.Setup(c => c.CreateRequest(It.IsAny <Uri>(), It.IsAny <bool>())).Returns(request.Object); var hashProvider = new Mock <IHashProvider>(); hashProvider.Setup(h => h.VerifyHash(It.IsAny <byte[]>(), It.IsAny <byte[]>())).Returns(false); var packageFactory = new Mock <IPackageFactory>(); packageFactory.Setup(f => f.CreatePackage(It.IsAny <Func <Stream> >())).Returns(new Mock <IPackage>().Object).Callback <Func <Stream> >(streamFactory => streamFactory()); var downloader = new PackageDownloader(httpClient.Object, packageFactory.Object, hashProvider.Object); var package = PackageUtility.CreatePackage("A", "1.0"); // Act, Assert ExceptionAssert.Throws <InvalidDataException>(() => downloader.DownloadPackage(new Uri("http://example.com/"), new byte[0], package)); }
private IEnumerable <PackageIdentity> CreatePackageIdentityFromNupkgPath() { PackageIdentity identity = null; IPackage package = null; try { // Example: install-package2 https://az320820.vo.msecnd.net/packages/microsoft.aspnet.mvc.4.0.20505.nupkg if (_isHttp) { PackageIdentity packageIdentity = ParsePackageIdentityFromHttpSource(Id); IPackageCacheRepository packageCache = this.Projects.FirstOrDefault().TryGetFeature <IPackageCacheRepository>(); IPackageName packageName = CoreConverters.SafeToPackageName(packageIdentity); SemanticVersion packageSemVer = CoreConverters.SafeToSemanticVersion(packageIdentity.Version); Uri downloadUri = new Uri(Id); PackageDownloader downloader = new PackageDownloader(); // Try to download the package through the cache. bool success = packageCache.InvokeOnPackage( packageIdentity.Id, packageSemVer, (targetStream) => downloader.DownloadPackage( new HttpClient(downloadUri), packageName, targetStream)); if (success) { // Try to get it from the cache again package = packageCache.FindPackage(packageIdentity.Id, packageSemVer); } } else { // Example: install-package2 c:\temp\packages\jQuery.1.10.2.nupkg string fullPath = Path.GetFullPath(Id); package = new OptimizedZipPackage(fullPath); } if (package != null) { Id = package.Id; Version = package.Version.ToString(); identity = new PackageIdentity(Id, NuGetVersion.Parse(Version)); } } catch (Exception ex) { Log(MessageLevel.Error, Resources.Cmdlet_FailToParsePackages, Id, ex.Message); } return(new List <PackageIdentity>() { identity }); }
public static void GetPackageAndDependencies(string packageId, string packageVersion, string sourceServer, string targetFolder, bool overwriteExistingFiles, bool includePrerelease, bool allowUnlisted, DependencyVersionTypeToDownload depVersionToDownload) { log.Trace($"Entered GetPackageAndDependencies(packageId='{packageId}', packageVersion='{packageVersion}', sourceServer='{sourceServer}', targetFolder='{targetFolder}', overwriteExistingFiles={overwriteExistingFiles}, includePrerelease={includePrerelease}, allowUnlisted={allowUnlisted}, depVersionToDownload={depVersionToDownload})"); var repo = PackageRepositoryFactory.Default.CreateRepository(sourceServer); var package = repo.FindPackage(packageId, packageVersion == null?null:new SemanticVersion(packageVersion), NullConstraintProvider.Instance, includePrerelease, allowUnlisted) as DataServicePackage; if (package == null) { log.Warn($"Package '{packageId} {packageVersion}' could not be found in the repository '{sourceServer}', or it could be converted as DataServicePackage"); return; } var finalPackagePath = Path.Combine(targetFolder, $"{package.Id}.{package.Version}.nupkg"); if (File.Exists(finalPackagePath) && !overwriteExistingFiles) { log.Info($"Skipping '{finalPackagePath}'"); return; } if (!Directory.Exists(targetFolder)) { Directory.CreateDirectory(targetFolder); } using (var fs = File.Open(finalPackagePath, FileMode.Create)) { log.Debug($"Downloading package '{package.Id}' from '{package.DownloadUrl}' ... "); var downloader = new PackageDownloader(); downloader.DownloadPackage(package.DownloadUrl, package, fs); log.Info($"Package {package.Id} downloaded!"); } foreach (var dset in package.DependencySets.Where(dset => dset.Dependencies.Count > 0)) { log.Debug($"Processing dependency set: {dset.TargetFramework?.ToString() ?? "<default set>"} "); foreach (var dep in dset.Dependencies) { log.Debug($"Processing dependency '{dep.Id}'"); var dependencyVersion = depVersionToDownload == DependencyVersionTypeToDownload.Max ? dep.VersionSpec?.MaxVersion?.ToString() : dep.VersionSpec?.MinVersion?.ToString(); GetPackageAndDependencies(dep.Id, dependencyVersion, sourceServer, targetFolder, overwriteExistingFiles, includePrerelease, allowUnlisted, depVersionToDownload); } } log.Trace("Exiting GetPackageAndDependencies"); }
public void DownloadPackageWithUnverifiedPackageThrowsInvalidDataException() { // Arrange var downloadClient = new Mock<IHttpClient>(); downloadClient.Setup(c => c.DownloadData()).Returns(new byte[] { 123 }); var hashProvider = new Mock<IHashProvider>(); hashProvider.Setup(h => h.VerifyHash(It.IsAny<byte[]>(), It.IsAny<byte[]>())).Returns(false); var packageFactory = new Mock<IPackageFactory>(); packageFactory.Setup(f => f.CreatePackage(It.IsAny<Func<Stream>>())).Returns(new Mock<IPackage>().Object).Callback<Func<Stream>>(streamFactory => streamFactory()); var downloader = new PackageDownloader(packageFactory.Object, hashProvider.Object); var package = PackageUtility.CreatePackage("A", "1.0"); // Act, Assert ExceptionAssert.Throws<InvalidDataException>(() => downloader.DownloadPackage(downloadClient.Object, new byte[0], package)); }
private static void DownloadPackage(IPackage package, string fullPathToDownloadTo, PackageDownloader directDownloader) { Log.VerboseFormat("Found package {0} v{1}", package.Id, package.Version); Log.Verbose("Downloading to: " + fullPathToDownloadTo); if (package is DataServicePackage dsp && directDownloader != null) { Log.Verbose("A direct download is possible; bypassing the NuGet machine cache"); using (var targetFile = new FileStream(fullPathToDownloadTo, FileMode.CreateNew)) directDownloader.DownloadPackage(dsp.DownloadUrl, dsp, targetFile); return; } var physical = new PhysicalFileSystem(Path.GetDirectoryName(fullPathToDownloadTo)); var local = new LocalPackageRepository(new FixedFilePathResolver(package.Id, fullPathToDownloadTo), physical); local.AddPackage(package); }
static void RunPackageDownloader(BlockingCollection <DataServicePackage> packages, string localDirectory) { while (!packages.IsCompleted) { DataServicePackage p; try { p = packages.Take(); } catch (InvalidOperationException) { Console.WriteLine("Adding was completed!"); return; } var sb = new StringBuilder(); var pkgFilename = sb.Append(p.Id).Append(".").Append(p.Version).Append(@".nupkg"); var packageFile = Path.Combine(localDirectory, pkgFilename.ToString()); if (!File.Exists(packageFile)) { Console.WriteLine("Package {0} does not exists. Downloading...", p); using (var fs = new FileStream(packageFile, FileMode.Create)) { try { var pd = new PackageDownloader(); pd.DownloadPackage(p.DownloadUrl, p, fs); } catch (Exception e) { Console.WriteLine(e); } } } else { Console.WriteLine("Package {0} exists", p); } } Console.WriteLine("\r\nNo more items to take. Press the Enter key to exit."); }
public void DownloadPackageWithUnverifiedPackageThrowsInvalidDataException() { // Arrange var response = new Mock<WebResponse>(); response.Setup(r => r.GetResponseStream()).Returns(new MemoryStream(new byte[] { 123 })); var request = new Mock<WebRequest>(); request.Setup(r => r.GetResponse()).Returns(response.Object); var httpClient = new Mock<IHttpClient>(); httpClient.Setup(c => c.CreateRequest(It.IsAny<Uri>(), It.IsAny<bool>())).Returns(request.Object); var hashProvider = new Mock<IHashProvider>(); hashProvider.Setup(h => h.VerifyHash(It.IsAny<byte[]>(), It.IsAny<byte[]>())).Returns(false); var packageFactory = new Mock<IPackageFactory>(); packageFactory.Setup(f => f.CreatePackage(It.IsAny<Func<Stream>>())).Returns(new Mock<IPackage>().Object).Callback<Func<Stream>>(streamFactory => streamFactory()); var downloader = new PackageDownloader(httpClient.Object, packageFactory.Object, hashProvider.Object); var package = PackageUtility.CreatePackage("A", "1.0"); // Act, Assert ExceptionAssert.Throws<InvalidDataException>(() => downloader.DownloadPackage(new Uri("http://example.com/"), new byte[0], package)); }
internal static IPackage GetPackage(IPackageCacheRepository packageCache, PackageIdentity packageIdentity, Uri downloadUri) { var packageSemVer = CoreConverters.SafeToSemanticVersion(packageIdentity.Version); var packageName = CoreConverters.SafeToPackageName(packageIdentity); var downloader = new PackageDownloader(); IPackage package = null; if (packageCache != null) { package = packageCache.FindPackage(packageName.Id, packageSemVer); if (package != null) { NuGetTraceSources.ActionExecutor.Info( "download/cachehit", "[{0}] Download: Cache Hit!", packageIdentity); // Success! return(package); } if (downloadUri == null) { throw new InvalidOperationException(String.Format( CultureInfo.CurrentCulture, Strings.DownloadActionHandler_NoDownloadUrl, packageIdentity)); } // Try to download the package through the cache. bool success = packageCache.InvokeOnPackage( packageIdentity.Id, packageSemVer, (targetStream) => downloader.DownloadPackage( new HttpClient(downloadUri), packageName, targetStream)); if (success) { NuGetTraceSources.ActionExecutor.Info( "download/downloadedtocache", "[{0}] Download: Downloaded to cache", packageName); // Try to get it from the cache again package = packageCache.FindPackage(packageIdentity.Id, packageSemVer); } } // Either: // 1. We failed to load the package into the cache, which can happen when // access to the %LocalAppData% directory is blocked, // e.g. on Windows Azure Web Site build OR // B. It was purged from the cache before it could be retrieved again. // Regardless, the cache isn't working for us, so download it in to memory. if (package == null) { NuGetTraceSources.ActionExecutor.Info( "download/cachefailing", "[{0}] Download: Cache isn't working. Downloading to RAM", packageName); using (var targetStream = new MemoryStream()) { downloader.DownloadPackage( new HttpClient(downloadUri), packageName, targetStream); targetStream.Seek(0, SeekOrigin.Begin); package = new ZipPackage(targetStream); } } return(package); }
internal static IPackage GetPackage(IPackageCacheRepository packageCache, PackageIdentity packageIdentity, Uri downloadUri) { var packageSemVer = CoreConverters.SafeToSemanticVersion(packageIdentity.Version); var packageName = CoreConverters.SafeToPackageName(packageIdentity); var downloader = new PackageDownloader(); IPackage package = null; if (packageCache != null) { package = packageCache.FindPackage(packageName.Id, packageSemVer); if (package != null) { NuGetTraceSources.ActionExecutor.Info( "download/cachehit", "[{0}] Download: Cache Hit!", packageIdentity); // Success! return package; } if (downloadUri == null) { throw new InvalidOperationException(String.Format( CultureInfo.CurrentCulture, Strings.DownloadActionHandler_NoDownloadUrl, packageIdentity)); } // Try to download the package through the cache. bool success = packageCache.InvokeOnPackage( packageIdentity.Id, packageSemVer, (targetStream) => downloader.DownloadPackage( new HttpClient(downloadUri), packageName, targetStream)); if (success) { NuGetTraceSources.ActionExecutor.Info( "download/downloadedtocache", "[{0}] Download: Downloaded to cache", packageName); // Try to get it from the cache again package = packageCache.FindPackage(packageIdentity.Id, packageSemVer); } } // Either: // 1. We failed to load the package into the cache, which can happen when // access to the %LocalAppData% directory is blocked, // e.g. on Windows Azure Web Site build OR // B. It was purged from the cache before it could be retrieved again. // Regardless, the cache isn't working for us, so download it in to memory. if (package == null) { NuGetTraceSources.ActionExecutor.Info( "download/cachefailing", "[{0}] Download: Cache isn't working. Downloading to RAM", packageName); using (var targetStream = new MemoryStream()) { downloader.DownloadPackage( new HttpClient(downloadUri), packageName, targetStream); targetStream.Seek(0, SeekOrigin.Begin); package = new ZipPackage(targetStream); } } return package; }
private IEnumerable<PackageIdentity> CreatePackageIdentityFromNupkgPath() { PackageIdentity identity = null; IPackage package = null; try { // Example: install-package2 https://az320820.vo.msecnd.net/packages/microsoft.aspnet.mvc.4.0.20505.nupkg if (_isHttp) { PackageIdentity packageIdentity = ParsePackageIdentityFromHttpSource(Id); IPackageCacheRepository packageCache = this.Projects.FirstOrDefault().TryGetFeature<IPackageCacheRepository>(); IPackageName packageName = CoreConverters.SafeToPackageName(packageIdentity); SemanticVersion packageSemVer = CoreConverters.SafeToSemanticVersion(packageIdentity.Version); Uri downloadUri = new Uri(Id); PackageDownloader downloader = new PackageDownloader(); // Try to download the package through the cache. bool success = packageCache.InvokeOnPackage( packageIdentity.Id, packageSemVer, (targetStream) => downloader.DownloadPackage( new HttpClient(downloadUri), packageName, targetStream)); if (success) { // Try to get it from the cache again package = packageCache.FindPackage(packageIdentity.Id, packageSemVer); } } else { // Example: install-package2 c:\temp\packages\jQuery.1.10.2.nupkg string fullPath = Path.GetFullPath(Id); package = new OptimizedZipPackage(fullPath); } if (package != null) { Id = package.Id; Version = package.Version.ToString(); identity = new PackageIdentity(Id, NuGetVersion.Parse(Version)); } } catch (Exception ex) { Log(MessageLevel.Error, Resources.Cmdlet_FailToParsePackages, Id, ex.Message); } return new List<PackageIdentity>() { identity }; }