SocketTest.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. // System.Net.Sockets.SocketTest.cs
  2. //
  3. // Authors:
  4. // Brad Fitzpatrick ([email protected])
  5. //
  6. // (C) Copyright 2003 Brad Fitzpatrick
  7. //
  8. using System;
  9. using System.Collections;
  10. using System.Net;
  11. using System.Net.Sockets;
  12. using NUnit.Framework;
  13. namespace MonoTests.System.Net.Sockets
  14. {
  15. [TestFixture]
  16. public class SocketTest
  17. {
  18. // note: also used in SocketCas tests
  19. public const string BogusAddress = "192.168.244.244";
  20. public const int BogusPort = 23483;
  21. [Test]
  22. [Ignore ("Bug #75154")]
  23. public void ConnectIPAddressAny ()
  24. {
  25. IPEndPoint ep = new IPEndPoint (IPAddress.Any, 0);
  26. try {
  27. using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp)) {
  28. s.Connect (ep);
  29. s.Close ();
  30. }
  31. Assert.Fail ("#1");
  32. } catch (SocketException ex) {
  33. Assert.AreEqual (10049, ex.ErrorCode, "#2");
  34. }
  35. try {
  36. using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) {
  37. s.Connect (ep);
  38. s.Close ();
  39. }
  40. Assert.Fail ("#3");
  41. } catch (SocketException ex) {
  42. Assert.AreEqual (10049, ex.ErrorCode, "#4");
  43. }
  44. }
  45. [Test]
  46. [Category ("InetAccess")]
  47. public void EndConnect ()
  48. {
  49. IPAddress ipOne = IPAddress.Parse (BogusAddress);
  50. IPEndPoint ipEP = new IPEndPoint (ipOne, BogusPort);
  51. Socket sock = new Socket (ipEP.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
  52. IAsyncResult ar = sock.BeginConnect (ipEP, null, null);
  53. bool gotException = false;
  54. try {
  55. sock.EndConnect (ar); // should raise an exception because connect was bogus
  56. } catch {
  57. gotException = true;
  58. }
  59. Assertion.AssertEquals ("A01", gotException, true);
  60. }
  61. [Test]
  62. [ExpectedException (typeof (ArgumentNullException))]
  63. public void SelectEmpty ()
  64. {
  65. ArrayList list = new ArrayList ();
  66. Socket.Select (list, list, list, 1000);
  67. }
  68. private bool BlockingConnect (bool block)
  69. {
  70. IPEndPoint ep = new IPEndPoint(IPAddress.Loopback, 1234);
  71. Socket server = new Socket(AddressFamily.InterNetwork,
  72. SocketType.Stream,
  73. ProtocolType.Tcp);
  74. server.Bind(ep);
  75. server.Blocking=block;
  76. server.Listen(0);
  77. Socket conn = new Socket (AddressFamily.InterNetwork,
  78. SocketType.Stream,
  79. ProtocolType.Tcp);
  80. conn.Connect (ep);
  81. Socket client = server.Accept();
  82. bool client_block = client.Blocking;
  83. client.Close();
  84. conn.Close();
  85. server.Close();
  86. return(client_block);
  87. }
  88. [Test]
  89. public void AcceptBlockingStatus()
  90. {
  91. bool block;
  92. block = BlockingConnect(true);
  93. Assertion.AssertEquals ("BlockingStatus01",
  94. block, true);
  95. block = BlockingConnect(false);
  96. Assertion.AssertEquals ("BlockingStatus02",
  97. block, false);
  98. }
  99. [Test]
  100. #if !NET_2_0
  101. [ExpectedException (typeof (ArgumentException))]
  102. #endif
  103. public void SetSocketOptionBoolean ()
  104. {
  105. IPEndPoint ep = new IPEndPoint (IPAddress.Loopback, 1);
  106. Socket sock = new Socket (ep.Address.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
  107. try {
  108. sock.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.KeepAlive, true);
  109. } finally {
  110. sock.Close ();
  111. }
  112. }
  113. }
  114. }