-
Notifications
You must be signed in to change notification settings - Fork 273
/
Copy pathSendQueueTest.cs
334 lines (309 loc) · 11.1 KB
/
SendQueueTest.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
// Copyright 2020 Ayoub Kaanich <[email protected]>
// Copyright 2020-2021 Chris Morgan <[email protected]>
// SPDX-License-Identifier: MIT
using NUnit.Framework;
using PacketDotNet;
using SharpPcap;
using SharpPcap.LibPcap;
using System;
using System.Collections.Generic;
using System.Linq;
using static Test.TestHelper;
using static System.TimeSpan;
using System.Threading;
using System.Threading.Tasks;
namespace Test
{
[TestFixture]
[NonParallelizable]
[Category("SendPacket")]
public class SendQueueTest
{
private const string Filter = "ether proto 0x1234";
private const int PacketCount = 500;
// Windows is usually able to simulate inter packet gaps down to 20µs
// We test with 100µs to avoid flaky tests
internal static readonly decimal DeltaTime = 100E-6M;
/// <summary>
/// Transmit with normal works correctly
/// </summary>
///
// This test gets affected by host performance, so retry it up to 3 times
[Retry(3)]
[Test]
public void TestTransmitNormalSyncFalse()
{
if (SendQueue.IsHardwareAccelerated)
{
var received = RunCapture(Filter, (device) =>
{
GetSendQueue().Transmit(device, false);
});
AssertGoodTransmitNormal(received);
}
else
{
Assert.Ignore("Skipping test as no hardware acceleration is present");
}
}
/// <summary>
/// Transmit with Normal works as expected
/// </summary>
// This test gets affected by host performance, so retry it up to 3 times
[Retry(3)]
[Test]
public void TestTransmitNormal()
{
if (SendQueue.IsHardwareAccelerated)
{
var received = RunCapture(Filter, (device) =>
{
GetSendQueue().Transmit(device, SendQueueTransmitModes.Normal);
});
AssertGoodTransmitNormal(received);
}
else
{
Assert.Ignore("Skipping test as no hardware acceleration is present");
}
}
/// <summary>
/// Transmit with an empty queue returns zero
/// </summary>
[Test]
public void TestTransmitEmpty()
{
using var device = GetPcapDevice();
device.Open();
var queue = new SendQueueWrapper(1024);
Assert.That(queue.Transmit(device, SendQueueTransmitModes.Normal), Is.EqualTo(0));
}
[Test]
public void TestAddByteArray()
{
var queue = new SendQueueWrapper(1024);
var bytes = new Byte[] { 0x1, 0x2, 0x3 };
queue.Add(bytes);
Assert.That(queue.CurrentLength, Is.EqualTo(PcapHeader.MemorySize + bytes.Length));
}
[Test]
public void TestAddPacket()
{
var queue = new SendQueueWrapper(1024);
var packet = EthernetPacket.RandomPacket();
queue.Add(packet);
Assert.That(queue.CurrentLength, Is.EqualTo(PcapHeader.MemorySize + packet.TotalPacketLength));
}
[Test]
public void TestAddRawCapture()
{
var queue = new SendQueueWrapper(1024);
var bytes = new Byte[] { 0x1, 0x2, 0x3 };
var rawCapture = new RawCapture(LinkLayers.Ethernet, new PosixTimeval(10, 20), bytes);
queue.Add(rawCapture);
Assert.That(queue.CurrentLength, Is.EqualTo(PcapHeader.MemorySize + rawCapture.PacketLength));
}
// This test gets affected by host performance, so retry it up to 3 times
[Retry(3)]
[Test]
public void TestNativeTransmitNormal()
{
if (SendQueue.IsHardwareAccelerated)
{
var received = RunCapture(Filter, (device) =>
{
GetSendQueue().NativeTransmit(device, SendQueueTransmitModes.Normal);
});
AssertGoodTransmitNormal(received);
}
else
{
Assert.Ignore("Skipping test as no hardware acceleration is present");
}
}
// This test gets affected by host performance, so retry it up to 3 times
[Retry(3)]
[Test]
public void TestNativeTransmitSync()
{
if (SendQueue.IsHardwareAccelerated)
{
var received = RunCapture(Filter, (device) =>
{
GetSendQueue().NativeTransmit(device, SendQueueTransmitModes.Synchronized);
});
AssertGoodTransmitSync(received);
}
else
{
Assert.Ignore("Skipping test as no hardware acceleration is present");
}
}
// This test gets affected by host performance, so retry it up to 3 times
[Retry(3)]
[Test]
public void TestManagedTransmitNormal()
{
var received = RunCapture(Filter, (device) =>
{
GetSendQueue().ManagedTransmit(device, SendQueueTransmitModes.Normal);
});
AssertGoodTransmitNormal(received);
}
// This test gets affected by host performance, so retry it up to 3 times
[Retry(3)]
[Test]
public void TestManagedTransmitSync()
{
var received = RunCapture(Filter, (device) =>
{
GetSendQueue().ManagedTransmit(device, SendQueueTransmitModes.Synchronized);
});
AssertGoodTransmitSync(received);
}
private static decimal[] GetDeltaTimes(List<RawCapture> received)
{
// Skip the first two, those get penalized due to JIT
var times = received.Skip(2)
.Select(r => r.Timeval.Value).ToArray();
var times1 = times.Skip(1);
var times2 = times.Take(times.Length - 1);
return times1.Zip(times2, (t1, t2) => t1 - t2).ToArray();
}
private static void AssertGoodTransmitNormal(List<RawCapture> received)
{
Assert.That(received, Has.Count.EqualTo(PacketCount));
var deltas = GetDeltaTimes(received);
// Windows usually can not put a delta smaller than 20µs
// We tolorate up to 100µs to avoid flaky tests
// Ensure 90% of packets have delta < 100µs
Assert.That(Percentile(deltas, 0.9M), Is.LessThan(100e-6M));
}
private static void AssertGoodTransmitSync(List<RawCapture> received)
{
Assert.That(received, Has.Count.EqualTo(PacketCount));
var deltas = GetDeltaTimes(received);
// Ensure avg of packets delta = DeltaTime +/- 10%
Assert.That(deltas.Average(), Is.GreaterThan(DeltaTime * 0.9M));
Assert.That(deltas.Average(), Is.LessThan(DeltaTime * 1.1M));
}
/// <summary>
/// Statistical percentile
/// From https://stackoverflow.com/a/8137455
/// </summary>
/// <param name="sequence"></param>
/// <param name="percentile"></param>
/// <returns></returns>
private static decimal Percentile(decimal[] sequence, decimal percentile)
{
Array.Sort(sequence);
var N = sequence.Length;
var n = (N - 1) * percentile + 1;
// Another method: double n = (N + 1) * excelPercentile;
if (n == 1)
{
return sequence[0];
}
else if (n == N)
{
return sequence[N - 1];
}
else
{
var k = (int)n;
var d = n - k;
return sequence[k - 1] + d * (sequence[k] - sequence[k - 1]);
}
}
[Test]
public void TestReturnValue()
{
if (SendQueue.IsHardwareAccelerated)
{
using var device = GetPcapDevice();
device.Open();
var queue = GetSendQueue();
var managed = queue.ManagedTransmit(device, SendQueueTransmitModes.Normal);
var native = queue.NativeTransmit(device, SendQueueTransmitModes.Normal);
Assert.That(native, Is.EqualTo(managed));
}
else
{
Assert.Ignore("Skipping test as no hardware acceleration is present");
}
}
[Test]
public void TestIsHardwareAccelerated()
{
Assert.That(
Environment.OSVersion.Platform == PlatformID.Win32NT
, Is.EqualTo(SendQueue.IsHardwareAccelerated));
}
/// <summary>
/// Helper method
/// </summary>
/// <returns></returns>
internal static SendQueueWrapper GetSendQueue(ushort ethertype = 0x1234)
{
var queue = new SendQueueWrapper((14 + PcapHeader.MemorySize) * PacketCount);
var packet = EthernetPacket.RandomPacket();
packet.Type = (EthernetType)ethertype;
var time = new PosixTimeval();
for (var i = 0; i < PacketCount; i++)
{
time.Value = 123456 + i * DeltaTime;
Assert.That(queue.Add(packet.Bytes, (int)time.Seconds, (int)time.MicroSeconds), Is.True);
}
return queue;
}
internal class SendQueueWrapper : SendQueue
{
public SendQueueWrapper(int memSize) : base(memSize)
{
}
internal new int NativeTransmit(PcapDevice device, SendQueueTransmitModes transmitMode)
{
return base.NativeTransmit(device, transmitMode);
}
internal int ManagedTransmit(PcapDevice device, SendQueueTransmitModes transmitMode)
{
return base.ManagedTransmit(device, transmitMode, CancellationToken.None);
}
}
[SetUp]
public void SetUp()
{
TestHelper.ConfirmIdleState();
}
[TearDown]
public void Cleanup()
{
TestHelper.ConfirmIdleState();
}
[TestCase(true)]
[TestCase(false)]
[Retry(3)]
public void TestCancelSending(bool synchronized)
{
var queue = new SendQueue(1024*1024*64);
int packages = 0;
while (queue.Add(EthernetPacket.RandomPacket()))
{
packages++;
}
var canceller = new CancellationTokenSource();
int packagesSent = 0;
using (var testInterface = TestHelper.GetPcapDevice())
{
testInterface.Open();
var snd = new Task(() => { packagesSent = queue.Transmit(testInterface, synchronized, canceller.Token); } );
snd.Start();
Thread.Sleep(100);
canceller.Cancel();
snd.Wait();
}
Assert.That(packagesSent < packages);
}
}
}