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