Merge pull request #1222 from LogosBible/uri-trycreate
[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                                 Assert.AreEqual (0, tcpClient.Available, "#A2");
86                                 Assert.IsTrue (tcpClient.Connected, "#A3");
87                                 // Assert.IsFalse (tcpClient.ExclusiveAddressUse, "#A4");
88                                 tcpClient.Close ();
89                                 Assert.IsNotNull (tcpClient.Client, "#A5");
90                                 try {
91                                         int available = tcpClient.Available;
92                                         Assert.Fail ("#A6: " + available);
93                                 } catch (ObjectDisposedException) {
94                                 }
95                                 Assert.IsFalse (tcpClient.Connected, "#A7");
96                                 // not supported on linux
97                                 /*
98                                 try {
99                                         bool exclusive = tcpClient.ExclusiveAddressUse;
100                                         Assert.Fail ("#A8: " + exclusive);
101                                 } catch (ObjectDisposedException) {
102                                 }
103                                 */
104                         }
105
106                         using (SocketResponder sr = new SocketResponder (localEP, new SocketRequestHandler (CloseRequestHandler))) {
107                                 sr.Start ();
108
109                                 TcpClient tcpClient = new TcpClient (IPAddress.Loopback.ToString (), 8765);
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 }