NetworkStreamTest.cs 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. public class NetworkStreamTest
  17. {
  18. [Test]
  19. // See bug #371923
  20. #if FEATURE_NO_BSD_SOCKETS
  21. [ExpectedException (typeof (PlatformNotSupportedException))]
  22. #else
  23. [ExpectedException(typeof(IOException))]
  24. #endif
  25. public void NetworkStreamConnection ()
  26. {
  27. IPEndPoint ipe = new IPEndPoint(Dns.GetHostEntry ("www.google.com").AddressList [0], 80);
  28. Socket s = new Socket(ipe.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
  29. s.Close ();
  30. NetworkStream ns = new NetworkStream (s);
  31. }
  32. [Test]
  33. #if FEATURE_NO_BSD_SOCKETS
  34. [ExpectedException (typeof (PlatformNotSupportedException))]
  35. #endif
  36. public void ReadTimeout ()
  37. {
  38. Socket sock = new Socket (AddressFamily.InterNetwork,
  39. SocketType.Stream,
  40. ProtocolType.Tcp);
  41. Socket listen = new Socket (AddressFamily.InterNetwork,
  42. SocketType.Stream,
  43. ProtocolType.Tcp);
  44. IPEndPoint ep = new IPEndPoint (IPAddress.Loopback, 0);
  45. listen.Bind (ep);
  46. listen.Listen (1);
  47. sock.Connect (listen.LocalEndPoint);
  48. NetworkStream stream = new NetworkStream (sock);
  49. stream.ReadTimeout = 1000;
  50. byte[] buf = new byte[1024];
  51. try {
  52. stream.Read (buf, 0, buf.Length);
  53. Assert.Fail ("ReadTimeout #1");
  54. } catch (IOException ex) {
  55. Exception inner = ex.InnerException;
  56. SocketException sockex = inner as SocketException;
  57. Assert.IsNotNull (sockex, "ReadTimeout #2");
  58. /* Linux gives error 10035 (EWOULDBLOCK) here, whereas windows has 10060 (ETIMEDOUT)
  59. Assert.AreEqual (10060, sockex.ErrorCode, "ReadTimeout #3");
  60. */
  61. } catch {
  62. Assert.Fail ("ReadTimeout #4");
  63. } finally {
  64. stream.Close ();
  65. sock.Close ();
  66. listen.Close ();
  67. }
  68. }
  69. }
  70. }