PageRenderTime 70ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 1ms

/mcs/class/System/Test/System.Net.Sockets/SocketTest.cs

https://bitbucket.org/danipen/mono
C# | 4309 lines | 3449 code | 679 blank | 181 comment | 58 complexity | 355a8255fc0c4f390e0aea77ac584ca7 MD5 | raw file
Possible License(s): Unlicense, Apache-2.0, LGPL-2.0, MPL-2.0-no-copyleft-exception, CC-BY-SA-3.0, GPL-2.0
  1. // System.Net.Sockets.SocketTest.cs
  2. //
  3. // Authors:
  4. // Brad Fitzpatrick (brad@danga.com)
  5. // Gonzalo Paniagua Javier (gonzalo@novell.com)
  6. //
  7. // (C) Copyright 2003 Brad Fitzpatrick
  8. // Copyright (c) 2005 Novell, Inc. (http://www.novell.com)
  9. //
  10. using System;
  11. using System.Collections;
  12. using System.Threading;
  13. using System.Net;
  14. using System.Net.Sockets;
  15. using NUnit.Framework;
  16. using System.IO;
  17. #if NET_2_0
  18. using System.Collections.Generic;
  19. #endif
  20. namespace MonoTests.System.Net.Sockets
  21. {
  22. [TestFixture]
  23. public class SocketTest
  24. {
  25. // note: also used in SocketCas tests
  26. public const string BogusAddress = "192.168.244.244";
  27. public const int BogusPort = 23483;
  28. [Test]
  29. public void ConnectIPAddressAny ()
  30. {
  31. IPEndPoint ep = new IPEndPoint (IPAddress.Any, 0);
  32. /* UDP sockets use Any to disconnect
  33. try {
  34. using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp)) {
  35. s.Connect (ep);
  36. s.Close ();
  37. }
  38. Assert.Fail ("#1");
  39. } catch (SocketException ex) {
  40. Assert.AreEqual (10049, ex.ErrorCode, "#2");
  41. }
  42. */
  43. try {
  44. using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) {
  45. s.Connect (ep);
  46. s.Close ();
  47. }
  48. Assert.Fail ("#3");
  49. } catch (SocketException ex) {
  50. Assert.AreEqual (10049, ex.ErrorCode, "#4");
  51. }
  52. }
  53. [Test]
  54. [Ignore ("Bug #75158")] // Looks like MS fails after the .ctor, when you try to use the socket
  55. public void IncompatibleAddress ()
  56. {
  57. IPEndPoint epIPv6 = new IPEndPoint (IPAddress.IPv6Any,
  58. 0);
  59. try {
  60. using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP)) {
  61. s.Connect (epIPv6);
  62. s.Close ();
  63. }
  64. Assert.Fail ("#1");
  65. } catch (SocketException ex) {
  66. #if !NET_2_0
  67. // invalid argument
  68. int expectedError = 10022;
  69. #else
  70. // address incompatible with protocol
  71. int expectedError = 10047;
  72. #endif
  73. Assert.AreEqual (expectedError, ex.ErrorCode,
  74. "#2");
  75. }
  76. }
  77. [Test]
  78. [Category ("InetAccess")]
  79. public void BogusEndConnect ()
  80. {
  81. IPAddress ipOne = IPAddress.Parse (BogusAddress);
  82. IPEndPoint ipEP = new IPEndPoint (ipOne, BogusPort);
  83. Socket sock = new Socket (ipEP.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
  84. IAsyncResult ar = sock.BeginConnect (ipEP, null, null);
  85. try {
  86. // should raise an exception because connect was bogus
  87. sock.EndConnect (ar);
  88. Assert.Fail ("#1");
  89. } catch (SocketException ex) {
  90. // Actual error code depends on network configuration.
  91. var error = (SocketError) ex.ErrorCode;
  92. Assert.That (error == SocketError.TimedOut ||
  93. error == SocketError.ConnectionRefused ||
  94. error == SocketError.NetworkUnreachable ||
  95. error == SocketError.HostUnreachable, "#2");
  96. }
  97. }
  98. [Test]
  99. [ExpectedException (typeof (ArgumentNullException))]
  100. public void SelectEmpty ()
  101. {
  102. ArrayList list = new ArrayList ();
  103. Socket.Select (list, list, list, 1000);
  104. }
  105. private bool BlockingConnect (bool block)
  106. {
  107. IPEndPoint ep = new IPEndPoint(IPAddress.Loopback, 1234);
  108. Socket server = new Socket(AddressFamily.InterNetwork,
  109. SocketType.Stream,
  110. ProtocolType.Tcp);
  111. server.Bind(ep);
  112. server.Blocking=block;
  113. server.Listen(0);
  114. Socket conn = new Socket (AddressFamily.InterNetwork,
  115. SocketType.Stream,
  116. ProtocolType.Tcp);
  117. conn.Connect (ep);
  118. Socket client = server.Accept();
  119. bool client_block = client.Blocking;
  120. client.Close();
  121. conn.Close();
  122. server.Close();
  123. return(client_block);
  124. }
  125. [Test]
  126. public void AcceptBlockingStatus()
  127. {
  128. bool block;
  129. block = BlockingConnect(true);
  130. Assert.AreEqual (block, true, "BlockingStatus01");
  131. block = BlockingConnect(false);
  132. Assert.AreEqual (block, false, "BlockingStatus02");
  133. }
  134. static bool CFAConnected = false;
  135. static ManualResetEvent CFACalledBack;
  136. private static void CFACallback (IAsyncResult asyncResult)
  137. {
  138. Socket sock = (Socket)asyncResult.AsyncState;
  139. CFAConnected = sock.Connected;
  140. if (sock.Connected) {
  141. sock.EndConnect (asyncResult);
  142. }
  143. CFACalledBack.Set ();
  144. }
  145. [Test] // Connect (IPEndPoint)
  146. public void Connect1_RemoteEP_Null ()
  147. {
  148. Socket s = new Socket (AddressFamily.InterNetwork,
  149. SocketType.Stream, ProtocolType.Tcp);
  150. try {
  151. s.Connect ((IPEndPoint) null);
  152. Assert.Fail ("#1");
  153. } catch (ArgumentNullException ex) {
  154. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  155. Assert.IsNull (ex.InnerException, "#3");
  156. Assert.IsNotNull (ex.Message, "#4");
  157. Assert.AreEqual ("remoteEP", ex.ParamName, "#5");
  158. }
  159. }
  160. [Test]
  161. public void ConnectFailAsync ()
  162. {
  163. Socket sock = new Socket (AddressFamily.InterNetwork,
  164. SocketType.Stream,
  165. ProtocolType.Tcp);
  166. sock.Blocking = false;
  167. CFACalledBack = new ManualResetEvent (false);
  168. CFACalledBack.Reset ();
  169. /* Need a port that is not being used for
  170. * anything...
  171. */
  172. sock.BeginConnect (new IPEndPoint (IPAddress.Loopback,
  173. 114),
  174. new AsyncCallback (CFACallback),
  175. sock);
  176. CFACalledBack.WaitOne ();
  177. Assert.AreEqual (CFAConnected, false, "ConnectFail");
  178. }
  179. #if !TARGET_JVM
  180. [Test]
  181. #if !NET_2_0
  182. [ExpectedException (typeof (ArgumentException))]
  183. #endif
  184. public void SetSocketOptionBoolean ()
  185. {
  186. IPEndPoint ep = new IPEndPoint (IPAddress.Loopback, 1);
  187. Socket sock = new Socket (ep.Address.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
  188. try {
  189. sock.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.KeepAlive, true);
  190. } finally {
  191. sock.Close ();
  192. }
  193. }
  194. #endif
  195. [Test]
  196. public void TestSelect1 ()
  197. {
  198. Socket srv = CreateServer ();
  199. ClientSocket clnt = new ClientSocket (srv.LocalEndPoint);
  200. Thread th = new Thread (new ThreadStart (clnt.ConnectSleepClose));
  201. Socket acc = null;
  202. try {
  203. th.Start ();
  204. acc = srv.Accept ();
  205. clnt.Write ();
  206. ArrayList list = new ArrayList ();
  207. ArrayList empty = new ArrayList ();
  208. list.Add (acc);
  209. Socket.Select (list, empty, empty, 100);
  210. Assert.AreEqual (0, empty.Count, "#01");
  211. Assert.AreEqual (1, list.Count, "#02");
  212. Socket.Select (empty, list, empty, 100);
  213. Assert.AreEqual (0, empty.Count, "#03");
  214. Assert.AreEqual (1, list.Count, "#04");
  215. Socket.Select (list, empty, empty, -1);
  216. Assert.AreEqual (0, empty.Count, "#05");
  217. Assert.AreEqual (1, list.Count, "#06");
  218. // Need to read the 10 bytes from the client to avoid a RST
  219. byte [] bytes = new byte [10];
  220. acc.Receive (bytes);
  221. } finally {
  222. if (acc != null)
  223. acc.Close ();
  224. srv.Close ();
  225. }
  226. }
  227. static Socket CreateServer ()
  228. {
  229. Socket sock = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  230. sock.Bind (new IPEndPoint (IPAddress.Loopback, 0));
  231. sock.Listen (1);
  232. return sock;
  233. }
  234. class ClientSocket {
  235. Socket sock;
  236. EndPoint ep;
  237. public ClientSocket (EndPoint ep)
  238. {
  239. this.ep = ep;
  240. sock = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  241. }
  242. public void ConnectSleepClose ()
  243. {
  244. sock.Connect (ep);
  245. Thread.Sleep (2000);
  246. sock.Close ();
  247. }
  248. public void Write ()
  249. {
  250. byte [] b = new byte [10];
  251. sock.Send (b);
  252. }
  253. }
  254. byte[] buf = new byte[100];
  255. [Test]
  256. [ExpectedException (typeof (ObjectDisposedException))]
  257. public void Disposed2 ()
  258. {
  259. Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
  260. s.Close();
  261. s.Blocking = true;
  262. }
  263. [Test]
  264. [ExpectedException (typeof (ObjectDisposedException))]
  265. public void Disposed6 ()
  266. {
  267. Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
  268. s.Close();
  269. s.Listen (5);
  270. }
  271. [Test]
  272. [ExpectedException (typeof (ObjectDisposedException))]
  273. public void Disposed7 ()
  274. {
  275. Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
  276. s.Close();
  277. s.Poll (100, 0);
  278. }
  279. [Test]
  280. [ExpectedException (typeof (ObjectDisposedException))]
  281. public void Disposed15 ()
  282. {
  283. Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
  284. s.Close();
  285. s.Send (buf);
  286. }
  287. [Test]
  288. [ExpectedException (typeof (ObjectDisposedException))]
  289. public void Disposed16 ()
  290. {
  291. Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
  292. s.Close();
  293. s.Send (buf, 0);
  294. }
  295. [Test]
  296. [ExpectedException (typeof (ObjectDisposedException))]
  297. public void Disposed17 ()
  298. {
  299. Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
  300. s.Close();
  301. s.Send (buf, 10, 0);
  302. }
  303. [Test]
  304. [ExpectedException (typeof (ObjectDisposedException))]
  305. public void Disposed18 ()
  306. {
  307. Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
  308. s.Close();
  309. s.Send (buf, 0, 10, 0);
  310. }
  311. [Test]
  312. [ExpectedException (typeof (ObjectDisposedException))]
  313. public void Disposed19 ()
  314. {
  315. Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
  316. EndPoint ep = new IPEndPoint (IPAddress.Any, 31337);
  317. s.Close();
  318. s.SendTo (buf, 0, ep);
  319. }
  320. [Test]
  321. [ExpectedException (typeof (ObjectDisposedException))]
  322. public void Disposed20 ()
  323. {
  324. Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
  325. EndPoint ep = new IPEndPoint (IPAddress.Any, 31337);
  326. s.Close();
  327. s.SendTo (buf, 10, 0, ep);
  328. }
  329. [Test]
  330. [ExpectedException (typeof (ObjectDisposedException))]
  331. public void Disposed21 ()
  332. {
  333. Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
  334. EndPoint ep = new IPEndPoint (IPAddress.Any, 31337);
  335. s.Close();
  336. s.SendTo (buf, 0, 10, 0, ep);
  337. }
  338. [Test]
  339. [ExpectedException (typeof (ObjectDisposedException))]
  340. public void Disposed22 ()
  341. {
  342. Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
  343. EndPoint ep = new IPEndPoint (IPAddress.Any, 31337);
  344. s.Close();
  345. s.SendTo (buf, ep);
  346. }
  347. [Test]
  348. [ExpectedException (typeof (ObjectDisposedException))]
  349. public void Disposed23 ()
  350. {
  351. Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
  352. s.Close();
  353. s.Shutdown (0);
  354. }
  355. [Test]
  356. public void GetHashCodeTest ()
  357. {
  358. Socket server = new Socket (AddressFamily.InterNetwork,
  359. SocketType.Stream, ProtocolType.Tcp);
  360. IPEndPoint ep = new IPEndPoint (IPAddress.Loopback,
  361. 9010);
  362. server.Bind (ep);
  363. server.Listen (1);
  364. Socket client = new Socket (AddressFamily.InterNetwork,
  365. SocketType.Stream, ProtocolType.Tcp);
  366. int hashcodeA = client.GetHashCode ();
  367. client.Connect (ep);
  368. int hashcodeB = client.GetHashCode ();
  369. Assert.AreEqual (hashcodeA, hashcodeB, "#1");
  370. client.Close ();
  371. int hashcodeC = client.GetHashCode ();
  372. #if NET_2_0
  373. Assert.AreEqual (hashcodeB, hashcodeC, "#2");
  374. #else
  375. Assert.IsFalse (hashcodeB == hashcodeC, "#2");
  376. #endif
  377. server.Close ();
  378. }
  379. static ManualResetEvent SocketError_event = new ManualResetEvent (false);
  380. private static void SocketError_callback (IAsyncResult ar)
  381. {
  382. Socket sock = (Socket)ar.AsyncState;
  383. if(sock.Connected) {
  384. sock.EndConnect (ar);
  385. }
  386. SocketError_event.Set ();
  387. }
  388. [Test]
  389. public void SocketErrorTest ()
  390. {
  391. Socket sock = new Socket (AddressFamily.InterNetwork,
  392. SocketType.Stream,
  393. ProtocolType.Tcp);
  394. IPEndPoint ep = new IPEndPoint (IPAddress.Loopback,
  395. BogusPort);
  396. SocketError_event.Reset ();
  397. sock.Blocking = false;
  398. sock.BeginConnect (ep, new AsyncCallback(SocketError_callback),
  399. sock);
  400. if (SocketError_event.WaitOne (2000, false) == false) {
  401. Assert.Fail ("SocketError wait timed out");
  402. }
  403. Assert.AreEqual (false, sock.Connected, "SocketError #1");
  404. int error;
  405. error = (int)sock.GetSocketOption (SocketOptionLevel.Socket, SocketOptionName.Error);
  406. Assert.AreEqual (10061, error, "SocketError #2");
  407. error = (int)sock.GetSocketOption (SocketOptionLevel.Socket, SocketOptionName.Error);
  408. Assert.AreEqual (10061, error, "SocketError #3");
  409. sock.Close ();
  410. }
  411. #if NET_2_0
  412. [Test]
  413. public void SocketInformationCtor ()
  414. {
  415. }
  416. [Test]
  417. public void DontFragmentDefaultTcp ()
  418. {
  419. Socket sock = new Socket (AddressFamily.InterNetwork,
  420. SocketType.Stream,
  421. ProtocolType.Tcp);
  422. Assert.AreEqual (false, sock.DontFragment, "DontFragmentDefaultTcp");
  423. sock.Close ();
  424. }
  425. [Test]
  426. [Category ("NotWorking")] // DontFragment doesn't work
  427. public void DontFragmentChangeTcp ()
  428. {
  429. Socket sock = new Socket (AddressFamily.InterNetwork,
  430. SocketType.Stream,
  431. ProtocolType.Tcp);
  432. sock.DontFragment = true;
  433. Assert.AreEqual (true, sock.DontFragment, "DontFragmentChangeTcp");
  434. sock.Close ();
  435. }
  436. [Test]
  437. public void DontFragmentDefaultUdp ()
  438. {
  439. Socket sock = new Socket (AddressFamily.InterNetwork,
  440. SocketType.Dgram,
  441. ProtocolType.Udp);
  442. Assert.AreEqual (false, sock.DontFragment, "DontFragmentDefaultUdp");
  443. sock.Close ();
  444. }
  445. [Test]
  446. [Category ("NotWorking")] // DontFragment doesn't work
  447. public void DontFragmentChangeUdp ()
  448. {
  449. Socket sock = new Socket (AddressFamily.InterNetwork,
  450. SocketType.Dgram,
  451. ProtocolType.Udp);
  452. sock.DontFragment = true;
  453. Assert.AreEqual (true, sock.DontFragment, "DontFragmentChangeUdp");
  454. sock.Close ();
  455. }
  456. [Test]
  457. [ExpectedException (typeof(ObjectDisposedException))]
  458. public void DontFragmentClosed ()
  459. {
  460. Socket sock = new Socket (AddressFamily.InterNetwork,
  461. SocketType.Stream,
  462. ProtocolType.Tcp);
  463. sock.Close ();
  464. bool val = sock.DontFragment;
  465. }
  466. [Test]
  467. [Category ("NotWorking")] // Need to pick a non-IP AddressFamily that "works" on both mono and ms, this one only works on ms
  468. public void DontFragment ()
  469. {
  470. Socket sock = new Socket (AddressFamily.NetBios,
  471. SocketType.Seqpacket,
  472. ProtocolType.Unspecified);
  473. try {
  474. sock.DontFragment = true;
  475. Assert.Fail ("DontFragment #1");
  476. } catch (NotSupportedException) {
  477. } catch {
  478. Assert.Fail ("DontFragment #2");
  479. } finally {
  480. sock.Close ();
  481. }
  482. }
  483. [Test]
  484. public void EnableBroadcastDefaultTcp ()
  485. {
  486. Socket sock = new Socket (AddressFamily.InterNetwork,
  487. SocketType.Stream,
  488. ProtocolType.Tcp);
  489. try {
  490. bool value = sock.EnableBroadcast;
  491. Assert.Fail ("EnableBroadcastDefaultTcp #1");
  492. } catch (SocketException ex) {
  493. Assert.AreEqual (10042, ex.ErrorCode, "EnableBroadcastDefaultTcp #2");
  494. } catch {
  495. Assert.Fail ("EnableBroadcastDefaultTcp #2");
  496. } finally {
  497. sock.Close ();
  498. }
  499. }
  500. [Test]
  501. public void EnableBroadcastDefaultUdp ()
  502. {
  503. Socket sock = new Socket (AddressFamily.InterNetwork,
  504. SocketType.Dgram,
  505. ProtocolType.Udp);
  506. Assert.AreEqual (false, sock.EnableBroadcast, "EnableBroadcastDefaultUdp");
  507. sock.Close ();
  508. }
  509. [Test]
  510. public void EnableBroadcastChangeTcp ()
  511. {
  512. Socket sock = new Socket (AddressFamily.InterNetwork,
  513. SocketType.Stream,
  514. ProtocolType.Tcp);
  515. try {
  516. sock.EnableBroadcast = true;
  517. Assert.Fail ("EnableBroadcastChangeTcp #1");
  518. } catch (SocketException ex) {
  519. Assert.AreEqual (10042, ex.ErrorCode, "EnableBroadcastChangeTcp #2");
  520. } catch {
  521. Assert.Fail ("EnableBroadcastChangeTcp #2");
  522. } finally {
  523. sock.Close ();
  524. }
  525. }
  526. [Test]
  527. public void EnableBroadcastChangeUdp ()
  528. {
  529. Socket sock = new Socket (AddressFamily.InterNetwork,
  530. SocketType.Dgram,
  531. ProtocolType.Udp);
  532. sock.EnableBroadcast = true;
  533. Assert.AreEqual (true, sock.EnableBroadcast, "EnableBroadcastChangeUdp");
  534. sock.Close ();
  535. }
  536. [Test]
  537. [ExpectedException (typeof(ObjectDisposedException))]
  538. public void EnableBroadcastClosed ()
  539. {
  540. Socket sock = new Socket (AddressFamily.InterNetwork,
  541. SocketType.Dgram,
  542. ProtocolType.Udp);
  543. sock.Close ();
  544. bool val = sock.EnableBroadcast;
  545. }
  546. /* Can't test the default for ExclusiveAddressUse as
  547. * it's different on different versions and service
  548. * packs of windows
  549. */
  550. [Test]
  551. [Category ("NotWorking")] // Not supported on Linux
  552. public void ExclusiveAddressUseUnbound ()
  553. {
  554. Socket sock = new Socket (AddressFamily.InterNetwork,
  555. SocketType.Stream,
  556. ProtocolType.Tcp);
  557. sock.ExclusiveAddressUse = true;
  558. Assert.AreEqual (true, sock.ExclusiveAddressUse, "ExclusiveAddressUseUnbound");
  559. sock.Close ();
  560. }
  561. [Test]
  562. [ExpectedException (typeof(InvalidOperationException))]
  563. [Category ("NotWorking")] // Not supported on Linux
  564. public void ExclusiveAddressUseBound ()
  565. {
  566. Socket sock = new Socket (AddressFamily.InterNetwork,
  567. SocketType.Stream,
  568. ProtocolType.Tcp);
  569. sock.Bind (new IPEndPoint (IPAddress.Any, 1235));
  570. sock.ExclusiveAddressUse = true;
  571. sock.Close ();
  572. }
  573. [Test]
  574. [ExpectedException (typeof(ObjectDisposedException))]
  575. public void ExclusiveAddressUseClosed ()
  576. {
  577. Socket sock = new Socket (AddressFamily.InterNetwork,
  578. SocketType.Stream,
  579. ProtocolType.Tcp);
  580. sock.Close ();
  581. bool val = sock.ExclusiveAddressUse;
  582. }
  583. [Test]
  584. public void IsBoundTcp ()
  585. {
  586. Socket sock = new Socket (AddressFamily.InterNetwork,
  587. SocketType.Stream,
  588. ProtocolType.Tcp);
  589. IPEndPoint ep = new IPEndPoint (IPAddress.Loopback,
  590. BogusPort);
  591. Assert.AreEqual (false, sock.IsBound, "IsBoundTcp #1");
  592. sock.Bind (ep);
  593. Assert.AreEqual (true, sock.IsBound, "IsBoundTcp #2");
  594. sock.Listen (1);
  595. Socket sock2 = new Socket (AddressFamily.InterNetwork,
  596. SocketType.Stream,
  597. ProtocolType.Tcp);
  598. Assert.AreEqual (false, sock2.IsBound, "IsBoundTcp #3");
  599. sock2.Connect (ep);
  600. Assert.AreEqual (true, sock2.IsBound, "IsBoundTcp #4");
  601. sock2.Close ();
  602. Assert.AreEqual (true, sock2.IsBound, "IsBoundTcp #5");
  603. sock.Close ();
  604. Assert.AreEqual (true, sock.IsBound, "IsBoundTcp #6");
  605. }
  606. [Test]
  607. public void IsBoundUdp ()
  608. {
  609. Socket sock = new Socket (AddressFamily.InterNetwork,
  610. SocketType.Dgram,
  611. ProtocolType.Udp);
  612. IPEndPoint ep = new IPEndPoint (IPAddress.Loopback,
  613. BogusPort);
  614. Assert.AreEqual (false, sock.IsBound, "IsBoundUdp #1");
  615. sock.Bind (ep);
  616. Assert.AreEqual (true, sock.IsBound, "IsBoundUdp #2");
  617. sock.Close ();
  618. Assert.AreEqual (true, sock.IsBound, "IsBoundUdp #3");
  619. sock = new Socket (AddressFamily.InterNetwork,
  620. SocketType.Dgram,
  621. ProtocolType.Udp);
  622. Assert.AreEqual (false, sock.IsBound, "IsBoundUdp #4");
  623. sock.Connect (ep);
  624. Assert.AreEqual (true, sock.IsBound, "IsBoundUdp #5");
  625. sock.Close ();
  626. Assert.AreEqual (true, sock.IsBound, "IsBoundUdp #6");
  627. }
  628. [Test]
  629. /* Should not throw an exception */
  630. public void IsBoundClosed ()
  631. {
  632. Socket sock = new Socket (AddressFamily.InterNetwork,
  633. SocketType.Stream,
  634. ProtocolType.Tcp);
  635. sock.Close ();
  636. bool val = sock.IsBound;
  637. }
  638. /* Nothing much to test for LingerState */
  639. [Test]
  640. public void MulticastLoopbackDefaultTcp ()
  641. {
  642. Socket sock = new Socket (AddressFamily.InterNetwork,
  643. SocketType.Stream,
  644. ProtocolType.Tcp);
  645. try {
  646. bool value = sock.MulticastLoopback;
  647. Assert.Fail ("MulticastLoopbackDefaultTcp #1");
  648. } catch (SocketException ex) {
  649. Assert.AreEqual (10042, ex.ErrorCode, "MulticastLoopbackDefaultTcp #2");
  650. } catch {
  651. Assert.Fail ("MulticastLoopbackDefaultTcp #2");
  652. } finally {
  653. sock.Close ();
  654. }
  655. }
  656. [Test]
  657. public void MulticastLoopbackChangeTcp ()
  658. {
  659. Socket sock = new Socket (AddressFamily.InterNetwork,
  660. SocketType.Stream,
  661. ProtocolType.Tcp);
  662. try {
  663. sock.MulticastLoopback = false;
  664. Assert.Fail ("MulticastLoopbackChangeTcp #1");
  665. } catch (SocketException ex) {
  666. Assert.AreEqual (10042, ex.ErrorCode, "MulticastLoopbackChangeTcp #2");
  667. } catch {
  668. Assert.Fail ("MulticastLoopbackChangeTcp #2");
  669. } finally {
  670. sock.Close ();
  671. }
  672. }
  673. [Test]
  674. public void MulticastLoopbackDefaultUdp ()
  675. {
  676. Socket sock = new Socket (AddressFamily.InterNetwork,
  677. SocketType.Dgram,
  678. ProtocolType.Udp);
  679. Assert.AreEqual (true, sock.MulticastLoopback, "MulticastLoopbackDefaultUdp");
  680. sock.Close ();
  681. }
  682. [Test]
  683. public void MulticastLoopbackChangeUdp ()
  684. {
  685. Socket sock = new Socket (AddressFamily.InterNetwork,
  686. SocketType.Dgram,
  687. ProtocolType.Udp);
  688. sock.MulticastLoopback = false;
  689. Assert.AreEqual (false, sock.MulticastLoopback, "MulticastLoopbackChangeUdp");
  690. sock.Close ();
  691. }
  692. [Test]
  693. [ExpectedException (typeof(ObjectDisposedException))]
  694. public void MulticastLoopbackClosed ()
  695. {
  696. Socket sock = new Socket (AddressFamily.InterNetwork,
  697. SocketType.Stream,
  698. ProtocolType.Tcp);
  699. sock.Close ();
  700. bool val = sock.MulticastLoopback;
  701. }
  702. /* OSSupportsIPv6 depends on the environment */
  703. [Test]
  704. [Category("NotWorking")] // We have different defaults for perf reasons
  705. public void ReceiveBufferSizeDefault ()
  706. {
  707. Socket sock = new Socket (AddressFamily.InterNetwork,
  708. SocketType.Stream,
  709. ProtocolType.Tcp);
  710. Assert.AreEqual (8192, sock.ReceiveBufferSize, "ReceiveBufferSizeDefault");
  711. sock.Close ();
  712. }
  713. [Test]
  714. [Category("NotWorking")] // We have different defaults for perf reasons
  715. public void ReceiveBufferSizeDefaultUdp ()
  716. {
  717. Socket sock = new Socket (AddressFamily.InterNetwork,
  718. SocketType.Dgram,
  719. ProtocolType.Udp);
  720. Assert.AreEqual (8192, sock.ReceiveBufferSize, "ReceiveBufferSizeDefaultUdp");
  721. sock.Close ();
  722. }
  723. [Test]
  724. public void ReceiveBufferSizeChange ()
  725. {
  726. Socket sock = new Socket (AddressFamily.InterNetwork,
  727. SocketType.Stream,
  728. ProtocolType.Tcp);
  729. sock.ReceiveBufferSize = 16384;
  730. Assert.AreEqual (16384, sock.ReceiveBufferSize, "ReceiveBufferSizeChange");
  731. sock.Close ();
  732. }
  733. [Test]
  734. [Category("NotWorking")] // We cannot totally remove buffers (minimum is set to 256
  735. public void BuffersCheck_None ()
  736. {
  737. using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) {
  738. int original = s.ReceiveBufferSize;
  739. s.ReceiveBufferSize = 0;
  740. Assert.AreEqual (0, s.ReceiveBufferSize, "ReceiveBufferSize " + original.ToString ());
  741. original = s.SendBufferSize;
  742. s.SendBufferSize = 0;
  743. Assert.AreEqual (0, s.SendBufferSize, "SendBufferSize " + original.ToString ());
  744. }
  745. }
  746. [Test]
  747. [ExpectedException (typeof(ObjectDisposedException))]
  748. public void ReceiveBufferSizeClosed ()
  749. {
  750. Socket sock = new Socket (AddressFamily.InterNetwork,
  751. SocketType.Stream,
  752. ProtocolType.Tcp);
  753. sock.Close ();
  754. int val = sock.ReceiveBufferSize;
  755. }
  756. [Test]
  757. [Category("NotWorking")] // We have different defaults for perf reasons
  758. public void SendBufferSizeDefault ()
  759. {
  760. Socket sock = new Socket (AddressFamily.InterNetwork,
  761. SocketType.Stream,
  762. ProtocolType.Tcp);
  763. Assert.AreEqual (8192, sock.SendBufferSize, "SendBufferSizeDefault");
  764. sock.Close ();
  765. }
  766. [Test]
  767. [Category("NotWorking")] // We have different defaults for perf reasons
  768. public void SendBufferSizeDefaultUdp ()
  769. {
  770. Socket sock = new Socket (AddressFamily.InterNetwork,
  771. SocketType.Dgram,
  772. ProtocolType.Udp);
  773. Assert.AreEqual (8192, sock.SendBufferSize, "SendBufferSizeDefaultUdp");
  774. sock.Close ();
  775. }
  776. [Test]
  777. public void SendBufferSizeChange ()
  778. {
  779. Socket sock = new Socket (AddressFamily.InterNetwork,
  780. SocketType.Stream,
  781. ProtocolType.Tcp);
  782. sock.SendBufferSize = 16384;
  783. Assert.AreEqual (16384, sock.SendBufferSize, "SendBufferSizeChange");
  784. sock.Close ();
  785. }
  786. [Test]
  787. [ExpectedException (typeof(ObjectDisposedException))]
  788. public void SendBufferSizeClosed ()
  789. {
  790. Socket sock = new Socket (AddressFamily.InterNetwork,
  791. SocketType.Stream,
  792. ProtocolType.Tcp);
  793. sock.Close ();
  794. int val = sock.SendBufferSize;
  795. }
  796. /* No test for TTL default as it's platform dependent */
  797. [Test]
  798. public void TtlChange ()
  799. {
  800. Socket sock = new Socket (AddressFamily.InterNetwork,
  801. SocketType.Stream,
  802. ProtocolType.Tcp);
  803. sock.Ttl = 255;
  804. Assert.AreEqual (255, sock.Ttl, "TtlChange");
  805. sock.Close ();
  806. }
  807. [Test]
  808. [Category ("NotOnMac")] // Mac doesn't throw when overflowing the ttl
  809. public void TtlChangeOverflow ()
  810. {
  811. Socket sock = new Socket (AddressFamily.InterNetwork,
  812. SocketType.Stream,
  813. ProtocolType.Tcp);
  814. try {
  815. sock.Ttl = 256;
  816. Assert.Fail ("TtlChangeOverflow #1");
  817. } catch (SocketException ex) {
  818. Assert.AreEqual (10022, ex.ErrorCode,
  819. "TtlChangeOverflow #2");
  820. } catch {
  821. Assert.Fail ("TtlChangeoverflow #3");
  822. } finally {
  823. sock.Close ();
  824. }
  825. }
  826. /* Apparently you can set TTL=0 on the ms runtime!!
  827. try {
  828. sock.Ttl = 0;
  829. Assert.Fail ("TtlChangeOverflow #4");
  830. } catch (SocketException ex) {
  831. Assert.AreEqual (10022, ex.ErrorCode,
  832. "TtlChangeOverflow #5");
  833. } catch {
  834. Assert.Fail ("TtlChangeOverflow #6");
  835. } finally {
  836. sock.Close ();
  837. }
  838. */
  839. [Test]
  840. [ExpectedException (typeof(ObjectDisposedException))]
  841. public void TtlClosed ()
  842. {
  843. Socket sock = new Socket (AddressFamily.InterNetwork,
  844. SocketType.Stream,
  845. ProtocolType.Tcp);
  846. sock.Close ();
  847. int val = sock.Ttl;
  848. }
  849. [Test]
  850. public void UseOnlyOverlappedIODefault ()
  851. {
  852. Socket sock = new Socket (AddressFamily.InterNetwork,
  853. SocketType.Stream,
  854. ProtocolType.Tcp);
  855. Assert.AreEqual (false, sock.UseOnlyOverlappedIO, "UseOnlyOverlappedIODefault");
  856. sock.Close ();
  857. }
  858. //
  859. // We need this because the Linux kernel in certain configurations
  860. // will end up rounding up the values passed on to the kernel
  861. // for socket send/receive timeouts.
  862. //
  863. int Approximate (int target, int value)
  864. {
  865. int epsilon = 10;
  866. if (value > target-10 && value < target+10)
  867. return target;
  868. return value;
  869. }
  870. [Test]
  871. public void UseOnlyOverlappedIOChange ()
  872. {
  873. Socket sock = new Socket (AddressFamily.InterNetwork,
  874. SocketType.Stream,
  875. ProtocolType.Tcp);
  876. sock.UseOnlyOverlappedIO = true;
  877. Assert.AreEqual (true, sock.UseOnlyOverlappedIO, "UseOnlyOverlappedIOChange");
  878. sock.Close ();
  879. }
  880. [Test]
  881. /* Should not throw an exception */
  882. public void UseOnlyOverlappedIOClosed ()
  883. {
  884. Socket sock = new Socket (AddressFamily.InterNetwork,
  885. SocketType.Stream,
  886. ProtocolType.Tcp);
  887. sock.Close ();
  888. bool val = sock.UseOnlyOverlappedIO;
  889. }
  890. [Test]
  891. public void SendTimeoutDefault ()
  892. {
  893. Socket sock = new Socket (AddressFamily.InterNetwork,
  894. SocketType.Stream,
  895. ProtocolType.Tcp);
  896. Assert.AreEqual (0, sock.SendTimeout, "SendTimeoutDefault");
  897. sock.Close ();
  898. }
  899. [Test]
  900. public void SendTimeoutChange ()
  901. {
  902. Socket sock = new Socket (AddressFamily.InterNetwork,
  903. SocketType.Stream,
  904. ProtocolType.Tcp);
  905. /* Should be rounded up to 500, according to
  906. * the MSDN docs, but the MS runtime doesn't
  907. */
  908. sock.SendTimeout = 50;
  909. Assert.AreEqual (50, Approximate (50, sock.SendTimeout), "SendTimeoutChange #1");
  910. sock.SendTimeout = 2000;
  911. Assert.AreEqual (2000, Approximate (2000, sock.SendTimeout), "SendTimeoutChange #2");
  912. sock.SendTimeout = 0;
  913. Assert.AreEqual (0, Approximate (0, sock.SendTimeout), "SendTimeoutChange #3");
  914. /* Should be the same as setting 0 */
  915. sock.SendTimeout = -1;
  916. Assert.AreEqual (0, sock.SendTimeout, "SendTimeoutChange #4");
  917. sock.SendTimeout = 65536;
  918. Assert.AreEqual (65536, Approximate (65536, sock.SendTimeout), "SendTimeoutChange #5");
  919. try {
  920. sock.SendTimeout = -2;
  921. Assert.Fail ("SendTimeoutChange #8");
  922. } catch (ArgumentOutOfRangeException) {
  923. } catch {
  924. Assert.Fail ("SendTimeoutChange #9");
  925. } finally {
  926. sock.Close ();
  927. }
  928. }
  929. [Test]
  930. [ExpectedException (typeof(ObjectDisposedException))]
  931. public void SendTimeoutClosed ()
  932. {
  933. Socket sock = new Socket (AddressFamily.InterNetwork,
  934. SocketType.Stream,
  935. ProtocolType.Tcp);
  936. sock.Close ();
  937. int val = sock.SendTimeout;
  938. }
  939. [Test]
  940. public void ReceiveTimeoutDefault ()
  941. {
  942. Socket sock = new Socket (AddressFamily.InterNetwork,
  943. SocketType.Stream,
  944. ProtocolType.Tcp);
  945. Assert.AreEqual (0, sock.ReceiveTimeout, "ReceiveTimeoutDefault");
  946. sock.Close ();
  947. }
  948. [Test]
  949. public void ReceiveTimeoutChange ()
  950. {
  951. Socket sock = new Socket (AddressFamily.InterNetwork,
  952. SocketType.Stream,
  953. ProtocolType.Tcp);
  954. sock.ReceiveTimeout = 50;
  955. Assert.AreEqual (50, Approximate (50, sock.ReceiveTimeout), "ReceiveTimeoutChange #1");
  956. sock.ReceiveTimeout = 2000;
  957. Assert.AreEqual (2000, Approximate (2000, sock.ReceiveTimeout), "ReceiveTimeoutChange #2");
  958. sock.ReceiveTimeout = 0;
  959. Assert.AreEqual (0, sock.ReceiveTimeout, "ReceiveTimeoutChange #3");
  960. /* Should be the same as setting 0 */
  961. sock.ReceiveTimeout = -1;
  962. Assert.AreEqual (0, sock.ReceiveTimeout, "ReceiveTimeoutChange #4");
  963. sock.ReceiveTimeout = 65536;
  964. Assert.AreEqual (65536, Approximate (65536, sock.ReceiveTimeout), "ReceiveTimeoutChange #5");
  965. try {
  966. sock.ReceiveTimeout = -2;
  967. Assert.Fail ("ReceiveTimeoutChange #8");
  968. } catch (ArgumentOutOfRangeException) {
  969. } catch {
  970. Assert.Fail ("ReceiveTimeoutChange #9");
  971. } finally {
  972. sock.Close ();
  973. }
  974. }
  975. [Test]
  976. [ExpectedException (typeof(ObjectDisposedException))]
  977. public void ReceiveTimeoutClosed ()
  978. {
  979. Socket sock = new Socket (AddressFamily.InterNetwork,
  980. SocketType.Stream,
  981. ProtocolType.Tcp);
  982. sock.Close ();
  983. int val = sock.ReceiveTimeout;
  984. }
  985. [Test]
  986. public void NoDelayDefaultTcp ()
  987. {
  988. Socket sock = new Socket (AddressFamily.InterNetwork,
  989. SocketType.Stream,
  990. ProtocolType.Tcp);
  991. Assert.AreEqual (false, sock.NoDelay, "NoDelayDefaultTcp");
  992. sock.Close ();
  993. }
  994. [Test]
  995. public void NoDelayChangeTcp ()
  996. {
  997. Socket sock = new Socket (AddressFamily.InterNetwork,
  998. SocketType.Stream,
  999. ProtocolType.Tcp);
  1000. sock.NoDelay = true;
  1001. Assert.AreEqual (true, sock.NoDelay, "NoDelayChangeTcp");
  1002. sock.Close ();
  1003. }
  1004. [Test]
  1005. public void NoDelayDefaultUdp ()
  1006. {
  1007. Socket sock = new Socket (AddressFamily.InterNetwork,
  1008. SocketType.Dgram,
  1009. ProtocolType.Udp);
  1010. try {
  1011. bool val = sock.NoDelay;
  1012. Assert.Fail ("NoDelayDefaultUdp #1");
  1013. } catch (SocketException ex) {
  1014. Assert.AreEqual (10042, ex.ErrorCode,
  1015. "NoDelayDefaultUdp #2");
  1016. } catch {
  1017. Assert.Fail ("NoDelayDefaultUdp #3");
  1018. } finally {
  1019. sock.Close ();
  1020. }
  1021. }
  1022. [Test]
  1023. public void NoDelayChangeUdp ()
  1024. {
  1025. Socket sock = new Socket (AddressFamily.InterNetwork,
  1026. SocketType.Dgram,
  1027. ProtocolType.Udp);
  1028. try {
  1029. sock.NoDelay = true;
  1030. Assert.Fail ("NoDelayChangeUdp #1");
  1031. } catch (SocketException ex) {
  1032. Assert.AreEqual (10042, ex.ErrorCode,
  1033. "NoDelayChangeUdp #2");
  1034. } catch {
  1035. Assert.Fail ("NoDelayChangeUdp #3");
  1036. } finally {
  1037. sock.Close ();
  1038. }
  1039. }
  1040. [Test]
  1041. [ExpectedException (typeof(ObjectDisposedException))]
  1042. public void NoDelayClosed ()
  1043. {
  1044. Socket sock = new Socket (AddressFamily.InterNetwork,
  1045. SocketType.Stream,
  1046. ProtocolType.Tcp);
  1047. sock.Close ();
  1048. bool val = sock.NoDelay;
  1049. }
  1050. static bool BAAccepted = false;
  1051. static Socket BASocket = null;
  1052. static ManualResetEvent BACalledBack = new ManualResetEvent (false);
  1053. private static void BACallback (IAsyncResult asyncResult)
  1054. {
  1055. Socket sock = (Socket)asyncResult.AsyncState;
  1056. BASocket = sock.EndAccept (asyncResult);
  1057. BAAccepted = true;
  1058. BACalledBack.Set ();
  1059. }
  1060. [Test]
  1061. [ExpectedException (typeof(InvalidOperationException))]
  1062. public void BeginAcceptNotBound ()
  1063. {
  1064. Socket sock = new Socket (AddressFamily.InterNetwork,
  1065. SocketType.Stream,
  1066. ProtocolType.Tcp);
  1067. sock.BeginAccept (BACallback, sock);
  1068. sock.Close ();
  1069. }
  1070. [Test]
  1071. [ExpectedException (typeof(InvalidOperationException))]
  1072. public void BeginAcceptNotListening ()
  1073. {
  1074. Socket sock = new Socket (AddressFamily.InterNetwork,
  1075. SocketType.Stream,
  1076. ProtocolType.Tcp);
  1077. sock.Bind (new IPEndPoint (IPAddress.Any, 1236));
  1078. sock.BeginAccept (BACallback, sock);
  1079. sock.Close ();
  1080. }
  1081. [Test]
  1082. public void BeginAccept ()
  1083. {
  1084. Socket sock = new Socket (AddressFamily.InterNetwork,
  1085. SocketType.Stream,
  1086. ProtocolType.Tcp);
  1087. IPEndPoint ep = new IPEndPoint (IPAddress.Loopback,
  1088. 1237);
  1089. sock.Bind (ep);
  1090. sock.Listen (1);
  1091. BACalledBack.Reset ();
  1092. sock.BeginAccept (BACallback, sock);
  1093. Socket conn = new Socket (AddressFamily.InterNetwork,
  1094. SocketType.Stream,
  1095. ProtocolType.Tcp);
  1096. conn.Connect (ep);
  1097. if (BACalledBack.WaitOne (2000, false) == false) {
  1098. Assert.Fail ("BeginAccept wait timed out");
  1099. }
  1100. Assert.AreEqual (true, BAAccepted, "BeginAccept #1");
  1101. Assert.AreEqual (true, BASocket.Connected, "BeginAccept #2");
  1102. Assert.AreEqual (false, sock.Connected, "BeginAccept #3");
  1103. Assert.AreEqual (true, conn.Connected, "BeginAccept #4");
  1104. BASocket.Close ();
  1105. conn.Close ();
  1106. sock.Close ();
  1107. }
  1108. [Test]
  1109. [ExpectedException (typeof(ObjectDisposedException))]
  1110. public void BeginAcceptClosed ()
  1111. {
  1112. Socket sock = new Socket (AddressFamily.InterNetwork,
  1113. SocketType.Stream,
  1114. ProtocolType.Tcp);
  1115. sock.Close ();
  1116. sock.BeginAccept (BACallback, sock);
  1117. }
  1118. static bool BADAccepted = false;
  1119. static Socket BADSocket = null;
  1120. static byte[] BADBytes;
  1121. static int BADByteCount;
  1122. static ManualResetEvent BADCalledBack = new ManualResetEvent (false);
  1123. private static void BADCallback (IAsyncResult asyncResult)
  1124. {
  1125. Socket sock = (Socket)asyncResult.AsyncState;
  1126. BADSocket = sock.EndAccept (out BADBytes,
  1127. out BADByteCount,
  1128. asyncResult);
  1129. BADAccepted = true;
  1130. BADCalledBack.Set ();
  1131. }
  1132. [Test]
  1133. public void BeginAcceptData ()
  1134. {
  1135. Socket sock = new Socket (AddressFamily.InterNetwork,
  1136. SocketType.Stream,
  1137. ProtocolType.Tcp);
  1138. IPEndPoint ep = new IPEndPoint (IPAddress.Loopback,
  1139. 1238);
  1140. sock.Bind (ep);
  1141. sock.Listen (1);
  1142. BADCalledBack.Reset ();
  1143. sock.BeginAccept (256, BADCallback, sock);
  1144. Socket conn = new Socket (AddressFamily.InterNetwork,
  1145. SocketType.Stream,
  1146. ProtocolType.Tcp);
  1147. byte[] send_bytes = new byte[] {10, 11, 12, 13};
  1148. conn.Connect (ep);
  1149. conn.Send (send_bytes);
  1150. if (BADCalledBack.WaitOne (2000, false) == false) {
  1151. Assert.Fail ("BeginAcceptData wait timed out");
  1152. }
  1153. Assert.AreEqual (true, BADAccepted, "BeginAcceptData #1");
  1154. Assert.AreEqual (true, BADSocket.Connected, "BeginAcceptData #2");
  1155. Assert.AreEqual (false, sock.Connected, "BeginAcceptData #3");
  1156. Assert.AreEqual (true, conn.Connected, "BeginAcceptData #4");
  1157. Assert.AreEqual (send_bytes.Length, BADByteCount, "BeginAcceptData #5");
  1158. /* The MS runtime gives the returned data in a
  1159. * much bigger array. TODO: investigate
  1160. * whether it the size correlates to the first
  1161. * parameter in BeginAccept()
  1162. */
  1163. Assert.IsFalse (BADBytes.Length == send_bytes.Length,
  1164. "BeginAcceptData #6");
  1165. for(int i = 0; i < send_bytes.Length; i++) {
  1166. Assert.AreEqual (send_bytes[i], BADBytes[i], "BeginAcceptData #" + (i+7).ToString ());
  1167. }
  1168. BADSocket.Close ();
  1169. conn.Close ();
  1170. sock.Close ();
  1171. }
  1172. [Test]
  1173. [ExpectedException (typeof(ObjectDisposedException))]
  1174. public void BeginAcceptDataClosed ()
  1175. {
  1176. Socket sock = new Socket (AddressFamily.InterNetwork,
  1177. SocketType.Stream,
  1178. ProtocolType.Tcp);
  1179. sock.Close ();
  1180. sock.BeginAccept (256, BADCallback, sock);
  1181. }
  1182. [Test]
  1183. public void BeginAcceptSocketUdp ()
  1184. {
  1185. Socket sock = new Socket (AddressFamily.InterNetwork,
  1186. SocketType.Stream,
  1187. ProtocolType.Tcp);
  1188. Socket acc = new Socket (AddressFamily.InterNetwork,
  1189. SocketType.Dgram,
  1190. ProtocolType.Udp);
  1191. IPEndPoint ep = new IPEndPoint (IPAddress.Loopback,
  1192. 1239);
  1193. sock.Bind (ep);
  1194. sock.Listen (1);
  1195. try {
  1196. sock.BeginAccept (acc, 256, BADCallback, sock);
  1197. Assert.Fail ("BeginAcceptSocketUdp #1");
  1198. } catch (SocketException ex) {
  1199. Assert.AreEqual (10022, ex.ErrorCode, "BeginAcceptSocketUdp #2");
  1200. } catch {
  1201. Assert.Fail ("BeginAcceptSocketUdp #3");
  1202. } finally {
  1203. acc.Close ();
  1204. sock.Close ();
  1205. }
  1206. }
  1207. [Test]
  1208. public void BeginAcceptSocketBound ()
  1209. {
  1210. Socket sock = new Socket (AddressFamily.InterNetwork,
  1211. SocketType.Stream,
  1212. ProtocolType.Tcp);
  1213. Socket acc = new Socket (AddressFamily.InterNetwork,
  1214. SocketType.Stream,
  1215. ProtocolType.Tcp);
  1216. IPEndPoint ep1 = new IPEndPoint (IPAddress.Loopback,
  1217. 1240);
  1218. IPEndPoint ep2 = new IPEndPoint (IPAddress.Loopback,
  1219. 1241);
  1220. sock.Bind (ep1);
  1221. sock.Listen (1);
  1222. acc.Bind (ep2);
  1223. try {
  1224. sock.BeginAccept (acc, 256, BADCallback, sock);
  1225. Assert.Fail ("BeginAcceptSocketBound #1");
  1226. } catch (InvalidOperationException) {
  1227. } catch {
  1228. Assert.Fail ("BeginAcceptSocketBound #2");
  1229. } finally {
  1230. acc.Close ();
  1231. sock.Close ();
  1232. }
  1233. }
  1234. [Test]
  1235. public void BeginAcceptSocket ()
  1236. {
  1237. Socket sock = new Socket (AddressFamily.InterNetwork,
  1238. SocketType.Stream,
  1239. ProtocolType.Tcp);
  1240. Socket acc = new Socket (AddressFamily.InterNetwork,
  1241. SocketType.Stream,
  1242. ProtocolType.Tcp);
  1243. IPEndPoint ep = new IPEndPoint (IPAddress.Loopback,
  1244. 1242);
  1245. sock.Bind (ep);
  1246. sock.Listen (1);
  1247. BADCalledBack.Reset ();
  1248. sock.BeginAccept (acc, 256, BADCallback, sock);
  1249. Socket conn = new Socket (AddressFamily.InterNetwork,
  1250. SocketType.Stream,
  1251. ProtocolType.Tcp);
  1252. byte[] send_bytes = new byte[] {10, 11, 12, 13};
  1253. conn.Connect (ep);
  1254. conn.Send (send_bytes);
  1255. if (BADCalledBack.WaitOne (2000, false) == false) {
  1256. Assert.Fail ("BeginAcceptSocket wait timed out");
  1257. }
  1258. Assert.AreEqual (true, BADAccepted, "BeginAcceptSocket #1");
  1259. Assert.AreEqual (true, BADSocket.Connected, "BeginAcceptSocket #2");
  1260. Assert.AreEqual (false, sock.Connected, "BeginAcceptSocket #3");
  1261. Assert.AreEqual (true, conn.Connected, "BeginAcceptSocket #4");
  1262. Assert.AreEqual (send_bytes.Length, BADByteCount, "BeginAcceptSocket #5");
  1263. Assert.AreEqual (AddressFamily.InterNetwork, acc.AddressFamily, "BeginAcceptSocket #6");
  1264. Assert.AreEqual (SocketType.Stream, acc.SocketType, "BeginAcceptSocket #7");
  1265. Assert.AreEqual (ProtocolType.Tcp, acc.ProtocolType, "BeginAcceptSocket #8");
  1266. Assert.AreEqual (conn.LocalEndPoint, acc.RemoteEndPoint, "BeginAcceptSocket #9");
  1267. /* The MS runtime gives the returned data in a
  1268. * much bigger array. TODO: investigate
  1269. * whether it the size correlates to the first
  1270. * parameter in BeginAccept()
  1271. */
  1272. Assert.IsFalse (BADBytes.Length == send_bytes.Length,
  1273. "BeginAcceptSocket #10");
  1274. for(int i = 0; i < send_bytes.Length; i++) {
  1275. Assert.AreEqual (send_bytes[i], BADBytes[i], "BeginAcceptSocket #" + (i+11).ToString ());
  1276. }
  1277. BADSocket.Close ();
  1278. conn.Close ();
  1279. acc.Close ();
  1280. sock.Close ();
  1281. }
  1282. [Test]
  1283. public void BeginAcceptSocketClosed ()
  1284. {
  1285. Socket sock = new Socket (AddressFamily.InterNetwork,
  1286. SocketType.Stream,
  1287. ProtocolType.Tcp);
  1288. Socket acc = new Socket (AddressFamily.InterNetwork,
  1289. SocketType.Stream,
  1290. ProtocolType.Tcp);
  1291. sock.Close ();
  1292. try {
  1293. sock.BeginAccept (acc, 256, BADCallback, null);
  1294. Assert.Fail ("BeginAcceptSocketClosed #1");
  1295. } catch (ObjectDisposedException) {
  1296. } catch {
  1297. Assert.Fail ("BeginAcceptSocketClosed #2");
  1298. } finally {
  1299. acc.Close ();
  1300. }
  1301. }
  1302. [Test]
  1303. public void BeginAcceptSocketAccClosed ()
  1304. {
  1305. Socket sock = new Socket (AddressFamily.InterNetwork,
  1306. SocketType.Stream,
  1307. ProtocolType.Tcp);
  1308. Socket acc = new Socket (AddressFamily.InterNetwork,
  1309. SocketType.Stream,
  1310. ProtocolType.Tcp);
  1311. IPEndPoint ep = new IPEndPoint (IPAddress.Loopback,
  1312. 1243);
  1313. sock.Bind (ep);
  1314. sock.Listen (1);
  1315. acc.Close ();
  1316. BADCalledBack.Reset ();
  1317. try {
  1318. sock.BeginAccept (acc, 256, BADCallback, null);
  1319. Assert.Fail ("BeginAcceptSocketAccClosed #1");
  1320. } catch (ObjectDisposedException) {
  1321. } catch {
  1322. Assert.Fail ("BeginAcceptSocketAccClosed #2");
  1323. } finally {
  1324. sock.Close ();
  1325. }
  1326. }
  1327. static bool BCConnected = false;
  1328. static ManualResetEvent BCCalledBack = new ManualResetEvent (false);
  1329. private static void BCCallback (IAsyncResult asyncResult)
  1330. {
  1331. Socket sock = (Socket)asyncResult.AsyncState;
  1332. sock.EndConnect (asyncResult);
  1333. BCConnected = true;
  1334. BCCalledBack.Set ();
  1335. }
  1336. [Test]
  1337. public void BeginConnectAddressPort ()
  1338. {
  1339. Socket sock = new Socket (AddressFamily.InterNetwork,
  1340. SocketType.Stream,
  1341. ProtocolType.Tcp);
  1342. Socket listen = new Socket (AddressFamily.InterNetwork,
  1343. SocketType.Stream,
  1344. ProtocolType.Tcp);
  1345. IPAddress ip = IPAddress.Loopback;
  1346. IPEndPoint ep = new IPEndPoint (ip, 1244);
  1347. listen.Bind (ep);
  1348. listen.Listen (1);
  1349. BCCalledBack.Reset ();
  1350. BCConnected = false;
  1351. sock.BeginConnect (ip, 1244, BCCallback, sock);
  1352. if (BCCalledBack.WaitOne (2000, false) == false) {
  1353. Assert.Fail ("BeginConnectAddressPort wait timed out");
  1354. }
  1355. Assert.AreEqual (true, BCConnected, "BeginConnectAddressPort #1");
  1356. sock.Close ();
  1357. listen.Close ();
  1358. }
  1359. [Test]
  1360. public void BeginConnectAddressPortNull ()
  1361. {
  1362. Socket sock = new Socket (AddressFamily.InterNetwork,
  1363. SocketType.Stream,
  1364. ProtocolType.Tcp);
  1365. IPAddress ip = null;
  1366. try {
  1367. sock.BeginConnect (ip, 1244, BCCallback,
  1368. sock);
  1369. Assert.Fail ("BeginConnectAddressPortNull #1");
  1370. } catch (ArgumentNullException) {
  1371. } catch {
  1372. Assert.Fail ("BeginConnectAddressPortNull #2");
  1373. } finally {
  1374. sock.Close ();
  1375. }
  1376. }
  1377. [Test]
  1378. public void BeginConnectAddressPortListen ()
  1379. {
  1380. Socket sock = new Socket (AddressFamily.InterNetwork,
  1381. SocketType.Stream,
  1382. ProtocolType.Tcp);
  1383. IPAddress ip = IPAddress.Loopback;
  1384. IPEndPoint ep = new IPEndPoint (ip, 1245);
  1385. sock.Bind (ep);
  1386. sock.Listen (1);
  1387. try {
  1388. sock.BeginConnect (ip, 1245, BCCallback, sock);
  1389. Assert.Fail ("BeginConnectAddressPortListen #1");
  1390. } catch (InvalidOperationException) {
  1391. } catch {
  1392. Assert.Fail ("BeginConnectAddressPortListen #2");
  1393. } finally {
  1394. sock.Close ();
  1395. }
  1396. }
  1397. [Test]
  1398. [ExpectedException (typeof(ObjectDisposedException))]
  1399. public void BeginConnectAddressPortClosed ()
  1400. {
  1401. Socket sock = new Socket (AddressFamily.InterNetwork,
  1402. SocketType.Stream,
  1403. ProtocolType.Tcp);
  1404. IPAddress ip = IPAddress.Loopback;
  1405. sock.Close ();
  1406. sock.BeginConnect (ip, 1244, BCCallback, sock);
  1407. }
  1408. [Test]
  1409. [Category ("NotOnMac")]
  1410. /*
  1411. * This is not a Mono bug.
  1412. *
  1413. * By default, only 127.0.0.1 is enabled and you must explicitly
  1414. * enable additional addresses using 'sudo ifconfig lo0 alias 127.0.0.1'.
  1415. *
  1416. * I tested this on Mac OS 10.7.4; a 'ping 127.0.0.2' does not work
  1417. * until I add that alias.
  1418. *
  1419. */
  1420. public void BeginConnectMultiple ()
  1421. {
  1422. Socket sock = new Socket (AddressFamily.InterNetwork,
  1423. SocketType.Stream,
  1424. ProtocolType.Tcp);
  1425. Socket listen = new Socket (AddressFamily.InterNetwork,
  1426. SocketType.Stream,
  1427. ProtocolType.Tcp);
  1428. IPEndPoint ep = new IPEndPoint (IPAddress.Loopback,
  1429. 1246);
  1430. IPAddress[] ips = new IPAddress[4];
  1431. ips[0] = IPAddress.Parse ("127.0.0.4");
  1432. ips[1] = IPAddress.Parse ("127.0.0.3");
  1433. ips[2] = IPAddress.Parse ("127.0.0.2");
  1434. ips[3] = IPAddress.Parse ("127.0.0.1");
  1435. listen.Bind (ep);
  1436. listen.Listen (1);
  1437. BCCalledBack.Reset ();
  1438. BCConnected = false;
  1439. sock.BeginConnect (ips, 1246, BCCallback, sock);
  1440. /* Longer wait here, because the ms runtime
  1441. * takes a lot longer to not connect
  1442. */
  1443. if (BCCalledBack.WaitOne (10000, false) == false) {
  1444. Assert.Fail ("BeginConnectMultiple wait failed");
  1445. }
  1446. Assert.AreEqual (true, BCConnected, "BeginConnectMultiple #1");
  1447. Assert.AreEqual (AddressFamily.InterNetwork, sock.RemoteEndPoint.AddressFamily, "BeginConnectMultiple #2");
  1448. IPEndPoint remep = (IPEndPoint)sock.RemoteEndPoint;
  1449. Assert.AreEqual (IPAddress.Loopback, remep.Address, "BeginConnectMultiple #2");
  1450. sock.Close ();
  1451. listen.Close ();
  1452. }
  1453. [Test]
  1454. public void BeginConnectMultiple2 ()
  1455. {
  1456. Socket sock = new Socket (AddressFamily.InterNetwork,
  1457. SocketType.Stream,
  1458. ProtocolType.Tcp);
  1459. Socket listen = new Socket (AddressFamily.InterNetwork,
  1460. SocketType.Stream,
  1461. ProtocolType.Tcp);
  1462. // Need at least two addresses.
  1463. var ips = Dns.GetHostAddresses (string.Empty);
  1464. if (ips.Length < 1)
  1465. return;
  1466. var allIps = new IPAddress [ips.Length + 1];
  1467. allIps [0] = IPAddress.Loopback;
  1468. ips.CopyTo (allIps, 1);
  1469. /*
  1470. * Only bind to the loopback interface, so all the non-loopback
  1471. * IP addresses will fail. BeginConnect()/EndConnect() should
  1472. * succeed it it can connect to at least one of the requested
  1473. * addresses.
  1474. */
  1475. IPEndPoint ep = new IPEndPoint (IPAddress.Loopback, 1246);
  1476. listen.Bind (ep);
  1477. listen.Listen (1);
  1478. BCCalledBack.Reset ();
  1479. BCConnected = false;
  1480. sock.BeginConnect (allIps, 1246, BCCallback, sock);
  1481. /* Longer wait here, because the ms runtime
  1482. * takes a lot longer to not connect
  1483. */
  1484. if (BCCalledBack.WaitOne (10000, false) == false) {
  1485. Assert.Fail ("BeginConnectMultiple2 wait failed");
  1486. }
  1487. Assert.AreEqual (true, BCConnected, "BeginConnectMultiple2 #1");
  1488. Assert.AreEqual (AddressFamily.InterNetwork, sock.RemoteEndPoint.AddressFamily, "BeginConnectMultiple2 #2");
  1489. IPEndPoint remep = (IPEndPoint)sock.RemoteEndPoint;
  1490. Assert.AreEqual (IPAddress.Loopback, remep.Address, "BeginConnectMultiple2 #2");
  1491. sock.Close ();
  1492. listen.Close ();
  1493. }
  1494. [Test]
  1495. public void BeginConnectMultipleNull ()
  1496. {
  1497. Socket sock = new Socket (AddressFamily.InterNetwork,
  1498. SocketType.Stream,
  1499. ProtocolType.Tcp);
  1500. IPAddress[] ips = null;
  1501. try {
  1502. sock.BeginConnect (ips, 1246, BCCallback,
  1503. sock);
  1504. Assert.Fail ("BeginConnectMultipleNull #1");
  1505. } catch (ArgumentNullException) {
  1506. } catch {
  1507. Assert.Fail ("BeginConnectMultipleNull #2");
  1508. } finally {
  1509. sock.Close ();
  1510. }
  1511. }
  1512. [Test]
  1513. public void BeginConnectMultipleListen ()
  1514. {
  1515. Socket sock = new Socket (AddressFamily.InterNetwork,
  1516. SocketType.Stream,
  1517. ProtocolType.Tcp);
  1518. IPAddress[] ips = new IPAddress[4];
  1519. IPEndPoint ep = new IPEndPoint (IPAddress.Loopback,
  1520. 1247);
  1521. ips[0] = IPAddress.Parse ("127.0.0.4");
  1522. ips[1] = IPAddress.Parse ("127.0.0.3");
  1523. ips[2] = IPAddress.Parse ("127.0.0.2");
  1524. ips[3] = IPAddress.Parse ("127.0.0.1");
  1525. sock.Bind (ep);
  1526. sock.Listen (1);
  1527. try {
  1528. sock.BeginConnect (ips, 1247, BCCallback,
  1529. sock);
  1530. Assert.Fail ("BeginConnectMultipleListen #1");
  1531. } catch (InvalidOperationException) {
  1532. } catch {
  1533. Assert.Fail ("BeginConnectMultipleListen #2");
  1534. } finally {
  1535. sock.Close ();
  1536. }
  1537. }
  1538. [Test]
  1539. [ExpectedException (typeof(ObjectDisposedException))]
  1540. public void BeginConnectMultipleClosed ()
  1541. {
  1542. Socket sock = new Socket (AddressFamily.InterNetwork,
  1543. SocketType.Stream,
  1544. ProtocolType.Tcp);
  1545. IPAddress[] ips = new IPAddress[4];
  1546. ips[0] = IPAddress.Parse ("127.0.0.4");
  1547. ips[1] = IPAddress.Parse ("127.0.0.3");
  1548. ips[2] = IPAddress.Parse ("127.0.0.2");
  1549. ips[3] = IPAddress.Parse ("127.0.0.1");
  1550. sock.Close ();
  1551. sock.BeginConnect (ips, 1247, BCCallback, sock);
  1552. }
  1553. [Test]
  1554. public void BeginConnectHostPortNull ()
  1555. {
  1556. Socket sock = new Socket (AddressFamily.InterNetwork,
  1557. SocketType.Stream,
  1558. ProtocolType.Tcp);
  1559. try {
  1560. sock.BeginConnect ((string)null, 0,
  1561. BCCallback, sock);
  1562. Assert.Fail ("BeginConnectHostPort #1");
  1563. } catch (ArgumentNullException) {
  1564. } catch {
  1565. Assert.Fail ("BeginConnectHostPort #2");
  1566. } finally {
  1567. sock.Close ();
  1568. }
  1569. }
  1570. [Test]
  1571. public void BeginConnectHostPortListen ()
  1572. {
  1573. Socket sock = new Socket (AddressFamily.InterNetwork,
  1574. SocketType.Stream,
  1575. ProtocolType.Tcp);
  1576. IPAddress ip = IPAddress.Loopback;
  1577. IPEndPoint ep = new IPEndPoint (ip, 1248);
  1578. sock.Bind (ep);
  1579. sock.Listen (1);
  1580. try {
  1581. sock.BeginConnect ("localhost", 1248,
  1582. BCCallback, sock);
  1583. Assert.Fail ("BeginConnectHostPortListen #1");
  1584. } catch (InvalidOperationException) {
  1585. } catch {
  1586. Assert.Fail ("BeginConnectHostPortListen #2");
  1587. } finally {
  1588. sock.Close ();
  1589. }
  1590. }
  1591. [Test]
  1592. [Category ("NotWorking")] // Need to pick a non-IP AddressFamily that "works" on both mono and ms, this one only works on ms
  1593. public void BeginConnectHostPortNotIP ()
  1594. {
  1595. Socket sock = new Socket (AddressFamily.NetBios,
  1596. SocketType.Seqpacket,
  1597. ProtocolType.Unspecified);
  1598. try {
  1599. sock.BeginConnect ("localhost", 0, BCCallback,
  1600. sock);
  1601. Assert.Fail ("BeginConnectHostPortNotIP #1");
  1602. } catch (NotSupportedException) {
  1603. } catch {
  1604. Assert.Fail ("BeginConnectHostPortNotIP #2");
  1605. } finally {
  1606. sock.Close ();
  1607. }
  1608. }
  1609. [Test]
  1610. [ExpectedException (typeof(ObjectDisposedException))]
  1611. public void BeginConnectHostPortClosed ()
  1612. {
  1613. Socket sock = new Socket (AddressFamily.InterNetwork,
  1614. SocketType.Stream,
  1615. ProtocolType.Tcp);
  1616. sock.Close ();
  1617. sock.BeginConnect ("localhost", 0, BCCallback, sock);
  1618. }
  1619. static bool BDDisconnected = false;
  1620. static ManualResetEvent BDCalledBack = new ManualResetEvent (false);
  1621. private static void BDCallback (IAsyncResult asyncResult)
  1622. {
  1623. Socket sock = (Socket)asyncResult.AsyncState;
  1624. sock.EndDisconnect (asyncResult);
  1625. BDDisconnected = true;
  1626. BDCalledBack.Set ();
  1627. }
  1628. [Test]
  1629. [Category ("NotDotNet")] // "Needs XP or later"
  1630. public void BeginDisconnect ()
  1631. {
  1632. Socket sock = new Socket (AddressFamily.InterNetwork,
  1633. SocketType.Stream,
  1634. ProtocolType.Tcp);
  1635. Socket listen = new Socket (AddressFamily.InterNetwork,
  1636. SocketType.Stream,
  1637. ProtocolType.Tcp);
  1638. IPAddress ip = IPAddress.Loopback;
  1639. IPEndPoint ep = new IPEndPoint (ip, 1254);
  1640. listen.Bind (ep);
  1641. listen.Listen (1);
  1642. sock.Connect (ip, 1254);
  1643. Assert.AreEqual (true, sock.Connected, "BeginDisconnect #1");
  1644. sock.Shutdown (SocketShutdown.Both);
  1645. BDCalledBack.Reset ();
  1646. BDDisconnected = false;
  1647. sock.BeginDisconnect (false, BDCallback, sock);
  1648. if (BDCalledBack.WaitOne (2000, false) == false) {
  1649. Assert.Fail ("BeginDisconnect wait timed out");
  1650. }
  1651. Assert.AreEqual (true, BDDisconnected, "BeginDisconnect #2");
  1652. Assert.AreEqual (false, sock.Connected, "BeginDisconnect #3");
  1653. sock.Close ();
  1654. listen.Close ();
  1655. }
  1656. [Test]
  1657. public void BeginReceiveSocketError ()
  1658. {
  1659. }
  1660. [Test]
  1661. public void BeginReceiveGeneric ()
  1662. {
  1663. }
  1664. [Test]
  1665. public void BeginReceiveGenericSocketError ()
  1666. {
  1667. }
  1668. private static void BSCallback (IAsyncResult asyncResult)
  1669. {
  1670. Socket sock = (Socket)asyncResult.AsyncState;
  1671. sock.EndSend (asyncResult);
  1672. }
  1673. [Test]
  1674. public void BeginSendNotConnected ()
  1675. {
  1676. Socket sock = new Socket (AddressFamily.InterNetwork,
  1677. SocketType.Stream,
  1678. ProtocolType.Tcp);
  1679. byte[] send_bytes = new byte[] {10, 11, 12, 13};
  1680. try {
  1681. sock.BeginSend (send_bytes, 0,
  1682. send_bytes.Length,
  1683. SocketFlags.None, BSCallback,
  1684. sock);
  1685. Assert.Fail ("BeginSendNotConnected #1");
  1686. } catch (SocketException ex) {
  1687. Assert.AreEqual (10057, ex.ErrorCode, "BeginSendNotConnected #2");
  1688. } catch {
  1689. Assert.Fail ("BeginSendNotConnected #3");
  1690. } finally {
  1691. sock.Close ();
  1692. }
  1693. }
  1694. [Test]
  1695. public void BeginSendSocketError ()
  1696. {
  1697. }
  1698. [Test]
  1699. public void BeginSendGeneric ()
  1700. {
  1701. }
  1702. [Test]
  1703. public void BeginSendGenericSocketError ()
  1704. {
  1705. }
  1706. [Test]
  1707. public void BindTwice ()
  1708. {
  1709. Socket sock = new Socket (AddressFamily.InterNetwork,
  1710. SocketType.Stream,
  1711. ProtocolType.Tcp);
  1712. IPEndPoint ep1 = new IPEndPoint (IPAddress.Loopback,
  1713. 1256);
  1714. IPEndPoint ep2 = new IPEndPoint (IPAddress.Loopback,
  1715. 1257);
  1716. sock.Bind (ep1);
  1717. try {
  1718. sock.Bind (ep2);
  1719. Assert.Fail ("BindTwice #1");
  1720. } catch (SocketException ex) {
  1721. Assert.AreEqual (10022, ex.ErrorCode, "BindTwice #2");
  1722. } catch {
  1723. Assert.Fail ("BindTwice #3");
  1724. } finally {
  1725. sock.Close ();
  1726. }
  1727. }
  1728. [Test]
  1729. public void Close ()
  1730. {
  1731. Socket sock = new Socket (AddressFamily.InterNetwork,
  1732. SocketType.Stream,
  1733. ProtocolType.Tcp);
  1734. Socket listen = new Socket (AddressFamily.InterNetwork,
  1735. SocketType.Stream,
  1736. ProtocolType.Tcp);
  1737. IPEndPoint ep = new IPEndPoint (IPAddress.Loopback,
  1738. 1258);
  1739. listen.Bind (ep);
  1740. listen.Listen (1);
  1741. sock.Connect (ep);
  1742. Assert.AreEqual (true, sock.Connected, "Close #1");
  1743. sock.Close (2);
  1744. Thread.Sleep (3000);
  1745. Assert.AreEqual (false, sock.Connected, "Close #2");
  1746. listen.Close ();
  1747. }
  1748. [Test]
  1749. public void ConnectAddressPort ()
  1750. {
  1751. Socket sock = new Socket (AddressFamily.InterNetwork,
  1752. SocketType.Stream,
  1753. ProtocolType.Tcp);
  1754. Socket listen = new Socket (AddressFamily.InterNetwork,
  1755. SocketType.Stream,
  1756. ProtocolType.Tcp);
  1757. IPAddress ip = IPAddress.Loopback;
  1758. IPEndPoint ep = new IPEndPoint (ip, 1249);
  1759. listen.Bind (ep);
  1760. listen.Listen (1);
  1761. sock.Connect (ip, 1249);
  1762. Assert.AreEqual (true, sock.Connected, "ConnectAddressPort #1");
  1763. sock.Close ();
  1764. listen.Close ();
  1765. }
  1766. [Test]
  1767. public void ConnectAddressPortNull ()
  1768. {
  1769. Socket sock = new Socket (AddressFamily.InterNetwork,
  1770. SocketType.Stream,
  1771. ProtocolType.Tcp);
  1772. IPAddress ip = null;
  1773. try {
  1774. sock.Connect (ip, 1249);
  1775. Assert.Fail ("ConnectAddressPortNull #1");
  1776. } catch (ArgumentNullException) {
  1777. } catch {
  1778. Assert.Fail ("ConnectAddressPortNull #2");
  1779. } finally {
  1780. sock.Close ();
  1781. }
  1782. }
  1783. [Test]
  1784. public void ConnectAddressPortListen ()
  1785. {
  1786. Socket sock = new Socket (AddressFamily.InterNetwork,
  1787. SocketType.Stream,
  1788. ProtocolType.Tcp);
  1789. IPAddress ip = IPAddress.Loopback;
  1790. IPEndPoint ep = new IPEndPoint (ip, 1250);
  1791. sock.Bind (ep);
  1792. sock.Listen (1);
  1793. try {
  1794. sock.Connect (ip, 1250);
  1795. Assert.Fail ("ConnectAddressPortListen #1");
  1796. } catch (InvalidOperationException) {
  1797. } catch {
  1798. Assert.Fail ("ConnectAddressPortListen #2");
  1799. } finally {
  1800. sock.Close ();
  1801. }
  1802. }
  1803. [Test]
  1804. [ExpectedException (typeof(ObjectDisposedException))]
  1805. public void ConnectAddressPortClosed ()
  1806. {
  1807. Socket sock = new Socket (AddressFamily.InterNetwork,
  1808. SocketType.Stream,
  1809. ProtocolType.Tcp);
  1810. IPAddress ip = IPAddress.Loopback;
  1811. sock.Close ();
  1812. sock.Connect (ip, 1250);
  1813. }
  1814. [Test]
  1815. [Category ("NotOnMac")] // MacOSX trashes the fd after the failed connect attempt to 127.0.0.4
  1816. /*
  1817. * This is not a Mono bug.
  1818. *
  1819. * By default, only 127.0.0.1 is enabled and you must explicitly
  1820. * enable additional addresses using 'sudo ifconfig lo0 alias 127.0.0.1'.
  1821. *
  1822. * I tested this on Mac OS 10.7.4; a 'ping 127.0.0.2' does not work
  1823. * until I add that alias.
  1824. *
  1825. * However, after doing so, Mac OS treats these as separate addresses, ie. attempting
  1826. * to connect to 127.0.0.4 yields a connection refused.
  1827. *
  1828. * When using Connect(), the .NET runtime also throws an exception if connecting to
  1829. * any of the IP addresses fails. This is different with BeginConnect()/EndConnect()
  1830. * which succeeds when it can connect to at least one of the addresses.
  1831. *
  1832. */
  1833. public void ConnectMultiple ()
  1834. {
  1835. Socket sock = new Socket (AddressFamily.InterNetwork,
  1836. SocketType.Stream,
  1837. ProtocolType.Tcp);
  1838. Socket listen = new Socket (AddressFamily.InterNetwork,
  1839. SocketType.Stream,
  1840. ProtocolType.Tcp);
  1841. IPEndPoint ep = new IPEndPoint (IPAddress.Loopback,
  1842. 1251);
  1843. IPAddress[] ips = new IPAddress[4];
  1844. ips[0] = IPAddress.Parse ("127.0.0.4");
  1845. ips[1] = IPAddress.Parse ("127.0.0.3");
  1846. ips[2] = IPAddress.Parse ("127.0.0.2");
  1847. ips[3] = IPAddress.Parse ("127.0.0.1");
  1848. listen.Bind (ep);
  1849. listen.Listen (1);
  1850. sock.Connect (ips, 1251);
  1851. Assert.AreEqual (true, sock.Connected, "ConnectMultiple #1");
  1852. Assert.AreEqual (AddressFamily.InterNetwork, sock.RemoteEndPoint.AddressFamily, "ConnectMultiple #2");
  1853. IPEndPoint remep = (IPEndPoint)sock.RemoteEndPoint;
  1854. Assert.AreEqual (IPAddress.Loopback, remep.Address, "ConnectMultiple #2");
  1855. sock.Close ();
  1856. listen.Close ();
  1857. }
  1858. [Test]
  1859. public void ConnectMultiple2 ()
  1860. {
  1861. Socket sock = new Socket (AddressFamily.InterNetwork,
  1862. SocketType.Stream,
  1863. ProtocolType.Tcp);
  1864. Socket listen = new Socket (AddressFamily.InterNetwork,
  1865. SocketType.Stream,
  1866. ProtocolType.Tcp);
  1867. // Need at least two addresses.
  1868. var ips = Dns.GetHostAddresses (string.Empty);
  1869. if (ips.Length < 1)
  1870. return;
  1871. var allIps = new IPAddress [ips.Length + 1];
  1872. allIps [0] = IPAddress.Loopback;
  1873. ips.CopyTo (allIps, 1);
  1874. /*
  1875. * Bind to IPAddress.Any; Connect() will fail unless it can
  1876. * connect to all the addresses in allIps.
  1877. */
  1878. IPEndPoint ep = new IPEndPoint (IPAddress.Any, 1251);
  1879. listen.Bind (ep);
  1880. listen.Listen (1);
  1881. sock.Connect (allIps, 1251);
  1882. Assert.AreEqual (true, sock.Connected, "ConnectMultiple2 #1");
  1883. Assert.AreEqual (AddressFamily.InterNetwork, sock.RemoteEndPoint.AddressFamily, "ConnectMultiple2 #2");
  1884. IPEndPoint remep = (IPEndPoint)sock.RemoteEndPoint;
  1885. Assert.AreEqual (IPAddress.Loopback, remep.Address, "ConnectMultiple2 #3");
  1886. sock.Close ();
  1887. listen.Close ();
  1888. }
  1889. [Test]
  1890. public void ConnectMultipleNull ()
  1891. {
  1892. Socket sock = new Socket (AddressFamily.InterNetwork,
  1893. SocketType.Stream,
  1894. ProtocolType.Tcp);
  1895. IPAddress[] ips = null;
  1896. try {
  1897. sock.Connect (ips, 1251);
  1898. Assert.Fail ("ConnectMultipleNull #1");
  1899. } catch (ArgumentNullException) {
  1900. } catch {
  1901. Assert.Fail ("ConnectMultipleNull #2");
  1902. } finally {
  1903. sock.Close ();
  1904. }
  1905. }
  1906. [Test]
  1907. public void ConnectMultipleListen ()
  1908. {
  1909. Socket sock = new Socket (AddressFamily.InterNetwork,
  1910. SocketType.Stream,
  1911. ProtocolType.Tcp);
  1912. IPAddress[] ips = new IPAddress[4];
  1913. IPEndPoint ep = new IPEndPoint (IPAddress.Loopback,
  1914. 1252);
  1915. ips[0] = IPAddress.Parse ("127.0.0.4");
  1916. ips[1] = IPAddress.Parse ("127.0.0.3");
  1917. ips[2] = IPAddress.Parse ("127.0.0.2");
  1918. ips[3] = IPAddress.Parse ("127.0.0.1");
  1919. sock.Bind (ep);
  1920. sock.Listen (1);
  1921. try {
  1922. sock.Connect (ips, 1252);
  1923. Assert.Fail ("ConnectMultipleListen #1");
  1924. } catch (InvalidOperationException) {
  1925. } catch {
  1926. Assert.Fail ("ConnectMultipleListen #2");
  1927. } finally {
  1928. sock.Close ();
  1929. }
  1930. }
  1931. [Test]
  1932. [ExpectedException (typeof(ObjectDisposedException))]
  1933. public void ConnectMultipleClosed ()
  1934. {
  1935. Socket sock = new Socket (AddressFamily.InterNetwork,
  1936. SocketType.Stream,
  1937. ProtocolType.Tcp);
  1938. IPAddress[] ips = new IPAddress[4];
  1939. ips[0] = IPAddress.Parse ("127.0.0.4");
  1940. ips[1] = IPAddress.Parse ("127.0.0.3");
  1941. ips[2] = IPAddress.Parse ("127.0.0.2");
  1942. ips[3] = IPAddress.Parse ("127.0.0.1");
  1943. sock.Close ();
  1944. sock.Connect (ips, 1252);
  1945. }
  1946. [Test]
  1947. public void ConnectHostPortNull ()
  1948. {
  1949. Socket sock = new Socket (AddressFamily.InterNetwork,
  1950. SocketType.Stream,
  1951. ProtocolType.Tcp);
  1952. try {
  1953. sock.Connect ((string)null, 0);
  1954. Assert.Fail ("ConnectHostPort #1");
  1955. } catch (ArgumentNullException) {
  1956. } catch {
  1957. Assert.Fail ("ConnectHostPort #2");
  1958. } finally {
  1959. sock.Close ();
  1960. }
  1961. }
  1962. [Test]
  1963. public void ConnectHostPortListen ()
  1964. {
  1965. Socket sock = new Socket (AddressFamily.InterNetwork,
  1966. SocketType.Stream,
  1967. ProtocolType.Tcp);
  1968. IPAddress ip = IPAddress.Loopback;
  1969. IPEndPoint ep = new IPEndPoint (ip, 1253);
  1970. sock.Bind (ep);
  1971. sock.Listen (1);
  1972. try {
  1973. sock.Connect ("localhost", 1253);
  1974. Assert.Fail ("ConnectHostPortListen #1");
  1975. } catch (InvalidOperationException) {
  1976. } catch {
  1977. Assert.Fail ("ConnectHostPortListen #2");
  1978. } finally {
  1979. sock.Close ();
  1980. }
  1981. }
  1982. [Test]
  1983. [Category ("NotWorking")] // Need to pick a non-IP AddressFamily that "works" on both mono and ms, this one only works on ms
  1984. public void ConnectHostPortNotIP ()
  1985. {
  1986. Socket sock = new Socket (AddressFamily.NetBios,
  1987. SocketType.Seqpacket,
  1988. ProtocolType.Unspecified);
  1989. try {
  1990. sock.Connect ("localhost", 0);
  1991. Assert.Fail ("ConnectHostPortNotIP #1");
  1992. } catch (NotSupportedException) {
  1993. } catch {
  1994. Assert.Fail ("ConnectHostPortNotIP #2");
  1995. } finally {
  1996. sock.Close ();
  1997. }
  1998. }
  1999. [Test]
  2000. [ExpectedException (typeof(ObjectDisposedException))]
  2001. public void ConnectHostPortClosed ()
  2002. {
  2003. Socket sock = new Socket (AddressFamily.InterNetwork,
  2004. SocketType.Stream,
  2005. ProtocolType.Tcp);
  2006. sock.Close ();
  2007. sock.Connect ("localhost", 0);
  2008. }
  2009. [Test]
  2010. [Category ("NotDotNet")] // "Needs XP or later"
  2011. public void Disconnect ()
  2012. {
  2013. Socket sock = new Socket (AddressFamily.InterNetwork,
  2014. SocketType.Stream,
  2015. ProtocolType.Tcp);
  2016. Socket listen = new Socket (AddressFamily.InterNetwork,
  2017. SocketType.Stream,
  2018. ProtocolType.Tcp);
  2019. IPAddress ip = IPAddress.Loopback;
  2020. IPEndPoint ep = new IPEndPoint (ip, 1255);
  2021. listen.Bind (ep);
  2022. listen.Listen (1);
  2023. sock.Connect (ip, 1255);
  2024. Assert.AreEqual (true, sock.Connected, "Disconnect #1");
  2025. sock.Shutdown (SocketShutdown.Both);
  2026. sock.Disconnect (false);
  2027. Assert.AreEqual (false, sock.Connected, "BeginDisconnect #3");
  2028. sock.Close ();
  2029. listen.Close ();
  2030. }
  2031. [Test]
  2032. public void DuplicateAndClose ()
  2033. {
  2034. }
  2035. [Test]
  2036. public void IOControl ()
  2037. {
  2038. }
  2039. [Test]
  2040. public void ReceiveGeneric ()
  2041. {
  2042. int i;
  2043. IPEndPoint endpoint = new IPEndPoint(IPAddress.Loopback, 1258);
  2044. Socket listensock = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  2045. listensock.Bind (endpoint);
  2046. listensock.Listen(1);
  2047. Socket sendsock = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  2048. sendsock.Connect(endpoint);
  2049. Socket clientsock = listensock.Accept();
  2050. byte[] sendbuf = new byte[256];
  2051. for(i = 0; i < 256; i++) {
  2052. sendbuf[i] = (byte)i;
  2053. }
  2054. for (i = 4; i < 6; i++) {
  2055. Assert.AreEqual (sendbuf[i], (byte)i,
  2056. "#1/" + i.ToString());
  2057. }
  2058. SocketError err;
  2059. sendsock.Send (sendbuf, 0, 256, SocketFlags.None,
  2060. out err);
  2061. byte[] recvbuf = new byte[256];
  2062. List<ArraySegment<byte>> recvbuflist = new List<ArraySegment<byte>>(2);
  2063. recvbuflist.Add(new ArraySegment<byte>(recvbuf, 4, 2));
  2064. recvbuflist.Add(new ArraySegment<byte>(recvbuf, 20, 230));
  2065. clientsock.Receive (recvbuflist);
  2066. /* recvbuf should now hold the first 2 bytes
  2067. * of sendbuf from pos 4, and the next 230
  2068. * bytes of sendbuf from pos 20
  2069. */
  2070. for (i = 0; i < 2; i++) {
  2071. Assert.AreEqual (sendbuf[i], recvbuf[i + 4],
  2072. "#2/" + i.ToString());
  2073. }
  2074. for (i = 2; i < 232; i++) {
  2075. Assert.AreEqual (sendbuf[i], recvbuf[i + 18],
  2076. "#2/" + i.ToString());
  2077. }
  2078. sendsock.Close ();
  2079. clientsock.Close ();
  2080. listensock.Close ();
  2081. }
  2082. [Test]
  2083. public void SendGeneric ()
  2084. {
  2085. int i;
  2086. IPEndPoint endpoint = new IPEndPoint(IPAddress.Loopback, 1259);
  2087. Socket listensock = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  2088. listensock.Bind (endpoint);
  2089. listensock.Listen(1);
  2090. Socket sendsock = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  2091. sendsock.Connect(endpoint);
  2092. Socket clientsock = listensock.Accept();
  2093. byte[] sendbuf = new byte[256];
  2094. List<ArraySegment<byte>> sendbuflist = new List<ArraySegment<byte>>(2);
  2095. sendbuflist.Add(new ArraySegment<byte>(sendbuf, 4, 2));
  2096. sendbuflist.Add(new ArraySegment<byte>(sendbuf, 20, 230));
  2097. for(i = 0; i < 256; i++) {
  2098. sendbuf[i] = (byte)i;
  2099. }
  2100. for (i = 4; i < 6; i++) {
  2101. Assert.AreEqual (sendbuf[i], (byte)i,
  2102. "#1/" + i.ToString());
  2103. }
  2104. SocketError err;
  2105. sendsock.Send (sendbuflist, SocketFlags.None, out err);
  2106. byte[] recvbuf = new byte[256];
  2107. clientsock.Receive (recvbuf);
  2108. /* The first 2 bytes of recvbuf should now
  2109. * hold 2 bytes of sendbuf from pos 4, and the
  2110. * next 230 bytes of recvbuf should be sendbuf
  2111. * from pos 20
  2112. */
  2113. for (i = 0; i < 2; i++) {
  2114. Assert.AreEqual (recvbuf[i], sendbuf[i + 4],
  2115. "#2/" + i.ToString());
  2116. }
  2117. for (i = 2; i < 232; i++) {
  2118. Assert.AreEqual (recvbuf[i], sendbuf[i + 18],
  2119. "#2/" + i.ToString());
  2120. }
  2121. sendsock.Close ();
  2122. clientsock.Close ();
  2123. listensock.Close ();
  2124. }
  2125. [Test]
  2126. public void ListenNotBound ()
  2127. {
  2128. Socket sock = new Socket (AddressFamily.InterNetwork,
  2129. SocketType.Stream,
  2130. ProtocolType.Tcp);
  2131. try {
  2132. sock.Listen (1);
  2133. Assert.Fail ("ListenNotBound #1");
  2134. } catch (SocketException ex) {
  2135. Assert.AreEqual (10022, ex.ErrorCode, "ListenNotBound #2");
  2136. } catch {
  2137. Assert.Fail ("ListenNotBound #3");
  2138. } finally {
  2139. sock.Close ();
  2140. }
  2141. }
  2142. #endif
  2143. static Socket CWRSocket;
  2144. static bool CWRReceiving = true;
  2145. static ManualResetEvent CWRReady = new ManualResetEvent (false);
  2146. private static void CWRReceiveThread ()
  2147. {
  2148. byte[] buf = new byte[256];
  2149. try {
  2150. CWRSocket.Receive (buf);
  2151. } catch (SocketException) {
  2152. CWRReceiving = false;
  2153. }
  2154. CWRReady.Set ();
  2155. }
  2156. [Test]
  2157. public void CloseWhileReceiving ()
  2158. {
  2159. CWRSocket = new Socket (AddressFamily.InterNetwork,
  2160. SocketType.Dgram,
  2161. ProtocolType.Udp);
  2162. CWRSocket.Bind (new IPEndPoint (IPAddress.Loopback,
  2163. 1256));
  2164. Thread recv_thread = new Thread (new ThreadStart (CWRReceiveThread));
  2165. CWRReady.Reset ();
  2166. recv_thread.Start ();
  2167. Thread.Sleep (250); /* Wait for the thread to be already receiving */
  2168. CWRSocket.Close ();
  2169. if (CWRReady.WaitOne (1000, false) == false) {
  2170. Assert.Fail ("CloseWhileReceiving wait timed out");
  2171. }
  2172. Assert.IsFalse (CWRReceiving);
  2173. }
  2174. static bool RRCLastRead = false;
  2175. static ManualResetEvent RRCReady = new ManualResetEvent (false);
  2176. private static void RRCClientThread ()
  2177. {
  2178. byte[] bytes = new byte[8];
  2179. int readbyte;
  2180. Socket sock = new Socket (AddressFamily.InterNetwork,
  2181. SocketType.Stream,
  2182. ProtocolType.Tcp);
  2183. sock.Connect (new IPEndPoint (IPAddress.Loopback,
  2184. 1257));
  2185. NetworkStream stream = new NetworkStream (sock);
  2186. readbyte = stream.ReadByte ();
  2187. Assert.AreEqual (0, readbyte, "ReceiveRemoteClosed #1");
  2188. stream.Read (bytes, 0, 0);
  2189. readbyte = stream.ReadByte ();
  2190. Assert.AreEqual (0, readbyte, "ReceiveRemoteClosed #2");
  2191. stream.Read (bytes, 0, 0);
  2192. readbyte = stream.ReadByte ();
  2193. Assert.AreEqual (-1, readbyte, "ReceiveRemoteClosed #3");
  2194. sock.Close ();
  2195. RRCLastRead = true;
  2196. RRCReady.Set ();
  2197. }
  2198. [Test] // Receive (Byte [])
  2199. public void Receive1_Buffer_Null ()
  2200. {
  2201. Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
  2202. ProtocolType.Tcp);
  2203. try {
  2204. s.Receive ((byte []) null);
  2205. Assert.Fail ("#1");
  2206. } catch (ArgumentNullException ex) {
  2207. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  2208. Assert.IsNull (ex.InnerException, "#3");
  2209. Assert.IsNotNull (ex.Message, "#4");
  2210. Assert.AreEqual ("buffer", ex.ParamName, "#5");
  2211. } finally {
  2212. s.Close ();
  2213. }
  2214. }
  2215. [Test] // Receive (Byte [])
  2216. public void Receive1_Socket_Closed ()
  2217. {
  2218. Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
  2219. ProtocolType.Tcp);
  2220. s.Close ();
  2221. try {
  2222. s.Receive ((byte []) null);
  2223. Assert.Fail ("#1");
  2224. } catch (ObjectDisposedException ex) {
  2225. // Cannot access a disposed object
  2226. Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
  2227. Assert.IsNull (ex.InnerException, "#3");
  2228. Assert.IsNotNull (ex.Message, "#4");
  2229. Assert.AreEqual (typeof (Socket).FullName, ex.ObjectName, "#5");
  2230. }
  2231. }
  2232. [Test] // Receive (Byte [], SocketFlags)
  2233. public void Receive2_Buffer_Null ()
  2234. {
  2235. Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
  2236. ProtocolType.Tcp);
  2237. try {
  2238. s.Receive ((byte []) null, (SocketFlags) 666);
  2239. Assert.Fail ("#1");
  2240. } catch (ArgumentNullException ex) {
  2241. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  2242. Assert.IsNull (ex.InnerException, "#3");
  2243. Assert.IsNotNull (ex.Message, "#4");
  2244. Assert.AreEqual ("buffer", ex.ParamName, "#5");
  2245. } finally {
  2246. s.Close ();
  2247. }
  2248. }
  2249. [Test] // Receive (Byte [], SocketFlags)
  2250. public void Receive2_Socket_Closed ()
  2251. {
  2252. Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
  2253. ProtocolType.Tcp);
  2254. s.Close ();
  2255. try {
  2256. s.Receive ((byte []) null, (SocketFlags) 666);
  2257. Assert.Fail ("#1");
  2258. } catch (ObjectDisposedException ex) {
  2259. // Cannot access a disposed object
  2260. Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
  2261. Assert.IsNull (ex.InnerException, "#3");
  2262. Assert.IsNotNull (ex.Message, "#4");
  2263. Assert.AreEqual (typeof (Socket).FullName, ex.ObjectName, "#5");
  2264. }
  2265. }
  2266. [Test] // Receive (Byte [], Int32, SocketFlags)
  2267. public void Receive3_Buffer_Null ()
  2268. {
  2269. Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
  2270. ProtocolType.Tcp);
  2271. try {
  2272. s.Receive ((byte []) null, 0, (SocketFlags) 666);
  2273. Assert.Fail ("#1");
  2274. } catch (ArgumentNullException ex) {
  2275. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  2276. Assert.IsNull (ex.InnerException, "#3");
  2277. Assert.IsNotNull (ex.Message, "#4");
  2278. Assert.AreEqual ("buffer", ex.ParamName, "#5");
  2279. } finally {
  2280. s.Close ();
  2281. }
  2282. }
  2283. [Test] // Receive (Byte [], Int32, SocketFlags)
  2284. public void Receive3_Socket_Closed ()
  2285. {
  2286. Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
  2287. ProtocolType.Tcp);
  2288. s.Close ();
  2289. try {
  2290. s.Receive ((byte []) null, 0, (SocketFlags) 666);
  2291. Assert.Fail ("#1");
  2292. } catch (ObjectDisposedException ex) {
  2293. // Cannot access a disposed object
  2294. Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
  2295. Assert.IsNull (ex.InnerException, "#3");
  2296. Assert.IsNotNull (ex.Message, "#4");
  2297. Assert.AreEqual (typeof (Socket).FullName, ex.ObjectName, "#5");
  2298. }
  2299. }
  2300. [Test] // Receive (Byte [], Int32, Int32, SocketFlags)
  2301. public void Receive4_Buffer_Null ()
  2302. {
  2303. Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
  2304. ProtocolType.Tcp);
  2305. try {
  2306. s.Receive ((byte []) null, 0, 0, (SocketFlags) 666);
  2307. Assert.Fail ("#1");
  2308. } catch (ArgumentNullException ex) {
  2309. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  2310. Assert.IsNull (ex.InnerException, "#3");
  2311. Assert.IsNotNull (ex.Message, "#4");
  2312. Assert.AreEqual ("buffer", ex.ParamName, "#5");
  2313. } finally {
  2314. s.Close ();
  2315. }
  2316. }
  2317. [Test] // Receive (Byte [], Int32, Int32, SocketFlags)
  2318. public void Receive4_Socket_Closed ()
  2319. {
  2320. Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
  2321. ProtocolType.Tcp);
  2322. s.Close ();
  2323. try {
  2324. s.Receive ((byte []) null, 0, 0, (SocketFlags) 666);
  2325. Assert.Fail ("#1");
  2326. } catch (ObjectDisposedException ex) {
  2327. // Cannot access a disposed object
  2328. Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
  2329. Assert.IsNull (ex.InnerException, "#3");
  2330. Assert.IsNotNull (ex.Message, "#4");
  2331. Assert.AreEqual (typeof (Socket).FullName, ex.ObjectName, "#5");
  2332. }
  2333. }
  2334. #if NET_2_0
  2335. [Test] // Receive (Byte [], Int32, Int32, SocketFlags, out SocketError)
  2336. public void Receive5_Buffer_Null ()
  2337. {
  2338. Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
  2339. ProtocolType.Tcp);
  2340. SocketError error;
  2341. try {
  2342. s.Receive ((byte []) null, 0, 0, SocketFlags.None, out error);
  2343. Assert.Fail ("#1");
  2344. } catch (ArgumentNullException ex) {
  2345. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  2346. Assert.IsNull (ex.InnerException, "#3");
  2347. Assert.IsNotNull (ex.Message, "#4");
  2348. Assert.AreEqual ("buffer", ex.ParamName, "#5");
  2349. } finally {
  2350. s.Close ();
  2351. }
  2352. }
  2353. [Test] // Receive (Byte [], Int32, Int32, SocketFlags, out SocketError)
  2354. public void Receive5_Socket_Closed ()
  2355. {
  2356. Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
  2357. ProtocolType.Tcp);
  2358. s.Close ();
  2359. SocketError error;
  2360. try {
  2361. s.Receive ((byte []) null, 0, 0, SocketFlags.None, out error);
  2362. Assert.Fail ("#1");
  2363. } catch (ObjectDisposedException ex) {
  2364. // Cannot access a disposed object
  2365. Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
  2366. Assert.IsNull (ex.InnerException, "#3");
  2367. Assert.IsNotNull (ex.Message, "#4");
  2368. Assert.AreEqual (typeof (Socket).FullName, ex.ObjectName, "#5");
  2369. }
  2370. }
  2371. [Test] // Receive (IList<ArraySegment<Byte>>)
  2372. public void Receive6_Buffers_Null ()
  2373. {
  2374. Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
  2375. ProtocolType.Tcp);
  2376. try {
  2377. s.Receive ((IList<ArraySegment<byte>>) null);
  2378. Assert.Fail ("#1");
  2379. } catch (ArgumentNullException ex) {
  2380. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  2381. Assert.IsNull (ex.InnerException, "#3");
  2382. Assert.IsNotNull (ex.Message, "#4");
  2383. Assert.AreEqual ("buffers", ex.ParamName, "#5");
  2384. } finally {
  2385. s.Close ();
  2386. }
  2387. }
  2388. [Test] // Receive (IList<ArraySegment<Byte>>)
  2389. public void Receive6_Socket_Closed ()
  2390. {
  2391. Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
  2392. ProtocolType.Tcp);
  2393. s.Close ();
  2394. try {
  2395. s.Receive ((IList<ArraySegment<byte>>) null);
  2396. Assert.Fail ("#1");
  2397. } catch (ObjectDisposedException ex) {
  2398. // Cannot access a disposed object
  2399. Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
  2400. Assert.IsNull (ex.InnerException, "#3");
  2401. Assert.IsNotNull (ex.Message, "#4");
  2402. Assert.AreEqual (typeof (Socket).FullName, ex.ObjectName, "#5");
  2403. }
  2404. }
  2405. [Test] // Receive (IList<ArraySegment<Byte>>, SocketFlags)
  2406. public void Receive7_Buffers_Null ()
  2407. {
  2408. Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
  2409. ProtocolType.Tcp);
  2410. try {
  2411. s.Receive ((IList<ArraySegment<byte>>) null, (SocketFlags) 666);
  2412. Assert.Fail ("#1");
  2413. } catch (ArgumentNullException ex) {
  2414. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  2415. Assert.IsNull (ex.InnerException, "#3");
  2416. Assert.IsNotNull (ex.Message, "#4");
  2417. Assert.AreEqual ("buffers", ex.ParamName, "#5");
  2418. } finally {
  2419. s.Close ();
  2420. }
  2421. }
  2422. [Test] // Receive (IList<ArraySegment<Byte>>, SocketFlags)
  2423. public void Receive7_Socket_Closed ()
  2424. {
  2425. Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
  2426. ProtocolType.Tcp);
  2427. s.Close ();
  2428. try {
  2429. s.Receive ((IList<ArraySegment<byte>>) null, (SocketFlags) 666);
  2430. Assert.Fail ("#1");
  2431. } catch (ObjectDisposedException ex) {
  2432. // Cannot access a disposed object
  2433. Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
  2434. Assert.IsNull (ex.InnerException, "#3");
  2435. Assert.IsNotNull (ex.Message, "#4");
  2436. Assert.AreEqual (typeof (Socket).FullName, ex.ObjectName, "#5");
  2437. }
  2438. }
  2439. [Test] // Receive (IList<ArraySegment<Byte>>, SocketFlags, out SocketError)
  2440. public void Receive8_Buffers_Null ()
  2441. {
  2442. Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
  2443. ProtocolType.Tcp);
  2444. SocketError error;
  2445. try {
  2446. s.Receive ((IList<ArraySegment<byte>>) null, (SocketFlags) 666,
  2447. out error);
  2448. Assert.Fail ("#1");
  2449. } catch (ArgumentNullException ex) {
  2450. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  2451. Assert.IsNull (ex.InnerException, "#3");
  2452. Assert.IsNotNull (ex.Message, "#4");
  2453. Assert.AreEqual ("buffers", ex.ParamName, "#5");
  2454. } finally {
  2455. s.Close ();
  2456. }
  2457. }
  2458. [Test] // Receive (IList<ArraySegment<Byte>>, SocketFlags, out SocketError)
  2459. public void Receive8_Socket_Closed ()
  2460. {
  2461. Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
  2462. ProtocolType.Tcp);
  2463. s.Close ();
  2464. SocketError error;
  2465. try {
  2466. s.Receive ((IList<ArraySegment<byte>>) null, (SocketFlags) 666,
  2467. out error);
  2468. Assert.Fail ("#1");
  2469. } catch (ObjectDisposedException ex) {
  2470. // Cannot access a disposed object
  2471. Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
  2472. Assert.IsNull (ex.InnerException, "#3");
  2473. Assert.IsNotNull (ex.Message, "#4");
  2474. Assert.AreEqual (typeof (Socket).FullName, ex.ObjectName, "#5");
  2475. } finally {
  2476. s.Close ();
  2477. }
  2478. }
  2479. #endif
  2480. [Test] // ReceiveFrom (Byte [], ref EndPoint)
  2481. public void ReceiveFrom1_Buffer_Null ()
  2482. {
  2483. Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
  2484. ProtocolType.Tcp);
  2485. EndPoint remoteEP = new IPEndPoint (IPAddress.Loopback, 8001);
  2486. try {
  2487. s.ReceiveFrom ((Byte []) null, ref remoteEP);
  2488. Assert.Fail ("#1");
  2489. } catch (ArgumentNullException ex) {
  2490. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  2491. Assert.IsNull (ex.InnerException, "#3");
  2492. Assert.IsNotNull (ex.Message, "#4");
  2493. Assert.AreEqual ("buffer", ex.ParamName, "#5");
  2494. } finally {
  2495. s.Close ();
  2496. }
  2497. }
  2498. [Test] // ReceiveFrom (Byte [], ref EndPoint)
  2499. public void ReceiveFrom1_RemoteEP_Null ()
  2500. {
  2501. Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
  2502. ProtocolType.Tcp);
  2503. byte [] buffer = new byte [0];
  2504. EndPoint remoteEP = null;
  2505. try {
  2506. s.ReceiveFrom (buffer, ref remoteEP);
  2507. Assert.Fail ("#1");
  2508. } catch (ArgumentNullException ex) {
  2509. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  2510. Assert.IsNull (ex.InnerException, "#3");
  2511. Assert.IsNotNull (ex.Message, "#4");
  2512. Assert.AreEqual ("remoteEP", ex.ParamName, "#5");
  2513. } finally {
  2514. s.Close ();
  2515. }
  2516. }
  2517. [Test] // ReceiveFrom (Byte [], ref EndPoint)
  2518. public void ReceiveFrom1_Socket_Closed ()
  2519. {
  2520. Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
  2521. ProtocolType.Tcp);
  2522. s.Close ();
  2523. EndPoint remoteEP = new IPEndPoint (IPAddress.Loopback, 8001);
  2524. try {
  2525. s.ReceiveFrom ((Byte []) null, ref remoteEP);
  2526. Assert.Fail ("#1");
  2527. } catch (ObjectDisposedException ex) {
  2528. // Cannot access a disposed object
  2529. Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
  2530. Assert.IsNull (ex.InnerException, "#3");
  2531. Assert.IsNotNull (ex.Message, "#4");
  2532. Assert.AreEqual (typeof (Socket).FullName, ex.ObjectName, "#5");
  2533. }
  2534. }
  2535. [Test] // ReceiveFrom (Byte [], SocketFlags, ref EndPoint)
  2536. public void ReceiveFrom2_Buffer_Null ()
  2537. {
  2538. Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
  2539. ProtocolType.Tcp);
  2540. EndPoint remoteEP = new IPEndPoint (IPAddress.Loopback, 8001);
  2541. try {
  2542. s.ReceiveFrom ((Byte []) null, (SocketFlags) 666, ref remoteEP);
  2543. Assert.Fail ("#1");
  2544. } catch (ArgumentNullException ex) {
  2545. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  2546. Assert.IsNull (ex.InnerException, "#3");
  2547. Assert.IsNotNull (ex.Message, "#4");
  2548. Assert.AreEqual ("buffer", ex.ParamName, "#5");
  2549. } finally {
  2550. s.Close ();
  2551. }
  2552. }
  2553. [Test] // ReceiveFrom (Byte [], SocketFlags, ref EndPoint)
  2554. public void ReceiveFrom2_RemoteEP_Null ()
  2555. {
  2556. Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
  2557. ProtocolType.Tcp);
  2558. byte [] buffer = new byte [5];
  2559. EndPoint remoteEP = null;
  2560. try {
  2561. s.ReceiveFrom (buffer, (SocketFlags) 666, ref remoteEP);
  2562. Assert.Fail ("#1");
  2563. } catch (ArgumentNullException ex) {
  2564. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  2565. Assert.IsNull (ex.InnerException, "#3");
  2566. Assert.IsNotNull (ex.Message, "#4");
  2567. Assert.AreEqual ("remoteEP", ex.ParamName, "#5");
  2568. } finally {
  2569. s.Close ();
  2570. }
  2571. }
  2572. [Test] // ReceiveFrom (Byte [], SocketFlags, ref EndPoint)
  2573. public void ReceiveFrom2_Socket_Closed ()
  2574. {
  2575. Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
  2576. ProtocolType.Tcp);
  2577. s.Close ();
  2578. EndPoint remoteEP = new IPEndPoint (IPAddress.Loopback, 8001);
  2579. try {
  2580. s.ReceiveFrom ((Byte []) null, (SocketFlags) 666, ref remoteEP);
  2581. Assert.Fail ("#1");
  2582. } catch (ObjectDisposedException ex) {
  2583. // Cannot access a disposed object
  2584. Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
  2585. Assert.IsNull (ex.InnerException, "#3");
  2586. Assert.IsNotNull (ex.Message, "#4");
  2587. Assert.AreEqual (typeof (Socket).FullName, ex.ObjectName, "#5");
  2588. }
  2589. }
  2590. [Test] // ReceiveFrom (Byte [], Int32, SocketFlags, ref EndPoint)
  2591. public void ReceiveFrom3_Buffer_Null ()
  2592. {
  2593. Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
  2594. ProtocolType.Tcp);
  2595. EndPoint remoteEP = new IPEndPoint (IPAddress.Loopback, 8001);
  2596. try {
  2597. s.ReceiveFrom ((Byte []) null, -1, (SocketFlags) 666,
  2598. ref remoteEP);
  2599. Assert.Fail ("#1");
  2600. } catch (ArgumentNullException ex) {
  2601. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  2602. Assert.IsNull (ex.InnerException, "#3");
  2603. Assert.IsNotNull (ex.Message, "#4");
  2604. Assert.AreEqual ("buffer", ex.ParamName, "#5");
  2605. } finally {
  2606. s.Close ();
  2607. }
  2608. }
  2609. [Test] // ReceiveFrom (Byte [], Int32, SocketFlags, ref EndPoint)
  2610. public void ReceiveFrom3_RemoteEP_Null ()
  2611. {
  2612. Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
  2613. ProtocolType.Tcp);
  2614. byte [] buffer = new byte [5];
  2615. EndPoint remoteEP = null;
  2616. try {
  2617. s.ReceiveFrom (buffer, -1, (SocketFlags) 666, ref remoteEP);
  2618. Assert.Fail ("#1");
  2619. } catch (ArgumentNullException ex) {
  2620. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  2621. Assert.IsNull (ex.InnerException, "#3");
  2622. Assert.IsNotNull (ex.Message, "#4");
  2623. Assert.AreEqual ("remoteEP", ex.ParamName, "#5");
  2624. } finally {
  2625. s.Close ();
  2626. }
  2627. }
  2628. [Test] // ReceiveFrom (Byte [], Int32, SocketFlags, ref EndPoint)
  2629. public void ReceiveFrom3_Size_OutOfRange ()
  2630. {
  2631. Socket s;
  2632. byte [] buffer = new byte [5];
  2633. EndPoint remoteEP = new IPEndPoint (IPAddress.Loopback, 8001);
  2634. // size negative
  2635. s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
  2636. ProtocolType.Tcp);
  2637. try {
  2638. s.ReceiveFrom (buffer, -1, (SocketFlags) 666, ref remoteEP);
  2639. Assert.Fail ("#A1");
  2640. } catch (ArgumentOutOfRangeException ex) {
  2641. // Specified argument was out of the range of valid values
  2642. Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#A2");
  2643. Assert.IsNull (ex.InnerException, "#A3");
  2644. Assert.IsNotNull (ex.Message, "#A4");
  2645. Assert.AreEqual ("size", ex.ParamName, "#A5");
  2646. } finally {
  2647. s.Close ();
  2648. }
  2649. // size > buffer length
  2650. s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
  2651. ProtocolType.Tcp);
  2652. try {
  2653. s.ReceiveFrom (buffer, (buffer.Length + 1), (SocketFlags) 666,
  2654. ref remoteEP);
  2655. Assert.Fail ("#B1");
  2656. } catch (ArgumentOutOfRangeException ex) {
  2657. // Specified argument was out of the range of valid values
  2658. Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#B2");
  2659. Assert.IsNull (ex.InnerException, "#B3");
  2660. Assert.IsNotNull (ex.Message, "#B4");
  2661. Assert.AreEqual ("size", ex.ParamName, "#B5");
  2662. } finally {
  2663. s.Close ();
  2664. }
  2665. }
  2666. [Test] // ReceiveFrom (Byte [], Int32, SocketFlags, ref EndPoint)
  2667. public void ReceiveFrom3_Socket_Closed ()
  2668. {
  2669. Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
  2670. ProtocolType.Tcp);
  2671. s.Close ();
  2672. EndPoint remoteEP = new IPEndPoint (IPAddress.Loopback, 8001);
  2673. try {
  2674. s.ReceiveFrom ((Byte []) null, -1, (SocketFlags) 666,
  2675. ref remoteEP);
  2676. Assert.Fail ("#1");
  2677. } catch (ObjectDisposedException ex) {
  2678. // Cannot access a disposed object
  2679. Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
  2680. Assert.IsNull (ex.InnerException, "#3");
  2681. Assert.IsNotNull (ex.Message, "#4");
  2682. Assert.AreEqual (typeof (Socket).FullName, ex.ObjectName, "#5");
  2683. }
  2684. }
  2685. [Test] // ReceiveFrom (Byte [], Int32, Int32, SocketFlags, EndPoint)
  2686. public void ReceiveFrom4_Buffer_Null ()
  2687. {
  2688. Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
  2689. ProtocolType.Tcp);
  2690. EndPoint remoteEP = new IPEndPoint (IPAddress.Loopback, 8001);
  2691. try {
  2692. s.ReceiveFrom ((Byte []) null, -1, -1, (SocketFlags) 666,
  2693. ref remoteEP);
  2694. Assert.Fail ("#1");
  2695. } catch (ArgumentNullException ex) {
  2696. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  2697. Assert.IsNull (ex.InnerException, "#3");
  2698. Assert.IsNotNull (ex.Message, "#4");
  2699. Assert.AreEqual ("buffer", ex.ParamName, "#5");
  2700. }
  2701. }
  2702. [Test] // ReceiveFrom (Byte [], Int32, Int32, SocketFlags, EndPoint)
  2703. public void ReceiveFrom4_Offset_OutOfRange ()
  2704. {
  2705. Socket s;
  2706. byte [] buffer = new byte [5];
  2707. EndPoint remoteEP = new IPEndPoint (IPAddress.Loopback, 8001);
  2708. // offset negative
  2709. s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
  2710. ProtocolType.Tcp);
  2711. try {
  2712. s.ReceiveFrom (buffer, -1, 0, (SocketFlags) 666,
  2713. ref remoteEP);
  2714. Assert.Fail ("#A1");
  2715. } catch (ArgumentOutOfRangeException ex) {
  2716. // Specified argument was out of the range of valid values
  2717. Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#A2");
  2718. Assert.IsNull (ex.InnerException, "#A3");
  2719. Assert.IsNotNull (ex.Message, "#A4");
  2720. Assert.AreEqual ("offset", ex.ParamName, "#A5");
  2721. } finally {
  2722. s.Close ();
  2723. }
  2724. // offset > buffer length
  2725. s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
  2726. ProtocolType.Tcp);
  2727. try {
  2728. s.ReceiveFrom (buffer, (buffer.Length + 1), 0, (SocketFlags) 666,
  2729. ref remoteEP);
  2730. Assert.Fail ("#B1");
  2731. } catch (ArgumentOutOfRangeException ex) {
  2732. // Specified argument was out of the range of valid values
  2733. Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#B2");
  2734. Assert.IsNull (ex.InnerException, "#B3");
  2735. Assert.IsNotNull (ex.Message, "#B4");
  2736. Assert.AreEqual ("offset", ex.ParamName, "#B5");
  2737. } finally {
  2738. s.Close ();
  2739. }
  2740. }
  2741. [Test] // ReceiveFrom (Byte [], Int32, Int32, SocketFlags, ref IPEndPoint)
  2742. public void ReceiveFrom4_RemoteEP_Null ()
  2743. {
  2744. Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
  2745. ProtocolType.Tcp);
  2746. byte [] buffer = new byte [5];
  2747. EndPoint remoteEP = null;
  2748. try {
  2749. s.ReceiveFrom (buffer, -1, -1, (SocketFlags) 666, ref remoteEP);
  2750. Assert.Fail ("#1");
  2751. } catch (ArgumentNullException ex) {
  2752. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  2753. Assert.IsNull (ex.InnerException, "#3");
  2754. Assert.IsNotNull (ex.Message, "#4");
  2755. Assert.AreEqual ("remoteEP", ex.ParamName, "#5");
  2756. } finally {
  2757. s.Close ();
  2758. }
  2759. }
  2760. [Test] // ReceiveFrom (Byte [], Int32, Int32, SocketFlags, EndPoint)
  2761. public void ReceiveFrom4_Size_OutOfRange ()
  2762. {
  2763. Socket s;
  2764. byte [] buffer = new byte [5];
  2765. EndPoint remoteEP = new IPEndPoint (IPAddress.Loopback, 8001);
  2766. // size negative
  2767. s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
  2768. ProtocolType.Tcp);
  2769. try {
  2770. s.ReceiveFrom (buffer, 0, -1, (SocketFlags) 666,
  2771. ref remoteEP);
  2772. Assert.Fail ("#A1");
  2773. } catch (ArgumentOutOfRangeException ex) {
  2774. // Specified argument was out of the range of valid values
  2775. Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#A2");
  2776. Assert.IsNull (ex.InnerException, "#A3");
  2777. Assert.IsNotNull (ex.Message, "#A4");
  2778. Assert.AreEqual ("size", ex.ParamName, "#A5");
  2779. } finally {
  2780. s.Close ();
  2781. }
  2782. // size > buffer length
  2783. s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
  2784. ProtocolType.Tcp);
  2785. try {
  2786. s.ReceiveFrom (buffer, 0, (buffer.Length + 1), (SocketFlags) 666,
  2787. ref remoteEP);
  2788. Assert.Fail ("#B1");
  2789. } catch (ArgumentOutOfRangeException ex) {
  2790. // Specified argument was out of the range of valid values
  2791. Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#B2");
  2792. Assert.IsNull (ex.InnerException, "#B3");
  2793. Assert.IsNotNull (ex.Message, "#B4");
  2794. Assert.AreEqual ("size", ex.ParamName, "#B5");
  2795. } finally {
  2796. s.Close ();
  2797. }
  2798. // offset + size > buffer length
  2799. s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
  2800. ProtocolType.Tcp);
  2801. try {
  2802. s.ReceiveFrom (buffer, 2, 4, (SocketFlags) 666, ref remoteEP);
  2803. Assert.Fail ("#C1");
  2804. } catch (ArgumentOutOfRangeException ex) {
  2805. // Specified argument was out of the range of valid values
  2806. Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#C2");
  2807. Assert.IsNull (ex.InnerException, "#C3");
  2808. Assert.IsNotNull (ex.Message, "#C4");
  2809. Assert.AreEqual ("size", ex.ParamName, "#C5");
  2810. } finally {
  2811. s.Close ();
  2812. }
  2813. }
  2814. [Test] // ReceiveFrom (Byte [], Int32, Int32, SocketFlags, ref EndPoint)
  2815. public void ReceiveFrom4_Socket_Closed ()
  2816. {
  2817. Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
  2818. ProtocolType.Tcp);
  2819. s.Close ();
  2820. byte [] buffer = new byte [5];
  2821. EndPoint remoteEP = new IPEndPoint (IPAddress.Loopback, 8001);
  2822. try {
  2823. s.ReceiveFrom (buffer, -1, -1, (SocketFlags) 666,
  2824. ref remoteEP);
  2825. Assert.Fail ("#1");
  2826. } catch (ObjectDisposedException ex) {
  2827. // Cannot access a disposed object
  2828. Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
  2829. Assert.IsNull (ex.InnerException, "#3");
  2830. Assert.IsNotNull (ex.Message, "#4");
  2831. Assert.AreEqual (typeof (Socket).FullName, ex.ObjectName, "#5");
  2832. }
  2833. }
  2834. [Test]
  2835. public void ReceiveRemoteClosed ()
  2836. {
  2837. Socket sock = new Socket (AddressFamily.InterNetwork,
  2838. SocketType.Stream,
  2839. ProtocolType.Tcp);
  2840. sock.Bind (new IPEndPoint (IPAddress.Loopback, 1257));
  2841. sock.Listen (1);
  2842. RRCReady.Reset ();
  2843. Thread client_thread = new Thread (new ThreadStart (RRCClientThread));
  2844. client_thread.Start ();
  2845. Socket client = sock.Accept ();
  2846. NetworkStream stream = new NetworkStream (client);
  2847. stream.WriteByte (0x00);
  2848. stream.WriteByte (0x00);
  2849. client.Close ();
  2850. sock.Close ();
  2851. RRCReady.WaitOne (1000, false);
  2852. Assert.IsTrue (RRCLastRead);
  2853. }
  2854. //
  2855. // Test case for bug #471580
  2856. [Test]
  2857. public void UdpDoubleBind ()
  2858. {
  2859. Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
  2860. s.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, 1);
  2861. s.Bind (new IPEndPoint (IPAddress.Any, 12345));
  2862. Socket ss = new Socket (AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
  2863. ss.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, 1);
  2864. ss.Bind (new IPEndPoint (IPAddress.Any, 12345));
  2865. // If we make it this far, we succeeded.
  2866. ss.Close ();
  2867. s.Close ();
  2868. }
  2869. #if NET_2_0
  2870. [Test]
  2871. [Category ("NotOnMac")]
  2872. public void ConnectedProperty ()
  2873. {
  2874. TcpListener listener = new TcpListener (IPAddress.Loopback, 23456);
  2875. listener.Start();
  2876. Socket client = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  2877. client.Connect (IPAddress.Loopback, 23456);
  2878. Socket server = listener.AcceptSocket ();
  2879. try {
  2880. server.EndSend(server.BeginSend (new byte[10], 0, 10, SocketFlags.None, null, null));
  2881. client.Close ();
  2882. try {
  2883. server.EndReceive (server.BeginReceive (new byte[10], 0, 10, SocketFlags.None, null, null));
  2884. } catch {
  2885. }
  2886. Assert.IsTrue (!client.Connected);
  2887. Assert.IsTrue (!server.Connected);
  2888. } finally {
  2889. listener.Stop ();
  2890. client.Close ();
  2891. server.Close ();
  2892. }
  2893. }
  2894. #endif
  2895. [Test] // GetSocketOption (SocketOptionLevel, SocketOptionName)
  2896. public void GetSocketOption1_Socket_Closed ()
  2897. {
  2898. Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  2899. s.Close ();
  2900. try {
  2901. s.GetSocketOption (0, 0);
  2902. Assert.Fail ("#1");
  2903. } catch (ObjectDisposedException ex) {
  2904. // Cannot access a disposed object
  2905. Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
  2906. Assert.IsNull (ex.InnerException, "#3");
  2907. Assert.IsNotNull (ex.Message, "#4");
  2908. Assert.AreEqual (typeof (Socket).FullName, ex.ObjectName, "#5");
  2909. }
  2910. }
  2911. [Test] // GetSocketOption (SocketOptionLevel, SocketOptionName, Byte [])
  2912. public void GetSocketOption2_OptionValue_Null ()
  2913. {
  2914. Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  2915. try {
  2916. s.GetSocketOption (SocketOptionLevel.Socket, SocketOptionName.Linger,
  2917. (byte []) null);
  2918. Assert.Fail ("#1");
  2919. } catch (SocketException ex) {
  2920. // The system detected an invalid pointer address in attempting
  2921. // to use a pointer argument in a call
  2922. Assert.AreEqual (typeof (SocketException), ex.GetType (), "#2");
  2923. Assert.AreEqual (10014, ex.ErrorCode, "#3");
  2924. Assert.IsNull (ex.InnerException, "#4");
  2925. Assert.IsNotNull (ex.Message, "#5");
  2926. Assert.AreEqual (10014, ex.NativeErrorCode, "#6");
  2927. #if NET_2_0
  2928. Assert.AreEqual (SocketError.Fault, ex.SocketErrorCode, "#7");
  2929. #endif
  2930. }
  2931. }
  2932. [Test] // GetSocketOption (SocketOptionLevel, SocketOptionName, Byte [])
  2933. public void GetSocketOption2_Socket_Closed ()
  2934. {
  2935. Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  2936. s.Close ();
  2937. try {
  2938. s.GetSocketOption (0, 0, (byte []) null);
  2939. Assert.Fail ("#1");
  2940. } catch (ObjectDisposedException ex) {
  2941. // Cannot access a disposed object
  2942. Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
  2943. Assert.IsNull (ex.InnerException, "#3");
  2944. Assert.IsNotNull (ex.Message, "#4");
  2945. Assert.AreEqual (typeof (Socket).FullName, ex.ObjectName, "#5");
  2946. }
  2947. }
  2948. [Test] // GetSocketOption (SocketOptionLevel, SocketOptionName, Int32)
  2949. public void GetSocketOption3_Socket_Closed ()
  2950. {
  2951. Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  2952. s.Close ();
  2953. try {
  2954. s.GetSocketOption (0, 0, 0);
  2955. Assert.Fail ("#1");
  2956. } catch (ObjectDisposedException ex) {
  2957. // Cannot access a disposed object
  2958. Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
  2959. Assert.IsNull (ex.InnerException, "#3");
  2960. Assert.IsNotNull (ex.Message, "#4");
  2961. Assert.AreEqual (typeof (Socket).FullName, ex.ObjectName, "#5");
  2962. }
  2963. }
  2964. [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Byte [])
  2965. public void SetSocketOption1_DontLinger ()
  2966. {
  2967. using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) {
  2968. s.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.DontLinger,
  2969. new byte [] { 0x00 });
  2970. s.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.DontLinger,
  2971. new byte [] { 0x01 });
  2972. }
  2973. }
  2974. [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Byte [])
  2975. public void SetSocketOption1_DontLinger_Null ()
  2976. {
  2977. using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) {
  2978. try {
  2979. s.SetSocketOption (SocketOptionLevel.Socket,
  2980. SocketOptionName.DontLinger, (byte []) null);
  2981. Assert.Fail ("#1");
  2982. } catch (SocketException ex) {
  2983. // The system detected an invalid pointer address in attempting
  2984. // to use a pointer argument in a call
  2985. Assert.AreEqual (typeof (SocketException), ex.GetType (), "#2");
  2986. Assert.AreEqual (10014, ex.ErrorCode, "#3");
  2987. Assert.IsNull (ex.InnerException, "#4");
  2988. Assert.IsNotNull (ex.Message, "#5");
  2989. Assert.AreEqual (10014, ex.NativeErrorCode, "#6");
  2990. #if NET_2_0
  2991. Assert.AreEqual (SocketError.Fault, ex.SocketErrorCode, "#7");
  2992. #endif
  2993. }
  2994. }
  2995. }
  2996. [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Byte [])
  2997. public void SetSocketOption1_Linger_Null ()
  2998. {
  2999. using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) {
  3000. try {
  3001. s.SetSocketOption (SocketOptionLevel.Socket,
  3002. SocketOptionName.DontLinger, (byte []) null);
  3003. Assert.Fail ("#1");
  3004. } catch (SocketException ex) {
  3005. // The system detected an invalid pointer address in attempting
  3006. // to use a pointer argument in a call
  3007. Assert.AreEqual (typeof (SocketException), ex.GetType (), "#2");
  3008. Assert.AreEqual (10014, ex.ErrorCode, "#3");
  3009. Assert.IsNull (ex.InnerException, "#4");
  3010. Assert.IsNotNull (ex.Message, "#5");
  3011. Assert.AreEqual (10014, ex.NativeErrorCode, "#6");
  3012. #if NET_2_0
  3013. Assert.AreEqual (SocketError.Fault, ex.SocketErrorCode, "#7");
  3014. #endif
  3015. }
  3016. }
  3017. }
  3018. [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Byte [])
  3019. public void SetSocketOption1_Socket_Close ()
  3020. {
  3021. Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  3022. s.Close ();
  3023. try {
  3024. s.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.DontLinger,
  3025. new byte [] { 0x00 });
  3026. Assert.Fail ("#1");
  3027. } catch (ObjectDisposedException ex) {
  3028. Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
  3029. Assert.IsNull (ex.InnerException, "#3");
  3030. Assert.IsNotNull (ex.Message, "#4");
  3031. Assert.AreEqual (typeof (Socket).FullName, ex.ObjectName, "#5");
  3032. }
  3033. }
  3034. [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Int32)
  3035. public void SetSocketOption2_DontLinger ()
  3036. {
  3037. using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) {
  3038. s.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.DontLinger, 0);
  3039. s.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.DontLinger, 5);
  3040. }
  3041. }
  3042. [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Int32)
  3043. [Category ("NotWorking")]
  3044. public void SetSocketOption2_Linger ()
  3045. {
  3046. using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) {
  3047. s.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.Linger, 0);
  3048. s.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.Linger, 5);
  3049. }
  3050. }
  3051. [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Int32)
  3052. public void SetSocketOption2_Socket_Closed ()
  3053. {
  3054. Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  3055. s.Close ();
  3056. try {
  3057. s.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.DontLinger, 0);
  3058. Assert.Fail ("#1");
  3059. } catch (ObjectDisposedException ex) {
  3060. // Cannot access a disposed object
  3061. Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
  3062. Assert.IsNull (ex.InnerException, "#3");
  3063. Assert.IsNotNull (ex.Message, "#4");
  3064. Assert.AreEqual (typeof (Socket).FullName, ex.ObjectName, "#5");
  3065. }
  3066. }
  3067. [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Object)
  3068. public void SetSocketOption3_AddMembershipIPv4_IPv6MulticastOption ()
  3069. {
  3070. IPAddress mcast_addr = IPAddress.Parse ("239.255.255.250");
  3071. using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp)) {
  3072. s.Bind (new IPEndPoint (IPAddress.Any, 1901));
  3073. try {
  3074. s.SetSocketOption (SocketOptionLevel.IP, SocketOptionName.AddMembership,
  3075. new IPv6MulticastOption (mcast_addr));
  3076. Assert.Fail ("#1");
  3077. } catch (ArgumentException ex) {
  3078. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
  3079. Assert.IsNull (ex.InnerException, "#3");
  3080. Assert.IsNotNull (ex.Message, "#4");
  3081. #if NET_2_0
  3082. // The specified value is not a valid 'MulticastOption'
  3083. Assert.IsTrue (ex.Message.IndexOf ("'MulticastOption'") != -1, "#5:" + ex.Message);
  3084. Assert.AreEqual ("optionValue", ex.ParamName, "#6");
  3085. #else
  3086. Assert.AreEqual ("optionValue", ex.Message, "#5");
  3087. Assert.IsNull (ex.ParamName, "#6");
  3088. #endif
  3089. }
  3090. }
  3091. }
  3092. [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Object)
  3093. public void SetSocketOption3_AddMembershipIPv4_MulticastOption ()
  3094. {
  3095. IPAddress mcast_addr = IPAddress.Parse ("239.255.255.250");
  3096. using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp)) {
  3097. s.Bind (new IPEndPoint (IPAddress.Any, 1901));
  3098. s.SetSocketOption (SocketOptionLevel.IP, SocketOptionName.AddMembership,
  3099. new MulticastOption (mcast_addr));
  3100. }
  3101. }
  3102. [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Object)
  3103. [Category ("NotWorking")]
  3104. public void SetSocketOption3_AddMembershipIPv4_Socket_NotBound ()
  3105. {
  3106. IPAddress mcast_addr = IPAddress.Parse ("239.255.255.250");
  3107. Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
  3108. try {
  3109. s.SetSocketOption (SocketOptionLevel.IP, SocketOptionName.AddMembership,
  3110. new MulticastOption (mcast_addr));
  3111. Assert.Fail ("#1");
  3112. } catch (SocketException ex) {
  3113. // An invalid argument was supplied
  3114. Assert.AreEqual (typeof (SocketException), ex.GetType (), "#2");
  3115. Assert.AreEqual (10022, ex.ErrorCode, "#3");
  3116. Assert.IsNull (ex.InnerException, "#4");
  3117. Assert.IsNotNull (ex.Message, "#5");
  3118. Assert.AreEqual (10022, ex.NativeErrorCode, "#6");
  3119. #if NET_2_0
  3120. Assert.AreEqual (SocketError.InvalidArgument, ex.SocketErrorCode, "#7");
  3121. #endif
  3122. } finally {
  3123. s.Close ();
  3124. }
  3125. }
  3126. [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Object)
  3127. public void SetSocketOption3_AddMembershipIPv6_IPv6MulticastOption ()
  3128. {
  3129. #if NET_2_0
  3130. if (!Socket.OSSupportsIPv6)
  3131. #else
  3132. if (!Socket.SupportsIPv6)
  3133. #endif
  3134. Assert.Ignore ("IPv6 not enabled.");
  3135. IPAddress mcast_addr = IPAddress.Parse ("ff02::1");
  3136. using (Socket s = new Socket (AddressFamily.InterNetworkV6, SocketType.Dgram, ProtocolType.Udp)) {
  3137. s.Bind (new IPEndPoint (IPAddress.IPv6Any, 1902));
  3138. s.SetSocketOption (SocketOptionLevel.IPv6, SocketOptionName.AddMembership,
  3139. new IPv6MulticastOption (mcast_addr));
  3140. }
  3141. }
  3142. [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Object)
  3143. public void SetSocketOption3_AddMembershipIPv6_MulticastOption ()
  3144. {
  3145. #if NET_2_0
  3146. if (!Socket.OSSupportsIPv6)
  3147. #else
  3148. if (!Socket.SupportsIPv6)
  3149. #endif
  3150. Assert.Ignore ("IPv6 not enabled.");
  3151. IPAddress mcast_addr = IPAddress.Parse ("ff02::1");
  3152. using (Socket s = new Socket (AddressFamily.InterNetworkV6, SocketType.Dgram, ProtocolType.Udp)) {
  3153. s.Bind (new IPEndPoint (IPAddress.IPv6Any, 1902));
  3154. try {
  3155. s.SetSocketOption (SocketOptionLevel.IPv6, SocketOptionName.AddMembership,
  3156. new MulticastOption (mcast_addr));
  3157. Assert.Fail ("#1");
  3158. } catch (ArgumentException ex) {
  3159. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
  3160. Assert.IsNull (ex.InnerException, "#3");
  3161. Assert.IsNotNull (ex.Message, "#4");
  3162. #if NET_2_0
  3163. // The specified value is not a valid 'IPv6MulticastOption'
  3164. Assert.IsTrue (ex.Message.IndexOf ("'IPv6MulticastOption'") != -1, "#5:" + ex.Message);
  3165. Assert.AreEqual ("optionValue", ex.ParamName, "#6");
  3166. #else
  3167. Assert.AreEqual ("optionValue", ex.Message, "#5");
  3168. Assert.IsNull (ex.ParamName, "#6");
  3169. #endif
  3170. }
  3171. }
  3172. }
  3173. [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Object)
  3174. [Category ("NotWorking")]
  3175. public void SetSocketOption3_AddMembershipIPv6_Socket_NotBound ()
  3176. {
  3177. IPAddress mcast_addr = IPAddress.Parse ("ff02::1");
  3178. Socket s = new Socket (AddressFamily.InterNetworkV6, SocketType.Dgram, ProtocolType.Udp);
  3179. try {
  3180. s.SetSocketOption (SocketOptionLevel.IPv6, SocketOptionName.AddMembership,
  3181. new IPv6MulticastOption (mcast_addr));
  3182. Assert.Fail ("#1");
  3183. } catch (SocketException ex) {
  3184. // An invalid argument was supplied
  3185. Assert.AreEqual (typeof (SocketException), ex.GetType (), "#2");
  3186. Assert.AreEqual (10022, ex.ErrorCode, "#3");
  3187. Assert.IsNull (ex.InnerException, "#4");
  3188. Assert.IsNotNull (ex.Message, "#5");
  3189. Assert.AreEqual (10022, ex.NativeErrorCode, "#6");
  3190. #if NET_2_0
  3191. Assert.AreEqual (SocketError.InvalidArgument, ex.SocketErrorCode, "#7");
  3192. #endif
  3193. } finally {
  3194. s.Close ();
  3195. }
  3196. }
  3197. [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Object)
  3198. public void SetSocketOption3_DontLinger_Boolean ()
  3199. {
  3200. using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) {
  3201. try {
  3202. s.SetSocketOption (SocketOptionLevel.Socket,
  3203. SocketOptionName.DontLinger, (object) false);
  3204. Assert.Fail ("#1");
  3205. } catch (ArgumentException ex) {
  3206. // The specified value is not valid
  3207. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
  3208. Assert.IsNull (ex.InnerException, "#3");
  3209. #if NET_2_0
  3210. Assert.IsNotNull (ex.Message, "#4");
  3211. Assert.AreEqual ("optionValue", ex.ParamName, "#5");
  3212. #else
  3213. Assert.AreEqual ("optionValue", ex.Message, "#4");
  3214. Assert.IsNull (ex.ParamName, "#5");
  3215. #endif
  3216. }
  3217. }
  3218. }
  3219. [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Object)
  3220. public void SetSocketOption3_DontLinger_Int32 ()
  3221. {
  3222. using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) {
  3223. try {
  3224. s.SetSocketOption (SocketOptionLevel.Socket,
  3225. SocketOptionName.DontLinger, (object) 0);
  3226. Assert.Fail ("#1");
  3227. } catch (ArgumentException ex) {
  3228. // The specified value is not valid
  3229. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
  3230. Assert.IsNull (ex.InnerException, "#3");
  3231. #if NET_2_0
  3232. Assert.IsNotNull (ex.Message, "#4");
  3233. Assert.AreEqual ("optionValue", ex.ParamName, "#5");
  3234. #else
  3235. Assert.AreEqual ("optionValue", ex.Message, "#4");
  3236. Assert.IsNull (ex.ParamName, "#5");
  3237. #endif
  3238. }
  3239. }
  3240. }
  3241. [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Object)
  3242. public void SetSocketOption3_DontLinger_LingerOption ()
  3243. {
  3244. using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) {
  3245. try {
  3246. s.SetSocketOption (SocketOptionLevel.Socket,
  3247. SocketOptionName.DontLinger, new LingerOption (true, 1000));
  3248. Assert.Fail ("#1");
  3249. } catch (ArgumentException ex) {
  3250. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
  3251. Assert.IsNull (ex.InnerException, "#3");
  3252. #if NET_2_0
  3253. // The specified value is not valid
  3254. Assert.IsNotNull (ex.Message, "#4");
  3255. Assert.AreEqual ("optionValue", ex.ParamName, "#5");
  3256. #else
  3257. Assert.AreEqual ("optionValue", ex.Message, "#4");
  3258. Assert.IsNull (ex.ParamName, "#5");
  3259. #endif
  3260. }
  3261. }
  3262. }
  3263. [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Object)
  3264. public void SetSocketOption3_Linger_Boolean ()
  3265. {
  3266. using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) {
  3267. try {
  3268. s.SetSocketOption (SocketOptionLevel.Socket,
  3269. SocketOptionName.Linger, (object) false);
  3270. Assert.Fail ("#1");
  3271. } catch (ArgumentException ex) {
  3272. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
  3273. Assert.IsNull (ex.InnerException, "#3");
  3274. #if NET_2_0
  3275. // The specified value is not valid
  3276. Assert.IsNotNull (ex.Message, "#4");
  3277. Assert.AreEqual ("optionValue", ex.ParamName, "#5");
  3278. #else
  3279. Assert.AreEqual ("optionValue", ex.Message, "#4");
  3280. Assert.IsNull (ex.ParamName, "#5");
  3281. #endif
  3282. }
  3283. }
  3284. }
  3285. [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Object)
  3286. public void SetSocketOption3_Linger_Int32 ()
  3287. {
  3288. using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) {
  3289. try {
  3290. s.SetSocketOption (SocketOptionLevel.Socket,
  3291. SocketOptionName.Linger, (object) 0);
  3292. Assert.Fail ("#1");
  3293. } catch (ArgumentException ex) {
  3294. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
  3295. Assert.IsNull (ex.InnerException, "#3");
  3296. #if NET_2_0
  3297. // The specified value is not valid
  3298. Assert.IsNotNull (ex.Message, "#4");
  3299. Assert.AreEqual ("optionValue", ex.ParamName, "#5");
  3300. #else
  3301. Assert.AreEqual ("optionValue", ex.Message, "#4");
  3302. Assert.IsNull (ex.ParamName, "#5");
  3303. #endif
  3304. }
  3305. }
  3306. }
  3307. [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Object)
  3308. public void SetSocketOption3_Linger_LingerOption ()
  3309. {
  3310. using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) {
  3311. s.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.Linger,
  3312. new LingerOption (false, 0));
  3313. s.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.Linger,
  3314. new LingerOption (true, 0));
  3315. s.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.Linger,
  3316. new LingerOption (false, 1000));
  3317. s.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.Linger,
  3318. new LingerOption (true, 1000));
  3319. }
  3320. }
  3321. [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Object)
  3322. public void SetSocketOption3_DropMembershipIPv4_IPv6MulticastOption ()
  3323. {
  3324. IPAddress mcast_addr = IPAddress.Parse ("239.255.255.250");
  3325. using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp)) {
  3326. s.Bind (new IPEndPoint (IPAddress.Any, 1901));
  3327. s.SetSocketOption (SocketOptionLevel.IP, SocketOptionName.AddMembership,
  3328. new MulticastOption (mcast_addr));
  3329. try {
  3330. s.SetSocketOption (SocketOptionLevel.IP, SocketOptionName.DropMembership,
  3331. new IPv6MulticastOption (mcast_addr));
  3332. Assert.Fail ("#1");
  3333. } catch (ArgumentException ex) {
  3334. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
  3335. Assert.IsNull (ex.InnerException, "#3");
  3336. Assert.IsNotNull (ex.Message, "#4");
  3337. #if NET_2_0
  3338. // The specified value is not a valid 'MulticastOption'
  3339. Assert.IsTrue (ex.Message.IndexOf ("'MulticastOption'") != -1, "#5:" + ex.Message);
  3340. Assert.AreEqual ("optionValue", ex.ParamName, "#6");
  3341. #else
  3342. Assert.AreEqual ("optionValue", ex.Message, "#5");
  3343. Assert.IsNull (ex.ParamName, "#6");
  3344. #endif
  3345. }
  3346. }
  3347. }
  3348. [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Object)
  3349. public void SetSocketOption3_DropMembershipIPv4_MulticastOption ()
  3350. {
  3351. IPAddress mcast_addr = IPAddress.Parse ("239.255.255.250");
  3352. using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp)) {
  3353. MulticastOption option = new MulticastOption (mcast_addr);
  3354. s.Bind (new IPEndPoint (IPAddress.Any, 1901));
  3355. s.SetSocketOption (SocketOptionLevel.IP, SocketOptionName.AddMembership,
  3356. option);
  3357. s.SetSocketOption (SocketOptionLevel.IP, SocketOptionName.DropMembership,
  3358. option);
  3359. }
  3360. }
  3361. [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Object)
  3362. [Category ("NotWorking")]
  3363. public void SetSocketOption3_DropMembershipIPv4_Socket_NotBound ()
  3364. {
  3365. IPAddress mcast_addr = IPAddress.Parse ("239.255.255.250");
  3366. Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
  3367. try {
  3368. s.SetSocketOption (SocketOptionLevel.IP, SocketOptionName.DropMembership,
  3369. new MulticastOption (mcast_addr));
  3370. Assert.Fail ("#1");
  3371. } catch (SocketException ex) {
  3372. // An invalid argument was supplied
  3373. Assert.AreEqual (typeof (SocketException), ex.GetType (), "#2");
  3374. Assert.AreEqual (10022, ex.ErrorCode, "#3");
  3375. Assert.IsNull (ex.InnerException, "#4");
  3376. Assert.IsNotNull (ex.Message, "#5");
  3377. Assert.AreEqual (10022, ex.NativeErrorCode, "#6");
  3378. #if NET_2_0
  3379. Assert.AreEqual (SocketError.InvalidArgument, ex.SocketErrorCode, "#7");
  3380. #endif
  3381. } finally {
  3382. s.Close ();
  3383. }
  3384. }
  3385. [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Object)
  3386. public void SetSocketOption3_DropMembershipIPv6_IPv6MulticastOption ()
  3387. {
  3388. #if NET_2_0
  3389. if (!Socket.OSSupportsIPv6)
  3390. #else
  3391. if (!Socket.SupportsIPv6)
  3392. #endif
  3393. Assert.Ignore ("IPv6 not enabled.");
  3394. using (Socket s = new Socket (AddressFamily.InterNetworkV6, SocketType.Dgram, ProtocolType.Udp)) {
  3395. IPv6MulticastOption option = new IPv6MulticastOption (
  3396. IPAddress.Parse ("ff02::1"));
  3397. s.Bind (new IPEndPoint (IPAddress.IPv6Any, 1902));
  3398. s.SetSocketOption (SocketOptionLevel.IPv6, SocketOptionName.AddMembership,
  3399. option);
  3400. s.SetSocketOption (SocketOptionLevel.IPv6, SocketOptionName.DropMembership,
  3401. option);
  3402. }
  3403. }
  3404. [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Object)
  3405. public void SetSocketOption3_DropMembershipIPv6_MulticastOption ()
  3406. {
  3407. #if NET_2_0
  3408. if (!Socket.OSSupportsIPv6)
  3409. #else
  3410. if (!Socket.SupportsIPv6)
  3411. #endif
  3412. Assert.Ignore ("IPv6 not enabled.");
  3413. IPAddress mcast_addr = IPAddress.Parse ("ff02::1");
  3414. using (Socket s = new Socket (AddressFamily.InterNetworkV6, SocketType.Dgram, ProtocolType.Udp)) {
  3415. s.Bind (new IPEndPoint (IPAddress.IPv6Any, 1902));
  3416. s.SetSocketOption (SocketOptionLevel.IPv6, SocketOptionName.AddMembership,
  3417. new IPv6MulticastOption (mcast_addr));
  3418. try {
  3419. s.SetSocketOption (SocketOptionLevel.IPv6, SocketOptionName.DropMembership,
  3420. new MulticastOption (mcast_addr));
  3421. Assert.Fail ("#1");
  3422. } catch (ArgumentException ex) {
  3423. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
  3424. Assert.IsNull (ex.InnerException, "#3");
  3425. Assert.IsNotNull (ex.Message, "#4");
  3426. #if NET_2_0
  3427. // The specified value is not a valid 'IPv6MulticastOption'
  3428. Assert.IsTrue (ex.Message.IndexOf ("'IPv6MulticastOption'") != -1, "#5:" + ex.Message);
  3429. Assert.AreEqual ("optionValue", ex.ParamName, "#6");
  3430. #else
  3431. Assert.AreEqual ("optionValue", ex.Message, "#5");
  3432. Assert.IsNull (ex.ParamName, "#6");
  3433. #endif
  3434. }
  3435. }
  3436. }
  3437. [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Object)
  3438. [Category ("NotWorking")]
  3439. public void SetSocketOption3_DropMembershipIPv6_Socket_NotBound ()
  3440. {
  3441. IPAddress mcast_addr = IPAddress.Parse ("ff02::1");
  3442. Socket s = new Socket (AddressFamily.InterNetworkV6, SocketType.Dgram, ProtocolType.Udp);
  3443. try {
  3444. s.SetSocketOption (SocketOptionLevel.IPv6, SocketOptionName.DropMembership,
  3445. new IPv6MulticastOption (mcast_addr));
  3446. Assert.Fail ("#1");
  3447. } catch (SocketException ex) {
  3448. // An invalid argument was supplied
  3449. Assert.AreEqual (typeof (SocketException), ex.GetType (), "#2");
  3450. Assert.AreEqual (10022, ex.ErrorCode, "#3");
  3451. Assert.IsNull (ex.InnerException, "#4");
  3452. Assert.IsNotNull (ex.Message, "#5");
  3453. Assert.AreEqual (10022, ex.NativeErrorCode, "#6");
  3454. #if NET_2_0
  3455. Assert.AreEqual (SocketError.InvalidArgument, ex.SocketErrorCode, "#7");
  3456. #endif
  3457. } finally {
  3458. s.Close ();
  3459. }
  3460. }
  3461. [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Object)
  3462. public void SetSocketOption3_OptionValue_Null ()
  3463. {
  3464. using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) {
  3465. try {
  3466. s.SetSocketOption (SocketOptionLevel.Socket,
  3467. SocketOptionName.Linger, (object) null);
  3468. Assert.Fail ("#1");
  3469. } catch (ArgumentNullException ex) {
  3470. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  3471. Assert.IsNull (ex.InnerException, "#3");
  3472. Assert.IsNotNull (ex.Message, "#4");
  3473. Assert.AreEqual ("optionValue", ex.ParamName, "#5");
  3474. }
  3475. }
  3476. }
  3477. [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Object)
  3478. public void SetSocketOption3_Socket_Closed ()
  3479. {
  3480. Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  3481. s.Close ();
  3482. try {
  3483. s.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.Linger,
  3484. new LingerOption (false, 0));
  3485. Assert.Fail ("#1");
  3486. } catch (ObjectDisposedException ex) {
  3487. // Cannot access a disposed object
  3488. Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
  3489. Assert.IsNull (ex.InnerException, "#3");
  3490. Assert.IsNotNull (ex.Message, "#4");
  3491. Assert.AreEqual (typeof (Socket).FullName, ex.ObjectName, "#5");
  3492. }
  3493. }
  3494. [Test]
  3495. public void Shutdown_NoConnect ()
  3496. {
  3497. Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  3498. s.Bind (new IPEndPoint (IPAddress.Loopback, 0));
  3499. s.Listen (1);
  3500. try {
  3501. s.Shutdown (SocketShutdown.Both);
  3502. Assert.Fail ("#1");
  3503. } catch (SocketException exc) {
  3504. Assert.AreEqual (10057, exc.ErrorCode, "#2");
  3505. } finally {
  3506. s.Close ();
  3507. }
  3508. }
  3509. [Test]
  3510. [ExpectedException (typeof (NullReferenceException))]
  3511. public void ReceiveAsync_Null ()
  3512. {
  3513. using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) {
  3514. s.ReceiveAsync (null);
  3515. }
  3516. }
  3517. [Test]
  3518. [ExpectedException (typeof (NullReferenceException))]
  3519. public void ReceiveAsync_Default ()
  3520. {
  3521. using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) {
  3522. SocketAsyncEventArgs saea = new SocketAsyncEventArgs ();
  3523. s.ReceiveAsync (saea);
  3524. }
  3525. }
  3526. [Test]
  3527. [ExpectedException (typeof (NullReferenceException))]
  3528. public void ReceiveAsync_NullBuffer ()
  3529. {
  3530. using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) {
  3531. SocketAsyncEventArgs saea = new SocketAsyncEventArgs ();
  3532. saea.SetBuffer (null, 0, 0);
  3533. s.ReceiveAsync (null);
  3534. }
  3535. }
  3536. [Test]
  3537. [ExpectedException (typeof (ObjectDisposedException))]
  3538. public void ReceiveAsync_ClosedSocket ()
  3539. {
  3540. Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  3541. s.Close ();
  3542. s.ReceiveAsync (null);
  3543. }
  3544. [Test]
  3545. [ExpectedException (typeof (NullReferenceException))]
  3546. public void SendAsync_Null ()
  3547. {
  3548. using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) {
  3549. s.SendAsync (null);
  3550. }
  3551. }
  3552. [Test]
  3553. [ExpectedException (typeof (NullReferenceException))]
  3554. public void SendAsync_Default ()
  3555. {
  3556. using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) {
  3557. SocketAsyncEventArgs saea = new SocketAsyncEventArgs ();
  3558. s.SendAsync (saea);
  3559. }
  3560. }
  3561. [Test]
  3562. [ExpectedException (typeof (NullReferenceException))]
  3563. public void SendAsync_NullBuffer ()
  3564. {
  3565. using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) {
  3566. SocketAsyncEventArgs saea = new SocketAsyncEventArgs ();
  3567. saea.SetBuffer (null, 0, 0);
  3568. s.SendAsync (null);
  3569. }
  3570. }
  3571. [Test]
  3572. [ExpectedException (typeof (ObjectDisposedException))]
  3573. public void SendAsync_ClosedSocket ()
  3574. {
  3575. Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  3576. s.Close ();
  3577. s.SendAsync (null);
  3578. }
  3579. [Test]
  3580. public void SendAsyncFile ()
  3581. {
  3582. Socket serverSocket = StartSocketServer ();
  3583. Socket clientSocket = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  3584. clientSocket.Connect (serverSocket.LocalEndPoint);
  3585. clientSocket.NoDelay = true;
  3586. // Initialize buffer used to create testing file
  3587. var buffer = new byte [1024];
  3588. for (int i = 0; i < 1024; ++i)
  3589. buffer [i] = (byte) (i % 256);
  3590. string temp = Path.GetTempFileName ();
  3591. try {
  3592. // Testing file creation
  3593. using (StreamWriter sw = new StreamWriter (temp)) {
  3594. sw.Write (buffer);
  3595. }
  3596. var m = new ManualResetEvent (false);
  3597. // Async Send File to server
  3598. clientSocket.BeginSendFile(temp, (ar) => {
  3599. Socket client = (Socket) ar.AsyncState;
  3600. client.EndSendFile (ar);
  3601. m.Set ();
  3602. }, clientSocket);
  3603. if (!m.WaitOne (1500))
  3604. throw new TimeoutException ();
  3605. m.Reset ();
  3606. } finally {
  3607. if (File.Exists (temp))
  3608. File.Delete (temp);
  3609. clientSocket.Close ();
  3610. serverSocket.Close ();
  3611. }
  3612. }
  3613. Socket StartSocketServer ()
  3614. {
  3615. Socket listenSocket = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  3616. listenSocket.Bind (new IPEndPoint (IPAddress.Loopback, 8001));
  3617. listenSocket.Listen (1);
  3618. listenSocket.BeginAccept (new AsyncCallback (ReceiveCallback), listenSocket);
  3619. return listenSocket;
  3620. }
  3621. public static void ReceiveCallback (IAsyncResult AsyncCall)
  3622. {
  3623. byte[] bytes = new byte [1024];
  3624. Socket listener = (Socket)AsyncCall.AsyncState;
  3625. Socket client = listener.EndAccept (AsyncCall);
  3626. client.Receive (bytes, bytes.Length, 0);
  3627. client.Close ();
  3628. }
  3629. }
  3630. }