public async Task SavesCertificateToStorage() { // Arrange var certificate = TestResources.GetTestCertificate(TestResources.TrustedCARootCertificate); var sha256Thumbprint = certificate.ComputeSHA256Thumbprint(); var expectedFileName = GetExpectedFileName(sha256Thumbprint); var expectedUri = GetExpectedUri(sha256Thumbprint); var cancellationToken = CancellationToken.None; var content = new StreamStorageContent(new MemoryStream(certificate.RawData)); var storage = new Mock <IStorage>(MockBehavior.Strict); storage .Setup(m => m.ResolveUri(expectedFileName)) .Returns(expectedUri).Verifiable(); storage .Setup(m => m.Save(expectedUri, It.IsAny <StreamStorageContent>(), false, cancellationToken)) .Callback <Uri, StorageContent, bool, CancellationToken>((uri, sc, overwrite, ct) => { Assert.Equal(certificate.RawData, ToByteArray(sc)); }) .Returns(Task.CompletedTask) .Verifiable(); var logger = new Mock <ILogger <CertificateStore> >(MockBehavior.Loose); var certificateStore = new CertificateStore(storage.Object, logger.Object); // Act await certificateStore.SaveAsync(certificate, cancellationToken); // Assert storage.Verify(); }
public async Task ProperlyRoundTripsBytes(bool compressContent) { // Arrange await CreateBlobAsync(_dirPath, BlobName); _target.CompressContent = compressContent; var inputBytes = new byte[] { 0x80 }; var inputContent = new StreamStorageContent(new MemoryStream(inputBytes)); await _target.Save(_resourceUri, inputContent, overwrite : true, cancellationToken : CancellationToken.None); // Act var response = await _target.Load(_resourceUri, CancellationToken.None); // Assert using (var stream = response.GetContentStream()) { var buffer = new MemoryStream(); await stream.CopyToAsync(buffer); var outputBytes = buffer.ToArray(); Assert.Equal(inputBytes, outputBytes); } }
private async Task ExtractAndStoreIconAsync( Stream packageStream, string iconPath, Storage destinationStorage, Uri destinationUri, CancellationToken cancellationToken, string packageId, string normalizedPackageVersion) { using (var zipArchive = new ZipArchive(packageStream, ZipArchiveMode.Read, leaveOpen: true)) { var iconEntry = zipArchive.Entries.FirstOrDefault(e => e.FullName.Equals(iconPath, StringComparison.InvariantCultureIgnoreCase)); if (iconEntry != null) { using (var iconStream = iconEntry.Open()) { _logger.LogInformation("Extracting icon to the destination storage {DestinationUri}", destinationUri); // TODO: align the mime type determination with Gallery https://github.com/nuget/nugetgallery/issues/7061 var iconContent = new StreamStorageContent(iconStream, string.Empty, DnxConstants.DefaultCacheControl); await destinationStorage.SaveAsync(destinationUri, iconContent, cancellationToken); _logger.LogInformation("Done"); } } else { _telemetryService.TrackIconExtractionFailure(packageId, normalizedPackageVersion); _logger.LogWarning("Zip archive entry {IconPath} does not exist", iconPath); } } }
private async Task <Uri> SaveNupkgAsync(Stream nupkgStream, Storage storage, string id, string version, CancellationToken cancellationToken) { Uri nupkgUri = new Uri(storage.BaseAddress, GetRelativeAddressNupkg(id, version)); var content = new StreamStorageContent( nupkgStream, DnxConstants.ApplicationOctetStreamContentType, DnxConstants.DefaultCacheControl); await storage.SaveAsync(nupkgUri, content, cancellationToken); return(nupkgUri); }