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 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 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 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 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); } } } } }