Skip to content

Commit 64006dd

Browse files
committed
Improve tests when comparing buffers
1 parent 2ff9b8c commit 64006dd

File tree

8 files changed

+81
-22
lines changed

8 files changed

+81
-22
lines changed

src/LibObjectFile.Tests/Ar/ArTests.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -160,13 +160,13 @@ public void CheckInvalidFileEntry()
160160
Assert.AreEqual(2U, arFile.Files[0].Size, "Invalid size of file entry[0] found");
161161
Assert.IsInstanceOfType<ArBinaryFile>(arFile.Files[0], "Invalid instance of of file entry[0] ");
162162

163-
var fileStream = ((ArBinaryFile) arFile.Files[0]).Stream;
163+
var fileStream = ((ArBinaryFile) arFile.Files[0]).Stream!;
164164
var read = new byte[]
165165
{
166166
(byte)fileStream.ReadByte(),
167167
(byte)fileStream.ReadByte()
168168
};
169-
CollectionAssert.AreEqual(new byte[] { 0, 1}, read, "Invalid content of of file entry[0] ");
169+
ByteArrayAssert.AreEqual(new byte[] { 0, 1}, read, "Invalid content of of file entry[0] ");
170170

171171
Assert.IsNull(arFile.SymbolTable, "Invalid non-null symbol table found");
172172

@@ -312,7 +312,7 @@ public void CheckLibraryWithELF()
312312
stream.CopyTo(originalStream);
313313
var originalArray = originalStream.ToArray();
314314

315-
CollectionAssert.AreEqual(originalArray, newArray, $"Non binary matching between file {cppLib} and {cppLibCopy}");
315+
ByteArrayAssert.AreEqual(originalArray, newArray, $"Non binary matching between file {cppLib} and {cppLibCopy}");
316316
}
317317
}
318318

@@ -364,7 +364,7 @@ public void CheckCreateArLibrary()
364364
{
365365
if (!(fileEntry is ArBinaryFile arBinary)) continue;
366366

367-
arBinary.Stream.Position = 0;
367+
arBinary.Stream!.Position = 0;
368368
contentBuilder.Append(Encoding.UTF8.GetString(((MemoryStream) arBinary.Stream).ToArray()));
369369
}
370370

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// Copyright (c) Alexandre Mutel. All rights reserved.
2+
// This file is licensed under the BSD-Clause 2 license.
3+
// See the license.txt file in the project root for more information.
4+
5+
using System;
6+
using System.Text;
7+
8+
namespace LibObjectFile.Tests;
9+
10+
/// <summary>
11+
/// Helper class to assert two byte arrays are equal.
12+
/// </summary>
13+
public static class ByteArrayAssert
14+
{
15+
public static void AreEqual(byte[] expected, byte[] actual, string? message = null)
16+
{
17+
if (expected.Length != actual.Length)
18+
{
19+
throw new AssertFailedException($"The expected array `{expected.Length}` is not equal to the actual array `{actual.Length}`. {message}");
20+
}
21+
22+
for (int i = 0; i < expected.Length; i++)
23+
{
24+
if (expected[i] != actual[i])
25+
{
26+
StringBuilder builder = new();
27+
28+
var minLength = Math.Min(expected.Length, actual.Length);
29+
30+
const int maxLines = 5;
31+
32+
int j = Math.Max(i - maxLines, 0);
33+
34+
var minText = $"[{j} / 0x{j:X}]".Length;
35+
36+
builder.AppendLine("```");
37+
builder.AppendLine($"{new(' ',minText - 2)} Expected == Actual");
38+
39+
for (; j < Math.Min(minLength, i + maxLines + 1); j++)
40+
{
41+
if (expected[j] != actual[j])
42+
{
43+
builder.AppendLine($"[{j} / 0x{j:X}] 0x{expected[j]:X2} != 0x{actual[j]:X2} <---- Actual value is not matching expecting");
44+
}
45+
else
46+
{
47+
builder.AppendLine($"[{j} / 0x{j:X}] 0x{expected[j]:X2}");
48+
}
49+
}
50+
builder.AppendLine("```");
51+
52+
throw new AssertFailedException($"The expected array is not equal to the actual array at index {i}. {message}{Environment.NewLine}{Environment.NewLine}{builder}");
53+
}
54+
}
55+
}
56+
}

