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