Merge pull request #2400 from esdrubal/extrahead
[mono.git] / mcs / class / System / Test / System.Net / DnsCas.cs
1 //
2 // DnsCas.cs - CAS unit tests for System.Net.Dns class
3 //
4 // Author:
5 //      Sebastien Pouliot  <sebastien@ximian.com>
6 //
7 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
8 //
9
10 #if !MOBILE
11
12 using NUnit.Framework;
13
14 using System;
15 using System.Net;
16 using System.Security;
17 using System.Security.Permissions;
18 using System.Threading;
19
20 namespace MonoCasTests.System.Net {
21
22         [TestFixture]
23         [Category ("CAS")]
24         [Category ("NotWorking")] // compiler (CSC) issue (on Windows)
25         public class DnsCas {
26
27                 private const string site = "www.go-mono.com";
28                 private const int timeout = 30000;
29
30                 static ManualResetEvent reset;
31                 private string message;
32                 private string hostname;
33
34                 [TestFixtureSetUp]
35                 public void FixtureSetUp ()
36                 {
37                         reset = new ManualResetEvent (false);
38                         hostname = Dns.GetHostName ();
39                         var ip = Dns.Resolve (site).AddressList[0];
40                 }
41
42                 [TestFixtureTearDown]
43                 public void FixtureTearDown ()
44                 {
45                         reset.Close ();
46                 }
47
48                 [SetUp]
49                 public void SetUp ()
50                 {
51                         if (!SecurityManager.SecurityEnabled)
52                                 Assert.Ignore ("SecurityManager.SecurityEnabled is OFF");
53                 }
54
55                 // test Demand by denying it's caller from the required permission
56
57                 [Test]
58                 [DnsPermission (SecurityAction.Deny, Unrestricted = true)]
59                 [ExpectedException (typeof (SecurityException))]
60                 public void Deny_BeginGetHostName ()
61                 {
62                         Dns.BeginGetHostByName (null, null, null);
63                 }
64
65                 [Test]
66                 [DnsPermission (SecurityAction.Deny, Unrestricted = true)]
67                 [ExpectedException (typeof (ArgumentNullException))]
68                 public void Deny_EndGetHostByName ()
69                 {
70                         Dns.EndGetHostByName (null);
71                 }
72
73                 [Test]
74                 [DnsPermission (SecurityAction.Deny, Unrestricted = true)]
75                 [ExpectedException (typeof (SecurityException))]
76                 public void Deny_BeginResolve ()
77                 {
78                         Dns.BeginResolve (null, null, null);
79                 }
80
81                 [Test]
82                 [DnsPermission (SecurityAction.Deny, Unrestricted = true)]
83                 [ExpectedException (typeof (ArgumentNullException))]
84                 public void Deny_EndResolve ()
85                 {
86                         Dns.EndResolve (null);
87                 }
88
89                 [Test]
90                 [DnsPermission (SecurityAction.Deny, Unrestricted = true)]
91                 [ExpectedException (typeof (SecurityException))]
92                 public void Deny_GetHostByAddress_IPAddress ()
93                 {
94                         Dns.GetHostByAddress ((IPAddress)null);
95                 }
96
97                 [Test]
98                 [DnsPermission (SecurityAction.Deny, Unrestricted = true)]
99                 [ExpectedException (typeof (SecurityException))]
100                 public void Deny_GetHostByAddress_String ()
101                 {
102                         Dns.GetHostByAddress ((string)null);
103                 }
104
105                 [Test]
106                 [DnsPermission (SecurityAction.Deny, Unrestricted = true)]
107                 [ExpectedException (typeof (SecurityException))]
108                 public void Deny_GetHostByName ()
109                 {
110                         Dns.GetHostByName (site);
111                 }
112
113                 [Test]
114                 [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
118                 public void Deny_GetHostByName_Null ()
119                 {
120                         Dns.GetHostByName (null);
121                 }
122
123                 [Test]
124                 [DnsPermission (SecurityAction.Deny, Unrestricted = true)]
125                 [ExpectedException (typeof (SecurityException))]
126                 public void Deny_GetHostByName_HostName ()
127                 {
128                         // ... so my first guess is that you can only query 
129                         // yourself without having unrestricted DnsPermission
130                         Assert.IsNotNull (Dns.GetHostByName (hostname));
131                         // but that's wrong :-(
132                 }
133
134                 [Test]
135                 [DnsPermission (SecurityAction.Deny, Unrestricted = true)]
136                 [ExpectedException (typeof (SecurityException))]
137                 public void Deny_GetHostName ()
138                 {
139                         Dns.GetHostName ();
140                 }
141
142                 [Test]
143                 [DnsPermission (SecurityAction.Deny, Unrestricted = true)]
144                 [ExpectedException (typeof (SecurityException))]
145                 public void Deny_Resolve ()
146                 {
147                         Dns.Resolve (null);
148                 }
149
150                 // TODO: New 2.0 methods aren't yet implemented in Mono
151 /*
152                 [Test]
153                 [DnsPermission (SecurityAction.Deny, Unrestricted = true)]
154                 [ExpectedException (typeof (SecurityException))]
155                 public void Deny_BeginGetHostAddresses ()
156                 {
157                         Dns.BeginGetHostAddresses (null, null, null);
158                 }
159
160                 [Test]
161                 [DnsPermission (SecurityAction.Deny, Unrestricted = true)]
162                 [ExpectedException (typeof (ArgumentNullException))]
163                 public void Deny_EndGetHostAddresses ()
164                 {
165                         Dns.EndGetHostAddresses (null);
166                 }
167
168                 [Test]
169                 [DnsPermission (SecurityAction.Deny, Unrestricted = true)]
170                 [ExpectedException (typeof (SecurityException))]
171                 public void Deny_BeginGetHostEntry_IPAddress ()
172                 {
173                         Dns.BeginGetHostEntry ((IPAddress)null, null, null);
174                 }
175
176                 [Test]
177                 [DnsPermission (SecurityAction.Deny, Unrestricted = true)]
178                 [ExpectedException (typeof (SecurityException))]
179                 public void Deny_BeginGetHostEntry_String ()
180                 {
181                         Dns.BeginGetHostEntry ((string)null, null, null);
182                 }
183
184                 [Test]
185                 [DnsPermission (SecurityAction.Deny, Unrestricted = true)]
186                 [ExpectedException (typeof (ArgumentNullException))]
187                 public void Deny_EndGetHostEntry ()
188                 {
189                         Dns.EndGetHostEntry (null);
190                 }
191
192                 [Test]
193                 [DnsPermission (SecurityAction.Deny, Unrestricted = true)]
194                 [ExpectedException (typeof (SecurityException))]
195                 public void Deny_GetHostEntry_IPAddress ()
196                 {
197                         Dns.GetHostEntry ((IPAddress)null);
198                 }
199
200                 [Test]
201                 [DnsPermission (SecurityAction.Deny, Unrestricted = true)]
202                 [ExpectedException (typeof (SecurityException))]
203                 public void Deny_GetHostEntry_String ()
204                 {
205                         Dns.GetHostEntry ((string)null);
206                 }
207
208                 [Test]
209                 [DnsPermission (SecurityAction.Deny, Unrestricted = true)]
210                 [ExpectedException (typeof (SecurityException))]
211                 public void Deny_GetHostAddresses ()
212                 {
213                         Dns.GetHostAddresses (null);
214                 }
215 */
216
217                 // ensure that only DnsPermission is required to call the methods
218
219                 [Test]
220                 [DnsPermission (SecurityAction.PermitOnly, Unrestricted = true)]
221                 [ExpectedException (typeof (ArgumentNullException))]
222                 public void PermitOnly_BeginGetHostName ()
223                 {
224                         Dns.BeginGetHostByName (null, null, null);
225                 }
226
227                 [Test]
228                 [DnsPermission (SecurityAction.PermitOnly, Unrestricted = true)]
229                 [ExpectedException (typeof (ArgumentNullException))]
230                 public void PermitOnly_BeginResolve ()
231                 {
232                         Dns.BeginResolve (null, null, null);
233                 }
234
235                 [Test]
236                 [DnsPermission (SecurityAction.PermitOnly, Unrestricted = true)]
237                 [ExpectedException (typeof (ArgumentNullException))]
238                 public void PermitOnly_GetHostByAddress_IPAddress ()
239                 {
240                         Dns.GetHostByAddress ((IPAddress)null);
241                 }
242
243                 [Test]
244                 [DnsPermission (SecurityAction.PermitOnly, Unrestricted = true)]
245                 [ExpectedException (typeof (ArgumentNullException))]
246                 public void PermitOnly_GetHostByAddress_String ()
247                 {
248                         Dns.GetHostByAddress ((string)null);
249                 }
250
251                 [Test]
252                 [DnsPermission (SecurityAction.PermitOnly, Unrestricted = true)]
253                 [ExpectedException (typeof (ArgumentNullException))]
254                 public void PermitOnly_GetHostByName ()
255                 {
256                         Dns.GetHostByName (null);
257                 }
258
259                 [Test]
260                 [DnsPermission (SecurityAction.PermitOnly, Unrestricted = true)]
261                 public void PermitOnly_GetHostName ()
262                 {
263                         Assert.IsNotNull (Dns.GetHostName ());
264                 }
265
266                 [Test]
267                 [DnsPermission (SecurityAction.PermitOnly, Unrestricted = true)]
268                 [ExpectedException (typeof (ArgumentNullException))]
269                 public void PermitOnly_Resolve ()
270                 {
271                         Dns.Resolve (null);
272                 }
273
274                 // TODO: New 2.0 methods aren't yet implemented in Mono
275 /*
276                 [Test]
277                 [DnsPermission (SecurityAction.PermitOnly, Unrestricted = true)]
278                 [ExpectedException (typeof (ArgumentNullException))]
279                 public void PermitOnly_BeginGetHostAddresses ()
280                 {
281                         Dns.BeginGetHostAddresses (null, null, null);
282                 }
283
284                 [Test]
285                 [DnsPermission (SecurityAction.PermitOnly, Unrestricted = true)]
286                 [ExpectedException (typeof (ArgumentNullException))]
287                 public void PermitOnly_EndGetHostAddresses ()
288                 {
289                         Dns.EndGetHostAddresses (null);
290                 }
291
292                 [Test]
293                 [DnsPermission (SecurityAction.PermitOnly, Unrestricted = true)]
294                 [ExpectedException (typeof (ArgumentNullException))]
295                 public void PermitOnly_BeginGetHostEntry_IPAddress ()
296                 {
297                         Dns.BeginGetHostEntry ((IPAddress)null, null, null);
298                 }
299
300                 [Test]
301                 [DnsPermission (SecurityAction.PermitOnly, Unrestricted = true)]
302                 [ExpectedException (typeof (ArgumentNullException))]
303                 public void PermitOnly_BeginGetHostEntry_String ()
304                 {
305                         Dns.BeginGetHostEntry ((string)null, null, null);
306                 }
307
308                 [Test]
309                 [DnsPermission (SecurityAction.PermitOnly, Unrestricted = true)]
310                 [ExpectedException (typeof (ArgumentNullException))]
311                 public void PermitOnly_EndGetHostEntry ()
312                 {
313                         Dns.EndGetHostEntry (null);
314                 }
315
316                 [Test]
317                 [DnsPermission (SecurityAction.PermitOnly, Unrestricted = true)]
318                 [ExpectedException (typeof (ArgumentNullException))]
319                 public void PermitOnly_GetHostEntry_IPAddress ()
320                 {
321                         Dns.GetHostEntry ((IPAddress)null);
322                 }
323
324                 [Test]
325                 [DnsPermission (SecurityAction.PermitOnly, Unrestricted = true)]
326                 [ExpectedException (typeof (ArgumentNullException))]
327                 public void PermitOnly_GetHostEntry_String ()
328                 {
329                         Dns.GetHostEntry ((string)null);
330                 }
331
332                 [Test]
333                 [DnsPermission (SecurityAction.PermitOnly, Unrestricted = true)]
334                 [ExpectedException (typeof (ArgumentNullException))]
335                 public void PermitOnly_GetHostAddresses ()
336                 {
337                         Dns.GetHostAddresses (null);
338                 }
339 */
340
341                 // async tests (for stack propagation)
342
343                 [DnsPermission (SecurityAction.Deny, Unrestricted = true)]
344                 private void GetHostByNameCallback (IAsyncResult ar)
345                 {
346                         Dns.EndGetHostByName (ar);
347                         try {
348                                 // can we do something bad here ?
349                                 Assert.IsNotNull (Environment.GetEnvironmentVariable ("USERNAME"));
350                                 message = "Expected a SecurityException";
351                         }
352                         catch (SecurityException) {
353                                 message = null;
354                                 reset.Set ();
355                         }
356                         catch (Exception e)
357                         {
358                                 message = e.ToString ();
359                         }
360                 }
361
362                 [Test]
363                 [EnvironmentPermission (SecurityAction.Deny, Read = "USERNAME")]
364                 public void AsyncGetHostByName ()
365                 {
366                         message = "AsyncGetHostByName";
367                         reset.Reset ();
368                         IAsyncResult r = Dns.BeginGetHostByName (site, new AsyncCallback (GetHostByNameCallback), null);
369                         Assert.IsNotNull (r, "IAsyncResult");
370                         // note for some reason r.AsyncWaitHandle.Wait won't work as expected
371                         // if (!r.AsyncWaitHandle.WaitOne (timeout, true))
372                         if (!reset.WaitOne (timeout, true))
373                                 Assert.Ignore ("Timeout");
374                         Assert.IsNull (message, message);
375                 }
376
377                 [DnsPermission (SecurityAction.Deny, Unrestricted = true)]
378                 private void ResolveCallback (IAsyncResult ar)
379                 {
380                         Dns.EndResolve (ar);
381                         try {
382                                 // can we do something bad here ?
383                                 Assert.IsNotNull (Environment.GetEnvironmentVariable ("USERNAME"));
384                                 message = "Expected a SecurityException";
385                         }
386                         catch (SecurityException) {
387                                 message = null;
388                                 reset.Set ();
389                         }
390                         catch (Exception e) {
391                                 message = e.ToString ();
392                         }
393                 }
394
395                 [Test]
396                 [EnvironmentPermission (SecurityAction.Deny, Read = "USERNAME")]
397                 public void AsyncResolve ()
398                 {
399                         message = "AsyncResolve";
400                         reset.Reset ();
401                         IAsyncResult r = Dns.BeginResolve (site, new AsyncCallback (ResolveCallback), null);
402                         Assert.IsNotNull (r, "IAsyncResult");
403                         // note for some reason r.AsyncWaitHandle.Wait won't work as expected
404                         // if (!r.AsyncWaitHandle.WaitOne (timeout, true))
405                         if (!reset.WaitOne (timeout, true))
406                                 Assert.Ignore ("Timeout");
407                         Assert.IsNull (message, message);
408                 }
409
410                 // TODO: New 2.0 methods aren't yet implemented in Mono
411 /*
412                 [DnsPermission (SecurityAction.Deny, Unrestricted = true)]
413                 private void GetHostEntryCallback (IAsyncResult ar)
414                 {
415                         Dns.EndGetHostEntry (ar);
416                         try {
417                                 // can we do something bad here ?
418                                 Assert.IsNotNull (Environment.GetEnvironmentVariable ("USERNAME"));
419                                 message = "Expected a SecurityException";
420                         }
421                         catch (SecurityException) {
422                                 message = null;
423                                 reset.Set ();
424                         }
425                         catch (Exception e) {
426                                 message = e.ToString ();
427                         }
428                 }
429
430                 [Test]
431                 [EnvironmentPermission (SecurityAction.Deny, Read = "USERNAME")]
432                 [Ignore ("fails when EndGetHostEntry is called")]
433                 public void AsyncGetHostEntry_IPAddress ()
434                 {
435                         message = "AsyncGetHostEntry_IPAddress";
436                         reset.Reset ();
437                         IAsyncResult r = Dns.BeginGetHostEntry (ip, new AsyncCallback (GetHostEntryCallback), null);
438                         Assert.IsNotNull (r, "IAsyncResult");
439                         // note for some reason r.AsyncWaitHandle.Wait won't work as expected
440                         // if (!r.AsyncWaitHandle.WaitOne (timeout, true))
441                         if (!reset.WaitOne (timeout, true))
442                                 Assert.Ignore ("Timeout");
443                         Assert.IsNull (message, message);
444                 }
445
446                 [Test]
447                 [EnvironmentPermission (SecurityAction.Deny, Read = "USERNAME")]
448                 public void AsyncGetHostEntry_String ()
449                 {
450                         message = "AsyncGetHostEntry_String";
451                         reset.Reset ();
452                         IAsyncResult r = Dns.BeginGetHostEntry (site, new AsyncCallback (GetHostEntryCallback), null);
453                         Assert.IsNotNull (r, "IAsyncResult");
454                         // note for some reason r.AsyncWaitHandle.Wait won't work as expected
455                         // if (!r.AsyncWaitHandle.WaitOne (timeout, true))
456                         if (!reset.WaitOne (timeout, true))
457                                 Assert.Ignore ("Timeout");
458                         Assert.IsNull (message, message);
459                 }
460
461                 [DnsPermission (SecurityAction.Deny, Unrestricted = true)]
462                 private void GetHostAddressesCallback (IAsyncResult ar)
463                 {
464                         Dns.EndGetHostEntry (ar);
465                         try {
466                                 // can we do something bad here ?
467                                 Assert.IsNotNull (Environment.GetEnvironmentVariable ("USERNAME"));
468                                 message = "Expected a SecurityException";
469                         }
470                         catch (SecurityException) {
471                                 message = null;
472                                 reset.Set ();
473                         }
474                         catch (Exception e) {
475                                 message = e.ToString ();
476                         }
477                 }
478
479                 [Test]
480                 [EnvironmentPermission (SecurityAction.Deny, Read = "USERNAME")]
481                 public void AsyncGetHostAddresses ()
482                 {
483                         message = "AsyncGetHostAddresses";
484                         reset.Reset ();
485                         IAsyncResult r = Dns.BeginGetHostAddresses (site, new AsyncCallback (GetHostAddressesCallback), null);
486                         Assert.IsNotNull (r, "IAsyncResult");
487                         // note for some reason r.AsyncWaitHandle.Wait won't work as expected
488                         // if (!r.AsyncWaitHandle.WaitOne (timeout, true))
489                         if (!reset.WaitOne (timeout, true))
490                                 Assert.Ignore ("Timeout");
491                         Assert.IsNull (message, message);
492                 }
493 */
494         }
495 }
496
497 #endif