[2020-02][System.Native] Handle ReadDir EINTR (#21029)
[mono-project.git] / mcs / class / System / Test / System.Net.Sockets / SocketCas.cs
blob526b80c2d8d5d4c647015796046edbbe8488bfa5
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 //
10 using NUnit.Framework;
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;
21 using MonoTests.System.Net.Sockets;
23 namespace MonoCasTests.System.Net.Sockets {
25 [TestFixture]
26 [Category ("CAS")]
27 public class SocketCas {
29 private const int timeout = 30000;
31 static ManualResetEvent reset;
32 private string message;
33 static Socket socket;
34 static EndPoint ep;
36 [TestFixtureSetUp]
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);
45 socket.Connect (ep);
48 [TestFixtureTearDown]
49 public void FixtureTearDown ()
51 reset.Close ();
54 [SetUp]
55 public void SetUp ()
57 if (!SecurityManager.SecurityEnabled)
58 Assert.Ignore ("SecurityManager.SecurityEnabled is OFF");
61 // async tests (for stack propagation)
63 private void AcceptCallback (IAsyncResult ar)
65 try {
66 // can we do something bad here ?
67 Assert.IsNotNull (Environment.GetEnvironmentVariable ("USERNAME"));
68 message = "Expected a SecurityException";
70 catch (SecurityException) {
71 message = null;
72 reset.Set ();
74 catch (Exception e) {
75 message = e.ToString ();
79 [Test]
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);
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");
93 Socket c = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
94 c.Connect (ep);
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;
104 s.EndConnect (ar);
105 try {
106 // can we do something bad here ?
107 Assert.IsNotNull (Environment.GetEnvironmentVariable ("USERNAME"));
108 message = "Expected a SecurityException";
110 catch (SecurityException) {
111 message = null;
112 reset.Set ();
114 catch (Exception e) {
115 message = e.ToString ();
119 [Test]
120 [EnvironmentPermission (SecurityAction.Deny, Read = "USERNAME")]
121 [Category ("InetAccess")]
122 public void AsyncConnect ()
124 message = "AsyncConnect";
125 reset.Reset ();
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;
138 s.EndReceive (ar);
139 try {
140 // can we do something bad here ?
141 Assert.IsNotNull (Environment.GetEnvironmentVariable ("USERNAME"));
142 message = "Expected a SecurityException";
144 catch (SecurityException) {
145 message = null;
146 reset.Set ();
148 catch (Exception e) {
149 message = e.ToString ();
153 [Test]
154 [EnvironmentPermission (SecurityAction.Deny, Read = "USERNAME")]
155 [Category ("InetAccess")]
156 public void AsyncReceive ()
158 message = "AsyncReceive";
159 reset.Reset ();
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 ();
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);
178 try {
179 // can we do something bad here ?
180 Assert.IsNotNull (Environment.GetEnvironmentVariable ("USERNAME"));
181 message = "Expected a SecurityException";
183 catch (SecurityException) {
184 message = null;
185 reset.Set ();
187 catch (Exception e) {
188 message = e.ToString ();
192 [Test]
193 [EnvironmentPermission (SecurityAction.Deny, Read = "USERNAME")]
194 [Category ("InetAccess")]
195 public void AsyncReceiveFrom ()
197 message = "AsyncReceiveFrom";
198 reset.Reset ();
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 ();
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;
216 s.EndSend (ar);
217 try {
218 // can we do something bad here ?
219 Assert.IsNotNull (Environment.GetEnvironmentVariable ("USERNAME"));
220 message = "Expected a SecurityException";
222 catch (SecurityException) {
223 message = null;
224 reset.Set ();
226 catch (Exception e) {
227 message = e.ToString ();
231 [Test]
232 [EnvironmentPermission (SecurityAction.Deny, Read = "USERNAME")]
233 [Category ("InetAccess")]
234 public void AsyncSend ()
236 message = "AsyncSend";
237 reset.Reset ();
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;
251 s.EndSendTo (ar);
252 try {
253 // can we do something bad here ?
254 Assert.IsNotNull (Environment.GetEnvironmentVariable ("USERNAME"));
255 message = "Expected a SecurityException";
257 catch (SecurityException) {
258 message = null;
259 reset.Set ();
261 catch (Exception e) {
262 message = e.ToString ();
266 [Test]
267 [EnvironmentPermission (SecurityAction.Deny, Read = "USERNAME")]
268 [Category ("InetAccess")]
269 public void AsyncSendTo ()
271 message = "AsyncSendTo";
272 reset.Reset ();
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);