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 EncodingProperty(Encoding encoding, string expectedOutput) { //arrange var random = new FakeRandomGenerator(new[] { 11, 0, 1, 2, 2, 3, 4, 5, 3, 6, 2, 7 }); var service = GetStringGenerationService(random); //act var result = service.GenerateRandomString(new GetStringParameters { MinLength = 11, MaxLength = 11, AllowedCharacters = "Helo Wrd", Encoding = encoding }); //assert result.Should().Be(expectedOutput); }
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 CorrectOrderOfFormattingAndEncoding() { //arrange const string expectedOutput = "aGVsbG9Xb3JsZA=="; var random = new FakeRandomGenerator(new[] { 11, 0, 1, 2, 2, 3, 4, 5, 3, 6, 2, 7 }); var service = GetStringGenerationService(random); //act var result = service.GenerateRandomString(new GetStringParameters { MinLength = 11, MaxLength = 11, AllowedCharacters = "Helo Wrd", Format = Format.Camel, Encoding = Encoding.Base64 }); //assert result.Should().Be(expectedOutput); }
public static void TestLongerReadWrite() { byte[] bytesToWrite = new FakeRandomGenerator().Generate(V2AxCryptDataStream.WriteChunkSize + V2AxCryptDataStream.WriteChunkSize / 2); byte[] buffer = new byte[bytesToWrite.Length + 2000]; using (V2AxCryptDataStream axCryptDataStreamWriter = V2AxCryptDataStream.Create(new MemoryStream(buffer))) { AxCrypt1Guid.Write(axCryptDataStreamWriter.Chained); new PreambleHeaderBlock().Write(axCryptDataStreamWriter.Chained); new DataHeaderBlock().Write(axCryptDataStreamWriter.Chained); axCryptDataStreamWriter.Write(bytesToWrite, 0, bytesToWrite.Length); axCryptDataStreamWriter.Flush(); new V2HmacHeaderBlock().Write(axCryptDataStreamWriter.Chained); } using (AxCryptReader reader = new TestingAxCryptReader(new LookAheadStream(new MemoryStream(buffer)))) { while (reader.Read()) { ; } reader.SetStartOfData(); using (V2AxCryptDataStream axCryptDataStreamReader = V2AxCryptDataStream.Create(reader, Stream.Null)) { byte[] bytesRead = new byte[bytesToWrite.Length]; int offset = 0; int count; do { count = axCryptDataStreamReader.Read(bytesRead, offset, 100); offset += count; } while (count > 0); Assert.That(bytesRead, Is.EquivalentTo(bytesToWrite)); } } }