Skip to content

Commit 599096c

Browse files
committed
Remove usage of LinuxUtil and use Verify
1 parent 5e84c7a commit 599096c

18 files changed

+137
-121
lines changed

src/LibObjectFile.Tests/Ar/ArTestBase.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@
44

55
using System;
66
using LibObjectFile.Diagnostics;
7+
using LibObjectFile.Tests.Elf;
78

89
namespace LibObjectFile.Tests.Ar;
910

10-
public abstract class ArTestBase
11+
public abstract class ArTestBase : ElfTestBase
1112
{
1213
protected static void ExpectNoDiagnostics(DiagnosticBag diagnostics)
1314
{

src/LibObjectFile.Tests/Ar/ArTests.cs

+17-18
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@
66
using System.IO;
77
using System.Linq;
88
using System.Text;
9+
using System.Threading.Tasks;
910
using LibObjectFile.Ar;
1011
using LibObjectFile.Diagnostics;
12+
using VerifyTests;
1113

1214
namespace LibObjectFile.Tests.Ar;
1315

@@ -272,13 +274,8 @@ public void CheckInvalidBSDFileEntry()
272274
[TestMethod]
273275
public void CheckLibraryWithELF()
274276
{
275-
var cppName = "helloworld";
276-
var cppObj = $"{cppName}.o";
277-
var cppLib = $"lib{cppName}.a";
278-
File.Delete(cppObj);
279-
File.Delete(cppLib);
280-
LinuxUtil.RunLinuxExe("gcc", $"{cppName}.cpp -c -o {cppObj}");
281-
LinuxUtil.RunLinuxExe("ar", $"rcs {cppLib} {cppObj}");
277+
var cppObj = "helloworld.o";
278+
var cppLib = GetFile("libhelloworld.a");
282279

283280
using (var stream = new FileStream(cppLib, FileMode.Open, FileAccess.Read))
284281
{
@@ -301,23 +298,23 @@ public void CheckLibraryWithELF()
301298
var newArray = outStream.ToArray();
302299
outStream.Position = 0;
303300

304-
var cppLibCopy = $"lib{cppName}_copy.a";
305-
using (var copyStream = new FileStream(cppLibCopy, FileMode.Create, FileAccess.Write))
306-
{
307-
outStream.CopyTo(copyStream);
308-
}
301+
//var cppLibCopy = $"lib{cppName}_copy.a";
302+
//using (var copyStream = new FileStream(cppLibCopy, FileMode.Create, FileAccess.Write))
303+
//{
304+
// outStream.CopyTo(copyStream);
305+
//}
309306

310307
var originalStream = new MemoryStream();
311308
stream.Position = 0;
312309
stream.CopyTo(originalStream);
313310
var originalArray = originalStream.ToArray();
314311

315-
ByteArrayAssert.AreEqual(originalArray, newArray, $"Non binary matching between file {cppLib} and {cppLibCopy}");
312+
ByteArrayAssert.AreEqual(originalArray, newArray, $"Non binary matching for file {cppLib} ");
316313
}
317314
}
318315

319316
[TestMethod]
320-
public void CheckCreateArLibrary()
317+
public async Task CheckCreateArLibrary()
321318
{
322319
var libName = "libcustom.a";
323320

@@ -343,6 +340,8 @@ public void CheckCreateArLibrary()
343340
stream.Flush();
344341
}
345342

343+
Recording.Start();
344+
346345
// Check that AR is able to read back what we just serialized
347346
{
348347
var fileNameBuilder = new StringBuilder();
@@ -353,8 +352,7 @@ public void CheckCreateArLibrary()
353352
}
354353

355354
var fileNameList = fileNameBuilder.ToString().Trim();
356-
var fileNameListFromAr = LinuxUtil.RunLinuxExe("ar", $"t {libName}").Trim();
357-
Assert.AreEqual(fileNameListFromAr, fileNameList);
355+
Recording.Add("filenames", fileNameList);
358356
}
359357

360358
// Display the content of each file via AR
@@ -369,10 +367,11 @@ public void CheckCreateArLibrary()
369367
}
370368

371369
var content = contentBuilder.ToString().Trim();
372-
var contentFromAr = LinuxUtil.RunLinuxExe("ar", $"p {libName}").Trim();
373-
Assert.AreEqual(contentFromAr, content);
370+
Recording.Add("filecontent", content);
374371
}
375372

