[2020-02][System.Native] Handle ReadDir EINTR (#21029)
[mono-project.git] / mcs / class / System / Test / System.Net.Sockets / TcpListenerTest.cs
blobbf63ddb4d6c2278c79ad11cc444a020b18930378
1 // System.Net.Sockets.TcpListenerTest.cs
2 //
3 // Authors:
4 // Phillip Pearson (pp@myelin.co.nz)
5 // Martin Willemoes Hansen (mwh@sysrq.dk)
6 // Gonzalo Paniagua Javier (gonzalo@ximian.com)
7 //
8 // (C) Copyright 2001 Phillip Pearson (http://www.myelin.co.nz)
9 // (C) Copyright 2003 Martin Willemoes Hansen (mwh@sysrq.dk)
10 // (c) 2003 Ximian, Inc. (http://www.ximian.com)
13 using System;
14 using System.Net;
15 using System.Net.Sockets;
16 using NUnit.Framework;
18 using MonoTests.Helpers;
20 namespace MonoTests.System.Net.Sockets
22 [TestFixture]
23 public class TcpListenerTest
25 [Test]
26 #if FEATURE_NO_BSD_SOCKETS
27 [ExpectedException (typeof (PlatformNotSupportedException))]
28 #endif
29 public void TcpListener ()
31 // listen with a new listener (IPv4 is the default)
32 TcpListener inListener = NetworkHelpers.CreateAndStartTcpListener (out int port);
35 // connect to it from a new socket
36 IPHostEntry hostent = Dns.GetHostByAddress (IPAddress.Loopback);
37 Socket outSock = null;
39 foreach (IPAddress address in hostent.AddressList) {
40 if (address.AddressFamily == AddressFamily.InterNetwork) {
41 /// Only keep IPv4 addresses, our Server is in IPv4 only mode.
42 outSock = new Socket (address.AddressFamily, SocketType.Stream,
43 ProtocolType.IP);
44 IPEndPoint remote = new IPEndPoint (address, port);
45 outSock.Connect (remote);
46 break;
50 // There is no guarantee that the connecting socket will be in the listener's
51 // accept queue yet (though it is highly likely on Linux). We wait up to one
52 // second for the connecting socket to enter the listener's accept queue.
53 Assert.IsTrue (inListener.Server.Poll (1000, SelectMode.SelectRead));
54 Assert.IsTrue (inListener.Pending ());
55 Socket inSock = inListener.AcceptSocket ();
57 // now send some data and see if it comes out the other end
58 const int len = 1024;
59 byte[] outBuf = new Byte [len];
60 for (int i=0; i<len; i++)
61 outBuf [i] = (byte) (i % 256);
63 outSock.Send (outBuf, 0, len, 0);
65 byte[] inBuf = new Byte[len];
66 int ret = inSock.Receive (inBuf, 0, len, 0);
69 // let's see if it arrived OK
70 Assert.IsTrue (ret != 0);
71 for (int i=0; i<len; i++)
72 Assert.IsTrue (inBuf[i] == outBuf [i]);
74 // tidy up after ourselves
75 inSock.Close ();
77 inListener.Stop ();
80 [Test]
81 #if FEATURE_NO_BSD_SOCKETS
82 [ExpectedException (typeof (PlatformNotSupportedException))]
83 #endif
84 public void CtorInt1 ()
86 int nex = 0;
87 try { new TcpListener (-1); } catch { nex++; }
88 new TcpListener (0);
89 new TcpListener (65535);
90 try { new TcpListener (65536); } catch { nex++; }
91 try { new TcpListener (100000); } catch { nex++; }
92 Assert.IsTrue (nex == 3);
95 [Test]
96 #if FEATURE_NO_BSD_SOCKETS && !WASM
97 [ExpectedException (typeof (PlatformNotSupportedException))]
98 #else
99 [ExpectedException (typeof (ArgumentNullException))]
100 #endif
101 public void CtorIPEndPoint ()
103 new TcpListener (null);
106 [Test]
107 #if FEATURE_NO_BSD_SOCKETS && !WASM
108 [ExpectedException (typeof (PlatformNotSupportedException))]
109 #else
110 [ExpectedException (typeof (ArgumentNullException))]
111 #endif
112 public void CtorIPAddressInt1 ()
114 new TcpListener (null, 100000);
117 [Test]
118 #if FEATURE_NO_BSD_SOCKETS && !WASM
119 [ExpectedException (typeof (PlatformNotSupportedException))]
120 #else
121 [ExpectedException (typeof (ArgumentOutOfRangeException))]
122 #endif
123 public void CtorIPAddressInt2 ()
125 new TcpListener (IPAddress.Any, 100000);
128 class MyListener : TcpListener
130 public MyListener ()
131 : base (IPAddress.Loopback, 0)
135 public Socket GetSocket ()
137 return Server;
140 public bool IsActive {
141 get { return Active; }
145 [Test]
146 #if FEATURE_NO_BSD_SOCKETS
147 [ExpectedException (typeof (PlatformNotSupportedException))]
148 #endif
149 public void PreStartStatus ()
151 MyListener listener = new MyListener ();
152 Assert.AreEqual (false, listener.IsActive, "#01");
153 Assert.IsTrue (null != listener.GetSocket (), "#02");
154 try {
155 listener.AcceptSocket ();
156 Assert.Fail ("Exception not thrown");
157 } catch (InvalidOperationException) {
160 try {
161 listener.AcceptTcpClient ();
162 Assert.Fail ("Exception not thrown");
163 } catch (InvalidOperationException) {
166 try {
167 listener.Pending ();
168 Assert.Fail ("Exception not thrown");
169 } catch (InvalidOperationException) {
172 listener.Stop ();
175 [Test]
176 #if FEATURE_NO_BSD_SOCKETS
177 [ExpectedException (typeof (PlatformNotSupportedException))]
178 #endif
179 public void PostStartStatus ()
181 MyListener listener = new MyListener ();
182 listener.Start ();
183 Assert.AreEqual (true, listener.IsActive, "#01");
184 Assert.IsTrue (null != listener.GetSocket (), "#02");
186 Socket sock = listener.GetSocket ();
187 listener.Start (); // Start called twice
188 Assert.AreEqual (true, listener.IsActive, "#03");
189 Assert.IsTrue (null != listener.GetSocket (), "#04");
191 Assert.AreEqual (false, listener.Pending (), "#05");
193 listener.Stop ();
194 Assert.AreEqual (false, listener.IsActive, "#06");
195 Assert.IsTrue (null != listener.GetSocket (), "#07");
196 Assert.IsTrue (sock != listener.GetSocket (), "#08");
199 [Test]
200 #if FEATURE_NO_BSD_SOCKETS
201 [ExpectedException (typeof (PlatformNotSupportedException))]
202 #endif
203 public void StartListenMoreThan5 ()
205 TcpListener listen = new TcpListener (IPAddress.Loopback, 0);
207 listen.Start (6);
208 listen.Stop ();
210 listen.Start (256);
211 listen.Stop ();
213 listen.Start (1024);
214 listen.Stop ();
216 listen.Start (32768);
217 listen.Stop ();
219 listen.Start (65536);
220 listen.Stop ();
223 [Test]
224 #if FEATURE_NO_BSD_SOCKETS
225 [ExpectedException (typeof (PlatformNotSupportedException))]
226 #endif
227 public void EndAcceptTcpClient ()
229 var listenerSocket = NetworkHelpers.CreateAndStartTcpListener (IPAddress.Any, out int port);
230 listenerSocket.BeginAcceptTcpClient (new AsyncCallback (l => {
231 listenerSocket.EndAcceptTcpClient (l);
232 }), null);
235 using (var outClient = new TcpClient ("localhost", port)) {
236 using (var stream = outClient.GetStream ()) {
237 stream.WriteByte (3);