This repository was archived by the owner on Jun 10, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 120
Expand file tree
/
Copy pathCorrelationIdLookupHelperTest.cs
More file actions
108 lines (90 loc) · 4.41 KB
/
CorrelationIdLookupHelperTest.cs
File metadata and controls
108 lines (90 loc) · 4.41 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
namespace Microsoft.ApplicationInsights.AspNetCore.Tests
{
using System;
using System.Globalization;
using System.Threading;
using System.Threading.Tasks;
using ApplicationInsights.Extensibility;
using DiagnosticListeners;
using Xunit;
public class CorrelationIdLookupHelperTest
{
private const string TestInstrumentationKey = "11111111-2222-3333-4444-555555555555";
/// <summary>
/// Makes sure that the first call to get app id returns false, because it hasn't been fetched yet.
/// But the second call is able to get it from the dictionary.
/// </summary>
[Fact]
public void CorrelationIdLookupHelperReturnsAppIdOnSecondCall()
{
var correlationIdLookupHelper = new CorrelationIdLookupHelper((ikey) =>
{
// Pretend App Id is the same as Ikey
return Task.FromResult(ikey);
});
string instrumenationKey = Guid.NewGuid().ToString();
string cid;
// First call returns false;
Assert.False(correlationIdLookupHelper.TryGetXComponentCorrelationId(instrumenationKey, out cid));
// Let's wait for the task to complete. It should be really quick (based on the test setup) but not immediate.
while (correlationIdLookupHelper.IsFetchAppInProgress(instrumenationKey))
{
Thread.Sleep(10); // wait 10 ms.
}
// Once fetch is complete, subsequent calls should return correlation id.
Assert.True(correlationIdLookupHelper.TryGetXComponentCorrelationId(instrumenationKey, out cid));
}
/// <summary>
/// Test that if an malicious value is returned, that value will be truncated.
/// </summary>
[Fact]
public void CorrelationIdLookupHelperTruncatesMaliciousValue()
{
// 50 character string.
var value = "a123456789b123546789c123456789d123456798e123456789";
// An arbitrary string that is expected to be truncated.
var malicious = "00000000000000000000000000000000000000000000000000000000000";
var cidPrefix = "cid-v1:";
var correlationIdLookupHelper = new CorrelationIdLookupHelper((ikey) =>
{
return Task.FromResult(value + malicious);
});
string instrumenationKey = Guid.NewGuid().ToString();
// first request fails because this will create the fetch task.
Assert.False(correlationIdLookupHelper.TryGetXComponentCorrelationId(instrumenationKey, out string ignore));
// Let's wait for the task to complete. It should be really quick (based on the test setup) but not immediate.
while (correlationIdLookupHelper.IsFetchAppInProgress(instrumenationKey))
{
Thread.Sleep(10); // wait 10 ms.
}
// Once fetch is complete, subsequent calls should return correlation id.
Assert.True(correlationIdLookupHelper.TryGetXComponentCorrelationId(instrumenationKey, out string cid));
Assert.Equal(cidPrefix + value, cid);
}
[Fact]
public void TryGetXComponentCorrelationIdShouldReturnEmptyWhenIKeyIsNull()
{
TelemetryConfiguration config = new TelemetryConfiguration(TestInstrumentationKey, new FakeTelemetryChannel()
{
EndpointAddress = "https://endpoint"
});
CorrelationIdLookupHelper target = new CorrelationIdLookupHelper(() => config);
string result = "Not null value";
target.TryGetXComponentCorrelationId(null, out result);
Assert.Equal(string.Empty, result);
}
[Fact]
public void TryGetXComponentCorrelationIdShouldReturnEmptyWhenBaseAddressIsNotGiven()
{
// CorrelationIdLookupHelper should fail gracefully when it can't fetch the base address from the channel.
TelemetryConfiguration config = new TelemetryConfiguration(TestInstrumentationKey, new FakeTelemetryChannel()
{
EndpointAddress = string.Empty
});
CorrelationIdLookupHelper target = new CorrelationIdLookupHelper(() => config);
string result = "Not null value";
target.TryGetXComponentCorrelationId(null, out result);
Assert.Equal(string.Empty, result);
}
}
}