Skip to content

Commit 10149a1

Browse files
author
BRUNER Patrick
committed
unit tests fix
1 parent 31b5bfd commit 10149a1

4 files changed

Lines changed: 64 additions & 8 deletions

File tree

src/PluginRegistry.Tests/PluginHashCalculatorTests.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,26 @@ public class PluginHashCalculatorTests
99
{
1010
private string _testDirectory;
1111
private string _testFilePath;
12+
private bool _originalBypassSetting;
1213

1314
[SetUp]
1415
public void SetUp ()
1516
{
1617
_testDirectory = Path.Join(Path.GetTempPath(), "LogExpertPluginHashTests");
1718
_ = Directory.CreateDirectory(_testDirectory);
1819
_testFilePath = Path.Join(_testDirectory, "test-plugin.dll");
20+
21+
// Save original bypass setting and disable it for tests that need actual verification
22+
_originalBypassSetting = PluginHashCalculator.BypassHashVerification;
1923
}
2024

2125
[TearDown]
2226
[System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1031:Do not catch general exception types", Justification = "Unit testcases")]
2327
public void TearDown ()
2428
{
29+
// Restore original bypass setting
30+
PluginHashCalculator.BypassHashVerification = _originalBypassSetting;
31+
2532
try
2633
{
2734
if (Directory.Exists(_testDirectory))
@@ -121,6 +128,7 @@ public void CalculateHash_WhitespacePath_ThrowsArgumentException ()
121128
public void VerifyHash_MatchingHash_ReturnsTrue ()
122129
{
123130
// Arrange
131+
PluginHashCalculator.BypassHashVerification = false;
124132
var testContent = "Test content for hash verification";
125133
File.WriteAllText(_testFilePath, testContent);
126134
var expectedHash = PluginHashCalculator.CalculateHash(_testFilePath);
@@ -136,6 +144,7 @@ public void VerifyHash_MatchingHash_ReturnsTrue ()
136144
public void VerifyHash_MismatchedHash_ReturnsFalse ()
137145
{
138146
// Arrange
147+
PluginHashCalculator.BypassHashVerification = false;
139148
var testContent = "Test content";
140149
File.WriteAllText(_testFilePath, testContent);
141150
var wrongHash = "0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF";
@@ -151,6 +160,7 @@ public void VerifyHash_MismatchedHash_ReturnsFalse ()
151160
public void VerifyHash_CaseInsensitiveUpperCase_ReturnsTrue ()
152161
{
153162
// Arrange
163+
PluginHashCalculator.BypassHashVerification = false;
154164
var testContent = "Test content";
155165
File.WriteAllText(_testFilePath, testContent);
156166
var hash = PluginHashCalculator.CalculateHash(_testFilePath);
@@ -168,6 +178,7 @@ public void VerifyHash_CaseInsensitiveUpperCase_ReturnsTrue ()
168178
public void VerifyHash_CaseInsensitiveLowerCase_ReturnsTrue ()
169179
{
170180
// Arrange
181+
PluginHashCalculator.BypassHashVerification = false;
171182
var testContent = "Test content";
172183
File.WriteAllText(_testFilePath, testContent);
173184
var hash = PluginHashCalculator.CalculateHash(_testFilePath);
@@ -184,6 +195,7 @@ public void VerifyHash_CaseInsensitiveLowerCase_ReturnsTrue ()
184195
public void VerifyHash_ModifiedFile_ReturnsFalse ()
185196
{
186197
// Arrange
198+
PluginHashCalculator.BypassHashVerification = false;
187199
var originalContent = "Original content";
188200
File.WriteAllText(_testFilePath, originalContent);
189201
var originalHash = PluginHashCalculator.CalculateHash(_testFilePath);
@@ -203,6 +215,7 @@ public void VerifyHash_ModifiedFile_ReturnsFalse ()
203215
public void VerifyHash_NullExpectedHash_ThrowsArgumentNullException ()
204216
{
205217
// Arrange
218+
PluginHashCalculator.BypassHashVerification = false;
206219
File.WriteAllText(_testFilePath, "Test content");
207220

208221
// Act & Assert
@@ -213,12 +226,29 @@ public void VerifyHash_NullExpectedHash_ThrowsArgumentNullException ()
213226
public void VerifyHash_EmptyExpectedHash_ThrowsArgumentException ()
214227
{
215228
// Arrange
229+
PluginHashCalculator.BypassHashVerification = false;
216230
File.WriteAllText(_testFilePath, "Test content");
217231

218232
// Act & Assert
219233
_ = Assert.Throws<ArgumentException>(() => PluginHashCalculator.VerifyHash(_testFilePath, string.Empty));
220234
}
221235

236+
[Test]
237+
public void VerifyHash_WithBypassEnabled_AlwaysReturnsTrue ()
238+
{
239+
// Arrange
240+
PluginHashCalculator.BypassHashVerification = true;
241+
var testContent = "Test content";
242+
File.WriteAllText(_testFilePath, testContent);
243+
var wrongHash = "WRONGHASH123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456";
244+
245+
// Act
246+
var result = PluginHashCalculator.VerifyHash(_testFilePath, wrongHash);
247+
248+
// Assert
249+
Assert.That(result, Is.True, "Bypass mode should always return true");
250+
}
251+
222252
[Test]
223253
public void CalculateHashes_MultipleFiles_ReturnsAllHashes ()
224254
{

src/PluginRegistry.Tests/PluginValidatorTests.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ public class PluginValidatorTests
1515
{
1616
private string _testDataPath = null!;
1717
private string _testPluginsPath = null!;
18+
private bool _originalBypassSetting;
1819

1920
[SetUp]
2021
public void SetUp()
@@ -23,11 +24,17 @@ public void SetUp()
2324
_testDataPath = Path.Join(Path.GetTempPath(), "LogExpertValidatorTests", Guid.NewGuid().ToString());
2425
_testPluginsPath = Path.Join(_testDataPath, "plugins");
2526
Directory.CreateDirectory(_testPluginsPath);
27+
28+
// Save original bypass setting
29+
_originalBypassSetting = PluginHashCalculator.BypassHashVerification;
2630
}
2731

2832
[TearDown]
2933
public void TearDown()
3034
{
35+
// Restore original bypass setting
36+
PluginHashCalculator.BypassHashVerification = _originalBypassSetting;
37+
3138
// Clean up test directories
3239
if (Directory.Exists(_testDataPath))
3340
{
@@ -161,6 +168,7 @@ public void CalculateHash_WithEmptyFile_ShouldReturnValidHash()
161168
public void VerifyHash_WithMatchingHash_ShouldReturnTrue()
162169
{
163170
// Arrange
171+
PluginHashCalculator.BypassHashVerification = false;
164172
var pluginPath = Path.Join(_testPluginsPath, "TestPlugin.dll");
165173
CreateDummyDll(pluginPath, content: "Test Content");
166174
var expectedHash = PluginHashCalculator.CalculateHash(pluginPath);
@@ -176,6 +184,7 @@ public void VerifyHash_WithMatchingHash_ShouldReturnTrue()
176184
public void VerifyHash_WithMismatchedHash_ShouldReturnFalse()
177185
{
178186
// Arrange
187+
PluginHashCalculator.BypassHashVerification = false;
179188
var pluginPath = Path.Join(_testPluginsPath, "TestPlugin.dll");
180189
CreateDummyDll(pluginPath, content: "Test Content");
181190
var wrongHash = "0000000000000000000000000000000000000000000000000000000000000000";

src/PluginRegistry/PluginHashCalculator.cs

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,17 @@ namespace LogExpert.PluginRegistry;
88
/// </summary>
99
public static class PluginHashCalculator
1010
{
11+
/// <summary>
12+
/// When true, hash verification is bypassed (useful for development and testing).
13+
/// Default is true in DEBUG builds, false in RELEASE builds.
14+
/// </summary>
15+
public static bool BypassHashVerification { get; set; } =
16+
#if DEBUG
17+
true;
18+
#else
19+
false;
20+
#endif
21+
1122
/// <summary>
1223
/// Calculates the SHA256 hash of a plugin DLL file.
1324
/// </summary>
@@ -47,20 +58,25 @@ public static string CalculateHash (string filePath)
4758
/// <returns>True if the file's hash matches the expected hash, false otherwise.</returns>
4859
/// <exception cref="ArgumentNullException">Thrown when filePath or expectedHash is null or empty.</exception>
4960
/// <exception cref="FileNotFoundException">Thrown when the file does not exist.</exception>
61+
/// <remarks>
62+
/// When <see cref="BypassHashVerification"/> is true, this method always returns true
63+
/// to facilitate development and testing scenarios.
64+
/// </remarks>
5065
public static bool VerifyHash (string filePath, string expectedHash)
5166
{
52-
// In Debug mode we trust the plugins (so testing and developing is possible)
53-
#if DEBUG
54-
return true;
55-
#else
67+
// Allow bypassing hash verification for development and testing
68+
if (BypassHashVerification)
69+
{
70+
return true;
71+
}
72+
5673
ArgumentException.ThrowIfNullOrWhiteSpace(filePath);
5774
ArgumentException.ThrowIfNullOrWhiteSpace(expectedHash);
5875

5976
var actualHash = CalculateHash(filePath);
6077

6178
// Case-insensitive comparison
6279
return string.Equals(actualHash, expectedHash, StringComparison.OrdinalIgnoreCase);
63-
#endif
6480
}
6581

6682
/// <summary>

src/RegexColumnizer.UnitTests/RegexColumnizerErrorHandlingTests.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ public void SetUp ()
2222
}
2323

2424
[TearDown]
25+
[System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1031:Do not catch general exception types", Justification = "Unit Tests")]
2526
public void TearDown ()
2627
{
2728
if (Directory.Exists(_testDirectory))
@@ -54,7 +55,7 @@ public void Init_InvalidRegex_SetsRegexToNullAndUsesEmptyColumns ()
5455
configField?.SetValue(columnizer, config);
5556

5657
// Act - Should not throw
57-
Assert.DoesNotThrow(() => columnizer.Init());
58+
Assert.DoesNotThrow(columnizer.Init);
5859

5960
// Assert - Regex should be null
6061
var regexProperty = typeof(BaseRegexColumnizer).GetProperty("Regex");
@@ -80,7 +81,7 @@ public void Init_CatastrophicBacktracking_HandledByRegexHelper ()
8081

8182
// Act - Should complete quickly due to RegexHelper timeout protection
8283
var stopwatch = System.Diagnostics.Stopwatch.StartNew();
83-
Assert.DoesNotThrow(() => columnizer.Init());
84+
Assert.DoesNotThrow(columnizer.Init);
8485
stopwatch.Stop();
8586

8687
// Assert - Should complete in reasonable time (RegexHelper has timeout)
@@ -281,7 +282,7 @@ public void Init_EmptyExpression_HandlesGracefully ()
281282
configField?.SetValue(columnizer, config);
282283

283284
// Act - Should not throw
284-
Assert.DoesNotThrow(() => columnizer.Init());
285+
Assert.DoesNotThrow(columnizer.Init);
285286

286287
// Assert - Regex might be null or default
287288
var regexProperty = typeof(BaseRegexColumnizer).GetProperty("Regex");

0 commit comments

Comments
 (0)