ProtocolTests.cs 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. using System;
  2. using System.IO;
  3. using System.Linq;
  4. using System.Threading;
  5. using System.Threading.Tasks;
  6. using Microsoft.VisualStudio.TestTools.UnitTesting;
  7. using S7.Net.Protocol;
  8. namespace S7.Net.UnitTest
  9. {
  10. [TestClass]
  11. public class ProtocolUnitTest
  12. {
  13. public TestContext TestContext { get; set; }
  14. [TestMethod]
  15. public async Task TPKT_Read()
  16. {
  17. var m = new MemoryStream(StringToByteArray("0300002902f0803203000000010002001400000401ff0400807710000100000103000000033f8ccccd"));
  18. var t = await TPKT.ReadAsync(m, TestContext.CancellationTokenSource.Token);
  19. Assert.AreEqual(0x03, t.Version);
  20. Assert.AreEqual(0x29, t.Length);
  21. }
  22. [TestMethod]
  23. [ExpectedException(typeof(TPKTInvalidException))]
  24. public async Task TPKT_ReadShort()
  25. {
  26. var m = new MemoryStream(StringToByteArray("0300002902f0803203000000010002001400000401ff040080"));
  27. var t = await TPKT.ReadAsync(m, CancellationToken.None);
  28. }
  29. [TestMethod]
  30. [ExpectedException(typeof(TPKTInvalidException))]
  31. public async Task TPKT_ReadShortAsync()
  32. {
  33. var m = new MemoryStream(StringToByteArray("0300002902f0803203000000010002001400000401ff040080"));
  34. var t = await TPKT.ReadAsync(m, TestContext.CancellationTokenSource.Token);
  35. }
  36. [TestMethod]
  37. public async Task COTP_ReadTSDU()
  38. {
  39. var expected = StringToByteArray("320700000400000800080001120411440100ff09000400000000");
  40. var m = new MemoryStream(StringToByteArray("0300000702f0000300000702f0000300002102f080320700000400000800080001120411440100ff09000400000000"));
  41. var t = await COTP.TSDU.ReadAsync(m, TestContext.CancellationTokenSource.Token);
  42. Assert.IsTrue(expected.SequenceEqual(t));
  43. }
  44. public static byte[] StringToByteArray(string hex)
  45. {
  46. return Enumerable.Range(0, hex.Length)
  47. .Where(x => x % 2 == 0)
  48. .Select(x => Convert.ToByte(hex.Substring(x, 2), 16))
  49. .ToArray();
  50. }
  51. [TestMethod]
  52. public async Task TestResponseCode()
  53. {
  54. var expected = StringToByteArray("320700000400000800080001120411440100ff09000400000000");
  55. var m = new MemoryStream(StringToByteArray("0300000702f0000300000702f0000300002102f080320700000400000800080001120411440100ff09000400000000"));
  56. var t = await COTP.TSDU.ReadAsync(m, CancellationToken.None);
  57. Assert.IsTrue(expected.SequenceEqual(t));
  58. // Test all possible byte values. Everything except 0xff should throw an exception.
  59. var testData = Enumerable.Range(0, 256).Select(i => new { StatusCode = (ReadWriteErrorCode)i, ThrowsException = i != (byte)ReadWriteErrorCode.Success });
  60. foreach (var entry in testData)
  61. {
  62. if (entry.ThrowsException)
  63. {
  64. Assert.ThrowsException<Exception>(() => Plc.ValidateResponseCode(entry.StatusCode));
  65. }
  66. else
  67. {
  68. Plc.ValidateResponseCode(entry.StatusCode);
  69. }
  70. }
  71. }
  72. }
  73. }