Skip to content

Commit

Permalink
Merge pull request dotnet/corefx#21707 from hughbe/add-font-file-compat
Browse files Browse the repository at this point in the history
Fix missing .NET Core argument validation for PrivateFontCollection.AddFontFile

Commit migrated from dotnet/corefx@5fd039d
  • Loading branch information
mellinoe committed Jul 3, 2017
2 parents 98b1843 + 48bbe45 commit c248d73
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 9 deletions.
Expand Up @@ -5,6 +5,7 @@
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Globalization;
using System.IO;

namespace System.Drawing.Text
{
Expand Down Expand Up @@ -56,6 +57,8 @@ protected override void Dispose(bool disposing)
/// </summary>
public void AddFontFile(string filename)
{
Path.GetFullPath(filename);

int status = SafeNativeMethods.Gdip.GdipPrivateAddFontFile(new HandleRef(this, _nativeFontCollection), filename);
SafeNativeMethods.Gdip.CheckStatus(status);

Expand Down
Expand Up @@ -5,6 +5,7 @@
using System.Drawing.Tests;
using System.IO;
using System.Runtime.InteropServices;
using System.Security.Permissions;
using Xunit;

namespace System.Drawing.Text.Tests
Expand All @@ -21,7 +22,7 @@ public void Ctor_Default()
}

[ConditionalFact(nameof(PlatformDetection) + "." + nameof(PlatformDetection.IsNotWindowsNanoServer))]
public void AddFontFile_FontFile_Success()
public void AddFontFile_AbsolutePath_Success()
{
// GDI+ on Windows 7 incorrectly throws a FileNotFoundException.
if (PlatformDetection.IsWindows7)
Expand All @@ -34,13 +35,73 @@ public void AddFontFile_FontFile_Success()
fontCollection.AddFontFile(Helpers.GetTestBitmapPath("empty.file"));
fontCollection.AddFontFile(Helpers.GetTestFontPath("CodeNewRoman.otf"));

FontFamily font = Assert.Single(fontCollection.Families);
Assert.Equal("Code New Roman", font.Name);
FontFamily fontFamily = Assert.Single(fontCollection.Families);
Assert.Equal("Code New Roman", fontFamily.Name);
}
}

[ConditionalFact(nameof(PlatformDetection) + "." + nameof(PlatformDetection.IsNotWindowsNanoServer))]
public void AddFontFile_RelativePath_Success()
{
// GDI+ on Windows 7 incorrectly throws a FileNotFoundException.
if (PlatformDetection.IsWindows7)
{
return;
}

using (var fontCollection = new PrivateFontCollection())
{
string relativePath = Path.Combine("fonts", "CodeNewRoman.ttf");
fontCollection.AddFontFile(relativePath);

FontFamily fontFamily = Assert.Single(fontCollection.Families);
Assert.Equal("Code New Roman", fontFamily.Name);
}
}

[ConditionalFact(nameof(PlatformDetection) + "." + nameof(PlatformDetection.IsNotWindowsNanoServer))]
public void AddFontFile_SamePathMultipleTimes_FamiliesContainsOnlyOneFont()
{
// GDI+ on Windows 7 incorrectly throws a FileNotFoundException.
if (PlatformDetection.IsWindows7)
{
return;
}

using (var fontCollection = new PrivateFontCollection())
{
fontCollection.AddFontFile(Helpers.GetTestFontPath("CodeNewRoman.ttf"));
fontCollection.AddFontFile(Helpers.GetTestFontPath("CodeNewRoman.ttf"));

FontFamily fontFamily = Assert.Single(fontCollection.Families);
Assert.Equal("Code New Roman", fontFamily.Name);
}
}

[ConditionalFact(nameof(PlatformDetection) + "." + nameof(PlatformDetection.IsNotWindowsNanoServer))]
public void AddFontFile_SameNameMultipleTimes_FamiliesContainsFirstFontOnly()
{
// GDI+ on Windows 7 incorrectly throws a FileNotFoundException.
if (PlatformDetection.IsWindows7)
{
return;
}

using (var fontCollection = new PrivateFontCollection())
{
fontCollection.AddFontFile(Helpers.GetTestFontPath("CodeNewRoman.ttf"));
fontCollection.AddFontFile(Helpers.GetTestFontPath("CodeNewRoman.otf"));

// Verify that the first file is used by checking that it contains metadata
// associated with CodeNewRoman.ttf.
const int FrenchLCID = 1036;
FontFamily fontFamily = Assert.Single(fontCollection.Families);
Assert.Equal("Code New Roman", fontFamily.Name);
Assert.Equal("Bonjour", fontFamily.GetName(FrenchLCID));
}
}

[ConditionalFact(nameof(PlatformDetection) + "." + nameof(PlatformDetection.IsNotWindowsNanoServer))]
[ActiveIssue(21558, TargetFrameworkMonikers.Netcoreapp)]
public void AddFontFile_NullFileName_ThrowsArgumentNullException()
{
using (var fontCollection = new PrivateFontCollection())
Expand All @@ -50,18 +111,16 @@ public void AddFontFile_NullFileName_ThrowsArgumentNullException()
}

[ConditionalFact(nameof(PlatformDetection) + "." + nameof(PlatformDetection.IsNotWindowsNanoServer))]
[ActiveIssue(21558, TargetFrameworkMonikers.Netcoreapp)]
public void AddFontFile_InvalidPath_ThrowsArgumentException()
{
using (var fontCollection = new PrivateFontCollection())
{
AssertExtensions.Throws<ArgumentException>(null, () => fontCollection.AddFontFile(string.Empty));
AssertExtensions.Throws<ArgumentException>("path", null, () => fontCollection.AddFontFile(string.Empty));
}
}

[ConditionalFact(nameof(PlatformDetection) + "." + nameof(PlatformDetection.IsNotWindowsNanoServer))]
[ActiveIssue(21558, TargetFrameworkMonikers.Netcoreapp)]
public void AddFontFile_NoSuchFilePath_ThrowsArgumentException()
public void AddFontFile_NoSuchFilePath_ThrowsFileNotFoundException()
{
using (var fontCollection = new PrivateFontCollection())
{
Expand All @@ -70,7 +129,6 @@ public void AddFontFile_NoSuchFilePath_ThrowsArgumentException()
}

[ConditionalFact(nameof(PlatformDetection) + "." + nameof(PlatformDetection.IsNotWindowsNanoServer))]
[ActiveIssue(21558, TargetFrameworkMonikers.Netcoreapp)]
public void AddFontFile_LongFilePath_ThrowsPathTooLongException()
{
using (var fontCollection = new PrivateFontCollection())
Expand Down

0 comments on commit c248d73

Please sign in to comment.