[System] Try to fix Cookies tests
[mono.git] / mcs / class / System / Test / Microsoft.Win32 / SystemEventsCas.cs
1 //
2 // SystemEventsCas.cs - CAS unit tests for Microsoft.Win32.SystemEvents
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.Reflection;
33 using System.Security;
34 using System.Security.Permissions;
35 using Microsoft.Win32;
36
37 namespace MonoCasTests.Microsoft.Win32 {
38
39         [TestFixture]
40         [Category ("CAS")]
41         public class SystemEventsCas {
42
43                 [SetUp]
44                 public virtual void SetUp ()
45                 {
46                         if (!SecurityManager.SecurityEnabled)
47                                 Assert.Ignore ("SecurityManager.SecurityEnabled is OFF");
48                 }
49
50                 private void TimerCallback (object o, TimerElapsedEventArgs args)
51                 {
52                 }
53
54                 [Test]
55                 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
56                 public void Methods_Deny_Unrestricted ()
57                 {
58                         IntPtr timer = SystemEvents.CreateTimer (5000);
59                         SystemEvents.KillTimer (timer);
60
61                         try {
62                                 SystemEvents.InvokeOnEventsThread (new TimerElapsedEventHandler (TimerCallback));
63                         }
64                         catch (NotImplementedException) {
65                                 // mono
66                         }
67                 }
68
69                 private void EventCallback (object o, EventArgs args)
70                 {
71                 }
72
73                 [Test]
74                 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
75                 public void DisplaySettingsChanged_Deny_Unrestricted ()
76                 {
77                         try {
78                                 SystemEvents.DisplaySettingsChanged += new EventHandler (EventCallback);
79                                 SystemEvents.DisplaySettingsChanged -= new EventHandler (EventCallback);
80                         }
81                         catch (NotImplementedException) {
82                                 // mono
83                         }
84                 }
85 #if NET_2_0
86                 [Test]
87                 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
88                 public void DisplaySettingsChanging_Deny_Unrestricted ()
89                 {
90                         try {
91                                 SystemEvents.DisplaySettingsChanging += new EventHandler (EventCallback);
92                                 SystemEvents.DisplaySettingsChanging -= new EventHandler (EventCallback);
93                         }
94                         catch (NotImplementedException) {
95                                 // mono
96                         }
97                 }
98 #endif
99                 [Test]
100                 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
101                 public void EventsThreadShutdown_Deny_Unrestricted ()
102                 {
103                         try {
104                                 SystemEvents.EventsThreadShutdown += new EventHandler (EventCallback);
105                                 SystemEvents.EventsThreadShutdown -= new EventHandler (EventCallback);
106                         }
107                         catch (NotImplementedException) {
108                                 // mono
109                         }
110                 }
111
112                 [Test]
113                 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
114                 public void InstalledFontsChanged_Deny_Unrestricted ()
115                 {
116                         try {
117                                 SystemEvents.InstalledFontsChanged += new EventHandler (EventCallback);
118                                 SystemEvents.InstalledFontsChanged -= new EventHandler (EventCallback);
119                         }
120                         catch (NotImplementedException) {
121                                 // mono
122                         }
123                 }
124
125                 [Test]
126                 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
127                 public void LowMemory_Deny_Unrestricted ()
128                 {
129                         try {
130                                 SystemEvents.LowMemory += new EventHandler (EventCallback);
131                                 SystemEvents.LowMemory -= new EventHandler (EventCallback);
132                         }
133                         catch (NotImplementedException) {
134                                 // mono
135                         }
136                 }
137
138                 [Test]
139                 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
140                 public void PaletteChanged_Deny_Unrestricted ()
141                 {
142                         try {
143                                 SystemEvents.PaletteChanged += new EventHandler (EventCallback);
144                                 SystemEvents.PaletteChanged -= new EventHandler (EventCallback);
145                         }
146                         catch (NotImplementedException) {
147                                 // mono
148                         }
149                 }
150
151                 private void PowerModeChangedCallback (object o, PowerModeChangedEventArgs args)
152                 {
153                 }
154
155                 [Test]
156                 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
157                 public void PowerModeChanged_Deny_Unrestricted ()
158                 {
159                         try {
160                                 SystemEvents.PowerModeChanged += new PowerModeChangedEventHandler (PowerModeChangedCallback);
161                                 SystemEvents.PowerModeChanged -= new PowerModeChangedEventHandler (PowerModeChangedCallback);
162                         }
163                         catch (NotImplementedException) {
164                                 // mono
165                         }
166                 }
167
168                 private void SessionEndedCallback (object o, SessionEndedEventArgs args)
169                 {
170                 }
171
172                 [Test]
173                 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
174                 public void SessionEnded_Deny_Unrestricted ()
175                 {
176                         try {
177                                 SystemEvents.SessionEnded += new SessionEndedEventHandler (SessionEndedCallback);
178                                 SystemEvents.SessionEnded -= new SessionEndedEventHandler (SessionEndedCallback);
179                         }
180                         catch (NotImplementedException) {
181                                 // mono
182                         }
183                 }
184
185                 private void SessionEndingCallback (object o, SessionEndingEventArgs args)
186                 {
187                 }
188
189                 [Test]
190                 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
191                 public void SessionEnding_Deny_Unrestricted ()
192                 {
193                         try {
194                                 SystemEvents.SessionEnding += new SessionEndingEventHandler (SessionEndingCallback);
195                                 SystemEvents.SessionEnding -= new SessionEndingEventHandler (SessionEndingCallback);
196                         }
197                         catch (NotImplementedException) {
198                                 // mono
199                         }
200                 }
201 #if NET_2_0
202                 private void SessionSwitchCallback (object o, SessionSwitchEventArgs args)
203                 {
204                 }
205
206                 [Test]
207                 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
208                 public void SessionSwitch_Deny_Unrestricted ()
209                 {
210                         try {
211                                 SystemEvents.SessionSwitch += new SessionSwitchEventHandler (SessionSwitchCallback);
212                                 SystemEvents.SessionSwitch -= new SessionSwitchEventHandler (SessionSwitchCallback);
213                         }
214                         catch (NotImplementedException) {
215                                 // mono
216                         }
217                 }
218 #endif
219                 [Test]
220                 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
221                 public void TimeChanged_Deny_Unrestricted ()
222                 {
223                         try {
224                                 SystemEvents.TimeChanged += new EventHandler (EventCallback);
225                                 SystemEvents.TimeChanged -= new EventHandler (EventCallback);
226                         }
227                         catch (NotImplementedException) {
228                                 // mono
229                         }
230                 }
231
232                 private void TimerElapsedCallback (object o, TimerElapsedEventArgs args)
233                 {
234                 }
235
236                 [Test]
237                 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
238                 public void TimerElapsed_Deny_Unrestricted ()
239                 {
240                         SystemEvents.TimerElapsed += new TimerElapsedEventHandler (TimerElapsedCallback);
241                         SystemEvents.TimerElapsed -= new TimerElapsedEventHandler (TimerElapsedCallback);
242                 }
243
244                 private void UserPreferenceChangedCallback (object o, UserPreferenceChangedEventArgs args)
245                 {
246                 }
247
248                 [Test]
249                 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
250                 public void UserPreferenceChanged_Deny_Unrestricted ()
251                 {
252                         SystemEvents.UserPreferenceChanged += new UserPreferenceChangedEventHandler (UserPreferenceChangedCallback);
253                         SystemEvents.UserPreferenceChanged -= new UserPreferenceChangedEventHandler (UserPreferenceChangedCallback);
254                 }
255
256                 private void UserPreferenceChangingCallback (object o, UserPreferenceChangingEventArgs args)
257                 {
258                 }
259
260                 [Test]
261                 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
262                 public void UserPreferenceChanging_Deny_Unrestricted ()
263                 {
264                         try {
265                                 SystemEvents.UserPreferenceChanging += new UserPreferenceChangingEventHandler (UserPreferenceChangingCallback);
266                                 SystemEvents.UserPreferenceChanging -= new UserPreferenceChangingEventHandler (UserPreferenceChangingCallback);
267                         }
268                         catch (NotImplementedException) {
269                                 // mono
270                         }
271                 }
272
273                 // LinkDemand
274
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).
278
279                 public virtual object Create ()
280                 {
281                         MethodInfo mi = typeof (SystemEvents).GetMethod ("CreateTimer");
282                         Assert.IsNotNull (mi, "CreateTimer");
283                         return mi.Invoke (null, new object[1] { 5000 });
284                 }
285
286                 [Test]
287                 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
288                 [ExpectedException (typeof (SecurityException))]
289                 public void LinkDemand_Deny_Unrestricted ()
290                 {
291                         Assert.IsNotNull (Create ());
292                 }
293
294                 [Test]
295                 [EnvironmentPermission (SecurityAction.Deny, Read = "MONO")]
296                 [ExpectedException (typeof (SecurityException))]
297                 public void LinkDemand_Deny_Anything ()
298                 {
299                         // denying any permissions -> not full trust!
300                         Assert.IsNotNull (Create ());
301                 }
302
303                 [Test]
304                 [PermissionSet (SecurityAction.PermitOnly, Unrestricted = true)]
305                 public void LinkDemand_PermitOnly_Unrestricted ()
306                 {
307                         Assert.IsNotNull (Create ());
308                 }
309         }
310 }