NetworkStreamTest.cs 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. Assertion.AssertEquals ("ReadTimeout #3",
  54. 10060,
  55. sockex.ErrorCode);
  56. */
  57. } catch {
  58. Assert.Fail ("ReadTimeout #4");
  59. } finally {
  60. stream.Close ();
  61. sock.Close ();
  62. listen.Close ();
  63. }
  64. }
  65. #endif
  66. }
  67. }