Esempio n. 1
0
        public void HttpStreamValidation_ValidateNupkg_RejectsPartialNupkg()
        {
            // Arrange
            using (var zipStream = new MemoryStream())
                using (var stream = new MemoryStream())
                {
                    using (var zip = new ZipArchive(zipStream, ZipArchiveMode.Create, leaveOpen: true))
                    {
                        zip.AddEntry("package.nuspec", new byte[0]);
                    }

                    zipStream.Seek(0, SeekOrigin.Begin);
                    var partialLength = (int)zipStream.Length - 1;
                    stream.Write(zipStream.ToArray(), 0, partialLength);
                    stream.Seek(0, SeekOrigin.Begin);

                    // Act & Assert
                    var actual = Assert.Throws <InvalidDataException>(() =>
                    {
                        HttpStreamValidation.ValidateNupkg(
                            Uri,
                            stream);
                    });

                    Assert.IsType <InvalidDataException>(actual.InnerException);
                }
        }
Esempio n. 2
0
        public void HttpStreamValidation_ValidateNupkg_RejectsNupkgWithBadPrefix()
        {
            // Arrange
            using (var zipStream = new MemoryStream())
                using (var stream = new MemoryStream())
                {
                    using (var zip = new ZipArchive(zipStream, ZipArchiveMode.Create, leaveOpen: true))
                    {
                        zip.AddEntry("package.nuspec", new byte[0]);
                    }

                    zipStream.Seek(0, SeekOrigin.Begin);

                    var badPrefix = Encoding.ASCII.GetBytes("bad!!!");
                    stream.Write(badPrefix, 0, badPrefix.Length);
                    zipStream.CopyTo(stream);
                    stream.Seek(0, SeekOrigin.Begin);

                    // Act & Assert
                    var actual = Assert.Throws <InvalidDataException>(() =>
                    {
                        HttpStreamValidation.ValidateNupkg(
                            Uri,
                            stream);
                    });

                    Assert.IsType <InvalidDataException>(actual.InnerException);
                }
        }
Esempio n. 3
0
        public void HttpStreamValidation_ValidateNupkg_RejectsCompletelyInvalidZipNupkg()
        {
            // Arrange
            using (var stream = new MemoryStream(Encoding.ASCII.GetBytes("not a zip!")))
            {
                // Act & Assert
                var actual = Assert.Throws <InvalidDataException>(() =>
                {
                    HttpStreamValidation.ValidateNupkg(
                        Uri,
                        stream);
                });

                Assert.IsType <InvalidDataException>(actual.InnerException);
            }
        }
        private async Task <NupkgEntry> OpenNupkgStreamAsyncCore(PackageInfo package, CancellationToken cancellationToken)
        {
            for (var retry = 0; retry != 3; ++retry)
            {
                try
                {
                    using (var data = await _httpSource.GetAsync(
                               package.ContentUri,
                               "nupkg_" + package.Id + "." + package.Version,
                               CreateCacheContext(retry),
                               Logger,
                               ignoreNotFounds: false,
                               ensureValidContents: stream => HttpStreamValidation.ValidateNupkg(package.ContentUri, stream),
                               cancellationToken: cancellationToken))
                    {
                        return(new NupkgEntry
                        {
                            TempFileName = data.CacheFileName
                        });
                    }
                }
                catch (TaskCanceledException) when(retry < 2)
                {
                    // Requests can get cancelled if we got the data from elsewhere, no reason to warn.
                    string message = string.Format(CultureInfo.CurrentCulture, Strings.Log_CanceledNupkgDownload, package.ContentUri);

                    Logger.LogMinimal(message);
                }
                catch (Exception ex)
                {
                    var message = string.Format(CultureInfo.CurrentCulture, Strings.Log_FailedToDownloadPackage, package.ContentUri)
                                  + Environment.NewLine
                                  + ExceptionUtilities.DisplayMessage(ex);
                    if (retry == 2)
                    {
                        Logger.LogError(message);
                    }
                    else
                    {
                        Logger.LogMinimal(message);
                    }
                }
            }

            return(null);
        }
Esempio n. 5
0
        public void HttpStreamValidation_ValidateNupkg_AcceptsMinimalNupkg()
        {
            // Arrange
            using (var stream = new MemoryStream())
            {
                using (var zip = new ZipArchive(stream, ZipArchiveMode.Create, leaveOpen: true))
                {
                    zip.AddEntry("package.nuspec", @"<?xml version=""1.0"" encoding=""utf-8""?>
<package>
</package>");
                }

                stream.Seek(0, SeekOrigin.Begin);

                // Act & Assert
                HttpStreamValidation.ValidateNupkg(
                    Uri,
                    stream);
            }
        }
        private async Task <NupkgEntry> OpenNupkgStreamAsyncCore(RemoteSourceDependencyInfo package, CancellationToken cancellationToken)
        {
            for (var retry = 0; retry != 3; ++retry)
            {
                try
                {
                    using (var data = await _httpSource.GetAsync(
                               package.ContentUri,
                               "nupkg_" + package.Identity.Id + "." + package.Identity.Version.ToNormalizedString(),
                               CreateCacheContext(retry),
                               Logger,
                               ignoreNotFounds: false,
                               ensureValidContents: stream => HttpStreamValidation.ValidateNupkg(package.ContentUri, stream),
                               cancellationToken: cancellationToken))
                    {
                        return(new NupkgEntry
                        {
                            TempFileName = data.CacheFileName
                        });
                    }
                }
                catch (Exception ex) when(retry < 2)
                {
                    var message = string.Format(CultureInfo.CurrentCulture, Strings.Log_FailedToDownloadPackage, package.ContentUri)
                                  + Environment.NewLine
                                  + ExceptionUtilities.DisplayMessage(ex);

                    Logger.LogMinimal(message);
                }
                catch (Exception ex) when(retry == 2)
                {
                    var message = string.Format(CultureInfo.CurrentCulture, Strings.Log_FailedToDownloadPackage, package.ContentUri)
                                  + Environment.NewLine
                                  + ExceptionUtilities.DisplayMessage(ex);

                    Logger.LogError(message);
                }
            }

            return(null);
        }
Esempio n. 7
0
        public void HttpStreamValidation_ValidateNupkg_RejectsBrokenNuspec()
        {
            // Arrange
            using (var stream = new MemoryStream())
            {
                using (var zip = new ZipArchive(stream, ZipArchiveMode.Create, leaveOpen: true))
                {
                    zip.AddEntry("package.nuspec", new byte[0]);
                }

                stream.Seek(0, SeekOrigin.Begin);

                // Act & Assert
                var actual = Assert.Throws <InvalidDataException>(() =>
                {
                    HttpStreamValidation.ValidateNupkg(
                        Uri,
                        stream);
                });

                Assert.IsType <XmlException>(actual.InnerException);
            }
        }