SocketTest.cs 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. // System.Net.Sockets.TcpClientTest.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. [Test]
  19. [Category ("InetAccess")]
  20. public void EndConnect ()
  21. {
  22. IPAddress ipOne = IPAddress.Parse ("192.168.244.244"); // something bogus
  23. IPEndPoint ipEP = new IPEndPoint (ipOne, 23483); // something bogus
  24. Socket sock = new Socket (ipEP.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
  25. IAsyncResult ar = sock.BeginConnect (ipEP, null, null);
  26. bool gotException = false;
  27. try {
  28. sock.EndConnect (ar); // should raise an exception because connect was bogus
  29. } catch {
  30. gotException = true;
  31. }
  32. Assertion.AssertEquals ("A01", gotException, true);
  33. }
  34. [Test]
  35. [ExpectedException (typeof (ArgumentNullException))]
  36. public void SelectEmpty ()
  37. {
  38. ArrayList list = new ArrayList ();
  39. Socket.Select (list, list, list, 1000);
  40. }
  41. private bool BlockingConnect (bool block)
  42. {
  43. IPEndPoint ep = new IPEndPoint(IPAddress.Any, 1234);
  44. Socket server = new Socket(AddressFamily.InterNetwork,
  45. SocketType.Stream,
  46. ProtocolType.Tcp);
  47. server.Bind(ep);
  48. server.Blocking=block;
  49. server.Listen(0);
  50. Socket conn = new Socket (AddressFamily.InterNetwork,
  51. SocketType.Stream,
  52. ProtocolType.Tcp);
  53. conn.Connect (ep);
  54. Socket client = server.Accept();
  55. bool client_block = client.Blocking;
  56. client.Close();
  57. conn.Close();
  58. server.Close();
  59. return(client_block);
  60. }
  61. [Test]
  62. [Category("NotDotNet")]
  63. public void AcceptBlockingStatus()
  64. {
  65. bool block;
  66. block = BlockingConnect(true);
  67. Assertion.AssertEquals ("BlockingStatus01",
  68. block, true);
  69. block = BlockingConnect(false);
  70. Assertion.AssertEquals ("BlockingStatus02",
  71. block, false);
  72. }
  73. [Test]
  74. #if !NET_2_0
  75. [ExpectedException (typeof (ArgumentException))]
  76. #endif
  77. public void SetSocketOptionBoolean ()
  78. {
  79. IPEndPoint ep = new IPEndPoint (IPAddress.Loopback, 1);
  80. Socket sock = new Socket (ep.Address.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
  81. try {
  82. sock.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.KeepAlive, true);
  83. } finally {
  84. sock.Close ();
  85. }
  86. }
  87. }
  88. }