Browse Source

In Test/System.Net.Sockets:
2009-09-26 Sebastien Pouliot <[email protected]>

* SocketAsyncEventArgsTest.cs: New. Unit tests

In System.Net.Sockets:
2009-09-26 Sebastien Pouliot <[email protected]>

* SocketAsyncEventArgs.cs: Fix paramater validations and default
values

In .:
2009-09-26 Sebastien Pouliot <[email protected]>

* System_test.dll.sources: Add unit tests for SocketAsyncEventArgs


svn path=/trunk/mcs/; revision=142675

Sebastien Pouliot 16 years ago
parent
commit
0328ff83e6

+ 4 - 0
mcs/class/System/ChangeLog

@@ -1,3 +1,7 @@
+2009-09-26  Sebastien Pouliot  <[email protected]>
+
+	* System_test.dll.sources: Add unit tests for SocketAsyncEventArgs
+
 2009-09-21  Sebastien Pouliot  <[email protected]>
 
 	* net_2_1_raw_System.dll.sources: Remove System.Net.Mime since

+ 5 - 0
mcs/class/System/System.Net.Sockets/ChangeLog

@@ -1,3 +1,8 @@
+2009-09-26  Sebastien Pouliot  <[email protected]>
+
+	* SocketAsyncEventArgs.cs: Fix paramater validations and default 
+	values
+
 2009-09-23  Sebastien Pouliot  <[email protected]>
 
 	* Socket_2_1.cs: Remove NET_2_1 socket policy checks from here.

+ 7 - 6
mcs/class/System/System.Net.Sockets/SocketAsyncEventArgs.cs

@@ -82,6 +82,7 @@ namespace System.Net.Sockets
 		public SendPacketsElement[] SendPacketsElements { get; set; }
 		public TransmitFileOptions SendPacketsFlags { get; set; }
 #endif
+		[MonoTODO ("unused property")]
 		public int SendPacketsSendSize { get; set; }
 		public SocketError SocketError { get; set; }
 		public SocketFlags SocketFlags { get; set; }
@@ -124,7 +125,7 @@ namespace System.Net.Sockets
 			SendPacketsElements = null;
 			SendPacketsFlags = TransmitFileOptions.UseDefaultWorkerThread;
 #endif
-			SendPacketsSendSize = 0;
+			SendPacketsSendSize = -1;
 			SocketError = SocketError.Success;
 			SocketFlags = SocketFlags.None;
 			UserToken = null;
@@ -176,15 +177,15 @@ namespace System.Net.Sockets
 					throw new ArgumentException ("Buffer and BufferList properties cannot both be non-null.");
 				
 				int buflen = buffer.Length;
-				if (offset < 0 || offset >= buflen)
+				if (offset < 0 || (offset != 0 && offset >= buflen))
 					throw new ArgumentOutOfRangeException ("offset");
 
-				if (count < 0 || count + offset > buflen)
+				if (count < 0 || count > buflen - offset)
 					throw new ArgumentOutOfRangeException ("count");
-			}
 
-			Count = count;
-			Offset = offset;
+				Count = count;
+				Offset = offset;
+			}
 			Buffer = buffer;
 		}
 

+ 1 - 0
mcs/class/System/System_test.dll.sources

@@ -230,6 +230,7 @@ System.Net.Sockets/NetworkStreamTest.cs
 System.Net.Sockets/TcpClientTest.cs
 System.Net.Sockets/TcpListenerTest.cs
 System.Net.Sockets/SocketTest.cs
+System.Net.Sockets/SocketAsyncEventArgsTest.cs
 System.Net.Sockets/UdpClientTest.cs
 System.Net.Mail/LinkedResourceTest.cs
 System.Net.Mail/AttachmentCollectionTest.cs

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

@@ -1,3 +1,7 @@
+2009-09-26  Sebastien Pouliot  <[email protected]>
+
+	* SocketAsyncEventArgsTest.cs: New. Unit tests
+
 2009-06-28  Gert Driesen  <[email protected]>
 
 	* SocketTest.cs: Added tests for argument checks, and improved tests

+ 253 - 0
mcs/class/System/Test/System.Net.Sockets/SocketAsyncEventArgsTest.cs

