2 // DnsCas.cs - CAS unit tests for System.Net.Dns class
5 // Sebastien Pouliot <sebastien@ximian.com>
7 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
12 using NUnit.Framework;
16 using System.Security;
17 using System.Security.Permissions;
18 using System.Threading;
20 namespace MonoCasTests.System.Net {
24 [Category ("NotWorking")] // compiler (CSC) issue (on Windows)
27 private const string site = "www.go-mono.com";
28 private const int timeout = 30000;
30 static ManualResetEvent reset;
31 private string message;
32 private string hostname;
36 public void FixtureSetUp ()
38 reset = new ManualResetEvent (false);
39 hostname = Dns.GetHostName ();
40 ip = Dns.Resolve (site).AddressList[0];
44 public void FixtureTearDown ()
52 if (!SecurityManager.SecurityEnabled)
53 Assert.Ignore ("SecurityManager.SecurityEnabled is OFF");
56 // test Demand by denying it's caller from the required permission
59 [DnsPermission (SecurityAction.Deny, Unrestricted = true)]
60 [ExpectedException (typeof (SecurityException))]
61 public void Deny_BeginGetHostName ()
63 Dns.BeginGetHostByName (null, null, null);
67 [DnsPermission (SecurityAction.Deny, Unrestricted = true)]
68 [ExpectedException (typeof (ArgumentNullException))]
69 public void Deny_EndGetHostByName ()
71 Dns.EndGetHostByName (null);
75 [DnsPermission (SecurityAction.Deny, Unrestricted = true)]
76 [ExpectedException (typeof (SecurityException))]
77 public void Deny_BeginResolve ()
79 Dns.BeginResolve (null, null, null);
83 [DnsPermission (SecurityAction.Deny, Unrestricted = true)]
84 [ExpectedException (typeof (ArgumentNullException))]
85 public void Deny_EndResolve ()
87 Dns.EndResolve (null);
91 [DnsPermission (SecurityAction.Deny, Unrestricted = true)]
92 [ExpectedException (typeof (SecurityException))]
93 public void Deny_GetHostByAddress_IPAddress ()
95 Dns.GetHostByAddress ((IPAddress)null);
99 [DnsPermission (SecurityAction.Deny, Unrestricted = true)]
100 [ExpectedException (typeof (SecurityException))]
101 public void Deny_GetHostByAddress_String ()
103 Dns.GetHostByAddress ((string)null);
107 [DnsPermission (SecurityAction.Deny, Unrestricted = true)]
108 [ExpectedException (typeof (SecurityException))]
109 public void Deny_GetHostByName ()
111 Dns.GetHostByName (site);
115 [DnsPermission (SecurityAction.Deny, Unrestricted = true)]
117 [ExpectedException (typeof (ArgumentNullException))]
118 // so it's not a declarative attribute on the method as the
119 // null check is done before throwing the SecurityException
121 [ExpectedException (typeof (SecurityException))]
123 public void Deny_GetHostByName_Null ()
125 Dns.GetHostByName (null);
129 [DnsPermission (SecurityAction.Deny, Unrestricted = true)]
130 [ExpectedException (typeof (SecurityException))]
131 public void Deny_GetHostByName_HostName ()
133 // ... so my first guess is that you can only query
134 // yourself without having unrestricted DnsPermission
135 Assert.IsNotNull (Dns.GetHostByName (hostname));
136 // but that's wrong :-(
140 [DnsPermission (SecurityAction.Deny, Unrestricted = true)]
141 [ExpectedException (typeof (SecurityException))]
142 public void Deny_GetHostName ()
148 [DnsPermission (SecurityAction.Deny, Unrestricted = true)]
149 [ExpectedException (typeof (SecurityException))]
150 public void Deny_Resolve ()
156 // TODO: New 2.0 methods aren't yet implemented in Mono
159 [DnsPermission (SecurityAction.Deny, Unrestricted = true)]
160 [ExpectedException (typeof (SecurityException))]
161 public void Deny_BeginGetHostAddresses ()
163 Dns.BeginGetHostAddresses (null, null, null);
167 [DnsPermission (SecurityAction.Deny, Unrestricted = true)]
168 [ExpectedException (typeof (ArgumentNullException))]
169 public void Deny_EndGetHostAddresses ()
171 Dns.EndGetHostAddresses (null);
175 [DnsPermission (SecurityAction.Deny, Unrestricted = true)]
176 [ExpectedException (typeof (SecurityException))]
177 public void Deny_BeginGetHostEntry_IPAddress ()
179 Dns.BeginGetHostEntry ((IPAddress)null, null, null);
183 [DnsPermission (SecurityAction.Deny, Unrestricted = true)]
184 [ExpectedException (typeof (SecurityException))]
185 public void Deny_BeginGetHostEntry_String ()
187 Dns.BeginGetHostEntry ((string)null, null, null);
191 [DnsPermission (SecurityAction.Deny, Unrestricted = true)]
192 [ExpectedException (typeof (ArgumentNullException))]
193 public void Deny_EndGetHostEntry ()
195 Dns.EndGetHostEntry (null);
199 [DnsPermission (SecurityAction.Deny, Unrestricted = true)]
200 [ExpectedException (typeof (SecurityException))]
201 public void Deny_GetHostEntry_IPAddress ()
203 Dns.GetHostEntry ((IPAddress)null);
207 [DnsPermission (SecurityAction.Deny, Unrestricted = true)]
208 [ExpectedException (typeof (SecurityException))]
209 public void Deny_GetHostEntry_String ()
211 Dns.GetHostEntry ((string)null);
215 [DnsPermission (SecurityAction.Deny, Unrestricted = true)]
216 [ExpectedException (typeof (SecurityException))]
217 public void Deny_GetHostAddresses ()
219 Dns.GetHostAddresses (null);
224 // ensure that only DnsPermission is required to call the methods
227 [DnsPermission (SecurityAction.PermitOnly, Unrestricted = true)]
228 [ExpectedException (typeof (ArgumentNullException))]
229 public void PermitOnly_BeginGetHostName ()
231 Dns.BeginGetHostByName (null, null, null);
235 [DnsPermission (SecurityAction.PermitOnly, Unrestricted = true)]
236 [ExpectedException (typeof (ArgumentNullException))]
237 public void PermitOnly_BeginResolve ()
239 Dns.BeginResolve (null, null, null);
243 [DnsPermission (SecurityAction.PermitOnly, Unrestricted = true)]
244 [ExpectedException (typeof (ArgumentNullException))]
245 public void PermitOnly_GetHostByAddress_IPAddress ()
247 Dns.GetHostByAddress ((IPAddress)null);
251 [DnsPermission (SecurityAction.PermitOnly, Unrestricted = true)]
252 [ExpectedException (typeof (ArgumentNullException))]
253 public void PermitOnly_GetHostByAddress_String ()
255 Dns.GetHostByAddress ((string)null);
259 [DnsPermission (SecurityAction.PermitOnly, Unrestricted = true)]
260 [ExpectedException (typeof (ArgumentNullException))]
261 public void PermitOnly_GetHostByName ()
263 Dns.GetHostByName (null);
267 [DnsPermission (SecurityAction.PermitOnly, Unrestricted = true)]
268 public void PermitOnly_GetHostName ()
270 Assert.IsNotNull (Dns.GetHostName ());
274 [DnsPermission (SecurityAction.PermitOnly, Unrestricted = true)]
275 [ExpectedException (typeof (ArgumentNullException))]
276 public void PermitOnly_Resolve ()
282 // TODO: New 2.0 methods aren't yet implemented in Mono
285 [DnsPermission (SecurityAction.PermitOnly, Unrestricted = true)]
286 [ExpectedException (typeof (ArgumentNullException))]
287 public void PermitOnly_BeginGetHostAddresses ()
289 Dns.BeginGetHostAddresses (null, null, null);
293 [DnsPermission (SecurityAction.PermitOnly, Unrestricted = true)]
294 [ExpectedException (typeof (ArgumentNullException))]
295 public void PermitOnly_EndGetHostAddresses ()
297 Dns.EndGetHostAddresses (null);
301 [DnsPermission (SecurityAction.PermitOnly, Unrestricted = true)]
302 [ExpectedException (typeof (ArgumentNullException))]
303 public void PermitOnly_BeginGetHostEntry_IPAddress ()
305 Dns.BeginGetHostEntry ((IPAddress)null, null, null);
309 [DnsPermission (SecurityAction.PermitOnly, Unrestricted = true)]
310 [ExpectedException (typeof (ArgumentNullException))]
311 public void PermitOnly_BeginGetHostEntry_String ()
313 Dns.BeginGetHostEntry ((string)null, null, null);
317 [DnsPermission (SecurityAction.PermitOnly, Unrestricted = true)]
318 [ExpectedException (typeof (ArgumentNullException))]
319 public void PermitOnly_EndGetHostEntry ()
321 Dns.EndGetHostEntry (null);
325 [DnsPermission (SecurityAction.PermitOnly, Unrestricted = true)]
326 [ExpectedException (typeof (ArgumentNullException))]
327 public void PermitOnly_GetHostEntry_IPAddress ()
329 Dns.GetHostEntry ((IPAddress)null);
333 [DnsPermission (SecurityAction.PermitOnly, Unrestricted = true)]
334 [ExpectedException (typeof (ArgumentNullException))]
335 public void PermitOnly_GetHostEntry_String ()
337 Dns.GetHostEntry ((string)null);
341 [DnsPermission (SecurityAction.PermitOnly, Unrestricted = true)]
342 [ExpectedException (typeof (ArgumentNullException))]
343 public void PermitOnly_GetHostAddresses ()
345 Dns.GetHostAddresses (null);
350 // async tests (for stack propagation)
352 [DnsPermission (SecurityAction.Deny, Unrestricted = true)]
353 private void GetHostByNameCallback (IAsyncResult ar)
355 Dns.EndGetHostByName (ar);
357 // can we do something bad here ?
358 Assert.IsNotNull (Environment.GetEnvironmentVariable ("USERNAME"));
359 message = "Expected a SecurityException";
361 catch (SecurityException) {
367 message = e.ToString ();
372 [EnvironmentPermission (SecurityAction.Deny, Read = "USERNAME")]
373 public void AsyncGetHostByName ()
375 message = "AsyncGetHostByName";
377 IAsyncResult r = Dns.BeginGetHostByName (site, new AsyncCallback (GetHostByNameCallback), null);
378 Assert.IsNotNull (r, "IAsyncResult");
379 // note for some reason r.AsyncWaitHandle.Wait won't work as expected
380 // if (!r.AsyncWaitHandle.WaitOne (timeout, true))
381 if (!reset.WaitOne (timeout, true))
382 Assert.Ignore ("Timeout");
383 Assert.IsNull (message, message);
386 [DnsPermission (SecurityAction.Deny, Unrestricted = true)]
387 private void ResolveCallback (IAsyncResult ar)
391 // can we do something bad here ?
392 Assert.IsNotNull (Environment.GetEnvironmentVariable ("USERNAME"));
393 message = "Expected a SecurityException";
395 catch (SecurityException) {
399 catch (Exception e) {
400 message = e.ToString ();
405 [EnvironmentPermission (SecurityAction.Deny, Read = "USERNAME")]
406 public void AsyncResolve ()
408 message = "AsyncResolve";
410 IAsyncResult r = Dns.BeginResolve (site, new AsyncCallback (ResolveCallback), null);
411 Assert.IsNotNull (r, "IAsyncResult");
412 // note for some reason r.AsyncWaitHandle.Wait won't work as expected
413 // if (!r.AsyncWaitHandle.WaitOne (timeout, true))
414 if (!reset.WaitOne (timeout, true))
415 Assert.Ignore ("Timeout");
416 Assert.IsNull (message, message);
420 // TODO: New 2.0 methods aren't yet implemented in Mono
422 [DnsPermission (SecurityAction.Deny, Unrestricted = true)]
423 private void GetHostEntryCallback (IAsyncResult ar)
425 Dns.EndGetHostEntry (ar);
427 // can we do something bad here ?
428 Assert.IsNotNull (Environment.GetEnvironmentVariable ("USERNAME"));
429 message = "Expected a SecurityException";
431 catch (SecurityException) {
435 catch (Exception e) {
436 message = e.ToString ();
441 [EnvironmentPermission (SecurityAction.Deny, Read = "USERNAME")]
442 [Ignore ("fails when EndGetHostEntry is called")]
443 public void AsyncGetHostEntry_IPAddress ()
445 message = "AsyncGetHostEntry_IPAddress";
447 IAsyncResult r = Dns.BeginGetHostEntry (ip, new AsyncCallback (GetHostEntryCallback), null);
448 Assert.IsNotNull (r, "IAsyncResult");
449 // note for some reason r.AsyncWaitHandle.Wait won't work as expected
450 // if (!r.AsyncWaitHandle.WaitOne (timeout, true))
451 if (!reset.WaitOne (timeout, true))
452 Assert.Ignore ("Timeout");
453 Assert.IsNull (message, message);
457 [EnvironmentPermission (SecurityAction.Deny, Read = "USERNAME")]
458 public void AsyncGetHostEntry_String ()
460 message = "AsyncGetHostEntry_String";
462 IAsyncResult r = Dns.BeginGetHostEntry (site, new AsyncCallback (GetHostEntryCallback), null);
463 Assert.IsNotNull (r, "IAsyncResult");
464 // note for some reason r.AsyncWaitHandle.Wait won't work as expected
465 // if (!r.AsyncWaitHandle.WaitOne (timeout, true))
466 if (!reset.WaitOne (timeout, true))
467 Assert.Ignore ("Timeout");
468 Assert.IsNull (message, message);
471 [DnsPermission (SecurityAction.Deny, Unrestricted = true)]
472 private void GetHostAddressesCallback (IAsyncResult ar)
474 Dns.EndGetHostEntry (ar);
476 // can we do something bad here ?
477 Assert.IsNotNull (Environment.GetEnvironmentVariable ("USERNAME"));
478 message = "Expected a SecurityException";
480 catch (SecurityException) {
484 catch (Exception e) {
485 message = e.ToString ();
490 [EnvironmentPermission (SecurityAction.Deny, Read = "USERNAME")]
491 public void AsyncGetHostAddresses ()
493 message = "AsyncGetHostAddresses";
495 IAsyncResult r = Dns.BeginGetHostAddresses (site, new AsyncCallback (GetHostAddressesCallback), null);
496 Assert.IsNotNull (r, "IAsyncResult");
497 // note for some reason r.AsyncWaitHandle.Wait won't work as expected
498 // if (!r.AsyncWaitHandle.WaitOne (timeout, true))
499 if (!reset.WaitOne (timeout, true))
500 Assert.Ignore ("Timeout");
501 Assert.IsNull (message, message);