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)
10 using NUnit.Framework;
14 using System.Security;
15 using System.Security.Permissions;
16 using System.Threading;
18 namespace MonoCasTests.System.Net {
22 [Category ("NotWorking")] // compiler (CSC) issue (on Windows)
25 private const string site = "www.go-mono.com";
26 private const int timeout = 30000;
28 static ManualResetEvent reset;
29 private string message;
30 private string hostname;
34 public void FixtureSetUp ()
36 reset = new ManualResetEvent (false);
37 hostname = Dns.GetHostName ();
38 ip = Dns.Resolve (site).AddressList[0];
42 public void FixtureTearDown ()
50 if (!SecurityManager.SecurityEnabled)
51 Assert.Ignore ("SecurityManager.SecurityEnabled is OFF");
54 // test Demand by denying it's caller from the required permission
57 [DnsPermission (SecurityAction.Deny, Unrestricted = true)]
58 [ExpectedException (typeof (SecurityException))]
59 public void Deny_BeginGetHostName ()
61 Dns.BeginGetHostByName (null, null, null);
65 [DnsPermission (SecurityAction.Deny, Unrestricted = true)]
66 [ExpectedException (typeof (ArgumentNullException))]
67 public void Deny_EndGetHostByName ()
69 Dns.EndGetHostByName (null);
73 [DnsPermission (SecurityAction.Deny, Unrestricted = true)]
74 [ExpectedException (typeof (SecurityException))]
75 public void Deny_BeginResolve ()
77 Dns.BeginResolve (null, null, null);
81 [DnsPermission (SecurityAction.Deny, Unrestricted = true)]
82 [ExpectedException (typeof (ArgumentNullException))]
83 public void Deny_EndResolve ()
85 Dns.EndResolve (null);
89 [DnsPermission (SecurityAction.Deny, Unrestricted = true)]
90 [ExpectedException (typeof (SecurityException))]
91 public void Deny_GetHostByAddress_IPAddress ()
93 Dns.GetHostByAddress ((IPAddress)null);
97 [DnsPermission (SecurityAction.Deny, Unrestricted = true)]
98 [ExpectedException (typeof (SecurityException))]
99 public void Deny_GetHostByAddress_String ()
101 Dns.GetHostByAddress ((string)null);
105 [DnsPermission (SecurityAction.Deny, Unrestricted = true)]
106 [ExpectedException (typeof (SecurityException))]
107 public void Deny_GetHostByName ()
109 Dns.GetHostByName (site);
113 [DnsPermission (SecurityAction.Deny, Unrestricted = true)]
115 [ExpectedException (typeof (ArgumentNullException))]
116 // so it's not a declarative attribute on the method as the
117 // null check is done before throwing the SecurityException
119 [ExpectedException (typeof (SecurityException))]
121 public void Deny_GetHostByName_Null ()
123 Dns.GetHostByName (null);
127 [DnsPermission (SecurityAction.Deny, Unrestricted = true)]
128 [ExpectedException (typeof (SecurityException))]
129 public void Deny_GetHostByName_HostName ()
131 // ... so my first guess is that you can only query
132 // yourself without having unrestricted DnsPermission
133 Assert.IsNotNull (Dns.GetHostByName (hostname));
134 // but that's wrong :-(
138 [DnsPermission (SecurityAction.Deny, Unrestricted = true)]
139 [ExpectedException (typeof (SecurityException))]
140 public void Deny_GetHostName ()
146 [DnsPermission (SecurityAction.Deny, Unrestricted = true)]
147 [ExpectedException (typeof (SecurityException))]
148 public void Deny_Resolve ()
154 // TODO: New 2.0 methods aren't yet implemented in Mono
157 [DnsPermission (SecurityAction.Deny, Unrestricted = true)]
158 [ExpectedException (typeof (SecurityException))]
159 public void Deny_BeginGetHostAddresses ()
161 Dns.BeginGetHostAddresses (null, null, null);
165 [DnsPermission (SecurityAction.Deny, Unrestricted = true)]
166 [ExpectedException (typeof (ArgumentNullException))]
167 public void Deny_EndGetHostAddresses ()
169 Dns.EndGetHostAddresses (null);
173 [DnsPermission (SecurityAction.Deny, Unrestricted = true)]
174 [ExpectedException (typeof (SecurityException))]
175 public void Deny_BeginGetHostEntry_IPAddress ()
177 Dns.BeginGetHostEntry ((IPAddress)null, null, null);
181 [DnsPermission (SecurityAction.Deny, Unrestricted = true)]
182 [ExpectedException (typeof (SecurityException))]
183 public void Deny_BeginGetHostEntry_String ()
185 Dns.BeginGetHostEntry ((string)null, null, null);
189 [DnsPermission (SecurityAction.Deny, Unrestricted = true)]
190 [ExpectedException (typeof (ArgumentNullException))]
191 public void Deny_EndGetHostEntry ()
193 Dns.EndGetHostEntry (null);
197 [DnsPermission (SecurityAction.Deny, Unrestricted = true)]
198 [ExpectedException (typeof (SecurityException))]
199 public void Deny_GetHostEntry_IPAddress ()
201 Dns.GetHostEntry ((IPAddress)null);
205 [DnsPermission (SecurityAction.Deny, Unrestricted = true)]
206 [ExpectedException (typeof (SecurityException))]
207 public void Deny_GetHostEntry_String ()
209 Dns.GetHostEntry ((string)null);
213 [DnsPermission (SecurityAction.Deny, Unrestricted = true)]
214 [ExpectedException (typeof (SecurityException))]
215 public void Deny_GetHostAddresses ()
217 Dns.GetHostAddresses (null);
222 // ensure that only DnsPermission is required to call the methods
225 [DnsPermission (SecurityAction.PermitOnly, Unrestricted = true)]
226 [ExpectedException (typeof (ArgumentNullException))]
227 public void PermitOnly_BeginGetHostName ()
229 Dns.BeginGetHostByName (null, null, null);
233 [DnsPermission (SecurityAction.PermitOnly, Unrestricted = true)]
234 [ExpectedException (typeof (ArgumentNullException))]
235 public void PermitOnly_BeginResolve ()
237 Dns.BeginResolve (null, null, null);
241 [DnsPermission (SecurityAction.PermitOnly, Unrestricted = true)]
242 [ExpectedException (typeof (ArgumentNullException))]
243 public void PermitOnly_GetHostByAddress_IPAddress ()
245 Dns.GetHostByAddress ((IPAddress)null);
249 [DnsPermission (SecurityAction.PermitOnly, Unrestricted = true)]
250 [ExpectedException (typeof (ArgumentNullException))]
251 public void PermitOnly_GetHostByAddress_String ()
253 Dns.GetHostByAddress ((string)null);
257 [DnsPermission (SecurityAction.PermitOnly, Unrestricted = true)]
258 [ExpectedException (typeof (ArgumentNullException))]
259 public void PermitOnly_GetHostByName ()
261 Dns.GetHostByName (null);
265 [DnsPermission (SecurityAction.PermitOnly, Unrestricted = true)]
266 public void PermitOnly_GetHostName ()
268 Assert.IsNotNull (Dns.GetHostName ());
272 [DnsPermission (SecurityAction.PermitOnly, Unrestricted = true)]
273 [ExpectedException (typeof (ArgumentNullException))]
274 public void PermitOnly_Resolve ()
280 // TODO: New 2.0 methods aren't yet implemented in Mono
283 [DnsPermission (SecurityAction.PermitOnly, Unrestricted = true)]
284 [ExpectedException (typeof (ArgumentNullException))]
285 public void PermitOnly_BeginGetHostAddresses ()
287 Dns.BeginGetHostAddresses (null, null, null);
291 [DnsPermission (SecurityAction.PermitOnly, Unrestricted = true)]
292 [ExpectedException (typeof (ArgumentNullException))]
293 public void PermitOnly_EndGetHostAddresses ()
295 Dns.EndGetHostAddresses (null);
299 [DnsPermission (SecurityAction.PermitOnly, Unrestricted = true)]
300 [ExpectedException (typeof (ArgumentNullException))]
301 public void PermitOnly_BeginGetHostEntry_IPAddress ()
303 Dns.BeginGetHostEntry ((IPAddress)null, null, null);
307 [DnsPermission (SecurityAction.PermitOnly, Unrestricted = true)]
308 [ExpectedException (typeof (ArgumentNullException))]
309 public void PermitOnly_BeginGetHostEntry_String ()
311 Dns.BeginGetHostEntry ((string)null, null, null);
315 [DnsPermission (SecurityAction.PermitOnly, Unrestricted = true)]
316 [ExpectedException (typeof (ArgumentNullException))]
317 public void PermitOnly_EndGetHostEntry ()
319 Dns.EndGetHostEntry (null);
323 [DnsPermission (SecurityAction.PermitOnly, Unrestricted = true)]
324 [ExpectedException (typeof (ArgumentNullException))]
325 public void PermitOnly_GetHostEntry_IPAddress ()
327 Dns.GetHostEntry ((IPAddress)null);
331 [DnsPermission (SecurityAction.PermitOnly, Unrestricted = true)]
332 [ExpectedException (typeof (ArgumentNullException))]
333 public void PermitOnly_GetHostEntry_String ()
335 Dns.GetHostEntry ((string)null);
339 [DnsPermission (SecurityAction.PermitOnly, Unrestricted = true)]
340 [ExpectedException (typeof (ArgumentNullException))]
341 public void PermitOnly_GetHostAddresses ()
343 Dns.GetHostAddresses (null);
348 // async tests (for stack propagation)
350 [DnsPermission (SecurityAction.Deny, Unrestricted = true)]
351 private void GetHostByNameCallback (IAsyncResult ar)
353 Dns.EndGetHostByName (ar);
355 // can we do something bad here ?
356 Assert.IsNotNull (Environment.GetEnvironmentVariable ("USERNAME"));
357 message = "Expected a SecurityException";
359 catch (SecurityException) {
365 message = e.ToString ();
370 [EnvironmentPermission (SecurityAction.Deny, Read = "USERNAME")]
371 public void AsyncGetHostByName ()
373 message = "AsyncGetHostByName";
375 IAsyncResult r = Dns.BeginGetHostByName (site, new AsyncCallback (GetHostByNameCallback), null);
376 Assert.IsNotNull (r, "IAsyncResult");
377 // note for some reason r.AsyncWaitHandle.Wait won't work as expected
378 // if (!r.AsyncWaitHandle.WaitOne (timeout, true))
379 if (!reset.WaitOne (timeout, true))
380 Assert.Ignore ("Timeout");
381 Assert.IsNull (message, message);
384 [DnsPermission (SecurityAction.Deny, Unrestricted = true)]
385 private void ResolveCallback (IAsyncResult ar)
389 // can we do something bad here ?
390 Assert.IsNotNull (Environment.GetEnvironmentVariable ("USERNAME"));
391 message = "Expected a SecurityException";
393 catch (SecurityException) {
397 catch (Exception e) {
398 message = e.ToString ();
403 [EnvironmentPermission (SecurityAction.Deny, Read = "USERNAME")]
404 public void AsyncResolve ()
406 message = "AsyncResolve";
408 IAsyncResult r = Dns.BeginResolve (site, new AsyncCallback (ResolveCallback), null);
409 Assert.IsNotNull (r, "IAsyncResult");
410 // note for some reason r.AsyncWaitHandle.Wait won't work as expected
411 // if (!r.AsyncWaitHandle.WaitOne (timeout, true))
412 if (!reset.WaitOne (timeout, true))
413 Assert.Ignore ("Timeout");
414 Assert.IsNull (message, message);
418 // TODO: New 2.0 methods aren't yet implemented in Mono
420 [DnsPermission (SecurityAction.Deny, Unrestricted = true)]
421 private void GetHostEntryCallback (IAsyncResult ar)
423 Dns.EndGetHostEntry (ar);
425 // can we do something bad here ?
426 Assert.IsNotNull (Environment.GetEnvironmentVariable ("USERNAME"));
427 message = "Expected a SecurityException";
429 catch (SecurityException) {
433 catch (Exception e) {
434 message = e.ToString ();
439 [EnvironmentPermission (SecurityAction.Deny, Read = "USERNAME")]
440 [Ignore ("fails when EndGetHostEntry is called")]
441 public void AsyncGetHostEntry_IPAddress ()
443 message = "AsyncGetHostEntry_IPAddress";
445 IAsyncResult r = Dns.BeginGetHostEntry (ip, new AsyncCallback (GetHostEntryCallback), null);
446 Assert.IsNotNull (r, "IAsyncResult");
447 // note for some reason r.AsyncWaitHandle.Wait won't work as expected
448 // if (!r.AsyncWaitHandle.WaitOne (timeout, true))
449 if (!reset.WaitOne (timeout, true))
450 Assert.Ignore ("Timeout");
451 Assert.IsNull (message, message);
455 [EnvironmentPermission (SecurityAction.Deny, Read = "USERNAME")]
456 public void AsyncGetHostEntry_String ()
458 message = "AsyncGetHostEntry_String";
460 IAsyncResult r = Dns.BeginGetHostEntry (site, new AsyncCallback (GetHostEntryCallback), null);
461 Assert.IsNotNull (r, "IAsyncResult");
462 // note for some reason r.AsyncWaitHandle.Wait won't work as expected
463 // if (!r.AsyncWaitHandle.WaitOne (timeout, true))
464 if (!reset.WaitOne (timeout, true))
465 Assert.Ignore ("Timeout");
466 Assert.IsNull (message, message);
469 [DnsPermission (SecurityAction.Deny, Unrestricted = true)]
470 private void GetHostAddressesCallback (IAsyncResult ar)
472 Dns.EndGetHostEntry (ar);
474 // can we do something bad here ?
475 Assert.IsNotNull (Environment.GetEnvironmentVariable ("USERNAME"));
476 message = "Expected a SecurityException";
478 catch (SecurityException) {
482 catch (Exception e) {
483 message = e.ToString ();
488 [EnvironmentPermission (SecurityAction.Deny, Read = "USERNAME")]
489 public void AsyncGetHostAddresses ()
491 message = "AsyncGetHostAddresses";
493 IAsyncResult r = Dns.BeginGetHostAddresses (site, new AsyncCallback (GetHostAddressesCallback), null);
494 Assert.IsNotNull (r, "IAsyncResult");
495 // note for some reason r.AsyncWaitHandle.Wait won't work as expected
496 // if (!r.AsyncWaitHandle.WaitOne (timeout, true))
497 if (!reset.WaitOne (timeout, true))
498 Assert.Ignore ("Timeout");
499 Assert.IsNull (message, message);