Example #1
0
 public async Task <BlobDescriptor> WriteToAsync(Stream outputStream)
 {
     using (Stream fileIn = new BufferedStream(Files.NewInputStream(file)))
     {
         return(await Digests.ComputeDigestAsync(fileIn, outputStream).ConfigureAwait(false));
     }
 }
Example #2
0
        public async Task TestHandleResponse_unexpectedDigestAsync()
        {
            using (MemoryStream blobContent = new MemoryStream(Encoding.UTF8.GetBytes("some BLOB content")))
            {
                BlobDescriptor descriptor = await Digests.ComputeDigestAsync(blobContent).ConfigureAwait(false);

                DescriptorDigest testBlobDigest = descriptor.GetDigest();
                blobContent.Position = 0;

                using (HttpResponseMessage mockResponse = new HttpResponseMessage()
                {
                    Content = new StringContent("some BLOB content")
                })
                {
                    try
                    {
                        await testBlobPuller.HandleResponseAsync(mockResponse).ConfigureAwait(false);

                        Assert.Fail("Receiving an unexpected digest should fail");
                    }
                    catch (UnexpectedBlobDigestException ex)
                    {
                        Assert.AreEqual(
                            "The pulled BLOB has digest '"
                            + testBlobDigest
                            + "', but the request digest was '"
                            + fakeDigest
                            + "'",
                            ex.Message);
                    }
                }
            }
        }
Example #3
0
        public async Task TestHandleResponseAsync()
        {
            using (MemoryStream blobContent = new MemoryStream(Encoding.UTF8.GetBytes("some BLOB content")))
            {
                BlobDescriptor descriptor = await Digests.ComputeDigestAsync(blobContent).ConfigureAwait(false);

                DescriptorDigest testBlobDigest = descriptor.GetDigest();
                blobContent.Position = 0;

                using (HttpResponseMessage mockResponse = new HttpResponseMessage()
                {
                    Content = new StringContent("some BLOB content")
                })
                {
                    LongAdder  byteCount  = new LongAdder();
                    BlobPuller blobPuller =
                        new BlobPuller(
                            fakeRegistryEndpointRequestProperties,
                            testBlobDigest,
                            layerOutputStream,
                            size => Assert.AreEqual("some BLOB content".Length, size),
                            byteCount.Add);
                    await blobPuller.HandleResponseAsync(mockResponse).ConfigureAwait(false);

                    Assert.AreEqual(
                        "some BLOB content",
                        Encoding.UTF8.GetString(layerContentOutputStream.ToArray()));
                    Assert.AreEqual(testBlobDigest, layerOutputStream.ComputeDigest().GetDigest());
                    Assert.AreEqual("some BLOB content".Length, byteCount.Sum());
                }
            }
        }
Example #4
0
        public async Task <object> HandleResponseAsync(HttpResponseMessage response)
        {
            if (!response.IsSuccessStatusCode)
            {
                throw new HttpResponseException(response);
            }
            blobSizeListener(response.Content.Headers.ContentLength ?? 0);

            using (Stream outputStream =
                       new NotifyingOutputStream(destinationOutputStream, writtenByteCountListener, true))
            {
                BlobDescriptor receivedBlobDescriptor;
                using (Stream contentStream = await response.Content.ReadAsStreamAsync().ConfigureAwait(false))
                {
                    receivedBlobDescriptor = await Digests.ComputeDigestAsync(contentStream, outputStream).ConfigureAwait(false);
                }

                if (!blobDigest.Equals(receivedBlobDescriptor.GetDigest()))
                {
                    throw new UnexpectedBlobDigestException(
                              "The pulled BLOB has digest '"
                              + receivedBlobDescriptor.GetDigest()
                              + "', but the request digest was '"
                              + blobDigest
                              + "'");
                }
            }

            return(null);
        }
Example #5
0
 public async Task <BlobDescriptor> WriteToAsync(Stream outputStream)
 {
     // Cannot rewrite.
     if (isWritten)
     {
         throw new InvalidOperationException(Resources.InputStreamBlobRewriteExceptionMessage);
     }
     try
     {
         using (Stream inputStream = this.inputStream)
         {
             return(await Digests.ComputeDigestAsync(inputStream, outputStream).ConfigureAwait(false));
         }
     }
     finally
     {
         isWritten = true;
     }
 }
Example #6
0
        /** 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());
                }
            }
        }
Example #7
0
 public Task <BlobDescriptor> WriteToAsync(Stream outputStream)
 {
     return(Digests.ComputeDigestAsync(s => s.WriteAsync(_bytes, 0, _bytes.Length), outputStream));
 }
Example #8
0
 public async Task <BlobDescriptor> WriteToAsync(Stream outputStream)
 {
     return(await Digests.ComputeDigestAsync(writableContents, outputStream).ConfigureAwait(false));
 }