Welcome to mirror list, hosted at ThFree Co, Russian Federation.

TcpListenerTest.cs « System.Net.Sockets « Test « System « class « mcs - github.com/mono/mono.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 37e5969b139f10fc221e46eb2cf8ec14d9c2bc93 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
// System.Net.Sockets.TcpListenerTest.cs
//
// Authors:
//    Phillip Pearson (pp@myelin.co.nz)
//    Martin Willemoes Hansen (mwh@sysrq.dk)
//    Gonzalo Paniagua Javier (gonzalo@ximian.com)
//
// (C) Copyright 2001 Phillip Pearson (http://www.myelin.co.nz)
// (C) Copyright 2003 Martin Willemoes Hansen (mwh@sysrq.dk)
// (c) 2003 Ximian, Inc. (http://www.ximian.com)
//

using System;
using System.Net;
using System.Net.Sockets;
using NUnit.Framework;

namespace MonoTests.System.Net.Sockets
{
	[TestFixture]
	public class TcpListenerTest
	{
		[Test]
		public void TcpListener ()
		{
			// listen with a new listener (IPv4 is the default)
			TcpListener inListener = new TcpListener (8766);
			inListener.Start();
			

			// connect to it from a new socket
			IPHostEntry hostent = Dns.GetHostByAddress (IPAddress.Loopback);
			Socket outSock = null;

			foreach (IPAddress address in hostent.AddressList) {
				if (address.AddressFamily == AddressFamily.InterNetwork) {
					/// Only keep IPv4 addresses, our Server is in IPv4 only mode.
					outSock = new Socket (address.AddressFamily, SocketType.Stream,
						ProtocolType.IP);
					IPEndPoint remote = new IPEndPoint (address, 8766);
					outSock.Connect (remote);
					break;
				}
			}
			
			// make sure the connection arrives
			Assert.IsTrue (inListener.Pending ());
			Socket inSock = inListener.AcceptSocket ();

			// now send some data and see if it comes out the other end
			const int len = 1024;
			byte[] outBuf = new Byte [len];
			for (int i=0; i<len; i++) 
				outBuf [i] = (byte) (i % 256);

			outSock.Send (outBuf, 0, len, 0);

			byte[] inBuf = new Byte[len];
			int ret = inSock.Receive (inBuf, 0, len, 0);


			// let's see if it arrived OK
			Assert.IsTrue (ret != 0);
			for (int i=0; i<len; i++) 
				Assert.IsTrue (inBuf[i] == outBuf [i]);

			// tidy up after ourselves
			inSock.Close ();

			inListener.Stop ();
		}

		[Test]
		public void CtorInt1 ()
		{
			int nex = 0;
			try { new TcpListener (-1); } catch { nex++; }
			new TcpListener (0);
			new TcpListener (65535);
			try { new TcpListener (65536); } catch { nex++; }
			try { new TcpListener (100000); } catch { nex++; }
			Assert.IsTrue (nex == 3);			
		}

		[Test]
		[ExpectedException (typeof (ArgumentNullException))]
		public void CtorIPEndPoint ()
		{
			new TcpListener (null);
		}

		[Test]
		[ExpectedException (typeof (ArgumentNullException))]
		public void CtorIPAddressInt1 ()
		{
			new TcpListener (null, 100000);
		}

		[Test]
		[ExpectedException (typeof (ArgumentOutOfRangeException))]
		public void CtorIPAddressInt2 ()
		{
			new TcpListener (IPAddress.Any, 100000);
		}

		class MyListener : TcpListener
		{
			public MyListener ()
				: base (IPAddress.Loopback, 5000)
			{
			}

			public Socket GetSocket ()
			{
				return Server;
			}

			public bool IsActive {
				get { return Active; }
			}
		}

		[Test]
		public void PreStartStatus ()
		{
			MyListener listener = new MyListener ();
			Assert.AreEqual (false, listener.IsActive, "#01");
			Assert.IsTrue (null != listener.GetSocket (), "#02");
			try {
				listener.AcceptSocket ();
				Assert.Fail ("Exception not thrown");
			} catch (InvalidOperationException) {
			}

			try {
				listener.AcceptTcpClient ();
				Assert.Fail ("Exception not thrown");
			} catch (InvalidOperationException) {
			}

			try {
				listener.Pending ();
				Assert.Fail ("Exception not thrown");
			} catch (InvalidOperationException) {
			}

			listener.Stop ();
		}

		[Test]
		public void PostStartStatus ()
		{
			MyListener listener = new MyListener ();
			listener.Start ();
			Assert.AreEqual (true, listener.IsActive, "#01");
			Assert.IsTrue (null != listener.GetSocket (), "#02");
			
			Socket sock = listener.GetSocket ();
			listener.Start (); // Start called twice
			Assert.AreEqual (true, listener.IsActive, "#03");
			Assert.IsTrue (null != listener.GetSocket (), "#04");

			Assert.AreEqual (false, listener.Pending (), "#05");

			listener.Stop ();
			Assert.AreEqual (false, listener.IsActive, "#06");
			Assert.IsTrue (null != listener.GetSocket (), "#07");
			Assert.IsTrue (sock != listener.GetSocket (), "#08");
		}

		[Test]
		public void StartListenMoreThan5 ()
		{
			TcpListener listen = new TcpListener (IPAddress.Loopback, 1234);

			listen.Start (6);
			listen.Stop ();
			
			listen.Start (256);
			listen.Stop ();
			
			listen.Start (1024);
			listen.Stop ();

			listen.Start (32768);
			listen.Stop ();
			
			listen.Start (65536);
			listen.Stop ();
		}
	}
}