UdpClientTest.cs 38 KB

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