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) {
87 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
88 public void DisplaySettingsChanging_Deny_Unrestricted ()
91 SystemEvents.DisplaySettingsChanging += new EventHandler (EventCallback);
92 SystemEvents.DisplaySettingsChanging -= new EventHandler (EventCallback);
94 catch (NotImplementedException) {
100 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
101 public void EventsThreadShutdown_Deny_Unrestricted ()
104 SystemEvents.EventsThreadShutdown += new EventHandler (EventCallback);
105 SystemEvents.EventsThreadShutdown -= new EventHandler (EventCallback);
107 catch (NotImplementedException) {
113 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
114 public void InstalledFontsChanged_Deny_Unrestricted ()
117 SystemEvents.InstalledFontsChanged += new EventHandler (EventCallback);
118 SystemEvents.InstalledFontsChanged -= new EventHandler (EventCallback);
120 catch (NotImplementedException) {
126 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
127 public void LowMemory_Deny_Unrestricted ()
130 SystemEvents.LowMemory += new EventHandler (EventCallback);
131 SystemEvents.LowMemory -= new EventHandler (EventCallback);
133 catch (NotImplementedException) {
139 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
140 public void PaletteChanged_Deny_Unrestricted ()
143 SystemEvents.PaletteChanged += new EventHandler (EventCallback);
144 SystemEvents.PaletteChanged -= new EventHandler (EventCallback);
146 catch (NotImplementedException) {
151 private void PowerModeChangedCallback (object o, PowerModeChangedEventArgs args)
156 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
157 public void PowerModeChanged_Deny_Unrestricted ()
160 SystemEvents.PowerModeChanged += new PowerModeChangedEventHandler (PowerModeChangedCallback);
161 SystemEvents.PowerModeChanged -= new PowerModeChangedEventHandler (PowerModeChangedCallback);
163 catch (NotImplementedException) {
168 private void SessionEndedCallback (object o, SessionEndedEventArgs args)
173 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
174 public void SessionEnded_Deny_Unrestricted ()
177 SystemEvents.SessionEnded += new SessionEndedEventHandler (SessionEndedCallback);
178 SystemEvents.SessionEnded -= new SessionEndedEventHandler (SessionEndedCallback);
180 catch (NotImplementedException) {
185 private void SessionEndingCallback (object o, SessionEndingEventArgs args)
190 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
191 public void SessionEnding_Deny_Unrestricted ()
194 SystemEvents.SessionEnding += new SessionEndingEventHandler (SessionEndingCallback);
195 SystemEvents.SessionEnding -= new SessionEndingEventHandler (SessionEndingCallback);
197 catch (NotImplementedException) {
202 private void SessionSwitchCallback (object o, SessionSwitchEventArgs args)
207 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
208 public void SessionSwitch_Deny_Unrestricted ()
211 SystemEvents.SessionSwitch += new SessionSwitchEventHandler (SessionSwitchCallback);
212 SystemEvents.SessionSwitch -= new SessionSwitchEventHandler (SessionSwitchCallback);
214 catch (NotImplementedException) {
220 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
221 public void TimeChanged_Deny_Unrestricted ()
224 SystemEvents.TimeChanged += new EventHandler (EventCallback);
225 SystemEvents.TimeChanged -= new EventHandler (EventCallback);
227 catch (NotImplementedException) {
232 private void TimerElapsedCallback (object o, TimerElapsedEventArgs args)
237 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
238 public void TimerElapsed_Deny_Unrestricted ()
240 SystemEvents.TimerElapsed += new TimerElapsedEventHandler (TimerElapsedCallback);
241 SystemEvents.TimerElapsed -= new TimerElapsedEventHandler (TimerElapsedCallback);
244 private void UserPreferenceChangedCallback (object o, UserPreferenceChangedEventArgs args)
249 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
250 public void UserPreferenceChanged_Deny_Unrestricted ()
252 SystemEvents.UserPreferenceChanged += new UserPreferenceChangedEventHandler (UserPreferenceChangedCallback);
253 SystemEvents.UserPreferenceChanged -= new UserPreferenceChangedEventHandler (UserPreferenceChangedCallback);
256 private void UserPreferenceChangingCallback (object o, UserPreferenceChangingEventArgs args)
261 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
262 public void UserPreferenceChanging_Deny_Unrestricted ()
265 SystemEvents.UserPreferenceChanging += new UserPreferenceChangingEventHandler (UserPreferenceChangingCallback);
266 SystemEvents.UserPreferenceChanging -= new UserPreferenceChangingEventHandler (UserPreferenceChangingCallback);
268 catch (NotImplementedException) {
275 // we use reflection to call this class as it is protected by a LinkDemand
276 // (which will be converted into full demand, i.e. a stack walk) when
277 // reflection is used (i.e. it gets testable).
279 public virtual object Create ()
281 MethodInfo mi = typeof (SystemEvents).GetMethod ("CreateTimer");
282 Assert.IsNotNull (mi, "CreateTimer");
283 return mi.Invoke (null, new object[1] { 5000 });
287 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
288 [ExpectedException (typeof (SecurityException))]
289 public void LinkDemand_Deny_Unrestricted ()
291 Assert.IsNotNull (Create ());
295 [EnvironmentPermission (SecurityAction.Deny, Read = "MONO")]
296 [ExpectedException (typeof (SecurityException))]
297 public void LinkDemand_Deny_Anything ()
299 // denying any permissions -> not full trust!
300 Assert.IsNotNull (Create ());
304 [PermissionSet (SecurityAction.PermitOnly, Unrestricted = true)]
305 public void LinkDemand_PermitOnly_Unrestricted ()
307 Assert.IsNotNull (Create ());