373+
await Verify();
374+
376375
ArArchiveFile file2;
377376
using (var stream = new FileStream(libName, FileMode.Open, FileAccess.Read))
378377
{

src/LibObjectFile.Tests/Dwarf/DwarfTests.cs

+16-63
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,12 @@
77
using LibObjectFile.Diagnostics;
88
using LibObjectFile.Dwarf;
99
using LibObjectFile.Elf;
10+
using LibObjectFile.Tests.Elf;
1011

1112
namespace LibObjectFile.Tests.Dwarf;
1213

1314
[TestClass]
14-
public class DwarfTests
15+
public class DwarfTests : ElfTestBase
1516
{
1617
[DataTestMethod]
1718
[DataRow(0UL)]
@@ -81,17 +82,7 @@ public void TestSignedLEB128(long value)
8182
[TestMethod]
8283
public void TestDebugLineHelloWorld()
8384
{
84-
var cppName = "helloworld";
85-
var cppExe = $"{cppName}_debug";
86-
LinuxUtil.RunLinuxExe("gcc", $"{cppName}.cpp -gdwarf-4 -o {cppExe}");
87-
88-
ElfFile elf;
89-
using (var inStream = File.OpenRead(cppExe))
90-
{
91-
Console.WriteLine($"ReadBack from {cppExe}");
92-
elf = ElfFile.Read(inStream);
93-
elf.Print(Console.Out);
94-
}
85+
ElfFile elf = LoadElf("helloworld_debug");
9586

9687
var elfContext = new DwarfElfContext(elf);
9788
var inputContext = new DwarfReaderContext(elfContext);
@@ -140,17 +131,7 @@ public void TestDebugLineHelloWorld()
140131
[TestMethod]
141132
public void TestDebugLineLibMultipleObjs()
142133
{
143-
var cppName = "lib";
144-
var libShared = $"{cppName}_debug.so";
145-
LinuxUtil.RunLinuxExe("gcc", $"{cppName}_a.cpp {cppName}_b.cpp -gdwarf-4 -shared -o {libShared}");
146-
147-
ElfFile elf;
148-
using (var inStream = File.OpenRead(libShared))
149-
{
150-
Console.WriteLine($"ReadBack from {libShared}");
151-
elf = ElfFile.Read(inStream);
152-
elf.Print(Console.Out);
153-
}
134+
ElfFile elf = LoadElf("lib_debug.so");
154135

155136
var elfContext = new DwarfElfContext(elf);
156137
var inputContext = new DwarfReaderContext(elfContext);
@@ -199,16 +180,7 @@ public void TestDebugLineLibMultipleObjs()
199180
[TestMethod]
200181
public void TestDebugLineSmall()
201182
{
202-
var cppName = "small";
203-
var cppObj = $"{cppName}_debug.o";
204-
LinuxUtil.RunLinuxExe("gcc", $"{cppName}.cpp -gdwarf-4 -c -o {cppObj}");
205-
ElfFile elf;
206-
using (var inStream = File.OpenRead(cppObj))
207-
{
208-
Console.WriteLine($"ReadBack from {cppObj}");
209-
elf = ElfFile.Read(inStream);
210-
elf.Print(Console.Out);
211-
}
183+
ElfFile elf = LoadElf("small_debug.o");
212184

213185
var elfContext = new DwarfElfContext(elf);
214186
var inputContext = new DwarfReaderContext(elfContext);
@@ -257,17 +229,7 @@ public void TestDebugLineSmall()
257229
[TestMethod]
258230
public void TestDebugLineMultipleFunctions()
259231
{
260-
var cppName = "multiple_functions";
261-
var cppObj = $"{cppName}_debug.o";
262-
LinuxUtil.RunLinuxExe("gcc", $"{cppName}.cpp -gdwarf-4 -c -o {cppObj}");
263-
264-
ElfFile elf;
265-
using (var inStream = File.OpenRead(cppObj))
266-
{
267-
Console.WriteLine($"ReadBack from {cppObj}");
268-
elf = ElfFile.Read(inStream);
269-
elf.Print(Console.Out);
270-
}
232+
ElfFile elf = LoadElf("multiple_functions_debug.o");
271233

272234
var elfContext = new DwarfElfContext(elf);
273235
var inputContext = new DwarfReaderContext(elfContext);
@@ -315,16 +277,7 @@ public void TestDebugLineMultipleFunctions()
315277
[TestMethod]
316278
public void TestDebugInfoSmall()
317279
{
318-
var cppName = "small";
319-
var cppObj = $"{cppName}_debug.o";
320-
LinuxUtil.RunLinuxExe("gcc", $"{cppName}.cpp -gdwarf-4 -c -o {cppObj}");
321-
322-
ElfFile elf;
323-
using (var inStream = File.OpenRead(cppObj))
324-
{
325-
elf = ElfFile.Read(inStream);
326-
elf.Print(Console.Out);
327-
}
280+
ElfFile elf = LoadElf("small_debug.o");
328281

329282
var elfContext = new DwarfElfContext(elf);
330283
var inputContext = new DwarfReaderContext(elfContext);
@@ -361,13 +314,13 @@ public void TestDebugInfoSmall()
361314

362315
dwarf.WriteToElf(elfContext);
363316

364-
var cppObj2 = $"{cppName}_debug2.o";
365-
using (var outStream = new FileStream(cppObj2, FileMode.Create))
366-
{
367-
elf.Write(outStream);
368-
}
317+
//var cppObj2 = $"{cppName}_debug2.o";
318+
//using (var outStream = new FileStream(cppObj2, FileMode.Create))
319+
//{
320+
// elf.Write(outStream);
321+
//}
369322

370-
PrintStreamLength(outputContext);
323+
//PrintStreamLength(outputContext);
371324
}
372325

373326

@@ -516,9 +469,9 @@ public void CreateDwarf()
516469
Console.WriteLine();
517470
dwarfFile.InfoSection.Print(Console.Out);
518471

519-
Console.WriteLine("ReadBack --debug-dump=rawline");
520-
var readelf = LinuxUtil.ReadElf(outputFileName, "--debug-dump=rawline").TrimEnd();
521-
Console.WriteLine(readelf);
472+
//Console.WriteLine("ReadBack --debug-dump=rawline");
473+
//var readelf = LinuxUtil.ReadElf(outputFileName, "--debug-dump=rawline").TrimEnd();
474+
//Console.WriteLine(readelf);
522475
}
523476

524477
private static void PrintStreamLength(DwarfReaderWriterContext context)

src/LibObjectFile.Tests/Elf/ElfTestBase.cs

+4-2
Original file line numberDiff line numberDiff line change
@@ -73,16 +73,18 @@ protected async Task LoadAndVerifyElf(string name)
7373

7474
protected ElfFile LoadElf(string name)
7575
{
76-
var file = Path.Combine(AppContext.BaseDirectory, "Elf", name);
76+
var file = GetFile(name);
7777
using var stream = File.OpenRead(file);
7878
return ElfFile.Read(stream);
7979
}
8080

8181
protected ElfFile LoadElf(string name, out byte[] originalBinary)
8282
{
83-
var file = Path.Combine(AppContext.BaseDirectory, "Elf", name);
83+
var file = GetFile(name);
8484
originalBinary = File.ReadAllBytes(file);
8585
using var stream = File.OpenRead(file);
8686
return ElfFile.Read(stream);
8787
}
88+
89+
protected string GetFile(string name) => Path.Combine(AppContext.BaseDirectory, "Elf", name);
8890
}

src/LibObjectFile.Tests/Elf/compile_files.sh

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#!/bin/sh
22
gcc helloworld.cpp -o helloworld
3+
gcc helloworld.cpp -c -o helloworld.o
4+
ar rcs libhelloworld.a helloworld.o
35
gcc helloworld.cpp -gdwarf-4 -o helloworld_debug
46
gcc lib_a.cpp lib_b.cpp -gdwarf-4 -shared -o lib_debug.so
57
gcc small.cpp -gdwarf-4 -c -o small_debug.o
1.48 KB
Binary file not shown.
8 Bytes
Binary file not shown.
8 Bytes
Binary file not shown.
1.62 KB
Binary file not shown.
Binary file not shown.
0 Bytes
Binary file not shown.

src/LibObjectFile.Tests/LibObjectFile.Tests.csproj

+8
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@
1010

1111
<ItemGroup>
1212
<None Remove="Elf\helloworld" />
13+
<None Remove="Elf\helloworld.o" />
1314
<None Remove="Elf\helloworld_debug" />
15+
<None Remove="Elf\libhelloworld.a" />
1416
<None Remove="Elf\lib_debug.so" />
1517
<None Remove="Elf\multiple_functions_debug.o" />
1618
<None Remove="Elf\small_debug.o" />
@@ -30,9 +32,15 @@
3032
<Content Include="Elf\helloworld">
3133
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
3234
</Content>
35+
<Content Include="Elf\helloworld.o">
36+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
37+
</Content>
3338
<Content Include="Elf\helloworld_debug">
3439
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
3540
</Content>
41+
<Content Include="Elf\libhelloworld.a">
42+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
43+
</Content>
3644
<Content Include="Elf\libstdc++.so">
3745
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
3846
</Content>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
filenames:
3+
file2.txt
4+
file3.txt
5+
file4.txt
6+
file5.txt
7+
long_file_name_large_file6.txt,
8+
filecontent: this is filethis is file3this is file4this is file5this is file6 yoyo
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
ELF Header:
2+
Magic: 7f 45 4c 46 02 01 01 00 00 00 00 00 00 00 00 00
3+
Class: ELF64
4+
Data: 2's complement, little endian
5+
Version: 1 (current)
6+
OS/ABI: UNIX - System V
7+
ABI Version: 0
8+
Type: REL (Relocatable file)
9+
Machine: Advanced Micro Devices X86-64
10+
Version: 0x1
11+
Entry point address: 0x0
12+
Start of program headers: 0 (bytes into file)
13+
Start of section headers: 4128 (bytes into file)
14+
Flags: 0x0
15+
Size of this header: 64 (bytes)
16+
Size of program headers: 0 (bytes)
17+
Number of program headers: 0
18+
Size of section headers: 64 (bytes)
19+
Number of section headers: 3
20+
Section header string table index: 2
21+
22+
Section Headers:
23+
[Nr] Name Type Address Off Size ES Flg Lk Inf Al
24+
[ 0] NULL 0000000000000000 000000 000000 00 0 0 0
25+
[ 1] .text PROGBITS 0000000000000000 001000 00000e 00 AX 0 0 0
26+
[ 2] .shstrtab STRTAB 0000000000000000 00100e 000011 00 0 0 0
27+
Key to Flags:
28+
W (write), A (alloc), X (execute), M (merge), S (strings), I (info),
29+
L (link order), O (extra OS processing required), G (group), T (TLS),
30+
C (compressed), x (unknown), o (OS specific), E (exclude),
31+
D (mbind), l (large), p (processor specific)
32+
33+
There are no section groups in this file.
34+
35+
There are no program headers in this file.
36+
37+
There is no dynamic section in this file.
38+
39+
There are no relocations in this file.
40+
No processor specific unwind information to decode
41+
42+
No version information found in this file.

src/LibObjectFile.Tests/Verified/ElfSimpleTests.TestElf_name=helloworld_debug.verified.txt

+6-6
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
Version: 0x1
1111
Entry point address: 0x1060
1212
Start of program headers: 64 (bytes into file)
13-
Start of section headers: 14824 (bytes into file)
13+
Start of section headers: 14832 (bytes into file)
1414
Flags: 0x0
1515
Size of this header: 64 (bytes)
1616
Size of program headers: 56 (bytes)
@@ -53,10 +53,10 @@ Section Headers:
5353
[29] .debug_info PROGBITS 0000000000000000 00306b 0000dd 00 0 0 1
5454
[30] .debug_abbrev PROGBITS 0000000000000000 003148 000060 00 0 0 1
5555
[31] .debug_line PROGBITS 0000000000000000 0031a8 00004a 00 0 0 1
56-
[32] .debug_str PROGBITS 0000000000000000 0031f2 000156 01 MS 0 0 1
57-
[33] .symtab SYMTAB 0000000000000000 003348 000360 18 34 18 8
58-
[34] .strtab STRTAB 0000000000000000 0036a8 0001e2 00 0 0 1
59-
[35] .shstrtab STRTAB 0000000000000000 00388a 00015a 00 0 0 1
56+
[32] .debug_str PROGBITS 0000000000000000 0031f2 00015a 01 MS 0 0 1
57+
[33] .symtab SYMTAB 0000000000000000 003350 000360 18 34 18 8
58+
[34] .strtab STRTAB 0000000000000000 0036b0 0001e2 00 0 0 1
59+
[35] .shstrtab STRTAB 0000000000000000 003892 00015a 00 0 0 1
6060
Key to Flags:
6161
W (write), A (alloc), X (execute), M (merge), S (strings), I (info),
6262
L (link order), O (extra OS processing required), G (group), T (TLS),
@@ -172,7 +172,7 @@ Displaying notes found in: .note.gnu.property
172172

173173
Displaying notes found in: .note.gnu.build-id
174174
Owner Data size Description
175-
GNU 0x00000014 NT_GNU_BUILD_ID (unique build ID bitstring) Build ID: e24b02ce370f3d95023dc7feb6e8392f84879f73
175+
GNU 0x00000014 NT_GNU_BUILD_ID (unique build ID bitstring) Build ID: e1e668f3756a7fc90bd1a79a2017586b8ac2ab0b
176176

177177
Displaying notes found in: .note.ABI-tag
178178
Owner Data size Description

0 commit comments

Comments
 (0)