-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WinDivertPacket增加ApplyLengthToIPHeader()方法
- Loading branch information
Showing
2 changed files
with
157 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
using System; | ||
using System.Net; | ||
using System.Net.Sockets; | ||
using Xunit; | ||
|
||
namespace WindivertDotnet.Test | ||
{ | ||
public class WinDivertPacketTest | ||
{ | ||
[Fact] | ||
public void CtorTest() | ||
{ | ||
using var packet = new WinDivertPacket(10); | ||
Assert.Equal(10, packet.Capacity); | ||
Assert.Equal(0, packet.Length); | ||
Assert.Equal(0, packet.Span.Length); | ||
} | ||
|
||
[Fact] | ||
public void LengthTest() | ||
{ | ||
using var packet = new WinDivertPacket(10); | ||
packet.Length = 5; | ||
Assert.Equal(10, packet.Capacity); | ||
Assert.Equal(5, packet.Length); | ||
Assert.Equal(5, packet.Span.Length); | ||
|
||
Assert.Throws<ArgumentOutOfRangeException>(() => packet.Length = 12); | ||
} | ||
|
||
[Fact] | ||
public void GetSpanTest() | ||
{ | ||
using var packet = new WinDivertPacket(10); | ||
packet.Length = 1; | ||
Assert.Equal(0, packet.GetSpan(0, 10)[0]); | ||
Assert.Throws<ArgumentOutOfRangeException>(() => packet.GetSpan(1, 10)); | ||
} | ||
|
||
[Fact] | ||
public unsafe void CalcNetworkIfIdxTest() | ||
{ | ||
var router = new WinDivertRouter(IPAddress.Loopback); | ||
using var addr = new WinDivertAddress(); | ||
|
||
using var packet = new WinDivertPacket(); | ||
var ipv4Header = CreateIPV4Header(router); | ||
packet.GetWriter().Write(ipv4Header); | ||
|
||
packet.CalcNetworkIfIdx(addr); | ||
Assert.Equal(router.InterfaceIndex, addr.Network->IfIdx); | ||
} | ||
|
||
[Fact] | ||
public void CalcLoopbackFlagTest() | ||
{ | ||
var router = new WinDivertRouter(IPAddress.Loopback); | ||
using var addr = new WinDivertAddress(); | ||
|
||
using var packet = new WinDivertPacket(); | ||
var ipv4Header = CreateIPV4Header(router); | ||
packet.GetWriter().Write(ipv4Header); | ||
|
||
packet.CalcLoopbackFlag(addr); | ||
Assert.Equal(router.IsLoopback, addr.Flags.HasFlag(WinDivertAddressFlag.Loopback)); | ||
} | ||
|
||
|
||
[Fact] | ||
public void CalcOutboundFlagTest() | ||
{ | ||
var router = new WinDivertRouter(IPAddress.Loopback); | ||
using var addr = new WinDivertAddress(); | ||
|
||
using var packet = new WinDivertPacket(); | ||
var ipv4Header = CreateIPV4Header(router); | ||
packet.GetWriter().Write(ipv4Header); | ||
|
||
packet.CalcOutboundFlag(addr); | ||
Assert.Equal(router.IsOutbound, addr.Flags.HasFlag(WinDivertAddressFlag.Outbound)); | ||
} | ||
|
||
[Fact] | ||
public unsafe void ApplyLengthToIPHeaderTest() | ||
{ | ||
var router = new WinDivertRouter(IPAddress.Loopback); | ||
using var packet = new WinDivertPacket(); | ||
|
||
var ipv4Header = CreateIPV4Header(router); | ||
ipv4Header.Length = 0; | ||
packet.GetWriter().Write(ipv4Header); | ||
|
||
packet.ApplyLengthToIPHeader(); | ||
var result = packet.GetParseResult(); | ||
|
||
Assert.Equal(sizeof(IPV4Header), result.IPV4Header->Length); | ||
} | ||
|
||
private static IPV4Header CreateIPV4Header(WinDivertRouter router) | ||
{ | ||
return new IPV4Header | ||
{ | ||
Version = 4, | ||
HdrLength = 5, | ||
Length = 20, | ||
Protocol = ProtocolType.IP, | ||
SrcAddr = router.SrcAddress, | ||
DstAddr = router.DstAddress | ||
}; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters