Skip to content

Commit

Permalink
[release/6.0-preview7] Make System.Drawing.Common throw on Unix unles…
Browse files Browse the repository at this point in the history
…s a config switch is set (#56114)

* Make System.Drawing.Common throw on Unix unless a config switch is set

* Update src/libraries/System.Drawing.Common/src/Resources/Strings.resx

Co-authored-by: Immo Landwerth <immo@landwerth.net>

* Update src/libraries/System.Drawing.Common/src/Resources/Strings.resx

* Add test that validates the exception is thrown

* Move throw check to LibraryResolver.EnsureRegistered

* Update src/libraries/System.Drawing.Common/src/System/Drawing/GdiplusNative.Unix.cs

* Update package description

* Enable Drawing support for Unix for resources tests

* Enable drawing unix support on more tests

Co-authored-by: Santiago Fernandez Madero <safern@microsoft.com>
Co-authored-by: Immo Landwerth <immo@landwerth.net>
  • Loading branch information
3 people authored Jul 22, 2021
1 parent 91e4d50 commit b0cf153
Show file tree
Hide file tree
Showing 11 changed files with 67 additions and 8 deletions.
7 changes: 4 additions & 3 deletions src/libraries/System.Drawing.Common/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
<Import Project="..\Directory.Build.props" />
<PropertyGroup>
<StrongNameKeyId>Open</StrongNameKeyId>
<IncludePlatformAttributes>true</IncludePlatformAttributes>
<UnsupportedOSPlatforms>browser</UnsupportedOSPlatforms>
<SupportedOSPlatforms>windows</SupportedOSPlatforms>
<PackageDescription>Provides access to GDI+ graphics functionality.

Commonly Used Types:
Expand All @@ -12,6 +11,8 @@ System.Drawing.BitmapData
System.Drawing.Brush
System.Drawing.Font
System.Drawing.Graphics
System.Drawing.Icon</PackageDescription>
System.Drawing.Icon

Unix support is disabled by default. See https://aka.ms/systemdrawingnonwindows for more information.</PackageDescription>
</PropertyGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,9 @@
<data name="PlatformNotSupported_Drawing" xml:space="preserve">
<value>System.Drawing is not supported on this platform.</value>
</data>
<data name="PlatformNotSupported_Unix" xml:space="preserve">
<value>System.Drawing.Common is not supported on non-Windows platforms. See https://aka.ms/systemdrawingnonwindows for more information.</value>
</data>
<data name="PrintDocumentDesc" xml:space="preserve">
<value>Defines an object that sends output to a printer.</value>
</data>
Expand Down Expand Up @@ -464,4 +467,4 @@
<data name="SystemDrawingCommon_PlatformNotSupported" xml:space="preserve">
<value>System.Drawing.Common is not supported on this platform.</value>
</data>
</root>
</root>
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,8 @@
Link="Common\Interop\Windows\User32\Interop.LOGFONT.cs" />
<Compile Include="$(CommonPath)Interop\Windows\Gdi32\Interop.RasterOp.cs"
Link="Common\Interop\Windows\Gdi32\Interop.RasterOp.cs" />
<Compile Include="$(CommonPath)System\LocalAppContextSwitches.Common.cs"
Link="System\LocalAppContextSwitches.Common.cs" />
<Compile Include="$(CommonPath)System\Text\ValueStringBuilder.cs"
Link="Common\System\Text\ValueStringBuilder.cs" />
<Compile Include="$(CommonPath)System\Obsoletions.cs"
Expand Down Expand Up @@ -207,7 +209,7 @@
<Compile Include="System\Drawing\Internal\GpPathData.cs" />
<Compile Include="System\Drawing\Internal\GPStream.cs" />
<Compile Include="System\Drawing\Internal\SystemColorTracker.cs" />
<Compile Include="System\Drawing\LocalAppContextSwitches.cs" />
<Compile Include="System\Drawing\LocalAppContextSwitches.Windows.cs" />
<Compile Include="System\Drawing\Pen.Windows.cs" />
<Compile Include="System\Drawing\Printing\DefaultPrintController.cs" />
<Compile Include="System\Drawing\Printing\ModeField.cs" />
Expand Down Expand Up @@ -243,8 +245,6 @@
<Compile Include="Interop\Windows\Interop.Shell32.cs" />
<Compile Include="Interop\Windows\Interop.User32.cs" />
<Compile Include="Interop\Windows\Interop.Winspool.cs" />
<Compile Include="$(CommonPath)System\LocalAppContextSwitches.Common.cs"
Link="System\LocalAppContextSwitches.Common.cs" />
<Compile Include="$(CommonPath)Interop\Windows\Interop.Libraries.cs"
Link="Common\Interop\Windows\Interop.Libraries.cs" />
<Compile Include="$(CommonPath)Interop\Windows\Gdi32\Interop.CombineRgn.cs"
Expand Down Expand Up @@ -359,6 +359,7 @@
<Compile Include="System\Drawing\GdiplusNative.Unix.cs" />
<Compile Include="System\Drawing\GdiPlusStreamHelper.Unix.cs" />
<Compile Include="System\Drawing\LibX11Functions.cs" />
<Compile Include="System\Drawing\LocalAppContextSwitches.Unix.cs" />
<Compile Include="System\Drawing\MarshallingHelpers.cs" />
<Compile Include="System\Drawing\Image.Unix.cs" />
<Compile Include="System\Drawing\Pen.Unix.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ private static IntPtr DllImportResolver(string libraryName, Assembly assembly, D

internal static void EnsureRegistered()
{
// dummy call to trigger the static constructor
if (!LocalAppContextSwitches.EnableUnixSupport)
throw new PlatformNotSupportedException(SR.PlatformNotSupported_Unix);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// 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;

namespace System
{
internal static partial class LocalAppContextSwitches
{
private static int s_enableUnixSupport;
public static bool EnableUnixSupport
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get
{
return GetCachedSwitchValue(@"System.Drawing.EnableUnixSupport", ref s_enableUnixSupport);
}
}
}
}
13 changes: 13 additions & 0 deletions src/libraries/System.Drawing.Common/tests/BitmapTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using Microsoft.DotNet.RemoteExecutor;
using Microsoft.DotNet.XUnitExtensions;
using Xunit;

Expand All @@ -44,6 +45,18 @@ public static IEnumerable<object[]> Ctor_FilePath_TestData()
yield return new object[] { "16x16_nonindexed_24bit.png", 16, 16, PixelFormat.Format24bppRgb, ImageFormat.Png };
}

[PlatformSpecific(TestPlatforms.AnyUnix)]
[ConditionalFact(typeof(RemoteExecutor), nameof(RemoteExecutor.IsSupported))]
public void UnixSupportDisabledThrows()
{
RemoteExecutor.Invoke(() =>
{
AppContext.SetSwitch("System.Drawing.EnableUnixSupport", false);
TypeInitializationException exception = Assert.Throws<TypeInitializationException>(() => new Bitmap(100, 100));
Assert.IsType<PlatformNotSupportedException>(exception.InnerException);
}).Dispose();
}

[ConditionalTheory(Helpers.IsDrawingSupported)]
[MemberData(nameof(Ctor_FilePath_TestData))]
public void Ctor_FilePath(string filename, int width, int height, PixelFormat pixelFormat, ImageFormat rawFormat)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"configProperties": {
"System.Drawing.EnableUnixSupport": true
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"configProperties": {
"System.Drawing.EnableUnixSupport": true
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"configProperties": {
"System.Drawing.EnableUnixSupport": true
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"configProperties": {
"System.Drawing.EnableUnixSupport": true
}
}

0 comments on commit b0cf153

Please sign in to comment.