2 // X509Certificate2UITest.cs - CAS tests for X509Certificate2UI
5 // Sebastien Pouliot <sebastien@ximian.com>
7 // Copyright (C) 2006 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.
30 using NUnit.Framework;
33 using System.Reflection;
34 using System.Security;
35 using System.Security.Cryptography.X509Certificates;
36 using System.Security.Permissions;
38 using MonoTests.System.Security.Cryptography.X509Certificates;
40 namespace MonoCasTests.System.Security.Cryptography.X509Certificates {
44 public class X509Certificate2UICas {
46 private X509Certificate2UITest unit;
49 public void FixtureSetUp ()
51 unit = new X509Certificate2UITest ();
56 [UIPermission (SecurityAction.Deny, Window=UIPermissionWindow.AllWindows)]
57 [ExpectedException (typeof (SecurityException))]
58 public void DisplayCertificate_Deny_Unrestricted ()
60 unit.DisplayCertificate ();
64 [UIPermission (SecurityAction.PermitOnly, Window = UIPermissionWindow.SafeSubWindows)]
65 [ExpectedException (typeof (SecurityException))]
66 public void DisplayCertificate_PermitOnly_SafeSubWindows ()
68 unit.DisplayCertificate ();
72 [UIPermission (SecurityAction.PermitOnly, Window = UIPermissionWindow.SafeTopLevelWindows)]
73 [Ignore ("UI would block tests")]
74 public void DisplayCertificate_PermitOnly_SafeTopLevelWindows ()
76 unit.DisplayCertificate ();
80 [UIPermission (SecurityAction.PermitOnly, Window = UIPermissionWindow.SafeSubWindows)]
81 [ExpectedException (typeof (SecurityException))]
82 public void DisplayCertificate_IntPtr_PermitOnly_SafeSubWindows ()
84 unit.DisplayCertificate_IntPtr_Zero ();
88 [UIPermission (SecurityAction.PermitOnly, Window = UIPermissionWindow.SafeTopLevelWindows)]
89 [Ignore ("UI would block tests")]
90 public void DisplayCertificate_IntPtr_PermitOnly_SafeTopLevelWindows ()
92 unit.DisplayCertificate_IntPtr_Zero ();
97 [UIPermission (SecurityAction.Deny, Window = UIPermissionWindow.AllWindows)]
98 [ExpectedException (typeof (SecurityException))]
99 public void SelectFromCollection_Deny_Unrestricted ()
101 unit.SelectFromCollection ();
105 [UIPermission (SecurityAction.PermitOnly, Window = UIPermissionWindow.SafeSubWindows)]
106 [ExpectedException (typeof (SecurityException))]
107 public void SelectFromCollection_PermitOnly_SafeSubWindows ()
109 unit.SelectFromCollection ();
113 [UIPermission (SecurityAction.PermitOnly, Window = UIPermissionWindow.SafeTopLevelWindows)]
114 [Ignore ("UI would block tests")]
115 public void SelectFromCollection_PermitOnly_SafeTopLevelWindows ()
117 unit.SelectFromCollection ();
121 [UIPermission (SecurityAction.PermitOnly, Window = UIPermissionWindow.SafeSubWindows)]
122 [ExpectedException (typeof (SecurityException))]
123 public void SelectFromCollection_IntPtr_PermitOnly_SafeSubWindows ()
125 unit.SelectFromCollection_IntPtr_Zero ();
129 [UIPermission (SecurityAction.PermitOnly, Window = UIPermissionWindow.SafeTopLevelWindows)]
130 [Ignore ("UI would block tests")]
131 public void SelectFromCollection_IntPtr_PermitOnly_SafeTopLevelWindows ()
133 unit.SelectFromCollection_IntPtr_Zero ();
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
141 [SecurityPermission (SecurityAction.Deny, UnmanagedCode = true)]
142 [Ignore ("UI would block tests")]
143 public void LinkDemand_DisplayCertificate_Deny_UnmanagedCode ()
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
152 [SecurityPermission (SecurityAction.Deny, UnmanagedCode = true)]
153 [ExpectedException (typeof (SecurityException))]
154 public void LinkDemand_DisplayCertificate_IntPtr_Deny_UnmanagedCode ()
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 } );
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 ()
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 });
174 [SecurityPermission (SecurityAction.Deny, UnmanagedCode = true)]
175 [Ignore ("UI would block tests")]
176 public void LinkDemand_SelectFromCollection_Deny_UnmanagedCode ()
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
185 [SecurityPermission (SecurityAction.Deny, UnmanagedCode = true)]
186 [ExpectedException (typeof (SecurityException))]
187 public void LinkDemand_SelectFromCollection_IntPtr_Deny_UnmanagedCode ()
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 });
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 ()
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 });