Skip to content

Commit

Permalink
Remove usage of LinuxUtil and use Verify
Browse files Browse the repository at this point in the history
  • Loading branch information
xoofx committed Oct 14, 2024
1 parent 5e84c7a commit 599096c
Show file tree
Hide file tree
Showing 18 changed files with 137 additions and 121 deletions.
3 changes: 2 additions & 1 deletion src/LibObjectFile.Tests/Ar/ArTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@

using System;
using LibObjectFile.Diagnostics;
using LibObjectFile.Tests.Elf;

namespace LibObjectFile.Tests.Ar;

public abstract class ArTestBase
public abstract class ArTestBase : ElfTestBase
{
protected static void ExpectNoDiagnostics(DiagnosticBag diagnostics)
{
Expand Down
35 changes: 17 additions & 18 deletions src/LibObjectFile.Tests/Ar/ArTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using LibObjectFile.Ar;
using LibObjectFile.Diagnostics;
using VerifyTests;

namespace LibObjectFile.Tests.Ar;

Expand Down Expand Up @@ -272,13 +274,8 @@ public void CheckInvalidBSDFileEntry()
[TestMethod]
public void CheckLibraryWithELF()
{
var cppName = "helloworld";
var cppObj = $"{cppName}.o";
var cppLib = $"lib{cppName}.a";
File.Delete(cppObj);
File.Delete(cppLib);
LinuxUtil.RunLinuxExe("gcc", $"{cppName}.cpp -c -o {cppObj}");
LinuxUtil.RunLinuxExe("ar", $"rcs {cppLib} {cppObj}");
var cppObj = "helloworld.o";
var cppLib = GetFile("libhelloworld.a");

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

var cppLibCopy = $"lib{cppName}_copy.a";
using (var copyStream = new FileStream(cppLibCopy, FileMode.Create, FileAccess.Write))
{
outStream.CopyTo(copyStream);
}
//var cppLibCopy = $"lib{cppName}_copy.a";
//using (var copyStream = new FileStream(cppLibCopy, FileMode.Create, FileAccess.Write))
//{
// outStream.CopyTo(copyStream);
//}

var originalStream = new MemoryStream();
stream.Position = 0;
stream.CopyTo(originalStream);
var originalArray = originalStream.ToArray();

ByteArrayAssert.AreEqual(originalArray, newArray, $"Non binary matching between file {cppLib} and {cppLibCopy}");
ByteArrayAssert.AreEqual(originalArray, newArray, $"Non binary matching for file {cppLib} ");
}
}

[TestMethod]
public void CheckCreateArLibrary()
public async Task CheckCreateArLibrary()
{
var libName = "libcustom.a";

Expand All @@ -343,6 +340,8 @@ public void CheckCreateArLibrary()
stream.Flush();
}

Recording.Start();

// Check that AR is able to read back what we just serialized
{
var fileNameBuilder = new StringBuilder();
Expand All @@ -353,8 +352,7 @@ public void CheckCreateArLibrary()
}

var fileNameList = fileNameBuilder.ToString().Trim();
var fileNameListFromAr = LinuxUtil.RunLinuxExe("ar", $"t {libName}").Trim();
Assert.AreEqual(fileNameListFromAr, fileNameList);
Recording.Add("filenames", fileNameList);
}

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

var content = contentBuilder.ToString().Trim();
var contentFromAr = LinuxUtil.RunLinuxExe("ar", $"p {libName}").Trim();
Assert.AreEqual(contentFromAr, content);
Recording.Add("filecontent", content);
}

await Verify();

