Skip to content

Commit

Permalink
增加BufferWriter测试
Browse files Browse the repository at this point in the history
  • Loading branch information
xljiulang committed Oct 19, 2022
1 parent 9e54d59 commit b99ea6a
Show file tree
Hide file tree
Showing 6 changed files with 156 additions and 11 deletions.
3 changes: 2 additions & 1 deletion WindivertDotnet.Test/FilterTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

namespace WindivertDotnet.Test
{
[SupportedOSPlatform("windows")]
public class FilterTest
{
[Fact]
Expand Down Expand Up @@ -51,6 +50,7 @@ public void OrFilterTest()
}

[Fact]
[SupportedOSPlatform("windows")]
public void BaseTest()
{
var filter = Filter.False
Expand All @@ -74,6 +74,7 @@ public void BaseTest()


[Fact]
[SupportedOSPlatform("windows")]
public void NetworkTest()
{
var filter = Filter.False
Expand Down
44 changes: 44 additions & 0 deletions WindivertDotnet.Test/HeaderSizeTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using System.Runtime.CompilerServices;
using Xunit;

namespace WindivertDotnet.Test
{
public class HeaderSizeTest
{
[Fact]
public void IPV4HeaderSizeTest()
{
Assert.Equal(20, Unsafe.SizeOf<IPV4Header>());
}

[Fact]
public void IPV6HeaderSizeTest()
{
Assert.Equal(40, Unsafe.SizeOf<IPV6Header>());
}

[Fact]
public void IcmpV4HeaderSizeTest()
{
Assert.Equal(8, Unsafe.SizeOf<IcmpV4Header>());
}

[Fact]
public void IcmpV6HeaderSizeTest()
{
Assert.Equal(8, Unsafe.SizeOf<IcmpV6Header>());
}

[Fact]
public void TcpHeaderHeaderSizeTest()
{
Assert.Equal(20, Unsafe.SizeOf<TcpHeader>());
}

[Fact]
public void UdpHeaderHeaderSizeTest()
{
Assert.Equal(8, Unsafe.SizeOf<UdpHeader>());
}
}
}
94 changes: 94 additions & 0 deletions WindivertDotnet.Test/WinDivertBufferWriterTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
using System;
using System.Buffers.Binary;
using System.Linq;
using System.Runtime.CompilerServices;
using Xunit;

namespace WindivertDotnet.Test
{
public class WinDivertBufferWriterTest
{
[Fact]
public void WriteByteTest()
{
using var packet = new WinDivertPacket();
var writer = packet.GetWriter();
writer.Write(byte.MaxValue);
Assert.Equal(1, packet.Length);
Assert.Equal(byte.MaxValue, packet.Span[0]);
}


[Fact]
public void WriteSpanTest()
{
using var packet = new WinDivertPacket();
var writer = packet.GetWriter();
writer.Write(new byte[] { 1, 2 });
Assert.Equal(2, packet.Length);
Assert.True(packet.Span.SequenceEqual(new byte[] { 1, 2 }));
}


[Fact]
public void WriteInt32Test()
{
using var packet = new WinDivertPacket();
var writer = packet.GetWriter();
writer.Write(100);
Assert.Equal(sizeof(int), packet.Length);
Assert.Equal(100, BitConverter.ToInt32(packet.Span));
}

[Fact]
public void WriteMyStructTest()
{
using var packet = new WinDivertPacket();
var writer = packet.GetWriter();
var myStruct = new MyStruct { Byte1 = 1, Byte2 = 2 };
writer.Write(myStruct);

Assert.Equal(Unsafe.SizeOf<MyStruct>(), packet.Length);
Assert.True(packet.Span[0] == 1 && packet.Span[1] == 2);
}


[Fact]
public void WriteReverseInt32Test()
{
using var packet = new WinDivertPacket();
var writer = packet.GetWriter();
writer.WriteReverse(100);
Assert.Equal(sizeof(int), packet.Length);
Assert.Equal(BinaryPrimitives.ReverseEndianness(100), BitConverter.ToInt32(packet.Span));
}

[Fact]
public void WriteReverseMyStructTest()
{
using var packet = new WinDivertPacket();
var writer = packet.GetWriter();
var myStruct = new MyStruct { Byte1 = 1, Byte2 = 2 };
writer.WriteReverse(myStruct);

Assert.Equal(Unsafe.SizeOf<MyStruct>(), packet.Length);
Assert.True(packet.Span[0] == 2 && packet.Span[1] == 1);
}

[Fact]
public void OffsetLengthTest()
{
using var packet = new WinDivertPacket();
var writer = packet.GetWriter(10);
writer.Advance(8);

Assert.Equal(18, packet.Length);
}

struct MyStruct
{
public byte Byte1;
public byte Byte2;
}
}
}
8 changes: 7 additions & 1 deletion WindivertDotnet/WinDivertPacket.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ namespace WindivertDotnet
/// <summary>
/// 表示WinDivert的数据包
/// </summary>
[SupportedOSPlatform("windows")]
[DebuggerDisplay("Length = {Length}, Capacity = {Capacity}")]
public class WinDivertPacket : SafeHandleZeroOrMinusOneIsInvalid
{
Expand Down Expand Up @@ -124,6 +123,7 @@ public unsafe Span<byte> GetSpan(int offset, int count)
/// <param name="addr">地址信息</param>
/// <param name="flag"></param>
/// <returns></returns>
[SupportedOSPlatform("windows")]
public bool CalcChecksums(WinDivertAddress addr, ChecksumsFlag flag = ChecksumsFlag.All)
{
return WinDivertNative.WinDivertHelperCalcChecksums(this, this.length, addr, flag);
Expand All @@ -136,6 +136,7 @@ public bool CalcChecksums(WinDivertAddress addr, ChecksumsFlag flag = ChecksumsF
/// <param name="addr">地址信息</param>
/// <returns></returns>
/// <exception cref="NetworkInformationException"></exception>
[SupportedOSPlatform("windows")]
public unsafe bool CalcNetworkIfIdx(WinDivertAddress addr)
{
if (addr.Layer == WinDivertLayer.Network &&
Expand All @@ -154,6 +155,7 @@ public unsafe bool CalcNetworkIfIdx(WinDivertAddress addr)
/// </summary>
/// <param name="addr">地址信息</param>
/// <returns></returns>
[SupportedOSPlatform("windows")]
public unsafe bool CalcOutboundFlag(WinDivertAddress addr)
{
if (addr.Layer != WinDivertLayer.Network ||
Expand Down Expand Up @@ -236,6 +238,7 @@ private unsafe bool TryParseIPAddress(
/// ttl减1
/// </summary>
/// <returns></returns>
[SupportedOSPlatform("windows")]
public bool DecrementTTL()
{
return WinDivertNative.WinDivertHelperDecrementTTL(this, this.length);
Expand All @@ -245,6 +248,7 @@ public bool DecrementTTL()
/// 获取包的哈希
/// </summary>
/// <returns></returns>
[SupportedOSPlatform("windows")]
public override int GetHashCode()
{
return this.GetHashCode(seed: 0L);
Expand All @@ -255,6 +259,7 @@ public override int GetHashCode()
/// </summary>
/// <param name="seed"></param>
/// <returns></returns>
[SupportedOSPlatform("windows")]
public int GetHashCode(long seed)
{
return WinDivertNative.WinDivertHelperHashPacket(this, this.length, seed);
Expand All @@ -264,6 +269,7 @@ public int GetHashCode(long seed)
/// 获取包的解析结果
/// </summary>
/// <returns></returns>
[SupportedOSPlatform("windows")]
public unsafe WinDivertParseResult GetParseResult()
{
IPV4Header* pIPV4Header;
Expand Down
16 changes: 8 additions & 8 deletions WindivertDotnet/WindivertBufferWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace WindivertDotnet
{
/// <summary>
/// Windivert缓冲区Writer
/// </summary>
/// </summary>
public class WindivertBufferWriter : IBufferWriter<byte>
{
private int index;
Expand All @@ -18,7 +18,7 @@ public class WindivertBufferWriter : IBufferWriter<byte>
/// <param name="packet">数据包</param>
/// <param name="offset">偏移量</param>
public WindivertBufferWriter(WinDivertPacket packet, int offset)
{
{
this.packet = packet;
this.index = offset;
}
Expand Down Expand Up @@ -67,9 +67,9 @@ public void Write(ReadOnlySpan<byte> value)
/// <exception cref="ArgumentOutOfRangeException"></exception>
public void Write(byte value)
{
const int size = 1;
this.GetSpan(size)[0] = value;
this.Advance(size);
const int count = 1;
this.GetSpan(count)[0] = value;
this.Advance(count);
}

/// <summary>
Expand All @@ -79,9 +79,9 @@ public void Write(byte value)
/// <exception cref="ArgumentOutOfRangeException"></exception>
public void Advance(int count)
{
var value = this.index + count;
this.packet.Length = value;
this.index = value;
var size = this.index + count;
this.packet.Length = size;
this.index = size;
}

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion WindivertDotnet/WindivertDotnet.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<Version>1.0.5</Version>
<Version>1.0.6</Version>
<TargetFrameworks>netcoreapp3.1;net6.0</TargetFrameworks>
<Nullable>enable</Nullable>
<NoWarn>CA2012</NoWarn>
Expand Down

0 comments on commit b99ea6a

Please sign in to comment.