StreamTests.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. using Microsoft.VisualStudio.TestTools.UnitTesting;
  2. using System;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Threading;
  6. using System.Threading.Tasks;
  7. namespace S7.Net.UnitTest
  8. {
  9. /// <summary>
  10. /// Test stream which only gives 1 byte per read.
  11. /// </summary>
  12. class TestStream1BytePerRead : Stream
  13. {
  14. public TestStream1BytePerRead(byte[] data)
  15. {
  16. Data = data;
  17. }
  18. public override bool CanRead => _position < Data.Length;
  19. public override bool CanSeek => throw new NotImplementedException();
  20. public override bool CanWrite => throw new NotImplementedException();
  21. public override long Length => throw new NotImplementedException();
  22. public override long Position { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
  23. public byte[] Data { get; }
  24. int _position = 0;
  25. public override void Flush()
  26. {
  27. throw new NotImplementedException();
  28. }
  29. public override int Read(byte[] buffer, int offset, int count)
  30. {
  31. throw new NotImplementedException();
  32. }
  33. public override Task<int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
  34. {
  35. cancellationToken.ThrowIfCancellationRequested();
  36. if (_position >= Data.Length)
  37. {
  38. return Task.FromResult(0);
  39. }
  40. buffer[offset] = Data[_position];
  41. ++_position;
  42. return Task.FromResult(1);
  43. }
  44. public override long Seek(long offset, SeekOrigin origin)
  45. {
  46. throw new NotImplementedException();
  47. }
  48. public override void SetLength(long value)
  49. {
  50. throw new NotImplementedException();
  51. }
  52. public override void Write(byte[] buffer, int offset, int count)
  53. {
  54. throw new NotImplementedException();
  55. }
  56. }
  57. /// <summary>
  58. /// These tests are intended to test <see cref="StreamExtensions"/> functions and other stream-related special cases.
  59. /// </summary>
  60. [TestClass]
  61. public class StreamTests
  62. {
  63. public TestContext TestContext { get; set; }
  64. [TestMethod]
  65. public async Task TPKT_ReadRestrictedStreamAsync()
  66. {
  67. var fullMessage = ProtocolUnitTest.StringToByteArray("0300002902f0803203000000010002001400000401ff0400807710000100000103000000033f8ccccd");
  68. var m = new TestStream1BytePerRead(fullMessage);
  69. var t = await TPKT.ReadAsync(m, TestContext.CancellationTokenSource.Token);
  70. Assert.AreEqual(fullMessage.Length, t.Length);
  71. Assert.AreEqual(fullMessage.Last(), t.Data.Last());
  72. }
  73. [TestMethod]
  74. public async Task TPKT_ReadRestrictedStream()
  75. {
  76. var fullMessage = ProtocolUnitTest.StringToByteArray("0300002902f0803203000000010002001400000401ff0400807710000100000103000000033f8ccccd");
  77. var m = new TestStream1BytePerRead(fullMessage);
  78. var t = await TPKT.ReadAsync(m, CancellationToken.None);
  79. Assert.AreEqual(fullMessage.Length, t.Length);
  80. Assert.AreEqual(fullMessage.Last(), t.Data.Last());
  81. }
  82. [TestMethod]
  83. public async Task TPKT_ReadStreamTooShort()
  84. {
  85. var fullMessage = ProtocolUnitTest.StringToByteArray("0300002902f0803203000000010002001400");
  86. var m = new TestStream1BytePerRead(fullMessage);
  87. await Assert.ThrowsExceptionAsync<TPKTInvalidException>(() => TPKT.ReadAsync(m, CancellationToken.None));
  88. }
  89. }
  90. }