UdpClientTest.cs 42 KB

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