Esempio n. 1
0
            public ValidationResult Validate(
                Dictionary <string, List <string> > hostBlobIdsPerStreamId,
                Dictionary <string, IBlob> hostBlobs,
                Dictionary <string, string> chunkingSchemePerStreamId,
                IResourceManager resourceManager)
            {
                if (!chunkingSchemePerStreamId[this.StreamId].Equals(this.ExpectedChunkingScheme))
                {
                    return(new ValidationResult($"For StreamId '{this.StreamId}', ResponseChunkingScheme '{chunkingSchemePerStreamId[ this.StreamId ]}' not equal to ExpectedChunkingScheme: {this.ExpectedChunkingScheme}"));
                }

                if (!hostBlobIdsPerStreamId.ContainsKey(this.StreamId))
                {
                    return(new ValidationResult(String.Format(CultureInfo.CurrentCulture, "streamId:{0} does not exist in hostBlobIdsPerStreamId", this.StreamId)));
                }

                // Get the chunk ids of the file from download
                string[] hostBlobIds = hostBlobIdsPerStreamId[this.StreamId].ToArray();
                IReadOnlyDictionary <string, IBlob> existingBlobs = new Dictionary <string, IBlob>();

                if (chunkingSchemePerStreamId[this.StreamId].Equals(ChunkingScheme.Zip.ToString()))
                {
                    // Zip Chunking
                    if (AlreadyExistingContentResourceId == null && ExpectedContentResourceId == null)
                    {
                        return(new ValidationResult());
                    }

                    ChunkProcessorFactory.Instance.CreateInstance(ChunkingScheme.Zip)
                    .StreamToBlobs(resourceManager, AlreadyExistingContentResourceId, out string[] _, out existingBlobs);

                    // Combine blobs into a complete stream
                    IReadOnlyDictionary <string, IBlob> hostRevisionBlobs = CreateHostRevisionBlobs(hostBlobIds, hostBlobs, existingBlobs);
                    byte[] reconstructedBytes = StreamBuilder.BlobsToBytes(hostBlobIds, hostRevisionBlobs);

                    // Zip Chunking validation
                    using (Stream reconstructedStream = new MemoryStream(reconstructedBytes))
                        using (Stream expectedStream = resourceManager.GetZipChunkingResourceStream(ExpectedContentResourceId))
                        {
                            if (!StreamUtil.StreamEquals(expectedStream, reconstructedStream))
                            {
                                return(new ValidationResult(String.Format(CultureInfo.CurrentCulture, "Reconstructed stream and expected stream are not equal.")));
                            }
                        }
                }
                else
                {
                    // FullFile Chunking
                    if (AlreadyExistingContent == null && ExpectedContent == null)
                    {
                        return(new ValidationResult());
                    }

                    byte[] AlreadyExistingContentBytes = Encoding.UTF8.GetBytes(AlreadyExistingContent);

                    ChunkProcessorFactory.Instance.CreateInstance(ChunkingScheme.FullFile)
                    .StreamToBlobs(new MemoryStream(AlreadyExistingContentBytes), new BlobAllocator(), out string[] _, out existingBlobs);

                    // Combine blobs into a complete stream
                    IReadOnlyDictionary <string, IBlob> hostRevisionBlobs = CreateHostRevisionBlobs(hostBlobIds, hostBlobs, existingBlobs);
                    byte[] reconstructedBytes = StreamBuilder.BlobsToBytes(hostBlobIds, hostRevisionBlobs);

                    // Full File Chunking validation
                    string reconstructedString = Encoding.UTF8.GetString(reconstructedBytes);

                    if (!string.Equals(ExpectedContent, reconstructedString))
                    {
                        return(new ValidationResult(String.Format(CultureInfo.CurrentCulture, "Reconstructed payload '{0}' and expected value '{1}' are not equal", reconstructedString, this.ExpectedContent)));
                    }
                }

                return(new ValidationResult());
            }