Ensure all protections are split properly

This commit is contained in:
Matt Nadareski
2024-12-03 03:09:52 -05:00
parent c2547295bf
commit ec091ada95
3 changed files with 53 additions and 44 deletions

View File

@@ -3,7 +3,7 @@ using Xunit;
namespace BinaryObjectScanner.Test
{
public class ScannerTests
public class ProtectionDictionaryTests
{
#region ProcessProtectionString
@@ -11,7 +11,7 @@ namespace BinaryObjectScanner.Test
public void ProcessProtectionString_Null_Empty()
{
string? protection = null;
List<string> actual = Scanner.ProcessProtectionString(protection);
List<string> actual = ProtectionDictionary.ProcessProtectionString(protection);
Assert.Empty(actual);
}
@@ -19,7 +19,7 @@ namespace BinaryObjectScanner.Test
public void ProcessProtectionString_Empty_Empty()
{
string? protection = string.Empty;
List<string> actual = Scanner.ProcessProtectionString(protection);
List<string> actual = ProtectionDictionary.ProcessProtectionString(protection);
Assert.Empty(actual);
}
@@ -27,7 +27,7 @@ namespace BinaryObjectScanner.Test
public void ProcessProtectionString_NoIndicator_Single()
{
string? protection = "item1";
List<string> actual = Scanner.ProcessProtectionString(protection);
List<string> actual = ProtectionDictionary.ProcessProtectionString(protection);
Assert.Single(actual);
}
@@ -35,7 +35,7 @@ namespace BinaryObjectScanner.Test
public void ProcessProtectionString_Indicator_Multiple()
{
string? protection = "item1;item2";
List<string> actual = Scanner.ProcessProtectionString(protection);
List<string> actual = ProtectionDictionary.ProcessProtectionString(protection);
Assert.Equal(2, actual.Count);
}

View File

@@ -21,13 +21,16 @@ namespace BinaryObjectScanner
/// </summary>
/// <param name="key">Key to add information to</param>
/// <param name="value">String value to add</param>
public void Append(string key, string value)
public void Append(string key, string? value)
{
// If the value is empty, don't add it
if (value == null || value.Trim().Length == 0)
return;
Append(key, [value]);
foreach (string subValue in ProcessProtectionString(value))
{
this[key].Enqueue(subValue);
}
}
/// <summary>
@@ -47,7 +50,10 @@ namespace BinaryObjectScanner
if (value == null || value.Trim().Length == 0)
continue;
this[key].Enqueue(value);
foreach (string subValue in ProcessProtectionString(value))
{
this[key].Enqueue(subValue);
}
}
}
@@ -186,10 +192,43 @@ namespace BinaryObjectScanner
if (value == null || value.Trim().Length == 0)
continue;
this[key].Enqueue(value);
foreach (string subValue in ProcessProtectionString(value))
{
this[key].Enqueue(subValue);
}
}
}
#region Helpers
/// <summary>
/// Process a protection string if it includes multiple protections
/// </summary>
/// <param name="protection">Protection string to process</param>
/// <returns>Set of protections parsed, empty on error</returns>
internal static List<string> ProcessProtectionString(string? protection)
{
// If we have an invalid protection string
if (string.IsNullOrEmpty(protection))
return [];
// Setup the output queue
var protections = new List<string>();
// If we have an indicator of multiple protections
if (protection!.Contains(";"))
{
var splitProtections = protection.Split(';');
protections.AddRange(splitProtections);
}
else
{
protections.Add(protection);
}
return protections;
}
/// <summary>
/// Ensure the collection for the given key exists
/// </summary>
@@ -202,5 +241,7 @@ namespace BinaryObjectScanner
TryAdd(key, new ConcurrentQueue<string>());
#endif
}
#endregion
}
}

View File

@@ -284,7 +284,7 @@ namespace BinaryObjectScanner
else
{
var subProtection = detectable.Detect(stream, fileName, _options.IncludeDebug);
protections.Append(fileName, ProcessProtectionString(subProtection));
protections.Append(fileName, subProtection);
}
}
@@ -395,8 +395,8 @@ namespace BinaryObjectScanner
if (File.Exists(path))
{
var protection = impl.CheckFilePath(path!);
var subProtections = ProcessProtectionString(protection);
protections.AddRange(subProtections);
if (protection != null)
protections.Add(protection);
}
// If we have a directory path
@@ -411,37 +411,5 @@ namespace BinaryObjectScanner
}
#endregion
#region Helpers
/// <summary>
/// Process a protection string if it includes multiple protections
/// </summary>
/// <param name="protection">Protection string to process</param>
/// <returns>Set of protections parsed, empty on error</returns>
internal static List<string> ProcessProtectionString(string? protection)
{
// If we have an invalid protection string
if (string.IsNullOrEmpty(protection))
return [];
// Setup the output queue
var protections = new List<string>();
// If we have an indicator of multiple protections
if (protection!.Contains(";"))
{
var splitProtections = protection.Split(';');
protections.AddRange(splitProtections);
}
else
{
protections.Add(protection);
}
return protections;
}
#endregion
}
}