@@ -0,0 +1,253 @@
+//
+// SocketAsyncEventArgsTest.cs - NUnit Test Cases for SocketAsyncEventArgs
+//
+// Author:
+//	Sebastien Pouliot ([email protected])
+//
+// Copyright (C) 2009 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.
+//
+
+#if NET_2_0
+
+using System;
+using System.Collections.Generic;
+using System.Net.Sockets;
+using System.Text;
+
+using NUnit.Framework;
+
+namespace MonoTests.System.Net.Sockets {
+
+	[TestFixture]
+	public class SocketAsyncEventArgsTest {
+
+		[Test]
+		public void Defaults ()
+		{
+			SocketAsyncEventArgs saea = new SocketAsyncEventArgs ();
+			Assert.IsNull (saea.AcceptSocket, "AcceptSocket");
+			Assert.IsNull (saea.Buffer, "Buffer");
+			Assert.IsNull (saea.BufferList, "BufferList");
+			Assert.AreEqual (0, saea.BytesTransferred, "BytesTransferred");
+			Assert.AreEqual (0, saea.Count, "Count");
+			Assert.IsFalse (saea.DisconnectReuseSocket, "DisconnectReuseSocket");
+			Assert.AreEqual (SocketAsyncOperation.None, saea.LastOperation, "LastOperation");
+			Assert.AreEqual (0, saea.Offset, "Offset");
+			Assert.IsNotNull (saea.ReceiveMessageFromPacketInfo, "ReceiveMessageFromPacketInfo");
+			Assert.IsNull (saea.RemoteEndPoint, "RemoteEndPoint");
+			Assert.IsNull (saea.SendPacketsElements, "SendPacketsElements");
+			Assert.AreEqual (TransmitFileOptions.UseDefaultWorkerThread, saea.SendPacketsFlags, "SendPacketsFlags");
+			Assert.AreEqual (-1, saea.SendPacketsSendSize, "SendPacketsSendSize");
+			Assert.AreEqual (SocketError.Success, saea.SocketError, "SocketError");
+			Assert.AreEqual (SocketFlags.None, saea.SocketFlags, "SocketFlags");
+			Assert.IsNull (saea.UserToken, "UserToken");
+
+			saea.Dispose ();
+			saea.Dispose (); // twice
+		}
+
+		[Test]
+		public void SetBuffer_ByteArray ()
+		{
+			SocketAsyncEventArgs saea = new SocketAsyncEventArgs ();
+
+			byte [] buffer = new byte [0];
+			saea.SetBuffer (buffer, 0, 0);
+			Assert.AreEqual (0, saea.Buffer.Length, "0");
+			Assert.AreSame (saea.Buffer, buffer, "same");
+
+			saea.SetBuffer (null, 0, 0);
+			Assert.IsNull (saea.Buffer, "null");
+		}
+
+		[Test]
+		[ExpectedException (typeof (ArgumentException))]
+		public void SetBuffer_BufferList_ByteArray ()
+		{
+			SocketAsyncEventArgs saea = new SocketAsyncEventArgs ();
+			saea.BufferList = new List<ArraySegment<byte>> ();
+			saea.SetBuffer (new byte [0], 0, 0);
+		}
+
+		[Test]
+		public void SetBuffer_BufferList_NullByteArray ()
+		{
+			SocketAsyncEventArgs saea = new SocketAsyncEventArgs ();
+			saea.BufferList = new List<ArraySegment<byte>> ();
+			saea.SetBuffer (null, 0, 0);
+		}
+
+		[Test]
+		[ExpectedException (typeof (ArgumentException))]
+		public void SetBuffer_ByteArray_BufferList ()
+		{
+			SocketAsyncEventArgs saea = new SocketAsyncEventArgs ();
+			saea.SetBuffer (new byte [0], 0, 0);
+			saea.BufferList = new List<ArraySegment<byte>> ();
+		}
+
+		[Test]
+		public void SetBuffer_NullByteArray_BufferList ()
+		{
+			SocketAsyncEventArgs saea = new SocketAsyncEventArgs ();
+			saea.SetBuffer (null, 0, 0);
+			saea.BufferList = new List<ArraySegment<byte>> ();
+			Assert.IsNull (saea.Buffer, "Buffer");
+		}
+
+		[Test]
+		[ExpectedException (typeof (ArgumentOutOfRangeException))]
+		public void SetBuffer_ByteArray_StartNegative ()
+		{
+			SocketAsyncEventArgs saea = new SocketAsyncEventArgs ();
+			saea.SetBuffer (new byte [10], -1, 0);
+		}
+
+		[Test]
+		public void SetBuffer_Null_StartNegative ()
+		{
+			SocketAsyncEventArgs saea = new SocketAsyncEventArgs ();
+			saea.SetBuffer (null, -1, 0);
+			Assert.IsNull (saea.Buffer, "Buffer");
+			Assert.IsNull (saea.BufferList, "BufferList");
+			Assert.AreEqual (0, saea.Count, "Count");
+			Assert.AreEqual (0, saea.Offset, "Offset");
+		}
+
+		[Test]
+		[ExpectedException (typeof (ArgumentOutOfRangeException))]
+		public void SetBuffer_ByteArray_CountNegative ()
+		{
+			SocketAsyncEventArgs saea = new SocketAsyncEventArgs ();
+			saea.SetBuffer (new byte [10], 1, -1);
+		}
+
+		[Test]
+		public void SetBuffer_BufferList ()
+		{
+			SocketAsyncEventArgs saea = new SocketAsyncEventArgs ();
+			saea.BufferList = new List<ArraySegment<byte>> ();
+			saea.SetBuffer (1, -1);
+			saea.SetBuffer (-1, 0);
+		}
+
+		[Test]
+		public void SetBuffer_Null_CountNegative ()
+		{
+			SocketAsyncEventArgs saea = new SocketAsyncEventArgs ();
+			saea.SetBuffer (null, 1, -1);
+			Assert.IsNull (saea.Buffer, "Buffer");
+			Assert.IsNull (saea.BufferList, "BufferList");
+			Assert.AreEqual (0, saea.Count, "Count");
+			Assert.AreEqual (0, saea.Offset, "Offset");
+		}
+
+		[Test]
+		[ExpectedException (typeof (ArgumentOutOfRangeException))]
+		public void SetBuffer_ByteArray_StartOverflow ()
+		{
+			SocketAsyncEventArgs saea = new SocketAsyncEventArgs ();
+			saea.SetBuffer (new byte [10], Int32.MaxValue, 1);
+		}
+
+		[Test]
+		public void SetBuffer_Null_StartOverflow ()
+		{
+			SocketAsyncEventArgs saea = new SocketAsyncEventArgs ();
+			saea.SetBuffer (null, Int32.MaxValue, 1);
+			Assert.IsNull (saea.Buffer, "Buffer");
+			Assert.IsNull (saea.BufferList, "BufferList");
+			Assert.AreEqual (0, saea.Count, "Count");
+			Assert.AreEqual (0, saea.Offset, "Offset");
+		}
+
+		[Test]
+		[ExpectedException (typeof (ArgumentOutOfRangeException))]
+		public void SetBuffer_ByteArray_CountOverflow ()
+		{
+			SocketAsyncEventArgs saea = new SocketAsyncEventArgs ();
+			saea.SetBuffer (new byte [10], 1, Int32.MaxValue);
+		}
+
+		[Test]
+		public void SetBuffer_Null_CountOverflow ()
+		{
+			SocketAsyncEventArgs saea = new SocketAsyncEventArgs ();
+			saea.SetBuffer (null, 1, Int32.MaxValue);
+			Assert.IsNull (saea.Buffer, "Buffer");
+			Assert.IsNull (saea.BufferList, "BufferList");
+			Assert.AreEqual (0, saea.Count, "Count");
+			Assert.AreEqual (0, saea.Offset, "Offset");
+		}
+
+		[Test]
+		[ExpectedException (typeof (ArgumentOutOfRangeException))]
+		public void SetBuffer_StartNegative ()
+		{
+			SocketAsyncEventArgs saea = new SocketAsyncEventArgs ();
+			saea.SetBuffer (new byte [0], 0, 0);
+			saea.SetBuffer (-1, 0);
+		}
+
+		[Test]
+		[ExpectedException (typeof (ArgumentOutOfRangeException))]
+		public void SetBuffer_CountNegative ()
+		{
+			SocketAsyncEventArgs saea = new SocketAsyncEventArgs ();
+			saea.SetBuffer (new byte [10], 1, -1);
+		}
+
+		[Test]
+		[ExpectedException (typeof (ArgumentOutOfRangeException))]
+		public void SetBuffer_StartOverflow ()
+		{
+			SocketAsyncEventArgs saea = new SocketAsyncEventArgs ();
+			saea.SetBuffer (new byte [10], Int32.MaxValue, 1);
+		}
+
+		[Test]
+		[ExpectedException (typeof (ArgumentOutOfRangeException))]
+		public void SetBuffer_CountOverflow ()
+		{
+			SocketAsyncEventArgs saea = new SocketAsyncEventArgs ();
+			saea.SetBuffer (new byte [10], 1, Int32.MaxValue);
+		}
+
+		class SocketAsyncEventArgsPoker : SocketAsyncEventArgs {
+
+			public void OnCompleted_ (SocketAsyncEventArgs e)
+			{
+				base.OnCompleted (e);
+			}
+		}
+
+		[Test]
+		public void OnCompleted_Null ()
+		{
+			SocketAsyncEventArgsPoker saea = new SocketAsyncEventArgsPoker ();
+			saea.OnCompleted_ (null);
+		}
+	}
+}
+
+#endif
+