UdpClientTest.cs 38 KB

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