UdpClientTest.cs 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210
  1. // System.Net.Sockets.UdpClientTest.cs
  2. //
  3. // Authors:
  4. // Chris Bacon <[email protected]>
  5. // Gert Driesen <[email protected]>
  6. //
  7. using System;
  8. using System.Net;
  9. using System.Net.Sockets;
  10. using System.Threading;
  11. using System.Threading.Tasks;
  12. using NUnit.Framework;
  13. namespace MonoTests.System.Net.Sockets {
  14. [TestFixture]
  15. public class UdpClientTest {
  16. [Test] // .ctor ()
  17. #if FEATURE_NO_BSD_SOCKETS
  18. [ExpectedException (typeof (PlatformNotSupportedException))]
  19. #endif
  20. public void Constructor1 ()
  21. {
  22. MyUdpClient client;
  23. Socket s;
  24. client = new MyUdpClient ();
  25. s = client.Client;
  26. Assert.IsNotNull (s, "Client");
  27. Assert.AreEqual (AddressFamily.InterNetwork, s.AddressFamily, "Client:AddressFamily");
  28. Assert.IsFalse (s.Connected, "Client:Connected");
  29. Assert.IsFalse (s.IsBound, "#A:Client:IsBound");
  30. Assert.IsNull (s.LocalEndPoint, "Client:LocalEndPoint");
  31. Assert.AreEqual (ProtocolType.Udp, s.ProtocolType, "Client:ProtocolType");
  32. Assert.IsNull (s.RemoteEndPoint, "Client:RemoteEndPoint");
  33. Assert.AreEqual (SocketType.Dgram, s.SocketType, "Client:SocketType");
  34. Assert.IsFalse (client.Active, "Active");
  35. Assert.IsFalse (client.DontFragment, "DontFragment");
  36. Assert.IsFalse (client.EnableBroadcast, "EnableBroadcast");
  37. //Assert.IsFalse (client.ExclusiveAddressUse, "ExclusiveAddressUse");
  38. Assert.IsTrue (client.MulticastLoopback, "MulticastLoopback");
  39. //Assert.AreEqual (32, client.Ttl, "Ttl");
  40. client.Close ();
  41. }
  42. [Test] // .ctor (AddressFamily)
  43. #if FEATURE_NO_BSD_SOCKETS
  44. [ExpectedException (typeof (PlatformNotSupportedException))]
  45. #endif
  46. public void Constructor2 ()
  47. {
  48. MyUdpClient client;
  49. Socket s;
  50. client = new MyUdpClient (AddressFamily.InterNetwork);
  51. s = client.Client;
  52. Assert.IsNotNull (s, "#A:Client");
  53. Assert.AreEqual (AddressFamily.InterNetwork, s.AddressFamily, "#A:Client:AddressFamily");
  54. Assert.IsFalse (s.Connected, "#A:Client:Connected");
  55. Assert.IsFalse (s.IsBound, "#A:Client:IsBound");
  56. Assert.IsNull (s.LocalEndPoint, "#A:Client:LocalEndPoint");
  57. Assert.AreEqual (ProtocolType.Udp, s.ProtocolType, "#A:Client:ProtocolType");
  58. Assert.IsNull (s.RemoteEndPoint, "#A:Client:RemoteEndPoint");
  59. Assert.AreEqual (SocketType.Dgram, s.SocketType, "#A:Client:SocketType");
  60. Assert.IsFalse (client.Active, "#A:Active");
  61. //Assert.IsFalse (client.DontFragment, "#A:DontFragment");
  62. Assert.IsFalse (client.EnableBroadcast, "#A:EnableBroadcast");
  63. //Assert.IsFalse (client.ExclusiveAddressUse, "#A:ExclusiveAddressUse");
  64. Assert.IsTrue (client.MulticastLoopback, "#A:MulticastLoopback");
  65. //Assert.AreEqual (32, client.Ttl, "#A:Ttl");
  66. if (!Socket.OSSupportsIPv6)
  67. Assert.Ignore ("IPv6 not enabled.");
  68. client = new MyUdpClient (AddressFamily.InterNetworkV6);
  69. s = client.Client;
  70. Assert.IsNotNull (s, "#B:Client");
  71. Assert.AreEqual (AddressFamily.InterNetworkV6, s.AddressFamily, "#B:Client:AddressFamily");
  72. Assert.IsFalse (s.Connected, "#B:Client:Connected");
  73. Assert.IsFalse (s.IsBound, "#A:Client:IsBound");
  74. Assert.IsNull (s.LocalEndPoint, "#B:Client:LocalEndPoint");
  75. Assert.AreEqual (ProtocolType.Udp, s.ProtocolType, "#B:Client:ProtocolType");
  76. Assert.IsNull (s.RemoteEndPoint, "#B:Client:RemoteEndPoint");
  77. Assert.AreEqual (SocketType.Dgram, s.SocketType, "#B:Client:SocketType");
  78. Assert.IsFalse (client.Active, "#B:Active");
  79. //Assert.IsFalse (client.DontFragment, "#B:DontFragment");
  80. Assert.IsFalse (client.EnableBroadcast, "#B:EnableBroadcast");
  81. //Assert.IsFalse (client.ExclusiveAddressUse, "#B:ExclusiveAddressUse");
  82. Assert.IsTrue (client.MulticastLoopback, "#B:MulticastLoopback");
  83. //Assert.AreEqual (32, client.Ttl, "#B:Ttl");
  84. client.Close ();
  85. }
  86. [Test] // .ctor (AddressFamily)
  87. #if FEATURE_NO_BSD_SOCKETS
  88. [ExpectedException (typeof (PlatformNotSupportedException))]
  89. #endif
  90. public void Constructor2_Family_Invalid ()
  91. {
  92. try {
  93. new UdpClient (AddressFamily.NetBios);
  94. Assert.Fail ("#A1");
  95. } catch (ArgumentException ex) {
  96. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#A2");
  97. Assert.IsNull (ex.InnerException, "#A3");
  98. // 'UDP' Client can only accept InterNetwork or InterNetworkV6
  99. // addresses
  100. Assert.IsNotNull (ex.Message, "#A4");
  101. Assert.AreEqual ("family", ex.ParamName, "#A5");
  102. }
  103. try {
  104. new UdpClient ((AddressFamily) 666);
  105. Assert.Fail ("#B1");
  106. } catch (ArgumentException ex) {
  107. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#B2");
  108. Assert.IsNull (ex.InnerException, "#B3");
  109. Assert.IsNotNull (ex.Message, "#B4");
  110. Assert.AreEqual ("family", ex.ParamName, "#B5");
  111. }
  112. }
  113. [Test] // .ctor (Int32)
  114. #if FEATURE_NO_BSD_SOCKETS
  115. [ExpectedException (typeof (PlatformNotSupportedException))]
  116. #endif
  117. public void Constructor3 ()
  118. {
  119. Socket s;
  120. IPEndPoint localEP;
  121. using (MyUdpClient client = new MyUdpClient (IPEndPoint.MinPort))
  122. {
  123. s = client.Client;
  124. Assert.IsNotNull (s, "#A:Client");
  125. Assert.AreEqual (AddressFamily.InterNetwork, s.AddressFamily, "#A:Client:AddressFamily");
  126. Assert.IsFalse (s.Connected, "#A:Client:Connected");
  127. Assert.IsTrue (s.IsBound, "#A:Client:IsBound");
  128. Assert.AreEqual (ProtocolType.Udp, s.ProtocolType, "#A:Client:ProtocolType");
  129. Assert.AreEqual (SocketType.Dgram, s.SocketType, "#A:Client:SocketType");
  130. Assert.IsFalse (client.Active, "#A:Active");
  131. Assert.IsFalse (client.DontFragment, "#A:DontFragment");
  132. Assert.IsFalse (client.EnableBroadcast, "#A:EnableBroadcast");
  133. //Assert.IsFalse (client.ExclusiveAddressUse, "#A:ExclusiveAddressUse");
  134. Assert.IsTrue (client.MulticastLoopback, "#A:MulticastLoopback");
  135. //Assert.AreEqual (32, client.Ttl, "#A:Ttl");
  136. localEP = s.LocalEndPoint as IPEndPoint;
  137. Assert.IsNotNull (localEP, "#A:Client:LocalEndpoint");
  138. Assert.AreEqual (IPAddress.Any, localEP.Address, "#A:Client:LocalEndPoint/Address");
  139. Assert.AreEqual (AddressFamily.InterNetwork, localEP.AddressFamily, "#A:Client:LocalEndPoint/AddressFamily");
  140. }
  141. using (MyUdpClient client = new MyUdpClient (IPEndPoint.MaxPort))
  142. {
  143. s = client.Client;
  144. Assert.IsNotNull (s, "#B:Client");
  145. Assert.AreEqual (AddressFamily.InterNetwork, s.AddressFamily, "#B:Client:AddressFamily");
  146. Assert.IsFalse (s.Connected, "#B:Client:Connected");
  147. Assert.IsTrue (s.IsBound, "#B:Client:IsBound");
  148. Assert.AreEqual (ProtocolType.Udp, s.ProtocolType, "#B:Client:ProtocolType");
  149. Assert.AreEqual (SocketType.Dgram, s.SocketType, "#B:Client:SocketType");
  150. Assert.IsFalse (client.Active, "#B:Active");
  151. Assert.IsFalse (client.DontFragment, "#B:DontFragment");
  152. Assert.IsFalse (client.EnableBroadcast, "#B:EnableBroadcast");
  153. //Assert.IsFalse (client.ExclusiveAddressUse, "#B:ExclusiveAddressUse");
  154. Assert.IsTrue (client.MulticastLoopback, "#B:MulticastLoopback");
  155. //Assert.AreEqual (32, client.Ttl, "#B:Ttl");
  156. localEP = s.LocalEndPoint as IPEndPoint;
  157. Assert.IsNotNull (localEP, "#B:Client:LocalEndpoint");
  158. Assert.AreEqual (IPAddress.Any, localEP.Address, "#B:Client:LocalEndPoint/Address");
  159. Assert.AreEqual (AddressFamily.InterNetwork, localEP.AddressFamily, "#B:Client:LocalEndPoint/AddressFamily");
  160. Assert.AreEqual (IPEndPoint.MaxPort, localEP.Port, "#B:Client:LocalEndPoint/Port");
  161. }
  162. }
  163. [Test] // .ctor (Int32)
  164. #if FEATURE_NO_BSD_SOCKETS
  165. [ExpectedException (typeof (PlatformNotSupportedException))]
  166. #endif
  167. public void Constructor3_Port_OutOfRange ()
  168. {
  169. try {
  170. new UdpClient (IPEndPoint.MaxPort + 1);
  171. Assert.Fail ("#A1");
  172. } catch (ArgumentOutOfRangeException ex) {
  173. // Specified argument was out of the range of valid values
  174. Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#A2");
  175. Assert.IsNull (ex.InnerException, "#A3");
  176. Assert.IsNotNull (ex.Message, "#A4");
  177. Assert.AreEqual ("port", ex.ParamName, "#A5");
  178. }
  179. try {
  180. new UdpClient (IPEndPoint.MinPort - 1);
  181. Assert.Fail ("#A1");
  182. } catch (ArgumentOutOfRangeException ex) {
  183. // Specified argument was out of the range of valid values
  184. Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#A2");
  185. Assert.IsNull (ex.InnerException, "#A3");
  186. Assert.IsNotNull (ex.Message, "#A4");
  187. Assert.AreEqual ("port", ex.ParamName, "#A5");
  188. }
  189. }
  190. [Test] // .ctor (IPEndPoint)
  191. #if FEATURE_NO_BSD_SOCKETS
  192. [ExpectedException (typeof (PlatformNotSupportedException))]
  193. #endif
  194. public void Constructor4 ()
  195. {
  196. Socket s;
  197. IPEndPoint localEP;
  198. IPEndPoint clientEP;
  199. clientEP = new IPEndPoint (IPAddress.Loopback, 8001);
  200. using (MyUdpClient client = new MyUdpClient (clientEP))
  201. {
  202. s = client.Client;
  203. Assert.IsNotNull (s, "#A:Client");
  204. Assert.AreEqual (AddressFamily.InterNetwork, s.AddressFamily, "#A:Client:AddressFamily");
  205. Assert.IsFalse (s.Connected, "#A:Client:Connected");
  206. Assert.IsTrue (s.IsBound, "#A:Client:IsBound");
  207. Assert.AreEqual (ProtocolType.Udp, s.ProtocolType, "#A:Client:ProtocolType");
  208. Assert.AreEqual (SocketType.Dgram, s.SocketType, "#A:Client:SocketType");
  209. Assert.IsFalse (client.Active, "#A:Active");
  210. Assert.IsFalse (client.DontFragment, "#A:DontFragment");
  211. Assert.IsFalse (client.EnableBroadcast, "#A:EnableBroadcast");
  212. //Assert.IsFalse (client.ExclusiveAddressUse, "#A:ExclusiveAddressUse");
  213. Assert.IsTrue (client.MulticastLoopback, "#A:MulticastLoopback");
  214. //Assert.AreEqual (32, client.Ttl, "#A:Ttl");
  215. localEP = s.LocalEndPoint as IPEndPoint;
  216. Assert.IsNotNull (localEP, "#A:Client:LocalEndpoint");
  217. Assert.IsFalse (object.ReferenceEquals (clientEP, localEP), "#A:Client:LocalEndPoint/ReferenceEquality");
  218. Assert.AreEqual (clientEP.Address, localEP.Address, "#A:Client:LocalEndPoint/Address");
  219. Assert.AreEqual (clientEP.AddressFamily, localEP.AddressFamily, "#A:Client:LocalEndPoint/AddressFamily");
  220. Assert.AreEqual (clientEP.Port, localEP.Port, "#A:Client:LocalEndPoint/Port");
  221. }
  222. }
  223. [Test] // .ctor (IPEndPoint)
  224. #if FEATURE_NO_BSD_SOCKETS
  225. [ExpectedException (typeof (PlatformNotSupportedException))]
  226. #endif
  227. public void Constructor4_LocalEP_Null ()
  228. {
  229. try {
  230. new UdpClient ((IPEndPoint) null);
  231. Assert.Fail ("#1");
  232. } catch (ArgumentNullException ex) {
  233. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  234. Assert.IsNull (ex.InnerException, "#3");
  235. Assert.IsNotNull (ex.Message, "#4");
  236. Assert.AreEqual ("localEP", ex.ParamName, "#5");
  237. }
  238. }
  239. [Test] // .ctor (Int32, AddressFamily)
  240. #if FEATURE_NO_BSD_SOCKETS
  241. [ExpectedException (typeof (PlatformNotSupportedException))]
  242. #endif
  243. public void Constructor5 ()
  244. {
  245. Socket s;
  246. IPEndPoint localEP;
  247. using (MyUdpClient client = new MyUdpClient (IPEndPoint.MinPort, AddressFamily.InterNetwork))
  248. {
  249. s = client.Client;
  250. Assert.IsNotNull (s, "#A:Client");
  251. Assert.AreEqual (AddressFamily.InterNetwork, s.AddressFamily, "#A:Client:AddressFamily");
  252. Assert.IsFalse (s.Connected, "#A:Client:Connected");
  253. Assert.IsTrue (s.IsBound, "#A:Client:IsBound");
  254. Assert.AreEqual (ProtocolType.Udp, s.ProtocolType, "#A:Client:ProtocolType");
  255. Assert.AreEqual (SocketType.Dgram, s.SocketType, "#A:Client:SocketType");
  256. Assert.IsFalse (client.Active, "#A:Active");
  257. //Assert.IsFalse (client.DontFragment, "#A:DontFragment");
  258. Assert.IsFalse (client.EnableBroadcast, "#A:EnableBroadcast");
  259. //Assert.IsFalse (client.ExclusiveAddressUse, "#A:ExclusiveAddressUse");
  260. Assert.IsTrue (client.MulticastLoopback, "#A:MulticastLoopback");
  261. //Assert.AreEqual (32, client.Ttl, "#A:Ttl");
  262. localEP = s.LocalEndPoint as IPEndPoint;
  263. Assert.IsNotNull (localEP, "#A:Client:LocalEndpoint");
  264. Assert.AreEqual (IPAddress.Any, localEP.Address, "#A:Client:LocalEndPoint/Address");
  265. Assert.AreEqual (AddressFamily.InterNetwork, localEP.AddressFamily, "#A:Client:LocalEndPoint/AddressFamily");
  266. }
  267. if (!Socket.OSSupportsIPv6)
  268. Assert.Ignore ("IPv6 not enabled.");
  269. using (MyUdpClient client = new MyUdpClient (IPEndPoint.MaxPort, AddressFamily.InterNetworkV6))
  270. {
  271. s = client.Client;
  272. Assert.IsNotNull (s, "#B:Client");
  273. Assert.AreEqual (AddressFamily.InterNetworkV6, s.AddressFamily, "#B:Client:AddressFamily");
  274. Assert.IsFalse (s.Connected, "#B:Client:Connected");
  275. Assert.IsTrue (s.IsBound, "#B:Client:IsBound");
  276. Assert.AreEqual (ProtocolType.Udp, s.ProtocolType, "#B:Client:ProtocolType");
  277. Assert.AreEqual (SocketType.Dgram, s.SocketType, "#B:Client:SocketType");
  278. Assert.IsFalse (client.Active, "#B:Active");
  279. //Assert.IsFalse (client.DontFragment, "#B:DontFragment");
  280. Assert.IsFalse (client.EnableBroadcast, "#B:EnableBroadcast");
  281. //Assert.IsFalse (client.ExclusiveAddressUse, "#B:ExclusiveAddressUse");
  282. Assert.IsTrue (client.MulticastLoopback, "#B:MulticastLoopback");
  283. //Assert.AreEqual (32, client.Ttl, "#B:Ttl");
  284. localEP = s.LocalEndPoint as IPEndPoint;
  285. Assert.IsNotNull (localEP, "#B:Client:LocalEndpoint");
  286. Assert.AreEqual (IPAddress.IPv6Any, localEP.Address, "#B:Client:LocalEndPoint/Address");
  287. Assert.AreEqual (AddressFamily.InterNetworkV6, localEP.AddressFamily, "#B:Client:LocalEndPoint/AddressFamily");
  288. Assert.AreEqual (IPEndPoint.MaxPort, localEP.Port, "#B:Client:LocalEndPoint/Port");
  289. }
  290. }
  291. [Test] // .ctor (Int32, AddressFamily)
  292. #if FEATURE_NO_BSD_SOCKETS
  293. [ExpectedException (typeof (PlatformNotSupportedException))]
  294. #endif
  295. public void Constructor5_Family_Invalid ()
  296. {
  297. try {
  298. new UdpClient (80, AddressFamily.NetBios);
  299. Assert.Fail ("#A1");
  300. } catch (ArgumentException ex) {
  301. // family
  302. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#A2");
  303. Assert.IsNull (ex.InnerException, "#A3");
  304. // 'UDP' Client can only accept InterNetwork or InterNetworkV6
  305. // addresses
  306. Assert.IsNotNull (ex.Message, "#A4");
  307. Assert.AreEqual ("family", ex.ParamName, "#A5");
  308. }
  309. try {
  310. new UdpClient (80, (AddressFamily) 666);
  311. Assert.Fail ("#B1");
  312. } catch (ArgumentException ex) {
  313. // family
  314. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#B2");
  315. Assert.IsNull (ex.InnerException, "#B3");
  316. // 'UDP' Client can only accept InterNetwork or InterNetworkV6
  317. // addresses
  318. Assert.IsNotNull (ex.Message, "#B4");
  319. Assert.AreEqual ("family", ex.ParamName, "#B5");
  320. }
  321. }
  322. [Test] // .ctor (Int32, AddressFamily)
  323. #if FEATURE_NO_BSD_SOCKETS
  324. [ExpectedException (typeof (PlatformNotSupportedException))]
  325. #endif
  326. public void Constructor5_Port_OutOfRange ()
  327. {
  328. try {
  329. new UdpClient (IPEndPoint.MaxPort + 1, AddressFamily.InterNetwork);
  330. Assert.Fail ("#A1");
  331. } catch (ArgumentOutOfRangeException ex) {
  332. // Specified argument was out of the range of valid values
  333. Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#A2");
  334. Assert.IsNull (ex.InnerException, "#A3");
  335. Assert.IsNotNull (ex.Message, "#A4");
  336. Assert.AreEqual ("port", ex.ParamName, "#A5");
  337. }
  338. try {
  339. new UdpClient (IPEndPoint.MinPort - 1, AddressFamily.InterNetwork);
  340. Assert.Fail ("#A1");
  341. } catch (ArgumentOutOfRangeException ex) {
  342. // Specified argument was out of the range of valid values
  343. Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#A2");
  344. Assert.IsNull (ex.InnerException, "#A3");
  345. Assert.IsNotNull (ex.Message, "#A4");
  346. Assert.AreEqual ("port", ex.ParamName, "#A5");
  347. }
  348. }
  349. [Test] // .ctor (String, Int32)
  350. #if FEATURE_NO_BSD_SOCKETS
  351. [ExpectedException (typeof (PlatformNotSupportedException))]
  352. #endif
  353. public void Constructor6 ()
  354. {
  355. Socket s;
  356. IPEndPoint localEP;
  357. // Bug #5503
  358. // UDP port 0 doesn't seem to be valid.
  359. using (MyUdpClient client = new MyUdpClient ("127.0.0.1", 53))
  360. {
  361. s = client.Client;
  362. Assert.IsNotNull (s, "#A:Client");
  363. Assert.AreEqual (AddressFamily.InterNetwork, s.AddressFamily, "#A:Client:AddressFamily");
  364. Assert.IsTrue (s.Connected, "#A:Client:Connected");
  365. Assert.IsTrue (s.IsBound, "#A:Client:IsBound");
  366. Assert.AreEqual (ProtocolType.Udp, s.ProtocolType, "#A:Client:ProtocolType");
  367. Assert.AreEqual (SocketType.Dgram, s.SocketType, "#A:Client:SocketType");
  368. Assert.IsTrue (client.Active, "#A:Active");
  369. Assert.IsFalse (client.DontFragment, "#A:DontFragment");
  370. Assert.IsFalse (client.EnableBroadcast, "#A:EnableBroadcast");
  371. //Assert.IsFalse (client.ExclusiveAddressUse, "#A:ExclusiveAddressUse");
  372. //Assert.IsFalse (client.MulticastLoopback, "#A:MulticastLoopback");
  373. //Assert.AreEqual (32, client.Ttl, "#A:Ttl");
  374. localEP = s.LocalEndPoint as IPEndPoint;
  375. Assert.IsNotNull (localEP, "#A:Client:LocalEndpoint");
  376. Assert.AreEqual (IPAddress.Loopback, localEP.Address, "#A:Client:LocalEndPoint/Address");
  377. Assert.AreEqual (AddressFamily.InterNetwork, localEP.AddressFamily, "#A:Client:LocalEndPoint/AddressFamily");
  378. }
  379. using (MyUdpClient client = new MyUdpClient ("127.0.0.1", IPEndPoint.MaxPort))
  380. {
  381. s = client.Client;
  382. Assert.IsNotNull (s, "#B:Client");
  383. Assert.AreEqual (AddressFamily.InterNetwork, s.AddressFamily, "#B:Client:AddressFamily");
  384. Assert.IsTrue (s.Connected, "#B:Client:Connected");
  385. Assert.IsTrue (s.IsBound, "#B:Client:IsBound");
  386. Assert.AreEqual (ProtocolType.Udp, s.ProtocolType, "#B:Client:ProtocolType");
  387. Assert.AreEqual (SocketType.Dgram, s.SocketType, "#B:Client:SocketType");
  388. Assert.IsTrue (client.Active, "#B:Active");
  389. Assert.IsFalse (client.DontFragment, "#B:DontFragment");
  390. Assert.IsFalse (client.EnableBroadcast, "#B:EnableBroadcast");
  391. //Assert.IsFalse (client.ExclusiveAddressUse, "#B:ExclusiveAddressUse");
  392. //Assert.IsFalse (client.MulticastLoopback, "#B:MulticastLoopback");
  393. //Assert.AreEqual (32, client.Ttl, "#B:Ttl");
  394. localEP = s.LocalEndPoint as IPEndPoint;
  395. Assert.IsNotNull (localEP, "#B:Client:LocalEndpoint");
  396. Assert.AreEqual (IPAddress.Loopback, localEP.Address, "#B:Client:LocalEndPoint/Address");
  397. Assert.AreEqual (AddressFamily.InterNetwork, localEP.AddressFamily, "#B:Client:LocalEndPoint/AddressFamily");
  398. }
  399. }
  400. [Test] // .ctor (String, Int32)
  401. #if FEATURE_NO_BSD_SOCKETS
  402. [ExpectedException (typeof (PlatformNotSupportedException))]
  403. #endif
  404. public void Constructor6_HostName_Null ()
  405. {
  406. try {
  407. new UdpClient ((string) null, int.MaxValue);
  408. Assert.Fail ("#1");
  409. } catch (ArgumentNullException ex) {
  410. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  411. Assert.IsNull (ex.InnerException, "#3");
  412. Assert.IsNotNull (ex.Message, "#4");
  413. Assert.AreEqual ("hostname", ex.ParamName, "#5");
  414. }
  415. }
  416. [Test] // .ctor (String, Int32)
  417. #if FEATURE_NO_BSD_SOCKETS
  418. [ExpectedException (typeof (PlatformNotSupportedException))]
  419. #endif
  420. public void Constructor6_Port_OutOfRange ()
  421. {
  422. try {
  423. new UdpClient ("local", IPEndPoint.MaxPort + 1);
  424. Assert.Fail ("#A1");
  425. } catch (ArgumentOutOfRangeException ex) {
  426. // Specified argument was out of the range of valid values
  427. Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#A2");
  428. Assert.IsNull (ex.InnerException, "#A3");
  429. Assert.IsNotNull (ex.Message, "#A4");
  430. Assert.AreEqual ("port", ex.ParamName, "#A5");
  431. }
  432. try {
  433. new UdpClient ("local", IPEndPoint.MinPort - 1);
  434. Assert.Fail ("#A1");
  435. } catch (ArgumentOutOfRangeException ex) {
  436. // Specified argument was out of the range of valid values
  437. Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#A2");
  438. Assert.IsNull (ex.InnerException, "#A3");
  439. Assert.IsNotNull (ex.Message, "#A4");
  440. Assert.AreEqual ("port", ex.ParamName, "#A5");
  441. }
  442. }
  443. [Test]
  444. #if FEATURE_NO_BSD_SOCKETS
  445. [ExpectedException (typeof (PlatformNotSupportedException))]
  446. #endif
  447. public void UdpClientBroadcastTest ()
  448. {
  449. UdpClient client = new UdpClient ();
  450. byte[] bytes = new byte[] {10, 11, 12, 13};
  451. try {
  452. client.Send (bytes, bytes.Length, new IPEndPoint (IPAddress.Broadcast, 1235));
  453. } finally {
  454. client.Close ();
  455. }
  456. }
  457. [Test] // JoinMulticastGroup (IPAddress)
  458. #if FEATURE_NO_BSD_SOCKETS
  459. [ExpectedException (typeof (PlatformNotSupportedException))]
  460. #endif
  461. public void JoinMulticastGroup1_IPv4 ()
  462. {
  463. IPAddress mcast_addr = IPAddress.Parse ("224.0.0.23");
  464. using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Any, 1234))) {
  465. client.JoinMulticastGroup (mcast_addr);
  466. }
  467. }
  468. [Test] // JoinMulticastGroup (IPAddress)
  469. #if FEATURE_NO_BSD_SOCKETS
  470. [ExpectedException (typeof (PlatformNotSupportedException))]
  471. #endif
  472. public void JoinMulticastGroup1_IPv6 ()
  473. {
  474. if (!Socket.OSSupportsIPv6)
  475. Assert.Ignore ("IPv6 not enabled.");
  476. IPAddress mcast_addr = IPAddress.Parse ("ff02::1");
  477. using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.IPv6Any, 1234))) {
  478. client.JoinMulticastGroup (mcast_addr);
  479. }
  480. }
  481. [Test] // JoinMulticastGroup (IPAddress)
  482. #if FEATURE_NO_BSD_SOCKETS
  483. [ExpectedException (typeof (PlatformNotSupportedException))]
  484. #endif
  485. public void JoinMulticastGroup1_MulticastAddr_Null ()
  486. {
  487. using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Loopback, 1234))) {
  488. try {
  489. client.JoinMulticastGroup ((IPAddress) null);
  490. Assert.Fail ("#1");
  491. } catch (ArgumentNullException ex) {
  492. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  493. Assert.IsNull (ex.InnerException, "#3");
  494. Assert.IsNotNull (ex.Message, "#4");
  495. Assert.AreEqual ("multicastAddr", ex.ParamName, "#5");
  496. }
  497. }
  498. }
  499. [Test] // JoinMulticastGroup (IPAddress)
  500. #if FEATURE_NO_BSD_SOCKETS
  501. [ExpectedException (typeof (PlatformNotSupportedException))]
  502. #endif
  503. public void JoinMulticastGroup1_Socket_Closed ()
  504. {
  505. IPAddress mcast_addr = null;
  506. UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Loopback, 1234));
  507. client.Close ();
  508. try {
  509. client.JoinMulticastGroup (mcast_addr);
  510. Assert.Fail ("#1");
  511. } catch (ObjectDisposedException ex) {
  512. // Cannot access a disposed object
  513. Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
  514. Assert.IsNull (ex.InnerException, "#3");
  515. Assert.IsNotNull (ex.Message, "#4");
  516. Assert.AreEqual (typeof (UdpClient).FullName, ex.ObjectName, "#5");
  517. }
  518. }
  519. [Test] // JoinMulticastGroup (IPAddress)
  520. #if FEATURE_NO_BSD_SOCKETS
  521. [ExpectedException (typeof (PlatformNotSupportedException))]
  522. #endif
  523. public void JoinMulticastGroup1_Socket_NotBound ()
  524. {
  525. IPAddress mcast_addr = IPAddress.Parse ("224.0.0.23");
  526. using (UdpClient client = new UdpClient (AddressFamily.InterNetwork)) {
  527. client.JoinMulticastGroup (mcast_addr);
  528. }
  529. }
  530. [Test] // JoinMulticastGroup (In32, IPAddress)
  531. #if FEATURE_NO_BSD_SOCKETS
  532. [ExpectedException (typeof (PlatformNotSupportedException))]
  533. #endif
  534. public void JoinMulticastGroup2_IPv4 ()
  535. {
  536. IPAddress mcast_addr = IPAddress.Parse ("224.0.0.23");
  537. using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Any, 1234))) {
  538. try {
  539. client.JoinMulticastGroup (0, mcast_addr);
  540. Assert.Fail ("#1");
  541. } catch (SocketException ex) {
  542. // The attempted operation is not supported for the type of
  543. // object referenced
  544. Assert.AreEqual (typeof (SocketException), ex.GetType (), "#2");
  545. Assert.AreEqual (10045, ex.ErrorCode, "#3");
  546. Assert.IsNull (ex.InnerException, "#4");
  547. Assert.IsNotNull (ex.Message, "#5");
  548. Assert.AreEqual (10045, ex.NativeErrorCode, "#6");
  549. Assert.AreEqual (SocketError.OperationNotSupported, ex.SocketErrorCode, "#7");
  550. }
  551. }
  552. }
  553. [Test] // JoinMulticastGroup (In32, IPAddress)
  554. #if FEATURE_NO_BSD_SOCKETS
  555. [ExpectedException (typeof (PlatformNotSupportedException))]
  556. #endif
  557. public void JoinMulticastGroup2_IPv6 ()
  558. {
  559. if (!Socket.OSSupportsIPv6)
  560. Assert.Ignore ("IPv6 not enabled.");
  561. IPAddress mcast_addr = IPAddress.Parse ("ff02::1");
  562. using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.IPv6Any, 1234))) {
  563. client.JoinMulticastGroup (0, mcast_addr);
  564. }
  565. }
  566. [Test] // JoinMulticastGroup (Int32, IPAddress)
  567. #if FEATURE_NO_BSD_SOCKETS
  568. [ExpectedException (typeof (PlatformNotSupportedException))]
  569. #endif
  570. public void JoinMulticastGroup2_MulticastAddr_Null ()
  571. {
  572. using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Loopback, 1234))) {
  573. try {
  574. client.JoinMulticastGroup (0, (IPAddress) null);
  575. Assert.Fail ("#1");
  576. } catch (ArgumentNullException ex) {
  577. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  578. Assert.IsNull (ex.InnerException, "#3");
  579. Assert.IsNotNull (ex.Message, "#4");
  580. Assert.AreEqual ("multicastAddr", ex.ParamName, "#5");
  581. }
  582. }
  583. }
  584. [Test] // JoinMulticastGroup (Int32, IPAddress)
  585. #if FEATURE_NO_BSD_SOCKETS
  586. [ExpectedException (typeof (PlatformNotSupportedException))]
  587. #endif
  588. public void JoinMulticastGroup2_Socket_Closed ()
  589. {
  590. if (!Socket.OSSupportsIPv6)
  591. Assert.Ignore ("IPv6 not enabled.");
  592. IPAddress mcast_addr = null;
  593. UdpClient client = new UdpClient (new IPEndPoint (IPAddress.IPv6Any, 1234));
  594. client.Close ();
  595. try {
  596. client.JoinMulticastGroup (0, mcast_addr);
  597. Assert.Fail ("#1");
  598. } catch (ObjectDisposedException ex) {
  599. // Cannot access a disposed object
  600. Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
  601. Assert.IsNull (ex.InnerException, "#3");
  602. Assert.IsNotNull (ex.Message, "#4");
  603. Assert.AreEqual (typeof (UdpClient).FullName, ex.ObjectName, "#5");
  604. }
  605. }
  606. [Test] // JoinMulticastGroup (Int32, IPAddress)
  607. #if FEATURE_NO_BSD_SOCKETS
  608. [ExpectedException (typeof (PlatformNotSupportedException))]
  609. #endif
  610. public void JoinMulticastGroup2_Socket_NotBound ()
  611. {
  612. IPAddress mcast_addr = IPAddress.Parse ("ff02::1");
  613. using (UdpClient client = new UdpClient (AddressFamily.InterNetworkV6)) {
  614. client.JoinMulticastGroup (0, mcast_addr);
  615. }
  616. }
  617. [Test] // JoinMulticastGroup (IPAddress, Int32)
  618. #if FEATURE_NO_BSD_SOCKETS
  619. [ExpectedException (typeof (PlatformNotSupportedException))]
  620. #endif
  621. public void JoinMulticastGroup3_IPv4 ()
  622. {
  623. IPAddress mcast_addr = IPAddress.Parse ("224.0.0.23");
  624. using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Any, 1234))) {
  625. client.JoinMulticastGroup (mcast_addr, 0);
  626. }
  627. using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Any, 1234))) {
  628. client.JoinMulticastGroup (mcast_addr, 255);
  629. }
  630. }
  631. [Test] // JoinMulticastGroup (IPAddress, Int32)
  632. #if FEATURE_NO_BSD_SOCKETS
  633. [ExpectedException (typeof (PlatformNotSupportedException))]
  634. #endif
  635. public void JoinMulticastGroup3_IPv6 ()
  636. {
  637. if (!Socket.OSSupportsIPv6)
  638. Assert.Ignore ("IPv6 not enabled.");
  639. IPAddress mcast_addr = IPAddress.Parse ("ff02::1");
  640. using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.IPv6Any, 1234))) {
  641. client.JoinMulticastGroup (mcast_addr, 0);
  642. }
  643. using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.IPv6Any, 1234))) {
  644. client.JoinMulticastGroup (mcast_addr, 255);
  645. }
  646. }
  647. [Test] // JoinMulticastGroup (IPAddress, Int32)
  648. #if FEATURE_NO_BSD_SOCKETS
  649. [ExpectedException (typeof (PlatformNotSupportedException))]
  650. #endif
  651. public void JoinMulticastGroup3_MulticastAddr_Null ()
  652. {
  653. using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Loopback, 1234))) {
  654. try {
  655. client.JoinMulticastGroup ((IPAddress) null, int.MaxValue);
  656. Assert.Fail ("#1");
  657. } catch (ArgumentNullException ex) {
  658. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  659. Assert.IsNull (ex.InnerException, "#3");
  660. Assert.IsNotNull (ex.Message, "#4");
  661. Assert.AreEqual ("multicastAddr", ex.ParamName, "#5");
  662. }
  663. }
  664. }
  665. [Test] // JoinMulticastGroup (IPAddress, Int32)
  666. #if FEATURE_NO_BSD_SOCKETS
  667. [ExpectedException (typeof (PlatformNotSupportedException))]
  668. #endif
  669. public void JoinMulticastGroup3_Socket_Closed ()
  670. {
  671. IPAddress mcast_addr = null;
  672. UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Any, 1234));
  673. client.Close ();
  674. try {
  675. client.JoinMulticastGroup (mcast_addr, 0);
  676. Assert.Fail ("#1");
  677. } catch (ObjectDisposedException ex) {
  678. // Cannot access a disposed object
  679. Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
  680. Assert.IsNull (ex.InnerException, "#3");
  681. Assert.IsNotNull (ex.Message, "#4");
  682. Assert.AreEqual (typeof (UdpClient).FullName, ex.ObjectName, "#5");
  683. }
  684. }
  685. [Test] // JoinMulticastGroup (IPAddress, Int32)
  686. #if FEATURE_NO_BSD_SOCKETS
  687. [ExpectedException (typeof (PlatformNotSupportedException))]
  688. #endif
  689. public void JoinMulticastGroup3_Socket_NotBound ()
  690. {
  691. IPAddress mcast_addr = IPAddress.Parse ("224.0.0.23");
  692. using (UdpClient client = new UdpClient (AddressFamily.InterNetwork)) {
  693. client.JoinMulticastGroup (mcast_addr, 5);
  694. }
  695. }
  696. [Test] // JoinMulticastGroup (IPAddress, IPAddress)
  697. #if FEATURE_NO_BSD_SOCKETS
  698. [ExpectedException (typeof (PlatformNotSupportedException))]
  699. #endif
  700. public void JoinMulticastGroup4_IPv4 ()
  701. {
  702. IPAddress mcast_addr = IPAddress.Parse ("224.0.0.23");
  703. IPAddress local_addr = IPAddress.Any;
  704. using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Any, 1234))) {
  705. client.JoinMulticastGroup (mcast_addr, local_addr);
  706. }
  707. }
  708. [Test] // JoinMulticastGroup (IPAddress, IPAddress)
  709. #if FEATURE_NO_BSD_SOCKETS
  710. [ExpectedException (typeof (PlatformNotSupportedException))]
  711. #endif
  712. public void JoinMulticastGroup4_IPv6 ()
  713. {
  714. if (!Socket.OSSupportsIPv6)
  715. Assert.Ignore ("IPv6 not enabled.");
  716. IPAddress mcast_addr = IPAddress.Parse ("ff02::1");
  717. IPAddress local_addr = IPAddress.IPv6Any;
  718. using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.IPv6Any, 1234))) {
  719. try {
  720. client.JoinMulticastGroup (mcast_addr, local_addr);
  721. Assert.Fail ("#1");
  722. } catch (SocketException ex) {
  723. // The attempted operation is not supported for the type of
  724. // object referenced
  725. Assert.AreEqual (typeof (SocketException), ex.GetType (), "#2");
  726. Assert.AreEqual (10045, ex.ErrorCode, "#3");
  727. Assert.IsNull (ex.InnerException, "#4");
  728. Assert.IsNotNull (ex.Message, "#5");
  729. Assert.AreEqual (10045, ex.NativeErrorCode, "#6");
  730. Assert.AreEqual (SocketError.OperationNotSupported, ex.SocketErrorCode, "#7");
  731. }
  732. }
  733. }
  734. [Test] // JoinMulticastGroup (IPAddress, IPAddress)
  735. #if FEATURE_NO_BSD_SOCKETS
  736. [ExpectedException (typeof (PlatformNotSupportedException))]
  737. #endif
  738. public void JoinMulticastGroup4_LocalAddress_Null ()
  739. {
  740. IPAddress mcast_addr = IPAddress.Parse ("224.0.0.23");
  741. using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Loopback, 1234))) {
  742. try {
  743. client.JoinMulticastGroup (mcast_addr, (IPAddress) null);
  744. Assert.Fail ("#1");
  745. } catch (ArgumentNullException ex) {
  746. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  747. Assert.IsNull (ex.InnerException, "#3");
  748. Assert.IsNotNull (ex.Message, "#4");
  749. Assert.AreEqual ("mcint", ex.ParamName, "#5");
  750. }
  751. }
  752. }
  753. [Test] // JoinMulticastGroup (IPAddress, IPAddress)
  754. #if FEATURE_NO_BSD_SOCKETS
  755. [ExpectedException (typeof (PlatformNotSupportedException))]
  756. #endif
  757. public void JoinMulticastGroup4_MulticastAddr_Null ()
  758. {
  759. using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Loopback, 1234))) {
  760. try {
  761. client.JoinMulticastGroup ((IPAddress) null, IPAddress.Loopback);
  762. Assert.Fail ("#1");
  763. } catch (ArgumentNullException ex) {
  764. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  765. Assert.IsNull (ex.InnerException, "#3");
  766. Assert.IsNotNull (ex.Message, "#4");
  767. Assert.AreEqual ("group", ex.ParamName, "#5");
  768. }
  769. }
  770. }
  771. [Test] // JoinMulticastGroup (IPAddress, IPAddress)
  772. #if FEATURE_NO_BSD_SOCKETS
  773. [ExpectedException (typeof (PlatformNotSupportedException))]
  774. #endif
  775. public void JoinMulticastGroup4_Socket_Closed ()
  776. {
  777. IPAddress mcast_addr = null;
  778. IPAddress local_addr = null;
  779. UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Any, 1234));
  780. client.Close ();
  781. try {
  782. client.JoinMulticastGroup (mcast_addr, local_addr);
  783. Assert.Fail ("#1");
  784. } catch (ObjectDisposedException ex) {
  785. // Cannot access a disposed object
  786. Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
  787. Assert.IsNull (ex.InnerException, "#3");
  788. Assert.IsNotNull (ex.Message, "#4");
  789. Assert.AreEqual (typeof (UdpClient).FullName, ex.ObjectName, "#5");
  790. }
  791. }
  792. [Test] // JoinMulticastGroup (IPAddress, IPAddress)
  793. #if FEATURE_NO_BSD_SOCKETS
  794. [ExpectedException (typeof (PlatformNotSupportedException))]
  795. #endif
  796. public void JoinMulticastGroup4_Socket_NotBound ()
  797. {
  798. IPAddress mcast_addr = IPAddress.Parse ("224.0.0.23");
  799. IPAddress local_addr = Dns.GetHostEntry (string.Empty).AddressList [0];
  800. using (UdpClient client = new UdpClient (AddressFamily.InterNetwork)) {
  801. client.JoinMulticastGroup (mcast_addr, local_addr);
  802. }
  803. }
  804. [Test]
  805. #if FEATURE_NO_BSD_SOCKETS
  806. [ExpectedException (typeof (PlatformNotSupportedException))]
  807. #endif
  808. public void CloseInReceive ()
  809. {
  810. UdpClient client = null;
  811. var rnd = new Random ();
  812. for (int i = 0, max = 5; i < max; i++) {
  813. int port = rnd.Next (1025, 65534);
  814. try {
  815. client = new UdpClient (port);
  816. break;
  817. } catch (Exception) {
  818. if (i == max - 1)
  819. throw;
  820. }
  821. }
  822. ManualResetEvent ready = new ManualResetEvent (false);
  823. bool got_exc = false;
  824. Task receive_task = Task.Factory.StartNew (() => {
  825. IPEndPoint ep = new IPEndPoint (IPAddress.Any, 0);
  826. try {
  827. ready.Set ();
  828. client.Receive(ref ep);
  829. } catch (SocketException) {
  830. got_exc = true;
  831. } finally {
  832. client.Close ();
  833. }
  834. });
  835. ready.WaitOne (2000);
  836. Thread.Sleep (20);
  837. client.Close();
  838. Assert.IsTrue (receive_task.Wait (1000));
  839. Assert.IsTrue (got_exc);
  840. }
  841. // Test for bug 324033
  842. [Test]
  843. #if FEATURE_NO_BSD_SOCKETS
  844. [ExpectedException (typeof (PlatformNotSupportedException))]
  845. #endif
  846. public void JoinMulticastGroupWithLocal ()
  847. {
  848. UdpClient client = new UdpClient (9001);
  849. IPAddress mcast_addr = IPAddress.Parse ("224.0.0.24");
  850. IPAddress local_addr = IPAddress.Any;
  851. try {
  852. client.JoinMulticastGroup (mcast_addr, local_addr);
  853. } finally {
  854. client.Close ();
  855. }
  856. }
  857. [Test]
  858. #if FEATURE_NO_BSD_SOCKETS
  859. [ExpectedException (typeof (PlatformNotSupportedException))]
  860. #else
  861. [ExpectedException (typeof(ArgumentNullException))]
  862. #endif
  863. public void BeginSendNull ()
  864. {
  865. UdpClient client = new UdpClient ();
  866. client.BeginSend (null, 0, null, null);
  867. client.Close ();
  868. }
  869. static bool BSSent = false;
  870. static int BSBytes;
  871. static ManualResetEvent BSCalledBack = new ManualResetEvent (false);
  872. private static void BSCallback (IAsyncResult asyncResult)
  873. {
  874. UdpClient client = (UdpClient)asyncResult.AsyncState;
  875. BSBytes = client.EndSend (asyncResult);
  876. BSSent = true;
  877. BSCalledBack.Set ();
  878. }
  879. [Test]
  880. #if FEATURE_NO_BSD_SOCKETS
  881. [ExpectedException (typeof (PlatformNotSupportedException))]
  882. #endif
  883. public void BeginSend ()
  884. {
  885. UdpClient client = new UdpClient ();
  886. byte[] bytes = new byte[] {10, 11, 12, 13};
  887. try {
  888. client.BeginSend (bytes, bytes.Length, new AsyncCallback (BSCallback), client);
  889. Assert.Fail ("BeginSend #1");
  890. } catch (SocketException ex) {
  891. Assert.AreEqual (10057, ex.ErrorCode,
  892. "BeginSend #2");
  893. }
  894. try {
  895. client.BeginSend (bytes, bytes.Length, null, new AsyncCallback (BSCallback), client);
  896. Assert.Fail ("BeginSend #3");
  897. } catch (SocketException ex) {
  898. Assert.AreEqual (10057, ex.ErrorCode,
  899. "BeginSend #4");
  900. }
  901. IPEndPoint ep = new IPEndPoint (Dns.GetHostEntry (string.Empty).AddressList[0], 1236);
  902. BSCalledBack.Reset ();
  903. client.BeginSend (bytes, bytes.Length, ep,
  904. new AsyncCallback (BSCallback),
  905. client);
  906. if (BSCalledBack.WaitOne (2000, false) == false) {
  907. Assert.Fail ("BeginSend wait timed out");
  908. }
  909. Assert.AreEqual (true, BSSent, "BeginSend #5");
  910. Assert.AreEqual (4, BSBytes, "BeginSend #6");
  911. client.Close ();
  912. }
  913. static bool BRReceived = false;
  914. static byte[] BRBytes;
  915. static IPEndPoint BRFrom;
  916. static ManualResetEvent BRCalledBack = new ManualResetEvent (false);
  917. private static void BRCallback (IAsyncResult asyncResult)
  918. {
  919. UdpClient client = (UdpClient)asyncResult.AsyncState;
  920. BRBytes = client.EndReceive (asyncResult, ref BRFrom);
  921. BRReceived = true;
  922. BRCalledBack.Set ();
  923. }
  924. [Test]
  925. #if FEATURE_NO_BSD_SOCKETS
  926. [ExpectedException (typeof (PlatformNotSupportedException))]
  927. #endif
  928. public void BeginReceive ()
  929. {
  930. UdpClient client = new UdpClient (1237);
  931. BRCalledBack.Reset ();
  932. client.BeginReceive (BRCallback, client);
  933. IPEndPoint ep = new IPEndPoint (IPAddress.Loopback, 1237);
  934. byte[] send_bytes = new byte[] {10, 11, 12, 13};
  935. client.Send (send_bytes, send_bytes.Length, ep);
  936. if (BRCalledBack.WaitOne (2000, false) == false) {
  937. Assert.Fail ("BeginReceive wait timed out");
  938. }
  939. Assert.AreEqual (true, BRReceived, "BeginReceive #1");
  940. Assert.AreEqual (4, BRBytes.Length, "BeginReceive #2");
  941. Assert.AreEqual (ep. Port, BRFrom.Port, "BeginReceive #3");
  942. Assert.AreEqual (ep.Address, BRFrom.Address, "BeginReceive #4");
  943. client.Close ();
  944. }
  945. [Test]
  946. #if FEATURE_NO_BSD_SOCKETS
  947. [ExpectedException (typeof (PlatformNotSupportedException))]
  948. #endif
  949. public void Available ()
  950. {
  951. using (UdpClient client = new UdpClient (1238)) {
  952. IPEndPoint ep = new IPEndPoint (IPAddress.Loopback, 1238);
  953. byte[] bytes = new byte[] {10, 11, 12, 13};
  954. int res = client.Send (bytes, bytes.Length, ep);
  955. Assert.AreEqual (bytes.Length, res, "Send");
  956. // that might happen too quickly, data sent and not yet received/available
  957. Thread.Sleep (100);
  958. int avail = client.Available;
  959. Assert.AreEqual (bytes.Length, avail, "Available #1");
  960. client.Close ();
  961. }
  962. }
  963. [Test]
  964. [Category ("NotWorking")] // Using PMTU settings workaround on Linux, default true
  965. public void DontFragmentDefault ()
  966. {
  967. UdpClient client = new UdpClient ();
  968. /* Ignore the docs, testing shows the default
  969. * here is in fact false
  970. */
  971. Assert.AreEqual (false, client.DontFragment, "DontFragmentDefault");
  972. client.Close ();
  973. }
  974. [Test]
  975. #if FEATURE_NO_BSD_SOCKETS
  976. [ExpectedException (typeof (PlatformNotSupportedException))]
  977. #endif
  978. public void EnableBroadcastDefault ()
  979. {
  980. UdpClient client = new UdpClient ();
  981. Assert.AreEqual (false, client.EnableBroadcast, "EnableBroadcastDefault");
  982. client.Close ();
  983. }
  984. /* Can't test the default for ExclusiveAddressUse as
  985. * it's different on different versions and service
  986. * packs of windows
  987. */
  988. [Test]
  989. [Category ("NotWorking")] // Not supported on Linux
  990. public void ExclusiveAddressUseUnbound ()
  991. {
  992. UdpClient client = new UdpClient ();
  993. client.ExclusiveAddressUse = true;
  994. Assert.AreEqual (true, client.ExclusiveAddressUse, "ExclusiveAddressUseUnbound");
  995. client.Close ();
  996. }
  997. [Test]
  998. [ExpectedException (typeof(InvalidOperationException))]
  999. [Category ("NotWorking")] // Not supported on Linux
  1000. public void ExclusiveAddressUseBound ()
  1001. {
  1002. UdpClient client = new UdpClient (1239);
  1003. client.ExclusiveAddressUse = true;
  1004. client.Close ();
  1005. }
  1006. [Test]
  1007. #if FEATURE_NO_BSD_SOCKETS
  1008. [ExpectedException (typeof (PlatformNotSupportedException))]
  1009. #endif
  1010. public void MulticastLoopbackDefault ()
  1011. {
  1012. UdpClient client = new UdpClient ();
  1013. Assert.AreEqual (true, client.MulticastLoopback, "MulticastLoopbackDefault");
  1014. client.Close ();
  1015. }
  1016. [Test] // #6057
  1017. #if FEATURE_NO_BSD_SOCKETS
  1018. [ExpectedException (typeof (PlatformNotSupportedException))]
  1019. #endif
  1020. public void ReceiveIPv6 ()
  1021. {
  1022. if (!Socket.OSSupportsIPv6)
  1023. Assert.Ignore ("IPv6 not enabled.");
  1024. int PORT = 9997;
  1025. using(var udpClient = new UdpClient (PORT, AddressFamily.InterNetworkV6))
  1026. using(var udpClient2 = new UdpClient (PORT+1, AddressFamily.InterNetworkV6))
  1027. {
  1028. var dataSent = new byte [] {1,2,3};
  1029. udpClient2.SendAsync (dataSent, dataSent.Length, "::1", PORT);
  1030. IPEndPoint endPoint = new IPEndPoint (IPAddress.IPv6Any, 0);
  1031. var data = udpClient.Receive (ref endPoint);
  1032. Assert.AreEqual (dataSent.Length, data.Length);
  1033. }
  1034. }
  1035. /* No test for Ttl default as it is platform dependent */
  1036. class MyUdpClient : UdpClient
  1037. {
  1038. public MyUdpClient ()
  1039. {
  1040. }
  1041. public MyUdpClient (AddressFamily family)
  1042. : base (family)
  1043. {
  1044. }
  1045. public MyUdpClient (Int32 port)
  1046. : base (port)
  1047. {
  1048. }
  1049. public MyUdpClient (IPEndPoint localEP)
  1050. : base (localEP)
  1051. {
  1052. }
  1053. public MyUdpClient (int port, AddressFamily family)
  1054. : base (port, family)
  1055. {
  1056. }
  1057. public MyUdpClient (string hostname, int port)
  1058. : base (hostname, port)
  1059. {
  1060. }
  1061. public new bool Active {
  1062. get { return base.Active; }
  1063. set { base.Active = value; }
  1064. }
  1065. }
  1066. }
  1067. }