2 // SocketAsyncEventArgsTest.cs - NUnit Test Cases for SocketAsyncEventArgs
5 // Sebastien Pouliot (sebastien@ximian.com)
7 // Copyright (C) 2009 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.
31 using System.Collections.Generic;
32 using System.Net.Sockets;
35 using NUnit.Framework;
37 namespace MonoTests.System.Net.Sockets {
40 public class SocketAsyncEventArgsTest {
43 public void Defaults ()
45 SocketAsyncEventArgs saea = new SocketAsyncEventArgs ();
46 Assert.IsNull (saea.AcceptSocket, "AcceptSocket");
47 Assert.IsNull (saea.Buffer, "Buffer");
48 Assert.IsNull (saea.BufferList, "BufferList");
49 Assert.AreEqual (0, saea.BytesTransferred, "BytesTransferred");
50 Assert.AreEqual (0, saea.Count, "Count");
51 Assert.IsFalse (saea.DisconnectReuseSocket, "DisconnectReuseSocket");
52 Assert.AreEqual (SocketAsyncOperation.None, saea.LastOperation, "LastOperation");
53 Assert.AreEqual (0, saea.Offset, "Offset");
54 Assert.IsNull (saea.RemoteEndPoint, "RemoteEndPoint");
56 Assert.IsNotNull (saea.ReceiveMessageFromPacketInfo, "ReceiveMessageFromPacketInfo");
57 Assert.IsNull (saea.SendPacketsElements, "SendPacketsElements");
58 Assert.AreEqual (TransmitFileOptions.UseDefaultWorkerThread, saea.SendPacketsFlags, "SendPacketsFlags");
60 Assert.AreEqual (-1, saea.SendPacketsSendSize, "SendPacketsSendSize");
61 Assert.AreEqual (SocketError.Success, saea.SocketError, "SocketError");
62 Assert.AreEqual (SocketFlags.None, saea.SocketFlags, "SocketFlags");
63 Assert.IsNull (saea.UserToken, "UserToken");
66 saea.Dispose (); // twice
70 public void SetBuffer_ByteArray ()
72 SocketAsyncEventArgs saea = new SocketAsyncEventArgs ();
74 byte [] buffer = new byte [0];
75 saea.SetBuffer (buffer, 0, 0);
76 Assert.AreEqual (0, saea.Buffer.Length, "0");
77 Assert.AreSame (saea.Buffer, buffer, "same");
79 saea.SetBuffer (null, 0, 0);
80 Assert.IsNull (saea.Buffer, "null");
84 [ExpectedException (typeof (ArgumentException))]
85 public void SetBuffer_BufferList_ByteArray ()
87 SocketAsyncEventArgs saea = new SocketAsyncEventArgs ();
88 saea.BufferList = new List<ArraySegment<byte>> ();
89 saea.SetBuffer (new byte [0], 0, 0);
93 public void SetBuffer_BufferList_NullByteArray ()
95 SocketAsyncEventArgs saea = new SocketAsyncEventArgs ();
96 saea.BufferList = new List<ArraySegment<byte>> ();
97 saea.SetBuffer (null, 0, 0);
101 [ExpectedException (typeof (ArgumentException))]
102 public void SetBuffer_ByteArray_BufferList ()
104 SocketAsyncEventArgs saea = new SocketAsyncEventArgs ();
105 saea.SetBuffer (new byte [0], 0, 0);
106 saea.BufferList = new List<ArraySegment<byte>> ();
110 public void SetBuffer_NullByteArray_BufferList ()
112 SocketAsyncEventArgs saea = new SocketAsyncEventArgs ();
113 saea.SetBuffer (null, 0, 0);
114 saea.BufferList = new List<ArraySegment<byte>> ();
115 Assert.IsNull (saea.Buffer, "Buffer");
119 [ExpectedException (typeof (ArgumentOutOfRangeException))]
120 public void SetBuffer_ByteArray_StartNegative ()
122 SocketAsyncEventArgs saea = new SocketAsyncEventArgs ();
123 saea.SetBuffer (new byte [10], -1, 0);
127 public void SetBuffer_Null_StartNegative ()
129 SocketAsyncEventArgs saea = new SocketAsyncEventArgs ();
130 saea.SetBuffer (null, -1, 0);
131 Assert.IsNull (saea.Buffer, "Buffer");
132 Assert.IsNull (saea.BufferList, "BufferList");
133 Assert.AreEqual (0, saea.Count, "Count");
134 Assert.AreEqual (0, saea.Offset, "Offset");
138 [ExpectedException (typeof (ArgumentOutOfRangeException))]
139 public void SetBuffer_ByteArray_CountNegative ()
141 SocketAsyncEventArgs saea = new SocketAsyncEventArgs ();
142 saea.SetBuffer (new byte [10], 1, -1);
146 public void SetBuffer_BufferList ()
148 SocketAsyncEventArgs saea = new SocketAsyncEventArgs ();
149 saea.BufferList = new List<ArraySegment<byte>> ();
150 saea.SetBuffer (1, -1);
151 saea.SetBuffer (-1, 0);
155 public void SetBuffer_Null_CountNegative ()
157 SocketAsyncEventArgs saea = new SocketAsyncEventArgs ();
158 saea.SetBuffer (null, 1, -1);
159 Assert.IsNull (saea.Buffer, "Buffer");
160 Assert.IsNull (saea.BufferList, "BufferList");
161 Assert.AreEqual (0, saea.Count, "Count");
162 Assert.AreEqual (0, saea.Offset, "Offset");
166 [ExpectedException (typeof (ArgumentOutOfRangeException))]
167 public void SetBuffer_ByteArray_StartOverflow ()
169 SocketAsyncEventArgs saea = new SocketAsyncEventArgs ();
170 saea.SetBuffer (new byte [10], Int32.MaxValue, 1);
174 public void SetBuffer_Null_StartOverflow ()
176 SocketAsyncEventArgs saea = new SocketAsyncEventArgs ();
177 saea.SetBuffer (null, Int32.MaxValue, 1);
178 Assert.IsNull (saea.Buffer, "Buffer");
179 Assert.IsNull (saea.BufferList, "BufferList");
180 Assert.AreEqual (0, saea.Count, "Count");
181 Assert.AreEqual (0, saea.Offset, "Offset");
185 [ExpectedException (typeof (ArgumentOutOfRangeException))]
186 public void SetBuffer_ByteArray_CountOverflow ()
188 SocketAsyncEventArgs saea = new SocketAsyncEventArgs ();
189 saea.SetBuffer (new byte [10], 1, Int32.MaxValue);
193 public void SetBuffer_Null_CountOverflow ()
195 SocketAsyncEventArgs saea = new SocketAsyncEventArgs ();
196 saea.SetBuffer (null, 1, Int32.MaxValue);
197 Assert.IsNull (saea.Buffer, "Buffer");
198 Assert.IsNull (saea.BufferList, "BufferList");
199 Assert.AreEqual (0, saea.Count, "Count");
200 Assert.AreEqual (0, saea.Offset, "Offset");
204 [ExpectedException (typeof (ArgumentOutOfRangeException))]
205 public void SetBuffer_StartNegative ()
207 SocketAsyncEventArgs saea = new SocketAsyncEventArgs ();
208 saea.SetBuffer (new byte [0], 0, 0);
209 saea.SetBuffer (-1, 0);
213 [ExpectedException (typeof (ArgumentOutOfRangeException))]
214 public void SetBuffer_CountNegative ()
216 SocketAsyncEventArgs saea = new SocketAsyncEventArgs ();
217 saea.SetBuffer (new byte [10], 1, -1);
221 [ExpectedException (typeof (ArgumentOutOfRangeException))]
222 public void SetBuffer_StartOverflow ()
224 SocketAsyncEventArgs saea = new SocketAsyncEventArgs ();
225 saea.SetBuffer (new byte [10], Int32.MaxValue, 1);
229 [ExpectedException (typeof (ArgumentOutOfRangeException))]
230 public void SetBuffer_CountOverflow ()
232 SocketAsyncEventArgs saea = new SocketAsyncEventArgs ();
233 saea.SetBuffer (new byte [10], 1, Int32.MaxValue);
236 class SocketAsyncEventArgsPoker : SocketAsyncEventArgs {
238 public void OnCompleted_ (SocketAsyncEventArgs e)
240 base.OnCompleted (e);
245 public void OnCompleted_Null ()
247 SocketAsyncEventArgsPoker saea = new SocketAsyncEventArgsPoker ();
248 saea.OnCompleted_ (null);