* roottypes.cs: Rename from tree.cs.
[mono.git] / mcs / class / System / Test / System.Net.Sockets / SocketCas.cs
1 //
2 // SocketCas.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.Net.Sockets;
16 using System.Security;
17 using System.Security.Permissions;
18 using System.Text;
19 using System.Threading;
20
21 using MonoTests.System.Net.Sockets;
22
23 namespace MonoCasTests.System.Net.Sockets {
24
25         [TestFixture]
26         [Category ("CAS")]
27         public class SocketCas {
28
29                 private const int timeout = 30000;
30
31                 static ManualResetEvent reset;
32                 private string message;
33                 static Socket socket;
34                 static EndPoint ep;
35
36                 [TestFixtureSetUp]
37                 public void FixtureSetUp ()
38                 {
39                         reset = new ManualResetEvent (false);
40
41                         IPHostEntry host = Dns.Resolve ("www.google.com");
42                         IPAddress ip = host.AddressList[0];
43                         ep = new IPEndPoint (ip, 80);
44                         socket = new Socket (ip.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
45                         socket.Connect (ep);
46                 }
47
48                 [TestFixtureTearDown]
49                 public void FixtureTearDown ()
50                 {
51                         reset.Close ();
52                 }
53
54                 [SetUp]
55                 public void SetUp ()
56                 {
57                         if (!SecurityManager.SecurityEnabled)
58                                 Assert.Ignore ("SecurityManager.SecurityEnabled is OFF");
59                 }
60
61                 // async tests (for stack propagation)
62
63                 private void AcceptCallback (IAsyncResult ar)
64                 {
65                         try {
66                                 // can we do something bad here ?
67                                 Assert.IsNotNull (Environment.GetEnvironmentVariable ("USERNAME"));
68                                 message = "Expected a SecurityException";
69                         }
70                         catch (SecurityException) {
71                                 message = null;
72                                 reset.Set ();
73                         }
74                         catch (Exception e) {
75                                 message = e.ToString ();
76                         }
77                 }
78
79                 [Test]
80                 [EnvironmentPermission (SecurityAction.Deny, Read = "USERNAME")]
81                 [Category ("InetAccess")]
82                 public void AsyncAccept ()
83                 {
84                         IPEndPoint ep = new IPEndPoint (IPAddress.Loopback, 16279);
85                         Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
86                         s.Bind (ep);
87                         s.Listen (0);
88                         message = "AsyncAccept";
89                         reset.Reset ();
90                         IAsyncResult r = s.BeginAccept (new AsyncCallback (AcceptCallback), s);
91                         Assert.IsNotNull (r, "IAsyncResult");
92
93                         Socket c = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
94                         c.Connect (ep);
95
96                         if (!reset.WaitOne (timeout, true))
97                                 Assert.Ignore ("Timeout");
98                         Assert.IsNull (message, message);
99                 }
100
101                 private void ConnectCallback (IAsyncResult ar)
102                 {
103                         Socket s = (Socket)ar.AsyncState;
104                         s.EndConnect (ar);
105                         try {
106                                 // can we do something bad here ?
107                                 Assert.IsNotNull (Environment.GetEnvironmentVariable ("USERNAME"));
108                                 message = "Expected a SecurityException";
109                         }
110                         catch (SecurityException) {
111                                 message = null;
112                                 reset.Set ();
113                         }
114                         catch (Exception e) {
115                                 message = e.ToString ();
116                         }
117                 }
118
119                 [Test]
120                 [EnvironmentPermission (SecurityAction.Deny, Read = "USERNAME")]
121                 [Category ("InetAccess")]
122                 public void AsyncConnect ()
123                 {
124                         message = "AsyncConnect";
125                         reset.Reset ();
126
127                         Socket s = new Socket (ep.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
128                         IAsyncResult r = s.BeginConnect (ep, new AsyncCallback (ConnectCallback), s);
129                         Assert.IsNotNull (r, "IAsyncResult");
130                         if (!reset.WaitOne (timeout, true))
131                                 Assert.Ignore ("Timeout");
132                         Assert.IsNull (message, message);
133                 }
134
135                 private void ReceiveCallback (IAsyncResult ar)
136                 {
137                         Socket s = (Socket)ar.AsyncState;
138                         s.EndReceive (ar);
139                         try {
140                                 // can we do something bad here ?
141                                 Assert.IsNotNull (Environment.GetEnvironmentVariable ("USERNAME"));
142                                 message = "Expected a SecurityException";
143                         }
144                         catch (SecurityException) {
145                                 message = null;
146                                 reset.Set ();
147                         }
148                         catch (Exception e) {
149                                 message = e.ToString ();
150                         }
151                 }
152
153                 [Test]
154                 [EnvironmentPermission (SecurityAction.Deny, Read = "USERNAME")]
155                 [Category ("InetAccess")]
156                 public void AsyncReceive ()
157                 {
158                         message = "AsyncReceive";
159                         reset.Reset ();
160
161                         NetworkStream ns = new NetworkStream (socket, false);
162                         StreamWriter sw = new StreamWriter (ns);
163                         sw.Write ("GET / HTTP/1.0\n\n");
164                         sw.Flush ();
165
166                         IAsyncResult r = socket.BeginReceive (new byte[1024], 0, 1024, 
167                                 SocketFlags.None, new AsyncCallback (ReceiveCallback), socket);
168                         Assert.IsNotNull (r, "IAsyncResult");
169                         if (!reset.WaitOne (timeout, true))
170                                 Assert.Ignore ("Timeout");
171                         Assert.IsNull (message, message);
172                 }
173
174                 private void ReceiveFromCallback (IAsyncResult ar)
175                 {
176                         Socket s = (Socket)ar.AsyncState;
177                         s.EndReceiveFrom (ar, ref ep);
178                         try {
179                                 // can we do something bad here ?
180                                 Assert.IsNotNull (Environment.GetEnvironmentVariable ("USERNAME"));
181                                 message = "Expected a SecurityException";
182                         }
183                         catch (SecurityException) {
184                                 message = null;
185                                 reset.Set ();
186                         }
187                         catch (Exception e) {
188                                 message = e.ToString ();
189                         }
190                 }
191
192                 [Test]
193                 [EnvironmentPermission (SecurityAction.Deny, Read = "USERNAME")]
194                 [Category ("InetAccess")]
195                 public void AsyncReceiveFrom ()
196                 {
197                         message = "AsyncReceiveFrom";
198                         reset.Reset ();
199
200                         NetworkStream ns = new NetworkStream (socket, false);
201                         StreamWriter sw = new StreamWriter (ns);
202                         sw.Write ("GET / HTTP/1.0\n\n");
203                         sw.Flush ();
204
205                         IAsyncResult r = socket.BeginReceiveFrom (new byte[1024], 0, 1024,
206                                 SocketFlags.None, ref ep, new AsyncCallback (ReceiveFromCallback), socket);
207                         Assert.IsNotNull (r, "IAsyncResult");
208                         if (!reset.WaitOne (timeout, true))
209                                 Assert.Ignore ("Timeout");
210                         Assert.IsNull (message, message);
211                 }
212
213                 private void SendCallback (IAsyncResult ar)
214                 {
215                         Socket s = (Socket)ar.AsyncState;
216                         s.EndSend (ar);
217                         try {
218                                 // can we do something bad here ?
219                                 Assert.IsNotNull (Environment.GetEnvironmentVariable ("USERNAME"));
220                                 message = "Expected a SecurityException";
221                         }
222                         catch (SecurityException) {
223                                 message = null;
224                                 reset.Set ();
225                         }
226                         catch (Exception e) {
227                                 message = e.ToString ();
228                         }
229                 }
230
231                 [Test]
232                 [EnvironmentPermission (SecurityAction.Deny, Read = "USERNAME")]
233                 [Category ("InetAccess")]
234                 public void AsyncSend ()
235                 {
236                         message = "AsyncSend";
237                         reset.Reset ();
238
239                         byte[] get = Encoding.ASCII.GetBytes ("GET / HTTP/1.0\n\n");
240                         IAsyncResult r = socket.BeginSend (get, 0, get.Length, SocketFlags.None, 
241                                 new AsyncCallback (SendCallback), socket);
242                         Assert.IsNotNull (r, "IAsyncResult");
243                         if (!reset.WaitOne (timeout, true))
244                                 Assert.Ignore ("Timeout");
245                         Assert.IsNull (message, message);
246                 }
247
248                 private void SendToCallback (IAsyncResult ar)
249                 {
250                         Socket s = (Socket)ar.AsyncState;
251                         s.EndSendTo (ar);
252                         try {
253                                 // can we do something bad here ?
254                                 Assert.IsNotNull (Environment.GetEnvironmentVariable ("USERNAME"));
255                                 message = "Expected a SecurityException";
256                         }
257                         catch (SecurityException) {
258                                 message = null;
259                                 reset.Set ();
260                         }
261                         catch (Exception e) {
262                                 message = e.ToString ();
263                         }
264                 }
265
266                 [Test]
267                 [EnvironmentPermission (SecurityAction.Deny, Read = "USERNAME")]
268                 [Category ("InetAccess")]
269                 public void AsyncSendTo ()
270                 {
271                         message = "AsyncSendTo";
272                         reset.Reset ();
273
274                         byte[] get = Encoding.ASCII.GetBytes ("GET / HTTP/1.0\n\n");
275                         IAsyncResult r = socket.BeginSendTo (get, 0, get.Length, SocketFlags.None, 
276                                 ep, new AsyncCallback (SendToCallback), socket);
277                         Assert.IsNotNull (r, "IAsyncResult");
278                         if (!reset.WaitOne (timeout, true))
279                                 Assert.Ignore ("Timeout");
280                         Assert.IsNull (message, message);
281                 }
282         }
283 }