mirror of
https://github.com/S7NetPlus/s7netplus.git
synced 2026-02-17 22:38:27 +08:00
Change project to allow testing internal classes.
Add protocol tests Simplify async tests
This commit is contained in:
53
S7.Net.UnitTest/ProtocolTests.cs
Normal file
53
S7.Net.UnitTest/ProtocolTests.cs
Normal file
@@ -0,0 +1,53 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
using S7.Net;
|
||||
|
||||
using System.IO;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace S7.Net.UnitTest
|
||||
{
|
||||
[TestClass]
|
||||
public class ProtocolUnitTest
|
||||
{
|
||||
[TestMethod]
|
||||
public void TPKT_Read()
|
||||
{
|
||||
var m = new MemoryStream(StringToByteArray("0300002902f0803203000000010002001400000401ff0400807710000100000103000000033f8ccccd"));
|
||||
var t = TPKT.Read(m);
|
||||
Assert.AreEqual(0x03, t.Version);
|
||||
Assert.AreEqual(0x29, t.Length);
|
||||
m.Position = 0;
|
||||
t = TPKT.ReadAsync(m).Result;
|
||||
Assert.AreEqual(0x03, t.Version);
|
||||
Assert.AreEqual(0x29, t.Length);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
[ExpectedException(typeof(TPKTInvalidException))]
|
||||
public void TPKT_ReadShort()
|
||||
{
|
||||
var m = new MemoryStream(StringToByteArray("0300002902f0803203000000010002001400000401ff040080"));
|
||||
var t = TPKT.Read(m);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
[ExpectedException(typeof(TPKTInvalidException))]
|
||||
public async Task TPKT_ReadShortAsync()
|
||||
{
|
||||
var m = new MemoryStream(StringToByteArray("0300002902f0803203000000010002001400000401ff040080"));
|
||||
var t = await TPKT.ReadAsync(m);
|
||||
}
|
||||
|
||||
private static byte[] StringToByteArray(string hex)
|
||||
{
|
||||
return Enumerable.Range(0, hex.Length)
|
||||
.Where(x => x % 2 == 0)
|
||||
.Select(x => Convert.ToByte(hex.Substring(x, 2), 16))
|
||||
.ToArray();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -37,6 +37,12 @@
|
||||
<WarningLevel>4</WarningLevel>
|
||||
<Prefer32Bit>false</Prefer32Bit>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup>
|
||||
<SignAssembly>true</SignAssembly>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup>
|
||||
<AssemblyOriginatorKeyFile>S7.Net.snk</AssemblyOriginatorKeyFile>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<Reference Include="Microsoft.VisualStudio.QualityTools.UnitTestFramework, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
|
||||
<Private>False</Private>
|
||||
@@ -55,6 +61,7 @@
|
||||
</Choose>
|
||||
<ItemGroup>
|
||||
<Compile Include="ConvertersUnitTest.cs" />
|
||||
<Compile Include="ProtocolTests.cs" />
|
||||
<Compile Include="Helpers\ConsoleManager.cs" />
|
||||
<Compile Include="Helpers\NativeMethods.cs" />
|
||||
<Compile Include="Helpers\S7TestServer.cs" />
|
||||
@@ -81,6 +88,9 @@
|
||||
<Name>S7.Net</Name>
|
||||
</ProjectReference>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<None Include="S7.Net.snk" />
|
||||
</ItemGroup>
|
||||
<Choose>
|
||||
<When Condition="'$(VisualStudioVersion)' == '10.0' And '$(IsCodedUITest)' == 'True'">
|
||||
<ItemGroup>
|
||||
|
||||
@@ -479,22 +479,15 @@ namespace S7.Net.UnitTest
|
||||
|
||||
|
||||
[TestMethod]
|
||||
[ExpectedException(typeof(NullReferenceException))]
|
||||
public async Task Test_Async_ReadBytesThrowsExceptionIfPlcIsNotConnected()
|
||||
{
|
||||
using (var notConnectedPlc = new Plc(CpuType.S7300, "255.255.255.255", 0, 0))
|
||||
{
|
||||
Assert.IsFalse(notConnectedPlc.IsConnected);
|
||||
|
||||
TestClass tc = new TestClass();
|
||||
try
|
||||
{
|
||||
var res = await notConnectedPlc.ReadClassAsync(tc, DB2);
|
||||
Assert.Fail();
|
||||
}
|
||||
catch
|
||||
{
|
||||
|
||||
}
|
||||
var res = await notConnectedPlc.ReadClassAsync(tc, DB2);
|
||||
Assert.Fail();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -530,17 +523,13 @@ namespace S7.Net.UnitTest
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
[ExpectedException(typeof(NullReferenceException))]
|
||||
public async Task Test_Async_ReadClassWithGenericReturnsNullIfPlcIsNotConnected()
|
||||
{
|
||||
using (var notConnectedPlc = new Plc(CpuType.S7300, "255.255.255.255", 0, 0))
|
||||
{
|
||||
Assert.IsFalse(notConnectedPlc.IsConnected, "Before executing this test, the plc must be connected. Check constructor.");
|
||||
try
|
||||
{
|
||||
TestClass tc = await notConnectedPlc.ReadClassAsync<TestClass>(DB2);
|
||||
Assert.Fail();
|
||||
}
|
||||
catch { }
|
||||
TestClass tc = await notConnectedPlc.ReadClassAsync<TestClass>(DB2);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -574,37 +563,24 @@ namespace S7.Net.UnitTest
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
[ExpectedException(typeof(NullReferenceException))]
|
||||
public async Task Test_Async_ReadClassWithGenericAndClassFactoryThrowsExceptionPlcIsNotConnected()
|
||||
{
|
||||
using (var notConnectedPlc = new Plc(CpuType.S7300, "255.255.255.255", 0, 0))
|
||||
{
|
||||
Assert.IsFalse(notConnectedPlc.IsConnected);
|
||||
try
|
||||
{
|
||||
TestClass tc = await notConnectedPlc.ReadClassAsync(() => new TestClass(), DB2);
|
||||
Assert.Fail();
|
||||
}
|
||||
catch
|
||||
{
|
||||
|
||||
}
|
||||
TestClass tc = await notConnectedPlc.ReadClassAsync(() => new TestClass(), DB2);
|
||||
}
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
[ExpectedException(typeof(NullReferenceException))]
|
||||
public async Task Test_Async_ReadStructThrowsExceptionPlcIsNotConnected()
|
||||
{
|
||||
using (var notConnectedPlc = new Plc(CpuType.S7300, "255.255.255.255", 0, 0))
|
||||
{
|
||||
Assert.IsFalse(notConnectedPlc.IsConnected);
|
||||
try
|
||||
{
|
||||
object tsObj = await notConnectedPlc.ReadStructAsync(typeof(TestStruct), DB2);
|
||||
Assert.Fail();
|
||||
} catch
|
||||
{
|
||||
|
||||
}
|
||||
object tsObj = await notConnectedPlc.ReadStructAsync(typeof(TestStruct), DB2);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -639,16 +615,13 @@ namespace S7.Net.UnitTest
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
[ExpectedException(typeof(NullReferenceException))]
|
||||
public async Task Test_Async_ReadStructWithGenericThrowsExceptionIfPlcIsNotConnected()
|
||||
{
|
||||
using (var notConnectedPlc = new Plc(CpuType.S7300, "255.255.255.255", 0, 0))
|
||||
{
|
||||
Assert.IsFalse(notConnectedPlc.IsConnected);
|
||||
try
|
||||
{
|
||||
object tsObj = await notConnectedPlc.ReadStructAsync<TestStruct>(DB2);
|
||||
Assert.Fail();
|
||||
} catch { }
|
||||
object tsObj = await notConnectedPlc.ReadStructAsync<TestStruct>(DB2);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -787,7 +760,7 @@ namespace S7.Net.UnitTest
|
||||
|
||||
for (int x = 0; x < count; x++)
|
||||
{
|
||||
Assert.AreEqual(x % 256, res[x]);
|
||||
Assert.AreEqual(x % 256, res[x], string.Format("Bit {0} failed", x));
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
@@ -63,4 +63,23 @@ namespace S7.Net
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
internal class TPKTInvalidException : Exception
|
||||
{
|
||||
public TPKTInvalidException() : base()
|
||||
{
|
||||
}
|
||||
|
||||
public TPKTInvalidException(string message) : base(message)
|
||||
{
|
||||
}
|
||||
|
||||
public TPKTInvalidException(string message, Exception innerException) : base(message, innerException)
|
||||
{
|
||||
}
|
||||
|
||||
protected TPKTInvalidException(SerializationInfo info, StreamingContext context) : base(info, context)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,6 +14,9 @@ using System.Runtime.InteropServices;
|
||||
[assembly: AssemblyTrademark("")]
|
||||
[assembly: AssemblyCulture("")]
|
||||
|
||||
|
||||
[assembly: InternalsVisibleTo("S7Net.UnitTest, PublicKey=00240000048000009400000006020000002400005253413100040000010001002d1032db55f60d64bf90ea1cc2247b5a8b9b6168a07bcd464a07ce2e425d027ff9409a64ba0e3f37718e14c50cf964d0d921e5ae8b8d74bd8a82431794f897cebf0ee668feb2ccd030153611b2808fcb7785c5e5136a98e0ec23de3c1ed385d2026c26e4bed5805ff9db7e0544f59b1f19d369d43403a624586795926e38c48d")]
|
||||
|
||||
// Durch Festlegen von ComVisible auf "false" werden die Typen in dieser Assembly unsichtbar
|
||||
// für COM-Komponenten. Wenn Sie auf einen Typ in dieser Assembly von
|
||||
// COM zugreifen müssen, legen Sie das ComVisible-Attribut für diesen Typ auf "true" fest.
|
||||
|
||||
Reference in New Issue
Block a user