public bool HandleResponseSuccess(HttpResponseMessage response) { long?contentLength = response.Content.Headers.ContentLength; if (contentLength < 0 || contentLength == null) { throw new RegistryErrorExceptionBuilder(GetActionDescription()) .AddReason("Did not receive Content-Length header") .Build(); } if (blobDescriptor.GetSize() > 0 && contentLength.GetValueOrDefault() != blobDescriptor.GetSize()) { throw new RegistryErrorExceptionBuilder(GetActionDescription()) .AddReason($"Expected size {blobDescriptor.GetSize()} but got {contentLength.GetValueOrDefault()}") .Build(); } return(true); }
/** * Gets the manifest as a JSON template. The {@code containerConfigurationBlobDescriptor} must be * the {@link BlobDescriptor} obtained by writing out the container configuration JSON returned * from {@link #getContainerConfiguration()}. * * @param <T> child type of {@link BuildableManifestTemplate}. * @param manifestTemplateClass the JSON template to translate the image to. * @param containerConfigurationBlobDescriptor the container configuration descriptor. * @return the image contents serialized as JSON. */ public IBuildableManifestTemplate GetManifestTemplate( ManifestFormat manifestFormat, BlobDescriptor containerConfigurationBlobDescriptor) { containerConfigurationBlobDescriptor = containerConfigurationBlobDescriptor ?? throw new ArgumentNullException(nameof(containerConfigurationBlobDescriptor)); try { IBuildableManifestTemplate template; // ISet up the JSON template. switch (manifestFormat) { case ManifestFormat.V22: template = new V22ManifestTemplate(); break; case ManifestFormat.OCI: template = new OCIManifestTemplate(); break; default: throw new ArgumentOutOfRangeException(nameof(manifestFormat)); } IBuildableManifestTemplate buildableTemplate = template; // Adds the container configuration reference. DescriptorDigest containerConfigurationDigest = containerConfigurationBlobDescriptor.GetDigest(); long containerConfigurationSize = containerConfigurationBlobDescriptor.GetSize(); buildableTemplate.SetContainerConfiguration(containerConfigurationSize, containerConfigurationDigest); // Adds the layers. foreach (ILayer layer in image.GetLayers()) { buildableTemplate.AddLayer( layer.GetBlobDescriptor().GetSize(), layer.GetBlobDescriptor().GetDigest()); } // Serializes into JSON. return(template); } catch (JsonException ex) { throw new ArgumentException(manifestFormat + " cannot be instantiated", ex); } }
public async Task Test_smokeTestAsync() { foreach (KeyValuePair <string, string> knownHash in KNOWN_SHA256_HASHES) { string toHash = knownHash.Key; string expectedHash = knownHash.Value; byte[] bytesToHash = Encoding.UTF8.GetBytes(toHash); Stream underlyingOutputStream = new MemoryStream(); using (CountingDigestOutputStream countingDigestOutputStream = new CountingDigestOutputStream(underlyingOutputStream)) using (Stream toHashInputStream = new MemoryStream(bytesToHash)) { await toHashInputStream.CopyToAsync(countingDigestOutputStream).ConfigureAwait(false); BlobDescriptor blobDescriptor = countingDigestOutputStream.ComputeDigest(); Assert.AreEqual(DescriptorDigest.FromHash(expectedHash), blobDescriptor.GetDigest()); Assert.AreEqual(bytesToHash.Length, blobDescriptor.GetSize()); } } }
private async Task VerifyCachedLayerAsync(CachedLayer cachedLayer, IBlob uncompressedLayerBlob) { BlobDescriptor layerBlobDescriptor = await GetDigestAsync(Compress(uncompressedLayerBlob)).ConfigureAwait(false); BlobDescriptor layerDiffDescriptor = await GetDigestAsync(uncompressedLayerBlob).ConfigureAwait(false); DescriptorDigest layerDiffId = layerDiffDescriptor.GetDigest(); // Verifies cachedLayer is correct. Assert.AreEqual(layerBlobDescriptor.GetDigest(), cachedLayer.GetDigest()); Assert.AreEqual(layerDiffId, cachedLayer.GetDiffId()); Assert.AreEqual(layerBlobDescriptor.GetSize(), cachedLayer.GetSize()); CollectionAssert.AreEqual( await Blobs.WriteToByteArrayAsync(uncompressedLayerBlob).ConfigureAwait(false), await Blobs.WriteToByteArrayAsync(await DecompressAsync(cachedLayer.GetBlob()).ConfigureAwait(false)).ConfigureAwait(false)); // Verifies that the files are present. Assert.IsTrue( Files.Exists( cacheStorageFiles.GetLayerFile(cachedLayer.GetDigest(), cachedLayer.GetDiffId()))); }
/** Checks that the {@link Blob} streams the expected string. */ private async Task VerifyBlobWriteToAsync(string expected, IBlob blob) { using (MemoryStream outputStream = new MemoryStream()) { BlobDescriptor blobDescriptor = await blob.WriteToAsync(outputStream).ConfigureAwait(false); string output = Encoding.UTF8.GetString(outputStream.ToArray()); Assert.AreEqual(expected, output); byte[] expectedBytes = Encoding.UTF8.GetBytes(expected); Assert.AreEqual(expectedBytes.Length, blobDescriptor.GetSize()); using (MemoryStream stream = new MemoryStream(expectedBytes)) { BlobDescriptor digestDescriptor = await Digests.ComputeDigestAsync(stream).ConfigureAwait(false); DescriptorDigest expectedDigest = digestDescriptor.GetDigest(); Assert.AreEqual(expectedDigest, blobDescriptor.GetDigest()); } } }
public long GetSize() { return(blobDescriptor.GetSize()); }