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