1 // System.Net.Sockets.NetworkStreamTest.cs
2 //
3 // Author:
4 //	Dick Porter (dick@ximian.com)
5 //
6 // Copyright (C) 2007 Novell, Inc (http://www.novell.com)
7 //
8 
9 using System.Net.Sockets;
10 using System.Net;
11 using System;
12 using System.IO;
13 using NUnit.Framework;
14 
15 
16 namespace MonoTests.System.Net.Sockets
17 {
18 	[TestFixture]
19 	[Category ("InetAccess")]
20 	public class NetworkStreamTest
21 	{
22 	        [Test]
23 		// See bug #371923
24 
25 #if FEATURE_NO_BSD_SOCKETS
26 		[ExpectedException (typeof (PlatformNotSupportedException))]
27 #else
28 		[ExpectedException(typeof(IOException))]
29 #endif
NetworkStreamConnection()30 		public void NetworkStreamConnection ()
31 		{
32 			IPEndPoint ipe = new IPEndPoint(Dns.GetHostEntry ("www.google.com").AddressList [0], 80);
33 			Socket s = new Socket(ipe.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
34 			s.Close ();
35 			NetworkStream ns = new NetworkStream (s);
36 		}
37 
38 		[Test]
39 #if FEATURE_NO_BSD_SOCKETS
40 		[ExpectedException (typeof (PlatformNotSupportedException))]
41 #endif
ReadTimeout()42 		public void ReadTimeout ()
43 		{
44 			Socket sock = new Socket (AddressFamily.InterNetwork,
45 						  SocketType.Stream,
46 						  ProtocolType.Tcp);
47 			Socket listen = new Socket (AddressFamily.InterNetwork,
48 						    SocketType.Stream,
49 						    ProtocolType.Tcp);
50 			IPEndPoint ep = new IPEndPoint (IPAddress.Loopback, 0);
51 
52 			listen.Bind (ep);
53 			listen.Listen (1);
54 
55 			sock.Connect (listen.LocalEndPoint);
56 
57 			NetworkStream stream = new NetworkStream (sock);
58 			stream.ReadTimeout = 1000;
59 
60 			byte[] buf = new byte[1024];
61 
62 			try {
63 				stream.Read (buf, 0, buf.Length);
64 				Assert.Fail ("ReadTimeout #1");
65 			} catch (IOException ex) {
66 				Exception inner = ex.InnerException;
67 				SocketException sockex = inner as SocketException;
68 
69 				Assert.IsNotNull (sockex, "ReadTimeout #2");
70 
71 /* Linux gives error 10035 (EWOULDBLOCK) here, whereas windows has 10060 (ETIMEDOUT)
72 				Assert.AreEqual (10060, sockex.ErrorCode, "ReadTimeout #3");
73 */
74 			} catch {
75 				Assert.Fail ("ReadTimeout #4");
76 			} finally {
77 				stream.Close ();
78 				sock.Close ();
79 				listen.Close ();
80 			}
81 		}
82 	}
83 }
84