示例#1
0
        public void PrepareResumeFailsOnTooShortInputStream()
        {
            var streamMock = new Mock <Stream>();

            streamMock.Setup(s => s.Read(It.IsAny <byte[]>(), It.IsAny <int>(), It.IsAny <int>())).Returns(0);
            using (HashAlgorithm hashAlg = new SHA1Managed()) {
                Assert.Throws <IOException>(() => ContentTaskUtils.PrepareResume(this.successfulLength, streamMock.Object, hashAlg));
            }
        }
示例#2
0
        public void ResumeDownloadWithUtils()
        {
            long successfulLength = 1024;

            this.localFileStream.Write(this.remoteContent, 0, (int)successfulLength);
            this.localFileStream.Seek(0, SeekOrigin.Begin);

            byte[] remoteChunk = new byte[this.remoteLength - successfulLength];
            for (int i = 0; i < remoteChunk.Length; i++)
            {
                remoteChunk[i] = this.remoteContent[i + successfulLength];
            }

            this.mockedStream.Setup(stream => stream.Length).Returns(remoteChunk.Length);
            var memStream = new MemoryStream(remoteChunk);

            this.mockedStream.Setup(stream => stream.Stream).Returns(memStream);
            this.mock.Setup(doc => doc.ContentStreamLength).Returns(this.remoteLength);
            this.mock.Setup(doc => doc.ContentStreamId).Returns(this.contentStreamId);
            this.mock.Setup(doc => doc.GetContentStream(
                                It.Is <string>((string s) => s.Equals(this.contentStreamId)),
                                It.Is <long?>((long?l) => (l == successfulLength)),
                                It.Is <long?>((long?l) => l == remoteChunk.Length)))
            .Returns(this.mockedStream.Object);

            this.transmissionEvent.TransmissionStatus += delegate(object sender, TransmissionProgressEventArgs e)
            {
                if (e.ActualPosition != null)
                {
                    Assert.GreaterOrEqual((long)e.ActualPosition, successfulLength);
                    Assert.LessOrEqual((long)e.ActualPosition, this.remoteLength);
                }

                if (e.Percent != null)
                {
                    Assert.Greater(e.Percent, 0);
                    Assert.LessOrEqual(e.Percent, 100);
                }

                if (e.Length != null)
                {
                    Assert.GreaterOrEqual(e.Length, successfulLength);
                    Assert.LessOrEqual(e.Length, this.remoteLength);
                }
            };

            using (IFileDownloader downloader = new ChunkedDownloader(this.chunkSize))
            {
                ContentTaskUtils.PrepareResume(successfulLength, this.localFileStream, this.hashAlg);
                downloader.DownloadFile(this.mock.Object, this.localFileStream, this.transmissionEvent, this.hashAlg);
                Assert.AreEqual(this.remoteContent.Length, this.localFileStream.Length);
                Assert.AreEqual(SHA1Managed.Create().ComputeHash(this.remoteContent), this.hashAlg.Hash);
                Assert.AreEqual(SHA1Managed.Create().ComputeHash(this.localFileStream.ToArray()), this.hashAlg.Hash);
            }
            memStream.Dispose();
        }
示例#3
0
        public void PrepareResumeFailsOnIOException()
        {
            var streamMock = new Mock <Stream>();

            streamMock.Setup(s => s.Read(It.IsAny <byte[]>(), It.IsAny <int>(), It.IsAny <int>())).Throws(new IOException());
            using (HashAlgorithm hashAlg = new SHA1Managed())
            {
                ContentTaskUtils.PrepareResume(this.successfulLength, streamMock.Object, hashAlg);
            }
        }
示例#4
0
 public void PrepareResumeDoesNotChangeHashOnZeroLengthInputStream()
 {
     byte[] localContent = new byte[0];
     byte[] localHash    = new SHA1Managed().ComputeHash(localContent);
     using (MemoryStream stream = new MemoryStream(localContent))
         using (HashAlgorithm hashAlg = new SHA1Managed()) {
             ContentTaskUtils.PrepareResume(0, stream, hashAlg);
             hashAlg.TransformFinalBlock(new byte[0], 0, 0);
             Assert.AreEqual(localHash, hashAlg.Hash);
         }
 }
