public void TestSeek() { using (var stream = new ProgressStream(new DummyNetworkStream(), Update)) { Assert.Throws <NotSupportedException> (() => stream.Seek(0, SeekOrigin.Begin)); Assert.Throws <NotSupportedException> (() => stream.Position = 500); Assert.AreEqual(0, stream.Position); Assert.AreEqual(0, stream.Length); } }
public void Inner_Stream_Calls_Seek() { string key = "test"; var mockStream = new Mock <Stream>(); var progressStream = new ProgressStream(key, mockStream.Object, null, null); long offset = 0; SeekOrigin origin = SeekOrigin.Begin; progressStream.Seek(offset, origin); mockStream.Verify(s => s.Seek(offset, origin), Times.Once); }
public void ResumeTest() { byte[] inputContent = new byte[100]; long offset = 100; using (Stream stream = new MemoryStream(inputContent)) using (OffsetStream offsetstream = new OffsetStream(stream, offset)) { FileTransmissionEvent transmissionEvent = new FileTransmissionEvent(this.transmissionType, this.filename); transmissionEvent.TransmissionStatus += delegate(object sender, TransmissionProgressEventArgs args) { if (args.ActualPosition != null && args.Percent != null) { this.position = (long)args.ActualPosition; this.percent = (double)args.Percent; } }; using (ProgressStream progress = new ProgressStream(offsetstream, transmissionEvent)) { progress.Seek(0, SeekOrigin.Begin); Assert.AreEqual(offset, this.position); Assert.AreEqual(50, this.percent); progress.Seek(10, SeekOrigin.Current); Assert.AreEqual(offset + 10, this.position); progress.Seek(0, SeekOrigin.End); Assert.AreEqual(100, this.percent); Assert.AreEqual(offset + inputContent.Length, this.position); progress.Seek(0, SeekOrigin.Begin); progress.WriteByte(0); Assert.AreEqual(offset + 1, this.position); Assert.AreEqual(50.5, this.percent); progress.WriteByte(0); Assert.AreEqual(offset + 2, this.position); Assert.AreEqual(51, this.percent); progress.Write(new byte[10], 0, 10); Assert.AreEqual(56, this.percent); } } }
public static void TestPropertiesAndMethods() { using (MemoryStream memoryStream = FakeRuntimeFileInfo.ExpandableMemoryStream(new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 })) { string kilroy = String.Empty; using (FakeStream testStream = new FakeStream(memoryStream, (string wasHere) => { kilroy += wasHere; })) { using (ProgressStream progressStream = new ProgressStream(testStream, new ProgressContext())) { kilroy = String.Empty; Assert.That(progressStream.CanRead, Is.True, "The underlying stream is readable."); Assert.That(kilroy, Is.EqualTo("CanRead"), "ProgressStream should delegate to the underlying stream."); kilroy = String.Empty; Assert.That(progressStream.CanWrite, Is.True, "The underlying stream is writable."); Assert.That(kilroy, Is.EqualTo("CanWrite"), "ProgressStream should delegate to the underlying stream."); kilroy = String.Empty; Assert.That(progressStream.CanSeek, Is.True, "The underlying stream is seekable."); Assert.That(kilroy, Is.EqualTo("CanSeek"), "ProgressStream should delegate to the underlying stream."); kilroy = String.Empty; progressStream.Flush(); Assert.That(kilroy, Is.EqualTo("Flush"), "ProgressStream should delegate to the underlying stream."); kilroy = String.Empty; Assert.That(progressStream.Length, Is.EqualTo(10), "There are 10 bytes in the underlying stream."); Assert.That(kilroy, Is.EqualTo("Length"), "ProgressStream should delegate to the underlying stream."); kilroy = String.Empty; Assert.That(progressStream.Seek(-4, SeekOrigin.End), Is.EqualTo(6), "4 bytes from the end of 10 should be 6."); Assert.That(kilroy, Is.EqualTo("Seek"), "ProgressStream should delegate to the underlying stream."); kilroy = String.Empty; Assert.That(progressStream.Position, Is.EqualTo(6), "The position should still be at 6."); Assert.That(kilroy, Is.EqualTo("getPosition"), "ProgressStream should delegate to the underlying stream."); kilroy = String.Empty; progressStream.Position = 0; Assert.That(kilroy, Is.EqualTo("setPosition"), "ProgressStream should delegate to the underlying stream."); kilroy = String.Empty; progressStream.Write(new byte[] { 13 }, 0, 1); Assert.That(kilroy.Contains("Write"), Is.True, "ProgressStream should delegate to the underlying stream."); kilroy = String.Empty; progressStream.Position = 0; byte[] firstByte = new byte[1]; progressStream.Read(firstByte, 0, 1); Assert.That(kilroy.Contains("Read"), Is.True, "ProgressStream should delegate to the underlying stream."); kilroy = String.Empty; Assert.That(firstByte[0], Is.EqualTo(13), "13 was just written to the first position."); progressStream.SetLength(5); Assert.That(kilroy, Is.EqualTo("SetLength"), "ProgressStream should delegate to the underlying stream."); } } } }
public void ResumeTest() { byte[] inputContent = new byte[100]; long offset = 100; using (var stream = new MemoryStream(inputContent)) using (var offsetstream = new OffsetStream(stream, offset)) { using (var progress = new ProgressStream(offsetstream)) { progress.PropertyChanged += delegate(object sender, System.ComponentModel.PropertyChangedEventArgs e) { var p = sender as ProgressStream; if (e.PropertyName == Utils.NameOf(() => p.Position)) { this.position = (long)p.Position; this.percent = p.Percent.GetValueOrDefault(); } }; progress.Seek(0, SeekOrigin.Begin); Assert.AreEqual(offset, this.position); Assert.AreEqual(50, this.percent); progress.Seek(10, SeekOrigin.Current); Assert.AreEqual(offset + 10, this.position); progress.Seek(0, SeekOrigin.End); Assert.AreEqual(100, this.percent); Assert.AreEqual(offset + inputContent.Length, this.position); progress.Seek(0, SeekOrigin.Begin); progress.WriteByte(0); Assert.AreEqual(offset + 1, this.position); Assert.AreEqual(50.5, this.percent); progress.WriteByte(0); Assert.AreEqual(offset + 2, this.position); Assert.AreEqual(51, this.percent); progress.Write(new byte[10], 0, 10); Assert.AreEqual(56, this.percent); } } }
public void SeekTest() { using (Stream stream = new MemoryStream()) { FileTransmissionEvent transmissionEvent = new FileTransmissionEvent(this.transmissionType, this.filename); transmissionEvent.TransmissionStatus += delegate(object sender, TransmissionProgressEventArgs args) { if (args.ActualPosition != null) { this.positionCalls++; this.position = (long)args.ActualPosition; this.percent = (double)args.Percent; } }; using (ProgressStream progress = new ProgressStream(stream, transmissionEvent)) { progress.SetLength(100); progress.Seek(10, SeekOrigin.Begin); Assert.AreEqual(10, this.position); Assert.AreEqual(10, this.percent); progress.Seek(10, SeekOrigin.Current); Assert.AreEqual(20, this.position); Assert.AreEqual(20, this.percent); progress.Seek(10, SeekOrigin.Current); Assert.AreEqual(30, this.position); Assert.AreEqual(30, this.percent); progress.Seek(10, SeekOrigin.Current); Assert.AreEqual(40, this.position); Assert.AreEqual(40, this.percent); progress.Seek(5, SeekOrigin.Current); Assert.AreEqual(45, this.position); Assert.AreEqual(45, this.percent); progress.Seek(10, SeekOrigin.Current); Assert.AreEqual(55, this.position); Assert.AreEqual(55, this.percent); progress.SetLength(1000); progress.Seek(10, SeekOrigin.Current); Assert.AreEqual(65, this.position); Assert.AreEqual(6.5, this.percent); progress.Seek(0, SeekOrigin.End); Assert.AreEqual(100, this.percent); Assert.AreEqual(1000, this.position); } } }
public void SeekTest() { using (var stream = new MemoryStream()) { using (var progress = new ProgressStream(stream)) { progress.PropertyChanged += delegate(object sender, System.ComponentModel.PropertyChangedEventArgs e) { var p = sender as ProgressStream; if (e.PropertyName == Utils.NameOf(() => p.Position)) { this.positionCalls++; this.position = (long)p.Position; this.percent = (double)p.Percent; } }; progress.SetLength(100); progress.Seek(10, SeekOrigin.Begin); Assert.AreEqual(10, this.position); Assert.AreEqual(10, this.percent); progress.Seek(10, SeekOrigin.Current); Assert.AreEqual(20, this.position); Assert.AreEqual(20, this.percent); progress.Seek(10, SeekOrigin.Current); Assert.AreEqual(30, this.position); Assert.AreEqual(30, this.percent); progress.Seek(10, SeekOrigin.Current); Assert.AreEqual(40, this.position); Assert.AreEqual(40, this.percent); progress.Seek(5, SeekOrigin.Current); Assert.AreEqual(45, this.position); Assert.AreEqual(45, this.percent); progress.Seek(10, SeekOrigin.Current); Assert.AreEqual(55, this.position); Assert.AreEqual(55, this.percent); progress.SetLength(1000); progress.Seek(10, SeekOrigin.Current); Assert.AreEqual(65, this.position); Assert.AreEqual(6.5, this.percent); progress.Seek(0, SeekOrigin.End); Assert.AreEqual(100, this.percent); Assert.AreEqual(1000, this.position); } } }
private void UploadManagerThread() { for (; ;) { Thread.Sleep(1000 * 1); if (this.State == ManagerState.Stop) { return; } BackgroundUploadItem item = null; try { lock (this.ThisLock) { if (_settings.BackgroundUploadItems.Count > 0) { item = _settings.BackgroundUploadItems .Where(n => n.State == BackgroundUploadState.Encoding) .FirstOrDefault(); } } } catch (Exception) { return; } if (item == null) { continue; } try { if (item.Groups.Count == 0 && item.Keys.Count == 0) { Stream stream = null; try { if (item.Type == BackgroundItemType.Link) { var link = item.Value as Link; if (link == null) { throw new FormatException(); } stream = link.Export(_bufferManager); } else if (item.Type == BackgroundItemType.Store) { var store = item.Value as Store; if (store == null) { throw new FormatException(); } stream = store.Export(_bufferManager); } else { throw new FormatException(); } if (stream.Length == 0) { lock (this.ThisLock) { item.Seed.Rank = 0; if (item.DigitalSignature != null) { item.Seed.CreateCertificate(item.DigitalSignature); } _connectionsManager.Upload(item.Seed); item.State = BackgroundUploadState.Completed; } } else { KeyCollection keys = null; byte[] cryptoKey = null; try { using (ProgressStream encodingProgressStream = new ProgressStream(stream, (object sender, long readSize, long writeSize, out bool isStop) => { isStop = (this.State == ManagerState.Stop || !_settings.BackgroundUploadItems.Contains(item)); }, 1024 * 1024, true)) { item.Seed.Length = stream.Length; if (item.Seed.Length == 0) { throw new InvalidOperationException("Stream Length"); } if (item.HashAlgorithm == HashAlgorithm.Sha256) { cryptoKey = Sha256.ComputeHash(encodingProgressStream); } encodingProgressStream.Seek(0, SeekOrigin.Begin); item.State = BackgroundUploadState.Encoding; keys = _cacheManager.Encoding(encodingProgressStream, item.CompressionAlgorithm, item.CryptoAlgorithm, cryptoKey, item.BlockLength, item.HashAlgorithm); } } catch (StopIoException) { continue; } lock (this.ThisLock) { foreach (var key in keys) { item.UploadKeys.Add(key); item.LockedKeys.Add(key); } item.CryptoKey = cryptoKey; item.Keys.AddRange(keys); } } } finally { if (stream != null) { stream.Dispose(); } } } else if (item.Groups.Count == 0 && item.Keys.Count == 1) { lock (this.ThisLock) { item.Seed.Rank = item.Rank; item.Seed.Key = item.Keys[0]; item.Keys.Clear(); item.Seed.CompressionAlgorithm = item.CompressionAlgorithm; item.Seed.CryptoAlgorithm = item.CryptoAlgorithm; item.Seed.CryptoKey = item.CryptoKey; if (item.DigitalSignature != null) { item.Seed.CreateCertificate(item.DigitalSignature); } item.UploadKeys.Add(item.Seed.Key); foreach (var key in item.UploadKeys) { _connectionsManager.Upload(key); } this.SetKeyCount(item); foreach (var key in item.LockedKeys) { _cacheManager.Unlock(key); } item.LockedKeys.Clear(); item.State = BackgroundUploadState.Uploading; _connectionsManager.Upload(item.Seed); } } else if (item.Keys.Count > 0) { var length = Math.Min(item.Keys.Count, 128); var keys = new KeyCollection(item.Keys.Take(length)); Group group = null; try { group = _cacheManager.ParityEncoding(keys, item.HashAlgorithm, item.BlockLength, item.CorrectionAlgorithm, (object state2) => { return(this.State == ManagerState.Stop || !_settings.BackgroundUploadItems.Contains(item)); }); } catch (StopException) { continue; } lock (this.ThisLock) { foreach (var key in group.Keys.Skip(group.InformationLength)) { item.UploadKeys.Add(key); item.LockedKeys.Add(key); } item.Groups.Add(group); item.Keys.RemoveRange(0, length); } } else if (item.Groups.Count > 0 && item.Keys.Count == 0) { var index = new Index(); index.Groups.AddRange(item.Groups); index.CompressionAlgorithm = item.CompressionAlgorithm; index.CryptoAlgorithm = item.CryptoAlgorithm; index.CryptoKey = item.CryptoKey; byte[] cryptoKey = null; KeyCollection keys = null; try { using (var stream = index.Export(_bufferManager)) using (ProgressStream encodingProgressStream = new ProgressStream(stream, (object sender, long readSize, long writeSize, out bool isStop) => { isStop = (this.State == ManagerState.Stop || !_settings.BackgroundUploadItems.Contains(item)); }, 1024 * 1024, true)) { if (item.HashAlgorithm == HashAlgorithm.Sha256) { cryptoKey = Sha256.ComputeHash(encodingProgressStream); } encodingProgressStream.Seek(0, SeekOrigin.Begin); item.State = BackgroundUploadState.Encoding; keys = _cacheManager.Encoding(encodingProgressStream, item.CompressionAlgorithm, item.CryptoAlgorithm, cryptoKey, item.BlockLength, item.HashAlgorithm); } } catch (StopIoException) { continue; } lock (this.ThisLock) { foreach (var key in keys) { item.UploadKeys.Add(key); item.LockedKeys.Add(key); } item.CryptoKey = cryptoKey; item.Keys.AddRange(keys); item.Rank++; item.Groups.Clear(); } } } catch (Exception e) { item.State = BackgroundUploadState.Error; Log.Error(e); this.Remove(item); } } }