6747a263a6c966a549438d4989be1961db90cc6b
[mono.git] / mcs / class / System / Test / System.Net.Sockets / NetworkStreamCas.cs
1 //
2 // NetworkStreamCas.cs -CAS unit tests for System.Net.Sockets.NetworkStream
3 //
4 // Author:
5 //      Sebastien Pouliot  <sebastien@ximian.com>
6 //
7 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 // 
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 // 
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 //
28
29 using NUnit.Framework;
30
31 using System;
32 using System.IO;
33 using System.Net;
34 using System.Net.Sockets;
35 using System.Reflection;
36 using System.Security;
37 using System.Security.Permissions;
38 using System.Threading;
39 using System.Text;
40
41 namespace MonoCasTests.System.Net.Sockets {
42
43         [TestFixture]
44         [Category ("CAS")]
45         public class NetworkStreamCas {
46
47                 private const int timeout = 30000;
48                 private string message;
49
50                 static ManualResetEvent reset;
51                 static Socket socket;
52
53                 [TestFixtureSetUp]
54                 public void FixtureSetUp ()
55                 {
56                         reset = new ManualResetEvent (false);
57
58                         IPHostEntry host = Dns.Resolve ("www.google.com");
59                         IPAddress ip = host.AddressList[0];
60                         socket = new Socket (ip.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
61                         socket.Connect (new IPEndPoint (ip, 80));
62                 }
63
64                 [TestFixtureTearDown]
65                 public void FixtureTearDown ()
66                 {
67                         reset.Close ();
68                         socket.Close ();
69                 }
70
71                 [SetUp]
72                 public void SetUp ()
73                 {
74                         if (!SecurityManager.SecurityEnabled)
75                                 Assert.Ignore ("SecurityManager.SecurityEnabled is OFF");
76                 }
77
78                 // async tests (for stack propagation)
79
80                 private void ReadCallback (IAsyncResult ar)
81                 {
82                         NetworkStream s = (NetworkStream)ar.AsyncState;
83                         s.EndRead (ar);
84                         try {
85                                 // can we do something bad here ?
86                                 Assert.IsNotNull (Environment.GetEnvironmentVariable ("USERNAME"));
87                                 message = "Expected a SecurityException";
88                         }
89                         catch (SecurityException) {
90                                 message = null;
91                                 reset.Set ();
92                         }
93                         catch (Exception e) {
94                                 message = e.ToString ();
95                         }
96                 }
97
98                 [Test]
99                 [EnvironmentPermission (SecurityAction.Deny, Read = "USERNAME")]
100                 [Category ("InetAccess")]
101                 public void AsyncRead ()
102                 {
103                         message = "AsyncRead";
104                         reset.Reset ();
105
106                         NetworkStream ns = new NetworkStream (socket, false);
107                         StreamWriter sw = new StreamWriter (ns);
108                         sw.Write ("GET / HTTP/1.0\n\n");
109                         sw.Flush ();
110
111                         IAsyncResult r = ns.BeginRead (new byte [1024], 0, 1024, new AsyncCallback (ReadCallback), ns);
112                         Assert.IsNotNull (r, "IAsyncResult");
113                         if (!reset.WaitOne (timeout, true))
114                                 Assert.Ignore ("Timeout");
115                         Assert.IsNull (message, message);
116                         ns.Close ();
117                 }
118
119                 private void WriteCallback (IAsyncResult ar)
120                 {
121                         NetworkStream s = (NetworkStream)ar.AsyncState;
122                         s.EndWrite (ar);
123                         try {
124                                 // can we do something bad here ?
125                                 Assert.IsNotNull (Environment.GetEnvironmentVariable ("USERNAME"));
126                                 message = "Expected a SecurityException";
127                         }
128                         catch (SecurityException) {
129                                 message = null;
130                                 reset.Set ();
131                         }
132                         catch (Exception e) {
133                                 message = e.ToString ();
134                         }
135                 }
136
137                 [Test]
138                 [EnvironmentPermission (SecurityAction.Deny, Read = "USERNAME")]
139                 [Category ("InetAccess")]
140                 public void AsyncWrite ()
141                 {
142                         message = "AsyncWrite";
143                         reset.Reset ();
144
145                         NetworkStream ns = new NetworkStream (socket, false);
146                         byte[] get = Encoding.ASCII.GetBytes ("GET / HTTP/1.0\n\n");
147                         IAsyncResult r = ns.BeginWrite (get, 0, get.Length, new AsyncCallback (WriteCallback), ns);
148                         Assert.IsNotNull (r, "IAsyncResult");
149                         if (!reset.WaitOne (timeout, true))
150                                 Assert.Ignore ("Timeout");
151                         Assert.IsNull (message, message);
152                         ns.Close ();
153                 }
154         }
155 }