PageRenderTime 37ms CodeModel.GetById 0ms 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

Large files files are truncated, but you can click here to view the full file

  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 ("BeginConnectHostPor…

Large files files are truncated, but you can click here to view the full file