Commit b3fe733f authored by Brickner_cp's avatar Brickner_cp

--no commit message

--no commit message
parent e02fd8df
......@@ -346,6 +346,160 @@ namespace PcapDotNet.Core.Test
}
}
[TestMethod]
public void SetBigKernelMinimumBytesToCopyTest()
{
const string SourceMac = "11:22:33:44:55:66";
const string DestinationMac = "77:88:99:AA:BB:CC";
using (PacketCommunicator communicator = OpenLiveDevice())
{
communicator.SetFilter("ether src " + SourceMac + " and ether dst " + DestinationMac);
communicator.SetKernelMinimumBytesToCopy(1024 * 1024);
Packet expectedPacket = MoreRandom.BuildRandomPacket(SourceMac, DestinationMac, 100);
for (int i = 0; i != 5; ++i)
{
communicator.SendPacket(expectedPacket);
Packet packet;
DateTime start = DateTime.Now;
PacketCommunicatorReceiveResult result = communicator.GetPacket(out packet);
DateTime end = DateTime.Now;
Assert.AreEqual(PacketCommunicatorReceiveResult.Ok, result);
Assert.AreEqual(expectedPacket, packet);
MoreAssert.IsBigger(TimeSpan.FromSeconds(0.9), end - start);
}
}
}
[TestMethod]
public void SetSmallKernelMinimumBytesToCopyTest()
{
const string SourceMac = "11:22:33:44:55:66";
const string DestinationMac = "77:88:99:AA:BB:CC";
using (PacketCommunicator communicator = OpenLiveDevice())
{
communicator.SetFilter("ether src " + SourceMac + " and ether dst " + DestinationMac);
communicator.SetKernelMinimumBytesToCopy(1);
Packet expectedPacket = MoreRandom.BuildRandomPacket(SourceMac, DestinationMac, 100);
for (int i = 0; i != 100; ++i)
{
communicator.SendPacket(expectedPacket);
Packet packet;
DateTime start = DateTime.Now;
PacketCommunicatorReceiveResult result = communicator.GetPacket(out packet);
DateTime end = DateTime.Now;
Assert.AreEqual(PacketCommunicatorReceiveResult.Ok, result);
Assert.AreEqual(expectedPacket, packet);
MoreAssert.IsSmallerOrEqual(TimeSpan.FromSeconds(0.02), end - start);
}
}
}
[TestMethod]
public void SetSamplingMethodOneEveryNTest()
{
const string SourceMac = "11:22:33:44:55:66";
const string DestinationMac = "77:88:99:AA:BB:CC";
using (PacketCommunicator communicator = OpenLiveDevice())
{
communicator.SetFilter("ether src " + SourceMac + " and ether dst " + DestinationMac);
communicator.SetSamplingMethod(new SamplingMethodOneEveryN(5));
for (int i = 0; i != 20; ++i)
{
Packet expectedPacket = MoreRandom.BuildRandomPacket(SourceMac, DestinationMac, 60 * (i + 1));
communicator.SendPacket(expectedPacket);
}
Packet packet;
PacketCommunicatorReceiveResult result;
for (int i = 0; i != 4; ++i)
{
result = communicator.GetPacket(out packet);
Assert.AreEqual(PacketCommunicatorReceiveResult.Ok, result);
Assert.AreEqual(60 * 5 * (i + 1), packet.Length);
}
result = communicator.GetPacket(out packet);
Assert.AreEqual(PacketCommunicatorReceiveResult.Timeout, result);
Assert.IsNull(packet);
}
}
[TestMethod]
public void SetSamplingMethodFirstAfterIntervalTest()
{
const string SourceMac = "11:22:33:44:55:66";
const string DestinationMac = "77:88:99:AA:BB:CC";
using (PacketCommunicator communicator = OpenLiveDevice())
{
communicator.SetFilter("ether src " + SourceMac + " and ether dst " + DestinationMac);
communicator.SetSamplingMethod(new SamplingMethodFirstAfterInterval(TimeSpan.FromSeconds(1)));
Packet expectedPacket = MoreRandom.BuildRandomPacket(SourceMac, DestinationMac, 60);
communicator.SendPacket(expectedPacket);
Thread.Sleep(TimeSpan.FromSeconds(0.75));
for (int i = 0; i != 10; ++i)
{
expectedPacket = MoreRandom.BuildRandomPacket(SourceMac, DestinationMac, 60 * (i + 2));
communicator.SendPacket(expectedPacket);
Thread.Sleep(TimeSpan.FromSeconds(0.5));
}
Packet packet;
PacketCommunicatorReceiveResult result;
for (int i = 0; i != 6; ++i)
{
result = communicator.GetPacket(out packet);
Assert.AreEqual(PacketCommunicatorReceiveResult.Ok, result);
Assert.AreEqual(60 * (i * 2 + 1), packet.Length);
}
result = communicator.GetPacket(out packet);
Assert.AreEqual(PacketCommunicatorReceiveResult.Timeout, result);
Assert.IsNull(packet);
}
}
[TestMethod]
[ExpectedException(typeof(ArgumentOutOfRangeException))]
public void SetSamplingMethodOneEveryNErrorTest()
{
using (PacketCommunicator communicator = OpenLiveDevice())
{
communicator.SetSamplingMethod(new SamplingMethodOneEveryN(0));
}
}
[TestMethod]
[ExpectedException(typeof(ArgumentOutOfRangeException))]
public void SetSamplingMethodFirstAfterIntervalNegativeMsErrorTest()
{
using (PacketCommunicator communicator = OpenLiveDevice())
{
communicator.SetSamplingMethod(new SamplingMethodFirstAfterInterval(-1));
}
}
[TestMethod]
[ExpectedException(typeof(ArgumentOutOfRangeException))]
public void SetSamplingMethodFirstAfterIntervalNegativeTimespanErrorTest()
{
using (PacketCommunicator communicator = OpenLiveDevice())
{
communicator.SetSamplingMethod(new SamplingMethodFirstAfterInterval(TimeSpan.FromSeconds(-1)));
}
}
[TestMethod]
[ExpectedException(typeof(ArgumentOutOfRangeException))]
public void SetSamplingMethodFirstAfterIntervalBigTimespanErrorTest()
{
using (PacketCommunicator communicator = OpenLiveDevice())
{
communicator.SetSamplingMethod(new SamplingMethodFirstAfterInterval(TimeSpan.FromDays(25)));
}
}
private static void TestGetStatistics(string sourceMac, string destinationMac, int numPacketsToSend, int numStatisticsToGather, int numStatisticsToBreakLoop, double secondsToWait, int packetSize,
PacketCommunicatorReceiveResult expectedResult, int expectedNumStatistics, int expectedNumPackets, double expectedMinSeconds, double expectedMaxSeconds)
{
......@@ -492,6 +646,8 @@ namespace PcapDotNet.Core.Test
try
{
communicator.SetKernelBufferSize(2 * 1024 * 1024); // 2 MB instead of 1
communicator.SetKernelMinimumBytesToCopy(10); // 10 bytes minimum to copy
communicator.SetSamplingMethod(new SamplingMethodNone());
Assert.AreEqual(DataLinkKind.Ethernet, communicator.DataLink.Kind);
communicator.DataLink = communicator.DataLink;
Assert.AreEqual("EN10MB (Ethernet)", communicator.DataLink.ToString());
......
......@@ -8,7 +8,7 @@ namespace PcapDotNet.Core.Test
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");
throw new ArgumentOutOfRangeException("packetSize", packetSize, "Must be at least the ethernet header length (" + EthernetDatagram.HeaderLength + ")");
return PacketBuilder.Ethernet(timestamp,
new MacAddress(sourceMac),
......
......@@ -190,6 +190,63 @@ namespace PcapDotNet.Core.Test
}
}
[TestMethod]
[ExpectedException(typeof(InvalidOperationException))]
public void SetlKernelMinimumBytesToCopyErrorTest()
{
using (PacketCommunicator communicator = OpenOfflineDevice())
{
communicator.SetKernelMinimumBytesToCopy(1024);
}
}
[TestMethod]
public void SetSamplingMethodOneEveryNTest()
{
Packet expectedPacket = MoreRandom.BuildRandomPacket(100);
using (PacketCommunicator communicator = OpenOfflineDevice(101, expectedPacket))
{
communicator.SetSamplingMethod(new SamplingMethodOneEveryN(10));
PacketCommunicatorReceiveResult result;
Packet packet;
for (int i = 0; i != 10; ++i)
{
result = communicator.GetPacket(out packet);
Assert.AreEqual(PacketCommunicatorReceiveResult.Ok, result);
Assert.AreEqual(expectedPacket, packet);
}
result = communicator.GetPacket(out packet);
Assert.AreEqual(PacketCommunicatorReceiveResult.Eof, result);
Assert.IsNull(packet);
}
}
[TestMethod]
public void SetSamplingMethodFirstAfterIntervalTest()
{
const int NumPackets = 10;
Packet expectedPacket = MoreRandom.BuildRandomPacket(100);
using (PacketCommunicator communicator = OpenOfflineDevice(NumPackets, expectedPacket, TimeSpan.FromSeconds(1)))
{
communicator.SetSamplingMethod(new SamplingMethodFirstAfterInterval(TimeSpan.FromSeconds(2)));
PacketCommunicatorReceiveResult result;
Packet packet;
for (int i = 0; i != 5; ++i)
{
result = communicator.GetPacket(out packet);
Assert.AreEqual(PacketCommunicatorReceiveResult.Ok, result);
Assert.AreEqual(expectedPacket, packet);
DateTime expectedTimestamp = expectedPacket.Timestamp.AddSeconds(i * 2);
MoreAssert.IsInRange(expectedTimestamp.AddSeconds(-0.01), expectedTimestamp.AddSeconds(0.01), packet.Timestamp);
}
result = communicator.GetPacket(out packet);
Assert.AreEqual(PacketCommunicatorReceiveResult.Eof, result);
Assert.IsNull(packet);
}
}
private static void TestGetSomePackets(int numPacketsToSend, int numPacketsToGet, int numPacketsToBreakLoop,
PacketCommunicatorReceiveResult expectedResult, int expectedNumPackets,
double expectedMinSeconds, double expectedMaxSeconds)
......@@ -260,7 +317,13 @@ namespace PcapDotNet.Core.Test
return OpenOfflineDevice(10, MoreRandom.BuildRandomPacket(100));
}
public static PacketCommunicator OpenOfflineDevice(int numPackets, Packet packet)
{
return OpenOfflineDevice(numPackets, packet, TimeSpan.Zero);
}
public static PacketCommunicator OpenOfflineDevice(int numPackets, Packet packet, TimeSpan intervalBetweenPackets)
{
string dumpFilename = Path.GetTempPath() + @"dump.pcap";
......@@ -272,6 +335,13 @@ namespace PcapDotNet.Core.Test
int lastPosition = 0;
for (int i = 0; i != numPackets; ++i)
{
if (intervalBetweenPackets != TimeSpan.Zero && i != 0)
{
// Thread.Sleep(intervalBetweenPackets);
DateTime timestamp = packet.Timestamp;
timestamp = timestamp.Add(intervalBetweenPackets);
packet = new Packet(packet.Buffer, timestamp, packet.DataLink);
}
dumpFile.Dump(packet);
MoreAssert.IsBigger(lastPosition, dumpFile.Position);
lastPosition = dumpFile.Position;
......
......@@ -13,6 +13,61 @@ using namespace System::Collections::Generic;
using namespace Packets;
using namespace PcapDotNet::Core;
int SamplingMethodNone::Method::get()
{
return PCAP_SAMP_NOSAMP;
}
int SamplingMethodNone::Value::get()
{
return 0;
}
SamplingMethodOneEveryN::SamplingMethodOneEveryN(int n)
{
if (n <= 0)
throw gcnew ArgumentOutOfRangeException("n", n, "Must be positive");
_n = n;
}
int SamplingMethodOneEveryN::Method::get()
{
return PCAP_SAMP_1_EVERY_N;
}
int SamplingMethodOneEveryN::Value::get()
{
return _n;
}
SamplingMethodFirstAfterInterval::SamplingMethodFirstAfterInterval(int intervalInMs)
{
if (intervalInMs < 0)
throw gcnew ArgumentOutOfRangeException("intervalInMs", intervalInMs, "Must be non negative");
_intervalInMs = intervalInMs;
}
SamplingMethodFirstAfterInterval::SamplingMethodFirstAfterInterval(TimeSpan interval)
{
double intervalInMs = interval.TotalMilliseconds;
if (intervalInMs > Int32::MaxValue)
throw gcnew ArgumentOutOfRangeException("interval", interval, "Must be smaller than " + TimeSpan::FromMilliseconds(Int32::MaxValue).ToString());
if (intervalInMs < 0)
throw gcnew ArgumentOutOfRangeException("interval", interval, "Must be non negative");
_intervalInMs = (int)intervalInMs;
}
int SamplingMethodFirstAfterInterval::Method::get()
{
return PCAP_SAMP_FIRST_AFTER_N_MS;
}
int SamplingMethodFirstAfterInterval::Value::get()
{
return _intervalInMs;
}
PacketCommunicator::PacketCommunicator(const char* source, int snapshotLength, PacketDeviceOpenFlags flags, int readTimeout, pcap_rmtauth *auth, SocketAddress^ netmask)
{
// Open the device
......@@ -124,6 +179,19 @@ void PacketCommunicator::SetKernelBufferSize(int size)
throw BuildInvalidOperation("Error setting kernel buffer size to " + size.ToString());
}
void PacketCommunicator::SetKernelMinimumBytesToCopy(int size)
{
if (pcap_setmintocopy(_pcapDescriptor, size) != 0)
throw BuildInvalidOperation("Error setting kernel minimum bytes to copy to " + size.ToString());
}
void PacketCommunicator::SetSamplingMethod(SamplingMethod^ method)
{
pcap_samp* pcapSamplingMethod = pcap_setsampling(_pcapDescriptor);
pcapSamplingMethod->method = method->Method;
pcapSamplingMethod->value = method->Value;
}
PacketCommunicatorReceiveResult PacketCommunicator::GetPacket([Out] Packet^% packet)
{
AssertMode(PacketCommunicatorMode::Capture);
......
......@@ -13,6 +13,75 @@
namespace PcapDotNet { namespace Core
{
public ref class SamplingMethod abstract
{
internal:
virtual property int Method
{
int get() = 0;
}
virtual property int Value
{
int get() = 0;
}
};
public ref class SamplingMethodNone : SamplingMethod
{
internal:
virtual property int Method
{
int get() override;
}
virtual property int Value
{
int get() override;
}
};
public ref class SamplingMethodOneEveryN : SamplingMethod
{
public:
SamplingMethodOneEveryN(int n);
internal:
virtual property int Method
{
int get() override;
}
virtual property int Value
{
int get() override;
}
private:
int _n;
};
public ref class SamplingMethodFirstAfterInterval : SamplingMethod
{
public:
SamplingMethodFirstAfterInterval(int intervalInMs);
SamplingMethodFirstAfterInterval(System::TimeSpan interval);
internal:
virtual property int Method
{
int get() override;
}
virtual property int Value
{
int get() override;
}
private:
int _intervalInMs;
};
public ref class PacketCommunicator abstract : System::IDisposable
{
public:
......@@ -66,6 +135,10 @@ namespace PcapDotNet { namespace Core
void SetKernelBufferSize(int size);
void SetKernelMinimumBytesToCopy(int size);
void SetSamplingMethod(SamplingMethod^ method);
delegate void HandlePacket(Packets::Packet^ packet);
PacketCommunicatorReceiveResult GetPacket([System::Runtime::InteropServices::Out] Packets::Packet^% packet);
PacketCommunicatorReceiveResult GetSomePackets([System::Runtime::InteropServices::Out] int% numPacketsGot, int maxPackets, HandlePacket^ callBack);
......
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