UdpClientTest.cs 38 KB

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