Переглянути джерело

2005-05-20 Sebastien Pouliot <[email protected]>

	* NetworkStreamCas.cs: New. CAS unit tests for NetworkStream.
	* SocketCas.cs: New. CAS unit tests for Socket.
	* SocketTest.cs: Make some fields public so they can be reused.
	* TcpClientCas.cs: New. CAS unit tests for TcpClient (currently 
	commented as the Begin* methods aren't yet implemented).


svn path=/trunk/mcs/; revision=44814
Sebastien Pouliot 20 роки тому
батько
коміт
2dd68345bd

+ 8 - 0
mcs/class/System/Test/System.Net.Sockets/ChangeLog

@@ -1,3 +1,11 @@
+2005-05-20  Sebastien Pouliot  <[email protected]>
+
+	* NetworkStreamCas.cs: New. CAS unit tests for NetworkStream.
+	* SocketCas.cs: New. CAS unit tests for Socket.
+	* SocketTest.cs: Make some fields public so they can be reused.
+	* TcpClientCas.cs: New. CAS unit tests for TcpClient (currently 
+	commented as the Begin* methods aren't yet implemented).
+
 2005-05-06 Gonzalo Paniagua Javier <[email protected]>
 
 	* SocketTest.cs: added test for setting a boolean socket option.

+ 155 - 0
mcs/class/System/Test/System.Net.Sockets/NetworkStreamCas.cs

@@ -0,0 +1,155 @@
+//
+// NetworkStreamCas.cs -CAS unit tests for System.Net.Sockets.NetworkStream
+//
+// Author:
+//	Sebastien Pouliot  <[email protected]>
+//
+// Copyright (C) 2005 Novell, Inc (http://www.novell.com)
+//
+// Permission is hereby granted, free of charge, to any person obtaining
+// a copy of this software and associated documentation files (the
+// "Software"), to deal in the Software without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Software, and to
+// permit persons to whom the Software is furnished to do so, subject to
+// the following conditions:
+// 
+// The above copyright notice and this permission notice shall be
+// included in all copies or substantial portions of the Software.
+// 
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+//
+
+using NUnit.Framework;
+
+using System;
+using System.IO;
+using System.Net;
+using System.Net.Sockets;
+using System.Reflection;
+using System.Security;
+using System.Security.Permissions;
+using System.Threading;
+using System.Text;
+
+namespace MonoCasTests.System.Net.Sockets {
+
+	[TestFixture]
+	[Category ("CAS")]
+	public class NetworkStreamCas {
+
+		private const int timeout = 30000;
+		private string message;
+
+		static ManualResetEvent reset;
+		static Socket socket;
+
+		[TestFixtureSetUp]
+		public void FixtureSetUp ()
+		{
+			reset = new ManualResetEvent (false);
+
+			IPHostEntry host = Dns.Resolve ("www.google.com");
+			IPAddress ip = host.AddressList[0];
+			socket = new Socket (ip.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
+			socket.Connect (new IPEndPoint (ip, 80));
+		}
+
+		[TestFixtureTearDown]
+		public void FixtureTearDown ()
+		{
+			reset.Close ();
+			socket.Close ();
+		}
+
+		[SetUp]
+		public void SetUp ()
+		{
+			if (!SecurityManager.SecurityEnabled)
+				Assert.Ignore ("SecurityManager.SecurityEnabled is OFF");
+		}
+
+		// async tests (for stack propagation)
+
+		private void ReadCallback (IAsyncResult ar)
+		{
+			NetworkStream s = (NetworkStream)ar.AsyncState;
+			s.EndRead (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 AsyncRead ()
+		{
+			message = "AsyncRead";
+			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 = ns.BeginRead (new byte [1024], 0, 1024, new AsyncCallback (ReadCallback), ns);
+			Assert.IsNotNull (r, "IAsyncResult");
+			if (!reset.WaitOne (timeout, true))
+				Assert.Ignore ("Timeout");
+			Assert.IsNull (message, message);
+			ns.Close ();
+		}
+
+		private void WriteCallback (IAsyncResult ar)
+		{
+			NetworkStream s = (NetworkStream)ar.AsyncState;
+			s.EndWrite (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 AsyncWrite ()
+		{
+			message = "AsyncWrite";
+			reset.Reset ();
+
+			NetworkStream ns = new NetworkStream (socket, false);
+			byte[] get = Encoding.ASCII.GetBytes ("GET / HTTP/1.0\n\n");
+			IAsyncResult r = ns.BeginWrite (get, 0, get.Length, new AsyncCallback (WriteCallback), ns);
+			Assert.IsNotNull (r, "IAsyncResult");
+			if (!reset.WaitOne (timeout, true))
+				Assert.Ignore ("Timeout");
+			Assert.IsNull (message, message);
+			ns.Close ();
+		}
+	}
+}

+ 283 - 0
mcs/class/System/Test/System.Net.Sockets/SocketCas.cs

@@ -0,0 +1,283 @@
+//
+// 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);
+		}
+	}
+}

+ 7 - 3
mcs/class/System/Test/System.Net.Sockets/SocketTest.cs

@@ -1,4 +1,4 @@
-// System.Net.Sockets.TcpClientTest.cs
+// System.Net.Sockets.SocketTest.cs
 //
 // Authors:
 //    Brad Fitzpatrick ([email protected])
@@ -17,12 +17,16 @@ namespace MonoTests.System.Net.Sockets
 	[TestFixture]
 	public class SocketTest
 	{
+		// note: also used in SocketCas tests
+		public const string BogusAddress = "192.168.244.244";
+		public const int BogusPort = 23483;
+
 		[Test]
 		[Category ("InetAccess")]
 		public void EndConnect ()
 		{
-		    IPAddress ipOne = IPAddress.Parse ("192.168.244.244");   // something bogus
-		    IPEndPoint ipEP = new IPEndPoint (ipOne, 23483);  // something bogus
+		    IPAddress ipOne = IPAddress.Parse (BogusAddress);
+		    IPEndPoint ipEP = new IPEndPoint (ipOne, BogusPort);
 		    Socket sock = new Socket (ipEP.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
 		    IAsyncResult ar = sock.BeginConnect (ipEP, null, null);
 		    bool gotException = false;

+ 123 - 0
mcs/class/System/Test/System.Net.Sockets/TcpClientCas.cs

@@ -0,0 +1,123 @@
+//
+// TcpClientCas.cs - CAS unit tests for System.Net.Sockets.TcpClient 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.Threading;
+
+using MonoTests.System.Net.Sockets;
+
+namespace MonoCasTests.System.Net.Sockets {
+
+	[TestFixture]
+	[Category ("CAS")]
+	public class TcpClientCas {
+
+		private const int timeout = 30000;
+
+		static ManualResetEvent reset;
+		private string message;
+		private string uri = "http://www.google.com";
+
+		[TestFixtureSetUp]
+		public void FixtureSetUp ()
+		{
+			reset = new ManualResetEvent (false);
+		}
+
+		[TestFixtureTearDown]
+		public void FixtureTearDown ()
+		{
+			reset.Close ();
+		}
+
+		[SetUp]
+		public void SetUp ()
+		{
+			if (!SecurityManager.SecurityEnabled)
+				Assert.Ignore ("SecurityManager.SecurityEnabled is OFF");
+		}
+
+		// async tests (for stack propagation)
+#if NET_2_0
+/* Oops - not yet implemented in Mono
+		private void ConnectCallback (IAsyncResult ar)
+		{
+			TcpClient c = (TcpClient)ar.AsyncState;
+			c.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_StringIntAsyncCallbackObject ()
+		{
+			TcpClient s = new TcpClient ();
+			message = "AsyncConnect";
+			reset.Reset ();
+			IAsyncResult r = s.BeginConnect ("www.google.com", 80, new AsyncCallback (ConnectCallback), s);
+			Assert.IsNotNull (r, "IAsyncResult");
+			if (!reset.WaitOne (timeout, true))
+				Assert.Ignore ("Timeout");
+			Assert.IsNull (message, message);
+		}
+
+		[Test]
+		[EnvironmentPermission (SecurityAction.Deny, Read = "USERNAME")]
+		[Category ("InetAccess")]
+		public void AsyncConnect_IPAddressIntAsyncCallbackObject ()
+		{
+			IPHostEntry host = Dns.Resolve ("www.google.com");
+			TcpClient s = new TcpClient ();
+			message = "AsyncConnect";
+			reset.Reset ();
+			IAsyncResult r = s.BeginConnect (host.AddressList[0], 80, new AsyncCallback (ConnectCallback), s);
+			Assert.IsNotNull (r, "IAsyncResult");
+			if (!reset.WaitOne (timeout, true))
+				Assert.Ignore ("Timeout");
+			Assert.IsNull (message, message);
+		}
+
+		[Test]
+		[EnvironmentPermission (SecurityAction.Deny, Read = "USERNAME")]
+		[Category ("InetAccess")]
+		public void AsyncConnect_IPAddressArrayIntAsyncCallbackObject ()
+		{
+			IPHostEntry host = Dns.Resolve ("www.google.com");
+			TcpClient s = new TcpClient ();
+			message = "AsyncConnect";
+			reset.Reset ();
+			IAsyncResult r = s.BeginConnect (host.AddressList, 80, new AsyncCallback (ConnectCallback), s);
+			Assert.IsNotNull (r, "IAsyncResult");
+			if (!reset.WaitOne (timeout, true))
+				Assert.Ignore ("Timeout");
+			Assert.IsNull (message, message);
+		}
+*/
+#endif
+	}
+}