In System.Net.Sockets:
[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 #if NET_2_0
22                 [Test]
23                 public void ReadTimeout ()
24                 {
25                         Socket sock = new Socket (AddressFamily.InterNetwork,
26                                                   SocketType.Stream,
27                                                   ProtocolType.Tcp);
28                         Socket listen = new Socket (AddressFamily.InterNetwork,
29                                                     SocketType.Stream,
30                                                     ProtocolType.Tcp);
31                         IPEndPoint ep = new IPEndPoint (IPAddress.Loopback, 0);
32                         
33                         listen.Bind (ep);
34                         listen.Listen (1);
35                         
36                         sock.Connect (listen.LocalEndPoint);
37                         
38                         NetworkStream stream = new NetworkStream (sock);
39                         stream.ReadTimeout = 1000;
40
41                         byte[] buf = new byte[1024];
42                         
43                         try {
44                                 stream.Read (buf, 0, buf.Length);
45                                 Assert.Fail ("ReadTimeout #1");
46                         } catch (IOException ex) {
47                                 Exception inner = ex.InnerException;
48                                 SocketException sockex = inner as SocketException;
49                                 
50                                 Assert.IsNotNull (sockex, "ReadTimeout #2");
51
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 }