public void ResumeDownloadByWrongStorage() { long startPos = this.remoteLength / 2; SetupFullDownload(); SetupStorage(startPos); this.localFileStream.Write(this.remoteContent, 0, (int)startPos); this.localFileStream.Seek(0, SeekOrigin.Begin); Assert.AreEqual(this.remoteChunk.Length, this.remoteLength - this.localFileStream.Length); byte[] checksum = this.mockedStorage.Object.GetObjectList()[0].LastChecksum; checksum[0] = (byte)(checksum[0] + 1); using (var memorystream = new MemoryStream(this.remoteChunk)) { this.mockedStream.Setup(stream => stream.Stream).Returns(memorystream); using (IFileDownloader downloader = new ChunkedDownloader(this.chunkSize, this.mockedStorage.Object)) { 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); } } }
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(); }
public void ConstructorWithValidInputTest() { using (var downloader = new ChunkedDownloader(this.chunkSize)) { Assert.AreEqual(this.chunkSize, downloader.ChunkSize); } using (var downloader = new ChunkedDownloader()) { Assert.Greater(downloader.ChunkSize, 0); } }
public void FullDownload() { SetupFullDownload(); using (var memorystream = new MemoryStream(this.remoteContent)) { this.mockedStream.Setup(stream => stream.Stream).Returns(memorystream); using (IFileDownloader downloader = new ChunkedDownloader(this.chunkSize)) { 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); } } }
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); } } }
public void FullDownloadWithoutLengthTest() { this.mockedStream.Setup(stream => stream.Length).Returns((long?)null); var mockedMemoryStream = new Mock <MemoryStream>(this.remoteContent) { CallBase = true }; mockedMemoryStream.Setup(ms => ms.Length).Throws(new NotSupportedException()); this.mockedStream.Setup(stream => stream.Stream).Returns(mockedMemoryStream.Object); 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 == null || l == 0)), It.Is <long?>((long?l) => l != null))) .Returns(this.mockedStream.Object); this.transmissionEvent.TransmissionStatus += delegate(object sender, TransmissionProgressEventArgs e) { if (e.ActualPosition != null) { Assert.GreaterOrEqual((long)e.ActualPosition, 0); Assert.LessOrEqual((long)e.ActualPosition, this.remoteLength); } if (e.Percent != null) { Assert.IsTrue(e.Percent == 0 || e.Percent == 100); } if (e.Length != null) { Assert.IsTrue(e.Length == 0 || e.Length == this.remoteContent.Length); } }; using (IFileDownloader downloader = new ChunkedDownloader(this.chunkSize)) { 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); } }
public void FullDownloadTest() { this.mockedStream.Setup(stream => stream.Length).Returns(this.remoteLength); using (var memorystream = new MemoryStream(this.remoteContent)) { this.mockedStream.Setup(stream => stream.Stream).Returns(memorystream); 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 == null || l == 0)), It.Is <long?>((long?l) => l != null))) .Returns(this.mockedStream.Object); this.transmissionEvent.TransmissionStatus += delegate(object sender, TransmissionProgressEventArgs e) { if (e.ActualPosition != null) { Assert.GreaterOrEqual((long)e.ActualPosition, 0); Assert.LessOrEqual((long)e.ActualPosition, this.remoteLength); } if (e.Percent != null) { Assert.GreaterOrEqual(e.Percent, 0); Assert.LessOrEqual(e.Percent, 100); } if (e.Length != null) { Assert.GreaterOrEqual(e.Length, 0); Assert.LessOrEqual(e.Length, this.remoteLength); } }; using (IFileDownloader downloader = new ChunkedDownloader(this.chunkSize)) { 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); } } }
public void FullDownloadWithoutLength() { SetupFullDownload(); this.mockedStream.Setup(stream => stream.Length).Returns((long?)null); var mockedMemoryStream = new Mock <MemoryStream>(this.remoteContent) { CallBase = true }; mockedMemoryStream.Setup(ms => ms.Length).Throws(new NotSupportedException()); this.mockedStream.Setup(stream => stream.Stream).Returns(mockedMemoryStream.Object); using (IFileDownloader downloader = new ChunkedDownloader(this.chunkSize)) { 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); } }
public void ConstructorWithoutInput() { using (var downloader = new ChunkedDownloader(this.chunkSize)) { Assert.AreEqual(this.chunkSize, downloader.ChunkSize); } }
public void ConstructorWithValidInput() { using (var downloader = new ChunkedDownloader()) { Assert.Greater(downloader.ChunkSize, 0); } }