Skip to content

Commit

Permalink
Clean up tests and remove unnecessary usings
Browse files Browse the repository at this point in the history
Use correct 'using' syntax in test
  • Loading branch information
jtschuster committed Oct 10, 2024
1 parent 1c5121d commit 5268735
Show file tree
Hide file tree
Showing 21 changed files with 88 additions and 194 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Runtime.CompilerServices;

[assembly: InternalsVisibleTo("Microsoft.NET.HostModel.Tests, PublicKey="
+ "00240000048000009400000006020000"
+ "00240000525341310004000001000100"
+ "b5fc90e7027f67871e773a8fde8938c8"
+ "1dd402ba65b9201d60593e96c492651e"
+ "889cc13f1415ebb53fac1131ae0bd333"
+ "c5ee6021672d9718ea31a8aebd0da007"
+ "2f25d87dba6fc90ffd598ed4da35e44c"
+ "398c454307e8e33b8426143daec9f596"
+ "836f97c8f74750e5975c64e2189f45de"
+ "f46b2a2b1247adc3652bf5c308055da9")]

[assembly: InternalsVisibleTo("Microsoft.NET.HostModel.MachO.Tests, PublicKey="
+ "00240000048000009400000006020000"
+ "00240000525341310004000001000100"
+ "b5fc90e7027f67871e773a8fde8938c8"
+ "1dd402ba65b9201d60593e96c492651e"
+ "889cc13f1415ebb53fac1131ae0bd333"
+ "c5ee6021672d9718ea31a8aebd0da007"
+ "2f25d87dba6fc90ffd598ed4da35e44c"
+ "398c454307e8e33b8426143daec9f596"
+ "836f97c8f74750e5975c64e2189f45de"
+ "f46b2a2b1247adc3652bf5c308055da9")]
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,10 @@
// 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;
using System.Security.Cryptography;
using System.Text;
using Microsoft.NET.HostModel.MachO;
using Microsoft.NET.HostModel.MachO.Streams;

namespace Microsoft.NET.HostModel.MachO.CodeSign.Blobs
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using Microsoft.NET.HostModel.MachO.CodeSign.Blobs;

namespace Microsoft.NET.HostModel.MachO.CodeSign.Blobs
{
[GenerateReaderWriter]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using Microsoft.NET.HostModel.MachO;
using Microsoft.NET.HostModel.MachO.Streams;

namespace Microsoft.NET.HostModel.MachO.CodeSign
{
Expand All @@ -28,7 +23,7 @@ public MachCodeSignature SetCodeSignatureSize(uint codeSignatureSize)
return UpdateCodeSignatureLayout(codeSignatureSize);
}

public void EnsureSpace()
public void AllocateSpace()
{
var codeSignature = objectFile.LoadCommands.OfType<MachCodeSignature>().Single();
var requiredSize = codeSignature.FileOffset + codeSignature.FileSize;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using Microsoft.NET.HostModel.MachO.BinaryFormat;

namespace Microsoft.NET.HostModel.MachO
{
internal sealed class MachCustomLoadCommand : MachLoadCommand
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Text;
using Microsoft.NET.HostModel.MachO.BinaryFormat;

namespace Microsoft.NET.HostModel.MachO
{
internal abstract class MachDylibCommand : MachLoadCommand
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Text;
using Microsoft.NET.HostModel.MachO.BinaryFormat;

namespace Microsoft.NET.HostModel.MachO
{
internal sealed class MachEntrypointCommand : MachLoadCommand
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Numerics;
using Microsoft.NET.HostModel.MachO.Streams;

namespace Microsoft.NET.HostModel.MachO
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.Collections.Generic;
using System.Diagnostics;

namespace Microsoft.NET.HostModel.MachO
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

using Microsoft.NET.HostModel.MachO.Streams;
using System.IO;
using System;
using System.Collections.Generic;
using System.Linq;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Microsoft.NET.HostModel.MachO
{
internal sealed class MachRelocation
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Diagnostics;

namespace Microsoft.NET.HostModel.MachO
{
internal sealed class MachSymbol
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using System;
using System.Buffers.Binary;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.IO;
using System.Linq;
Expand Down
56 changes: 11 additions & 45 deletions src/installer/managed/Microsoft.NET.HostModel/MachO/Signer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,33 +7,7 @@
using System.IO;
using System.Linq;
using Microsoft.NET.HostModel.MachO.CodeSign.Blobs;
using Microsoft.NET.HostModel.MachO;
using Microsoft.NET.HostModel.MachO.Streams;
using System.Runtime.CompilerServices;

[assembly: InternalsVisibleTo("Microsoft.NET.HostModel.Tests, PublicKey="
+ "00240000048000009400000006020000"
+ "00240000525341310004000001000100"
+ "b5fc90e7027f67871e773a8fde8938c8"
+ "1dd402ba65b9201d60593e96c492651e"
+ "889cc13f1415ebb53fac1131ae0bd333"
+ "c5ee6021672d9718ea31a8aebd0da007"
+ "2f25d87dba6fc90ffd598ed4da35e44c"
+ "398c454307e8e33b8426143daec9f596"
+ "836f97c8f74750e5975c64e2189f45de"
+ "f46b2a2b1247adc3652bf5c308055da9")]

[assembly: InternalsVisibleTo("Microsoft.NET.HostModel.MachO.Tests, PublicKey="
+ "00240000048000009400000006020000"
+ "00240000525341310004000001000100"
+ "b5fc90e7027f67871e773a8fde8938c8"
+ "1dd402ba65b9201d60593e96c492651e"
+ "889cc13f1415ebb53fac1131ae0bd333"
+ "c5ee6021672d9718ea31a8aebd0da007"
+ "2f25d87dba6fc90ffd598ed4da35e44c"
+ "398c454307e8e33b8426143daec9f596"
+ "836f97c8f74750e5975c64e2189f45de"
+ "f46b2a2b1247adc3652bf5c308055da9")]

namespace Microsoft.NET.HostModel.MachO.CodeSign
{
Expand Down Expand Up @@ -68,17 +42,6 @@ private static HashType[] GetHashTypesForObjectFile(MachObjectFile objectFile)
return new[] { HashType.SHA1, HashType.SHA256 };
}

private static void AdHocSignMachO(string executablePath)
{
var bundleIdentifier = Path.GetFileName(executablePath);

using (FileStream inputFile = File.Open(executablePath, FileMode.Open, FileAccess.ReadWrite))
{
long newSize = AdHocSignMachO(inputFile, bundleIdentifier);
inputFile.SetLength(newSize);
}
}

public static long AdHocSignMachO(Stream machStream, string bundleId)
{
ExecutableSegmentFlags executableSegmentFlags = 0;
Expand Down Expand Up @@ -116,8 +79,7 @@ public static long AdHocSignMachO(Stream machStream, string bundleId)
{
codeSignatureCommand = codeSignAllocate.SetCodeSignatureSize((uint)signatureSizeEstimate);
}
// The streams passed in should allocate extra space at the end of the stream for the new signature
codeSignAllocate.EnsureSpace();
codeSignAllocate.AllocateSpace();

using (var tmpStream = new MemoryStream())
{
Expand Down Expand Up @@ -155,8 +117,6 @@ public static long AdHocSignMachO(Stream machStream, string bundleId)
blobs.Add((CodeDirectorySpecialSlot.CmsWrapper, cmsWrapperBlob));

// TODO: Hic sunt leones (all code below)

// FIXME: Adjust the size to match LinkEdit section?
long size = blobs.Sum(b => b.Data != null ? b.Data.Length + 8 : 0);

var embeddedSignatureBuffer = new byte[12 + (blobs.Count * 8)];
Expand Down Expand Up @@ -201,15 +161,21 @@ public static long AdHocSign(Stream stream, string identifier)
return AdHocSignMachO(stream, identifier);
}

public static void AdHocSign(string path)
internal static void AdHocSign(string executablePath)
{
var attributes = File.GetAttributes(path);
var attributes = File.GetAttributes(executablePath);

if (attributes.HasFlag(FileAttributes.Directory))
{
throw new NotImplementedException("Signing bundles is not yet supported");
}
AdHocSignMachO(path);

var bundleIdentifier = Path.GetFileName(executablePath);
using (FileStream inputFile = File.Open(executablePath, FileMode.Open, FileAccess.ReadWrite))
{
long newSize = AdHocSignMachO(inputFile, bundleIdentifier);
inputFile.SetLength(newSize);
}
}

internal static bool TryRemoveCodesign(Stream inputStream, out long newSize)
Expand Down Expand Up @@ -259,7 +225,7 @@ internal static bool TryRemoveCodesign(Stream inputStream, out long newSize)
/// <summary>
/// Removes the code signature from the Mach-O file on disk.
/// </summary>
public static bool TryRemoveCodesign(string inputPath, string outputPath = null)
internal static bool TryRemoveCodesign(string inputPath, string outputPath = null)
{
if (outputPath is not null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,6 @@
<PackageReference Include="System.Reflection.Metadata" Version="$(SystemReflectionMetadataToolsetVersion)" />
<!-- The SDK distributes the live version of Json we can't reference that https://github.com/dotnet/runtime/issues/108262 -->
<PackageReference Include="System.Text.Json" Version="$(SystemTextJsonToolsetVersion)" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">
<PackageReference Include="System.Memory" Version="$(SystemMemoryVersion)" />
</ItemGroup>

Expand All @@ -47,8 +44,4 @@
ReferenceOutputAssembly="false" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' != 'netstandard2.0'">
<Compile Remove="ThrowHelpers.cs" />
</ItemGroup>

</Project>
Loading

0 comments on commit 5268735

Please sign in to comment.