4d3b8325048d9038ebfcaf8210960ebc71562f1a
[mono.git] / mcs / class / System / Test / System.Net.Sockets / TcpClientTest.cs
1 // System.Net.Sockets.TcpClientTest.cs
2 //
3 // Authors:
4 //    Phillip Pearson (pp@myelin.co.nz)
5 //    Martin Willemoes Hansen (mwh@sysrq.dk)
6 //
7 // (C) Copyright 2001 Phillip Pearson (http://www.myelin.co.nz)
8 // (C) Copyright 2003 Martin Willemoes Hansen
9 //
10
11 using System;
12 using System.Net;
13 using System.Net.Sockets;
14 using NUnit.Framework;
15
16 using MonoTests.Helpers;
17
18 namespace MonoTests.System.Net.Sockets
19 {
20         /// <summary>
21         /// Tests System.Net.Sockets.TcpClient
22         /// </summary>
23         [TestFixture]
24         [Category ("RequiresBSDSockets")]
25         public class TcpClientTest
26         {
27                 
28                 /// <summary>
29                 /// Tests the TcpClient object
30                 /// (from System.Net.Sockets)
31                 /// </summary>
32                 [Test]
33                 public void TcpClient()
34                 {
35                         // set up a listening Socket
36                         Socket lSock = new Socket(AddressFamily.InterNetwork,
37                                 SocketType.Stream, ProtocolType.Tcp);
38                         
39                         var port = NetworkHelpers.FindFreePort ();
40                         lSock.Bind(new IPEndPoint(IPAddress.Any, port));
41                         lSock.Listen(-1);
42
43
44                         // connect to it with a TcpClient
45                         TcpClient outClient = new TcpClient("localhost", port);
46                         Socket inSock = lSock.Accept();
47
48                         
49                         // now try exchanging data
50                         NetworkStream stream = outClient.GetStream();
51
52                         const int len = 1024;
53                         byte[] outBuf = new Byte[len];
54                         for (int i=0; i<len; i++) 
55                         {
56                                 outBuf[i] = (byte)(i % 256);
57                         }
58
59                         // send it
60                         stream.Write(outBuf,0,len);
61
62                         // and see if it comes back
63                         byte[] inBuf = new Byte[len];
64                         int ret = inSock.Receive(inBuf, 0, len, 0);
65                         Assert.IsTrue (ret != 0);
66
67                         for (int i=0; i<len; i++) 
68                         {
69                                 Assert.IsTrue (inBuf[i] == outBuf[i]);
70                         }
71
72                         // tidy up
73                         inSock.Close();
74                         outClient.Close();
75                         lSock.Close();
76                         
77                 }
78
79                 [Test] // bug #81105
80                 public void CloseTest ()
81                 {
82                         var port = NetworkHelpers.FindFreePort ();
83                         IPEndPoint localEP = new IPEndPoint (IPAddress.Loopback, port);
84                         using (SocketResponder sr = new SocketResponder (localEP, s => CloseRequestHandler (s))) {
85                                 TcpClient tcpClient = new TcpClient (IPAddress.Loopback.ToString (), port);
86                                 NetworkStream ns = tcpClient.GetStream ();
87                                 Assert.IsNotNull (ns, "#A1");
88                                 Assert.AreEqual (0, tcpClient.Available, "#A2");
89                                 Assert.IsTrue (tcpClient.Connected, "#A3");
90                                 // Assert.IsFalse (tcpClient.ExclusiveAddressUse, "#A4");
91                                 tcpClient.Close ();
92                                 Assert.IsNotNull (tcpClient.Client, "#A5");
93                                 try {
94                                         int available = tcpClient.Available;
95                                         Assert.Fail ("#A6: " + available);
96                                 } catch (ObjectDisposedException) {
97                                 }
98                                 Assert.IsFalse (tcpClient.Connected, "#A7");
99                                 // not supported on linux
100                                 /*
101                                 try {
102                                         bool exclusive = tcpClient.ExclusiveAddressUse;
103                                         Assert.Fail ("#A8: " + exclusive);
104                                 } catch (ObjectDisposedException) {
105                                 }
106                                 */
107                         }
108
109                         using (SocketResponder sr = new SocketResponder (localEP, s => CloseRequestHandler (s))) {
110                                 TcpClient tcpClient = new TcpClient (IPAddress.Loopback.ToString (), port);
111                                 Assert.AreEqual (0, tcpClient.Available, "#B1");
112                                 Assert.IsTrue (tcpClient.Connected, "#B2");
113                                 // Assert.IsFalse (tcpClient.ExclusiveAddressUse, "#B3");
114                                 tcpClient.Close ();
115                                 Assert.IsNull (tcpClient.Client, "#B4");
116                                 try {
117                                         int available = tcpClient.Available;
118                                         Assert.Fail ("#B5: " + available);
119                                 } catch (NullReferenceException) {
120                                 }
121                                 try {
122                                         bool connected = tcpClient.Connected;
123                                         Assert.Fail ("#B6: " + connected);
124                                 } catch (NullReferenceException) {
125                                 }
126                                 // not supported on linux
127                                 /*
128                                 try {
129                                         bool exclusive = tcpClient.ExclusiveAddressUse;
130                                         Assert.Fail ("#B7: " + exclusive);
131                                 } catch (NullReferenceException) {
132                                 }
133                                 */
134                         }
135                 }
136
137                 byte [] CloseRequestHandler (Socket socket)
138                 {
139                         return new byte [0];
140                 }
141
142                 [Test]
143                 [ExpectedException (typeof(ArgumentNullException))]
144                 public void ConnectMultiNull ()
145                 {
146                         TcpClient client = new TcpClient ();
147                         IPAddress[] ipAddresses = null;
148                         
149                         client.Connect (ipAddresses, 1234);
150                 }
151                 
152                 [Test]
153                 public void ConnectMultiAny ()
154                 {
155                         TcpClient client = new TcpClient ();
156                         IPAddress[] ipAddresses = new IPAddress[1];
157                         
158                         ipAddresses[0] = IPAddress.Any;
159                         
160                         try {
161                                 client.Connect (ipAddresses, 1234);
162                                 Assert.Fail ("ConnectMultiAny #1");
163                         } catch (SocketException ex) {
164                                 Assert.AreEqual (10049, ex.ErrorCode, "ConnectMultiAny #2");
165                         } catch {
166                                 Assert.Fail ("ConnectMultiAny #3");
167                         }
168                 }
169                 
170                 [Test]
171                 public void ConnectMultiRefused ()
172                 {
173                         TcpClient client = new TcpClient ();
174                         IPAddress[] ipAddresses = new IPAddress[1];
175                         
176                         ipAddresses[0] = IPAddress.Loopback;
177                         
178                         try {
179                                 client.Connect (ipAddresses, 1234);
180                                 Assert.Fail ("ConnectMultiRefused #1");
181                         } catch (SocketException ex) {
182                                 Assert.AreEqual (10061, ex.ErrorCode, "ConnectMultiRefused #2");
183                         } catch {
184                                 Assert.Fail ("ConnectMultiRefused #3");
185                         }
186                 }
187         }
188 }