NetworkStreamTest.cs 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. // System.Net.Sockets.NetworkStreamTest.cs
  2. //
  3. // Author:
  4. // Dick Porter ([email protected])
  5. //
  6. // Copyright (C) 2007 Novell, Inc (http://www.novell.com)
  7. //
  8. using System.Net.Sockets;
  9. using System.Net;
  10. using System;
  11. using System.IO;
  12. using NUnit.Framework;
  13. namespace MonoTests.System.Net.Sockets
  14. {
  15. [TestFixture]
  16. [Category ("InetAccess")]
  17. public class NetworkStreamTest
  18. {
  19. [Test]
  20. // See bug #371923
  21. #if FEATURE_NO_BSD_SOCKETS
  22. [ExpectedException (typeof (PlatformNotSupportedException))]
  23. #else
  24. [ExpectedException(typeof(IOException))]
  25. #endif
  26. public void NetworkStreamConnection ()
  27. {
  28. IPEndPoint ipe = new IPEndPoint(Dns.GetHostEntry ("www.google.com").AddressList [0], 80);
  29. Socket s = new Socket(ipe.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
  30. s.Close ();
  31. NetworkStream ns = new NetworkStream (s);
  32. }
  33. [Test]
  34. #if FEATURE_NO_BSD_SOCKETS
  35. [ExpectedException (typeof (PlatformNotSupportedException))]
  36. #endif
  37. public void ReadTimeout ()
  38. {
  39. Socket sock = new Socket (AddressFamily.InterNetwork,
  40. SocketType.Stream,
  41. ProtocolType.Tcp);
  42. Socket listen = new Socket (AddressFamily.InterNetwork,
  43. SocketType.Stream,
  44. ProtocolType.Tcp);
  45. IPEndPoint ep = new IPEndPoint (IPAddress.Loopback, 0);
  46. listen.Bind (ep);
  47. listen.Listen (1);
  48. sock.Connect (listen.LocalEndPoint);
  49. NetworkStream stream = new NetworkStream (sock);
  50. stream.ReadTimeout = 1000;
  51. byte[] buf = new byte[1024];
  52. try {
  53. stream.Read (buf, 0, buf.Length);
  54. Assert.Fail ("ReadTimeout #1");
  55. } catch (IOException ex) {
  56. Exception inner = ex.InnerException;
  57. SocketException sockex = inner as SocketException;
  58. Assert.IsNotNull (sockex, "ReadTimeout #2");
  59. /* Linux gives error 10035 (EWOULDBLOCK) here, whereas windows has 10060 (ETIMEDOUT)
  60. Assert.AreEqual (10060, sockex.ErrorCode, "ReadTimeout #3");
  61. */
  62. } catch {
  63. Assert.Fail ("ReadTimeout #4");
  64. } finally {
  65. stream.Close ();
  66. sock.Close ();
  67. listen.Close ();
  68. }
  69. }
  70. }
  71. }