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