-
Notifications
You must be signed in to change notification settings - Fork 5.4k
Expand file tree
/
Copy pathTelemetryTest.cs
More file actions
269 lines (219 loc) · 12.9 KB
/
TelemetryTest.cs
File metadata and controls
269 lines (219 loc) · 12.9 KB
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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics.Tracing;
using System.Linq;
using System.Security.Authentication;
using System.Threading.Tasks;
using Microsoft.DotNet.RemoteExecutor;
using Microsoft.DotNet.XUnitExtensions;
using Xunit;
namespace System.Net.Security.Tests
{
public class TelemetryTest
{
[Fact]
public static void EventSource_ExistsWithCorrectId()
{
Type esType = typeof(SslStream).Assembly.GetType("System.Net.Security.NetSecurityTelemetry", throwOnError: true, ignoreCase: false);
Assert.NotNull(esType);
Assert.Equal("System.Net.Security", EventSource.GetName(esType));
Assert.Equal(Guid.Parse("7beee6b1-e3fa-5ddb-34be-1404ad0e2520"), EventSource.GetGuid(esType));
Assert.NotEmpty(EventSource.GenerateManifest(esType, esType.Assembly.Location));
}
[OuterLoop]
[ConditionalFact(typeof(RemoteExecutor), nameof(RemoteExecutor.IsSupported))]
[SkipOnPlatform(TestPlatforms.iOS | TestPlatforms.tvOS, "X509 certificate store is not supported on iOS or tvOS.")] // Match SslStream_StreamToStream_Authentication_Success
public static void EventSource_SuccessfulHandshake_LogsStartStop()
{
RemoteExecutor.Invoke(async () =>
{
try
{
using var listener = new TestEventListener("System.Net.Security", EventLevel.Verbose, eventCounterInterval: 0.1d);
listener.AddActivityTracking();
var events = new ConcurrentQueue<(EventWrittenEventArgs Event, Guid ActivityId)>();
await listener.RunWithCallbackAsync(e =>
{
events.Enqueue((e, e.ActivityId));
if (e.EventName == "HandshakeStart")
{
// Wait for a new counter group so that current-tls-handshakes is guaranteed a non-zero value
WaitForEventCountersAsync(events).GetAwaiter().GetResult();
}
},
async () =>
{
// Invoke tests that'll cause some events to be generated
var test = new SslStreamStreamToStreamTest_Async();
await test.SslStream_StreamToStream_Authentication_Success();
await WaitForEventCountersAsync(events);
});
Assert.DoesNotContain(events, ev => ev.Event.EventId == 0); // errors from the EventSource itself
(EventWrittenEventArgs Event, Guid ActivityId)[] starts = events.Where(e => e.Event.EventName == "HandshakeStart").ToArray();
Assert.Equal(2, starts.Length);
Assert.All(starts, s => Assert.Equal(2, s.Event.Payload.Count));
Assert.All(starts, s => Assert.NotEqual(Guid.Empty, s.ActivityId));
// isServer
(EventWrittenEventArgs Event, Guid ActivityId) serverStart = Assert.Single(starts, s => (bool)s.Event.Payload[0]);
(EventWrittenEventArgs Event, Guid ActivityId) clientStart = Assert.Single(starts, s => !(bool)s.Event.Payload[0]);
// targetHost
Assert.Empty(Assert.IsType<string>(serverStart.Event.Payload[1]));
Assert.NotEmpty(Assert.IsType<string>(clientStart.Event.Payload[1]));
Assert.NotEqual(serverStart.ActivityId, clientStart.ActivityId);
(EventWrittenEventArgs Event, Guid ActivityId)[] stops = events.Where(e => e.Event.EventName == "HandshakeStop").ToArray();
Assert.Equal(2, stops.Length);
EventWrittenEventArgs serverStop = Assert.Single(stops, s => s.ActivityId == serverStart.ActivityId).Event;
EventWrittenEventArgs clientStop = Assert.Single(stops, s => s.ActivityId == clientStart.ActivityId).Event;
SslProtocols serverProtocol = ValidateHandshakeStopEventPayload(serverStop);
SslProtocols clientProtocol = ValidateHandshakeStopEventPayload(clientStop);
Assert.Equal(serverProtocol, clientProtocol);
Assert.DoesNotContain(events, e => e.Event.EventName == "HandshakeFailed");
VerifyEventCounters(events, shouldHaveFailures: false);
}
catch (SkipTestException)
{
// Don't throw inside RemoteExecutor if SslStream_StreamToStream_Authentication_Success chose to skip the test
}
}).Dispose();
}
[OuterLoop]
[ConditionalFact(typeof(RemoteExecutor), nameof(RemoteExecutor.IsSupported))]
public static void EventSource_UnsuccessfulHandshake_LogsStartFailureStop()
{
RemoteExecutor.Invoke(async () =>
{
using var listener = new TestEventListener("System.Net.Security", EventLevel.Verbose, eventCounterInterval: 0.1d);
listener.AddActivityTracking();
var events = new ConcurrentQueue<(EventWrittenEventArgs Event, Guid ActivityId)>();
await listener.RunWithCallbackAsync(e =>
{
events.Enqueue((e, e.ActivityId));
if (e.EventName == "HandshakeStart")
{
// Wait for a new counter group so that current-tls-handshakes is guaranteed a non-zero value
WaitForEventCountersAsync(events).GetAwaiter().GetResult();
}
},
async () =>
{
// Invoke tests that'll cause some events to be generated
var test = new SslStreamStreamToStreamTest_Async();
await test.SslStream_ServerLocalCertificateSelectionCallbackReturnsNull_Throw();
await WaitForEventCountersAsync(events);
});
Assert.DoesNotContain(events, ev => ev.Event.EventId == 0); // errors from the EventSource itself
(EventWrittenEventArgs Event, Guid ActivityId)[] starts = events.Where(e => e.Event.EventName == "HandshakeStart").ToArray();
Assert.Equal(2, starts.Length);
Assert.All(starts, s => Assert.Equal(2, s.Event.Payload.Count));
Assert.All(starts, s => Assert.NotEqual(Guid.Empty, s.ActivityId));
// isServer
(EventWrittenEventArgs Event, Guid ActivityId) serverStart = Assert.Single(starts, s => (bool)s.Event.Payload[0]);
(EventWrittenEventArgs Event, Guid ActivityId) clientStart = Assert.Single(starts, s => !(bool)s.Event.Payload[0]);
// targetHost
Assert.Empty(Assert.IsType<string>(serverStart.Event.Payload[1]));
Assert.NotEmpty(Assert.IsType<string>(clientStart.Event.Payload[1]));
Assert.NotEqual(serverStart.ActivityId, clientStart.ActivityId);
(EventWrittenEventArgs Event, Guid ActivityId)[] stops = events.Where(e => e.Event.EventName == "HandshakeStop").ToArray();
Assert.Equal(2, stops.Length);
Assert.All(stops, s => ValidateHandshakeStopEventPayload(s.Event, failure: true));
EventWrittenEventArgs serverStop = Assert.Single(stops, s => s.ActivityId == serverStart.ActivityId).Event;
EventWrittenEventArgs clientStop = Assert.Single(stops, s => s.ActivityId == clientStart.ActivityId).Event;
(EventWrittenEventArgs Event, Guid ActivityId)[] failures = events.Where(e => e.Event.EventName == "HandshakeFailed").ToArray();
Assert.Equal(2, failures.Length);
Assert.All(failures, f => Assert.Equal(3, f.Event.Payload.Count));
Assert.All(failures, f => Assert.NotEmpty(f.Event.Payload[2] as string)); // exceptionMessage
EventWrittenEventArgs serverFailure = Assert.Single(failures, f => f.ActivityId == serverStart.ActivityId).Event;
EventWrittenEventArgs clientFailure = Assert.Single(failures, f => f.ActivityId == clientStart.ActivityId).Event;
// isServer
Assert.Equal(true, serverFailure.Payload[0]);
Assert.Equal(false, clientFailure.Payload[0]);
VerifyEventCounters(events, shouldHaveFailures: true);
}).Dispose();
}
private static SslProtocols ValidateHandshakeStopEventPayload(EventWrittenEventArgs stopEvent, bool failure = false)
{
Assert.Equal("HandshakeStop", stopEvent.EventName);
Assert.Equal(1, stopEvent.Payload.Count);
var protocol = (SslProtocols)stopEvent.Payload[0];
Assert.True(Enum.IsDefined(protocol));
if (failure)
{
Assert.Equal(SslProtocols.None, protocol);
}
else
{
Assert.NotEqual(SslProtocols.None, protocol);
}
return protocol;
}
private static void VerifyEventCounters(ConcurrentQueue<(EventWrittenEventArgs Event, Guid ActivityId)> events, bool shouldHaveFailures)
{
Dictionary<string, double[]> eventCounters = events
.Select(e => e.Event)
.Where(e => e.EventName == "EventCounters")
.Select(e => (IDictionary<string, object>)e.Payload.Single())
.GroupBy(d => (string)d["Name"], d => (double)(d.ContainsKey("Mean") ? d["Mean"] : d["Increment"]))
.ToDictionary(p => p.Key, p => p.ToArray());
Assert.True(eventCounters.TryGetValue("total-tls-handshakes", out double[] totalHandshakes));
Assert.Equal(2, totalHandshakes[^1]);
Assert.True(eventCounters.TryGetValue("tls-handshake-rate", out double[] handshakeRate));
Assert.Contains(handshakeRate, r => r > 0);
Assert.True(eventCounters.TryGetValue("failed-tls-handshakes", out double[] failedHandshakes));
if (shouldHaveFailures)
{
Assert.Equal(2, failedHandshakes[^1]);
}
else
{
Assert.All(failedHandshakes, f => Assert.Equal(0, f));
}
Assert.True(eventCounters.TryGetValue("current-tls-handshakes", out double[] currentHandshakes));
Assert.Contains(currentHandshakes, h => h > 0);
Assert.Equal(0, currentHandshakes[^1]);
double[] openedSessions = eventCounters
.Where(pair => pair.Key.EndsWith("-sessions-open"))
.Select(pair => pair.Value[^1])
.ToArray();
// Events should be emitted for all 5 sessions-open counters
Assert.Equal(5, openedSessions.Length);
Assert.All(openedSessions, oc => Assert.Equal(0, oc));
double[] allHandshakeDurations = eventCounters["all-tls-handshake-duration"];
double[][] tlsHandshakeDurations = eventCounters
.Where(pair => pair.Key.StartsWith("tls") && pair.Key.EndsWith("-handshake-duration"))
.Select(pair => pair.Value)
.ToArray();
// Events should be emitted for all 4 tls**-handshake-duration counters
Assert.Equal(4, tlsHandshakeDurations.Length);
if (shouldHaveFailures)
{
Assert.All(tlsHandshakeDurations, durations => Assert.All(durations, d => Assert.Equal(0, d)));
Assert.All(allHandshakeDurations, d => Assert.Equal(0, d));
}
else
{
Assert.Contains(tlsHandshakeDurations, durations => durations.Any(d => d > 0));
Assert.Contains(allHandshakeDurations, d => d > 0);
}
}
private static async Task WaitForEventCountersAsync(ConcurrentQueue<(EventWrittenEventArgs Event, Guid ActivityId)> events)
{
DateTime startTime = DateTime.UtcNow;
int startCount = events.Count;
while (events.Skip(startCount).Count(e => IsTlsHandshakeRateEventCounter(e.Event)) < 3)
{
if (DateTime.UtcNow.Subtract(startTime) > TimeSpan.FromSeconds(30))
throw new TimeoutException($"Timed out waiting for EventCounters");
await Task.Delay(100);
}
static bool IsTlsHandshakeRateEventCounter(EventWrittenEventArgs e)
{
if (e.EventName != "EventCounters")
return false;
var dictionary = (IDictionary<string, object>)e.Payload.Single();
return (string)dictionary["Name"] == "tls-handshake-rate";
}
}
}
}