Skip to content

Commit

Permalink
add test (#13129)
Browse files Browse the repository at this point in the history
throw instead of crash
  • Loading branch information
mjkkirschner authored Jul 22, 2022
1 parent 375351d commit ac8a987
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 3 deletions.
14 changes: 13 additions & 1 deletion src/Libraries/CoreNodes/FileSystem.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
using System.Collections;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using Autodesk.DesignScript.Runtime;
using Dynamo.Events;
using Dynamo.Graph.Nodes;
using Dynamo.Logging;
using Dynamo.Session;
using Path = System.IO.Path;
using Rectangle = System.Drawing.Rectangle;

Expand Down Expand Up @@ -440,6 +445,13 @@ public static Bitmap FromPixels(Color[] colors, int width, int height)

private static Bitmap FromPixelsHelper(IEnumerable<Color> colors, int width, int height)
{
//if the color data is larger than will fit in the bitmap buffer
//then this can cause an access violation - so we'll throw an exception.
if(colors?.Count() > width * height)
{
throw new ArgumentOutOfRangeException("colors", Properties.Resources.BitmapOverflowError);
}

var bitmap = new Bitmap(width, height, PixelFormat.Format32bppArgb);
var data = bitmap.LockBits(
new Rectangle(0, 0, width, height),
Expand Down
11 changes: 10 additions & 1 deletion src/Libraries/CoreNodes/Properties/Resources.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions src/Libraries/CoreNodes/Properties/Resources.en-US.resx
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,9 @@
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<data name="BitmapOverflowError" xml:space="preserve">
<value>The supplied color data is too large to fit in the image bounds.</value>
</data>
<data name="EnumDateOfWeekFriday" xml:space="preserve">
<value>Friday</value>
</data>
Expand Down
3 changes: 3 additions & 0 deletions src/Libraries/CoreNodes/Properties/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,9 @@
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<data name="BitmapOverflowError" xml:space="preserve">
<value>The supplied color data is too large to fit in the image bounds.</value>
</data>
<data name="EnumDateOfWeekFriday" xml:space="preserve">
<value>Friday</value>
</data>
Expand Down
14 changes: 13 additions & 1 deletion test/Libraries/CoreNodesTests/FileTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
Expand Down Expand Up @@ -413,6 +414,17 @@ public void Image_FromPixels()

Assert.AreEqual(Image.Pixels(bmpFromRect), Image.Pixels(bmpFromFlat));
}
[Test, Category("UnitTests")]
public void Image_FromPixels_ThrowsWhenDataTooLarge()
{
const int size = 50;

var pixels = Enumerable.Repeat(Color.ByColor(System.Drawing.Color.Blue), size).ToArray();
Assert.Throws<ArgumentOutOfRangeException>(() =>
{
Image.FromPixels(pixels, 1, 1);
});
}

[Test, Category("UnitTests")]
public void Image_Dimensions()
Expand Down

0 comments on commit ac8a987

Please sign in to comment.