ArArchiveFile file2;
using (var stream = new FileStream(libName, FileMode.Open, FileAccess.Read))
{
Expand Down
79 changes: 16 additions & 63 deletions src/LibObjectFile.Tests/Dwarf/DwarfTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@
using LibObjectFile.Diagnostics;
using LibObjectFile.Dwarf;
using LibObjectFile.Elf;
using LibObjectFile.Tests.Elf;

namespace LibObjectFile.Tests.Dwarf;

[TestClass]
public class DwarfTests
public class DwarfTests : ElfTestBase
{
[DataTestMethod]
[DataRow(0UL)]
Expand Down Expand Up @@ -81,17 +82,7 @@ public void TestSignedLEB128(long value)
[TestMethod]
public void TestDebugLineHelloWorld()
{
var cppName = "helloworld";
var cppExe = $"{cppName}_debug";
LinuxUtil.RunLinuxExe("gcc", $"{cppName}.cpp -gdwarf-4 -o {cppExe}");

ElfFile elf;
using (var inStream = File.OpenRead(cppExe))
{
Console.WriteLine($"ReadBack from {cppExe}");
elf = ElfFile.Read(inStream);
elf.Print(Console.Out);
}
ElfFile elf = LoadElf("helloworld_debug");

var elfContext = new DwarfElfContext(elf);
var inputContext = new DwarfReaderContext(elfContext);
Expand Down Expand Up @@ -140,17 +131,7 @@ public void TestDebugLineHelloWorld()
[TestMethod]
public void TestDebugLineLibMultipleObjs()
{
var cppName = "lib";
var libShared = $"{cppName}_debug.so";
LinuxUtil.RunLinuxExe("gcc", $"{cppName}_a.cpp {cppName}_b.cpp -gdwarf-4 -shared -o {libShared}");

ElfFile elf;
using (var inStream = File.OpenRead(libShared))
{
Console.WriteLine($"ReadBack from {libShared}");
elf = ElfFile.Read(inStream);
elf.Print(Console.Out);
}
ElfFile elf = LoadElf("lib_debug.so");

var elfContext = new DwarfElfContext(elf);
var inputContext = new DwarfReaderContext(elfContext);
Expand Down Expand Up @@ -199,16 +180,7 @@ public void TestDebugLineLibMultipleObjs()
[TestMethod]
public void TestDebugLineSmall()
{
var cppName = "small";
var cppObj = $"{cppName}_debug.o";
LinuxUtil.RunLinuxExe("gcc", $"{cppName}.cpp -gdwarf-4 -c -o {cppObj}");
ElfFile elf;
using (var inStream = File.OpenRead(cppObj))
{
Console.WriteLine($"ReadBack from {cppObj}");
elf = ElfFile.Read(inStream);
elf.Print(Console.Out);
}
ElfFile elf = LoadElf("small_debug.o");

var elfContext = new DwarfElfContext(elf);
var inputContext = new DwarfReaderContext(elfContext);
Expand Down Expand Up @@ -257,17 +229,7 @@ public void TestDebugLineSmall()
[TestMethod]
public void TestDebugLineMultipleFunctions()
{
var cppName = "multiple_functions";
var cppObj = $"{cppName}_debug.o";
LinuxUtil.RunLinuxExe("gcc", $"{cppName}.cpp -gdwarf-4 -c -o {cppObj}");

ElfFile elf;
using (var inStream = File.OpenRead(cppObj))
{
Console.WriteLine($"ReadBack from {cppObj}");
elf = ElfFile.Read(inStream);
elf.Print(Console.Out);
}
ElfFile elf = LoadElf("multiple_functions_debug.o");

var elfContext = new DwarfElfContext(elf);
var inputContext = new DwarfReaderContext(elfContext);
Expand Down Expand Up @@ -315,16 +277,7 @@ public void TestDebugLineMultipleFunctions()
[TestMethod]
public void TestDebugInfoSmall()
{
var cppName = "small";
var cppObj = $"{cppName}_debug.o";
LinuxUtil.RunLinuxExe("gcc", $"{cppName}.cpp -gdwarf-4 -c -o {cppObj}");

ElfFile elf;
using (var inStream = File.OpenRead(cppObj))
{
elf = ElfFile.Read(inStream);
elf.Print(Console.Out);
}
ElfFile elf = LoadElf("small_debug.o");

var elfContext = new DwarfElfContext(elf);
var inputContext = new DwarfReaderContext(elfContext);
Expand Down Expand Up @@ -361,13 +314,13 @@ public void TestDebugInfoSmall()

dwarf.WriteToElf(elfContext);

var cppObj2 = $"{cppName}_debug2.o";
using (var outStream = new FileStream(cppObj2, FileMode.Create))
{
elf.Write(outStream);
}
//var cppObj2 = $"{cppName}_debug2.o";
//using (var outStream = new FileStream(cppObj2, FileMode.Create))
//{
// elf.Write(outStream);
//}

PrintStreamLength(outputContext);
//PrintStreamLength(outputContext);
}


Expand Down Expand Up @@ -516,9 +469,9 @@ public void CreateDwarf()
Console.WriteLine();
dwarfFile.InfoSection.Print(Console.Out);

Console.WriteLine("ReadBack --debug-dump=rawline");
var readelf = LinuxUtil.ReadElf(outputFileName, "--debug-dump=rawline").TrimEnd();
Console.WriteLine(readelf);
//Console.WriteLine("ReadBack --debug-dump=rawline");
//var readelf = LinuxUtil.ReadElf(outputFileName, "--debug-dump=rawline").TrimEnd();
//Console.WriteLine(readelf);
}

private static void PrintStreamLength(DwarfReaderWriterContext context)
Expand Down
6 changes: 4 additions & 2 deletions src/LibObjectFile.Tests/Elf/ElfTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,16 +73,18 @@ protected async Task LoadAndVerifyElf(string name)

protected ElfFile LoadElf(string name)
{
var file = Path.Combine(AppContext.BaseDirectory, "Elf", name);
var file = GetFile(name);
using var stream = File.OpenRead(file);
return ElfFile.Read(stream);
}

protected ElfFile LoadElf(string name, out byte[] originalBinary)
{
var file = Path.Combine(AppContext.BaseDirectory, "Elf", name);
var file = GetFile(name);
originalBinary = File.ReadAllBytes(file);
using var stream = File.OpenRead(file);
return ElfFile.Read(stream);
}

protected string GetFile(string name) => Path.Combine(AppContext.BaseDirectory, "Elf", name);
}
2 changes: 2 additions & 0 deletions src/LibObjectFile.Tests/Elf/compile_files.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#!/bin/sh
gcc helloworld.cpp -o helloworld
gcc helloworld.cpp -c -o helloworld.o
ar rcs libhelloworld.a helloworld.o
gcc helloworld.cpp -gdwarf-4 -o helloworld_debug
gcc lib_a.cpp lib_b.cpp -gdwarf-4 -shared -o lib_debug.so
gcc small.cpp -gdwarf-4 -c -o small_debug.o
Expand Down
Binary file added src/LibObjectFile.Tests/Elf/helloworld.o
Binary file not shown.
Binary file modified src/LibObjectFile.Tests/Elf/helloworld_debug
Binary file not shown.
Binary file modified src/LibObjectFile.Tests/Elf/lib_debug.so
Binary file not shown.
Binary file added src/LibObjectFile.Tests/Elf/libhelloworld.a
Binary file not shown.
Binary file modified src/LibObjectFile.Tests/Elf/multiple_functions_debug.o
Binary file not shown.
Binary file modified src/LibObjectFile.Tests/Elf/small_debug.o
Binary file not shown.
8 changes: 8 additions & 0 deletions src/LibObjectFile.Tests/LibObjectFile.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@

<ItemGroup>
<None Remove="Elf\helloworld" />
<None Remove="Elf\helloworld.o" />
<None Remove="Elf\helloworld_debug" />
<None Remove="Elf\libhelloworld.a" />
<None Remove="Elf\lib_debug.so" />
<None Remove="Elf\multiple_functions_debug.o" />
<None Remove="Elf\small_debug.o" />
Expand All @@ -30,9 +32,15 @@
<Content Include="Elf\helloworld">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="Elf\helloworld.o">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="Elf\helloworld_debug">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="Elf\libhelloworld.a">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="Elf\libstdc++.so">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
filenames:
file2.txt
file3.txt
file4.txt
file5.txt
long_file_name_large_file6.txt,
filecontent: this is filethis is file3this is file4this is file5this is file6 yoyo
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
ELF Header:
Magic: 7f 45 4c 46 02 01 01 00 00 00 00 00 00 00 00 00
Class: ELF64
Data: 2's complement, little endian
Version: 1 (current)
OS/ABI: UNIX - System V
ABI Version: 0
Type: REL (Relocatable file)
Machine: Advanced Micro Devices X86-64
Version: 0x1
Entry point address: 0x0
Start of program headers: 0 (bytes into file)
Start of section headers: 4128 (bytes into file)
Flags: 0x0
Size of this header: 64 (bytes)
Size of program headers: 0 (bytes)
Number of program headers: 0
Size of section headers: 64 (bytes)
Number of section headers: 3
Section header string table index: 2

Section Headers:
[Nr] Name Type Address Off Size ES Flg Lk Inf Al
[ 0] NULL 0000000000000000 000000 000000 00 0 0 0
[ 1] .text PROGBITS 0000000000000000 001000 00000e 00 AX 0 0 0
[ 2] .shstrtab STRTAB 0000000000000000 00100e 000011 00 0 0 0
Key to Flags:
W (write), A (alloc), X (execute), M (merge), S (strings), I (info),
L (link order), O (extra OS processing required), G (group), T (TLS),
C (compressed), x (unknown), o (OS specific), E (exclude),
D (mbind), l (large), p (processor specific)

There are no section groups in this file.

There are no program headers in this file.

There is no dynamic section in this file.

There are no relocations in this file.
No processor specific unwind information to decode

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

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

Displaying notes found in: .note.ABI-tag
Owner Data size Description
Expand Down
Loading

0 comments on commit 599096c

Please sign in to comment.