NetworkStreamTest.cs 1.8 KB

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