Skip to content

Commit

Permalink
[fix] 修正IPacket.ReadBytes读取数据错误;增加IPacket单元测试
Browse files Browse the repository at this point in the history
  • Loading branch information
nnhy committed Dec 6, 2024
1 parent acb18ab commit 05b4f93
Show file tree
Hide file tree
Showing 4 changed files with 147 additions and 13 deletions.
2 changes: 1 addition & 1 deletion NewLife.Core/Data/IPacket.cs
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ public static Byte[] ReadBytes(this IPacket pk, Int32 offset = 0, Int32 count =
if (seg.Offset == 0 && seg.Count == seg.Array!.Length) return seg.Array;
}

return seg.Array!.ReadBytes(offset, count);
return seg.Array!.ReadBytes(seg.Offset + offset, count);
}

var span = pk.GetSpan();
Expand Down
140 changes: 140 additions & 0 deletions XUnitTest.Core/Data/IPacketTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
using NewLife;
using NewLife.Data;
using NewLife.Reflection;
using NewLife.Security;
using Xunit;

namespace XUnitTest.Data;

public class IPacketTests
{
[Fact]
public void OwnerPacketTest()
{
var pk = new OwnerPacket(123);

Assert.NotNull(pk.Buffer);
Assert.Equal(128, pk.Buffer.Length);
Assert.Equal(0, pk.Offset);
Assert.Equal(123, pk.Length);
Assert.Equal(123, pk.Total);
Assert.Null(pk.Next);
Assert.True((Boolean)pk.GetValue("_hasOwner"));

pk[77] = (Byte)'A';
Assert.Equal('A', (Char)pk[77]);

var span = pk.GetSpan();
Assert.Equal('A', (Char)span[77]);

var memory = pk.GetMemory();
Assert.Equal(123, memory.Length);
Assert.Equal('A', (Char)memory.Span[77]);

pk.Resize(127);

var pk2 = pk.Slice(7, 70, false) as OwnerPacket;
Assert.NotNull(pk2);
Assert.Equal(70, pk2.Length);
Assert.Equal(7, pk2.Offset);
Assert.False((Boolean)pk2.GetValue("_hasOwner"));
Assert.True((Boolean)pk.GetValue("_hasOwner"));

var rs = (pk2 as IPacket).TryGetArray(out var segment);
Assert.True(rs);
Assert.Equal(pk.Buffer, segment.Array);
Assert.Equal(7, segment.Offset);
Assert.Equal(70, segment.Count);

pk2.TryDispose();
Assert.False((Boolean)pk2.GetValue("_hasOwner"));
Assert.True((Boolean)pk.GetValue("_hasOwner"));

// 扩展头部
var pk3 = pk2.ExpandHeader(3) as OwnerPacket;
Assert.NotNull(pk3);
Assert.Equal(pk.Buffer, pk3.Buffer);
Assert.Equal(7 - 3, pk3.Offset);
Assert.Equal(70 + 3, pk3.Length);
}

[Fact]
public void MemoryPacketTest()
{
var buf = Rand.NextBytes(125);
var pk = new MemoryPacket(buf, 123);

Assert.Equal(125, pk.Memory.Length);
Assert.Equal(123, pk.Length);
Assert.Equal(123, pk.Total);
Assert.Null(pk.Next);

pk[77] = (Byte)'A';
Assert.Equal('A', (Char)pk[77]);

var span = pk.GetSpan();
Assert.Equal('A', (Char)span[77]);

var memory = pk.GetMemory();
Assert.Equal(123, memory.Length);
Assert.Equal('A', (Char)memory.Span[77]);

var pk2 = (MemoryPacket)pk.Slice(7, 70, false);
Assert.Equal(70, pk2.Length);

var rs = (pk2 as IPacket).TryGetArray(out var segment);
Assert.True(rs);
Assert.Equal(buf, segment.Array);
Assert.Equal(7, segment.Offset);
Assert.Equal(70, segment.Count);

pk2.TryDispose();

// 扩展头部
var pk3 = (OwnerPacket)pk2.ExpandHeader(3);
//Assert.Equal(pk.Memory, pk3.Memory);
Assert.Equal(70 + 3, pk3.Total);
}

[Fact]
public void ArrayPacketTest()
{
var buf = Rand.NextBytes(125);
var pk = new ArrayPacket(buf, 2, 123);

Assert.NotNull(pk.Buffer);
Assert.Equal(125, pk.Buffer.Length);
Assert.Equal(2, pk.Offset);
Assert.Equal(123, pk.Length);
Assert.Equal(123, pk.Total);
Assert.Null(pk.Next);

pk[77] = (Byte)'A';
Assert.Equal('A', (Char)pk[77]);

var span = pk.GetSpan();
Assert.Equal('A', (Char)span[77]);

var memory = pk.GetMemory();
Assert.Equal(123, memory.Length);
Assert.Equal('A', (Char)memory.Span[77]);

var pk2 = (ArrayPacket)pk.Slice(7, 70, false);
Assert.Equal(70, pk2.Length);
Assert.Equal(2 + 7, pk2.Offset);

var rs = (pk2 as IPacket).TryGetArray(out var segment);
Assert.True(rs);
Assert.Equal(pk.Buffer, segment.Array);
Assert.Equal(2 + 7, segment.Offset);
Assert.Equal(70, segment.Count);

pk2.TryDispose();

// 扩展头部
var pk3 = (ArrayPacket)pk2.ExpandHeader(3);
Assert.Equal(pk.Buffer, pk3.Buffer);
Assert.Equal(2 + 7 - 3, pk3.Offset);
Assert.Equal(70 + 3, pk3.Length);
}
}
4 changes: 1 addition & 3 deletions XUnitTest.Core/Data/PacketTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System;
using System.IO;
using System.Text;
using System.Text;
using NewLife;
using NewLife.Data;
using Xunit;
Expand Down
14 changes: 5 additions & 9 deletions XUnitTest.Core/XUnitTest.Core.csproj
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk" ToolsVersion="Current">
<PropertyGroup>
<TargetFrameworks>net6.0;net8.0;net9.0</TargetFrameworks>
<TargetFrameworks>net8.0;net9.0</TargetFrameworks>
<IsPackable>false</IsPackable>
<RootNamespace>XUnitTest</RootNamespace>
<DebugType>pdbonly</DebugType>
Expand All @@ -10,11 +10,7 @@
<SatelliteResourceLanguages>en</SatelliteResourceLanguages>
<ImplicitUsings>enable</ImplicitUsings>
<LangVersion>latest</LangVersion>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)'=='Debug'">
<DefineConstants>$(DefineConstants);DEBUG</DefineConstants>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<IsTestProject>true</IsTestProject>
</PropertyGroup>
<ItemGroup>
<Compile Remove="Applications\**" />
Expand Down Expand Up @@ -56,7 +52,7 @@
<EmbeddedResource Include="IO\excel.xlsx" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="BouncyCastle.Cryptography" Version="2.4.0" />
<PackageReference Include="BouncyCastle.Cryptography" Version="2.5.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.12.0" />
<PackageReference Include="Moq" Version="4.20.72" />
<PackageReference Include="NewLife.Modbus" Version="1.8.2024.426-beta1011" />
Expand All @@ -68,10 +64,10 @@
</PackageReference>
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)'=='net6.0'">
<PackageReference Include="Microsoft.Extensions.Primitives" Version="9.0.0" />
<PackageReference Include="Microsoft.Extensions.Primitives" Version="6.0.0" />
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)'=='net8.0'">
<PackageReference Include="Microsoft.Extensions.Primitives" Version="9.0.0" />
<PackageReference Include="Microsoft.Extensions.Primitives" Version="8.0.0" />
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)'=='net9.0'">
<PackageReference Include="Microsoft.Extensions.Primitives" Version="9.0.0" />
Expand Down

0 comments on commit 05b4f93

Please sign in to comment.