* PEAPI.cs: Clean up indentation. Split into ...
[mono.git] / mcs / class / System.Drawing / Test / System.Drawing / BitmapCas.cs
1 //
2 // BitmapCas.cs - CAS unit tests for System.Drawing.Bitmap
3 //
4 // Author:
5 //      Sebastien Pouliot  <sebastien@ximian.com>
6 //
7 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
8 //
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:
16 // 
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 // 
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.
27 //
28
29 using NUnit.Framework;
30
31 using System;
32 using System.Drawing;
33 using System.Reflection;
34 using System.Security;
35 using System.Security.Permissions;
36 using System.Security.Policy;
37
38 namespace MonoCasTests.System.Drawing {
39
40         [TestFixture]
41         [Category ("CAS")]
42         public class BitmapCas {
43
44                 private MethodInfo getHbitmap1;
45                 private MethodInfo getHbitmap2;
46                 private MethodInfo getHicon;
47
48                 [TestFixtureSetUp]
49                 public void FixtureSetUp ()
50                 {
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");
55                 }
56
57                 [SetUp]
58                 public void SetUp ()
59                 {
60                         if (!SecurityManager.SecurityEnabled)
61                                 Assert.Ignore ("SecurityManager.SecurityEnabled is OFF");
62                 }
63
64                 [Test]
65                 [SecurityPermission (SecurityAction.Deny, UnmanagedCode = true)]
66                 public void GetHbitmap ()
67                 {
68                         Bitmap b = new Bitmap (10, 10);
69                         try {
70                                 Assert.IsTrue (b.GetHbitmap () != IntPtr.Zero, "GetHbitmap");
71                         }
72                         catch (NotImplementedException) {
73                                 // not available on Mono
74                         }
75                         try {
76                                 Assert.IsTrue (b.GetHbitmap (Color.Aqua) != IntPtr.Zero, "GetHbitmap(Color)");
77                         }
78                         catch (NotImplementedException) {
79                                 // not available on Mono
80                         }
81                 }
82
83                 [Test]
84                 [SecurityPermission (SecurityAction.Deny, UnmanagedCode = true)]
85                 public void GetHicon ()
86                 {
87                         Bitmap b = new Bitmap (10, 10);
88                         try {
89                                 Assert.IsTrue (b.GetHicon () != IntPtr.Zero, "GetHicon");
90                         }
91                         catch (NotImplementedException) {
92                                 // not available on Mono
93                         }
94                 }
95
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).
99
100                 [Test]
101                 [SecurityPermission (SecurityAction.Deny, UnmanagedCode = true)]
102                 [ExpectedException (typeof (SecurityException))]
103                 public void GetHbitmap_Empty_LinkDemand ()
104                 {
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);
109                 }
110
111                 [Test]
112                 [SecurityPermission (SecurityAction.Deny, UnmanagedCode = true)]
113                 [ExpectedException (typeof (SecurityException))]
114                 public void GetHbitmap_Color_LinkDemand ()
115                 {
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 });
120                 }
121
122                 [Test]
123                 [SecurityPermission (SecurityAction.Deny, UnmanagedCode = true)]
124                 [ExpectedException (typeof (SecurityException))]
125                 public void GetHicon_LinkDemand ()
126                 {
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);
131                 }
132         }
133 }