6df77cbe141d136fda9c49495c2d8ea350fdea81
[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 {
15 #if TARGET_JVM
16     [Ignore("UdpClient is not supported - since UDP sockets are not supported")]
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 for bug 324033
66                 [Test]
67                 public void JoinMulticastGroupWithLocal ()
68                 {
69                         UdpClient client = new UdpClient (9001);
70                         IPAddress mcast_addr = IPAddress.Parse ("224.0.0.24");
71                         IPAddress local_addr = IPAddress.Any;
72                         
73                         bool exThrown = false;
74                         
75                         try {
76                                 client.JoinMulticastGroup (mcast_addr,
77                                                            local_addr);
78                         } catch (Exception) {
79                                 exThrown = true;
80                         } finally {
81                                 client.Close ();
82                         }
83                         
84                         Assert.IsFalse (exThrown, "UdpClient JoinMulticastGroupWithLocal #1");
85                 }
86                 
87                 [Test]
88                 [ExpectedException (typeof(ArgumentNullException))]
89                 public void BeginSendNull ()
90                 {
91                         UdpClient client = new UdpClient ();
92                         
93                         client.BeginSend (null, 0, null, null);
94
95                         client.Close ();
96                 }
97                 
98                 static bool BSSent = false;
99                 static int BSBytes;
100                 static ManualResetEvent BSCalledBack = new ManualResetEvent (false);
101                 
102                 private static void BSCallback (IAsyncResult asyncResult)
103                 {
104                         UdpClient client = (UdpClient)asyncResult.AsyncState;
105                         
106                         BSBytes = client.EndSend (asyncResult);
107                         
108                         BSSent = true;
109                         BSCalledBack.Set ();
110                 }
111                 
112                 [Test]
113                 public void BeginSend ()
114                 {
115                         UdpClient client = new UdpClient ();
116                         byte[] bytes = new byte[] {10, 11, 12, 13};
117
118                         try {
119                                 client.BeginSend (bytes, bytes.Length, new AsyncCallback (BSCallback), client);
120                                 Assert.Fail ("BeginSend #1");
121                         } catch (SocketException ex) {
122                                 Assert.AreEqual (10057, ex.ErrorCode,
123                                                  "BeginSend #2");
124                         }
125                         
126                         try {
127                                 client.BeginSend (bytes, bytes.Length, null, new AsyncCallback (BSCallback), client);
128                                 Assert.Fail ("BeginSend #3");
129                         } catch (SocketException ex) {
130                                 Assert.AreEqual (10057, ex.ErrorCode,
131                                                  "BeginSend #4");
132                         }
133
134                         IPEndPoint ep = new IPEndPoint (Dns.GetHostEntry ("").AddressList[0], 1236);
135                         
136                         BSCalledBack.Reset ();
137                         
138                         client.BeginSend (bytes, bytes.Length, ep,
139                                           new AsyncCallback (BSCallback),
140                                           client);
141                         if (BSCalledBack.WaitOne (2000, false) == false) {
142                                 Assert.Fail ("BeginSend wait timed out");
143                         }
144                         
145                         Assertion.AssertEquals ("BeginSend #5", true, BSSent);
146                         Assertion.AssertEquals ("BeginSend #6", 4, BSBytes);
147
148                         client.Close ();
149                 }
150                 
151                 static bool BRReceived = false;
152                 static byte[] BRBytes;
153                 static IPEndPoint BRFrom;
154                 static ManualResetEvent BRCalledBack = new ManualResetEvent (false);
155                 
156                 private static void BRCallback (IAsyncResult asyncResult)
157                 {
158                         UdpClient client = (UdpClient)asyncResult.AsyncState;
159                         
160                         BRBytes = client.EndReceive (asyncResult, ref BRFrom);
161                         
162                         BRReceived = true;
163                         BRCalledBack.Set ();
164                 }
165                 
166                 [Test]
167                 public void BeginReceive ()
168                 {
169                         UdpClient client = new UdpClient (1237);
170                         
171                         BRCalledBack.Reset ();
172                         
173                         client.BeginReceive (BRCallback, client);
174
175                         IPEndPoint ep = new IPEndPoint (Dns.GetHostEntry ("").AddressList[0], 1237);
176                         byte[] send_bytes = new byte[] {10, 11, 12, 13};
177                         client.Send (send_bytes, send_bytes.Length, ep);
178
179                         if (BRCalledBack.WaitOne (2000, false) == false) {
180                                 Assert.Fail ("BeginReceive wait timed out");
181                         }
182                         
183                         Assertion.AssertEquals ("BeginReceive #1", true,
184                                                 BRReceived);
185                         Assertion.AssertEquals ("BeginReceive #2", 4,
186                                                 BRBytes.Length);
187                         Assertion.AssertEquals ("BeginReceive #3", ep. Port,
188                                                 BRFrom.Port);
189                         Assertion.AssertEquals ("BeginReceive #4", ep.Address,
190                                                 BRFrom.Address);
191
192                         client.Close ();
193                 }
194                 
195                 [Test]
196                 public void Available ()
197                 {
198                         UdpClient client = new UdpClient (1238);
199                         IPEndPoint ep = new IPEndPoint (Dns.GetHostEntry ("").AddressList[0], 1238);
200                         byte[] bytes = new byte[] {10, 11, 12, 13};
201                         
202                         client.Send (bytes, bytes.Length, ep);
203                         int avail = client.Available;
204                         
205                         Assertion.AssertEquals ("Available #1", bytes.Length,
206                                                 avail);
207
208                         client.Close ();
209                 }
210                 
211                 [Test]
212                 [Category ("NotWorking")]  // Using PMTU settings workaround on Linux, default true
213                 public void DontFragmentDefault ()
214                 {
215                         UdpClient client = new UdpClient ();
216                         
217                         /* Ignore the docs, testing shows the default
218                          * here is in fact false
219                          */
220                         Assertion.AssertEquals ("DontFragmentDefault", false,
221                                                 client.DontFragment);
222
223                         client.Close ();
224                 }
225                 
226                 [Test]
227                 public void EnableBroadcastDefault ()
228                 {
229                         UdpClient client = new UdpClient ();
230                         
231                         Assertion.AssertEquals ("EnableBroadcastDefault",
232                                                 false, client.EnableBroadcast);
233
234                         client.Close ();
235                 }
236                 
237                 /* Can't test the default for ExclusiveAddressUse as
238                  * it's different on different versions and service
239                  * packs of windows
240                  */
241                 [Test]
242                 [Category ("NotWorking")] // Not supported on Linux
243                 public void ExclusiveAddressUseUnbound ()
244                 {
245                         UdpClient client = new UdpClient ();
246
247                         client.ExclusiveAddressUse = true;
248
249                         Assertion.AssertEquals ("ExclusiveAddressUseUnbound",
250                                                 true,
251                                                 client.ExclusiveAddressUse);
252
253                         client.Close ();
254                 }
255                 
256                 [Test]
257                 [ExpectedException (typeof(InvalidOperationException))]
258                 [Category ("NotWorking")] // Not supported on Linux
259                 public void ExclusiveAddressUseBound ()
260                 {
261                         UdpClient client = new UdpClient (1239);
262
263                         client.ExclusiveAddressUse = true;
264
265                         client.Close ();
266                 }
267                 
268                 [Test]
269                 public void MulticastLoopbackDefault ()
270                 {
271                         UdpClient client = new UdpClient ();
272                         
273                         Assertion.AssertEquals ("MulticastLoopbackDefault",
274                                                 true,
275                                                 client.MulticastLoopback);
276
277                         client.Close ();
278                 }
279                 
280                 /* No test for Ttl default as it is platform dependent */
281 #endif
282         }
283 }