Merge pull request #5206 from alexrp/profiler-gc-base-init
[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                 public void AsyncAccept ()
82                 {
83                         IPEndPoint ep = new IPEndPoint (IPAddress.Loopback, 16279);
84                         Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
85                         s.Bind (ep);
86                         s.Listen (0);
87                         message = "AsyncAccept";
88                         reset.Reset ();
89                         IAsyncResult r = s.BeginAccept (new AsyncCallback (AcceptCallback), s);
90                         Assert.IsNotNull (r, "IAsyncResult");
91
92                         Socket c = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
93                         c.Connect (ep);
94
95                         if (!reset.WaitOne (timeout, true))
96                                 Assert.Ignore ("Timeout");
97                         Assert.IsNull (message, message);
98                 }
99
100                 private void ConnectCallback (IAsyncResult ar)
101                 {
102                         Socket s = (Socket)ar.AsyncState;
103                         s.EndConnect (ar);
104                         try {
105                                 // can we do something bad here ?
106                                 Assert.IsNotNull (Environment.GetEnvironmentVariable ("USERNAME"));
107                                 message = "Expected a SecurityException";
108                         }
109                         catch (SecurityException) {
110                                 message = null;
111                                 reset.Set ();
112                         }
113                         catch (Exception e) {
114                                 message = e.ToString ();
115                         }
116                 }
117
118                 [Test]
119                 [EnvironmentPermission (SecurityAction.Deny, Read = "USERNAME")]
120                 public void AsyncConnect ()
121                 {
122                         message = "AsyncConnect";
123                         reset.Reset ();
124
125                         Socket s = new Socket (ep.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
126                         IAsyncResult r = s.BeginConnect (ep, new AsyncCallback (ConnectCallback), s);
127                         Assert.IsNotNull (r, "IAsyncResult");
128                         if (!reset.WaitOne (timeout, true))
129                                 Assert.Ignore ("Timeout");
130                         Assert.IsNull (message, message);
131                 }
132
133                 private void ReceiveCallback (IAsyncResult ar)
134                 {
135                         Socket s = (Socket)ar.AsyncState;
136                         s.EndReceive (ar);
137                         try {
138                                 // can we do something bad here ?
139                                 Assert.IsNotNull (Environment.GetEnvironmentVariable ("USERNAME"));
140                                 message = "Expected a SecurityException";
141                         }
142                         catch (SecurityException) {
143                                 message = null;
144                                 reset.Set ();
145                         }
146                         catch (Exception e) {
147                                 message = e.ToString ();
148                         }
149                 }
150
151                 [Test]
152                 [EnvironmentPermission (SecurityAction.Deny, Read = "USERNAME")]
153                 public void AsyncReceive ()
154                 {
155                         message = "AsyncReceive";
156                         reset.Reset ();
157
158                         NetworkStream ns = new NetworkStream (socket, false);
159                         StreamWriter sw = new StreamWriter (ns);
160                         sw.Write ("GET / HTTP/1.0\n\n");
161                         sw.Flush ();
162
163                         IAsyncResult r = socket.BeginReceive (new byte[1024], 0, 1024, 
164                                 SocketFlags.None, new AsyncCallback (ReceiveCallback), socket);
165                         Assert.IsNotNull (r, "IAsyncResult");
166                         if (!reset.WaitOne (timeout, true))
167                                 Assert.Ignore ("Timeout");
168                         Assert.IsNull (message, message);
169                 }
170
171                 private void ReceiveFromCallback (IAsyncResult ar)
172                 {
173                         Socket s = (Socket)ar.AsyncState;
174                         s.EndReceiveFrom (ar, ref ep);
175                         try {
176                                 // can we do something bad here ?
177                                 Assert.IsNotNull (Environment.GetEnvironmentVariable ("USERNAME"));
178                                 message = "Expected a SecurityException";
179                         }
180                         catch (SecurityException) {
181                                 message = null;
182                                 reset.Set ();
183                         }
184                         catch (Exception e) {
185                                 message = e.ToString ();
186                         }
187                 }
188
189                 [Test]
190                 [EnvironmentPermission (SecurityAction.Deny, Read = "USERNAME")]
191                 public void AsyncReceiveFrom ()
192                 {
193                         message = "AsyncReceiveFrom";
194                         reset.Reset ();
195
196                         NetworkStream ns = new NetworkStream (socket, false);
197                         StreamWriter sw = new StreamWriter (ns);
198                         sw.Write ("GET / HTTP/1.0\n\n");
199                         sw.Flush ();
200
201                         IAsyncResult r = socket.BeginReceiveFrom (new byte[1024], 0, 1024,
202                                 SocketFlags.None, ref ep, new AsyncCallback (ReceiveFromCallback), socket);
203                         Assert.IsNotNull (r, "IAsyncResult");
204                         if (!reset.WaitOne (timeout, true))
205                                 Assert.Ignore ("Timeout");
206                         Assert.IsNull (message, message);
207                 }
208
209                 private void SendCallback (IAsyncResult ar)
210                 {
211                         Socket s = (Socket)ar.AsyncState;
212                         s.EndSend (ar);
213                         try {
214                                 // can we do something bad here ?
215                                 Assert.IsNotNull (Environment.GetEnvironmentVariable ("USERNAME"));
216                                 message = "Expected a SecurityException";
217                         }
218                         catch (SecurityException) {
219                                 message = null;
220                                 reset.Set ();
221                         }
222                         catch (Exception e) {
223                                 message = e.ToString ();
224                         }
225                 }
226
227                 [Test]
228                 [EnvironmentPermission (SecurityAction.Deny, Read = "USERNAME")]
229                 public void AsyncSend ()
230                 {
231                         message = "AsyncSend";
232                         reset.Reset ();
233
234                         byte[] get = Encoding.ASCII.GetBytes ("GET / HTTP/1.0\n\n");
235                         IAsyncResult r = socket.BeginSend (get, 0, get.Length, SocketFlags.None, 
236                                 new AsyncCallback (SendCallback), socket);
237                         Assert.IsNotNull (r, "IAsyncResult");
238                         if (!reset.WaitOne (timeout, true))
239                                 Assert.Ignore ("Timeout");
240                         Assert.IsNull (message, message);
241                 }
242
243                 private void SendToCallback (IAsyncResult ar)
244                 {
245                         Socket s = (Socket)ar.AsyncState;
246                         s.EndSendTo (ar);
247                         try {
248                                 // can we do something bad here ?
249                                 Assert.IsNotNull (Environment.GetEnvironmentVariable ("USERNAME"));
250                                 message = "Expected a SecurityException";
251                         }
252                         catch (SecurityException) {
253                                 message = null;
254                                 reset.Set ();
255                         }
256                         catch (Exception e) {
257                                 message = e.ToString ();
258                         }
259                 }
260
261                 [Test]
262                 [EnvironmentPermission (SecurityAction.Deny, Read = "USERNAME")]
263                 public void AsyncSendTo ()
264                 {
265                         message = "AsyncSendTo";
266                         reset.Reset ();
267
268                         byte[] get = Encoding.ASCII.GetBytes ("GET / HTTP/1.0\n\n");
269                         IAsyncResult r = socket.BeginSendTo (get, 0, get.Length, SocketFlags.None, 
270                                 ep, new AsyncCallback (SendToCallback), socket);
271                         Assert.IsNotNull (r, "IAsyncResult");
272                         if (!reset.WaitOne (timeout, true))
273                                 Assert.Ignore ("Timeout");
274                         Assert.IsNull (message, message);
275                 }
276         }
277 }