2 // SystemEventsCas.cs - CAS unit tests for Microsoft.Win32.SystemEvents
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;
32 using System.Reflection;
33 using System.Security;
34 using System.Security.Permissions;
35 using Microsoft.Win32;
37 namespace MonoCasTests.Microsoft.Win32 {
41 public class SystemEventsCas {
44 public virtual void SetUp ()
46 if (!SecurityManager.SecurityEnabled)
47 Assert.Ignore ("SecurityManager.SecurityEnabled is OFF");
50 private void TimerCallback (object o, TimerElapsedEventArgs args)
55 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
56 public void Methods_Deny_Unrestricted ()
58 IntPtr timer = SystemEvents.CreateTimer (5000);
59 SystemEvents.KillTimer (timer);
62 SystemEvents.InvokeOnEventsThread (new TimerElapsedEventHandler (TimerCallback));
64 catch (NotImplementedException) {
69 private void EventCallback (object o, EventArgs args)
74 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
75 public void DisplaySettingsChanged_Deny_Unrestricted ()
78 SystemEvents.DisplaySettingsChanged += new EventHandler (EventCallback);
79 SystemEvents.DisplaySettingsChanged -= new EventHandler (EventCallback);
81 catch (NotImplementedException) {
86 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
87 public void DisplaySettingsChanging_Deny_Unrestricted ()
90 SystemEvents.DisplaySettingsChanging += new EventHandler (EventCallback);
91 SystemEvents.DisplaySettingsChanging -= new EventHandler (EventCallback);
93 catch (NotImplementedException) {
98 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
99 public void EventsThreadShutdown_Deny_Unrestricted ()
102 SystemEvents.EventsThreadShutdown += new EventHandler (EventCallback);
103 SystemEvents.EventsThreadShutdown -= new EventHandler (EventCallback);
105 catch (NotImplementedException) {
111 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
112 public void InstalledFontsChanged_Deny_Unrestricted ()
115 SystemEvents.InstalledFontsChanged += new EventHandler (EventCallback);
116 SystemEvents.InstalledFontsChanged -= new EventHandler (EventCallback);
118 catch (NotImplementedException) {
124 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
125 public void LowMemory_Deny_Unrestricted ()
128 SystemEvents.LowMemory += new EventHandler (EventCallback);
129 SystemEvents.LowMemory -= new EventHandler (EventCallback);
131 catch (NotImplementedException) {
137 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
138 public void PaletteChanged_Deny_Unrestricted ()
141 SystemEvents.PaletteChanged += new EventHandler (EventCallback);
142 SystemEvents.PaletteChanged -= new EventHandler (EventCallback);
144 catch (NotImplementedException) {
149 private void PowerModeChangedCallback (object o, PowerModeChangedEventArgs args)
154 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
155 public void PowerModeChanged_Deny_Unrestricted ()
158 SystemEvents.PowerModeChanged += new PowerModeChangedEventHandler (PowerModeChangedCallback);
159 SystemEvents.PowerModeChanged -= new PowerModeChangedEventHandler (PowerModeChangedCallback);
161 catch (NotImplementedException) {
166 private void SessionEndedCallback (object o, SessionEndedEventArgs args)
171 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
172 public void SessionEnded_Deny_Unrestricted ()
175 SystemEvents.SessionEnded += new SessionEndedEventHandler (SessionEndedCallback);
176 SystemEvents.SessionEnded -= new SessionEndedEventHandler (SessionEndedCallback);
178 catch (NotImplementedException) {
183 private void SessionEndingCallback (object o, SessionEndingEventArgs args)
188 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
189 public void SessionEnding_Deny_Unrestricted ()
192 SystemEvents.SessionEnding += new SessionEndingEventHandler (SessionEndingCallback);
193 SystemEvents.SessionEnding -= new SessionEndingEventHandler (SessionEndingCallback);
195 catch (NotImplementedException) {
199 private void SessionSwitchCallback (object o, SessionSwitchEventArgs args)
204 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
205 public void SessionSwitch_Deny_Unrestricted ()
208 SystemEvents.SessionSwitch += new SessionSwitchEventHandler (SessionSwitchCallback);
209 SystemEvents.SessionSwitch -= new SessionSwitchEventHandler (SessionSwitchCallback);
211 catch (NotImplementedException) {
216 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
217 public void TimeChanged_Deny_Unrestricted ()
220 SystemEvents.TimeChanged += new EventHandler (EventCallback);
221 SystemEvents.TimeChanged -= new EventHandler (EventCallback);
223 catch (NotImplementedException) {
228 private void TimerElapsedCallback (object o, TimerElapsedEventArgs args)
233 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
234 public void TimerElapsed_Deny_Unrestricted ()
236 SystemEvents.TimerElapsed += new TimerElapsedEventHandler (TimerElapsedCallback);
237 SystemEvents.TimerElapsed -= new TimerElapsedEventHandler (TimerElapsedCallback);
240 private void UserPreferenceChangedCallback (object o, UserPreferenceChangedEventArgs args)
245 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
246 public void UserPreferenceChanged_Deny_Unrestricted ()
248 SystemEvents.UserPreferenceChanged += new UserPreferenceChangedEventHandler (UserPreferenceChangedCallback);
249 SystemEvents.UserPreferenceChanged -= new UserPreferenceChangedEventHandler (UserPreferenceChangedCallback);
252 private void UserPreferenceChangingCallback (object o, UserPreferenceChangingEventArgs args)
257 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
258 public void UserPreferenceChanging_Deny_Unrestricted ()
261 SystemEvents.UserPreferenceChanging += new UserPreferenceChangingEventHandler (UserPreferenceChangingCallback);
262 SystemEvents.UserPreferenceChanging -= new UserPreferenceChangingEventHandler (UserPreferenceChangingCallback);
264 catch (NotImplementedException) {
271 // we use reflection to call this class as it is protected by a LinkDemand
272 // (which will be converted into full demand, i.e. a stack walk) when
273 // reflection is used (i.e. it gets testable).
275 public virtual object Create ()
277 MethodInfo mi = typeof (SystemEvents).GetMethod ("CreateTimer");
278 Assert.IsNotNull (mi, "CreateTimer");
279 return mi.Invoke (null, new object[1] { 5000 });
283 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
284 [ExpectedException (typeof (SecurityException))]
285 public void LinkDemand_Deny_Unrestricted ()
287 Assert.IsNotNull (Create ());
291 [EnvironmentPermission (SecurityAction.Deny, Read = "MONO")]
292 [ExpectedException (typeof (SecurityException))]
293 public void LinkDemand_Deny_Anything ()
295 // denying any permissions -> not full trust!
296 Assert.IsNotNull (Create ());
300 [PermissionSet (SecurityAction.PermitOnly, Unrestricted = true)]
301 public void LinkDemand_PermitOnly_Unrestricted ()
303 Assert.IsNotNull (Create ());