Esempio n. 1
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());
                }
            }
        }
Esempio n. 2
0
        /**
         * Writes an uncompressed {@code layerBlob} to the {@code layerDirectory}.
         *
         * @param uncompressedLayerBlob the uncompressed layer {@link Blob}
         * @param layerDirectory the directory for the layer
         * @return a {@link WrittenLayer} with the written layer information
         * @throws IOException if an I/O exception occurs
         */
        private async Task <WrittenLayer> WriteUncompressedLayerBlobToDirectoryAsync(
            IBlob uncompressedLayerBlob, SystemPath layerDirectory)
        {
            using (TemporaryFile temporaryLayerFile = CacheStorageFiles.GetTemporaryLayerFile(layerDirectory))
            {
                DescriptorDigest layerDiffId;
                BlobDescriptor   blobDescriptor;

                // Writes the layer with GZIP compression. The original bytes are captured as the layer's
                // diff ID and the bytes outputted from the GZIP compression are captured as the layer's
                // content descriptor.
                using (CountingDigestOutputStream compressedDigestOutputStream =
                           new CountingDigestOutputStream(
                               Files.NewOutputStream(temporaryLayerFile.Path)))
                {
                    using (GZipStream compressorStream = new GZipStream(compressedDigestOutputStream, CompressionMode.Compress, true))
                    {
                        BlobDescriptor descriptor = await uncompressedLayerBlob.WriteToAsync(compressorStream).ConfigureAwait(false);

                        layerDiffId = descriptor.GetDigest();
                    }
                    // The GZIPOutputStream must be closed in order to write out the remaining compressed data.
                    blobDescriptor = compressedDigestOutputStream.ComputeDigest();
                }
                DescriptorDigest layerDigest = blobDescriptor.GetDigest();
                long             layerSize   = blobDescriptor.GetSize();

                // Renames the temporary layer file to the correct filename.
                SystemPath layerFile = layerDirectory.Resolve(cacheStorageFiles.GetLayerFilename(layerDiffId));
                temporaryLayerFile.MoveIfDoesNotExist(layerFile);

                return(new WrittenLayer(layerDigest, layerDiffId, layerSize));
            }
        }
Esempio n. 3
0
 /**
  * Decompresses the file to obtain the diff ID.
  *
  * @param compressedFile the file containing the compressed contents
  * @return the digest of the decompressed file
  * @throws IOException if an I/O exception occurs
  */
 private static async Task <DescriptorDigest> GetDiffIdByDecompressingFileAsync(SystemPath compressedFile)
 {
     using (CountingDigestOutputStream diffIdCaptureOutputStream =
                new CountingDigestOutputStream(Stream.Null))
     {
         using (GZipStream decompressorStream = new GZipStream(Files.NewInputStream(compressedFile), CompressionMode.Decompress))
         {
             await ByteStreams.CopyAsync(decompressorStream, diffIdCaptureOutputStream).ConfigureAwait(false);
         }
         return(diffIdCaptureOutputStream.ComputeDigest().GetDigest());
     }
 }
        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());
                    }
            }
        }