WebHeaderCollection fixes
[mono.git] / mcs / class / System / Test / System.Net / HttpWebRequestCas.cs
1 //
2 // HttpWebRequestCas.cs - CAS unit tests for System.Net.WebRequest class
3 //
4 // Author:
5 //      Sebastien Pouliot  <sebastien@ximian.com>
6 //
7 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
8 //
9
10 using NUnit.Framework;
11
12 using System;
13 using System.IO;
14 using System.Net;
15 using System.Security;
16 using System.Security.Permissions;
17 using System.Threading;
18
19 namespace MonoCasTests.System.Net {
20
21         [TestFixture]
22         [Category ("CAS")]
23         public class HttpWebRequestCas {
24
25                 private const int timeout = 30000;
26
27                 static ManualResetEvent reset;
28                 private string message;
29                 private string uri = "http://www.google.com";
30
31                 [TestFixtureSetUp]
32                 public void FixtureSetUp ()
33                 {
34                         reset = new ManualResetEvent (false);
35                 }
36
37                 [TestFixtureTearDown]
38                 public void FixtureTearDown ()
39                 {
40                         reset.Close ();
41                 }
42
43                 [SetUp]
44                 public void SetUp ()
45                 {
46                         if (!SecurityManager.SecurityEnabled)
47                                 Assert.Ignore ("SecurityManager.SecurityEnabled is OFF");
48                 }
49
50                 // async tests (for stack propagation)
51
52                 private void GetRequestStreamCallback (IAsyncResult ar)
53                 {
54                         HttpWebRequest hwr = (HttpWebRequest)ar.AsyncState;
55                         hwr.EndGetRequestStream (ar);
56                         try {
57                                 // can we do something bad here ?
58                                 Assert.IsNotNull (Environment.GetEnvironmentVariable ("USERNAME"));
59                                 message = "Expected a SecurityException";
60                         }
61                         catch (SecurityException) {
62                                 message = null;
63                                 reset.Set ();
64                         }
65                         catch (Exception e) {
66                                 message = e.ToString ();
67                         }
68                 }
69
70                 [Test]
71                 [EnvironmentPermission (SecurityAction.Deny, Read = "USERNAME")]
72                 public void AsyncGetRequestStream ()
73                 {
74                         HttpWebRequest w = (HttpWebRequest)WebRequest.Create (uri);
75                         w.Method = "PUT";
76                         message = "AsyncGetRequestStream";
77                         reset.Reset ();
78                         IAsyncResult r = w.BeginGetRequestStream (new AsyncCallback (GetRequestStreamCallback), w);
79                         Assert.IsNotNull (r, "IAsyncResult");
80                         if (!reset.WaitOne (timeout, true))
81                                 Assert.Ignore ("Timeout");
82                         Assert.IsNull (message, message);
83                 }
84
85                 private void GetResponseCallback (IAsyncResult ar)
86                 {
87                         HttpWebRequest hwr = (HttpWebRequest)ar.AsyncState;
88                         hwr.EndGetResponse (ar);
89                         try {
90                                 // can we do something bad here ?
91                                 Assert.IsNotNull (Environment.GetEnvironmentVariable ("USERNAME"));
92                                 message = "Expected a SecurityException";
93                         }
94                         catch (SecurityException) {
95                                 message = null;
96                                 reset.Set ();
97                         }
98                         catch (Exception e) {
99                                 message = e.ToString ();
100                         }
101                 }
102
103                 [Test]
104                 [EnvironmentPermission (SecurityAction.Deny, Read = "USERNAME")]
105                 public void AsyncGetResponse ()
106                 {
107                         HttpWebRequest w = (HttpWebRequest)WebRequest.Create (uri);
108                         message = "AsyncGetResponse";
109                         reset.Reset ();
110                         IAsyncResult r = w.BeginGetResponse (new AsyncCallback (GetResponseCallback), w);
111                         Assert.IsNotNull (r, "IAsyncResult");
112                         if (!reset.WaitOne (timeout, true))
113                                 Assert.Ignore ("Timeout");
114                         Assert.IsNull (message, message);
115                 }
116         }
117 }