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;
35 public void FixtureSetUp ()
37 reset = new ManualResetEvent (false);
38 hostname = Dns.GetHostName ();
39 var ip = Dns.Resolve (site).AddressList[0];
43 public void FixtureTearDown ()
51 if (!SecurityManager.SecurityEnabled)
52 Assert.Ignore ("SecurityManager.SecurityEnabled is OFF");
55 // test Demand by denying it's caller from the required permission
58 [DnsPermission (SecurityAction.Deny, Unrestricted = true)]
59 [ExpectedException (typeof (SecurityException))]
60 public void Deny_BeginGetHostName ()
62 Dns.BeginGetHostByName (null, null, null);
66 [DnsPermission (SecurityAction.Deny, Unrestricted = true)]
67 [ExpectedException (typeof (ArgumentNullException))]
68 public void Deny_EndGetHostByName ()
70 Dns.EndGetHostByName (null);
74 [DnsPermission (SecurityAction.Deny, Unrestricted = true)]
75 [ExpectedException (typeof (SecurityException))]
76 public void Deny_BeginResolve ()
78 Dns.BeginResolve (null, null, null);
82 [DnsPermission (SecurityAction.Deny, Unrestricted = true)]
83 [ExpectedException (typeof (ArgumentNullException))]
84 public void Deny_EndResolve ()
86 Dns.EndResolve (null);
90 [DnsPermission (SecurityAction.Deny, Unrestricted = true)]
91 [ExpectedException (typeof (SecurityException))]
92 public void Deny_GetHostByAddress_IPAddress ()
94 Dns.GetHostByAddress ((IPAddress)null);
98 [DnsPermission (SecurityAction.Deny, Unrestricted = true)]
99 [ExpectedException (typeof (SecurityException))]
100 public void Deny_GetHostByAddress_String ()
102 Dns.GetHostByAddress ((string)null);
106 [DnsPermission (SecurityAction.Deny, Unrestricted = true)]
107 [ExpectedException (typeof (SecurityException))]
108 public void Deny_GetHostByName ()
110 Dns.GetHostByName (site);
114 [DnsPermission (SecurityAction.Deny, Unrestricted = true)]
116 [ExpectedException (typeof (ArgumentNullException))]
117 // so it's not a declarative attribute on the method as the
118 // null check is done before throwing the SecurityException
120 [ExpectedException (typeof (SecurityException))]
122 public void Deny_GetHostByName_Null ()
124 Dns.GetHostByName (null);
128 [DnsPermission (SecurityAction.Deny, Unrestricted = true)]
129 [ExpectedException (typeof (SecurityException))]
130 public void Deny_GetHostByName_HostName ()
132 // ... so my first guess is that you can only query
133 // yourself without having unrestricted DnsPermission
134 Assert.IsNotNull (Dns.GetHostByName (hostname));
135 // but that's wrong :-(
139 [DnsPermission (SecurityAction.Deny, Unrestricted = true)]
140 [ExpectedException (typeof (SecurityException))]
141 public void Deny_GetHostName ()
147 [DnsPermission (SecurityAction.Deny, Unrestricted = true)]
148 [ExpectedException (typeof (SecurityException))]
149 public void Deny_Resolve ()
155 // TODO: New 2.0 methods aren't yet implemented in Mono
158 [DnsPermission (SecurityAction.Deny, Unrestricted = true)]
159 [ExpectedException (typeof (SecurityException))]
160 public void Deny_BeginGetHostAddresses ()
162 Dns.BeginGetHostAddresses (null, null, null);
166 [DnsPermission (SecurityAction.Deny, Unrestricted = true)]
167 [ExpectedException (typeof (ArgumentNullException))]
168 public void Deny_EndGetHostAddresses ()
170 Dns.EndGetHostAddresses (null);
174 [DnsPermission (SecurityAction.Deny, Unrestricted = true)]
175 [ExpectedException (typeof (SecurityException))]
176 public void Deny_BeginGetHostEntry_IPAddress ()
178 Dns.BeginGetHostEntry ((IPAddress)null, null, null);
182 [DnsPermission (SecurityAction.Deny, Unrestricted = true)]
183 [ExpectedException (typeof (SecurityException))]
184 public void Deny_BeginGetHostEntry_String ()
186 Dns.BeginGetHostEntry ((string)null, null, null);
190 [DnsPermission (SecurityAction.Deny, Unrestricted = true)]
191 [ExpectedException (typeof (ArgumentNullException))]
192 public void Deny_EndGetHostEntry ()
194 Dns.EndGetHostEntry (null);
198 [DnsPermission (SecurityAction.Deny, Unrestricted = true)]
199 [ExpectedException (typeof (SecurityException))]
200 public void Deny_GetHostEntry_IPAddress ()
202 Dns.GetHostEntry ((IPAddress)null);
206 [DnsPermission (SecurityAction.Deny, Unrestricted = true)]
207 [ExpectedException (typeof (SecurityException))]
208 public void Deny_GetHostEntry_String ()
210 Dns.GetHostEntry ((string)null);
214 [DnsPermission (SecurityAction.Deny, Unrestricted = true)]
215 [ExpectedException (typeof (SecurityException))]
216 public void Deny_GetHostAddresses ()
218 Dns.GetHostAddresses (null);
223 // ensure that only DnsPermission is required to call the methods
226 [DnsPermission (SecurityAction.PermitOnly, Unrestricted = true)]
227 [ExpectedException (typeof (ArgumentNullException))]
228 public void PermitOnly_BeginGetHostName ()
230 Dns.BeginGetHostByName (null, null, null);
234 [DnsPermission (SecurityAction.PermitOnly, Unrestricted = true)]
235 [ExpectedException (typeof (ArgumentNullException))]
236 public void PermitOnly_BeginResolve ()
238 Dns.BeginResolve (null, null, null);
242 [DnsPermission (SecurityAction.PermitOnly, Unrestricted = true)]
243 [ExpectedException (typeof (ArgumentNullException))]
244 public void PermitOnly_GetHostByAddress_IPAddress ()
246 Dns.GetHostByAddress ((IPAddress)null);
250 [DnsPermission (SecurityAction.PermitOnly, Unrestricted = true)]
251 [ExpectedException (typeof (ArgumentNullException))]
252 public void PermitOnly_GetHostByAddress_String ()
254 Dns.GetHostByAddress ((string)null);
258 [DnsPermission (SecurityAction.PermitOnly, Unrestricted = true)]
259 [ExpectedException (typeof (ArgumentNullException))]
260 public void PermitOnly_GetHostByName ()
262 Dns.GetHostByName (null);
266 [DnsPermission (SecurityAction.PermitOnly, Unrestricted = true)]
267 public void PermitOnly_GetHostName ()
269 Assert.IsNotNull (Dns.GetHostName ());
273 [DnsPermission (SecurityAction.PermitOnly, Unrestricted = true)]
274 [ExpectedException (typeof (ArgumentNullException))]
275 public void PermitOnly_Resolve ()
281 // TODO: New 2.0 methods aren't yet implemented in Mono
284 [DnsPermission (SecurityAction.PermitOnly, Unrestricted = true)]
285 [ExpectedException (typeof (ArgumentNullException))]
286 public void PermitOnly_BeginGetHostAddresses ()
288 Dns.BeginGetHostAddresses (null, null, null);
292 [DnsPermission (SecurityAction.PermitOnly, Unrestricted = true)]
293 [ExpectedException (typeof (ArgumentNullException))]
294 public void PermitOnly_EndGetHostAddresses ()
296 Dns.EndGetHostAddresses (null);
300 [DnsPermission (SecurityAction.PermitOnly, Unrestricted = true)]
301 [ExpectedException (typeof (ArgumentNullException))]
302 public void PermitOnly_BeginGetHostEntry_IPAddress ()
304 Dns.BeginGetHostEntry ((IPAddress)null, null, null);
308 [DnsPermission (SecurityAction.PermitOnly, Unrestricted = true)]
309 [ExpectedException (typeof (ArgumentNullException))]
310 public void PermitOnly_BeginGetHostEntry_String ()
312 Dns.BeginGetHostEntry ((string)null, null, null);
316 [DnsPermission (SecurityAction.PermitOnly, Unrestricted = true)]
317 [ExpectedException (typeof (ArgumentNullException))]
318 public void PermitOnly_EndGetHostEntry ()
320 Dns.EndGetHostEntry (null);
324 [DnsPermission (SecurityAction.PermitOnly, Unrestricted = true)]
325 [ExpectedException (typeof (ArgumentNullException))]
326 public void PermitOnly_GetHostEntry_IPAddress ()
328 Dns.GetHostEntry ((IPAddress)null);
332 [DnsPermission (SecurityAction.PermitOnly, Unrestricted = true)]
333 [ExpectedException (typeof (ArgumentNullException))]
334 public void PermitOnly_GetHostEntry_String ()
336 Dns.GetHostEntry ((string)null);
340 [DnsPermission (SecurityAction.PermitOnly, Unrestricted = true)]
341 [ExpectedException (typeof (ArgumentNullException))]
342 public void PermitOnly_GetHostAddresses ()
344 Dns.GetHostAddresses (null);
349 // async tests (for stack propagation)
351 [DnsPermission (SecurityAction.Deny, Unrestricted = true)]
352 private void GetHostByNameCallback (IAsyncResult ar)
354 Dns.EndGetHostByName (ar);
356 // can we do something bad here ?
357 Assert.IsNotNull (Environment.GetEnvironmentVariable ("USERNAME"));
358 message = "Expected a SecurityException";
360 catch (SecurityException) {
366 message = e.ToString ();
371 [EnvironmentPermission (SecurityAction.Deny, Read = "USERNAME")]
372 public void AsyncGetHostByName ()
374 message = "AsyncGetHostByName";
376 IAsyncResult r = Dns.BeginGetHostByName (site, new AsyncCallback (GetHostByNameCallback), null);
377 Assert.IsNotNull (r, "IAsyncResult");
378 // note for some reason r.AsyncWaitHandle.Wait won't work as expected
379 // if (!r.AsyncWaitHandle.WaitOne (timeout, true))
380 if (!reset.WaitOne (timeout, true))
381 Assert.Ignore ("Timeout");
382 Assert.IsNull (message, message);
385 [DnsPermission (SecurityAction.Deny, Unrestricted = true)]
386 private void ResolveCallback (IAsyncResult ar)
390 // can we do something bad here ?
391 Assert.IsNotNull (Environment.GetEnvironmentVariable ("USERNAME"));
392 message = "Expected a SecurityException";
394 catch (SecurityException) {
398 catch (Exception e) {
399 message = e.ToString ();
404 [EnvironmentPermission (SecurityAction.Deny, Read = "USERNAME")]
405 public void AsyncResolve ()
407 message = "AsyncResolve";
409 IAsyncResult r = Dns.BeginResolve (site, new AsyncCallback (ResolveCallback), null);
410 Assert.IsNotNull (r, "IAsyncResult");
411 // note for some reason r.AsyncWaitHandle.Wait won't work as expected
412 // if (!r.AsyncWaitHandle.WaitOne (timeout, true))
413 if (!reset.WaitOne (timeout, true))
414 Assert.Ignore ("Timeout");
415 Assert.IsNull (message, message);
419 // TODO: New 2.0 methods aren't yet implemented in Mono
421 [DnsPermission (SecurityAction.Deny, Unrestricted = true)]
422 private void GetHostEntryCallback (IAsyncResult ar)
424 Dns.EndGetHostEntry (ar);
426 // can we do something bad here ?
427 Assert.IsNotNull (Environment.GetEnvironmentVariable ("USERNAME"));
428 message = "Expected a SecurityException";
430 catch (SecurityException) {
434 catch (Exception e) {
435 message = e.ToString ();
440 [EnvironmentPermission (SecurityAction.Deny, Read = "USERNAME")]
441 [Ignore ("fails when EndGetHostEntry is called")]
442 public void AsyncGetHostEntry_IPAddress ()
444 message = "AsyncGetHostEntry_IPAddress";
446 IAsyncResult r = Dns.BeginGetHostEntry (ip, new AsyncCallback (GetHostEntryCallback), null);
447 Assert.IsNotNull (r, "IAsyncResult");
448 // note for some reason r.AsyncWaitHandle.Wait won't work as expected
449 // if (!r.AsyncWaitHandle.WaitOne (timeout, true))
450 if (!reset.WaitOne (timeout, true))
451 Assert.Ignore ("Timeout");
452 Assert.IsNull (message, message);
456 [EnvironmentPermission (SecurityAction.Deny, Read = "USERNAME")]
457 public void AsyncGetHostEntry_String ()
459 message = "AsyncGetHostEntry_String";
461 IAsyncResult r = Dns.BeginGetHostEntry (site, new AsyncCallback (GetHostEntryCallback), null);
462 Assert.IsNotNull (r, "IAsyncResult");
463 // note for some reason r.AsyncWaitHandle.Wait won't work as expected
464 // if (!r.AsyncWaitHandle.WaitOne (timeout, true))
465 if (!reset.WaitOne (timeout, true))
466 Assert.Ignore ("Timeout");
467 Assert.IsNull (message, message);
470 [DnsPermission (SecurityAction.Deny, Unrestricted = true)]
471 private void GetHostAddressesCallback (IAsyncResult ar)
473 Dns.EndGetHostEntry (ar);
475 // can we do something bad here ?
476 Assert.IsNotNull (Environment.GetEnvironmentVariable ("USERNAME"));
477 message = "Expected a SecurityException";
479 catch (SecurityException) {
483 catch (Exception e) {
484 message = e.ToString ();
489 [EnvironmentPermission (SecurityAction.Deny, Read = "USERNAME")]
490 public void AsyncGetHostAddresses ()
492 message = "AsyncGetHostAddresses";
494 IAsyncResult r = Dns.BeginGetHostAddresses (site, new AsyncCallback (GetHostAddressesCallback), null);
495 Assert.IsNotNull (r, "IAsyncResult");
496 // note for some reason r.AsyncWaitHandle.Wait won't work as expected
497 // if (!r.AsyncWaitHandle.WaitOne (timeout, true))
498 if (!reset.WaitOne (timeout, true))
499 Assert.Ignore ("Timeout");
500 Assert.IsNull (message, message);