UdpClientTest.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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]
  57. [ExpectedException (typeof(ArgumentNullException))]
  58. public void BeginSendNull ()
  59. {
  60. UdpClient client = new UdpClient ();
  61. client.BeginSend (null, 0, null, null);
  62. client.Close ();
  63. }
  64. static bool BSSent = false;
  65. static int BSBytes;
  66. static ManualResetEvent BSCalledBack = new ManualResetEvent (false);
  67. private static void BSCallback (IAsyncResult asyncResult)
  68. {
  69. UdpClient client = (UdpClient)asyncResult.AsyncState;
  70. BSBytes = client.EndSend (asyncResult);
  71. BSSent = true;
  72. BSCalledBack.Set ();
  73. }
  74. [Test]
  75. public void BeginSend ()
  76. {
  77. UdpClient client = new UdpClient ();
  78. byte[] bytes = new byte[] {10, 11, 12, 13};
  79. try {
  80. client.BeginSend (bytes, bytes.Length, new AsyncCallback (BSCallback), client);
  81. Assert.Fail ("BeginSend #1");
  82. } catch (SocketException ex) {
  83. Assert.AreEqual (10057, ex.ErrorCode,
  84. "BeginSend #2");
  85. }
  86. try {
  87. client.BeginSend (bytes, bytes.Length, null, new AsyncCallback (BSCallback), client);
  88. Assert.Fail ("BeginSend #3");
  89. } catch (SocketException ex) {
  90. Assert.AreEqual (10057, ex.ErrorCode,
  91. "BeginSend #4");
  92. }
  93. IPEndPoint ep = new IPEndPoint (Dns.GetHostEntry ("").AddressList[0], 1236);
  94. BSCalledBack.Reset ();
  95. client.BeginSend (bytes, bytes.Length, ep,
  96. new AsyncCallback (BSCallback),
  97. client);
  98. if (BSCalledBack.WaitOne (2000, false) == false) {
  99. Assert.Fail ("BeginSend wait timed out");
  100. }
  101. Assertion.AssertEquals ("BeginSend #5", true, BSSent);
  102. Assertion.AssertEquals ("BeginSend #6", 4, BSBytes);
  103. client.Close ();
  104. }
  105. static bool BRReceived = false;
  106. static byte[] BRBytes;
  107. static IPEndPoint BRFrom;
  108. static ManualResetEvent BRCalledBack = new ManualResetEvent (false);
  109. private static void BRCallback (IAsyncResult asyncResult)
  110. {
  111. UdpClient client = (UdpClient)asyncResult.AsyncState;
  112. BRBytes = client.EndReceive (asyncResult, ref BRFrom);
  113. BRReceived = true;
  114. BRCalledBack.Set ();
  115. }
  116. [Test]
  117. public void BeginReceive ()
  118. {
  119. UdpClient client = new UdpClient (1237);
  120. BRCalledBack.Reset ();
  121. client.BeginReceive (BRCallback, client);
  122. IPEndPoint ep = new IPEndPoint (Dns.GetHostEntry ("").AddressList[0], 1237);
  123. byte[] send_bytes = new byte[] {10, 11, 12, 13};
  124. client.Send (send_bytes, send_bytes.Length, ep);
  125. if (BRCalledBack.WaitOne (2000, false) == false) {
  126. Assert.Fail ("BeginReceive wait timed out");
  127. }
  128. Assertion.AssertEquals ("BeginReceive #1", true,
  129. BRReceived);
  130. Assertion.AssertEquals ("BeginReceive #2", 4,
  131. BRBytes.Length);
  132. Assertion.AssertEquals ("BeginReceive #3", ep. Port,
  133. BRFrom.Port);
  134. Assertion.AssertEquals ("BeginReceive #4", ep.Address,
  135. BRFrom.Address);
  136. client.Close ();
  137. }
  138. [Test]
  139. public void Available ()
  140. {
  141. UdpClient client = new UdpClient (1238);
  142. IPEndPoint ep = new IPEndPoint (Dns.GetHostEntry ("").AddressList[0], 1238);
  143. byte[] bytes = new byte[] {10, 11, 12, 13};
  144. client.Send (bytes, bytes.Length, ep);
  145. int avail = client.Available;
  146. Assertion.AssertEquals ("Available #1", bytes.Length,
  147. avail);
  148. client.Close ();
  149. }
  150. [Test]
  151. [Category ("NotWorking")] // Using PMTU settings workaround on Linux, default true
  152. public void DontFragmentDefault ()
  153. {
  154. UdpClient client = new UdpClient ();
  155. /* Ignore the docs, testing shows the default
  156. * here is in fact false
  157. */
  158. Assertion.AssertEquals ("DontFragmentDefault", false,
  159. client.DontFragment);
  160. client.Close ();
  161. }
  162. [Test]
  163. public void EnableBroadcastDefault ()
  164. {
  165. UdpClient client = new UdpClient ();
  166. Assertion.AssertEquals ("EnableBroadcastDefault",
  167. false, client.EnableBroadcast);
  168. client.Close ();
  169. }
  170. /* Can't test the default for ExclusiveAddressUse as
  171. * it's different on different versions and service
  172. * packs of windows
  173. */
  174. [Test]
  175. [Category ("NotWorking")] // Not supported on Linux
  176. public void ExclusiveAddressUseUnbound ()
  177. {
  178. UdpClient client = new UdpClient ();
  179. client.ExclusiveAddressUse = true;
  180. Assertion.AssertEquals ("ExclusiveAddressUseUnbound",
  181. true,
  182. client.ExclusiveAddressUse);
  183. client.Close ();
  184. }
  185. [Test]
  186. [ExpectedException (typeof(InvalidOperationException))]
  187. [Category ("NotWorking")] // Not supported on Linux
  188. public void ExclusiveAddressUseBound ()
  189. {
  190. UdpClient client = new UdpClient (1239);
  191. client.ExclusiveAddressUse = true;
  192. client.Close ();
  193. }
  194. [Test]
  195. public void MulticastLoopbackDefault ()
  196. {
  197. UdpClient client = new UdpClient ();
  198. Assertion.AssertEquals ("MulticastLoopbackDefault",
  199. true,
  200. client.MulticastLoopback);
  201. client.Close ();
  202. }
  203. /* No test for Ttl default as it is platform dependent */
  204. #endif
  205. }
  206. }