UdpClientTest.cs 6.8 KB

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