Skip to content

Commit

Permalink
Add source for binary files and fix tests for new binaries
Browse files Browse the repository at this point in the history
  • Loading branch information
jtschuster committed Sep 23, 2024
1 parent 3a85c8e commit 767b609
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 42 deletions.
Binary file not shown.
4 changes: 4 additions & 0 deletions src/installer/tests/Melanzana.MachO.Tests/Data/library.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
int add(int a, int b)
{
return a + b;
}
6 changes: 6 additions & 0 deletions src/installer/tests/Melanzana.MachO.Tests/Data/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#include <stdio.h>

int main()
{
printf("Hello world");
}
19 changes: 11 additions & 8 deletions src/installer/tests/Melanzana.MachO.Tests/ReadTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,25 +24,28 @@ public void ReadExecutable()
var segments = objectFile.LoadCommands.OfType<MachSegment>().ToArray();
Assert.Equal("__PAGEZERO", segments[0].Name);
Assert.Equal("__TEXT", segments[1].Name);
Assert.Equal("__LINKEDIT", segments[2].Name);
Assert.Equal("__DATA_CONST", segments[2].Name);
Assert.Equal("__LINKEDIT", segments[3].Name);

var symbolTable = objectFile.LoadCommands.OfType<MachSymbolTable>().FirstOrDefault();
Assert.NotNull(symbolTable);
var symbols = symbolTable!.Symbols.ToArray();
Assert.Equal(2, symbols.Length);
Assert.Equal(3, symbols.Length);
Assert.Equal("__mh_execute_header", symbols[0].Name);
Assert.Equal(0x100000000u, symbols[0].Value);
Assert.Equal("_main", symbols[1].Name);
Assert.Equal(0x100003fa4u, symbols[1].Value);
Assert.Equal(0x100003F70u, symbols[1].Value);
Assert.Equal("_printf", symbols[2].Name);
Assert.Equal(0u, symbols[2].Value);

var buildVersion = objectFile.LoadCommands.OfType<MachBuildVersion>().FirstOrDefault();
Assert.NotNull(buildVersion);
Assert.Equal(MachPlatform.MacOS, buildVersion!.Platform);
Assert.Equal("12.0.0", buildVersion!.MinimumPlatformVersion.ToString());
Assert.Equal("12.0.0", buildVersion!.SdkVersion.ToString());
Assert.Equal("14.0.0", buildVersion!.MinimumPlatformVersion.ToString());
Assert.Equal("15.0.0", buildVersion!.SdkVersion.ToString());
Assert.Equal(1, buildVersion!.ToolVersions.Count);
Assert.Equal(MachBuildTool.Ld, buildVersion!.ToolVersions[0].BuildTool);
Assert.Equal("711.0.0", buildVersion!.ToolVersions[0].Version.ToString());
Assert.Equal("1115.7.3", buildVersion!.ToolVersions[0].Version.ToString());
}

[Fact]
Expand Down Expand Up @@ -80,8 +83,8 @@ public void ReadObjectFile()
Assert.Equal(0u, symbols[0].Value);
Assert.Equal("ltmp1", symbols[1].Name);
Assert.Equal(compactUnwindSection, symbols[1].Section);
Assert.Equal(0x18u, symbols[1].Value);
Assert.Equal("_main", symbols[2].Name);
Assert.Equal(0x20u, symbols[1].Value);
Assert.Equal("_add", symbols[2].Name);
Assert.Equal(textSection, symbols[2].Section);
Assert.Equal(0u, symbols[2].Value);
}
Expand Down
7 changes: 0 additions & 7 deletions src/installer/tests/Melanzana.MachO.Tests/RoundtripTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,6 @@ public void BasicRoundtrip()
TestRoundtrip(aOutStream);
}

[Fact]
public void FatRoundtrip()
{
var aFatOutStream = typeof(RoundtripTests).Assembly.GetManifestResourceStream("Melanzana.MachO.Tests.Data.a.fat.out")!;
TestFatRoundtrip(aFatOutStream);
}

[Fact]
public void ObjectFileRoundtrip()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Buffers.Binary;
using System.Diagnostics;
using System.IO;
using System.Linq;
Expand Down Expand Up @@ -263,32 +262,6 @@ public void ExecutableImage()
.Be(expectedPermissions);
}


[Fact]
public void DoesNotCodeSignAppHostByDefault()
{
using (TestArtifact artifact = CreateTestDirectory())
{
string sourceAppHostMock = PrepareMockMachAppHostFile(artifact.Location);
File.SetAttributes(sourceAppHostMock, FileAttributes.ReadOnly);
string destinationFilePath = Path.Combine(artifact.Location, "DestinationAppHost.exe.mock");
string appBinaryFilePath = "Test/App/Binary/Path.dll";
HostWriter.CreateAppHost(
sourceAppHostMock,
destinationFilePath,
appBinaryFilePath,
windowsGraphicalUserInterface: false);

if (!Codesign.IsAvailable())
{
return;
}

var (exitCode, stdErr) = Codesign.Run("-d", destinationFilePath);
stdErr.Should().Contain($"{Path.GetFullPath(destinationFilePath)}: code object is not signed at all");
}
}

[Theory]
[InlineData("")]
[InlineData("dir with spaces")]
Expand Down Expand Up @@ -339,6 +312,32 @@ public void CodeSignMachOAppHost(string subdir)
}
}
}

[Fact]
public void DoesNotCodeSignAppHostByDefault()
{
using (TestArtifact artifact = CreateTestDirectory())
{
string sourceAppHostMock = PrepareMockMachAppHostFile(artifact.Location);
File.SetAttributes(sourceAppHostMock, FileAttributes.ReadOnly);
string destinationFilePath = Path.Combine(artifact.Location, "DestinationAppHost.exe.mock");
string appBinaryFilePath = "Test/App/Binary/Path.dll";
HostWriter.CreateAppHost(
sourceAppHostMock,
destinationFilePath,
appBinaryFilePath,
windowsGraphicalUserInterface: false);

if (!Codesign.IsAvailable())
{
return;
}

var (exitCode, stdErr) = Codesign.Run("-d", destinationFilePath);
stdErr.Should().Contain($"{Path.GetFullPath(destinationFilePath)}: code object is not signed at all");
}
}

[Fact]
public void CodeSigningFailuresThrow()
{
Expand Down

0 comments on commit 767b609

Please sign in to comment.