| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283 |
- //
- // SocketCas.cs - CAS unit tests for System.Net.WebRequest class
- //
- // Author:
- // Sebastien Pouliot <[email protected]>
- //
- // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
- //
- using NUnit.Framework;
- using System;
- using System.IO;
- using System.Net;
- using System.Net.Sockets;
- using System.Security;
- using System.Security.Permissions;
- using System.Text;
- using System.Threading;
- using MonoTests.System.Net.Sockets;
- namespace MonoCasTests.System.Net.Sockets {
- [TestFixture]
- [Category ("CAS")]
- public class SocketCas {
- private const int timeout = 30000;
- static ManualResetEvent reset;
- private string message;
- static Socket socket;
- static EndPoint ep;
- [TestFixtureSetUp]
- public void FixtureSetUp ()
- {
- reset = new ManualResetEvent (false);
- IPHostEntry host = Dns.Resolve ("www.google.com");
- IPAddress ip = host.AddressList[0];
- ep = new IPEndPoint (ip, 80);
- socket = new Socket (ip.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
- socket.Connect (ep);
- }
- [TestFixtureTearDown]
- public void FixtureTearDown ()
- {
- reset.Close ();
- }
- [SetUp]
- public void SetUp ()
- {
- if (!SecurityManager.SecurityEnabled)
- Assert.Ignore ("SecurityManager.SecurityEnabled is OFF");
- }
- // async tests (for stack propagation)
- private void AcceptCallback (IAsyncResult ar)
- {
- try {
- // can we do something bad here ?
- Assert.IsNotNull (Environment.GetEnvironmentVariable ("USERNAME"));
- message = "Expected a SecurityException";
- }
- catch (SecurityException) {
- message = null;
- reset.Set ();
- }
- catch (Exception e) {
- message = e.ToString ();
- }
- }
- [Test]
- [EnvironmentPermission (SecurityAction.Deny, Read = "USERNAME")]
- [Category ("InetAccess")]
- public void AsyncAccept ()
- {
- IPEndPoint ep = new IPEndPoint (IPAddress.Loopback, 16279);
- Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
- s.Bind (ep);
- s.Listen (0);
- message = "AsyncAccept";
- reset.Reset ();
- IAsyncResult r = s.BeginAccept (new AsyncCallback (AcceptCallback), s);
- Assert.IsNotNull (r, "IAsyncResult");
- Socket c = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
- c.Connect (ep);
- if (!reset.WaitOne (timeout, true))
- Assert.Ignore ("Timeout");
- Assert.IsNull (message, message);
- }
- private void ConnectCallback (IAsyncResult ar)
- {
- Socket s = (Socket)ar.AsyncState;
- s.EndConnect (ar);
- try {
- // can we do something bad here ?
- Assert.IsNotNull (Environment.GetEnvironmentVariable ("USERNAME"));
- message = "Expected a SecurityException";
- }
- catch (SecurityException) {
- message = null;
- reset.Set ();
- }
- catch (Exception e) {
- message = e.ToString ();
- }
- }
- [Test]
- [EnvironmentPermission (SecurityAction.Deny, Read = "USERNAME")]
- [Category ("InetAccess")]
- public void AsyncConnect ()
- {
- message = "AsyncConnect";
- reset.Reset ();
- Socket s = new Socket (ep.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
- IAsyncResult r = s.BeginConnect (ep, new AsyncCallback (ConnectCallback), s);
- Assert.IsNotNull (r, "IAsyncResult");
- if (!reset.WaitOne (timeout, true))
- Assert.Ignore ("Timeout");
- Assert.IsNull (message, message);
- }
- private void ReceiveCallback (IAsyncResult ar)
- {
- Socket s = (Socket)ar.AsyncState;
- s.EndReceive (ar);
- try {
- // can we do something bad here ?
- Assert.IsNotNull (Environment.GetEnvironmentVariable ("USERNAME"));
- message = "Expected a SecurityException";
- }
- catch (SecurityException) {
- message = null;
- reset.Set ();
- }
- catch (Exception e) {
- message = e.ToString ();
- }
- }
- [Test]
- [EnvironmentPermission (SecurityAction.Deny, Read = "USERNAME")]
- [Category ("InetAccess")]
- public void AsyncReceive ()
- {
- message = "AsyncReceive";
- reset.Reset ();
- NetworkStream ns = new NetworkStream (socket, false);
- StreamWriter sw = new StreamWriter (ns);
- sw.Write ("GET / HTTP/1.0\n\n");
- sw.Flush ();
- IAsyncResult r = socket.BeginReceive (new byte[1024], 0, 1024,
- SocketFlags.None, new AsyncCallback (ReceiveCallback), socket);
- Assert.IsNotNull (r, "IAsyncResult");
- if (!reset.WaitOne (timeout, true))
- Assert.Ignore ("Timeout");
- Assert.IsNull (message, message);
- }
- private void ReceiveFromCallback (IAsyncResult ar)
- {
- Socket s = (Socket)ar.AsyncState;
- s.EndReceiveFrom (ar, ref ep);
- try {
- // can we do something bad here ?
- Assert.IsNotNull (Environment.GetEnvironmentVariable ("USERNAME"));
- message = "Expected a SecurityException";
- }
- catch (SecurityException) {
- message = null;
- reset.Set ();
- }
- catch (Exception e) {
- message = e.ToString ();
- }
- }
- [Test]
- [EnvironmentPermission (SecurityAction.Deny, Read = "USERNAME")]
- [Category ("InetAccess")]
- public void AsyncReceiveFrom ()
- {
- message = "AsyncReceiveFrom";
- reset.Reset ();
- NetworkStream ns = new NetworkStream (socket, false);
- StreamWriter sw = new StreamWriter (ns);
- sw.Write ("GET / HTTP/1.0\n\n");
- sw.Flush ();
- IAsyncResult r = socket.BeginReceiveFrom (new byte[1024], 0, 1024,
- SocketFlags.None, ref ep, new AsyncCallback (ReceiveFromCallback), socket);
- Assert.IsNotNull (r, "IAsyncResult");
- if (!reset.WaitOne (timeout, true))
- Assert.Ignore ("Timeout");
- Assert.IsNull (message, message);
- }
- private void SendCallback (IAsyncResult ar)
- {
- Socket s = (Socket)ar.AsyncState;
- s.EndSend (ar);
- try {
- // can we do something bad here ?
- Assert.IsNotNull (Environment.GetEnvironmentVariable ("USERNAME"));
- message = "Expected a SecurityException";
- }
- catch (SecurityException) {
- message = null;
- reset.Set ();
- }
- catch (Exception e) {
- message = e.ToString ();
- }
- }
- [Test]
- [EnvironmentPermission (SecurityAction.Deny, Read = "USERNAME")]
- [Category ("InetAccess")]
- public void AsyncSend ()
- {
- message = "AsyncSend";
- reset.Reset ();
- byte[] get = Encoding.ASCII.GetBytes ("GET / HTTP/1.0\n\n");
- IAsyncResult r = socket.BeginSend (get, 0, get.Length, SocketFlags.None,
- new AsyncCallback (SendCallback), socket);
- Assert.IsNotNull (r, "IAsyncResult");
- if (!reset.WaitOne (timeout, true))
- Assert.Ignore ("Timeout");
- Assert.IsNull (message, message);
- }
- private void SendToCallback (IAsyncResult ar)
- {
- Socket s = (Socket)ar.AsyncState;
- s.EndSendTo (ar);
- try {
- // can we do something bad here ?
- Assert.IsNotNull (Environment.GetEnvironmentVariable ("USERNAME"));
- message = "Expected a SecurityException";
- }
- catch (SecurityException) {
- message = null;
- reset.Set ();
- }
- catch (Exception e) {
- message = e.ToString ();
- }
- }
- [Test]
- [EnvironmentPermission (SecurityAction.Deny, Read = "USERNAME")]
- [Category ("InetAccess")]
- public void AsyncSendTo ()
- {
- message = "AsyncSendTo";
- reset.Reset ();
- byte[] get = Encoding.ASCII.GetBytes ("GET / HTTP/1.0\n\n");
- IAsyncResult r = socket.BeginSendTo (get, 0, get.Length, SocketFlags.None,
- ep, new AsyncCallback (SendToCallback), socket);
- Assert.IsNotNull (r, "IAsyncResult");
- if (!reset.WaitOne (timeout, true))
- Assert.Ignore ("Timeout");
- Assert.IsNull (message, message);
- }
- }
- }
|