UdpClientTest.cs 40 KB

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