src/LibObjectFile.Tests/Dwarf/DwarfTests.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ public void TestDebugLineHelloWorld()
9898
inputContext.DebugLinePrinter = Console.Out;
9999
var dwarf = DwarfFile.Read(inputContext);
100100

101-
inputContext.DebugLineStream.Position = 0;
101+
inputContext.DebugLineStream!.Position = 0;
102102

103103
var copyInputDebugLineStream = new MemoryStream();
104104
inputContext.DebugLineStream.CopyTo(copyInputDebugLineStream);
@@ -134,7 +134,7 @@ public void TestDebugLineHelloWorld()
134134

135135
var inputDebugLineBuffer = copyInputDebugLineStream.ToArray();
136136
var outputDebugLineBuffer = ((MemoryStream)reloadContext.DebugLineStream).ToArray();
137-
CollectionAssert.AreEqual(inputDebugLineBuffer, outputDebugLineBuffer);
137+
ByteArrayAssert.AreEqual(inputDebugLineBuffer, outputDebugLineBuffer);
138138
}
139139

140140
[TestMethod]
@@ -157,7 +157,7 @@ public void TestDebugLineLibMultipleObjs()
157157
inputContext.DebugLinePrinter = Console.Out;
158158
var dwarf = DwarfFile.Read(inputContext);
159159

160-
inputContext.DebugLineStream.Position = 0;
160+
inputContext.DebugLineStream!.Position = 0;
161161

162162
var copyInputDebugLineStream = new MemoryStream();
163163
inputContext.DebugLineStream.CopyTo(copyInputDebugLineStream);
@@ -193,7 +193,7 @@ public void TestDebugLineLibMultipleObjs()
193193

194194
var inputDebugLineBuffer = copyInputDebugLineStream.ToArray();
195195
var outputDebugLineBuffer = ((MemoryStream)reloadContext.DebugLineStream).ToArray();
196-
CollectionAssert.AreEqual(inputDebugLineBuffer, outputDebugLineBuffer);
196+
ByteArrayAssert.AreEqual(inputDebugLineBuffer, outputDebugLineBuffer);
197197
}
198198

199199
[TestMethod]
@@ -215,7 +215,7 @@ public void TestDebugLineSmall()
215215
inputContext.DebugLinePrinter = Console.Out;
216216
var dwarf = DwarfFile.Read(inputContext);
217217

218-
inputContext.DebugLineStream.Position = 0;
218+
inputContext.DebugLineStream!.Position = 0;
219219
var copyInputDebugLineStream = new MemoryStream();
220220
inputContext.DebugLineStream.CopyTo(copyInputDebugLineStream);
221221
inputContext.DebugLineStream.Position = 0;
@@ -249,7 +249,7 @@ public void TestDebugLineSmall()
249249

250250
var inputDebugLineBuffer = copyInputDebugLineStream.ToArray();
251251
var outputDebugLineBuffer = ((MemoryStream)reloadContext.DebugLineStream).ToArray();
252-
CollectionAssert.AreEqual(inputDebugLineBuffer, outputDebugLineBuffer);
252+
ByteArrayAssert.AreEqual(inputDebugLineBuffer, outputDebugLineBuffer);
253253
}
254254

255255

@@ -274,7 +274,7 @@ public void TestDebugLineMultipleFunctions()
274274
inputContext.DebugLinePrinter = Console.Out;
275275
var dwarf = DwarfFile.Read(inputContext);
276276

277-
inputContext.DebugLineStream.Position = 0;
277+
inputContext.DebugLineStream!.Position = 0;
278278
var copyInputDebugLineStream = new MemoryStream();
279279
inputContext.DebugLineStream.CopyTo(copyInputDebugLineStream);
280280
inputContext.DebugLineStream.Position = 0;
@@ -308,7 +308,7 @@ public void TestDebugLineMultipleFunctions()
308308

309309
var inputDebugLineBuffer = copyInputDebugLineStream.ToArray();
310310
var outputDebugLineBuffer = ((MemoryStream)reloadContext.DebugLineStream).ToArray();
311-
CollectionAssert.AreEqual(inputDebugLineBuffer, outputDebugLineBuffer);
311+
ByteArrayAssert.AreEqual(inputDebugLineBuffer, outputDebugLineBuffer);
312312
}
313313

314314

