UdpClientTest.cs 38 KB

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