Skip to content

Commit

Permalink
Merge pull request #6 from ghost1face/expand-support
Browse files Browse the repository at this point in the history
Expand support
  • Loading branch information
ghost1face authored Jan 18, 2022
2 parents de23b8e + 725d361 commit 853651b
Show file tree
Hide file tree
Showing 30 changed files with 603 additions and 150 deletions.
1 change: 1 addition & 0 deletions .github/CODEOWNERS
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* @ghost1face
28 changes: 28 additions & 0 deletions .github/workflows/dotnet.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
name: .NET

on:
push:
branches: [ master ]
pull_request:
branches: [ master ]

jobs:
build:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
- name: Setup .NET
uses: actions/setup-dotnet@v1
with:
dotnet-version: '6.0.x'
- name: Restore dependencies
working-directory: ./src
run: dotnet restore
- name: Build
working-directory: ./src
run: dotnet build --no-restore --configuration Release
- name: Test
working-directory: ./src
run: dotnet test --no-build --verbosity normal --configuration Release
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

netstandard library for detecting file types by 'magic numbers', similar to the `file` command in Linux/Unix. Useful for validating file uploads instead of trusting file extensions.

[![.NET](https://github.com/ghost1face/FileTypeInterrogator/actions/workflows/dotnet.yml/badge.svg?branch=master)](https://github.com/ghost1face/FileTypeInterrogator/actions/workflows/dotnet.yml)
[![NuGet](https://img.shields.io/nuget/v/FileTypeInterrogator.svg?style=flat-square&label=nuget)](https://www.nuget.org/packages/FileTypeInterrogator/)

# Usage
Expand Down
102 changes: 25 additions & 77 deletions src/FileTypeInterrogator.Tests/FileTypeInterrogator.Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,80 +1,28 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="..\packages\MSTest.TestAdapter.1.3.2\build\net45\MSTest.TestAdapter.props" Condition="Exists('..\packages\MSTest.TestAdapter.1.3.2\build\net45\MSTest.TestAdapter.props')" />
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{8181D724-8886-4541-8819-9FF208A28074}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>FileTypeInterrogator.Tests</RootNamespace>
<AssemblyName>FileTypeInterrogator.Tests</AssemblyName>
<TargetFrameworkVersion>v4.6</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<ProjectTypeGuids>{3AC096D0-A1C2-E12C-1390-A8335801FDAB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">15.0</VisualStudioVersion>
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
<ReferencePath>$(ProgramFiles)\Common Files\microsoft shared\VSTT\$(VisualStudioVersion)\UITestExtensionPackages</ReferencePath>
<IsCodedUITest>False</IsCodedUITest>
<TestProjectType>UnitTest</TestProjectType>
<NuGetPackageImportStamp>
</NuGetPackageImportStamp>
<TargetFrameworkProfile />
<RestoreProjectStyle>PackageReference</RestoreProjectStyle>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="Microsoft.VisualStudio.TestPlatform.TestFramework, Version=14.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\MSTest.TestFramework.1.3.2\lib\net45\Microsoft.VisualStudio.TestPlatform.TestFramework.dll</HintPath>
</Reference>
<Reference Include="Microsoft.VisualStudio.TestPlatform.TestFramework.Extensions, Version=14.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\MSTest.TestFramework.1.3.2\lib\net45\Microsoft.VisualStudio.TestPlatform.TestFramework.Extensions.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
</ItemGroup>
<ItemGroup>
<Compile Include="FileTypeInterrogatorTests.cs" />
<Compile Include="FileTypeInterrogatorTests_Alias.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
<None Include="TestFiles\*">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\FileTypeInterrogator\FileTypeInterrogator.csproj">
<Project>{e5333901-a30a-45d0-b787-bc2f58208f9f}</Project>
<Name>FileTypeInterrogator</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(VSToolsPath)\TeamTest\Microsoft.TestTools.targets" Condition="Exists('$(VSToolsPath)\TeamTest\Microsoft.TestTools.targets')" />
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<ErrorText>This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
<TargetFrameworks>netcoreapp2.1;netcoreapp3.1;net5.0;net6.0</TargetFrameworks>
<IsPackable>false</IsPackable>
</PropertyGroup>
<Error Condition="!Exists('..\packages\MSTest.TestAdapter.1.3.2\build\net45\MSTest.TestAdapter.props')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\MSTest.TestAdapter.1.3.2\build\net45\MSTest.TestAdapter.props'))" />
<Error Condition="!Exists('..\packages\MSTest.TestAdapter.1.3.2\build\net45\MSTest.TestAdapter.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\MSTest.TestAdapter.1.3.2\build\net45\MSTest.TestAdapter.targets'))" />
</Target>
<Import Project="..\packages\MSTest.TestAdapter.1.3.2\build\net45\MSTest.TestAdapter.targets" Condition="Exists('..\packages\MSTest.TestAdapter.1.3.2\build\net45\MSTest.TestAdapter.targets')" />

<ItemGroup>
<PackageReference Include="coverlet.collector" Version="1.2.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.0.0" />
<PackageReference Include="MSTest.TestAdapter" Version="2.2.8" />
<PackageReference Include="MSTest.TestFramework" Version="2.2.8" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\FileTypeInterrogator\FileTypeInterrogator.csproj" />
</ItemGroup>

<ItemGroup>
<None Update="TestFiles\*.*">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>

</Project>
90 changes: 55 additions & 35 deletions src/FileTypeInterrogator.Tests/FileTypeInterrogatorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,89 +44,109 @@ public void CanDetectUTF8BOM()
}

[DataTestMethod]
[DataRow("PDF", DisplayName = "PDF Test")]
[DataRow("FDF", DisplayName = "FDF Test")]
[DataRow("pdf", DisplayName = "PDF Test")]
[DataRow("fdf", DisplayName = "FDF Test")]
public void CanDetectAdobe(string extension)
{
DetectType(extension);
}

[DataTestMethod]
[DataRow("BMP", DisplayName = "BMP Test")]
[DataRow("GIF", DisplayName = "GIF Test")]
[DataRow("ICO", DisplayName = "ICO Test")]
[DataRow("JP2", DisplayName = "JP2 Test")]
[DataRow("JPG", DisplayName = "JPG Test")]
[DataRow("PNG", DisplayName = "PNG Test")]
[DataRow("PSD", DisplayName = "PSD Test")]
[DataRow("TIF", DisplayName = "TIF Test")]
[DataRow("ai", DisplayName = "AI Test")]
[DataRow("bmp", DisplayName = "BMP Test")]
[DataRow("gif", DisplayName = "GIF Test")]
[DataRow("ico", DisplayName = "ICO Test")]
[DataRow("jp2", DisplayName = "JP2 Test")]
[DataRow("jpg", DisplayName = "JPG Test")]
[DataRow("pcx", DisplayName = "PCX Test")]
[DataRow("png", DisplayName = "PNG Test")]
[DataRow("psd", DisplayName = "PSD Test")]
[DataRow("tif", DisplayName = "TIF Test")]
[DataRow("webp", DisplayName = "WEBP Test")]
public void CanDetectImages(string extension)
{
DetectType(extension);
}

[DataTestMethod]
[DataRow("3GP", DisplayName = "3GP Test")]
[DataRow("AVI", DisplayName = "AVI Test")]
[DataRow("FLV", DisplayName = "FLV Test")]
[DataRow("MID", DisplayName = "MID Test")]
[DataRow("MP4", DisplayName = "MP4 Test")]
[DataRow("WMV", DisplayName = "WMV Test")]
[DataRow("3gp", DisplayName = "3GP Test")]
[DataRow("avi", DisplayName = "AVI Test")]
[DataRow("flv", DisplayName = "FLV Test")]
[DataRow("mid", DisplayName = "MID Test")]
[DataRow("mkv", DisplayName = "MKV Test")]
[DataRow("mp4", DisplayName = "MP4 Test")]
[DataRow("webm", DisplayName = "WEBM Test")]
[DataRow("wmv", DisplayName = "WMV Test")]
public void CanDetectVideo(string extension)
{
DetectType(extension);
}

[DataTestMethod]
[DataRow("AC3", DisplayName = "AC3 Test")]
[DataRow("AIFF", DisplayName = "AIFF Test")]
[DataRow("FLAC", DisplayName = "FLAC Test")]
[DataRow("MP3", DisplayName = "MP3 Test")]
[DataRow("OGG", DisplayName = "OGG Test")]
[DataRow("RA", DisplayName = "RA Test")]
[DataRow("ac3", DisplayName = "AC3 Test")]
[DataRow("aiff", DisplayName = "AIFF Test")]
[DataRow("flac", DisplayName = "FLAC Test")]
[DataRow("mp3", DisplayName = "MP3 Test")]
[DataRow("ogg", DisplayName = "OGG Test")]
[DataRow("ra", DisplayName = "RA Test")]
[DataRow("wav", DisplayName = "WAV Test")]
public void CanDetectAudio(string extension)
{
DetectType(extension);
}

[DataTestMethod]
[DataRow("DOC", DisplayName = "DOC Test")]
[DataRow("DOCX", DisplayName = "DOC Test")]
[DataRow("PPT", DisplayName = "PPT Test")]
[DataRow("PPTX", DisplayName = "PPTX Test")]
[DataRow("XLS", DisplayName = "XLS Test")]
[DataRow("XLSX", DisplayName = "XLSX Test")]
[DataRow("doc", DisplayName = "DOC Test")]
[DataRow("docx", DisplayName = "DOC Test")]
[DataRow("odp", DisplayName = "ODP Test")]
[DataRow("odt", DisplayName = "ODT Test")]
[DataRow("ppt", DisplayName = "PPT Test")]
[DataRow("pptx", DisplayName = "PPTX Test")]
[DataRow("rtf", DisplayName = "RTF Test")]
[DataRow("xls", DisplayName = "XLS Test")]
[DataRow("xlsx", DisplayName = "XLSX Test")]
public void CanDetectOffice(string extension)
{
DetectType(extension);
}

[DataTestMethod]
[DataRow("OTF", DisplayName = "OTF Test")]
[DataRow("TTF", DisplayName = "TTF Test")]
[DataRow("WOFF", DisplayName = "WOFF Test")]
[DataRow("otf", DisplayName = "OTF Test")]
[DataRow("ttf", DisplayName = "TTF Test")]
[DataRow("woff", DisplayName = "WOFF Test")]
public void CanDetectFont(string extension)
{
DetectType(extension);
}

[DataTestMethod]
[DataRow("7Z", DisplayName = "7Z Test")]
[DataRow("RAR", DisplayName = "RAR Test")]
[DataRow("ZIP", DisplayName = "ZIP Test")]
[DataRow("7z", DisplayName = "7Z Test")]
[DataRow("gz", DisplayName = "GZ Test")]
[DataRow("rar", DisplayName = "RAR Test")]
[DataRow("zip", DisplayName = "ZIP Test")]
public void CanDetectCompressed(string extension)
{
DetectType(extension);
}

[DataTestMethod]
[DataRow("eml", DisplayName = "EML Test")]
[DataRow("vcf", DisplayName = "VCF Test")]
public void CanDetectOther(string extension)
{
DetectType(extension);
}

private void DetectType(string extension)
{
DetectType(extension, result =>
{
Assert.IsNotNull(result);
Assert.IsTrue(
result.FileType.Equals(extension, StringComparison.OrdinalIgnoreCase) ||
result.Alias?.Any(a => a.Equals(extension, StringComparison.OrdinalIgnoreCase)) == true);
result.Alias?.Any(a => a.Equals(extension, StringComparison.OrdinalIgnoreCase)) == true,
"{0} and/or {1} do not equal {2}",
result.FileType, result.Alias?.FirstOrDefault(), extension);
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public void Init()
[TestMethod]
public void CanDetectAlias_Jpg()
{
var filePath = GetFileByType("JPG");
var filePath = GetFileByType("jpg");
var fileContents = File.ReadAllBytes(filePath);

var result = fileTypeInterrogator.IsType(fileContents, "jpg");
Expand All @@ -28,7 +28,7 @@ public void CanDetectAlias_Jpg()
[TestMethod]
public void CanDetectAlias_Jpeg()
{
var filePath = GetFileByType("JPG");
var filePath = GetFileByType("jpg");
var fileContents = File.ReadAllBytes(filePath);

var result = fileTypeInterrogator.IsType(fileContents, "jpeg");
Expand All @@ -39,7 +39,7 @@ public void CanDetectAlias_Jpeg()
[TestMethod]
public void CanDetectJpg_By_MimeType()
{
var filePath = GetFileByType("JPG");
var filePath = GetFileByType("jpg");
var fileContents = File.ReadAllBytes(filePath);

var result = fileTypeInterrogator.IsType(fileContents, "image/jpeg");
Expand Down
20 changes: 0 additions & 20 deletions src/FileTypeInterrogator.Tests/Properties/AssemblyInfo.cs

This file was deleted.

Binary file modified src/FileTypeInterrogator.Tests/TestFiles/XLS.xls
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Loading

0 comments on commit 853651b

Please sign in to comment.