2 // NetworkStreamCas.cs -CAS unit tests for System.Net.Sockets.NetworkStream
5 // Sebastien Pouliot <sebastien@ximian.com>
7 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
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:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
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.
29 using NUnit.Framework;
34 using System.Net.Sockets;
35 using System.Reflection;
36 using System.Security;
37 using System.Security.Permissions;
38 using System.Threading;
41 namespace MonoCasTests.System.Net.Sockets {
45 public class NetworkStreamCas {
47 private const int timeout = 30000;
48 private string message;
50 static ManualResetEvent reset;
54 public void FixtureSetUp ()
56 reset = new ManualResetEvent (false);
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));
65 public void FixtureTearDown ()
74 if (!SecurityManager.SecurityEnabled)
75 Assert.Ignore ("SecurityManager.SecurityEnabled is OFF");
78 // async tests (for stack propagation)
80 private void ReadCallback (IAsyncResult ar)
82 NetworkStream s = (NetworkStream)ar.AsyncState;
85 // can we do something bad here ?
86 Assert.IsNotNull (Environment.GetEnvironmentVariable ("USERNAME"));
87 message = "Expected a SecurityException";
89 catch (SecurityException) {
94 message = e.ToString ();
99 [EnvironmentPermission (SecurityAction.Deny, Read = "USERNAME")]
100 [Category ("InetAccess")]
101 public void AsyncRead ()
103 message = "AsyncRead";
106 NetworkStream ns = new NetworkStream (socket, false);
107 StreamWriter sw = new StreamWriter (ns);
108 sw.Write ("GET / HTTP/1.0\n\n");
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);
119 private void WriteCallback (IAsyncResult ar)
121 NetworkStream s = (NetworkStream)ar.AsyncState;
124 // can we do something bad here ?
125 Assert.IsNotNull (Environment.GetEnvironmentVariable ("USERNAME"));
126 message = "Expected a SecurityException";
128 catch (SecurityException) {
132 catch (Exception e) {
133 message = e.ToString ();
138 [EnvironmentPermission (SecurityAction.Deny, Read = "USERNAME")]
139 [Category ("InetAccess")]
140 public void AsyncWrite ()
142 message = "AsyncWrite";
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);