NetworkStreamTest.cs 1.8 KB

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