UdpClientTest.cs 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163
  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. public void Constructor5 ()
  248. {
  249. MyUdpClient client;
  250. Socket s;
  251. IPEndPoint localEP;
  252. client = new MyUdpClient (IPEndPoint.MinPort, AddressFamily.InterNetwork);
  253. s = client.Client;
  254. Assert.IsNotNull (s, "#A:Client");
  255. Assert.AreEqual (AddressFamily.InterNetwork, s.AddressFamily, "#A:Client:AddressFamily");
  256. Assert.IsFalse (s.Connected, "#A:Client:Connected");
  257. #if NET_2_0
  258. Assert.IsTrue (s.IsBound, "#A:Client:IsBound");
  259. #endif
  260. Assert.AreEqual (ProtocolType.Udp, s.ProtocolType, "#A:Client:ProtocolType");
  261. Assert.AreEqual (SocketType.Dgram, s.SocketType, "#A:Client:SocketType");
  262. Assert.IsFalse (client.Active, "#A:Active");
  263. #if NET_2_0
  264. //Assert.IsFalse (client.DontFragment, "#A:DontFragment");
  265. Assert.IsFalse (client.EnableBroadcast, "#A:EnableBroadcast");
  266. //Assert.IsFalse (client.ExclusiveAddressUse, "#A:ExclusiveAddressUse");
  267. Assert.IsTrue (client.MulticastLoopback, "#A:MulticastLoopback");
  268. //Assert.AreEqual (32, client.Ttl, "#A:Ttl");
  269. #endif
  270. localEP = s.LocalEndPoint as IPEndPoint;
  271. Assert.IsNotNull (localEP, "#A:Client:LocalEndpoint");
  272. Assert.AreEqual (IPAddress.Any, localEP.Address, "#A:Client:LocalEndPoint/Address");
  273. Assert.AreEqual (AddressFamily.InterNetwork, localEP.AddressFamily, "#A:Client:LocalEndPoint/AddressFamily");
  274. client = new MyUdpClient (IPEndPoint.MaxPort, AddressFamily.InterNetworkV6);
  275. s = client.Client;
  276. Assert.IsNotNull (s, "#B:Client");
  277. Assert.AreEqual (AddressFamily.InterNetworkV6, s.AddressFamily, "#B:Client:AddressFamily");
  278. Assert.IsFalse (s.Connected, "#B:Client:Connected");
  279. #if NET_2_0
  280. Assert.IsTrue (s.IsBound, "#B:Client:IsBound");
  281. #endif
  282. Assert.AreEqual (ProtocolType.Udp, s.ProtocolType, "#B:Client:ProtocolType");
  283. Assert.AreEqual (SocketType.Dgram, s.SocketType, "#B:Client:SocketType");
  284. Assert.IsFalse (client.Active, "#B:Active");
  285. #if NET_2_0
  286. //Assert.IsFalse (client.DontFragment, "#B:DontFragment");
  287. Assert.IsFalse (client.EnableBroadcast, "#B:EnableBroadcast");
  288. //Assert.IsFalse (client.ExclusiveAddressUse, "#B:ExclusiveAddressUse");
  289. Assert.IsTrue (client.MulticastLoopback, "#B:MulticastLoopback");
  290. //Assert.AreEqual (32, client.Ttl, "#B:Ttl");
  291. #endif
  292. localEP = s.LocalEndPoint as IPEndPoint;
  293. Assert.IsNotNull (localEP, "#B:Client:LocalEndpoint");
  294. Assert.AreEqual (IPAddress.IPv6Any, localEP.Address, "#B:Client:LocalEndPoint/Address");
  295. Assert.AreEqual (AddressFamily.InterNetworkV6, localEP.AddressFamily, "#B:Client:LocalEndPoint/AddressFamily");
  296. Assert.AreEqual (IPEndPoint.MaxPort, localEP.Port, "#B:Client:LocalEndPoint/Port");
  297. }
  298. [Test] // .ctor (Int32, AddressFamily)
  299. public void Constructor5_Family_Invalid ()
  300. {
  301. try {
  302. new UdpClient (80, AddressFamily.NetBios);
  303. Assert.Fail ("#A1");
  304. } catch (ArgumentException ex) {
  305. // family
  306. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#A2");
  307. Assert.IsNull (ex.InnerException, "#A3");
  308. #if NET_2_0
  309. // 'UDP' Client can only accept InterNetwork or InterNetworkV6
  310. // addresses
  311. Assert.IsNotNull (ex.Message, "#A4");
  312. Assert.AreEqual ("family", ex.ParamName, "#A5");
  313. #else
  314. Assert.AreEqual ("family", ex.Message, "#A4");
  315. Assert.IsNull (ex.ParamName, "#A5");
  316. #endif
  317. }
  318. try {
  319. new UdpClient (80, (AddressFamily) 666);
  320. Assert.Fail ("#B1");
  321. } catch (ArgumentException ex) {
  322. // family
  323. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#B2");
  324. Assert.IsNull (ex.InnerException, "#B3");
  325. #if NET_2_0
  326. // 'UDP' Client can only accept InterNetwork or InterNetworkV6
  327. // addresses
  328. Assert.IsNotNull (ex.Message, "#B4");
  329. Assert.AreEqual ("family", ex.ParamName, "#B5");
  330. #else
  331. Assert.AreEqual ("family", ex.Message, "#B4");
  332. Assert.IsNull (ex.ParamName, "#B5");
  333. #endif
  334. }
  335. }
  336. [Test] // .ctor (Int32, AddressFamily)
  337. public void Constructor5_Port_OutOfRange ()
  338. {
  339. try {
  340. new UdpClient (IPEndPoint.MaxPort + 1, AddressFamily.InterNetwork);
  341. Assert.Fail ("#A1");
  342. } catch (ArgumentOutOfRangeException ex) {
  343. // Specified argument was out of the range of valid values
  344. Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#A2");
  345. Assert.IsNull (ex.InnerException, "#A3");
  346. Assert.IsNotNull (ex.Message, "#A4");
  347. Assert.AreEqual ("port", ex.ParamName, "#A5");
  348. }
  349. try {
  350. new UdpClient (IPEndPoint.MinPort - 1, AddressFamily.InterNetwork);
  351. Assert.Fail ("#A1");
  352. } catch (ArgumentOutOfRangeException ex) {
  353. // Specified argument was out of the range of valid values
  354. Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#A2");
  355. Assert.IsNull (ex.InnerException, "#A3");
  356. Assert.IsNotNull (ex.Message, "#A4");
  357. Assert.AreEqual ("port", ex.ParamName, "#A5");
  358. }
  359. }
  360. [Test] // .ctor (String, Int32)
  361. public void Constructor6 ()
  362. {
  363. MyUdpClient client;
  364. Socket s;
  365. IPEndPoint localEP;
  366. client = new MyUdpClient ("localhost", IPEndPoint.MinPort);
  367. s = client.Client;
  368. Assert.IsNotNull (s, "#A:Client");
  369. Assert.AreEqual (AddressFamily.InterNetwork, s.AddressFamily, "#A:Client:AddressFamily");
  370. Assert.IsTrue (s.Connected, "#A:Client:Connected");
  371. #if NET_2_0
  372. Assert.IsTrue (s.IsBound, "#A:Client:IsBound");
  373. #endif
  374. Assert.AreEqual (ProtocolType.Udp, s.ProtocolType, "#A:Client:ProtocolType");
  375. Assert.AreEqual (SocketType.Dgram, s.SocketType, "#A:Client:SocketType");
  376. Assert.IsTrue (client.Active, "#A:Active");
  377. #if NET_2_0
  378. Assert.IsFalse (client.DontFragment, "#A:DontFragment");
  379. Assert.IsFalse (client.EnableBroadcast, "#A:EnableBroadcast");
  380. //Assert.IsFalse (client.ExclusiveAddressUse, "#A:ExclusiveAddressUse");
  381. //Assert.IsFalse (client.MulticastLoopback, "#A:MulticastLoopback");
  382. //Assert.AreEqual (32, client.Ttl, "#A:Ttl");
  383. #endif
  384. localEP = s.LocalEndPoint as IPEndPoint;
  385. Assert.IsNotNull (localEP, "#A:Client:LocalEndpoint");
  386. Assert.AreEqual (IPAddress.Loopback, localEP.Address, "#A:Client:LocalEndPoint/Address");
  387. Assert.AreEqual (AddressFamily.InterNetwork, localEP.AddressFamily, "#A:Client:LocalEndPoint/AddressFamily");
  388. client = new MyUdpClient ("localhost", IPEndPoint.MaxPort);
  389. s = client.Client;
  390. Assert.IsNotNull (s, "#B:Client");
  391. Assert.AreEqual (AddressFamily.InterNetwork, s.AddressFamily, "#B:Client:AddressFamily");
  392. Assert.IsTrue (s.Connected, "#B:Client:Connected");
  393. #if NET_2_0
  394. Assert.IsTrue (s.IsBound, "#B:Client:IsBound");
  395. #endif
  396. Assert.AreEqual (ProtocolType.Udp, s.ProtocolType, "#B:Client:ProtocolType");
  397. Assert.AreEqual (SocketType.Dgram, s.SocketType, "#B:Client:SocketType");
  398. Assert.IsTrue (client.Active, "#B:Active");
  399. #if NET_2_0
  400. Assert.IsFalse (client.DontFragment, "#B:DontFragment");
  401. Assert.IsFalse (client.EnableBroadcast, "#B:EnableBroadcast");
  402. //Assert.IsFalse (client.ExclusiveAddressUse, "#B:ExclusiveAddressUse");
  403. //Assert.IsFalse (client.MulticastLoopback, "#B:MulticastLoopback");
  404. //Assert.AreEqual (32, client.Ttl, "#B:Ttl");
  405. #endif
  406. localEP = s.LocalEndPoint as IPEndPoint;
  407. Assert.IsNotNull (localEP, "#B:Client:LocalEndpoint");
  408. Assert.AreEqual (IPAddress.Loopback, localEP.Address, "#B:Client:LocalEndPoint/Address");
  409. Assert.AreEqual (AddressFamily.InterNetwork, localEP.AddressFamily, "#B:Client:LocalEndPoint/AddressFamily");
  410. }
  411. [Test] // .ctor (String, Int32)
  412. public void Constructor6_HostName_Null ()
  413. {
  414. try {
  415. new UdpClient ((string) null, int.MaxValue);
  416. Assert.Fail ("#1");
  417. } catch (ArgumentNullException ex) {
  418. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  419. Assert.IsNull (ex.InnerException, "#3");
  420. Assert.IsNotNull (ex.Message, "#4");
  421. Assert.AreEqual ("hostname", ex.ParamName, "#5");
  422. }
  423. }
  424. [Test] // .ctor (String, Int32)
  425. public void Constructor6_Port_OutOfRange ()
  426. {
  427. try {
  428. new UdpClient ("local", IPEndPoint.MaxPort + 1);
  429. Assert.Fail ("#A1");
  430. } catch (ArgumentOutOfRangeException ex) {
  431. // Specified argument was out of the range of valid values
  432. Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#A2");
  433. Assert.IsNull (ex.InnerException, "#A3");
  434. Assert.IsNotNull (ex.Message, "#A4");
  435. Assert.AreEqual ("port", ex.ParamName, "#A5");
  436. }
  437. try {
  438. new UdpClient ("local", IPEndPoint.MinPort - 1);
  439. Assert.Fail ("#A1");
  440. } catch (ArgumentOutOfRangeException ex) {
  441. // Specified argument was out of the range of valid values
  442. Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#A2");
  443. Assert.IsNull (ex.InnerException, "#A3");
  444. Assert.IsNotNull (ex.Message, "#A4");
  445. Assert.AreEqual ("port", ex.ParamName, "#A5");
  446. }
  447. }
  448. [Test]
  449. public void UdpClientBroadcastTest ()
  450. {
  451. UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Loopback, 1234));
  452. byte[] bytes = new byte[] {10, 11, 12, 13};
  453. try {
  454. client.Send (bytes, bytes.Length, new IPEndPoint (IPAddress.Broadcast, 1235));
  455. } finally {
  456. client.Close ();
  457. }
  458. }
  459. [Test] // JoinMulticastGroup (IPAddress)
  460. public void JoinMulticastGroup1_IPv4 ()
  461. {
  462. IPAddress mcast_addr = IPAddress.Parse ("224.0.0.23");
  463. using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Any, 1234))) {
  464. client.JoinMulticastGroup (mcast_addr);
  465. }
  466. }
  467. [Test] // JoinMulticastGroup (IPAddress)
  468. public void JoinMulticastGroup1_IPv6 ()
  469. {
  470. #if NET_2_0
  471. if (!Socket.OSSupportsIPv6)
  472. #else
  473. if (!Socket.SupportsIPv6)
  474. #endif
  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. public void JoinMulticastGroup1_MulticastAddr_Null ()
  483. {
  484. using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Loopback, 1234))) {
  485. try {
  486. client.JoinMulticastGroup ((IPAddress) null);
  487. Assert.Fail ("#1");
  488. #if NET_2_0
  489. } catch (ArgumentNullException ex) {
  490. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  491. Assert.IsNull (ex.InnerException, "#3");
  492. Assert.IsNotNull (ex.Message, "#4");
  493. Assert.AreEqual ("multicastAddr", ex.ParamName, "#5");
  494. }
  495. #else
  496. } catch (NullReferenceException ex) {
  497. Assert.AreEqual (typeof (NullReferenceException), ex.GetType (), "#2");
  498. Assert.IsNull (ex.InnerException, "#3");
  499. Assert.IsNotNull (ex.Message, "#4");
  500. }
  501. #endif
  502. }
  503. }
  504. [Test] // JoinMulticastGroup (IPAddress)
  505. public void JoinMulticastGroup1_Socket_Closed ()
  506. {
  507. IPAddress mcast_addr = null;
  508. UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Loopback, 1234));
  509. client.Close ();
  510. try {
  511. client.JoinMulticastGroup (mcast_addr);
  512. Assert.Fail ("#1");
  513. } catch (ObjectDisposedException ex) {
  514. // Cannot access a disposed object
  515. Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
  516. Assert.IsNull (ex.InnerException, "#3");
  517. Assert.IsNotNull (ex.Message, "#4");
  518. Assert.AreEqual (typeof (UdpClient).FullName, ex.ObjectName, "#5");
  519. }
  520. }
  521. [Test] // JoinMulticastGroup (IPAddress)
  522. [Category ("NotWorking")]
  523. public void JoinMulticastGroup1_Socket_NotBound ()
  524. {
  525. IPAddress mcast_addr = IPAddress.Parse ("224.0.0.23");
  526. UdpClient client = new UdpClient (AddressFamily.InterNetwork);
  527. try {
  528. client.JoinMulticastGroup (mcast_addr);
  529. Assert.Fail ("#1");
  530. } catch (SocketException ex) {
  531. // An invalid argument was supplied
  532. Assert.AreEqual (typeof (SocketException), ex.GetType (), "#2");
  533. Assert.AreEqual (10022, ex.ErrorCode, "#3");
  534. Assert.IsNull (ex.InnerException, "#4");
  535. Assert.IsNotNull (ex.Message, "#5");
  536. Assert.AreEqual (10022, ex.NativeErrorCode, "#6");
  537. #if NET_2_0
  538. Assert.AreEqual (SocketError.InvalidArgument, ex.SocketErrorCode, "#7");
  539. #endif
  540. } finally {
  541. client.Close ();
  542. }
  543. }
  544. [Test] // JoinMulticastGroup (In32, IPAddress)
  545. public void JoinMulticastGroup2_IPv4 ()
  546. {
  547. IPAddress mcast_addr = IPAddress.Parse ("224.0.0.23");
  548. using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Any, 1234))) {
  549. try {
  550. client.JoinMulticastGroup (0, mcast_addr);
  551. Assert.Fail ("#1");
  552. } catch (SocketException ex) {
  553. // The attempted operation is not supported for the type of
  554. // object referenced
  555. Assert.AreEqual (typeof (SocketException), ex.GetType (), "#2");
  556. Assert.AreEqual (10045, ex.ErrorCode, "#3");
  557. Assert.IsNull (ex.InnerException, "#4");
  558. Assert.IsNotNull (ex.Message, "#5");
  559. Assert.AreEqual (10045, ex.NativeErrorCode, "#6");
  560. #if NET_2_0
  561. Assert.AreEqual (SocketError.OperationNotSupported, ex.SocketErrorCode, "#7");
  562. #endif
  563. }
  564. }
  565. }
  566. [Test] // JoinMulticastGroup (In32, IPAddress)
  567. public void JoinMulticastGroup2_IPv6 ()
  568. {
  569. #if NET_2_0
  570. if (!Socket.OSSupportsIPv6)
  571. #else
  572. if (!Socket.SupportsIPv6)
  573. #endif
  574. Assert.Ignore ("IPv6 not enabled.");
  575. IPAddress mcast_addr = IPAddress.Parse ("ff02::1");
  576. using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.IPv6Any, 1234))) {
  577. client.JoinMulticastGroup (0, mcast_addr);
  578. }
  579. }
  580. [Test] // JoinMulticastGroup (Int32, IPAddress)
  581. public void JoinMulticastGroup2_MulticastAddr_Null ()
  582. {
  583. using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Loopback, 1234))) {
  584. try {
  585. client.JoinMulticastGroup (0, (IPAddress) null);
  586. Assert.Fail ("#1");
  587. } catch (ArgumentNullException ex) {
  588. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  589. Assert.IsNull (ex.InnerException, "#3");
  590. Assert.IsNotNull (ex.Message, "#4");
  591. Assert.AreEqual ("multicastAddr", ex.ParamName, "#5");
  592. }
  593. }
  594. }
  595. [Test] // JoinMulticastGroup (Int32, IPAddress)
  596. public void JoinMulticastGroup2_Socket_Closed ()
  597. {
  598. IPAddress mcast_addr = null;
  599. UdpClient client = new UdpClient (new IPEndPoint (IPAddress.IPv6Any, 1234));
  600. client.Close ();
  601. try {
  602. client.JoinMulticastGroup (0, mcast_addr);
  603. Assert.Fail ("#1");
  604. } catch (ObjectDisposedException ex) {
  605. // Cannot access a disposed object
  606. Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
  607. Assert.IsNull (ex.InnerException, "#3");
  608. Assert.IsNotNull (ex.Message, "#4");
  609. Assert.AreEqual (typeof (UdpClient).FullName, ex.ObjectName, "#5");
  610. }
  611. }
  612. [Test] // JoinMulticastGroup (Int32, IPAddress)
  613. [Category ("NotWorking")]
  614. public void JoinMulticastGroup2_Socket_NotBound ()
  615. {
  616. IPAddress mcast_addr = IPAddress.Parse ("ff02::1");
  617. UdpClient client = new UdpClient (AddressFamily.InterNetworkV6);
  618. try {
  619. client.JoinMulticastGroup (0, mcast_addr);
  620. Assert.Fail ("#1");
  621. } catch (SocketException ex) {
  622. // An invalid argument was supplied
  623. Assert.AreEqual (typeof (SocketException), ex.GetType (), "#2");
  624. Assert.AreEqual (10022, ex.ErrorCode, "#3");
  625. Assert.IsNull (ex.InnerException, "#4");
  626. Assert.IsNotNull (ex.Message, "#5");
  627. Assert.AreEqual (10022, ex.NativeErrorCode, "#6");
  628. #if NET_2_0
  629. Assert.AreEqual (SocketError.InvalidArgument, ex.SocketErrorCode, "#7");
  630. #endif
  631. } finally {
  632. client.Close ();
  633. }
  634. }
  635. [Test] // JoinMulticastGroup (IPAddress, Int32)
  636. public void JoinMulticastGroup3_IPv4 ()
  637. {
  638. IPAddress mcast_addr = IPAddress.Parse ("224.0.0.23");
  639. using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Any, 1234))) {
  640. client.JoinMulticastGroup (mcast_addr, 0);
  641. }
  642. using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Any, 1234))) {
  643. client.JoinMulticastGroup (mcast_addr, 255);
  644. }
  645. }
  646. [Test] // JoinMulticastGroup (IPAddress, Int32)
  647. public void JoinMulticastGroup3_IPv6 ()
  648. {
  649. #if NET_2_0
  650. if (!Socket.OSSupportsIPv6)
  651. #else
  652. if (!Socket.SupportsIPv6)
  653. #endif
  654. Assert.Ignore ("IPv6 not enabled.");
  655. IPAddress mcast_addr = IPAddress.Parse ("ff02::1");
  656. using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.IPv6Any, 1234))) {
  657. client.JoinMulticastGroup (mcast_addr, 0);
  658. }
  659. using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.IPv6Any, 1234))) {
  660. client.JoinMulticastGroup (mcast_addr, 255);
  661. }
  662. }
  663. [Test] // JoinMulticastGroup (IPAddress, Int32)
  664. public void JoinMulticastGroup3_MulticastAddr_Null ()
  665. {
  666. using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Loopback, 1234))) {
  667. try {
  668. client.JoinMulticastGroup ((IPAddress) null, int.MaxValue);
  669. Assert.Fail ("#1");
  670. } catch (ArgumentNullException ex) {
  671. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  672. Assert.IsNull (ex.InnerException, "#3");
  673. Assert.IsNotNull (ex.Message, "#4");
  674. Assert.AreEqual ("multicastAddr", ex.ParamName, "#5");
  675. }
  676. }
  677. }
  678. [Test] // JoinMulticastGroup (IPAddress, Int32)
  679. public void JoinMulticastGroup3_Socket_Closed ()
  680. {
  681. IPAddress mcast_addr = null;
  682. UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Any, 1234));
  683. client.Close ();
  684. try {
  685. client.JoinMulticastGroup (mcast_addr, 0);
  686. Assert.Fail ("#1");
  687. } catch (ObjectDisposedException ex) {
  688. // Cannot access a disposed object
  689. Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
  690. Assert.IsNull (ex.InnerException, "#3");
  691. Assert.IsNotNull (ex.Message, "#4");
  692. Assert.AreEqual (typeof (UdpClient).FullName, ex.ObjectName, "#5");
  693. }
  694. }
  695. [Test] // JoinMulticastGroup (IPAddress, Int32)
  696. [Category ("NotWorking")]
  697. public void JoinMulticastGroup3_Socket_NotBound ()
  698. {
  699. IPAddress mcast_addr = IPAddress.Parse ("224.0.0.23");
  700. UdpClient client = new UdpClient (AddressFamily.InterNetwork);
  701. try {
  702. client.JoinMulticastGroup (mcast_addr, 5);
  703. Assert.Fail ("#1");
  704. } catch (SocketException ex) {
  705. // An invalid argument was supplied
  706. Assert.AreEqual (typeof (SocketException), ex.GetType (), "#2");
  707. Assert.AreEqual (10022, ex.ErrorCode, "#3");
  708. Assert.IsNull (ex.InnerException, "#4");
  709. Assert.IsNotNull (ex.Message, "#5");
  710. Assert.AreEqual (10022, ex.NativeErrorCode, "#6");
  711. #if NET_2_0
  712. Assert.AreEqual (SocketError.InvalidArgument, ex.SocketErrorCode, "#7");
  713. #endif
  714. } finally {
  715. client.Close ();
  716. }
  717. }
  718. #if NET_2_0
  719. [Test] // JoinMulticastGroup (IPAddress, IPAddress)
  720. public void JoinMulticastGroup4_IPv4 ()
  721. {
  722. IPAddress mcast_addr = IPAddress.Parse ("224.0.0.23");
  723. IPAddress local_addr = IPAddress.Any;
  724. using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Any, 1234))) {
  725. client.JoinMulticastGroup (mcast_addr, local_addr);
  726. }
  727. }
  728. [Test] // JoinMulticastGroup (IPAddress, IPAddress)
  729. public void JoinMulticastGroup4_IPv6 ()
  730. {
  731. if (!Socket.OSSupportsIPv6)
  732. Assert.Ignore ("IPv6 not enabled.");
  733. IPAddress mcast_addr = IPAddress.Parse ("ff02::1");
  734. IPAddress local_addr = IPAddress.IPv6Any;
  735. using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.IPv6Any, 1234))) {
  736. try {
  737. client.JoinMulticastGroup (mcast_addr, local_addr);
  738. Assert.Fail ("#1");
  739. } catch (SocketException ex) {
  740. // The attempted operation is not supported for the type of
  741. // object referenced
  742. Assert.AreEqual (typeof (SocketException), ex.GetType (), "#2");
  743. Assert.AreEqual (10045, ex.ErrorCode, "#3");
  744. Assert.IsNull (ex.InnerException, "#4");
  745. Assert.IsNotNull (ex.Message, "#5");
  746. Assert.AreEqual (10045, ex.NativeErrorCode, "#6");
  747. Assert.AreEqual (SocketError.OperationNotSupported, ex.SocketErrorCode, "#7");
  748. }
  749. }
  750. }
  751. [Test] // JoinMulticastGroup (IPAddress, IPAddress)
  752. public void JoinMulticastGroup4_LocalAddress_Null ()
  753. {
  754. IPAddress mcast_addr = IPAddress.Parse ("224.0.0.23");
  755. using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Loopback, 1234))) {
  756. try {
  757. client.JoinMulticastGroup (mcast_addr, (IPAddress) null);
  758. Assert.Fail ("#1");
  759. } catch (ArgumentNullException ex) {
  760. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  761. Assert.IsNull (ex.InnerException, "#3");
  762. Assert.IsNotNull (ex.Message, "#4");
  763. Assert.AreEqual ("mcint", ex.ParamName, "#5");
  764. }
  765. }
  766. }
  767. [Test] // JoinMulticastGroup (IPAddress, IPAddress)
  768. public void JoinMulticastGroup4_MulticastAddr_Null ()
  769. {
  770. using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Loopback, 1234))) {
  771. try {
  772. client.JoinMulticastGroup ((IPAddress) null, IPAddress.Loopback);
  773. Assert.Fail ("#1");
  774. } catch (ArgumentNullException ex) {
  775. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  776. Assert.IsNull (ex.InnerException, "#3");
  777. Assert.IsNotNull (ex.Message, "#4");
  778. Assert.AreEqual ("group", ex.ParamName, "#5");
  779. }
  780. }
  781. }
  782. [Test] // JoinMulticastGroup (IPAddress, IPAddress)
  783. public void JoinMulticastGroup4_Socket_Closed ()
  784. {
  785. IPAddress mcast_addr = null;
  786. IPAddress local_addr = null;
  787. UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Any, 1234));
  788. client.Close ();
  789. try {
  790. client.JoinMulticastGroup (mcast_addr, local_addr);
  791. Assert.Fail ("#1");
  792. } catch (ObjectDisposedException ex) {
  793. // Cannot access a disposed object
  794. Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
  795. Assert.IsNull (ex.InnerException, "#3");
  796. Assert.IsNotNull (ex.Message, "#4");
  797. Assert.AreEqual (typeof (UdpClient).FullName, ex.ObjectName, "#5");
  798. }
  799. }
  800. [Test] // JoinMulticastGroup (IPAddress, IPAddress)
  801. [Category ("NotWorking")]
  802. public void JoinMulticastGroup4_Socket_NotBound ()
  803. {
  804. IPAddress mcast_addr = IPAddress.Parse ("224.0.0.23");
  805. IPAddress local_addr = Dns.GetHostEntry (string.Empty).AddressList [0];
  806. UdpClient client = new UdpClient (AddressFamily.InterNetwork);
  807. try {
  808. client.JoinMulticastGroup (mcast_addr, local_addr);
  809. Assert.Fail ("#1");
  810. } catch (SocketException ex) {
  811. // An invalid argument was supplied
  812. Assert.AreEqual (typeof (SocketException), ex.GetType (), "#2");
  813. Assert.AreEqual (10022, ex.ErrorCode, "#3");
  814. Assert.IsNull (ex.InnerException, "#4");
  815. Assert.IsNotNull (ex.Message, "#5");
  816. Assert.AreEqual (10022, ex.NativeErrorCode, "#6");
  817. Assert.AreEqual (SocketError.InvalidArgument, ex.SocketErrorCode, "#7");
  818. } finally {
  819. client.Close ();
  820. }
  821. }
  822. [Test]
  823. public void CloseInReceive ()
  824. {
  825. UdpClient client = new UdpClient (50000);
  826. new Thread(delegate() {
  827. Thread.Sleep(2000);
  828. client.Close();
  829. }).Start();
  830. bool got_exc = false;
  831. IPEndPoint ep = new IPEndPoint (IPAddress.Any, 0);
  832. try {
  833. client.Receive(ref ep);
  834. } catch (SocketException) {
  835. got_exc = true;
  836. } finally {
  837. client.Close ();
  838. }
  839. Assert.IsTrue (got_exc);
  840. }
  841. // Test for bug 324033
  842. [Test]
  843. public void JoinMulticastGroupWithLocal ()
  844. {
  845. UdpClient client = new UdpClient (9001);
  846. IPAddress mcast_addr = IPAddress.Parse ("224.0.0.24");
  847. IPAddress local_addr = IPAddress.Any;
  848. try {
  849. client.JoinMulticastGroup (mcast_addr, local_addr);
  850. } finally {
  851. client.Close ();
  852. }
  853. }
  854. [Test]
  855. [ExpectedException (typeof(ArgumentNullException))]
  856. public void BeginSendNull ()
  857. {
  858. UdpClient client = new UdpClient ();
  859. client.BeginSend (null, 0, null, null);
  860. client.Close ();
  861. }
  862. static bool BSSent = false;
  863. static int BSBytes;
  864. static ManualResetEvent BSCalledBack = new ManualResetEvent (false);
  865. private static void BSCallback (IAsyncResult asyncResult)
  866. {
  867. UdpClient client = (UdpClient)asyncResult.AsyncState;
  868. BSBytes = client.EndSend (asyncResult);
  869. BSSent = true;
  870. BSCalledBack.Set ();
  871. }
  872. [Test]
  873. public void BeginSend ()
  874. {
  875. UdpClient client = new UdpClient ();
  876. byte[] bytes = new byte[] {10, 11, 12, 13};
  877. try {
  878. client.BeginSend (bytes, bytes.Length, new AsyncCallback (BSCallback), client);
  879. Assert.Fail ("BeginSend #1");
  880. } catch (SocketException ex) {
  881. Assert.AreEqual (10057, ex.ErrorCode,
  882. "BeginSend #2");
  883. }
  884. try {
  885. client.BeginSend (bytes, bytes.Length, null, new AsyncCallback (BSCallback), client);
  886. Assert.Fail ("BeginSend #3");
  887. } catch (SocketException ex) {
  888. Assert.AreEqual (10057, ex.ErrorCode,
  889. "BeginSend #4");
  890. }
  891. IPEndPoint ep = new IPEndPoint (Dns.GetHostEntry (string.Empty).AddressList[0], 1236);
  892. BSCalledBack.Reset ();
  893. client.BeginSend (bytes, bytes.Length, ep,
  894. new AsyncCallback (BSCallback),
  895. client);
  896. if (BSCalledBack.WaitOne (2000, false) == false) {
  897. Assert.Fail ("BeginSend wait timed out");
  898. }
  899. Assert.AreEqual (true, BSSent, "BeginSend #5");
  900. Assert.AreEqual (4, BSBytes, "BeginSend #6");
  901. client.Close ();
  902. }
  903. static bool BRReceived = false;
  904. static byte[] BRBytes;
  905. static IPEndPoint BRFrom;
  906. static ManualResetEvent BRCalledBack = new ManualResetEvent (false);
  907. private static void BRCallback (IAsyncResult asyncResult)
  908. {
  909. UdpClient client = (UdpClient)asyncResult.AsyncState;
  910. BRBytes = client.EndReceive (asyncResult, ref BRFrom);
  911. BRReceived = true;
  912. BRCalledBack.Set ();
  913. }
  914. [Test]
  915. public void BeginReceive ()
  916. {
  917. UdpClient client = new UdpClient (1237);
  918. BRCalledBack.Reset ();
  919. client.BeginReceive (BRCallback, client);
  920. IPEndPoint ep = new IPEndPoint (Dns.GetHostEntry (string.Empty).AddressList[0], 1237);
  921. byte[] send_bytes = new byte[] {10, 11, 12, 13};
  922. client.Send (send_bytes, send_bytes.Length, ep);
  923. if (BRCalledBack.WaitOne (2000, false) == false) {
  924. Assert.Fail ("BeginReceive wait timed out");
  925. }
  926. Assert.AreEqual (true, BRReceived, "BeginReceive #1");
  927. Assert.AreEqual (4, BRBytes.Length, "BeginReceive #2");
  928. Assert.AreEqual (ep. Port, BRFrom.Port, "BeginReceive #3");
  929. Assert.AreEqual (ep.Address, BRFrom.Address, "BeginReceive #4");
  930. client.Close ();
  931. }
  932. [Test]
  933. public void Available ()
  934. {
  935. UdpClient client = new UdpClient (1238);
  936. IPEndPoint ep = new IPEndPoint (Dns.GetHostEntry (string.Empty).AddressList[0], 1238);
  937. byte[] bytes = new byte[] {10, 11, 12, 13};
  938. client.Send (bytes, bytes.Length, ep);
  939. int avail = client.Available;
  940. Assert.AreEqual (bytes.Length, avail, "Available #1");
  941. client.Close ();
  942. }
  943. [Test]
  944. [Category ("NotWorking")] // Using PMTU settings workaround on Linux, default true
  945. public void DontFragmentDefault ()
  946. {
  947. UdpClient client = new UdpClient ();
  948. /* Ignore the docs, testing shows the default
  949. * here is in fact false
  950. */
  951. Assert.AreEqual (false, client.DontFragment, "DontFragmentDefault");
  952. client.Close ();
  953. }
  954. [Test]
  955. public void EnableBroadcastDefault ()
  956. {
  957. UdpClient client = new UdpClient ();
  958. Assert.AreEqual (false, client.EnableBroadcast, "EnableBroadcastDefault");
  959. client.Close ();
  960. }
  961. /* Can't test the default for ExclusiveAddressUse as
  962. * it's different on different versions and service
  963. * packs of windows
  964. */
  965. [Test]
  966. [Category ("NotWorking")] // Not supported on Linux
  967. public void ExclusiveAddressUseUnbound ()
  968. {
  969. UdpClient client = new UdpClient ();
  970. client.ExclusiveAddressUse = true;
  971. Assert.AreEqual (true, client.ExclusiveAddressUse, "ExclusiveAddressUseUnbound");
  972. client.Close ();
  973. }
  974. [Test]
  975. [ExpectedException (typeof(InvalidOperationException))]
  976. [Category ("NotWorking")] // Not supported on Linux
  977. public void ExclusiveAddressUseBound ()
  978. {
  979. UdpClient client = new UdpClient (1239);
  980. client.ExclusiveAddressUse = true;
  981. client.Close ();
  982. }
  983. [Test]
  984. public void MulticastLoopbackDefault ()
  985. {
  986. UdpClient client = new UdpClient ();
  987. Assert.AreEqual (true, client.MulticastLoopback, "MulticastLoopbackDefault");
  988. client.Close ();
  989. }
  990. /* No test for Ttl default as it is platform dependent */
  991. #endif
  992. class MyUdpClient : UdpClient
  993. {
  994. public MyUdpClient ()
  995. {
  996. }
  997. public MyUdpClient (AddressFamily family)
  998. : base (family)
  999. {
  1000. }
  1001. public MyUdpClient (Int32 port)
  1002. : base (port)
  1003. {
  1004. }
  1005. public MyUdpClient (IPEndPoint localEP)
  1006. : base (localEP)
  1007. {
  1008. }
  1009. public MyUdpClient (int port, AddressFamily family)
  1010. : base (port, family)
  1011. {
  1012. }
  1013. public MyUdpClient (string hostname, int port)
  1014. : base (hostname, port)
  1015. {
  1016. }
  1017. public new bool Active {
  1018. get { return base.Active; }
  1019. set { base.Active = value; }
  1020. }
  1021. #if ONLY_1_1
  1022. public new Socket Client {
  1023. get { return base.Client; }
  1024. set { base.Client = value; }
  1025. }
  1026. #endif
  1027. }
  1028. }
  1029. }