2 // BitmapCas.cs - CAS unit tests for System.Drawing.Bitmap
5 // Sebastien Pouliot <sebastien@ximian.com>
7 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 using NUnit.Framework;
33 using System.Reflection;
34 using System.Security;
35 using System.Security.Permissions;
36 using System.Security.Policy;
38 namespace MonoCasTests.System.Drawing {
42 public class BitmapCas {
44 private MethodInfo getHbitmap1;
45 private MethodInfo getHbitmap2;
46 private MethodInfo getHicon;
49 public void FixtureSetUp ()
51 // this executes at fulltrust
52 getHbitmap1 = typeof (Bitmap).GetMethod ("GetHbitmap", new Type[0]);
53 getHbitmap2 = typeof (Bitmap).GetMethod ("GetHbitmap", new Type[1] { typeof (Color) });
54 getHicon = typeof (Bitmap).GetMethod ("GetHicon");
60 if (!SecurityManager.SecurityEnabled)
61 Assert.Ignore ("SecurityManager.SecurityEnabled is OFF");
65 [SecurityPermission (SecurityAction.Deny, UnmanagedCode = true)]
66 public void GetHbitmap ()
68 Bitmap b = new Bitmap (10, 10);
70 Assert.IsTrue (b.GetHbitmap () != IntPtr.Zero, "GetHbitmap");
72 catch (NotImplementedException) {
73 // not available on Mono
76 Assert.IsTrue (b.GetHbitmap (Color.Aqua) != IntPtr.Zero, "GetHbitmap(Color)");
78 catch (NotImplementedException) {
79 // not available on Mono
84 [SecurityPermission (SecurityAction.Deny, UnmanagedCode = true)]
85 public void GetHicon ()
87 Bitmap b = new Bitmap (10, 10);
89 Assert.IsTrue (b.GetHicon () != IntPtr.Zero, "GetHicon");
91 catch (NotImplementedException) {
92 // not available on Mono
96 // we use reflection to call Bitmap as it's GetHbitmap and GetHicon methods
97 // are protected by a LinkDemand (which will be converted into full demand,
98 // i.e. a stack walk) when reflection is used (i.e. it gets testable).
101 [SecurityPermission (SecurityAction.Deny, UnmanagedCode = true)]
102 [ExpectedException (typeof (SecurityException))]
103 public void GetHbitmap_Empty_LinkDemand ()
105 // requires FullTrust, so denying anything break the requirements
106 Assert.IsNotNull (getHbitmap1, "GetHbitmap");
107 Bitmap b = new Bitmap (10, 10);
108 getHbitmap1.Invoke (b, null);
112 [SecurityPermission (SecurityAction.Deny, UnmanagedCode = true)]
113 [ExpectedException (typeof (SecurityException))]
114 public void GetHbitmap_Color_LinkDemand ()
116 // requires FullTrust, so denying anything break the requirements
117 Assert.IsNotNull (getHbitmap2, "GetHbitmap(Color)");
118 Bitmap b = new Bitmap (10, 10);
119 getHbitmap2.Invoke (b, new object[1] { Color.Aqua });
123 [SecurityPermission (SecurityAction.Deny, UnmanagedCode = true)]
124 [ExpectedException (typeof (SecurityException))]
125 public void GetHicon_LinkDemand ()
127 // requires FullTrust, so denying anything break the requirements
128 Assert.IsNotNull (getHicon, "GetHicon");
129 Bitmap b = new Bitmap (10, 10);
130 getHicon.Invoke (b, null);