[System*] Throw a PlatformNotSupported exception when using the networking stack...
[mono.git] / mcs / class / System / Test / System.Net.Sockets / TcpListenerTest.cs
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)
11 //
12
13 using System;
14 using System.Net;
15 using System.Net.Sockets;
16 using NUnit.Framework;
17
18 using MonoTests.Helpers;
19
20 namespace MonoTests.System.Net.Sockets
21 {
22         [TestFixture]
23         public class TcpListenerTest
24         {
25                 [Test]
26 #if FEATURE_NO_BSD_SOCKETS
27                 [ExpectedException (typeof (PlatformNotSupportedException))]
28 #endif
29                 public void TcpListener ()
30                 {
31                         var port = NetworkHelpers.FindFreePort ();
32                         // listen with a new listener (IPv4 is the default)
33                         TcpListener inListener = new TcpListener (port);
34                         inListener.Start();
35                         
36
37                         // connect to it from a new socket
38                         IPHostEntry hostent = Dns.GetHostByAddress (IPAddress.Loopback);
39                         Socket outSock = null;
40
41                         foreach (IPAddress address in hostent.AddressList) {
42                                 if (address.AddressFamily == AddressFamily.InterNetwork) {
43                                         /// Only keep IPv4 addresses, our Server is in IPv4 only mode.
44                                         outSock = new Socket (address.AddressFamily, SocketType.Stream,
45                                                 ProtocolType.IP);
46                                         IPEndPoint remote = new IPEndPoint (address, port);
47                                         outSock.Connect (remote);
48                                         break;
49                                 }
50                         }
51                         
52                         // make sure the connection arrives
53                         Assert.IsTrue (inListener.Pending ());
54                         Socket inSock = inListener.AcceptSocket ();
55
56                         // now send some data and see if it comes out the other end
57                         const int len = 1024;
58                         byte[] outBuf = new Byte [len];
59                         for (int i=0; i<len; i++) 
60                                 outBuf [i] = (byte) (i % 256);
61
62                         outSock.Send (outBuf, 0, len, 0);
63
64                         byte[] inBuf = new Byte[len];
65                         int ret = inSock.Receive (inBuf, 0, len, 0);
66
67
68                         // let's see if it arrived OK
69                         Assert.IsTrue (ret != 0);
70                         for (int i=0; i<len; i++) 
71                                 Assert.IsTrue (inBuf[i] == outBuf [i]);
72
73                         // tidy up after ourselves
74                         inSock.Close ();
75
76                         inListener.Stop ();
77                 }
78
79                 [Test]
80 #if FEATURE_NO_BSD_SOCKETS
81                 [ExpectedException (typeof (PlatformNotSupportedException))]
82 #endif
83                 public void CtorInt1 ()
84                 {
85                         int nex = 0;
86                         try { new TcpListener (-1); } catch { nex++; }
87                         new TcpListener (0);
88                         new TcpListener (65535);
89                         try { new TcpListener (65536); } catch { nex++; }
90                         try { new TcpListener (100000); } catch { nex++; }
91                         Assert.IsTrue (nex == 3);                       
92                 }
93
94                 [Test]
95 #if FEATURE_NO_BSD_SOCKETS
96                 [ExpectedException (typeof (PlatformNotSupportedException))]
97 #else
98                 [ExpectedException (typeof (ArgumentNullException))]
99 #endif
100                 public void CtorIPEndPoint ()
101                 {
102                         new TcpListener (null);
103                 }
104
105                 [Test]
106 #if FEATURE_NO_BSD_SOCKETS
107                 [ExpectedException (typeof (PlatformNotSupportedException))]
108 #else
109                 [ExpectedException (typeof (ArgumentNullException))]
110 #endif
111                 public void CtorIPAddressInt1 ()
112                 {
113                         new TcpListener (null, 100000);
114                 }
115
116                 [Test]
117 #if FEATURE_NO_BSD_SOCKETS
118                 [ExpectedException (typeof (PlatformNotSupportedException))]
119 #else
120                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
121 #endif
122                 public void CtorIPAddressInt2 ()
123                 {
124                         new TcpListener (IPAddress.Any, 100000);
125                 }
126
127                 class MyListener : TcpListener
128                 {
129                         public MyListener ()
130                                 : base (IPAddress.Loopback, NetworkHelpers.FindFreePort ())
131                         {
132                         }
133
134                         public Socket GetSocket ()
135                         {
136                                 return Server;
137                         }
138
139                         public bool IsActive {
140                                 get { return Active; }
141                         }
142                 }
143
144                 [Test]
145 #if FEATURE_NO_BSD_SOCKETS
146                 [ExpectedException (typeof (PlatformNotSupportedException))]
147 #endif
148                 public void PreStartStatus ()
149                 {
150                         MyListener listener = new MyListener ();
151                         Assert.AreEqual (false, listener.IsActive, "#01");
152                         Assert.IsTrue (null != listener.GetSocket (), "#02");
153                         try {
154                                 listener.AcceptSocket ();
155                                 Assert.Fail ("Exception not thrown");
156                         } catch (InvalidOperationException) {
157                         }
158
159                         try {
160                                 listener.AcceptTcpClient ();
161                                 Assert.Fail ("Exception not thrown");
162                         } catch (InvalidOperationException) {
163                         }
164
165                         try {
166                                 listener.Pending ();
167                                 Assert.Fail ("Exception not thrown");
168                         } catch (InvalidOperationException) {
169                         }
170
171                         listener.Stop ();
172                 }
173
174                 [Test]
175 #if FEATURE_NO_BSD_SOCKETS
176                 [ExpectedException (typeof (PlatformNotSupportedException))]
177 #endif
178                 public void PostStartStatus ()
179                 {
180                         MyListener listener = new MyListener ();
181                         listener.Start ();
182                         Assert.AreEqual (true, listener.IsActive, "#01");
183                         Assert.IsTrue (null != listener.GetSocket (), "#02");
184                         
185                         Socket sock = listener.GetSocket ();
186                         listener.Start (); // Start called twice
187                         Assert.AreEqual (true, listener.IsActive, "#03");
188                         Assert.IsTrue (null != listener.GetSocket (), "#04");
189
190                         Assert.AreEqual (false, listener.Pending (), "#05");
191
192                         listener.Stop ();
193                         Assert.AreEqual (false, listener.IsActive, "#06");
194                         Assert.IsTrue (null != listener.GetSocket (), "#07");
195                         Assert.IsTrue (sock != listener.GetSocket (), "#08");
196                 }
197
198                 [Test]
199 #if FEATURE_NO_BSD_SOCKETS
200                 [ExpectedException (typeof (PlatformNotSupportedException))]
201 #endif
202                 public void StartListenMoreThan5 ()
203                 {
204                         var port = NetworkHelpers.FindFreePort ();
205                         TcpListener listen = new TcpListener (IPAddress.Loopback, port);
206
207                         listen.Start (6);
208                         listen.Stop ();
209                         
210                         listen.Start (256);
211                         listen.Stop ();
212                         
213                         listen.Start (1024);
214                         listen.Stop ();
215
216                         listen.Start (32768);
217                         listen.Stop ();
218                         
219                         listen.Start (65536);
220                         listen.Stop ();
221                 }
222         }
223 }