2004-10-14 Dick Porter <dick@ximian.com>
[mono.git] / mcs / class / System / Test / System.Net.Sockets / SocketTest.cs
1 // System.Net.Sockets.TcpClientTest.cs
2 //
3 // Authors:
4 //    Brad Fitzpatrick (brad@danga.com)
5 //
6 // (C) Copyright 2003 Brad Fitzpatrick
7 //
8
9 using System;
10 using System.Collections;
11 using System.Net;
12 using System.Net.Sockets;
13 using NUnit.Framework;
14
15 namespace MonoTests.System.Net.Sockets
16 {
17         [TestFixture]
18         public class SocketTest
19         {
20                 [Test]
21                 public void EndConnect ()
22                 {
23                     IPAddress ipOne = IPAddress.Parse ("192.168.244.244");   // something bogus
24                     IPEndPoint ipEP = new IPEndPoint (ipOne, 23483);  // something bogus
25                     Socket sock = new Socket (ipEP.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
26                     IAsyncResult ar = sock.BeginConnect (ipEP, null, null);
27                     bool gotException = false;
28
29                     try {
30                         sock.EndConnect (ar);  // should raise an exception because connect was bogus
31                     } catch {
32                         gotException = true;
33                     }
34
35                     Assertion.AssertEquals ("A01", gotException, true);
36                 }
37
38                 [Test]
39                 [ExpectedException (typeof (ArgumentNullException))]
40                 public void SelectEmpty ()
41                 {
42                         ArrayList list = new ArrayList ();
43                         Socket.Select (list, list, list, 1000);
44                 }
45                 
46                 private bool BlockingConnect (bool block)
47                 {
48                         IPEndPoint ep = new IPEndPoint(IPAddress.Any, 1234);
49                         Socket server = new Socket(AddressFamily.InterNetwork,
50                                                    SocketType.Stream,
51                                                    ProtocolType.Tcp);
52                         server.Bind(ep);
53                         server.Blocking=block;
54
55                         server.Listen(0);
56
57                         Socket conn = new Socket (AddressFamily.InterNetwork,
58                                                   SocketType.Stream,
59                                                   ProtocolType.Tcp);
60                         conn.Connect (ep);
61
62                         Socket client = server.Accept();
63                         bool client_block = client.Blocking;
64
65                         client.Close();
66                         conn.Close();
67                         server.Close();
68                         
69                         return(client_block);
70                 }
71
72                 [Test]
73                 public void AcceptBlockingStatus()
74                 {
75                         bool block;
76
77                         block = BlockingConnect(true);
78                         Assertion.AssertEquals ("BlockingStatus01",
79                                                 block, true);
80
81                         block = BlockingConnect(false);
82                         Assertion.AssertEquals ("BlockingStatus02",
83                                                 block, false);
84                 }
85         }
86
87 }
88