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