src/LibObjectFile.Tests/Elf/ElfSimpleTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -476,7 +476,7 @@ public void TestManySections()
476476
symbolTable = (ElfSymbolTable)elf.Sections[ushort.MaxValue + 3];
477477
for (int i = 0; i < ushort.MaxValue; i++)
478478
{
479-
Assert.AreEqual($".section{i}", symbolTable.Entries[i + 1].Section.Section.Name.Value);
479+
Assert.AreEqual($".section{i}", symbolTable.Entries[i + 1].Section.Section!.Name.Value);
480480
}
481481
}
482482

src/LibObjectFile.Tests/Elf/ElfTestBase.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ protected static void AssertReadElf(ElfObjectFile elf, string fileName)
1818
AssertLsbMsb(elf, fileName);
1919
}
2020

21-
protected static void AssertReadElfInternal(ElfObjectFile elf, string fileName, bool writeFile = true, string context = null, string readElfParams = null)
21+
protected static void AssertReadElfInternal(ElfObjectFile elf, string fileName, bool writeFile = true, string? context = null, string? readElfParams = null)
2222
{
2323
if (writeFile)
2424
{
@@ -76,7 +76,7 @@ protected static void AssertReadBack(ElfObjectFile elf, string fileName, bool re
7676
newObjectFile.Write(memoryStream);
7777
var newBuffer = memoryStream.ToArray();
7878

79-
CollectionAssert.AreEqual(originalBuffer, newBuffer, "Invalid binary diff between write -> (original) -> read -> write -> (new)");
79+
ByteArrayAssert.AreEqual(originalBuffer, newBuffer, "Invalid binary diff between write -> (original) -> read -> write -> (new)");
8080
}
8181
}
8282

src/LibObjectFile.Tests/LibObjectFile.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
66
<IsPackable>false</IsPackable>
77
<EnableMSTestRunner>true</EnableMSTestRunner>
8+
<Nullable>enable</Nullable>
89
</PropertyGroup>
910

1011
<ItemGroup>

src/LibObjectFile.Tests/LinuxUtil.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) Alexandre Mutel. All rights reserved.
1+
// Copyright (c) Alexandre Mutel. All rights reserved.
22
// This file is licensed under the BSD-Clause 2 license.
33
// See the license.txt file in the project root for more information.
44

@@ -33,7 +33,7 @@ public static string RunLinuxExe(string exe, string arguments, string distributi
3333
exe = "wsl.exe";
3434
}
3535

36-
StringBuilder errorBuilder = null;
36+
StringBuilder? errorBuilder = null;
3737
StringBuilder outputBuilder = new StringBuilder();
3838

3939
using (var process = new Process()

src/LibObjectFile.Tests/PE/PEReaderTests.cs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,16 @@ public async Task TestPrinter(string name)
5858
var output = new MemoryStream();
5959
peImage.Write(output);
6060
output.Position = 0;
61-
var outputBuffer = output.ToArray();
61+
byte[] outputBuffer = output.ToArray();
62+
63+
// Fake an error
64+
//outputBuffer[250] = 0x44;
6265

6366
//await Verifier.Verify(outputBuffer, sourceFile sourceFile).
6467
await File.WriteAllBytesAsync($"{sourceFile}.bak", outputBuffer);
6568

6669
// Compare the input and output buffer
67-
CollectionAssert.AreEqual(inputBuffer, outputBuffer);
70+
ByteArrayAssert.AreEqual(inputBuffer, outputBuffer, $"Invalid roundtrip for `{name}`");
6871
}
6972

7073
[DataTestMethod]
@@ -103,8 +106,7 @@ public async Task TestWindows(string sourceFile)
103106
TestContext.WriteLine($"SizeOfInitializedData changed from {sizeOfInitializedData} to {newSizeOfInitializedData}. Trying to reuse old size");
104107
peImage.ForceSizeOfInitializedData = sizeOfInitializedData;
105108
}
106-
107-
109+
108110
// Write the PE back to a byte buffer
109111
var output = new MemoryStream();
110112
peImage.Write(output);
@@ -124,7 +126,7 @@ public async Task TestWindows(string sourceFile)
124126
// await File.WriteAllBytesAsync(outputFileName, outputBuffer);
125127
//}
126128

127-
CollectionAssert.AreEqual(inputBuffer, outputBuffer);
129+
ByteArrayAssert.AreEqual(inputBuffer, outputBuffer, $"Invalid roundtrip for `{sourceFile}`");
128130
}
129131
}
130132

0 commit comments

Comments
 (0)