Commit b8ad42f6 authored by Brickner_cp's avatar Brickner_cp

--no commit message

--no commit message
parent 96f1212f
...@@ -128,6 +128,16 @@ namespace PcapDotNet.Core.Test ...@@ -128,6 +128,16 @@ namespace PcapDotNet.Core.Test
TestGetPackets(NumPacketsToSend, NumPacketsToSend, 0, PacketCommunicatorReceiveResult.BreakLoop, 0, 0.05, 0.05); TestGetPackets(NumPacketsToSend, NumPacketsToSend, 0, PacketCommunicatorReceiveResult.BreakLoop, 0, 0.05, 0.05);
} }
[TestMethod]
[ExpectedException(typeof(InvalidOperationException))]
public void StatisticsModeErrorTest()
{
using (PacketCommunicator communicator = OpenOfflineDevice())
{
communicator.Mode = PacketCommunicatorMode.Statistics;
}
}
[TestMethod] [TestMethod]
public void SetNonBlockTest() public void SetNonBlockTest()
{ {
...@@ -162,7 +172,7 @@ namespace PcapDotNet.Core.Test ...@@ -162,7 +172,7 @@ namespace PcapDotNet.Core.Test
[TestMethod] [TestMethod]
[ExpectedException(typeof(InvalidOperationException))] [ExpectedException(typeof(InvalidOperationException))]
public void SendPacketTest() public void SendPacketErrorTest()
{ {
using (PacketCommunicator communicator = OpenOfflineDevice()) using (PacketCommunicator communicator = OpenOfflineDevice())
{ {
...@@ -235,12 +245,12 @@ namespace PcapDotNet.Core.Test ...@@ -235,12 +245,12 @@ namespace PcapDotNet.Core.Test
} }
} }
private static PacketCommunicator OpenOfflineDevice() public static PacketCommunicator OpenOfflineDevice()
{ {
return OpenOfflineDevice(10, MoreRandom.BuildRandomPacket(100)); return OpenOfflineDevice(10, MoreRandom.BuildRandomPacket(100));
} }
private static PacketCommunicator OpenOfflineDevice(int numPackets, Packet packet) public static PacketCommunicator OpenOfflineDevice(int numPackets, Packet packet)
{ {
string dumpFilename = Path.GetTempPath() + @"dump.pcap"; string dumpFilename = Path.GetTempPath() + @"dump.pcap";
......
...@@ -59,33 +59,41 @@ namespace PcapDotNet.Core.Test ...@@ -59,33 +59,41 @@ namespace PcapDotNet.Core.Test
#endregion #endregion
[TestMethod] [TestMethod]
public void TransmitQueueTest() public void TransmitQueueToLiveTest()
{ {
TestTransmitQueue(0, 100, 0.5, false); TestTransmitQueueToLive(0, 100, 0.5, false);
TestTransmitQueue(10, 60, 0.5, false); TestTransmitQueueToLive(10, 60, 0.5, false);
TestTransmitQueue(10, 600, 0.5, false); TestTransmitQueueToLive(10, 600, 0.5, false);
TestTransmitQueue(10, 1500, 0.5, false); TestTransmitQueueToLive(10, 1500, 0.5, false);
TestTransmitQueue(10, 60, 0.5, true); TestTransmitQueueToLive(10, 60, 0.5, true);
} }
private static void TestTransmitQueue(int numPacketsToSend, int packetSize, double secondsBetweenTimestamps, bool isSynced) [TestMethod]
[ExpectedException(typeof(InvalidOperationException))]
public void TransmitQueueToOfflineTest()
{ {
const string SourceMac = "11:22:33:44:55:66"; const string SourceMac = "11:22:33:44:55:66";
const string DestinationMac = "77:88:99:AA:BB:CC"; 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))) List<Packet> packetsToSend;
using (PacketSendQueue queue = BuildQueue(out packetsToSend, 100, 100, SourceMac, DestinationMac, 0.5))
{ {
DateTime timestamp = DateTime.Now.AddSeconds(-100); using (PacketCommunicator communicator = OfflinePacketDeviceTests.OpenOfflineDevice())
List<Packet> packetsToSend = new List<Packet>(numPacketsToSend);
for (int i = 0; i != numPacketsToSend; ++i)
{ {
Packet packetToSend = MoreRandom.BuildRandomPacket(timestamp, SourceMac, DestinationMac, packetSize); communicator.SetFilter("ether src " + SourceMac + " and ether dst " + DestinationMac);
queue.Enqueue(packetToSend); communicator.Transmit(queue, false);
packetsToSend.Add(packetToSend);
timestamp = timestamp.AddSeconds(secondsBetweenTimestamps);
} }
}
}
private static void TestTransmitQueueToLive(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";
List<Packet> packetsToSend;
using (PacketSendQueue queue = BuildQueue(out packetsToSend, numPacketsToSend, packetSize, SourceMac, DestinationMac, secondsBetweenTimestamps))
{
using (PacketCommunicator communicator = LivePacketDeviceTests.OpenLiveDevice()) using (PacketCommunicator communicator = LivePacketDeviceTests.OpenLiveDevice())
{ {
communicator.SetFilter("ether src " + SourceMac + " and ether dst " + DestinationMac); communicator.SetFilter("ether src " + SourceMac + " and ether dst " + DestinationMac);
...@@ -125,5 +133,31 @@ namespace PcapDotNet.Core.Test ...@@ -125,5 +133,31 @@ namespace PcapDotNet.Core.Test
} }
} }
} }
private static PacketSendQueue BuildQueue(out List<Packet> packetsToSend, int numPackets, int packetSize, string sourceMac, string destinationMac, double secondsBetweenTimestamps)
{
int rawPacketSize = packetSize + 16; // I don't know why 16
PacketSendQueue queue = new PacketSendQueue((uint)(numPackets * rawPacketSize));
try
{
DateTime timestamp = DateTime.Now.AddSeconds(-100);
packetsToSend = new List<Packet>(numPackets);
for (int i = 0; i != numPackets; ++i)
{
Packet packetToSend = MoreRandom.BuildRandomPacket(timestamp, sourceMac, destinationMac, packetSize);
queue.Enqueue(packetToSend);
packetsToSend.Add(packetToSend);
timestamp = timestamp.AddSeconds(secondsBetweenTimestamps);
}
}
catch (Exception)
{
queue.Dispose();
throw;
}
return queue;
}
} }
} }
\ No newline at end of file
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace PcapDotNet.Core.Test
{
/// <summary>
/// Summary description for PacketTotalStatisticsTests
/// </summary>
[TestClass]
public class PacketTotalStatisticsTests
{
public PacketTotalStatisticsTests()
{
//
// 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 Test()
{
Assert.IsNotNull(new PacketTotalStatistics(1,2,3,4).ToString());
Assert.IsFalse(new PacketTotalStatistics(1,2,3,4).Equals(null));
}
}
}
\ No newline at end of file
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace PcapDotNet.Core.Test
{
/// <summary>
/// Summary description for PcapDataLinkTests
/// </summary>
[TestClass]
public class PcapDataLinkTests
{
public PcapDataLinkTests()
{
//
// 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 ConstructorsTest()
{
PcapDataLink dataLink = new PcapDataLink();
Assert.AreEqual(new PcapDataLink("NULL"), dataLink);
for (int i = 0; i != 1000; ++i)
{
dataLink = new PcapDataLink(i);
string dataLinkName;
try
{
dataLinkName = dataLink.Name;
}
catch (ArgumentException)
{
// Ignore invalid values
continue;
}
Assert.AreEqual(new PcapDataLink(dataLinkName), dataLink);
Assert.IsNotNull(dataLink.Description);
Assert.AreEqual(i, dataLink.Value);
}
}
[TestMethod]
[ExpectedException(typeof(NotSupportedException))]
public void UnsupportedKindErrorTest()
{
PcapDataLink dataLink = new PcapDataLink();
Assert.IsNotNull(dataLink.Kind);
}
[TestMethod]
[ExpectedException(typeof(ArgumentException))]
public void NoDescriptionErrorTest()
{
PcapDataLink dataLink = GetInvalidDataLink();
Assert.IsNotNull(dataLink.Description);
Assert.Fail();
}
[TestMethod]
[ExpectedException(typeof(ArgumentException))]
public void InvalidNameErrorTest()
{
PcapDataLink dataLink = new PcapDataLink("Invalid Name");
Assert.IsNotNull(dataLink);
Assert.Fail();
}
private static PcapDataLink GetInvalidDataLink()
{
for (int i = 0; i != 1000; ++i)
{
PcapDataLink dataLink = new PcapDataLink(i);
string dataLinkName;
try
{
dataLinkName = dataLink.Name;
Assert.IsNotNull(dataLinkName);
}
catch (ArgumentException)
{
return dataLink;
}
}
Assert.Fail();
return new PcapDataLink();
}
}
}
\ No newline at end of file
...@@ -49,6 +49,8 @@ ...@@ -49,6 +49,8 @@
<Compile Include="PacketHandler.cs" /> <Compile Include="PacketHandler.cs" />
<Compile Include="PacketSendQueueTests.cs" /> <Compile Include="PacketSendQueueTests.cs" />
<Compile Include="OfflinePacketDeviceTests.cs" /> <Compile Include="OfflinePacketDeviceTests.cs" />
<Compile Include="PacketTotalStatisticsTests.cs" />
<Compile Include="PcapDataLinkTests.cs" />
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="PcapLibTests.cs" /> <Compile Include="PcapLibTests.cs" />
</ItemGroup> </ItemGroup>
......
...@@ -24,3 +24,8 @@ PacketTotalStatistics^ LivePacketCommunicator::TotalStatistics::get() ...@@ -24,3 +24,8 @@ PacketTotalStatistics^ LivePacketCommunicator::TotalStatistics::get()
: 0); : 0);
return gcnew PacketTotalStatistics(packetsReceived, packetsDroppedByDriver, packetsDroppedByInterface, packetsCaptured); return gcnew PacketTotalStatistics(packetsReceived, packetsDroppedByDriver, packetsDroppedByInterface, packetsCaptured);
} }
void LivePacketCommunicator::Transmit(PacketSendQueue^ sendQueue, bool isSync)
{
sendQueue->Transmit(_pcapDescriptor, isSync);
}
...@@ -12,6 +12,8 @@ namespace PcapDotNet { namespace Core ...@@ -12,6 +12,8 @@ namespace PcapDotNet { namespace Core
PacketTotalStatistics^ get() override; PacketTotalStatistics^ get() override;
} }
virtual void Transmit(PacketSendQueue^ sendQueue, bool isSync) override;
internal: internal:
LivePacketCommunicator(const char* source, int snapshotLength, PacketDeviceOpenFlags flags, int readTimeout, pcap_rmtauth* auth, LivePacketCommunicator(const char* source, int snapshotLength, PacketDeviceOpenFlags flags, int readTimeout, pcap_rmtauth* auth,
SocketAddress^ netmask); SocketAddress^ netmask);
......
...@@ -13,3 +13,7 @@ OfflinePacketCommunicator::OfflinePacketCommunicator(const char* source, int sna ...@@ -13,3 +13,7 @@ OfflinePacketCommunicator::OfflinePacketCommunicator(const char* source, int sna
{ {
} }
void OfflinePacketCommunicator::Transmit(PacketSendQueue^ sendQueue, bool isSync)
{
throw gcnew InvalidOperationException("Can't transmit queue to an offline device");
}
...@@ -13,6 +13,8 @@ namespace PcapDotNet { namespace Core ...@@ -13,6 +13,8 @@ namespace PcapDotNet { namespace Core
PacketTotalStatistics^ get() override; PacketTotalStatistics^ get() override;
} }
virtual void Transmit(PacketSendQueue^ sendQueue, bool isSync) override;
internal: internal:
OfflinePacketCommunicator(const char* source, int snapshotLength, PacketDeviceOpenFlags flags, int readTimeout, pcap_rmtauth* auth); OfflinePacketCommunicator(const char* source, int snapshotLength, PacketDeviceOpenFlags flags, int readTimeout, pcap_rmtauth* auth);
}; };
......
...@@ -239,11 +239,6 @@ void PacketCommunicator::SendPacket(Packet^ packet) ...@@ -239,11 +239,6 @@ void PacketCommunicator::SendPacket(Packet^ packet)
} }
void PacketCommunicator::Transmit(PacketSendQueue^ sendQueue, bool isSync)
{
sendQueue->Transmit(_pcapDescriptor, isSync);
}
BerkeleyPacketFilter^ PacketCommunicator::CreateFilter(String^ filterString) BerkeleyPacketFilter^ PacketCommunicator::CreateFilter(String^ filterString)
{ {
return gcnew BerkeleyPacketFilter(_pcapDescriptor, filterString, _ipV4Netmask); return gcnew BerkeleyPacketFilter(_pcapDescriptor, filterString, _ipV4Netmask);
......
...@@ -76,7 +76,7 @@ namespace PcapDotNet { namespace Core ...@@ -76,7 +76,7 @@ namespace PcapDotNet { namespace Core
void Break(); void Break();
void SendPacket(Packets::Packet^ packet); void SendPacket(Packets::Packet^ packet);
void Transmit(PacketSendQueue^ sendQueue, bool isSync); virtual void Transmit(PacketSendQueue^ sendQueue, bool isSync) = 0;
BerkeleyPacketFilter^ CreateFilter(System::String^ filterString); BerkeleyPacketFilter^ CreateFilter(System::String^ filterString);
void SetFilter(BerkeleyPacketFilter^ filter); void SetFilter(BerkeleyPacketFilter^ filter);
......
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