2 // SocketCas.cs - CAS unit tests for System.Net.WebRequest class
5 // Sebastien Pouliot <sebastien@ximian.com>
7 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
10 using NUnit.Framework;
15 using System.Net.Sockets;
16 using System.Security;
17 using System.Security.Permissions;
19 using System.Threading;
21 using MonoTests.System.Net.Sockets;
23 namespace MonoCasTests.System.Net.Sockets {
27 public class SocketCas {
29 private const int timeout = 30000;
31 static ManualResetEvent reset;
32 private string message;
37 public void FixtureSetUp ()
39 reset = new ManualResetEvent (false);
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);
49 public void FixtureTearDown ()
57 if (!SecurityManager.SecurityEnabled)
58 Assert.Ignore ("SecurityManager.SecurityEnabled is OFF");
61 // async tests (for stack propagation)
63 private void AcceptCallback (IAsyncResult ar)
66 // can we do something bad here ?
67 Assert.IsNotNull (Environment.GetEnvironmentVariable ("USERNAME"));
68 message = "Expected a SecurityException";
70 catch (SecurityException) {
75 message = e.ToString ();
80 [EnvironmentPermission (SecurityAction.Deny, Read = "USERNAME")]
81 public void AsyncAccept ()
83 IPEndPoint ep = new IPEndPoint (IPAddress.Loopback, 16279);
84 Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
87 message = "AsyncAccept";
89 IAsyncResult r = s.BeginAccept (new AsyncCallback (AcceptCallback), s);
90 Assert.IsNotNull (r, "IAsyncResult");
92 Socket c = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
95 if (!reset.WaitOne (timeout, true))
96 Assert.Ignore ("Timeout");
97 Assert.IsNull (message, message);
100 private void ConnectCallback (IAsyncResult ar)
102 Socket s = (Socket)ar.AsyncState;
105 // can we do something bad here ?
106 Assert.IsNotNull (Environment.GetEnvironmentVariable ("USERNAME"));
107 message = "Expected a SecurityException";
109 catch (SecurityException) {
113 catch (Exception e) {
114 message = e.ToString ();
119 [EnvironmentPermission (SecurityAction.Deny, Read = "USERNAME")]
120 public void AsyncConnect ()
122 message = "AsyncConnect";
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);
133 private void ReceiveCallback (IAsyncResult ar)
135 Socket s = (Socket)ar.AsyncState;
138 // can we do something bad here ?
139 Assert.IsNotNull (Environment.GetEnvironmentVariable ("USERNAME"));
140 message = "Expected a SecurityException";
142 catch (SecurityException) {
146 catch (Exception e) {
147 message = e.ToString ();
152 [EnvironmentPermission (SecurityAction.Deny, Read = "USERNAME")]
153 public void AsyncReceive ()
155 message = "AsyncReceive";
158 NetworkStream ns = new NetworkStream (socket, false);
159 StreamWriter sw = new StreamWriter (ns);
160 sw.Write ("GET / HTTP/1.0\n\n");
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);
171 private void ReceiveFromCallback (IAsyncResult ar)
173 Socket s = (Socket)ar.AsyncState;
174 s.EndReceiveFrom (ar, ref ep);
176 // can we do something bad here ?
177 Assert.IsNotNull (Environment.GetEnvironmentVariable ("USERNAME"));
178 message = "Expected a SecurityException";
180 catch (SecurityException) {
184 catch (Exception e) {
185 message = e.ToString ();
190 [EnvironmentPermission (SecurityAction.Deny, Read = "USERNAME")]
191 public void AsyncReceiveFrom ()
193 message = "AsyncReceiveFrom";
196 NetworkStream ns = new NetworkStream (socket, false);
197 StreamWriter sw = new StreamWriter (ns);
198 sw.Write ("GET / HTTP/1.0\n\n");
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);
209 private void SendCallback (IAsyncResult ar)
211 Socket s = (Socket)ar.AsyncState;
214 // can we do something bad here ?
215 Assert.IsNotNull (Environment.GetEnvironmentVariable ("USERNAME"));
216 message = "Expected a SecurityException";
218 catch (SecurityException) {
222 catch (Exception e) {
223 message = e.ToString ();
228 [EnvironmentPermission (SecurityAction.Deny, Read = "USERNAME")]
229 public void AsyncSend ()
231 message = "AsyncSend";
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);
243 private void SendToCallback (IAsyncResult ar)
245 Socket s = (Socket)ar.AsyncState;
248 // can we do something bad here ?
249 Assert.IsNotNull (Environment.GetEnvironmentVariable ("USERNAME"));
250 message = "Expected a SecurityException";
252 catch (SecurityException) {
256 catch (Exception e) {
257 message = e.ToString ();
262 [EnvironmentPermission (SecurityAction.Deny, Read = "USERNAME")]
263 public void AsyncSendTo ()
265 message = "AsyncSendTo";
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);