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.example.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 [Category ("InetAccess")]
82 public void AsyncAccept ()
84 IPEndPoint ep
= new IPEndPoint (IPAddress
.Loopback
, 16279);
85 Socket s
= new Socket (AddressFamily
.InterNetwork
, SocketType
.Stream
, ProtocolType
.Tcp
);
88 message
= "AsyncAccept";
90 IAsyncResult r
= s
.BeginAccept (new AsyncCallback (AcceptCallback
), s
);
91 Assert
.IsNotNull (r
, "IAsyncResult");
93 Socket c
= new Socket (AddressFamily
.InterNetwork
, SocketType
.Stream
, ProtocolType
.Tcp
);
96 if (!reset
.WaitOne (timeout
, true))
97 Assert
.Ignore ("Timeout");
98 Assert
.IsNull (message
, message
);
101 private void ConnectCallback (IAsyncResult ar
)
103 Socket s
= (Socket
)ar
.AsyncState
;
106 // can we do something bad here ?
107 Assert
.IsNotNull (Environment
.GetEnvironmentVariable ("USERNAME"));
108 message
= "Expected a SecurityException";
110 catch (SecurityException
) {
114 catch (Exception e
) {
115 message
= e
.ToString ();
120 [EnvironmentPermission (SecurityAction
.Deny
, Read
= "USERNAME")]
121 [Category ("InetAccess")]
122 public void AsyncConnect ()
124 message
= "AsyncConnect";
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
);
135 private void ReceiveCallback (IAsyncResult ar
)
137 Socket s
= (Socket
)ar
.AsyncState
;
140 // can we do something bad here ?
141 Assert
.IsNotNull (Environment
.GetEnvironmentVariable ("USERNAME"));
142 message
= "Expected a SecurityException";
144 catch (SecurityException
) {
148 catch (Exception e
) {
149 message
= e
.ToString ();
154 [EnvironmentPermission (SecurityAction
.Deny
, Read
= "USERNAME")]
155 [Category ("InetAccess")]
156 public void AsyncReceive ()
158 message
= "AsyncReceive";
161 NetworkStream ns
= new NetworkStream (socket
, false);
162 StreamWriter sw
= new StreamWriter (ns
);
163 sw
.Write ("GET / HTTP/1.0\n\n");
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
);
174 private void ReceiveFromCallback (IAsyncResult ar
)
176 Socket s
= (Socket
)ar
.AsyncState
;
177 s
.EndReceiveFrom (ar
, ref ep
);
179 // can we do something bad here ?
180 Assert
.IsNotNull (Environment
.GetEnvironmentVariable ("USERNAME"));
181 message
= "Expected a SecurityException";
183 catch (SecurityException
) {
187 catch (Exception e
) {
188 message
= e
.ToString ();
193 [EnvironmentPermission (SecurityAction
.Deny
, Read
= "USERNAME")]
194 [Category ("InetAccess")]
195 public void AsyncReceiveFrom ()
197 message
= "AsyncReceiveFrom";
200 NetworkStream ns
= new NetworkStream (socket
, false);
201 StreamWriter sw
= new StreamWriter (ns
);
202 sw
.Write ("GET / HTTP/1.0\n\n");
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
);
213 private void SendCallback (IAsyncResult ar
)
215 Socket s
= (Socket
)ar
.AsyncState
;
218 // can we do something bad here ?
219 Assert
.IsNotNull (Environment
.GetEnvironmentVariable ("USERNAME"));
220 message
= "Expected a SecurityException";
222 catch (SecurityException
) {
226 catch (Exception e
) {
227 message
= e
.ToString ();
232 [EnvironmentPermission (SecurityAction
.Deny
, Read
= "USERNAME")]
233 [Category ("InetAccess")]
234 public void AsyncSend ()
236 message
= "AsyncSend";
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
);
248 private void SendToCallback (IAsyncResult ar
)
250 Socket s
= (Socket
)ar
.AsyncState
;
253 // can we do something bad here ?
254 Assert
.IsNotNull (Environment
.GetEnvironmentVariable ("USERNAME"));
255 message
= "Expected a SecurityException";
257 catch (SecurityException
) {
261 catch (Exception e
) {
262 message
= e
.ToString ();
267 [EnvironmentPermission (SecurityAction
.Deny
, Read
= "USERNAME")]
268 [Category ("InetAccess")]
269 public void AsyncSendTo ()
271 message
= "AsyncSendTo";
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
);