示例#5
0
        public void PrepareResumeWithExactFittingStream()
        {
            byte[] localContent = new byte[this.successfulLength];
            using (RandomNumberGenerator random = RandomNumberGenerator.Create()) {
                random.GetBytes(localContent);
            }

            byte[] localHash = new SHA1Managed().ComputeHash(localContent);

            using (MemoryStream stream = new MemoryStream(localContent))
                using (HashAlgorithm hashAlg = new SHA1Managed()) {
                    ContentTaskUtils.PrepareResume(this.successfulLength, stream, hashAlg);
                    hashAlg.TransformFinalBlock(new byte[0], 0, 0);
                    Assert.AreEqual(localHash, hashAlg.Hash);
                }
        }
示例#6
0
        public void ResumeDownloadWithUtils()
        {
            long startPos = this.remoteLength / 2;

            SetupResumeDownload(startPos);

            using (var memorystream = new MemoryStream(this.remoteChunk)) {
                this.mockedStream.Setup(stream => stream.Stream).Returns(memorystream);

                using (IFileDownloader downloader = new ChunkedDownloader(this.chunkSize)) {
                    ContentTaskUtils.PrepareResume(startPos, this.localFileStream, this.hashAlg);
                    downloader.DownloadFile(this.mock.Object, this.localFileStream, this.transmission, this.hashAlg);
                    Assert.AreEqual(this.remoteContent.Length, this.localFileStream.Length);
                    Assert.AreEqual(SHA1Managed.Create().ComputeHash(this.remoteContent), this.hashAlg.Hash);
                    Assert.AreEqual(SHA1Managed.Create().ComputeHash(this.localFileStream.ToArray()), this.hashAlg.Hash);
                }
            }
        }
示例#7
0
        public void PrepareResumeWithLongerLocalStream()
        {
            byte[] localContent = new byte[this.successfulLength];
            using (RandomNumberGenerator random = RandomNumberGenerator.Create()) {
                random.GetBytes(localContent);
            }

            byte[] localHash = new SHA1Managed().ComputeHash(localContent);

            using (MemoryStream stream = new MemoryStream())
                using (HashAlgorithm hashAlg = new SHA1Managed()) {
                    stream.Write(localContent, 0, (int)this.successfulLength);
                    stream.Write(localContent, 0, (int)this.successfulLength);
                    stream.Seek(0, SeekOrigin.Begin);
                    ContentTaskUtils.PrepareResume(this.successfulLength, stream, hashAlg);
                    hashAlg.TransformFinalBlock(new byte[0], 0, 0);
                    Assert.AreEqual(localHash, hashAlg.Hash);
                }
        }
示例#8
0
        public void ResumeUploadWithUtils()
        {
            double successfulUploadPart = 0.2;
            int    successfulUploaded   = (int)(this.fileLength * successfulUploadPart);
            double minPercent           = 100 * successfulUploadPart;

            this.InitRemoteChunkWithSize(successfulUploaded);
            this.transmission.AddLengthConstraint(Is.GreaterThanOrEqualTo(successfulUploaded));
            this.transmission.AddPercentConstraint(Is.GreaterThanOrEqualTo(minPercent));
            this.transmission.AddPositionConstraint(Is.GreaterThanOrEqualTo(successfulUploaded));

            using (IFileUploader uploader = new ChunkedUploader(this.chunkSize)) {
                ContentTaskUtils.PrepareResume(successfulUploaded, this.localFileStream, this.hashAlg);
                uploader.UploadFile(this.mockedDocument.Object, this.localFileStream, this.transmission, this.hashAlg);
            }

            this.AssertThatLocalAndRemoteContentAreEqualToHash();
            Assert.AreEqual(1, this.lastChunk);
        }
示例#9
0
        public void ResumeUploadWithUtils()
        {
            double successfulUploadPart = 0.2;
            int    successfulUploaded   = (int)(this.fileLength * successfulUploadPart);
            double minPercent           = 100 * successfulUploadPart;

            this.InitRemoteChunkWithSize(successfulUploaded);
            this.transmissionEvent.TransmissionStatus += delegate(object sender, TransmissionProgressEventArgs e) {
                this.AssertThatProgressFitsMinimumLimits(e, successfulUploaded, minPercent, successfulUploaded);
            };

            using (IFileUploader uploader = new ChunkedUploader(this.chunkSize)) {
                ContentTaskUtils.PrepareResume(successfulUploaded, this.localFileStream, this.hashAlg);
                uploader.UploadFile(this.mockedDocument.Object, this.localFileStream, this.transmissionEvent, this.hashAlg);
            }

            this.AssertThatLocalAndRemoteContentAreEqualToHash();
            Assert.AreEqual(1, this.lastChunk);
        }