UdpClientTest.cs 37 KB

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