public async Task StartReadAsyncTest2Async() { var data = Enumerable.Range(0, 200).Select(v => (byte)v).ToArray(); var buffer = new byte[100]; var Time = TimeSpan.FromMilliseconds(100); using var stream = new PipelineStream(); await stream.WriteAsync(data, 0, data.Length); int ReadBytes = 0; using (var TokenSource = new CancellationTokenSource(Time)) ReadBytes = await stream.ReadAsync(buffer, 0, buffer.Length, TokenSource.Token); Assert.AreEqual(buffer.Length, ReadBytes); ReadBytes = await stream.ReadAsync(buffer, 0, buffer.Length); Assert.AreEqual(buffer.Length, ReadBytes); using (var TokenSource = new CancellationTokenSource(Time)) await Assert.ThrowsExceptionAsync <OperationCanceledException>( () => stream.ReadAsync(buffer, 0, buffer.Length, TokenSource.Token)); await stream.WriteAsync(data, 0, data.Length); Assert.AreEqual(buffer.Length, ReadBytes); ReadBytes = await stream.ReadAsync(buffer, 0, buffer.Length); Assert.AreEqual(buffer.Length, ReadBytes); }
public void TestPipeline() { stream = new PipelineStream(256); ThreadPool.QueueUserWorkItem(WriteThread); var readed = false; stream.OnRead += (_) => { readed = true; }; var data = new byte[100]; int read; var actual = new StringBuilder(); var rand = new Random(); while ((read = stream.Read(data, 0, data.Length)) != 0) { var str = Encoding.UTF8.GetString(data, 0, read); actual.Append(str); Thread.Sleep(rand.Next(1, 5)); } stream.Dispose(); var expected = new StringBuilder(); for (var i = 0; i < 1000; i++) { expected.Append("0123456789"); } Assert.AreEqual(expected.ToString(), actual.ToString()); Assert.AreEqual(true, readed); }
/// <summary> /// Function called to handle the reading of the stream /// </summary> /// <param name="stm">The reading stream</param> protected override void OnRead(PipelineStream stm) { try { while (!stm.Eof) { DynamicStreamDataKey2 key = new DynamicStreamDataKey2("Root", Container, Graph.Logger, State); DataReader reader = new DataReader(stm); key.FromReader(reader); // Only fill in the frame if we read something, should this exit if it continues to read nothing? if (reader.ByteCount > 0) { WriteOutput(new DataFrame(key)); } } } catch (ThreadAbortException) { throw; } catch (EndOfStreamException) { // End of stream, do nothing } catch (Exception e) { LogException(e); } }
public async Task CompleteAsyncTestAsync() { using var Stream = new PipelineStream(); Assert.AreEqual(0, Stream.Length); Assert.IsTrue(Stream.CanRead); Assert.IsTrue(Stream.CanWrite); var WriteMessage1 = "HELLO"; await Stream.WriteAsync(Encoding.GetBytes(WriteMessage1)); await Stream.CompleteAsync(); await Stream.FlushAsync(); var ReadCount = Encoding.GetByteCount(WriteMessage1); Assert.AreEqual(ReadCount, Stream.Length); var ReadBytes = new byte[ReadCount]; ReadCount = await Stream.ReadAsync(ReadBytes); var ReadMessage1 = Encoding.GetString(ReadBytes.AsSpan().Slice(0, ReadCount)); Assert.AreEqual(WriteMessage1, ReadMessage1); Assert.IsTrue(Stream.CanRead); Assert.IsFalse(Stream.CanWrite); var WriteMessage2 = "HI!"; await Assert.ThrowsExceptionAsync <InvalidOperationException>(async() => await Stream.WriteAsync(Encoding.GetBytes(WriteMessage2))); }
public void TestSimpleByte() { byte[] result = null; bool complete = false; using (PipelineStream pipeline = new PipelineStream(new CancellationToken())) { Task producerTask = Task.Run(() => { pipeline.WriteByte(13); pipeline.Complete(); }); Task consumerTask = Task.Run(() => { byte[] r = new byte[1]; r[0] = (byte)pipeline.ReadByte(); result = r; complete = pipeline.ReadByte() == -1; }); Task.WaitAll(producerTask, consumerTask); } Assert.That(result[0], Is.EqualTo(13), "The byte written and read has the value 13."); Assert.That(complete, Is.True, "Since only one byte was written, complete should be set as well."); }
public void TestLargeStreamCopy() { FakeRandomGenerator frg = new FakeRandomGenerator(); using (MemoryStream source = new MemoryStream(frg.Generate(1000000))) { using (MemoryStream destination = new MemoryStream()) { using (PipelineStream pipeline = new PipelineStream(new CancellationToken())) { Task producerTask = Task.Run(() => { source.CopyTo(pipeline); pipeline.Complete(); }); Task consumerTask = Task.Run(() => { pipeline.CopyTo(destination); }); Task.WaitAll(producerTask, consumerTask); } Assert.That(source.ToArray().IsEquivalentTo(destination.ToArray()), "The source and destination should be the same after passing through the pipeline."); } } }
public void WriteTest() { var data = Enumerable.Range(0, 200).Select(v => (byte)v).ToArray(); var buffer = new byte[100]; using var stream = new PipelineStream(); stream.Write(data.AsSpan()); }
public void TestCanReadCanWrite() { var stream = new PipelineStream(256); stream.Write(Encoding.UTF8.GetBytes("0123456789"), 0, 10); Assert.AreEqual(true, stream.CanWrite); Assert.AreEqual(true, stream.CanRead); }
public void PipelineStreamTest() { using var Stream = new PipelineStream(); Assert.AreEqual(0, Stream.Length); Assert.IsTrue(Stream.CanRead); Assert.IsFalse(Stream.CanSeek); Assert.IsTrue(Stream.CanWrite); Assert.IsFalse(Stream.CanTimeout); }
public async Task WriteAsyncTestAsync() { var Time = TimeSpan.FromMilliseconds(100); var data = Enumerable.Range(0, 200).Select(v => (byte)v).ToArray(); var buffer = new byte[100]; using var stream = new PipelineStream(); using var TokenSource = new CancellationTokenSource(Time); await stream.WriteAsync(data, TokenSource.Token); }
public async Task StoppedReadAsyncTest2Async() { var buffer = new byte[100]; var Time = TimeSpan.FromMilliseconds(10); using var stream = new PipelineStream(); using var TokenSource = CreateTokenSource(Time); await Assert.ThrowsExceptionAsync <OperationCanceledException>( () => stream.ReadAsync(buffer, 0, buffer.Length, TokenSource.Token)); }
public void WriteAndCompleteAddingTest() { var data = Enumerable.Range(0, 200).Select(v => (byte)v).ToArray(); var buffer = new byte[100]; using var stream = new PipelineStream(); Assert.AreEqual(true, stream.CanWrite); stream.Write(data.AsSpan()); stream.Complete(); Assert.AreEqual(false, stream.CanWrite); Assert.ThrowsException <InvalidOperationException>(() => stream.Write(data.AsSpan())); }
public async Task FlushAsyncTestAsync() { var Message = "HELLO WORLD."; using var Stream = new PipelineStream(); Assert.AreEqual(0, Stream.Length); await using (var Writer = new StreamWriter(Stream, Encoding, 1024, true)) await Writer.WriteLineAsync(Message); Assert.AreNotEqual(0, Stream.Length); using (var Reader = new StreamReader(Stream, Encoding, false, 1024, true)) Assert.AreEqual(Message, await Reader.ReadLineAsync()); Assert.AreEqual(0, Stream.Length); }
public void FlushTest() { var Message = "HELLO WORLD."; using var Stream = new PipelineStream(); Assert.AreEqual(0, Stream.Length); using (var Writer = new StreamWriter(Stream, Encoding, 1024, true)) Writer.WriteLine(Message); Assert.AreNotEqual(0, Stream.Length); using (var Reader = new StreamReader(Stream, Encoding, false, 1024, true)) Assert.AreEqual(Message, Reader.ReadLine()); Assert.AreEqual(0, Stream.Length); }
public void Encrypt(Stream clearIn, string fileName, Action <Stream> processAction) { using (CancellationTokenSource tokenSource = new CancellationTokenSource()) { using (PipelineStream pipeline = new PipelineStream(tokenSource.Token)) { Task encryption = Task.Run(async() => { await EncryptAsync(clearIn, pipeline, fileName); pipeline.Complete(); }).ContinueWith((t) => { if (t.IsFaulted) { tokenSource.Cancel(); } }, tokenSource.Token, TaskContinuationOptions.ExecuteSynchronously, TaskScheduler.Current); Task process = Task.Run(() => { processAction(pipeline); }).ContinueWith((t) => { if (t.IsFaulted) { tokenSource.Cancel(); } }, tokenSource.Token, TaskContinuationOptions.ExecuteSynchronously, TaskScheduler.Current); try { Task.WaitAll(encryption, process); } catch (AggregateException ae) { IEnumerable <Exception> exceptions = ae.InnerExceptions.Where(ex1 => ex1.GetType() != typeof(OperationCanceledException)); if (!exceptions.Any()) { return; } IEnumerable <Exception> axCryptExceptions = exceptions.Where(ex2 => ex2 is AxCryptException); if (axCryptExceptions.Any()) { ExceptionDispatchInfo.Capture(axCryptExceptions.First()).Throw(); } throw exceptions.First(); } } } }
public async Task WriteAsyncAndCompleteAddingTestAsync() { var Time = TimeSpan.FromMilliseconds(100); var data = Enumerable.Range(0, 200).Select(v => (byte)v).ToArray(); var buffer = new byte[100]; using var stream = new PipelineStream(); Assert.AreEqual(true, stream.CanWrite); using (var TokenSource = new CancellationTokenSource(Time)) await stream.WriteAsync(data, TokenSource.Token); await stream.CompleteAsync(); Assert.AreEqual(false, stream.CanWrite); using (var TokenSource = new CancellationTokenSource(Time)) await Assert.ThrowsExceptionAsync <InvalidOperationException>( async() => await stream.WriteAsync(data, TokenSource.Token)); }
public void TestLargeWithVaryingChunkSizes() { FakeRandomGenerator frg = new FakeRandomGenerator(); byte[] source = frg.Generate(500000); int[] chunkSizes = { 1, 2, 3, 5, 7, 11, 13, 17, 19, 64, 128, 256, 257, }; using (MemoryStream destination = new MemoryStream()) { using (PipelineStream pipeline = new PipelineStream(new CancellationToken())) { Task producerTask = Task.Run(() => { int i = 0; int total = 0; while (total < source.Length) { int count = chunkSizes[i++ % chunkSizes.Length]; count = total + count > source.Length ? source.Length - total : count; pipeline.Write(source, total, count); total += count; } pipeline.Complete(); }); Task consumerTask = Task.Run(() => { byte[] read = new byte[17]; int count; while ((count = pipeline.Read(read, 0, read.Length)) > 0) { destination.Write(read, 0, count); } }); Task.WaitAll(producerTask, consumerTask); } byte[] result = destination.ToArray(); Assert.That(source.IsEquivalentTo(result), "The source and result should be the same after passing through the pipeline."); } }
public void CompleteTest() { using var Stream = new PipelineStream(); Assert.AreEqual(0, Stream.Length); var WriteMessage1 = "HELLO"; Stream.Write(Encoding.GetBytes(WriteMessage1)); Stream.Flush(); var ReadCount = Encoding.GetByteCount(WriteMessage1); Assert.AreEqual(ReadCount, Stream.Length); var ReadBytes = new byte[ReadCount]; ReadCount = Stream.Read(ReadBytes); Assert.AreEqual(0, Stream.Length); var ReadMessage1 = Encoding.GetString(ReadBytes.AsSpan().Slice(0, ReadCount)); Assert.AreEqual(WriteMessage1, ReadMessage1); Stream.Complete(); var WriteMessage2 = "HI!"; Assert.ThrowsException <InvalidOperationException>(() => Stream.Write(Encoding.GetBytes(WriteMessage2))); }
public void SetLengthTest() { using var Stream = new PipelineStream(); Assert.ThrowsException <NotSupportedException>(() => Stream.SetLength(0)); }
public async Task ChangeEncryptionAsync(IDataStore from, LogOnIdentity identity, EncryptionParameters encryptionParameters, IProgressContext progress) { if (!from.IsEncrypted()) { return; } using (CancellationTokenSource tokenSource = new CancellationTokenSource()) { if (IsFileInUse(from)) { throw new FileOperationException("File is in use, cannot write to it.", from.FullName, Abstractions.ErrorStatus.FileLocked, null); } using (PipelineStream pipeline = new PipelineStream(tokenSource.Token)) { EncryptedProperties encryptedProperties = EncryptedProperties.Create(from, identity); if (!EncryptionChangeNecessary(identity, encryptedProperties, encryptionParameters)) { return; } using (FileLock fileLock = New <FileLocker>().Acquire(from)) { Task decryption = Task.Run(() => { Decrypt(from, pipeline, identity); pipeline.Complete(); }).ContinueWith((t) => { if (t.IsFaulted) { tokenSource.Cancel(); } }, tokenSource.Token, TaskContinuationOptions.ExecuteSynchronously, TaskScheduler.Current); Task encryption = Task.Run(async() => { bool isWriteProteced = from.IsWriteProtected; if (isWriteProteced) { from.IsWriteProtected = false; } await EncryptToFileWithBackupAsync(fileLock, (Stream s) => { Encrypt(pipeline, s, encryptedProperties, encryptionParameters, AxCryptOptions.EncryptWithCompression, progress); return(Constant.CompletedTask); }, progress); from.IsWriteProtected = isWriteProteced; }).ContinueWith((t) => { if (t.IsFaulted) { tokenSource.Cancel(); } }, tokenSource.Token, TaskContinuationOptions.ExecuteSynchronously, TaskScheduler.Current); try { Task.WaitAll(decryption, encryption); } catch (AggregateException ae) { New <IReport>().Exception(ae); IEnumerable <Exception> exceptions = ae.InnerExceptions.Where(ex1 => ex1.GetType() != typeof(OperationCanceledException)); if (!exceptions.Any()) { return; } IEnumerable <Exception> axCryptExceptions = exceptions.Where(ex2 => ex2 is AxCryptException); if (axCryptExceptions.Any()) { ExceptionDispatchInfo.Capture(axCryptExceptions.First()).Throw(); } Exception ex = exceptions.First(); throw new InternalErrorException(ex.Message, Abstractions.ErrorStatus.Exception, ex); } } } } }
/// <summary> /// Function called by the thread /// </summary> /// <param name="stm">Reading stream</param> protected abstract void OnRead(PipelineStream stm);
/// <summary> /// Default constructor /// </summary> protected BaseStreamPipelineNode() { _input = new PipelineStream(); _lockObject = new object(); }
/// <summary> /// Default constructor /// </summary> protected BaseStreamPipelineNode() { _cancel_source = new CancellationTokenSource(); _input = new PipelineStream(_cancel_source.Token); _lockObject = new object(); }
public void TestCanSeek() { var stream = new PipelineStream(256); Assert.AreEqual(false, stream.CanSeek); }
public void TestSeek() { var stream = new PipelineStream(256); stream.Seek(0, SeekOrigin.Begin); }
public void TestSetLength() { var stream = new PipelineStream(256); stream.SetLength(0); }
public void TestSetPosition() { var stream = new PipelineStream(256); stream.Position = 10; }
public void TestGetPosition() { var stream = new PipelineStream(256); Assert.AreEqual(0, stream.Position); }
public void TestGetLength() { var stream = new PipelineStream(256); Assert.AreEqual(0, stream.Length); }
public void SeekTest() { using var Stream = new PipelineStream(); Assert.IsFalse(Stream.CanSeek); Assert.ThrowsException <NotSupportedException>(() => Stream.Seek(0, SeekOrigin.Begin)); }