Commit 3840c47a authored by Brickner_cp's avatar Brickner_cp

--no commit message

--no commit message
parent ab01160e
...@@ -80,11 +80,7 @@ namespace PcapDotNet.Core.Test ...@@ -80,11 +80,7 @@ namespace PcapDotNet.Core.Test
Assert.AreEqual<uint>(0, communicator.TotalStatistics.PacketsCaptured); Assert.AreEqual<uint>(0, communicator.TotalStatistics.PacketsCaptured);
MoreAssert.IsInRange(TimeSpan.FromSeconds(1), TimeSpan.FromSeconds(1.02), finishedWaiting - startWaiting); MoreAssert.IsInRange(TimeSpan.FromSeconds(1), TimeSpan.FromSeconds(1.02), finishedWaiting - startWaiting);
Packet sentPacket = PacketBuilder.Ethernet(DateTime.Now, Packet sentPacket = MoreRandom.BuildRandomPacket(SourceMac, DestinationMac, 24);
new MacAddress(SourceMac),
new MacAddress(DestinationMac),
EthernetType.IpV4,
GetRandomDatagram(10));
DateTime startSendingTime = DateTime.Now; DateTime startSendingTime = DateTime.Now;
...@@ -159,11 +155,7 @@ namespace PcapDotNet.Core.Test ...@@ -159,11 +155,7 @@ namespace PcapDotNet.Core.Test
string dumpFilename = Path.GetTempPath() + @"dump.pcap"; string dumpFilename = Path.GetTempPath() + @"dump.pcap";
const int NumPackets = 10; const int NumPackets = 10;
Packet expectedPacket = PacketBuilder.Ethernet(DateTime.Now, Packet expectedPacket = MoreRandom.BuildRandomPacket(SourceMac, DestinationMac, 24);
new MacAddress(SourceMac),
new MacAddress(DestinationMac),
EthernetType.IpV4,
GetRandomDatagram(10));
using (PacketCommunicator communicator = OpenLiveDevice()) using (PacketCommunicator communicator = OpenLiveDevice())
{ {
...@@ -211,11 +203,7 @@ namespace PcapDotNet.Core.Test ...@@ -211,11 +203,7 @@ namespace PcapDotNet.Core.Test
communicator.Mode = PacketCommunicatorMode.Statistics; communicator.Mode = PacketCommunicatorMode.Statistics;
communicator.SetFilter("ether src " + SourceMac + " and ether dst " + DestinationMac); communicator.SetFilter("ether src " + SourceMac + " and ether dst " + DestinationMac);
Packet sentPacket = PacketBuilder.Ethernet(DateTime.Now, Packet sentPacket = MoreRandom.BuildRandomPacket(SourceMac, DestinationMac, PacketSize);
new MacAddress(SourceMac),
new MacAddress(DestinationMac),
EthernetType.IpV4,
GetRandomDatagram(PacketSize - EthernetDatagram.HeaderLength));
PacketSampleStatistics statistics; PacketSampleStatistics statistics;
PacketCommunicatorReceiveResult result = communicator.GetNextStatistics(out statistics); PacketCommunicatorReceiveResult result = communicator.GetNextStatistics(out statistics);
...@@ -277,11 +265,7 @@ namespace PcapDotNet.Core.Test ...@@ -277,11 +265,7 @@ namespace PcapDotNet.Core.Test
communicator.SetFilter("ether src " + sourceMac + " and ether dst " + destinationMac); communicator.SetFilter("ether src " + sourceMac + " and ether dst " + destinationMac);
Packet sentPacket = PacketBuilder.Ethernet(DateTime.Now, Packet sentPacket = MoreRandom.BuildRandomPacket(sourceMac, destinationMac, packetSize);
new MacAddress(sourceMac),
new MacAddress(destinationMac),
EthernetType.IpV4,
GetRandomDatagram(packetSize - EthernetDatagram.HeaderLength));
PacketCommunicatorReceiveResult result = PacketCommunicatorReceiveResult.None; PacketCommunicatorReceiveResult result = PacketCommunicatorReceiveResult.None;
int numStatisticsGot = 0; int numStatisticsGot = 0;
...@@ -320,19 +304,15 @@ namespace PcapDotNet.Core.Test ...@@ -320,19 +304,15 @@ namespace PcapDotNet.Core.Test
} }
} }
private void TestGetSomePackets(string sourceMac, string destinationMac, int numPacketsToSend, int numPacketsToBreakLoop, int packetSize, bool nonBlocking, private static void TestGetSomePackets(string sourceMac, string destinationMac, int numPacketsToSend, int numPacketsToBreakLoop, int packetSize, bool nonBlocking,
PacketCommunicatorReceiveResult expectedResult, int expectedNumPackets, double expectedMinSeconds, double expectedMaxSeconds) PacketCommunicatorReceiveResult expectedResult, int expectedNumPackets, double expectedMinSeconds, double expectedMaxSeconds)
{ {
Packet packetToSend = PacketBuilder.Ethernet(DateTime.Now, Packet packetToSend = MoreRandom.BuildRandomPacket(sourceMac, destinationMac, packetSize);
new MacAddress(sourceMac),
new MacAddress(destinationMac),
EthernetType.IpV4,
GetRandomDatagram(packetSize - EthernetDatagram.HeaderLength));
using (PacketCommunicator communicator = OpenLiveDevice()) using (PacketCommunicator communicator = OpenLiveDevice())
{ {
communicator.NonBlocking = nonBlocking; communicator.NonBlocking = nonBlocking;
Assert.AreEqual(nonBlocking, communicator.NonBlocking);
communicator.SetFilter("ether src " + sourceMac + " and ether dst " + destinationMac); communicator.SetFilter("ether src " + sourceMac + " and ether dst " + destinationMac);
int numPacketsGot; int numPacketsGot;
...@@ -369,11 +349,7 @@ namespace PcapDotNet.Core.Test ...@@ -369,11 +349,7 @@ namespace PcapDotNet.Core.Test
{ {
communicator.SetFilter("ether src " + sourceMac + " and ether dst " + destinationMac); communicator.SetFilter("ether src " + sourceMac + " and ether dst " + destinationMac);
Packet sentPacket = PacketBuilder.Ethernet(DateTime.Now, Packet sentPacket = MoreRandom.BuildRandomPacket(sourceMac, destinationMac, packetSize);
new MacAddress(sourceMac),
new MacAddress(destinationMac),
EthernetType.IpV4,
GetRandomDatagram(packetSize - EthernetDatagram.HeaderLength));
PacketCommunicatorReceiveResult result = PacketCommunicatorReceiveResult.None; PacketCommunicatorReceiveResult result = PacketCommunicatorReceiveResult.None;
int numPacketsHandled = 0; int numPacketsHandled = 0;
...@@ -408,7 +384,7 @@ namespace PcapDotNet.Core.Test ...@@ -408,7 +384,7 @@ namespace PcapDotNet.Core.Test
} }
} }
private static PacketCommunicator OpenLiveDevice() public static PacketCommunicator OpenLiveDevice()
{ {
IList<LivePacketDevice> devices = LivePacketDevice.AllLocalMachine; IList<LivePacketDevice> devices = LivePacketDevice.AllLocalMachine;
MoreAssert.IsBiggerOrEqual(1, devices.Count); MoreAssert.IsBiggerOrEqual(1, devices.Count);
...@@ -437,14 +413,5 @@ namespace PcapDotNet.Core.Test ...@@ -437,14 +413,5 @@ namespace PcapDotNet.Core.Test
throw; throw;
} }
} }
private Datagram GetRandomDatagram(int length)
{
byte[] buffer = new byte[length];
_random.NextBytes(buffer);
return new Datagram(buffer);
}
private Random _random = new Random();
} }
} }
\ No newline at end of file
...@@ -3,7 +3,7 @@ using Microsoft.VisualStudio.TestTools.UnitTesting; ...@@ -3,7 +3,7 @@ using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace PcapDotNet.Core.Test namespace PcapDotNet.Core.Test
{ {
public static class MoreAssert internal static class MoreAssert
{ {
public static void IsBiggerOrEqual<T>(T expectedMinimum, T actual) where T : IComparable<T> public static void IsBiggerOrEqual<T>(T expectedMinimum, T actual) where T : IComparable<T>
{ {
......
using System;
using Packets;
namespace PcapDotNet.Core.Test
{
internal static class MoreRandom
{
public static Packet BuildRandomPacket(DateTime timestamp, string sourceMac, string destinationMac, int packetSize)
{
if (packetSize < EthernetDatagram.HeaderLength)
throw new ArgumentException("Packet size (" + packetSize + ") must be at least the ethernet header length", "packetSize");
return PacketBuilder.Ethernet(timestamp,
new MacAddress(sourceMac),
new MacAddress(destinationMac),
EthernetType.IpV4,
GetRandomDatagram(packetSize - EthernetDatagram.HeaderLength));
}
public static Packet BuildRandomPacket(string sourceMac, string destinationMac, int packetSize)
{
return BuildRandomPacket(DateTime.Now, sourceMac, destinationMac, packetSize);
}
private static Datagram GetRandomDatagram(int length)
{
byte[] buffer = new byte[length];
_random.NextBytes(buffer);
return new Datagram(buffer);
}
private static readonly Random _random = new Random();
}
}
\ No newline at end of file
using System;
using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Packets;
namespace PcapDotNet.Core.Test
{
/// <summary>
/// Summary description for PcapLibTests
/// </summary>
[TestClass]
public class PacketSendQueueTests
{
public PacketSendQueueTests()
{
//
// TODO: Add constructor logic here
//
}
private TestContext testContextInstance;
/// <summary>
///Gets or sets the test context which provides
///information about and functionality for the current test run.
///</summary>
public TestContext TestContext
{
get
{
return testContextInstance;
}
set
{
testContextInstance = value;
}
}
#region Additional test attributes
//
// You can use the following additional attributes as you write your tests:
//
// Use ClassInitialize to run code before running the first test in the class
// [ClassInitialize()]
// public static void MyClassInitialize(TestContext testContext) { }
//
// Use ClassCleanup to run code after all tests in a class have run
// [ClassCleanup()]
// public static void MyClassCleanup() { }
//
// Use TestInitialize to run code before running each test
// [TestInitialize()]
// public void MyTestInitialize() { }
//
// Use TestCleanup to run code after each test has run
// [TestCleanup()]
// public void MyTestCleanup() { }
//
#endregion
[TestMethod]
public void TransmitQueueTest()
{
TestTransmitQueue(0, 100, 0.5, false);
TestTransmitQueue(10, 60, 0.5, false);
TestTransmitQueue(10, 600, 0.5, false);
TestTransmitQueue(10, 1500, 0.5, false);
TestTransmitQueue(10, 60, 0.5, true);
}
private static void TestTransmitQueue(int numPacketsToSend, int packetSize, double secondsBetweenTimestamps, bool isSynced)
{
const string SourceMac = "11:22:33:44:55:66";
const string DestinationMac = "77:88:99:AA:BB:CC";
int rawPacketSize = packetSize + 16; // I don't know why 16
using (PacketSendQueue queue = new PacketSendQueue((uint)(numPacketsToSend * rawPacketSize)))
{
DateTime timestamp = DateTime.Now.AddSeconds(-100);
List<Packet> packetsToSend = new List<Packet>(numPacketsToSend);
for (int i = 0; i != numPacketsToSend; ++i)
{
Packet packetToSend = MoreRandom.BuildRandomPacket(timestamp, SourceMac, DestinationMac, packetSize);
queue.Enqueue(packetToSend);
packetsToSend.Add(packetToSend);
timestamp = timestamp.AddSeconds(secondsBetweenTimestamps);
}
using (PacketCommunicator communicator = LivePacketDeviceTests.OpenLiveDevice())
{
communicator.SetFilter("ether src " + SourceMac + " and ether dst " + DestinationMac);
communicator.Transmit(queue, isSynced);
DateTime lastTimestamp = DateTime.MinValue;
int numPacketsHandled = 0;
int numPacketsGot;
PacketCommunicatorReceiveResult result =
communicator.GetSomePackets(out numPacketsGot, numPacketsToSend,
delegate(Packet packet)
{
Assert.AreEqual(packetsToSend[numPacketsHandled], packet);
if (numPacketsHandled > 0)
{
TimeSpan expectedDiff;
if (isSynced)
{
expectedDiff =
packetsToSend[numPacketsHandled].Timestamp -
packetsToSend[numPacketsHandled - 1].Timestamp;
}
else
{
expectedDiff = TimeSpan.Zero;
}
TimeSpan actualDiff = packet.Timestamp - lastTimestamp;
Assert.AreEqual(expectedDiff, actualDiff);
}
lastTimestamp = packet.Timestamp;
++numPacketsHandled;
});
Assert.AreEqual(PacketCommunicatorReceiveResult.Ok, result);
Assert.AreEqual(numPacketsToSend, numPacketsGot);
Assert.AreEqual(numPacketsToSend, numPacketsHandled);
}
}
}
}
}
\ No newline at end of file
...@@ -44,6 +44,8 @@ ...@@ -44,6 +44,8 @@
<ItemGroup> <ItemGroup>
<Compile Include="MoreAssert.cs" /> <Compile Include="MoreAssert.cs" />
<Compile Include="LivePacketDeviceTests.cs" /> <Compile Include="LivePacketDeviceTests.cs" />
<Compile Include="MoreRandom.cs" />
<Compile Include="PacketSendQueueTests.cs" />
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="PcapLibTests.cs" /> <Compile Include="PcapLibTests.cs" />
</ItemGroup> </ItemGroup>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment