[corlib] Improve CancellationTokenSource test
[mono.git] / mcs / class / System.Security / Test / System.Security.Cryptography.X509Certificates / X509Certificate2UICas.cs
1 //
2 // X509Certificate2UITest.cs - CAS tests for X509Certificate2UI
3 //
4 // Author:
5 //      Sebastien Pouliot  <sebastien@ximian.com>
6 //
7 // Copyright (C) 2006 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
30 using NUnit.Framework;
31
32 using System;
33 using System.Reflection;
34 using System.Security;
35 using System.Security.Cryptography.X509Certificates;
36 using System.Security.Permissions;
37
38 using MonoTests.System.Security.Cryptography.X509Certificates;
39
40 namespace MonoCasTests.System.Security.Cryptography.X509Certificates {
41
42         [TestFixture]
43         [Category ("CAS")]
44         public class X509Certificate2UICas {
45
46                 private X509Certificate2UITest unit;
47
48                 [TestFixtureSetUp]
49                 public void FixtureSetUp ()
50                 {
51                         unit = new X509Certificate2UITest ();
52                         unit.FixtureSetUp ();
53                 }
54
55                 [Test]
56                 [UIPermission (SecurityAction.Deny, Window=UIPermissionWindow.AllWindows)]
57                 [ExpectedException (typeof (SecurityException))]
58                 public void DisplayCertificate_Deny_Unrestricted ()
59                 {
60                         unit.DisplayCertificate ();
61                 }
62
63                 [Test]
64                 [UIPermission (SecurityAction.PermitOnly, Window = UIPermissionWindow.SafeSubWindows)]
65                 [ExpectedException (typeof (SecurityException))]
66                 public void DisplayCertificate_PermitOnly_SafeSubWindows ()
67                 {
68                         unit.DisplayCertificate ();
69                 }
70
71                 [Test]
72                 [UIPermission (SecurityAction.PermitOnly, Window = UIPermissionWindow.SafeTopLevelWindows)]
73                 [Ignore ("UI would block tests")]
74                 public void DisplayCertificate_PermitOnly_SafeTopLevelWindows ()
75                 {
76                         unit.DisplayCertificate ();
77                 }
78
79                 [Test]
80                 [UIPermission (SecurityAction.PermitOnly, Window = UIPermissionWindow.SafeSubWindows)]
81                 [ExpectedException (typeof (SecurityException))]
82                 public void DisplayCertificate_IntPtr_PermitOnly_SafeSubWindows ()
83                 {
84                         unit.DisplayCertificate_IntPtr_Zero ();
85                 }
86
87                 [Test]
88                 [UIPermission (SecurityAction.PermitOnly, Window = UIPermissionWindow.SafeTopLevelWindows)]
89                 [Ignore ("UI would block tests")]
90                 public void DisplayCertificate_IntPtr_PermitOnly_SafeTopLevelWindows ()
91                 {
92                         unit.DisplayCertificate_IntPtr_Zero ();
93                 }
94
95
96                 [Test]
97                 [UIPermission (SecurityAction.Deny, Window = UIPermissionWindow.AllWindows)]
98                 [ExpectedException (typeof (SecurityException))]
99                 public void SelectFromCollection_Deny_Unrestricted ()
100                 {
101                         unit.SelectFromCollection ();
102                 }
103
104                 [Test]
105                 [UIPermission (SecurityAction.PermitOnly, Window = UIPermissionWindow.SafeSubWindows)]
106                 [ExpectedException (typeof (SecurityException))]
107                 public void SelectFromCollection_PermitOnly_SafeSubWindows ()
108                 {
109                         unit.SelectFromCollection ();
110                 }
111
112                 [Test]
113                 [UIPermission (SecurityAction.PermitOnly, Window = UIPermissionWindow.SafeTopLevelWindows)]
114                 [Ignore ("UI would block tests")]
115                 public void SelectFromCollection_PermitOnly_SafeTopLevelWindows ()
116                 {
117                         unit.SelectFromCollection ();
118                 }
119
120                 [Test]
121                 [UIPermission (SecurityAction.PermitOnly, Window = UIPermissionWindow.SafeSubWindows)]
122                 [ExpectedException (typeof (SecurityException))]
123                 public void SelectFromCollection_IntPtr_PermitOnly_SafeSubWindows ()
124                 {
125                         unit.SelectFromCollection_IntPtr_Zero ();
126                 }
127
128                 [Test]
129                 [UIPermission (SecurityAction.PermitOnly, Window = UIPermissionWindow.SafeTopLevelWindows)]
130                 [Ignore ("UI would block tests")]
131                 public void SelectFromCollection_IntPtr_PermitOnly_SafeTopLevelWindows ()
132                 {
133                         unit.SelectFromCollection_IntPtr_Zero ();
134                 }
135
136                 // the methods accepting an IntPtr are documented as having a LinkDemand
137                 // and InheritanceDemand for UnmanagedCode. InheritanceDemand doesn't make
138                 // sense as the class is sealed but we can test the LinkDemand with reflection
139
140                 [Test]
141                 [SecurityPermission (SecurityAction.Deny, UnmanagedCode = true)]
142                 [Ignore ("UI would block tests")]
143                 public void LinkDemand_DisplayCertificate_Deny_UnmanagedCode ()
144                 {
145                         Type[] types = new Type[1] { typeof (X509Certificate2) };
146                         MethodInfo mi = typeof (X509Certificate2UI).GetMethod ("DisplayCertificate", types);
147                         mi.Invoke (null, new object[1] { unit.x509 });
148                         // no LinkDemand on the DisplayCertificate(X509Certificate2) method
149                 }
150
151                 [Test]
152                 [SecurityPermission (SecurityAction.Deny, UnmanagedCode = true)]
153                 [ExpectedException (typeof (SecurityException))]
154                 public void LinkDemand_DisplayCertificate_IntPtr_Deny_UnmanagedCode ()
155                 {
156                         Type[] types = new Type[2] { typeof (X509Certificate2), typeof (IntPtr) };
157                         MethodInfo mi = typeof (X509Certificate2UI).GetMethod ("DisplayCertificate", types);
158                         mi.Invoke (null, new object[2] { unit.x509, IntPtr.Zero } );
159                 }
160
161                 [Test]
162                 [SecurityPermission (SecurityAction.PermitOnly, UnmanagedCode = true)]
163                 [UIPermission (SecurityAction.PermitOnly, Window = UIPermissionWindow.SafeTopLevelWindows)]
164                 [Ignore ("UI would block tests")]
165                 public void LinkDemand_DisplayCertificate_IntPtr_Permit ()
166                 {
167                         Type[] types = new Type[2] { typeof (X509Certificate2), typeof (IntPtr) };
168                         MethodInfo mi = typeof (X509Certificate2UI).GetMethod ("DisplayCertificate", types);
169                         mi.Invoke (null, new object[2] { unit.x509, IntPtr.Zero });
170                 }
171
172
173                 [Test]
174                 [SecurityPermission (SecurityAction.Deny, UnmanagedCode = true)]
175                 [Ignore ("UI would block tests")]
176                 public void LinkDemand_SelectFromCollection_Deny_UnmanagedCode ()
177                 {
178                         Type[] types = new Type[4] { typeof (X509Certificate2Collection), typeof (string), typeof (string), typeof (X509SelectionFlag) };
179                         MethodInfo mi = typeof (X509Certificate2UI).GetMethod ("SelectFromCollection", types);
180                         mi.Invoke (null, new object[4] { unit.coll, null, null, X509SelectionFlag.MultiSelection });
181                         // no LinkDemand on the SelectFromCollection(X509Certificate2Collection,string,string,X509SelectionFlag) method
182                 }
183
184                 [Test]
185                 [SecurityPermission (SecurityAction.Deny, UnmanagedCode = true)]
186                 [ExpectedException (typeof (SecurityException))]
187                 public void LinkDemand_SelectFromCollection_IntPtr_Deny_UnmanagedCode ()
188                 {
189                         Type[] types = new Type[5] { typeof (X509Certificate2Collection), typeof (string), typeof (string), typeof (X509SelectionFlag), typeof (IntPtr) };
190                         MethodInfo mi = typeof (X509Certificate2UI).GetMethod ("SelectFromCollection", types);
191                         mi.Invoke (null, new object[5] { unit.coll, null, null, X509SelectionFlag.MultiSelection, IntPtr.Zero });
192                 }
193
194                 [Test]
195                 [SecurityPermission (SecurityAction.PermitOnly, UnmanagedCode = true)]
196                 [UIPermission (SecurityAction.PermitOnly, Window = UIPermissionWindow.SafeTopLevelWindows)]
197                 [Ignore ("UI would block tests")]
198                 public void LinkDemand_SelectFromCollection_IntPtr_Permit ()
199                 {
200                         Type[] types = new Type[5] { typeof (X509Certificate2Collection), typeof (string), typeof (string), typeof (X509SelectionFlag), typeof (IntPtr) };
201                         MethodInfo mi = typeof (X509Certificate2UI).GetMethod ("SelectFromCollection", types);
202                         mi.Invoke (null, new object[5] { unit.coll, null, null, X509SelectionFlag.MultiSelection, IntPtr.Zero });
203                 }
204         }
205 }
206