PageRenderTime 55ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://bitbucket.org/danipen/mono
C# | 2947 lines | 2385 code | 478 blank | 84 comment | 24 complexity | e5ea4fcda1f077c5f678a8e07819e78f 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. namespace MonoTests.System.Net.Sockets
  17. {
  18. [TestFixture]
  19. public class SocketTest
  20. {
  21. // note: also used in SocketCas tests
  22. public const string BogusAddress = "192.168.244.244";
  23. public const int BogusPort = 23483;
  24. [Test]
  25. public void ConnectIPAddressAny ()
  26. {
  27. IPEndPoint ep = new IPEndPoint (IPAddress.Any, 0);
  28. #if !TARGET_JVM
  29. //udp sockets are not supported
  30. try {
  31. using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp)) {
  32. s.Connect (ep);
  33. s.Close ();
  34. }
  35. Assert.Fail ("#1");
  36. } catch (SocketException ex) {
  37. Assert.AreEqual (10049, ex.ErrorCode, "#2");
  38. }
  39. #endif
  40. try {
  41. using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) {
  42. s.Connect (ep);
  43. s.Close ();
  44. }
  45. Assert.Fail ("#3");
  46. } catch (SocketException ex) {
  47. Assert.AreEqual (10049, ex.ErrorCode, "#4");
  48. }
  49. }
  50. [Test]
  51. [Ignore ("Bug #75158")]
  52. public void IncompatibleAddress ()
  53. {
  54. IPEndPoint epIPv6 = new IPEndPoint (IPAddress.IPv6Any,
  55. 0);
  56. try {
  57. using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP)) {
  58. s.Connect (epIPv6);
  59. s.Close ();
  60. }
  61. Assert.Fail ("#1");
  62. } catch (SocketException ex) {
  63. #if !NET_2_0
  64. // invalid argument
  65. int expectedError = 10022;
  66. #else
  67. // address incompatible with protocol
  68. int expectedError = 10047;
  69. #endif
  70. Assert.AreEqual (expectedError, ex.ErrorCode,
  71. "#2");
  72. }
  73. }
  74. [Test]
  75. [Category ("InetAccess")]
  76. public void EndConnect ()
  77. {
  78. IPAddress ipOne = IPAddress.Parse (BogusAddress);
  79. IPEndPoint ipEP = new IPEndPoint (ipOne, BogusPort);
  80. Socket sock = new Socket (ipEP.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
  81. IAsyncResult ar = sock.BeginConnect (ipEP, null, null);
  82. bool gotException = false;
  83. try {
  84. sock.EndConnect (ar); // should raise an exception because connect was bogus
  85. } catch {
  86. gotException = true;
  87. }
  88. Assertion.AssertEquals ("A01", gotException, true);
  89. }
  90. [Test]
  91. [ExpectedException (typeof (ArgumentNullException))]
  92. public void SelectEmpty ()
  93. {
  94. ArrayList list = new ArrayList ();
  95. Socket.Select (list, list, list, 1000);
  96. }
  97. private bool BlockingConnect (bool block)
  98. {
  99. IPEndPoint ep = new IPEndPoint(IPAddress.Loopback, 1234);
  100. Socket server = new Socket(AddressFamily.InterNetwork,
  101. SocketType.Stream,
  102. ProtocolType.Tcp);
  103. server.Bind(ep);
  104. server.Blocking=block;
  105. server.Listen(0);
  106. Socket conn = new Socket (AddressFamily.InterNetwork,
  107. SocketType.Stream,
  108. ProtocolType.Tcp);
  109. conn.Connect (ep);
  110. Socket client = server.Accept();
  111. bool client_block = client.Blocking;
  112. client.Close();
  113. conn.Close();
  114. server.Close();
  115. return(client_block);
  116. }
  117. [Test]
  118. public void AcceptBlockingStatus()
  119. {
  120. bool block;
  121. block = BlockingConnect(true);
  122. Assertion.AssertEquals ("BlockingStatus01",
  123. block, true);
  124. block = BlockingConnect(false);
  125. Assertion.AssertEquals ("BlockingStatus02",
  126. block, false);
  127. }
  128. static bool CFAConnected = false;
  129. static ManualResetEvent CFACalledBack;
  130. private static void CFACallback (IAsyncResult asyncResult)
  131. {
  132. Socket sock = (Socket)asyncResult.AsyncState;
  133. CFAConnected = sock.Connected;
  134. if (sock.Connected) {
  135. sock.EndConnect (asyncResult);
  136. }
  137. CFACalledBack.Set ();
  138. }
  139. [Test]
  140. public void ConnectFailAsync ()
  141. {
  142. Socket sock = new Socket (AddressFamily.InterNetwork,
  143. SocketType.Stream,
  144. ProtocolType.Tcp);
  145. sock.Blocking = false;
  146. CFACalledBack = new ManualResetEvent (false);
  147. CFACalledBack.Reset ();
  148. /* Need a port that is not being used for
  149. * anything...
  150. */
  151. sock.BeginConnect (new IPEndPoint (IPAddress.Loopback,
  152. 114),
  153. new AsyncCallback (CFACallback),
  154. sock);
  155. CFACalledBack.WaitOne ();
  156. Assertion.AssertEquals ("ConnectFail", CFAConnected,
  157. false);
  158. }
  159. #if !TARGET_JVM
  160. [Test]
  161. #if !NET_2_0
  162. [ExpectedException (typeof (ArgumentException))]
  163. #endif
  164. public void SetSocketOptionBoolean ()
  165. {
  166. IPEndPoint ep = new IPEndPoint (IPAddress.Loopback, 1);
  167. Socket sock = new Socket (ep.Address.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
  168. try {
  169. sock.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.KeepAlive, true);
  170. } finally {
  171. sock.Close ();
  172. }
  173. }
  174. #endif
  175. [Test]
  176. #if TARGET_JVM
  177. [Ignore ("NMA")]
  178. #endif
  179. public void TestSelect1 ()
  180. {
  181. Socket srv = CreateServer ();
  182. ClientSocket clnt = new ClientSocket (srv.LocalEndPoint);
  183. Thread th = new Thread (new ThreadStart (clnt.ConnectSleepClose));
  184. Socket acc = null;
  185. try {
  186. th.Start ();
  187. acc = srv.Accept ();
  188. clnt.Write ();
  189. ArrayList list = new ArrayList ();
  190. ArrayList empty = new ArrayList ();
  191. list.Add (acc);
  192. Socket.Select (list, empty, empty, 100);
  193. Assertion.AssertEquals ("#01", 0, empty.Count);
  194. Assertion.AssertEquals ("#02", 1, list.Count);
  195. Socket.Select (empty, list, empty, 100);
  196. Assertion.AssertEquals ("#03", 0, empty.Count);
  197. Assertion.AssertEquals ("#04", 1, list.Count);
  198. Socket.Select (list, empty, empty, -1);
  199. Assertion.AssertEquals ("#05", 0, empty.Count);
  200. Assertion.AssertEquals ("#06", 1, list.Count);
  201. } finally {
  202. if (acc != null)
  203. acc.Close ();
  204. srv.Close ();
  205. }
  206. }
  207. static Socket CreateServer ()
  208. {
  209. Socket sock = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  210. sock.Bind (new IPEndPoint (IPAddress.Loopback, 0));
  211. sock.Listen (1);
  212. return sock;
  213. }
  214. class ClientSocket {
  215. Socket sock;
  216. EndPoint ep;
  217. public ClientSocket (EndPoint ep)
  218. {
  219. this.ep = ep;
  220. sock = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  221. }
  222. public void ConnectSleepClose ()
  223. {
  224. sock.Connect (ep);
  225. Thread.Sleep (2000);
  226. sock.Close ();
  227. }
  228. public void Write ()
  229. {
  230. byte [] b = new byte [10];
  231. sock.Send (b);
  232. }
  233. }
  234. byte[] buf = new byte[100];
  235. [Test]
  236. [ExpectedException (typeof (ObjectDisposedException))]
  237. public void Disposed1 ()
  238. {
  239. #if TARGET_JVM
  240. //UDP sockets are not supported in GH
  241. Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  242. #else
  243. Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
  244. #endif
  245. EndPoint ep = new IPEndPoint (IPAddress.Any, 31337);
  246. s.Close();
  247. s.ReceiveFrom (buf, ref ep);
  248. }
  249. [Test]
  250. [ExpectedException (typeof (ObjectDisposedException))]
  251. public void Disposed2 ()
  252. {
  253. #if TARGET_JVM
  254. //UDP sockets are not supported in GH
  255. Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  256. #else
  257. Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
  258. #endif
  259. s.Close();
  260. s.Blocking = true;
  261. }
  262. [Test]
  263. [ExpectedException (typeof (ObjectDisposedException))]
  264. public void Disposed3 ()
  265. {
  266. #if TARGET_JVM
  267. //UDP sockets are not supported in GH
  268. Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  269. #else
  270. Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
  271. #endif
  272. s.Close();
  273. s.GetSocketOption (0, 0);
  274. }
  275. [Test]
  276. [ExpectedException (typeof (ObjectDisposedException))]
  277. public void Disposed4 ()
  278. {
  279. #if TARGET_JVM
  280. //UDP sockets are not supported in GH
  281. Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  282. #else
  283. Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
  284. #endif
  285. s.Close();
  286. s.GetSocketOption (0, 0, null);
  287. }
  288. [Test]
  289. [ExpectedException (typeof (ObjectDisposedException))]
  290. public void Disposed5 ()
  291. {
  292. #if TARGET_JVM
  293. //UDP sockets are not supported in GH
  294. Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  295. #else
  296. Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
  297. #endif
  298. s.Close();
  299. s.GetSocketOption (0, 0, 0);
  300. }
  301. [Test]
  302. [ExpectedException (typeof (ObjectDisposedException))]
  303. public void Disposed6 ()
  304. {
  305. #if TARGET_JVM
  306. //UDP sockets are not supported in GH
  307. Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  308. #else
  309. Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
  310. #endif
  311. s.Close();
  312. s.Listen (5);
  313. }
  314. [Test]
  315. [ExpectedException (typeof (ObjectDisposedException))]
  316. public void Disposed7 ()
  317. {
  318. #if TARGET_JVM
  319. //UDP sockets are not supported in GH
  320. Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  321. #else
  322. Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
  323. #endif
  324. s.Close();
  325. s.Poll (100, 0);
  326. }
  327. [Test]
  328. [ExpectedException (typeof (ObjectDisposedException))]
  329. public void Disposed8 ()
  330. {
  331. #if TARGET_JVM
  332. //UDP sockets are not supported in GH
  333. Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  334. #else
  335. Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
  336. #endif
  337. s.Close();
  338. s.Receive (buf);
  339. }
  340. [Test]
  341. [ExpectedException (typeof (ObjectDisposedException))]
  342. public void Disposed9 ()
  343. {
  344. #if TARGET_JVM
  345. //UDP sockets are not supported in GH
  346. Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  347. #else
  348. Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
  349. #endif
  350. s.Close();
  351. s.Receive (buf, 0);
  352. }
  353. [Test]
  354. [ExpectedException (typeof (ObjectDisposedException))]
  355. public void Disposed10 ()
  356. {
  357. #if TARGET_JVM
  358. //UDP sockets are not supported in GH
  359. Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  360. #else
  361. Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
  362. #endif
  363. s.Close();
  364. s.Receive (buf, 10, 0);
  365. }
  366. [Test]
  367. [ExpectedException (typeof (ObjectDisposedException))]
  368. public void Disposed11 ()
  369. {
  370. #if TARGET_JVM
  371. //UDP sockets are not supported in GH
  372. Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  373. #else
  374. Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
  375. #endif
  376. EndPoint ep = new IPEndPoint (IPAddress.Any, 31337);
  377. s.Close();
  378. s.Receive (buf, 0, 10, 0);
  379. }
  380. [Test]
  381. [ExpectedException (typeof (ObjectDisposedException))]
  382. public void Disposed12 ()
  383. {
  384. #if TARGET_JVM
  385. //UDP sockets are not supported in GH
  386. Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  387. #else
  388. Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
  389. #endif
  390. EndPoint ep = new IPEndPoint (IPAddress.Any, 31337);
  391. s.Close();
  392. s.ReceiveFrom (buf, 0, ref ep);
  393. }
  394. [Test]
  395. [ExpectedException (typeof (ObjectDisposedException))]
  396. public void Disposed13 ()
  397. {
  398. #if TARGET_JVM
  399. //UDP sockets are not supported in GH
  400. Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  401. #else
  402. Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
  403. #endif
  404. EndPoint ep = new IPEndPoint (IPAddress.Any, 31337);
  405. s.Close();
  406. s.ReceiveFrom (buf, 10, 0, ref ep);
  407. }
  408. [Test]
  409. [ExpectedException (typeof (ObjectDisposedException))]
  410. public void Disposed14 ()
  411. {
  412. #if TARGET_JVM
  413. //UDP sockets are not supported in GH
  414. Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  415. #else
  416. Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
  417. #endif
  418. EndPoint ep = new IPEndPoint (IPAddress.Any, 31337);
  419. s.Close();
  420. s.ReceiveFrom (buf, 0, 10, 0, ref ep);
  421. }
  422. [Test]
  423. [ExpectedException (typeof (ObjectDisposedException))]
  424. public void Disposed15 ()
  425. {
  426. #if TARGET_JVM
  427. //UDP sockets are not supported in GH
  428. Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  429. #else
  430. Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
  431. #endif
  432. s.Close();
  433. s.Send (buf);
  434. }
  435. [Test]
  436. [ExpectedException (typeof (ObjectDisposedException))]
  437. public void Disposed16 ()
  438. {
  439. #if TARGET_JVM
  440. //UDP sockets are not supported in GH
  441. Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  442. #else
  443. Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
  444. #endif
  445. s.Close();
  446. s.Send (buf, 0);
  447. }
  448. [Test]
  449. [ExpectedException (typeof (ObjectDisposedException))]
  450. public void Disposed17 ()
  451. {
  452. #if TARGET_JVM
  453. //UDP sockets are not supported in GH
  454. Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  455. #else
  456. Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
  457. #endif
  458. s.Close();
  459. s.Send (buf, 10, 0);
  460. }
  461. [Test]
  462. [ExpectedException (typeof (ObjectDisposedException))]
  463. public void Disposed18 ()
  464. {
  465. #if TARGET_JVM
  466. //UDP sockets are not supported in GH
  467. Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  468. #else
  469. Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
  470. #endif
  471. EndPoint ep = new IPEndPoint (IPAddress.Any, 31337);
  472. s.Close();
  473. s.Send (buf, 0, 10, 0);
  474. }
  475. [Test]
  476. [ExpectedException (typeof (ObjectDisposedException))]
  477. public void Disposed19 ()
  478. {
  479. #if TARGET_JVM
  480. //UDP sockets are not supported in GH
  481. Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  482. #else
  483. Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
  484. #endif
  485. EndPoint ep = new IPEndPoint (IPAddress.Any, 31337);
  486. s.Close();
  487. s.SendTo (buf, 0, ep);
  488. }
  489. [Test]
  490. [ExpectedException (typeof (ObjectDisposedException))]
  491. public void Disposed20 ()
  492. {
  493. #if TARGET_JVM
  494. //UDP sockets are not supported in GH
  495. Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  496. #else
  497. Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
  498. #endif
  499. EndPoint ep = new IPEndPoint (IPAddress.Any, 31337);
  500. s.Close();
  501. s.SendTo (buf, 10, 0, ep);
  502. }
  503. [Test]
  504. [ExpectedException (typeof (ObjectDisposedException))]
  505. public void Disposed21 ()
  506. {
  507. #if TARGET_JVM
  508. //UDP sockets are not supported in GH
  509. Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  510. #else
  511. Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
  512. #endif
  513. EndPoint ep = new IPEndPoint(IPAddress.Any, 31337);
  514. s.Close();
  515. s.SendTo(buf, 0, 10, 0, ep);
  516. }
  517. [Test]
  518. [ExpectedException (typeof (ObjectDisposedException))]
  519. public void Disposed22 ()
  520. {
  521. #if TARGET_JVM
  522. //UDP sockets are not supported in GH
  523. Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  524. #else
  525. Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
  526. #endif
  527. EndPoint ep = new IPEndPoint (IPAddress.Any, 31337);
  528. s.Close();
  529. s.SendTo (buf, ep);
  530. }
  531. [Test]
  532. [ExpectedException (typeof (ObjectDisposedException))]
  533. public void Disposed23 ()
  534. {
  535. #if TARGET_JVM
  536. //UDP sockets are not supported in GH
  537. Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  538. #else
  539. Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
  540. #endif
  541. EndPoint ep = new IPEndPoint (IPAddress.Any, 31337);
  542. s.Close();
  543. s.Shutdown (0);
  544. }
  545. static ManualResetEvent SocketError_event = new ManualResetEvent (false);
  546. private static void SocketError_callback (IAsyncResult ar)
  547. {
  548. Socket sock = (Socket)ar.AsyncState;
  549. if(sock.Connected) {
  550. sock.EndConnect (ar);
  551. }
  552. SocketError_event.Set ();
  553. }
  554. [Test]
  555. #if TARGET_JVM
  556. [Ignore ("NMA")]
  557. #endif
  558. public void SocketError ()
  559. {
  560. Socket sock = new Socket (AddressFamily.InterNetwork,
  561. SocketType.Stream,
  562. ProtocolType.Tcp);
  563. IPEndPoint ep = new IPEndPoint (IPAddress.Loopback,
  564. BogusPort);
  565. SocketError_event.Reset ();
  566. sock.Blocking = false;
  567. sock.BeginConnect (ep, SocketError_callback,
  568. sock);
  569. if (SocketError_event.WaitOne (2000, false) == false) {
  570. Assert.Fail ("SocketError wait timed out");
  571. }
  572. Assertion.AssertEquals ("SocketError #1", false,
  573. sock.Connected);
  574. int error;
  575. error = (int)sock.GetSocketOption (SocketOptionLevel.Socket, SocketOptionName.Error);
  576. Assertion.AssertEquals ("SocketError #2", 10061,
  577. error);
  578. error = (int)sock.GetSocketOption (SocketOptionLevel.Socket, SocketOptionName.Error);
  579. Assertion.AssertEquals ("SocketError #3", 10061,
  580. error);
  581. sock.Close ();
  582. }
  583. #if NET_2_0
  584. [Test]
  585. public void SocketInformationCtor ()
  586. {
  587. }
  588. [Test]
  589. #if TARGET_JVM
  590. [Ignore ("Socket.DontFragment property is not supported")]
  591. #endif
  592. public void DontFragmentDefaultTcp ()
  593. {
  594. Socket sock = new Socket (AddressFamily.InterNetwork,
  595. SocketType.Stream,
  596. ProtocolType.Tcp);
  597. Assertion.AssertEquals ("DontFragmentDefaultTcp",
  598. false, sock.DontFragment);
  599. sock.Close ();
  600. }
  601. [Test]
  602. #if TARGET_JVM
  603. [Ignore("Socket.DontFragment property is not supported")]
  604. #endif
  605. public void DontFragmentChangeTcp ()
  606. {
  607. Socket sock = new Socket (AddressFamily.InterNetwork,
  608. SocketType.Stream,
  609. ProtocolType.Tcp);
  610. sock.DontFragment = true;
  611. Assertion.AssertEquals ("DontFragmentChangeTcp",
  612. true, sock.DontFragment);
  613. sock.Close ();
  614. }
  615. [Test]
  616. #if TARGET_JVM
  617. [Ignore("Socket.DontFragment property is not supported")]
  618. #endif
  619. public void DontFragmentDefaultUdp ()
  620. {
  621. Socket sock = new Socket (AddressFamily.InterNetwork,
  622. SocketType.Dgram,
  623. ProtocolType.Udp);
  624. Assertion.AssertEquals ("DontFragmentDefaultUdp",
  625. false, sock.DontFragment);
  626. sock.Close ();
  627. }
  628. [Test]
  629. #if TARGET_JVM
  630. [Ignore("Socket.DontFragment property is not supported")]
  631. #endif
  632. public void DontFragmentChangeUdp ()
  633. {
  634. Socket sock = new Socket (AddressFamily.InterNetwork,
  635. SocketType.Dgram,
  636. ProtocolType.Udp);
  637. sock.DontFragment = true;
  638. Assertion.AssertEquals ("DontFragmentChangeUdp",
  639. true, sock.DontFragment);
  640. sock.Close ();
  641. }
  642. [Test]
  643. [ExpectedException (typeof(ObjectDisposedException))]
  644. #if TARGET_JVM
  645. [Ignore("Socket.DontFragment property is not supported")]
  646. #endif
  647. public void DontFragmentClosed ()
  648. {
  649. Socket sock = new Socket (AddressFamily.InterNetwork,
  650. SocketType.Stream,
  651. ProtocolType.Tcp);
  652. sock.Close ();
  653. bool val = sock.DontFragment;
  654. }
  655. [Test]
  656. [Category ("NotWorking")] // Need to pick a non-IP AddressFamily that "works" on both mono and ms, this one only works on ms
  657. public void DontFragment ()
  658. {
  659. Socket sock = new Socket (AddressFamily.NetBios,
  660. SocketType.Seqpacket,
  661. ProtocolType.Unspecified);
  662. try {
  663. sock.DontFragment = true;
  664. Assert.Fail ("DontFragment #1");
  665. } catch (NotSupportedException) {
  666. } catch {
  667. Assert.Fail ("DontFragment #2");
  668. } finally {
  669. sock.Close ();
  670. }
  671. }
  672. [Test]
  673. #if TARGET_JVM
  674. [Ignore("System.Net.Sockets.Socket.EnableBroadcast property is not supported")]
  675. #endif
  676. public void EnableBroadcastDefaultTcp ()
  677. {
  678. Socket sock = new Socket (AddressFamily.InterNetwork,
  679. SocketType.Stream,
  680. ProtocolType.Tcp);
  681. try {
  682. bool value = sock.EnableBroadcast;
  683. Assert.Fail ("EnableBroadcastDefaultTcp #1");
  684. } catch (SocketException ex) {
  685. Assert.AreEqual (10042, ex.ErrorCode, "EnableBroadcastDefaultTcp #2");
  686. } catch {
  687. Assert.Fail ("EnableBroadcastDefaultTcp #2");
  688. } finally {
  689. sock.Close ();
  690. }
  691. }
  692. [Test]
  693. #if TARGET_JVM
  694. [Ignore("System.Net.Sockets.Socket.EnableBroadcast property is not supported")]
  695. #endif
  696. public void EnableBroadcastDefaultUdp ()
  697. {
  698. Socket sock = new Socket (AddressFamily.InterNetwork,
  699. SocketType.Dgram,
  700. ProtocolType.Udp);
  701. Assertion.AssertEquals ("EnableBroadcastDefaultUdp",
  702. false, sock.EnableBroadcast);
  703. sock.Close ();
  704. }
  705. [Test]
  706. #if TARGET_JVM
  707. [Ignore ("System.Net.Sockets.Socket.EnableBroadcast property is not supported")]
  708. #endif
  709. public void EnableBroadcastChangeTcp ()
  710. {
  711. Socket sock = new Socket (AddressFamily.InterNetwork,
  712. SocketType.Stream,
  713. ProtocolType.Tcp);
  714. try {
  715. sock.EnableBroadcast = true;
  716. Assert.Fail ("EnableBroadcastChangeTcp #1");
  717. } catch (SocketException ex) {
  718. Assert.AreEqual (10042, ex.ErrorCode, "EnableBroadcastChangeTcp #2");
  719. } catch {
  720. Assert.Fail ("EnableBroadcastChangeTcp #2");
  721. } finally {
  722. sock.Close ();
  723. }
  724. }
  725. [Test]
  726. #if TARGET_JVM
  727. [Ignore("System.Net.Sockets.Socket.EnableBroadcast property is not supported")]
  728. #endif
  729. public void EnableBroadcastChangeUdp ()
  730. {
  731. Socket sock = new Socket (AddressFamily.InterNetwork,
  732. SocketType.Dgram,
  733. ProtocolType.Udp);
  734. sock.EnableBroadcast = true;
  735. Assertion.AssertEquals ("EnableBroadcastChangeUdp",
  736. true, sock.EnableBroadcast);
  737. sock.Close ();
  738. }
  739. [Test]
  740. [ExpectedException (typeof(ObjectDisposedException))]
  741. #if TARGET_JVM
  742. [Ignore("System.Net.Sockets.Socket.EnableBroadcast property is not supported")]
  743. #endif
  744. public void EnableBroadcastClosed ()
  745. {
  746. Socket sock = new Socket (AddressFamily.InterNetwork,
  747. SocketType.Dgram,
  748. ProtocolType.Udp);
  749. sock.Close ();
  750. bool val = sock.EnableBroadcast;
  751. }
  752. /* Can't test the default for ExclusiveAddressUse as
  753. * it's different on different versions and service
  754. * packs of windows
  755. */
  756. [Test]
  757. [Category ("NotWorking")] // Not supported on Linux
  758. public void ExclusiveAddressUseUnbound ()
  759. {
  760. Socket sock = new Socket (AddressFamily.InterNetwork,
  761. SocketType.Stream,
  762. ProtocolType.Tcp);
  763. sock.ExclusiveAddressUse = true;
  764. Assertion.AssertEquals ("ExclusiveAddressUseUnbound",
  765. true,
  766. sock.ExclusiveAddressUse);
  767. sock.Close ();
  768. }
  769. [Test]
  770. [ExpectedException (typeof(InvalidOperationException))]
  771. [Category ("NotWorking")] // Not supported on Linux
  772. public void ExclusiveAddressUseBound ()
  773. {
  774. Socket sock = new Socket (AddressFamily.InterNetwork,
  775. SocketType.Stream,
  776. ProtocolType.Tcp);
  777. sock.Bind (new IPEndPoint (IPAddress.Any, 1235));
  778. sock.ExclusiveAddressUse = true;
  779. sock.Close ();
  780. }
  781. [Test]
  782. [ExpectedException (typeof(ObjectDisposedException))]
  783. #if TARGET_JVM
  784. [Ignore ("System.Net.Sockets.Socket.ExclusiveAddressUse is not supported")]
  785. #endif
  786. public void ExclusiveAddressUseClosed ()
  787. {
  788. Socket sock = new Socket (AddressFamily.InterNetwork,
  789. SocketType.Stream,
  790. ProtocolType.Tcp);
  791. sock.Close ();
  792. bool val = sock.ExclusiveAddressUse;
  793. }
  794. [Test]
  795. #if TARGET_JVM
  796. [Ignore ("System.Net.Sockets.Socket.IsBound property isn't supported")]
  797. #endif
  798. public void IsBoundTcp ()
  799. {
  800. Socket sock = new Socket (AddressFamily.InterNetwork,
  801. SocketType.Stream,
  802. ProtocolType.Tcp);
  803. IPEndPoint ep = new IPEndPoint (IPAddress.Loopback,
  804. BogusPort);
  805. Assertion.AssertEquals ("IsBoundTcp #1", false,
  806. sock.IsBound);
  807. sock.Bind (ep);
  808. Assertion.AssertEquals ("IsBoundTcp #2", true,
  809. sock.IsBound);
  810. sock.Listen (1);
  811. Socket sock2 = new Socket (AddressFamily.InterNetwork,
  812. SocketType.Stream,
  813. ProtocolType.Tcp);
  814. Assertion.AssertEquals ("IsBoundTcp #3", false,
  815. sock2.IsBound);
  816. sock2.Connect (ep);
  817. Assertion.AssertEquals ("IsBoundTcp #4", true,
  818. sock2.IsBound);
  819. sock2.Close ();
  820. Assertion.AssertEquals ("IsBoundTcp #5", true,
  821. sock2.IsBound);
  822. sock.Close ();
  823. Assertion.AssertEquals ("IsBoundTcp #6", true,
  824. sock.IsBound);
  825. }
  826. [Test]
  827. #if TARGET_JVM
  828. [Ignore("System.Net.Sockets.Socket.IsBound property isn't supported")]
  829. #endif
  830. public void IsBoundUdp ()
  831. {
  832. Socket sock = new Socket (AddressFamily.InterNetwork,
  833. SocketType.Dgram,
  834. ProtocolType.Udp);
  835. IPEndPoint ep = new IPEndPoint (IPAddress.Loopback,
  836. BogusPort);
  837. Assertion.AssertEquals ("IsBoundUdp #1", false,
  838. sock.IsBound);
  839. sock.Bind (ep);
  840. Assertion.AssertEquals ("IsBoundUdp #2", true,
  841. sock.IsBound);
  842. sock.Close ();
  843. Assertion.AssertEquals ("IsBoundUdp #3", true,
  844. sock.IsBound);
  845. sock = new Socket (AddressFamily.InterNetwork,
  846. SocketType.Dgram,
  847. ProtocolType.Udp);
  848. Assertion.AssertEquals ("IsBoundUdp #4", false,
  849. sock.IsBound);
  850. sock.Connect (ep);
  851. Assertion.AssertEquals ("IsBoundUdp #5", true,
  852. sock.IsBound);
  853. sock.Close ();
  854. Assertion.AssertEquals ("IsBoundUdp #6", true,
  855. sock.IsBound);
  856. }
  857. [Test]
  858. #if TARGET_JVM
  859. [Ignore ("System.Net.Sockets.Socket.IsBound property is not supported")]
  860. #endif
  861. /* Should not throw an exception */
  862. public void IsBoundClosed ()
  863. {
  864. Socket sock = new Socket (AddressFamily.InterNetwork,
  865. SocketType.Stream,
  866. ProtocolType.Tcp);
  867. sock.Close ();
  868. bool val = sock.IsBound;
  869. }
  870. /* Nothing much to test for LingerState */
  871. [Test]
  872. #if TARGET_JVM
  873. [Ignore("System.Net.Sockets.Socket.MulticastLoopback property is not supported")]
  874. #endif
  875. public void MulticastLoopbackDefaultTcp ()
  876. {
  877. Socket sock = new Socket (AddressFamily.InterNetwork,
  878. SocketType.Stream,
  879. ProtocolType.Tcp);
  880. try {
  881. bool value = sock.MulticastLoopback;
  882. Assert.Fail ("MulticastLoopbackDefaultTcp #1");
  883. } catch (SocketException ex) {
  884. Assert.AreEqual (10042, ex.ErrorCode, "MulticastLoopbackDefaultTcp #2");
  885. } catch {
  886. Assert.Fail ("MulticastLoopbackDefaultTcp #2");
  887. } finally {
  888. sock.Close ();
  889. }
  890. }
  891. [Test]
  892. #if TARGET_JVM
  893. [Ignore ("System.Net.Sockets.Socket.MulticastLoopback property is not supported")]
  894. #endif
  895. public void MulticastLoopbackChangeTcp ()
  896. {
  897. Socket sock = new Socket (AddressFamily.InterNetwork,
  898. SocketType.Stream,
  899. ProtocolType.Tcp);
  900. try {
  901. sock.MulticastLoopback = false;
  902. Assert.Fail ("MulticastLoopbackChangeTcp #1");
  903. } catch (SocketException ex) {
  904. Assert.AreEqual (10042, ex.ErrorCode, "MulticastLoopbackChangeTcp #2");
  905. } catch {
  906. Assert.Fail ("MulticastLoopbackChangeTcp #2");
  907. } finally {
  908. sock.Close ();
  909. }
  910. }
  911. [Test]
  912. #if TARGET_JVM
  913. [Ignore ("Udp sockets are not supported")]
  914. #endif
  915. public void MulticastLoopbackDefaultUdp ()
  916. {
  917. Socket sock = new Socket (AddressFamily.InterNetwork,
  918. SocketType.Dgram,
  919. ProtocolType.Udp);
  920. Assertion.AssertEquals ("MulticastLoopbackDefaultUdp",
  921. true, sock.MulticastLoopback);
  922. sock.Close ();
  923. }
  924. [Test]
  925. #if TARGET_JVM
  926. [Ignore ("Udp sockets are not supported")]
  927. #endif
  928. public void MulticastLoopbackChangeUdp ()
  929. {
  930. Socket sock = new Socket (AddressFamily.InterNetwork,
  931. SocketType.Dgram,
  932. ProtocolType.Udp);
  933. sock.MulticastLoopback = false;
  934. Assertion.AssertEquals ("MulticastLoopbackChangeUdp",
  935. false, sock.MulticastLoopback);
  936. sock.Close ();
  937. }
  938. [Test]
  939. [ExpectedException (typeof(ObjectDisposedException))]
  940. #if TARGET_JVM
  941. [Ignore("System.Net.Sockets.Socket.MulticastLoopback property is not supported")]
  942. #endif
  943. public void MulticastLoopbackClosed ()
  944. {
  945. Socket sock = new Socket (AddressFamily.InterNetwork,
  946. SocketType.Stream,
  947. ProtocolType.Tcp);
  948. sock.Close ();
  949. bool val = sock.MulticastLoopback;
  950. }
  951. /* OSSupportsIPv6 depends on the environment */
  952. [Test]
  953. #if TARGET_JVM
  954. [Ignore("System.Net.Sockets.Socket.ReseiveBufferSize property is not supported")]
  955. #endif
  956. public void ReceiveBufferSizeDefault ()
  957. {
  958. Socket sock = new Socket (AddressFamily.InterNetwork,
  959. SocketType.Stream,
  960. ProtocolType.Tcp);
  961. Assertion.AssertEquals ("ReceiveBufferSizeDefault",
  962. 8192, sock.ReceiveBufferSize);
  963. sock.Close ();
  964. }
  965. [Test]
  966. #if TARGET_JVM
  967. [Ignore("System.Net.Sockets.Socket.ReseiveBufferSize property is not supported")]
  968. #endif
  969. public void ReceiveBufferSizeDefaultUdp ()
  970. {
  971. Socket sock = new Socket (AddressFamily.InterNetwork,
  972. SocketType.Dgram,
  973. ProtocolType.Udp);
  974. Assertion.AssertEquals ("ReceiveBufferSizeDefaultUdp",
  975. 8192, sock.ReceiveBufferSize);
  976. sock.Close ();
  977. }
  978. [Test]
  979. #if TARGET_JVM
  980. [Ignore ("System.Net.Sockets.Socket.ReseiveBufferSize property is not supported")]
  981. #endif
  982. public void ReceiveBufferSizeChange ()
  983. {
  984. Socket sock = new Socket (AddressFamily.InterNetwork,
  985. SocketType.Stream,
  986. ProtocolType.Tcp);
  987. sock.ReceiveBufferSize = 16384;
  988. Assertion.AssertEquals ("ReceiveBufferSizeChange",
  989. 16384, sock.ReceiveBufferSize);
  990. sock.Close ();
  991. }
  992. [Test]
  993. [ExpectedException (typeof(ObjectDisposedException))]
  994. #if TARGET_JVM
  995. [Ignore("System.Net.Sockets.Socket.ReseiveBufferSize property is not supported")]
  996. #endif
  997. public void ReceiveBufferSizeClosed ()
  998. {
  999. Socket sock = new Socket (AddressFamily.InterNetwork,
  1000. SocketType.Stream,
  1001. ProtocolType.Tcp);
  1002. sock.Close ();
  1003. int val = sock.ReceiveBufferSize;
  1004. }
  1005. [Test]
  1006. #if TARGET_JVM
  1007. [Ignore("System.Net.Sockets.Socket.SendBufferSize property is not supported")]
  1008. #endif
  1009. public void SendBufferSizeDefault ()
  1010. {
  1011. Socket sock = new Socket (AddressFamily.InterNetwork,
  1012. SocketType.Stream,
  1013. ProtocolType.Tcp);
  1014. Assertion.AssertEquals ("SendBufferSizeDefault",
  1015. 8192, sock.SendBufferSize);
  1016. sock.Close ();
  1017. }
  1018. [Test]
  1019. #if TARGET_JVM
  1020. [Ignore("System.Net.Sockets.Socket.SendBufferSize property is not supported")]
  1021. #endif
  1022. public void SendBufferSizeDefaultUdp ()
  1023. {
  1024. Socket sock = new Socket (AddressFamily.InterNetwork,
  1025. SocketType.Dgram,
  1026. ProtocolType.Udp);
  1027. Assertion.AssertEquals ("SendBufferSizeDefaultUdp",
  1028. 8192, sock.SendBufferSize);
  1029. sock.Close ();
  1030. }
  1031. [Test]
  1032. #if TARGET_JVM
  1033. [Ignore ("System.Net.Sockets.Socket.SendBufferSize property is not supported")]
  1034. #endif
  1035. public void SendBufferSizeChange ()
  1036. {
  1037. Socket sock = new Socket (AddressFamily.InterNetwork,
  1038. SocketType.Stream,
  1039. ProtocolType.Tcp);
  1040. sock.SendBufferSize = 16384;
  1041. Assertion.AssertEquals ("SendBufferSizeChange",
  1042. 16384, sock.SendBufferSize);
  1043. sock.Close ();
  1044. }
  1045. [Test]
  1046. [ExpectedException (typeof(ObjectDisposedException))]
  1047. #if TARGET_JVM
  1048. [Ignore("System.Net.Sockets.Socket.SendBufferSize property is not supported")]
  1049. #endif
  1050. public void SendBufferSizeClosed ()
  1051. {
  1052. Socket sock = new Socket (AddressFamily.InterNetwork,
  1053. SocketType.Stream,
  1054. ProtocolType.Tcp);
  1055. sock.Close ();
  1056. int val = sock.SendBufferSize;
  1057. }
  1058. /* No test for TTL default as it's platform dependent */
  1059. [Test]
  1060. #if TARGET_JVM
  1061. [Ignore("System.Net.Sockets.Socket.Ttl property is not supported")]
  1062. #endif
  1063. public void TtlChange ()
  1064. {
  1065. Socket sock = new Socket (AddressFamily.InterNetwork,
  1066. SocketType.Stream,
  1067. ProtocolType.Tcp);
  1068. sock.Ttl = 255;
  1069. Assertion.AssertEquals ("TtlChange", 255, sock.Ttl);
  1070. sock.Close ();
  1071. }
  1072. [Test]
  1073. #if TARGET_JVM
  1074. [Ignore("System.Net.Sockets.Socket.Ttl property is not supported")]
  1075. #endif
  1076. public void TtlChangeOverflow ()
  1077. {
  1078. Socket sock = new Socket (AddressFamily.InterNetwork,
  1079. SocketType.Stream,
  1080. ProtocolType.Tcp);
  1081. try {
  1082. sock.Ttl = 256;
  1083. Assert.Fail ("TtlChangeOverflow #1");
  1084. } catch (SocketException ex) {
  1085. Assert.AreEqual (10022, ex.ErrorCode,
  1086. "TtlChangeOverflow #2");
  1087. } catch {
  1088. Assert.Fail ("TtlChangeoverflow #3");
  1089. } finally {
  1090. sock.Close ();
  1091. }
  1092. }
  1093. /* Apparently you can set TTL=0 on the ms runtime!!
  1094. try {
  1095. sock.Ttl = 0;
  1096. Assert.Fail ("TtlChangeOverflow #4");
  1097. } catch (SocketException ex) {
  1098. Assert.AreEqual (10022, ex.ErrorCode,
  1099. "TtlChangeOverflow #5");
  1100. } catch {
  1101. Assert.Fail ("TtlChangeOverflow #6");
  1102. } finally {
  1103. sock.Close ();
  1104. }
  1105. */
  1106. [Test]
  1107. [ExpectedException (typeof(ObjectDisposedException))]
  1108. #if TARGET_JVM
  1109. [Ignore("System.Net.Sockets.Socket.Ttl property is not supported")]
  1110. #endif
  1111. public void TtlClosed ()
  1112. {
  1113. Socket sock = new Socket (AddressFamily.InterNetwork,
  1114. SocketType.Stream,
  1115. ProtocolType.Tcp);
  1116. sock.Close ();
  1117. int val = sock.Ttl;
  1118. }
  1119. [Test]
  1120. #if TARGET_JVM
  1121. [Ignore("System.Net.Sockets.Socket.UseOnlyOverlappedIO property is not supported")]
  1122. #endif
  1123. public void UseOnlyOverlappedIODefault ()
  1124. {
  1125. Socket sock = new Socket (AddressFamily.InterNetwork,
  1126. SocketType.Stream,
  1127. ProtocolType.Tcp);
  1128. Assertion.AssertEquals ("UseOnlyOverlappedIODefault",
  1129. false,
  1130. sock.UseOnlyOverlappedIO);
  1131. sock.Close ();
  1132. }
  1133. //
  1134. // We need this because the Linux kernel in certain configurations
  1135. // will end up rounding up the values passed on to the kernel
  1136. // for socket send/receive timeouts.
  1137. //
  1138. int Approximate (int target, int value)
  1139. {
  1140. int epsilon = 10;
  1141. if (value > target-10 && value < target+10)
  1142. return target;
  1143. return value;
  1144. }
  1145. [Test]
  1146. #if TARGET_JVM
  1147. [Ignore("System.Net.Sockets.Socket.UseOnlyOverlappedIO property is not supported")]
  1148. #endif
  1149. public void UseOnlyOverlappedIOChange ()
  1150. {
  1151. Socket sock = new Socket (AddressFamily.InterNetwork,
  1152. SocketType.Stream,
  1153. ProtocolType.Tcp);
  1154. sock.UseOnlyOverlappedIO = true;
  1155. Assertion.AssertEquals ("UseOnlyOverlappedIOChange",
  1156. true,
  1157. sock.UseOnlyOverlappedIO);
  1158. sock.Close ();
  1159. }
  1160. [Test]
  1161. #if TARGET_JVM
  1162. [Ignore("System.Net.Sockets.Socket.UseOnlyOverlappedIO property is not supported")]
  1163. #endif
  1164. /* Should not throw an exception */
  1165. public void UseOnlyOverlappedIOClosed ()
  1166. {
  1167. Socket sock = new Socket (AddressFamily.InterNetwork,
  1168. SocketType.Stream,
  1169. ProtocolType.Tcp);
  1170. sock.Close ();
  1171. bool val = sock.UseOnlyOverlappedIO;
  1172. }
  1173. [Test]
  1174. #if TARGET_JVM
  1175. [Ignore("System.Net.Sockets.Socket.SendTimeout property is not supported")]
  1176. #endif
  1177. public void SendTimeoutDefault ()
  1178. {
  1179. Socket sock = new Socket (AddressFamily.InterNetwork,
  1180. SocketType.Stream,
  1181. ProtocolType.Tcp);
  1182. Assertion.AssertEquals ("SendTimeoutDefault",
  1183. 0, sock.SendTimeout);
  1184. sock.Close ();
  1185. }
  1186. [Test]
  1187. #if TARGET_JVM
  1188. [Ignore("System.Net.Sockets.Socket.SendTimeout property is not supported")]
  1189. #endif
  1190. public void SendTimeoutChange ()
  1191. {
  1192. Socket sock = new Socket (AddressFamily.InterNetwork,
  1193. SocketType.Stream,
  1194. ProtocolType.Tcp);
  1195. /* Should be rounded up to 500, according to
  1196. * the MSDN docs, but the MS runtime doesn't
  1197. */
  1198. sock.SendTimeout = 50;
  1199. Assertion.AssertEquals ("SendTimeoutChange #1",
  1200. 50, Approximate (50, sock.SendTimeout));
  1201. sock.SendTimeout = 2000;
  1202. Assertion.AssertEquals ("SendTimeoutChange #2",
  1203. 2000, Approximate (2000, sock.SendTimeout));
  1204. sock.SendTimeout = 0;
  1205. Assertion.AssertEquals ("SendTimeoutChange #3",
  1206. 0, Approximate (0, sock.SendTimeout));
  1207. /* Should be the same as setting 0 */
  1208. sock.SendTimeout = -1;
  1209. Assertion.AssertEquals ("SendTimeoutChange #4",
  1210. 0, sock.SendTimeout);
  1211. sock.SendTimeout = 65536;
  1212. Assertion.AssertEquals ("SendTimeoutChange #5",
  1213. 65536, Approximate (65536, sock.SendTimeout));
  1214. try {
  1215. sock.SendTimeout = -2;
  1216. Assert.Fail ("SendTimeoutChange #8");
  1217. } catch (ArgumentOutOfRangeException) {
  1218. } catch {
  1219. Assert.Fail ("SendTimeoutChange #9");
  1220. } finally {
  1221. sock.Close ();
  1222. }
  1223. }
  1224. [Test]
  1225. [ExpectedException (typeof(ObjectDisposedException))]
  1226. #if TARGET_JVM
  1227. [Ignore("System.Net.Sockets.Socket.SendTimeout property is not supported")]
  1228. #endif
  1229. public void SendTimeoutClosed ()
  1230. {
  1231. Socket sock = new Socket (AddressFamily.InterNetwork,
  1232. SocketType.Stream,
  1233. ProtocolType.Tcp);
  1234. sock.Close ();
  1235. int val = sock.SendTimeout;
  1236. }
  1237. [Test]
  1238. #if TARGET_JVM
  1239. [Ignore("System.Net.Sockets.Socket.ReceiveTimeout property is not supported")]
  1240. #endif
  1241. public void ReceiveTimeoutDefault ()
  1242. {
  1243. Socket sock = new Socket (AddressFamily.InterNetwork,
  1244. SocketType.Stream,
  1245. ProtocolType.Tcp);
  1246. Assertion.AssertEquals ("ReceiveTimeoutDefault",
  1247. 0, sock.ReceiveTimeout);
  1248. sock.Close ();
  1249. }
  1250. [Test]
  1251. #if TARGET_JVM
  1252. [Ignore("System.Net.Sockets.Socket.ReceiveTimeout property is not supported")]
  1253. #endif
  1254. public void ReceiveTimeoutChange ()
  1255. {
  1256. Socket sock = new Socket (AddressFamily.InterNetwork,
  1257. SocketType.Stream,
  1258. ProtocolType.Tcp);
  1259. sock.ReceiveTimeout = 50;
  1260. Assertion.AssertEquals ("ReceiveTimeoutChange #1",
  1261. 50, Approximate (50, sock.ReceiveTimeout));
  1262. sock.ReceiveTimeout = 2000;
  1263. Assertion.AssertEquals ("ReceiveTimeoutChange #2",
  1264. 2000, Approximate (2000, sock.ReceiveTimeout));
  1265. sock.ReceiveTimeout = 0;
  1266. Assertion.AssertEquals ("ReceiveTimeoutChange #3",
  1267. 0, sock.ReceiveTimeout);
  1268. /* Should be the same as setting 0 */
  1269. sock.ReceiveTimeout = -1;
  1270. Assertion.AssertEquals ("ReceiveTimeoutChange #4",
  1271. 0, sock.ReceiveTimeout);
  1272. sock.ReceiveTimeout = 65536;
  1273. Assertion.AssertEquals ("ReceiveTimeoutChange #5",
  1274. 65536, Approximate (65536, sock.ReceiveTimeout));
  1275. try {
  1276. sock.ReceiveTimeout = -2;
  1277. Assert.Fail ("ReceiveTimeoutChange #8");
  1278. } catch (ArgumentOutOfRangeException) {
  1279. } catch {
  1280. Assert.Fail ("ReceiveTimeoutChange #9");
  1281. } finally {
  1282. sock.Close ();
  1283. }
  1284. }
  1285. [Test]
  1286. [ExpectedException (typeof(ObjectDisposedException))]
  1287. #if TARGET_JVM
  1288. [Ignore("System.Net.Sockets.Socket.ReceiveTimeout property is not supported")]
  1289. #endif
  1290. public void ReceiveTimeoutClosed ()
  1291. {
  1292. Socket sock = new Socket (AddressFamily.InterNetwork,
  1293. SocketType.Stream,
  1294. ProtocolType.Tcp);
  1295. sock.Close ();
  1296. int val = sock.ReceiveTimeout;
  1297. }
  1298. [Test]
  1299. #if TARGET_JVM
  1300. [Ignore("System.Net.Sockets.Socket.NoDelay property is not supported")]
  1301. #endif
  1302. public void NoDelayDefaultTcp ()
  1303. {
  1304. Socket sock = new Socket (AddressFamily.InterNetwork,
  1305. SocketType.Stream,
  1306. ProtocolType.Tcp);
  1307. Assertion.AssertEquals ("NoDelayDefaultTcp", false,
  1308. sock.NoDelay);
  1309. sock.Close ();
  1310. }
  1311. [Test]
  1312. #if TARGET_JVM
  1313. [Ignore("System.Net.Sockets.Socket.NoDelay property is not supported")]
  1314. #endif
  1315. public void NoDelayChangeTcp ()
  1316. {
  1317. Socket sock = new Socket (AddressFamily.InterNetwork,
  1318. SocketType.Stream,
  1319. ProtocolType.Tcp);
  1320. sock.NoDelay = true;
  1321. Assertion.AssertEquals ("NoDelayChangeTcp", true,
  1322. sock.NoDelay);
  1323. sock.Close ();
  1324. }
  1325. [Test]
  1326. #if TARGET_JVM
  1327. [Ignore("System.Net.Sockets.Socket.NoDelay property is not supported")]
  1328. #endif
  1329. public void NoDelayDefaultUdp ()
  1330. {
  1331. Socket sock = new Socket (AddressFamily.InterNetwork,
  1332. SocketType.Dgram,
  1333. ProtocolType.Udp);
  1334. try {
  1335. bool val = sock.NoDelay;
  1336. Assert.Fail ("NoDelayDefaultUdp #1");
  1337. } catch (SocketException ex) {
  1338. Assert.AreEqual (10042, ex.ErrorCode,
  1339. "NoDelayDefaultUdp #2");
  1340. } catch {
  1341. Assert.Fail ("NoDelayDefaultUdp #3");
  1342. } finally {
  1343. sock.Close ();
  1344. }
  1345. }
  1346. [Test]
  1347. #if TARGET_JVM
  1348. [Ignore("System.Net.Sockets.Socket.NoDelay property is not supported")]
  1349. #endif
  1350. public void NoDelayChangeUdp ()
  1351. {
  1352. Socket sock = new Socket (AddressFamily.InterNetwork,
  1353. SocketType.Dgram,
  1354. ProtocolType.Udp);
  1355. try {
  1356. sock.NoDelay = true;
  1357. Assert.Fail ("NoDelayChangeUdp #1");
  1358. } catch (SocketException ex) {
  1359. Assert.AreEqual (10042, ex.ErrorCode,
  1360. "NoDelayChangeUdp #2");
  1361. } catch {
  1362. Assert.Fail ("NoDelayChangeUdp #3");
  1363. } finally {
  1364. sock.Close ();
  1365. }
  1366. }
  1367. [Test]
  1368. [ExpectedException (typeof(ObjectDisposedException))]
  1369. #if TARGET_JVM
  1370. [Ignore("System.Net.Sockets.Socket.NoDelay property is not supported")]
  1371. #endif
  1372. public void NoDelayClosed ()
  1373. {
  1374. Socket sock = new Socket (AddressFamily.InterNetwork,
  1375. SocketType.Stream,
  1376. ProtocolType.Tcp);
  1377. sock.Close ();
  1378. bool val = sock.NoDelay;
  1379. }
  1380. static bool BAAccepted = false;
  1381. static Socket BASocket = null;
  1382. static ManualResetEvent BACalledBack = new ManualResetEvent (false);
  1383. private static void BACallback (IAsyncResult asyncResult)
  1384. {
  1385. Socket sock = (Socket)asyncResult.AsyncState;
  1386. BASocket = sock.EndAccept (asyncResult);
  1387. BAAccepted = true;
  1388. BACalledBack.Set ();
  1389. }
  1390. [Test]
  1391. #if TARGET_JVM
  1392. [Ignore ("NMA")]
  1393. #endif
  1394. [ExpectedException (typeof(InvalidOperationException))]
  1395. public void BeginAcceptNotBound ()
  1396. {
  1397. Socket sock = new Socket (AddressFamily.InterNetwork,
  1398. SocketType.Stream,
  1399. ProtocolType.Tcp);
  1400. sock.BeginAccept (BACallback, sock);
  1401. sock.Close ();
  1402. }
  1403. [Test]
  1404. #if TARGET_JVM
  1405. [Ignore ("NMA")]
  1406. #endif
  1407. [ExpectedException (typeof(InvalidOperationException))]
  1408. public void BeginAcceptNotListening ()
  1409. {
  1410. Socket sock = new Socket (AddressFamily.InterNetwork,
  1411. SocketType.Stream,
  1412. ProtocolType.Tcp);
  1413. sock.Bind (new IPEndPoint (IPAddress.Any, 1236));
  1414. sock.BeginAccept (BACallback, sock);
  1415. sock.Close ();
  1416. }
  1417. [Test]
  1418. public void BeginAccept ()
  1419. {
  1420. Socket sock = new Socket (AddressFamily.InterNetwork,
  1421. SocketType.Stream,
  1422. ProtocolType.Tcp);
  1423. IPEndPoint ep = new IPEndPoint (IPAddress.Loopback,
  1424. 1237);
  1425. sock.Bind (ep);
  1426. sock.Listen (1);
  1427. BACalledBack.Reset ();
  1428. sock.BeginAccept (BACallback, sock);
  1429. Socket conn = new Socket (AddressFamily.InterNetwork,
  1430. SocketType.Stream,
  1431. ProtocolType.Tcp);
  1432. conn.Connect (ep);
  1433. if (BACalledBack.WaitOne (2000, false) == false) {
  1434. Assert.Fail ("BeginAccept wait timed out");
  1435. }
  1436. Assertion.AssertEquals ("BeginAccept #1", true,
  1437. BAAccepted);
  1438. Assertion.AssertEquals ("BeginAccept #2", true,
  1439. BASocket.Connected);
  1440. Assertion.AssertEquals ("BeginAccept #3", false,
  1441. sock.Connected);
  1442. Assertion.AssertEquals ("BeginAccept #4", true,
  1443. conn.Connected);
  1444. BASocket.Close ();
  1445. conn.Close ();
  1446. sock.Close ();
  1447. }
  1448. [Test]
  1449. [ExpectedException (typeof(ObjectDisposedException))]
  1450. public void BeginAcceptClosed ()
  1451. {
  1452. Socket sock = new Socket (AddressFamily.InterNetwork,
  1453. SocketType.Stream,
  1454. ProtocolType.Tcp);
  1455. sock.Close ();
  1456. sock.BeginAccept (BACallback, sock);
  1457. }
  1458. static bool BADAccepted = false;
  1459. static Socket BADSocket = null;
  1460. static byte[] BADBytes;
  1461. static int BADByteCount;
  1462. static ManualResetEvent BADCalledBack = new ManualResetEvent (false);
  1463. private static void BADCallback (IAsyncResult asyncResult)
  1464. {
  1465. #if !TARGET_JVM
  1466. Socket sock = (Socket)asyncResult.AsyncState;
  1467. BADSocket = sock.EndAccept (out BADBytes,
  1468. out BADByteCount,
  1469. asyncResult);
  1470. BADAccepted = true;
  1471. BADCalledBack.Set ();
  1472. #endif
  1473. }
  1474. [Test]
  1475. #if TARGET_JVM
  1476. [Ignore ("System.Net.Sockets.Socket.BeginAccept(int,AsyncCallback,object) is not supported")]
  1477. #endif
  1478. public void BeginAcceptData ()
  1479. {
  1480. Socket sock = new Socket (AddressFamily.InterNetwork,
  1481. SocketType.Stream,
  1482. ProtocolType.Tcp);
  1483. IPEndPoint ep = new IPEndPoint (IPAddress.Loopback,
  1484. 1238);
  1485. sock.Bind (ep);
  1486. sock.Listen (1);
  1487. BADCalledBack.Reset ();
  1488. sock.BeginAccept (256, BADCallback, sock);
  1489. Socket conn = new Socket (AddressFamily.InterNetwork,
  1490. SocketType.Stream,
  1491. ProtocolType.Tcp);
  1492. byte[] send_bytes = new byte[] {10, 11, 12, 13};
  1493. conn.Connect (ep);
  1494. conn.Send (send_bytes);
  1495. if (BADCalledBack.WaitOne (2000, false) == false) {
  1496. Assert.Fail ("BeginAcceptData wait timed out");
  1497. }
  1498. Assertion.AssertEquals ("BeginAcceptData #1", true,
  1499. BADAccepted);
  1500. Assertion.AssertEquals ("BeginAcceptData #2", true,
  1501. BADSocket.Connected);
  1502. Assertion.AssertEquals ("BeginAcceptData #3", false,
  1503. sock.Connected);
  1504. Assertion.AssertEquals ("BeginAcceptData #4", true,
  1505. conn.Connected);
  1506. Assertion.AssertEquals ("BeginAcceptData #5",
  1507. send_bytes.Length,
  1508. BADByteCount);
  1509. /* The MS runtime gives the returned data in a
  1510. * much bigger array. TODO: investigate
  1511. * whether it the size correlates to the first
  1512. * parameter in BeginAccept()
  1513. */
  1514. Assert.IsFalse (BADBytes.Length == send_bytes.Length,
  1515. "BeginAcceptData #6");
  1516. for(int i = 0; i < send_bytes.Length; i++) {
  1517. Assertion.AssertEquals ("BeginAcceptData #" + (i+7).ToString (), send_bytes[i], BADBytes[i]);
  1518. }
  1519. BADSocket.Close ();
  1520. conn.Close ();
  1521. sock.Close ();
  1522. }
  1523. [Test]
  1524. [ExpectedException (typeof(ObjectDisposedException))]
  1525. #if TARGET_JVM
  1526. [Ignore("System.Net.Sockets.Socket.BeginAccept(int,AsyncCallback,object) is not supported")]
  1527. #endif
  1528. public void BeginAcceptDataClosed ()
  1529. {
  1530. Socket sock = new Socket (AddressFamily.InterNetwork,
  1531. SocketType.Stream,
  1532. ProtocolType.Tcp);
  1533. sock.Close ();
  1534. sock.BeginAccept (256, BADCallback, sock);
  1535. }
  1536. [Test]
  1537. #if TARGET_JVM
  1538. [Ignore("System.Net.Sockets.Socket.BeginAccept(Socket,int,AsyncCallback,object) is not supported")]
  1539. #endif
  1540. public void BeginAcceptSocketUdp ()
  1541. {
  1542. Socket sock = new Socket (AddressFamily.InterNetwork,
  1543. SocketType.Stream,
  1544. ProtocolType.Tcp);
  1545. Socket acc = new Socket (AddressFamily.InterNetwork,
  1546. SocketType.Dgram,
  1547. ProtocolType.Udp);
  1548. IPEndPoint ep = new IPEndPoint (IPAddress.Loopback,
  1549. 1239);
  1550. sock.Bind (ep);
  1551. sock.Listen (1);
  1552. try {
  1553. sock.BeginAccept (acc, 256, BADCallback, sock);
  1554. Assert.Fail ("BeginAcceptSocketUdp #1");
  1555. } catch (SocketException ex) {
  1556. Assertion.AssertEquals ("BeginAcceptSocketUdp #2", 10022, ex.ErrorCode);
  1557. } catch {
  1558. Assert.Fail ("BeginAcceptSocketUdp #3");
  1559. } finally {
  1560. acc.Close ();
  1561. sock.Close ();
  1562. }
  1563. }
  1564. [Test]
  1565. #if TARGET_JVM
  1566. [Ignore("System.Net.Sockets.Socket.BeginAccept(Socket,int,AsyncCallback,object) is not supported")]
  1567. #endif
  1568. public void BeginAcceptSocketBound ()
  1569. {
  1570. Socket sock = new Socket (AddressFamily.InterNetwork,
  1571. SocketType.Stream,
  1572. ProtocolType.Tcp);
  1573. Socket acc = new Socket (AddressFamily.InterNetwork,
  1574. SocketType.Stream,
  1575. ProtocolType.Tcp);
  1576. IPEndPoint ep1 = new IPEndPoint (IPAddress.Loopback,
  1577. 1240);
  1578. IPEndPoint ep2 = new IPEndPoint (IPAddress.Loopback,
  1579. 1241);
  1580. sock.Bind (ep1);
  1581. sock.Listen (1);
  1582. acc.Bind (ep2);
  1583. try {
  1584. sock.BeginAccept (acc, 256, BADCallback, sock);
  1585. Assert.Fail ("BeginAcceptSocketBound #1");
  1586. } catch (InvalidOperationException) {
  1587. } catch {
  1588. Assert.Fail ("BeginAcceptSocketBound #2");
  1589. } finally {
  1590. acc.Close ();
  1591. sock.Close ();
  1592. }
  1593. }
  1594. [Test]
  1595. #if TARGET_JVM
  1596. [Ignore("System.Net.Sockets.Socket.BeginAccept(Socket,int,AsyncCallback,object) is not supported")]
  1597. #endif
  1598. public void BeginAcceptSocket ()
  1599. {
  1600. Socket sock = new Socket (AddressFamily.InterNetwork,
  1601. SocketType.Stream,
  1602. ProtocolType.Tcp);
  1603. Socket acc = new Socket (AddressFamily.InterNetwork,
  1604. SocketType.Stream,
  1605. ProtocolType.Tcp);
  1606. IPEndPoint ep = new IPEndPoint (IPAddress.Loopback,
  1607. 1242);
  1608. sock.Bind (ep);
  1609. sock.Listen (1);
  1610. BADCalledBack.Reset ();
  1611. sock.BeginAccept (acc, 256, BADCallback, sock);
  1612. Socket conn = new Socket (AddressFamily.InterNetwork,
  1613. SocketType.Stream,
  1614. ProtocolType.Tcp);
  1615. byte[] send_bytes = new byte[] {10, 11, 12, 13};
  1616. conn.Connect (ep);
  1617. conn.Send (send_bytes);
  1618. if (BADCalledBack.WaitOne (2000, false) == false) {
  1619. Assert.Fail ("BeginAcceptSocket wait timed out");
  1620. }
  1621. Assertion.AssertEquals ("BeginAcceptSocket #1", true,
  1622. BADAccepted);
  1623. Assertion.AssertEquals ("BeginAcceptSocket #2", true,
  1624. BADSocket.Connected);
  1625. Assertion.AssertEquals ("BeginAcceptSocket #3", false,
  1626. sock.Connected);
  1627. Assertion.AssertEquals ("BeginAcceptSocket #4", true,
  1628. conn.Connected);
  1629. Assertion.AssertEquals ("BeginAcceptSocket #5",
  1630. send_bytes.Length,
  1631. BADByteCount);
  1632. Assertion.AssertEquals ("BeginAcceptSocket #6",
  1633. AddressFamily.InterNetwork,
  1634. acc.AddressFamily);

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