In corlib/System.Runtime.InteropServices:
[mono.git] / mcs / class / System / Test / System.Net.Sockets / UdpClientTest.cs
1 // System.Net.Sockets.UdpClientTest.cs
2 //
3 // Authors:
4 //      Chris Bacon <chris.bacon@docobo.co.uk>
5 //
6
7
8 using System;
9 using System.Net;
10 using System.Net.Sockets;
11 using System.Threading;
12 using NUnit.Framework;
13
14 namespace MonoTests.System.Net.Sockets {\r
15 #if TARGET_JVM\r
16     [Ignore("UdpClient is not supported - since UDP sockets are not supported")]\r
17 #endif
18     [TestFixture]
19         public class UdpClientTest {
20                 [Test]
21                 public void UdpClientBroadcastTest () 
22                 {
23                         bool exThrown = false;
24                         UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Loopback, 1234));
25                         byte[] bytes = new byte[] {10, 11, 12, 13};
26
27                         try {
28                                 client.Send (bytes, bytes.Length, new IPEndPoint (IPAddress.Broadcast, 1235));
29                         } catch (SocketException) {
30                                 exThrown = true;
31                         }
32                         Assert.IsFalse(exThrown, "UdpClient Broadcast #1");
33
34                         client.Close ();
35                 }
36
37 #if NET_2_0
38                 [Test]
39                 public void JoinMulticastGroup ()
40                 {
41                         UdpClient client = new UdpClient ();
42                         IPAddress mcast_addr = IPAddress.Parse ("224.0.0.23");
43                         IPAddress local_addr = Dns.GetHostEntry ("").AddressList[0];
44                         bool exThrown = false;
45                         
46                         /* So much for the documented note "You cannot
47                          * call JoinMulticastGroup on a UdpClient
48                          * constructed without a specific local port
49                          * (that is, using the UdpClient or
50                          * UdpClient(AddressFamily) constructor).
51                          */
52                         try {
53                                 client.JoinMulticastGroup (mcast_addr,
54                                                            local_addr);
55                         } catch (Exception) {
56                                 exThrown = true;
57                         } finally {
58                                 client.Close ();
59                         }
60                         
61                         Assert.IsFalse (exThrown,
62                                         "UdpClient JoinMulticastGroup #1");
63                 }
64                 
65                 [Test]
66                 [ExpectedException (typeof(ArgumentNullException))]
67                 public void BeginSendNull ()
68                 {
69                         UdpClient client = new UdpClient ();
70                         
71                         client.BeginSend (null, 0, null, null);
72
73                         client.Close ();
74                 }
75                 
76                 static bool BSSent = false;
77                 static int BSBytes;
78                 static ManualResetEvent BSCalledBack = new ManualResetEvent (false);
79                 
80                 private static void BSCallback (IAsyncResult asyncResult)
81                 {
82                         UdpClient client = (UdpClient)asyncResult.AsyncState;
83                         
84                         BSBytes = client.EndSend (asyncResult);
85                         
86                         BSSent = true;
87                         BSCalledBack.Set ();
88                 }
89                 
90                 [Test]
91                 public void BeginSend ()
92                 {
93                         UdpClient client = new UdpClient ();
94                         byte[] bytes = new byte[] {10, 11, 12, 13};
95
96                         try {
97                                 client.BeginSend (bytes, bytes.Length, new AsyncCallback (BSCallback), client);
98                                 Assert.Fail ("BeginSend #1");
99                         } catch (SocketException ex) {
100                                 Assert.AreEqual (10057, ex.ErrorCode,
101                                                  "BeginSend #2");
102                         }
103                         
104                         try {
105                                 client.BeginSend (bytes, bytes.Length, null, new AsyncCallback (BSCallback), client);
106                                 Assert.Fail ("BeginSend #3");
107                         } catch (SocketException ex) {
108                                 Assert.AreEqual (10057, ex.ErrorCode,
109                                                  "BeginSend #4");
110                         }
111
112                         IPEndPoint ep = new IPEndPoint (Dns.GetHostEntry ("").AddressList[0], 1236);
113                         
114                         BSCalledBack.Reset ();
115                         
116                         client.BeginSend (bytes, bytes.Length, ep,
117                                           new AsyncCallback (BSCallback),
118                                           client);
119                         if (BSCalledBack.WaitOne (2000, false) == false) {
120                                 Assert.Fail ("BeginSend wait timed out");
121                         }
122                         
123                         Assertion.AssertEquals ("BeginSend #5", true, BSSent);
124                         Assertion.AssertEquals ("BeginSend #6", 4, BSBytes);
125
126                         client.Close ();
127                 }
128                 
129                 static bool BRReceived = false;
130                 static byte[] BRBytes;
131                 static IPEndPoint BRFrom;
132                 static ManualResetEvent BRCalledBack = new ManualResetEvent (false);
133                 
134                 private static void BRCallback (IAsyncResult asyncResult)
135                 {
136                         UdpClient client = (UdpClient)asyncResult.AsyncState;
137                         
138                         BRBytes = client.EndReceive (asyncResult, ref BRFrom);
139                         
140                         BRReceived = true;
141                         BRCalledBack.Set ();
142                 }
143                 
144                 [Test]
145                 public void BeginReceive ()
146                 {
147                         UdpClient client = new UdpClient (1237);
148                         
149                         BRCalledBack.Reset ();
150                         
151                         client.BeginReceive (BRCallback, client);
152
153                         IPEndPoint ep = new IPEndPoint (Dns.GetHostEntry ("").AddressList[0], 1237);
154                         byte[] send_bytes = new byte[] {10, 11, 12, 13};
155                         client.Send (send_bytes, send_bytes.Length, ep);
156
157                         if (BRCalledBack.WaitOne (2000, false) == false) {
158                                 Assert.Fail ("BeginReceive wait timed out");
159                         }
160                         
161                         Assertion.AssertEquals ("BeginReceive #1", true,
162                                                 BRReceived);
163                         Assertion.AssertEquals ("BeginReceive #2", 4,
164                                                 BRBytes.Length);
165                         Assertion.AssertEquals ("BeginReceive #3", ep. Port,
166                                                 BRFrom.Port);
167                         Assertion.AssertEquals ("BeginReceive #4", ep.Address,
168                                                 BRFrom.Address);
169
170                         client.Close ();
171                 }
172                 
173                 [Test]
174                 public void Available ()
175                 {
176                         UdpClient client = new UdpClient (1238);
177                         IPEndPoint ep = new IPEndPoint (Dns.GetHostEntry ("").AddressList[0], 1238);
178                         byte[] bytes = new byte[] {10, 11, 12, 13};
179                         
180                         client.Send (bytes, bytes.Length, ep);
181                         int avail = client.Available;
182                         
183                         Assertion.AssertEquals ("Available #1", bytes.Length,
184                                                 avail);
185
186                         client.Close ();
187                 }
188                 
189                 [Test]
190                 [Category ("NotWorking")]  // Using PMTU settings workaround on Linux, default true
191                 public void DontFragmentDefault ()
192                 {
193                         UdpClient client = new UdpClient ();
194                         
195                         /* Ignore the docs, testing shows the default
196                          * here is in fact false
197                          */
198                         Assertion.AssertEquals ("DontFragmentDefault", false,
199                                                 client.DontFragment);
200
201                         client.Close ();
202                 }
203                 
204                 [Test]
205                 public void EnableBroadcastDefault ()
206                 {
207                         UdpClient client = new UdpClient ();
208                         
209                         Assertion.AssertEquals ("EnableBroadcastDefault",
210                                                 false, client.EnableBroadcast);
211
212                         client.Close ();
213                 }
214                 
215                 /* Can't test the default for ExclusiveAddressUse as
216                  * it's different on different versions and service
217                  * packs of windows
218                  */
219                 [Test]
220                 [Category ("NotWorking")] // Not supported on Linux
221                 public void ExclusiveAddressUseUnbound ()
222                 {
223                         UdpClient client = new UdpClient ();
224
225                         client.ExclusiveAddressUse = true;
226
227                         Assertion.AssertEquals ("ExclusiveAddressUseUnbound",
228                                                 true,
229                                                 client.ExclusiveAddressUse);
230
231                         client.Close ();
232                 }
233                 
234                 [Test]
235                 [ExpectedException (typeof(InvalidOperationException))]
236                 [Category ("NotWorking")] // Not supported on Linux
237                 public void ExclusiveAddressUseBound ()
238                 {
239                         UdpClient client = new UdpClient (1239);
240
241                         client.ExclusiveAddressUse = true;
242
243                         client.Close ();
244                 }
245                 
246                 [Test]
247                 public void MulticastLoopbackDefault ()
248                 {
249                         UdpClient client = new UdpClient ();
250                         
251                         Assertion.AssertEquals ("MulticastLoopbackDefault",
252                                                 true,
253                                                 client.MulticastLoopback);
254
255                         client.Close ();
256                 }
257                 
258                 /* No test for Ttl default as it is platform dependent */
259 #endif
260         }
261 }