* roottypes.cs: Rename from tree.cs.
[mono.git] / mcs / class / System / Test / System.Net.Sockets / SocketTest.cs
1 // System.Net.Sockets.SocketTest.cs
2 //
3 // Authors:
4 //    Brad Fitzpatrick (brad@danga.com)
5 //    Gonzalo Paniagua Javier (gonzalo@novell.com)
6 //
7 // (C) Copyright 2003 Brad Fitzpatrick
8 // Copyright (c) 2005 Novell, Inc. (http://www.novell.com)
9 //
10
11 using System;
12 using System.Collections;
13 using System.Threading;
14 using System.Net;
15 using System.Net.Sockets;
16 using NUnit.Framework;
17
18 namespace MonoTests.System.Net.Sockets
19 {
20         [TestFixture]
21         public class SocketTest
22         {
23                 // note: also used in SocketCas tests
24                 public const string BogusAddress = "192.168.244.244";
25                 public const int BogusPort = 23483;
26
27                 [Test]
28                 public void ConnectIPAddressAny ()
29                 {
30                         IPEndPoint ep = new IPEndPoint (IPAddress.Any, 0);
31
32                         try {
33                                 using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp)) {
34                                         s.Connect (ep);
35                                         s.Close ();
36                                 }
37                                 Assert.Fail ("#1");
38                         } catch (SocketException ex) {
39                                 Assert.AreEqual (10049, ex.ErrorCode, "#2");
40                         }
41
42                         try {
43                                 using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) {
44                                         s.Connect (ep);
45                                         s.Close ();
46                                 }
47                                 Assert.Fail ("#3");
48                         } catch (SocketException ex) {
49                                 Assert.AreEqual (10049, ex.ErrorCode, "#4");
50                         }
51                 }
52
53                 [Test]
54                 [Ignore ("Bug #75158")]
55                 public void IncompatibleAddress ()
56                 {
57                         IPEndPoint epIPv6 = new IPEndPoint (IPAddress.IPv6Any,
58                                                                 0);
59
60                         try {
61                                 using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP)) {
62                                         s.Connect (epIPv6);
63                                         s.Close ();
64                                 }
65                                 Assert.Fail ("#1");
66                         } catch (SocketException ex) {
67 #if !NET_2_0
68                                 // invalid argument
69                                 int expectedError = 10022;
70 #else
71                                 // address incompatible with protocol
72                                 int expectedError = 10047;
73 #endif
74                                 Assert.AreEqual (expectedError, ex.ErrorCode,
75                                                 "#2");
76                         }
77                 }
78
79                 [Test]
80                 [Category ("InetAccess")]
81                 public void EndConnect ()
82                 {
83                     IPAddress ipOne = IPAddress.Parse (BogusAddress);
84                     IPEndPoint ipEP = new IPEndPoint (ipOne, BogusPort);
85                     Socket sock = new Socket (ipEP.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
86                     IAsyncResult ar = sock.BeginConnect (ipEP, null, null);
87                     bool gotException = false;
88
89                     try {
90                         sock.EndConnect (ar);  // should raise an exception because connect was bogus
91                     } catch {
92                         gotException = true;
93                     }
94
95                     Assertion.AssertEquals ("A01", gotException, true);
96                 }
97
98                 [Test]
99                 [ExpectedException (typeof (ArgumentNullException))]
100                 public void SelectEmpty ()
101                 {
102                         ArrayList list = new ArrayList ();
103                         Socket.Select (list, list, list, 1000);
104                 }
105                 
106                 private bool BlockingConnect (bool block)
107                 {
108                         IPEndPoint ep = new IPEndPoint(IPAddress.Loopback, 1234);
109                         Socket server = new Socket(AddressFamily.InterNetwork,
110                                                    SocketType.Stream,
111                                                    ProtocolType.Tcp);
112                         server.Bind(ep);
113                         server.Blocking=block;
114
115                         server.Listen(0);
116
117                         Socket conn = new Socket (AddressFamily.InterNetwork,
118                                                   SocketType.Stream,
119                                                   ProtocolType.Tcp);
120                         conn.Connect (ep);
121
122                         Socket client = server.Accept();
123                         bool client_block = client.Blocking;
124
125                         client.Close();
126                         conn.Close();
127                         server.Close();
128                         
129                         return(client_block);
130                 }
131
132                 [Test]
133                 public void AcceptBlockingStatus()
134                 {
135                         bool block;
136
137                         block = BlockingConnect(true);
138                         Assertion.AssertEquals ("BlockingStatus01",
139                                                 block, true);
140
141                         block = BlockingConnect(false);
142                         Assertion.AssertEquals ("BlockingStatus02",
143                                                 block, false);
144                 }
145
146                 [Test]
147 #if !NET_2_0
148                 [ExpectedException (typeof (ArgumentException))]
149 #endif
150                 public void SetSocketOptionBoolean ()
151                 {
152                         IPEndPoint ep = new IPEndPoint (IPAddress.Loopback, 1);
153                         Socket sock = new Socket (ep.Address.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
154                         try {
155                                 sock.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.KeepAlive, true);
156                         } finally {
157                                 sock.Close ();
158                         }
159                 }
160
161                 [Test]
162                 public void TestSelect1 ()
163                 {
164                         Socket srv = CreateServer ();
165                         ClientSocket clnt = new ClientSocket (srv.LocalEndPoint);
166                         Thread th = new Thread (new ThreadStart (clnt.ConnectSleepClose));
167                         Socket acc = null;
168                         try {
169                                 th.Start ();
170                                 acc = srv.Accept ();
171                                 clnt.Write ();
172                                 ArrayList list = new ArrayList ();
173                                 ArrayList empty = new ArrayList ();
174                                 list.Add (acc);
175                                 Socket.Select (list, empty, empty, 100);
176                                 Assertion.AssertEquals ("#01", 0, empty.Count);
177                                 Assertion.AssertEquals ("#02", 1, list.Count);
178                                 Socket.Select (empty, list, empty, 100);
179                                 Assertion.AssertEquals ("#03", 0, empty.Count);
180                                 Assertion.AssertEquals ("#04", 1, list.Count);
181                                 Socket.Select (list, empty, empty, -1);
182                                 Assertion.AssertEquals ("#05", 0, empty.Count);
183                                 Assertion.AssertEquals ("#06", 1, list.Count);
184                         } finally {
185                                 if (acc != null)
186                                         acc.Close ();
187                                 srv.Close ();
188                         }
189                 }
190
191                 static Socket CreateServer ()
192                 {
193                         Socket sock = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
194                         sock.Bind (new IPEndPoint (IPAddress.Loopback, 0));
195                         sock.Listen (1);
196                         return sock;
197                 }
198
199                 class ClientSocket {
200                         Socket sock;
201                         EndPoint ep;
202
203                         public ClientSocket (EndPoint ep)
204                         {
205                                 this.ep = ep;
206                                 sock = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
207                         }
208
209                         public void ConnectSleepClose ()
210                         {
211                                 sock.Connect (ep);
212                                 Thread.Sleep (2000);
213                                 sock.Close ();
214                         }
215
216                         public void Write ()
217                         {
218                                 byte [] b = new byte [10];
219                                 sock.Send (b);
220                         }
221                 }
222
223                 byte[] buf = new byte[100];
224
225                 [Test]
226                 [ExpectedException (typeof (ObjectDisposedException))]
227                 public void Disposed1 ()
228                 {
229                         Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
230                         EndPoint ep = new IPEndPoint (IPAddress.Any, 31337);
231                         s.Close();
232
233                         s.ReceiveFrom (buf, ref ep);
234                 }
235
236                 [Test]
237                 [ExpectedException (typeof (ObjectDisposedException))]
238                 public void Disposed2 ()
239                 {
240                         Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
241                         s.Close();
242
243                         s.Blocking = true;
244                 }
245
246                 [Test]
247                 [ExpectedException (typeof (ObjectDisposedException))]
248                 public void Disposed3 ()
249                 {
250                         Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
251                         s.Close();
252
253                         s.GetSocketOption (0, 0);
254                 }
255
256                 [Test]
257                 [ExpectedException (typeof (ObjectDisposedException))]
258                 public void Disposed4 ()
259                 {
260                         Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
261                         s.Close();
262
263                         s.GetSocketOption (0, 0, null);
264                 }
265
266                 [Test]
267                 [ExpectedException (typeof (ObjectDisposedException))]
268                 public void Disposed5 ()
269                 {
270                         Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
271                         s.Close();
272
273                         s.GetSocketOption (0, 0, 0);
274                 }
275
276                 [Test]
277                 [ExpectedException (typeof (ObjectDisposedException))]
278                 public void Disposed6 ()
279                 {
280                         Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
281                         s.Close();
282
283                         s.Listen (5);
284                 }
285
286                 [Test]
287                 [ExpectedException (typeof (ObjectDisposedException))]
288                 public void Disposed7 ()
289                 {
290                         Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
291                         s.Close();
292
293                         s.Poll (100, 0);
294                 }
295
296                 [Test]
297                 [ExpectedException (typeof (ObjectDisposedException))]
298                 public void Disposed8 ()
299                 {
300                         Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
301                         s.Close();
302
303                         s.Receive (buf);
304                 }
305
306                 [Test]
307                 [ExpectedException (typeof (ObjectDisposedException))]
308                 public void Disposed9 ()
309                 {
310                         Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
311                         s.Close();
312
313                         s.Receive (buf, 0);
314                 }
315
316                 [Test]
317                 [ExpectedException (typeof (ObjectDisposedException))]
318                 public void Disposed10 ()
319                 {
320                         Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
321                         s.Close();
322
323                         s.Receive (buf, 10, 0);
324                 }
325
326                 [Test]
327                 [ExpectedException (typeof (ObjectDisposedException))]
328                 public void Disposed11 ()
329                 {
330                         Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
331                         EndPoint ep = new IPEndPoint (IPAddress.Any, 31337);
332                         s.Close();
333
334                         s.Receive (buf, 0, 10, 0);
335                 }
336
337                 [Test]
338                 [ExpectedException (typeof (ObjectDisposedException))]
339                 public void Disposed12 ()
340                 {
341                         Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
342                         EndPoint ep = new IPEndPoint (IPAddress.Any, 31337);
343                         s.Close();
344
345                         s.ReceiveFrom (buf, 0, ref ep);
346                 }
347
348                 [Test]
349                 [ExpectedException (typeof (ObjectDisposedException))]
350                 public void Disposed13 ()
351                 {
352                         Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
353                         EndPoint ep = new IPEndPoint (IPAddress.Any, 31337);
354                         s.Close();
355
356                         s.ReceiveFrom (buf, 10, 0, ref ep);
357                 }
358
359                 [Test]
360                 [ExpectedException (typeof (ObjectDisposedException))]
361                 public void Disposed14 ()
362                 {
363                         Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
364                         EndPoint ep = new IPEndPoint (IPAddress.Any, 31337);
365                         s.Close();
366
367                         s.ReceiveFrom (buf, 0, 10, 0, ref ep);
368                 }
369
370                 [Test]
371                 [ExpectedException (typeof (ObjectDisposedException))]
372                 public void Disposed15 ()
373                 {
374                         Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
375                         s.Close();
376
377                         s.Send (buf);
378                 }
379
380                 [Test]
381                 [ExpectedException (typeof (ObjectDisposedException))]
382                 public void Disposed16 ()
383                 {
384                         Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
385                         s.Close();
386
387                         s.Send (buf, 0);
388                 }
389
390                 [Test]
391                 [ExpectedException (typeof (ObjectDisposedException))]
392                 public void Disposed17 ()
393                 {
394                         Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
395                         s.Close();
396
397                         s.Send (buf, 10, 0);
398                 }
399
400                 [Test]
401                 [ExpectedException (typeof (ObjectDisposedException))]
402                 public void Disposed18 ()
403                 {
404                         Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
405                         EndPoint ep = new IPEndPoint (IPAddress.Any, 31337);
406                         s.Close();
407
408                         s.Send (buf, 0, 10, 0);
409                 }
410
411                 [Test]
412                 [ExpectedException (typeof (ObjectDisposedException))]
413                 public void Disposed19 ()
414                 {
415                         Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
416                         EndPoint ep = new IPEndPoint (IPAddress.Any, 31337);
417                         s.Close();
418
419                         s.SendTo (buf, 0, ep);
420                 }
421
422                 [Test]
423                 [ExpectedException (typeof (ObjectDisposedException))]
424                 public void Disposed20 ()
425                 {
426                         Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
427                         EndPoint ep = new IPEndPoint (IPAddress.Any, 31337);
428                         s.Close();
429
430                         s.SendTo (buf, 10, 0, ep);
431                 }
432
433                 [Test]
434                 [ExpectedException (typeof (ObjectDisposedException))]
435                 public void Disposed21 ()
436                 {
437                         Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
438                         EndPoint ep = new IPEndPoint (IPAddress.Any, 31337);
439                         s.Close();
440
441                         s.SendTo (buf, 0, 10, 0, ep);
442                 }
443
444                 [Test]
445                 [ExpectedException (typeof (ObjectDisposedException))]
446                 public void Disposed22 ()
447                 {
448                         Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
449                         EndPoint ep = new IPEndPoint (IPAddress.Any, 31337);
450                         s.Close();
451
452                         s.SendTo (buf, ep);
453                 }
454
455                 [Test]
456                 [ExpectedException (typeof (ObjectDisposedException))]
457                 public void Disposed23 ()
458                 {
459                         Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
460                         EndPoint ep = new IPEndPoint (IPAddress.Any, 31337);
461                         s.Close();
462
463                         s.Shutdown (0);
464                 }
465         }
466 }
467