Commit e02fd8df authored by Brickner_cp's avatar Brickner_cp

--no commit message

--no commit message
parent f4eff1b4
...@@ -242,6 +242,110 @@ namespace PcapDotNet.Core.Test ...@@ -242,6 +242,110 @@ namespace PcapDotNet.Core.Test
} }
} }
[TestMethod]
[ExpectedException(typeof(InvalidOperationException))]
public void SetBigKernelBufferSizeErrorTest()
{
using (PacketCommunicator communicator = OpenLiveDevice())
{
communicator.SetKernelBufferSize(1024 * 1024 * 1024);
}
}
[TestMethod]
[ExpectedException(typeof(InvalidOperationException))]
public void SetSmallKernelBufferSizeGetPacketErrorTest()
{
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.SetKernelBufferSize(50);
Packet packet = MoreRandom.BuildRandomPacket(SourceMac, DestinationMac, 100);
communicator.SendPacket(packet);
communicator.GetPacket(out packet);
}
}
[TestMethod]
[ExpectedException(typeof(InvalidOperationException))]
public void SetSmallKernelBufferSizeGetSomePacketsErrorTest()
{
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.SetKernelBufferSize(50);
Packet packet = MoreRandom.BuildRandomPacket(SourceMac, DestinationMac, 100);
communicator.SendPacket(packet);
int numPacketsGot;
communicator.GetSomePackets(out numPacketsGot, 1, delegate { });
}
}
[TestMethod]
[ExpectedException(typeof(InvalidOperationException))]
public void SetSmallKernelBufferSizeGetPacketsErrorTest()
{
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.SetKernelBufferSize(50);
Packet packet = MoreRandom.BuildRandomPacket(SourceMac, DestinationMac, 100);
communicator.SendPacket(packet);
Exception exception = null;
Thread thread = new Thread(delegate()
{
try
{
communicator.GetPackets(1, delegate { });
}
catch (Exception e)
{
exception = e;
}
});
thread.Start();
if (!thread.Join(TimeSpan.FromSeconds(5)))
thread.Abort();
if (exception != null)
throw exception;
}
}
[TestMethod]
[ExpectedException(typeof(InvalidOperationException))]
public void SetSmallKernelBufferSizeGetNextStatisticsErrorTest()
{
using (PacketCommunicator communicator = OpenLiveDevice())
{
communicator.Mode = PacketCommunicatorMode.Statistics;
communicator.SetKernelBufferSize(50);
PacketSampleStatistics statistics;
communicator.GetNextStatistics(out statistics);
}
}
[TestMethod]
[ExpectedException(typeof(InvalidOperationException))]
public void SetSmallKernelBufferSizeGetStatisticsErrorTest()
{
using (PacketCommunicator communicator = OpenLiveDevice())
{
communicator.Mode = PacketCommunicatorMode.Statistics;
communicator.SetKernelBufferSize(50);
communicator.GetStatistics(1, delegate { });
}
}
private static void TestGetStatistics(string sourceMac, string destinationMac, int numPacketsToSend, int numStatisticsToGather, int numStatisticsToBreakLoop, double secondsToWait, int packetSize, 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) PacketCommunicatorReceiveResult expectedResult, int expectedNumStatistics, int expectedNumPackets, double expectedMinSeconds, double expectedMaxSeconds)
{ {
...@@ -329,9 +433,9 @@ namespace PcapDotNet.Core.Test ...@@ -329,9 +433,9 @@ namespace PcapDotNet.Core.Test
} }
} }
private void TestGetPackets(int numPacketsToSend, int numPacketsToWait, int numPacketsToBreakLoop, double secondsToWait, int packetSize, private static void TestGetPackets(int numPacketsToSend, int numPacketsToWait, int numPacketsToBreakLoop, double secondsToWait, int packetSize,
PacketCommunicatorReceiveResult expectedResult, int expectedNumPackets, PacketCommunicatorReceiveResult expectedResult, int expectedNumPackets,
double expectedMinSeconds, double expectedMaxSeconds) double expectedMinSeconds, double expectedMaxSeconds)
{ {
string TestDescription = "NumPacketsToSend=" + numPacketsToSend + ". NumPacketsToWait=" + numPacketsToWait + string TestDescription = "NumPacketsToSend=" + numPacketsToSend + ". NumPacketsToWait=" + numPacketsToWait +
". NumPacketsToBreakLoop=" + numPacketsToBreakLoop + ". SecondsToWait=" + ". NumPacketsToBreakLoop=" + numPacketsToBreakLoop + ". SecondsToWait=" +
...@@ -387,7 +491,9 @@ namespace PcapDotNet.Core.Test ...@@ -387,7 +491,9 @@ namespace PcapDotNet.Core.Test
PacketCommunicator communicator = device.Open(); PacketCommunicator communicator = device.Open();
try try
{ {
communicator.SetKernelBufferSize(2 * 1024 * 1024); // 2 MB instead of 1
Assert.AreEqual(DataLinkKind.Ethernet, communicator.DataLink.Kind); Assert.AreEqual(DataLinkKind.Ethernet, communicator.DataLink.Kind);
communicator.DataLink = communicator.DataLink;
Assert.AreEqual("EN10MB (Ethernet)", communicator.DataLink.ToString()); Assert.AreEqual("EN10MB (Ethernet)", communicator.DataLink.ToString());
Assert.AreEqual(communicator.DataLink, new PcapDataLink(communicator.DataLink.Name)); Assert.AreEqual(communicator.DataLink, new PcapDataLink(communicator.DataLink.Name));
Assert.IsTrue(communicator.IsFileSystemByteOrder); Assert.IsTrue(communicator.IsFileSystemByteOrder);
......
...@@ -180,6 +180,16 @@ namespace PcapDotNet.Core.Test ...@@ -180,6 +180,16 @@ namespace PcapDotNet.Core.Test
} }
} }
[TestMethod]
[ExpectedException(typeof(InvalidOperationException))]
public void SetKernelBufferSizeErrorTest()
{
using (PacketCommunicator communicator = OpenOfflineDevice())
{
communicator.SetKernelBufferSize(1024 * 1024);
}
}
private static void TestGetSomePackets(int numPacketsToSend, int numPacketsToGet, int numPacketsToBreakLoop, private static void TestGetSomePackets(int numPacketsToSend, int numPacketsToGet, int numPacketsToBreakLoop,
PacketCommunicatorReceiveResult expectedResult, int expectedNumPackets, PacketCommunicatorReceiveResult expectedResult, int expectedNumPackets,
double expectedMinSeconds, double expectedMaxSeconds) double expectedMinSeconds, double expectedMaxSeconds)
......
...@@ -48,7 +48,7 @@ void PacketCommunicator::DataLink::set(PcapDataLink value) ...@@ -48,7 +48,7 @@ void PacketCommunicator::DataLink::set(PcapDataLink value)
ReadOnlyCollection<PcapDataLink>^ PacketCommunicator::SupportedDataLinks::get() ReadOnlyCollection<PcapDataLink>^ PacketCommunicator::SupportedDataLinks::get()
{ {
throw gcnew NotSupportedException("Supported DataLinks is unsupported to avoid winpcap memory leak"); throw gcnew NotSupportedException("Supported DataLinks is unsupported to avoid winpcap memory leak");
/*
int* dataLinks; int* dataLinks;
int numDatalinks = pcap_list_datalinks(_pcapDescriptor, &dataLinks); int numDatalinks = pcap_list_datalinks(_pcapDescriptor, &dataLinks);
if (numDatalinks == -1) if (numDatalinks == -1)
...@@ -67,6 +67,7 @@ ReadOnlyCollection<PcapDataLink>^ PacketCommunicator::SupportedDataLinks::get() ...@@ -67,6 +67,7 @@ ReadOnlyCollection<PcapDataLink>^ PacketCommunicator::SupportedDataLinks::get()
// todo look for pcap_free_datalinks() // todo look for pcap_free_datalinks()
// free(dataLinks); // free(dataLinks);
} }
*/
} }
int PacketCommunicator::SnapshotLength::get() int PacketCommunicator::SnapshotLength::get()
...@@ -106,7 +107,7 @@ bool PacketCommunicator::NonBlocking::get() ...@@ -106,7 +107,7 @@ bool PacketCommunicator::NonBlocking::get()
char errbuf[PCAP_ERRBUF_SIZE]; char errbuf[PCAP_ERRBUF_SIZE];
int nonBlockValue = pcap_getnonblock(_pcapDescriptor, errbuf); int nonBlockValue = pcap_getnonblock(_pcapDescriptor, errbuf);
if (nonBlockValue == -1) if (nonBlockValue == -1)
throw gcnew InvalidOperationException("Error getting NonBlocking value"); throw BuildInvalidOperation("Error getting NonBlocking value");
return nonBlockValue != 0; return nonBlockValue != 0;
} }
...@@ -114,7 +115,13 @@ void PacketCommunicator::NonBlocking::set(bool value) ...@@ -114,7 +115,13 @@ void PacketCommunicator::NonBlocking::set(bool value)
{ {
char errbuf[PCAP_ERRBUF_SIZE]; char errbuf[PCAP_ERRBUF_SIZE];
if (pcap_setnonblock(_pcapDescriptor, value, errbuf) != 0) if (pcap_setnonblock(_pcapDescriptor, value, errbuf) != 0)
throw gcnew InvalidOperationException("Error setting NonBlocking to " + value.ToString()); throw BuildInvalidOperation("Error setting NonBlocking to " + value.ToString());
}
void PacketCommunicator::SetKernelBufferSize(int size)
{
if (pcap_setbuff(_pcapDescriptor, size) != 0)
throw BuildInvalidOperation("Error setting kernel buffer size to " + size.ToString());
} }
PacketCommunicatorReceiveResult PacketCommunicator::GetPacket([Out] Packet^% packet) PacketCommunicatorReceiveResult PacketCommunicator::GetPacket([Out] Packet^% packet)
......
...@@ -64,6 +64,8 @@ namespace PcapDotNet { namespace Core ...@@ -64,6 +64,8 @@ namespace PcapDotNet { namespace Core
void set(bool value); void set(bool value);
} }
void SetKernelBufferSize(int size);
delegate void HandlePacket(Packets::Packet^ packet); delegate void HandlePacket(Packets::Packet^ packet);
PacketCommunicatorReceiveResult GetPacket([System::Runtime::InteropServices::Out] Packets::Packet^% packet); PacketCommunicatorReceiveResult GetPacket([System::Runtime::InteropServices::Out] Packets::Packet^% packet);
PacketCommunicatorReceiveResult GetSomePackets([System::Runtime::InteropServices::Out] int% numPacketsGot, int maxPackets, HandlePacket^ callBack); 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