public void CorrelationIdLookupHelperReturnsAppIdOnSecondCall() { var correlationIdLookupHelper = new CorrelationIdLookupHelper((ikey) => { // Pretend App Id is the same as Ikey var tcs = new TaskCompletionSource <string>(); tcs.SetResult(ikey); return(tcs.Task); }); string instrumenationKey = Guid.NewGuid().ToString(); string cid; // First call returns false; Assert.IsFalse(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.IsTrue(correlationIdLookupHelper.TryGetXComponentCorrelationId(instrumenationKey, out cid)); }
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.IsFalse(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.IsTrue(correlationIdLookupHelper.TryGetXComponentCorrelationId(instrumenationKey, out string cid)); Assert.AreEqual(cidPrefix + value, cid); }
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); }
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); }
public void TryGetXComponentCorrelationIdShouldReturnAppIdWhenHit() { CorrelationIdLookupHelper target = new CorrelationIdLookupHelper((iKey) => { return(Task.FromResult(string.Format(CultureInfo.InvariantCulture, "AppId for {0}", iKey))); }); string actual = null; target.TryGetXComponentCorrelationId(TestInstrumentationKey, out actual); string expected = string.Format(CultureInfo.InvariantCulture, CorrelationIdLookupHelper.CorrelationIdFormat, "AppId for " + TestInstrumentationKey); Assert.Equal(expected, actual); }