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