Jo Shields 3c1f479b9d Imported Upstream version 4.0.0~alpha1
Former-commit-id: 806294f5ded97629b74c85c09952f2a74fe182d9
2015-04-07 09:35:12 +01:00

75 lines
1.8 KiB
C#

// System.Net.Sockets.NetworkStreamTest.cs
//
// Author:
// Dick Porter (dick@ximian.com)
//
// Copyright (C) 2007 Novell, Inc (http://www.novell.com)
//
using System.Net.Sockets;
using System.Net;
using System;
using System.IO;
using NUnit.Framework;
namespace MonoTests.System.Net.Sockets
{
[TestFixture]
public class NetworkStreamTest
{
[Test]
// See bug #371923
[ExpectedException(typeof(IOException))]
public void NetworkStreamConnection ()
{
IPEndPoint ipe = new IPEndPoint(Dns.GetHostEntry ("www.google.com").AddressList [0], 80);
Socket s = new Socket(ipe.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
s.Close ();
NetworkStream ns = new NetworkStream (s);
}
[Test]
public void ReadTimeout ()
{
Socket sock = new Socket (AddressFamily.InterNetwork,
SocketType.Stream,
ProtocolType.Tcp);
Socket listen = new Socket (AddressFamily.InterNetwork,
SocketType.Stream,
ProtocolType.Tcp);
IPEndPoint ep = new IPEndPoint (IPAddress.Loopback, 0);
listen.Bind (ep);
listen.Listen (1);
sock.Connect (listen.LocalEndPoint);
NetworkStream stream = new NetworkStream (sock);
stream.ReadTimeout = 1000;
byte[] buf = new byte[1024];
try {
stream.Read (buf, 0, buf.Length);
Assert.Fail ("ReadTimeout #1");
} catch (IOException ex) {
Exception inner = ex.InnerException;
SocketException sockex = inner as SocketException;
Assert.IsNotNull (sockex, "ReadTimeout #2");
/* Linux gives error 10035 (EWOULDBLOCK) here, whereas windows has 10060 (ETIMEDOUT)
Assert.AreEqual (10060, sockex.ErrorCode, "ReadTimeout #3");
*/
} catch {
Assert.Fail ("ReadTimeout #4");
} finally {
stream.Close ();
sock.Close ();
listen.Close ();
}
}
}
}