[System*] Throw a PlatformNotSupported exception when using the networking stack...
[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
24 #if FEATURE_NO_BSD_SOCKETS
25                 [ExpectedException (typeof (PlatformNotSupportedException))]
26 #else
27                 [ExpectedException(typeof(IOException))]
28 #endif
29                 public void NetworkStreamConnection ()
30                 {
31                         IPEndPoint ipe = new IPEndPoint(Dns.GetHostEntry ("www.google.com").AddressList [0], 80);
32                         Socket s = new Socket(ipe.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
33                         s.Close ();
34                         NetworkStream ns = new NetworkStream (s);
35                 }
36                 
37                 [Test]
38 #if FEATURE_NO_BSD_SOCKETS
39                 [ExpectedException (typeof (PlatformNotSupportedException))]
40 #endif
41                 public void ReadTimeout ()
42                 {
43                         Socket sock = new Socket (AddressFamily.InterNetwork,
44                                                   SocketType.Stream,
45                                                   ProtocolType.Tcp);
46                         Socket listen = new Socket (AddressFamily.InterNetwork,
47                                                     SocketType.Stream,
48                                                     ProtocolType.Tcp);
49                         IPEndPoint ep = new IPEndPoint (IPAddress.Loopback, 0);
50                         
51                         listen.Bind (ep);
52                         listen.Listen (1);
53                         
54                         sock.Connect (listen.LocalEndPoint);
55                         
56                         NetworkStream stream = new NetworkStream (sock);
57                         stream.ReadTimeout = 1000;
58
59                         byte[] buf = new byte[1024];
60                         
61                         try {
62                                 stream.Read (buf, 0, buf.Length);
63                                 Assert.Fail ("ReadTimeout #1");
64                         } catch (IOException ex) {
65                                 Exception inner = ex.InnerException;
66                                 SocketException sockex = inner as SocketException;
67                                 
68                                 Assert.IsNotNull (sockex, "ReadTimeout #2");
69
70 /* Linux gives error 10035 (EWOULDBLOCK) here, whereas windows has 10060 (ETIMEDOUT)
71                                 Assert.AreEqual (10060, sockex.ErrorCode, "ReadTimeout #3");
72 */
73                         } catch {
74                                 Assert.Fail ("ReadTimeout #4");
75                         } finally {
76                                 stream.Close ();
77                                 sock.Close ();
78                                 listen.Close ();
79                         }
80                 }
81         }
82 }