Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in / Register
Toggle navigation
P
Pcap-Net
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Administrator
Pcap-Net
Commits
3840c47a
Commit
3840c47a
authored
Jul 03, 2009
by
Brickner_cp
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
--no commit message
--no commit message
parent
ab01160e
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
177 additions
and
44 deletions
+177
-44
LivePacketDeviceTests.cs
PcapDotNet/src/PcapDotNet.Core.Test/LivePacketDeviceTests.cs
+10
-43
MoreAssert.cs
PcapDotNet/src/PcapDotNet.Core.Test/MoreAssert.cs
+1
-1
MoreRandom.cs
PcapDotNet/src/PcapDotNet.Core.Test/MoreRandom.cs
+35
-0
PacketSendQueueTests.cs
PcapDotNet/src/PcapDotNet.Core.Test/PacketSendQueueTests.cs
+129
-0
PcapDotNet.Core.Test.csproj
...tNet/src/PcapDotNet.Core.Test/PcapDotNet.Core.Test.csproj
+2
-0
No files found.
PcapDotNet/src/PcapDotNet.Core.Test/LivePacketDeviceTests.cs
View file @
3840c47a
...
...
@@ -80,11 +80,7 @@ namespace PcapDotNet.Core.Test
Assert
.
AreEqual
<
uint
>(
0
,
communicator
.
TotalStatistics
.
PacketsCaptured
);
MoreAssert
.
IsInRange
(
TimeSpan
.
FromSeconds
(
1
),
TimeSpan
.
FromSeconds
(
1.02
),
finishedWaiting
-
startWaiting
);
Packet
sentPacket
=
PacketBuilder
.
Ethernet
(
DateTime
.
Now
,
new
MacAddress
(
SourceMac
),
new
MacAddress
(
DestinationMac
),
EthernetType
.
IpV4
,
GetRandomDatagram
(
10
));
Packet
sentPacket
=
MoreRandom
.
BuildRandomPacket
(
SourceMac
,
DestinationMac
,
24
);
DateTime
startSendingTime
=
DateTime
.
Now
;
...
...
@@ -159,11 +155,7 @@ namespace PcapDotNet.Core.Test
string
dumpFilename
=
Path
.
GetTempPath
()
+
@"dump.pcap"
;
const
int
NumPackets
=
10
;
Packet
expectedPacket
=
PacketBuilder
.
Ethernet
(
DateTime
.
Now
,
new
MacAddress
(
SourceMac
),
new
MacAddress
(
DestinationMac
),
EthernetType
.
IpV4
,
GetRandomDatagram
(
10
));
Packet
expectedPacket
=
MoreRandom
.
BuildRandomPacket
(
SourceMac
,
DestinationMac
,
24
);
using
(
PacketCommunicator
communicator
=
OpenLiveDevice
())
{
...
...
@@ -211,11 +203,7 @@ namespace PcapDotNet.Core.Test
communicator
.
Mode
=
PacketCommunicatorMode
.
Statistics
;
communicator
.
SetFilter
(
"ether src "
+
SourceMac
+
" and ether dst "
+
DestinationMac
);
Packet
sentPacket
=
PacketBuilder
.
Ethernet
(
DateTime
.
Now
,
new
MacAddress
(
SourceMac
),
new
MacAddress
(
DestinationMac
),
EthernetType
.
IpV4
,
GetRandomDatagram
(
PacketSize
-
EthernetDatagram
.
HeaderLength
));
Packet
sentPacket
=
MoreRandom
.
BuildRandomPacket
(
SourceMac
,
DestinationMac
,
PacketSize
);
PacketSampleStatistics
statistics
;
PacketCommunicatorReceiveResult
result
=
communicator
.
GetNextStatistics
(
out
statistics
);
...
...
@@ -277,11 +265,7 @@ namespace PcapDotNet.Core.Test
communicator
.
SetFilter
(
"ether src "
+
sourceMac
+
" and ether dst "
+
destinationMac
);
Packet
sentPacket
=
PacketBuilder
.
Ethernet
(
DateTime
.
Now
,
new
MacAddress
(
sourceMac
),
new
MacAddress
(
destinationMac
),
EthernetType
.
IpV4
,
GetRandomDatagram
(
packetSize
-
EthernetDatagram
.
HeaderLength
));
Packet
sentPacket
=
MoreRandom
.
BuildRandomPacket
(
sourceMac
,
destinationMac
,
packetSize
);
PacketCommunicatorReceiveResult
result
=
PacketCommunicatorReceiveResult
.
None
;
int
numStatisticsGot
=
0
;
...
...
@@ -320,19 +304,15 @@ namespace PcapDotNet.Core.Test
}
}
private
void
TestGetSomePackets
(
string
sourceMac
,
string
destinationMac
,
int
numPacketsToSend
,
int
numPacketsToBreakLoop
,
int
packetSize
,
bool
nonBlocking
,
PacketCommunicatorReceiveResult
expectedResult
,
int
expectedNumPackets
,
double
expectedMinSeconds
,
double
expectedMaxSeconds
)
private
static
void
TestGetSomePackets
(
string
sourceMac
,
string
destinationMac
,
int
numPacketsToSend
,
int
numPacketsToBreakLoop
,
int
packetSize
,
bool
nonBlocking
,
PacketCommunicatorReceiveResult
expectedResult
,
int
expectedNumPackets
,
double
expectedMinSeconds
,
double
expectedMaxSeconds
)
{
Packet
packetToSend
=
PacketBuilder
.
Ethernet
(
DateTime
.
Now
,
new
MacAddress
(
sourceMac
),
new
MacAddress
(
destinationMac
),
EthernetType
.
IpV4
,
GetRandomDatagram
(
packetSize
-
EthernetDatagram
.
HeaderLength
));
Packet
packetToSend
=
MoreRandom
.
BuildRandomPacket
(
sourceMac
,
destinationMac
,
packetSize
);
using
(
PacketCommunicator
communicator
=
OpenLiveDevice
())
{
communicator
.
NonBlocking
=
nonBlocking
;
Assert
.
AreEqual
(
nonBlocking
,
communicator
.
NonBlocking
);
communicator
.
SetFilter
(
"ether src "
+
sourceMac
+
" and ether dst "
+
destinationMac
);
int
numPacketsGot
;
...
...
@@ -369,11 +349,7 @@ namespace PcapDotNet.Core.Test
{
communicator
.
SetFilter
(
"ether src "
+
sourceMac
+
" and ether dst "
+
destinationMac
);
Packet
sentPacket
=
PacketBuilder
.
Ethernet
(
DateTime
.
Now
,
new
MacAddress
(
sourceMac
),
new
MacAddress
(
destinationMac
),
EthernetType
.
IpV4
,
GetRandomDatagram
(
packetSize
-
EthernetDatagram
.
HeaderLength
));
Packet
sentPacket
=
MoreRandom
.
BuildRandomPacket
(
sourceMac
,
destinationMac
,
packetSize
);
PacketCommunicatorReceiveResult
result
=
PacketCommunicatorReceiveResult
.
None
;
int
numPacketsHandled
=
0
;
...
...
@@ -408,7 +384,7 @@ namespace PcapDotNet.Core.Test
}
}
p
rivate
static
PacketCommunicator
OpenLiveDevice
()
p
ublic
static
PacketCommunicator
OpenLiveDevice
()
{
IList
<
LivePacketDevice
>
devices
=
LivePacketDevice
.
AllLocalMachine
;
MoreAssert
.
IsBiggerOrEqual
(
1
,
devices
.
Count
);
...
...
@@ -437,14 +413,5 @@ namespace PcapDotNet.Core.Test
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
PcapDotNet/src/PcapDotNet.Core.Test/MoreAssert.cs
View file @
3840c47a
...
...
@@ -3,7 +3,7 @@ using Microsoft.VisualStudio.TestTools.UnitTesting;
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
>
{
...
...
PcapDotNet/src/PcapDotNet.Core.Test/MoreRandom.cs
0 → 100644
View file @
3840c47a
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
PcapDotNet/src/PcapDotNet.Core.Test/PacketSendQueueTests.cs
0 → 100644
View file @
3840c47a
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
PcapDotNet/src/PcapDotNet.Core.Test/PcapDotNet.Core.Test.csproj
View file @
3840c47a
...
...
@@ -44,6 +44,8 @@
<ItemGroup>
<Compile Include="MoreAssert.cs" />
<Compile Include="LivePacketDeviceTests.cs" />
<Compile Include="MoreRandom.cs" />
<Compile Include="PacketSendQueueTests.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="PcapLibTests.cs" />
</ItemGroup>
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment