Skip to content

Commit

Permalink
File type check on macosx (if file is "dylib") (#4206)
Browse files Browse the repository at this point in the history
* added file type check on macosx (if file is "dylib" AKA has "dylib" extension)

* Added unit test and fixes for empty path

* refactored to test Path and suffix ("extension") GetExtension throws

* inclusively covering all PE extensions (not excluding dylib only)

* fixing tests accoring new logic (reverting to old)

* style fixes

* Update src/Cake.Core/IO/FileExtensions.cs

Co-authored-by: Mattias Karlsson <[email protected]>

* Add `.` to extensions

Co-authored-by: Mattias Karlsson <[email protected]>

---------

Co-authored-by: Mattias Karlsson <[email protected]>
  • Loading branch information
moljac and devlead authored Oct 1, 2023
1 parent 2ab3fb4 commit 4ae30f3
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/Cake.Core.Tests/Unit/IO/FileExtensionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,21 @@ public void Should_Return_False_When_File_Is_Not_A_Valid_PE()
// Then
Assert.False(FileExtensions.IsClrAssembly(file));
}

[Fact]
public void Should_Return_False_When_File_Is_MacOS_MachO_dylib()
{
// Given
var file = Substitute.For<IFile>();

// When
file.Exists.Returns(true);
file.Path.Returns(new string("fullname.dylib"));
file.Length.Returns(_invalidPeBytes.Length);

// Then
Assert.False(FileExtensions.IsClrAssembly(file));
}
}
}
}
22 changes: 22 additions & 0 deletions src/Cake.Core/IO/FileExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,28 @@ public static bool IsClrAssembly(this IFile file)
return false;
}

// Is known extension?
switch (file.Path?.GetExtension().ToLowerInvariant())
{
case ".dll":
case ".exe":
case ".sys":
case ".tsp":
case ".acm":
case ".ax":
case ".cpl":
case ".drv":
case ".efi":
case ".mui":
case ".ocx":
case ".scr":
case null:
break;

default:
return false;
}

using (var fs = file.OpenRead())
{
using (var reader = new System.IO.BinaryReader(fs))
Expand Down

0 comments on commit 4ae30f3

Please sign in to comment.