Skip to content

Commit

Permalink
clean up tests
Browse files Browse the repository at this point in the history
  • Loading branch information
davidmrdavid committed Jul 3, 2024
1 parent e16b912 commit 8ed5d30
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 32 deletions.
4 changes: 2 additions & 2 deletions src/WebJobs.Extensions.DurableTask/EndToEndTraceHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ public static string LocalSlotName

internal void SanitizeString(string? rawPayload, out string iloggerString, out string durableKustoTableString)
{
string payload = rawPayload ?? "(null)";
int numCharacters = rawPayload != null ? payload.Length : 0;
string payload = rawPayload ?? string.Empty;
int numCharacters = payload.Length;
string sanitizedPayload = $"(Redacted {numCharacters} characters)";

// By default, both ilogger and kusto data should use the sanitized data
Expand Down
66 changes: 36 additions & 30 deletions test/FunctionsV2/EndToEndTraceHelperTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@ namespace WebJobs.Extensions.DurableTask.Tests.V2
public class EndToEndTraceHelperTests
{
[Theory]
[InlineData(true, true)]
[InlineData(false, true)]
[InlineData(true, false)]
[InlineData(false, false)]
public void StringSanitizerTest(bool shouldTraceRawData, bool testNullInput)
[InlineData(true, "DO NOT LOG ME")]
[InlineData(false, "DO NOT LOG ME")]
[InlineData(true, null)]
[InlineData(false, null)]
public void StringSanitizerTest(
bool shouldTraceRawData,
string? possiblySensitiveData)
{
// set up trace helper
var nullLogger = new NullLogger<EndToEndTraceHelper>();
Expand All @@ -25,30 +27,23 @@ public void StringSanitizerTest(bool shouldTraceRawData, bool testNullInput)
traceReplayEvents: false, // has not effect on sanitizer
shouldTraceRawData: shouldTraceRawData);

// prepare sensitive data to sanitize
string possibleSensitiveData = "DO NOT LOG ME";

// run sanitizer
traceHelper.SanitizeString(
rawPayload: testNullInput ? null : possibleSensitiveData,
rawPayload: possiblySensitiveData,
out string iLoggerString,
out string kustoTableString);

// expected: sanitized string should not contain the sensitive data
Assert.DoesNotContain(possibleSensitiveData, kustoTableString);
// skip this check if data is null
if (possiblySensitiveData != null)
{
Assert.DoesNotContain(possiblySensitiveData, kustoTableString);
}

if (shouldTraceRawData)
{
if (testNullInput)
{
// If provided input is null, it is logged as "(null)"
Assert.Equal("(null)", iLoggerString);
}
else
{
// Otherwise, we expect to see the data as-is
Assert.Equal(possibleSensitiveData, iLoggerString);
}
string expectedString = possiblySensitiveData ?? string.Empty;
Assert.Equal(expectedString, iLoggerString);
}
else
{
Expand All @@ -60,11 +55,13 @@ public void StringSanitizerTest(bool shouldTraceRawData, bool testNullInput)

Check warning on line 55 in test/FunctionsV2/EndToEndTraceHelperTests.cs

View workflow job for this annotation

GitHub Actions / build

Check warning on line 55 in test/FunctionsV2/EndToEndTraceHelperTests.cs

View workflow job for this annotation

GitHub Actions / build

Check warning on line 55 in test/FunctionsV2/EndToEndTraceHelperTests.cs

View workflow job for this annotation

GitHub Actions / build


[Theory]
[InlineData(true, true)]
[InlineData(false, true)]
[InlineData(true, false)]
[InlineData(false, false)]
public void ExceptionSanitizerTest(bool shouldTraceRawData, bool testNullInput)
[InlineData(true, "DO NOT LOG ME")]
[InlineData(false, "DO NOT LOG ME")]
[InlineData(true, null)]
[InlineData(false, null)]
public void ExceptionSanitizerTest(
bool shouldTraceRawData,
string? possiblySensitiveData)
{
// set up trace helper
var nullLogger = new NullLogger<EndToEndTraceHelper>();
Expand All @@ -74,25 +71,34 @@ public void ExceptionSanitizerTest(bool shouldTraceRawData, bool testNullInput)
shouldTraceRawData: shouldTraceRawData);

// exception to sanitize
var possiblySensitiveData = "DO NOT LOG ME";
var exception = new Exception(possiblySensitiveData);
Exception? exception = null;
if (possiblySensitiveData != null)
{
exception = new Exception(possiblySensitiveData);
}

// run sanitizer
traceHelper.SanitizeException(
exception: testNullInput ? null : exception,
exception: exception,
out string iLoggerString,
out string kustoTableString);

// exception message should not be part of the sanitized strings
Assert.DoesNotContain(possiblySensitiveData, kustoTableString);
// skip this check if data is null
if (possiblySensitiveData != null)
{
Assert.DoesNotContain(possiblySensitiveData, kustoTableString);
}

if (shouldTraceRawData)
{
var expectedString = testNullInput ? string.Empty : exception.ToString();
var expectedString = exception?.ToString() ?? string.Empty;
Assert.Equal(expectedString, iLoggerString);
}
else
{
// If raw data is not being traced,
// kusto and the ilogger should get the same data
Assert.Equal(iLoggerString, kustoTableString);
}
}
Expand Down

0 comments on commit 8ed5d30

Please sign in to comment.