PageRenderTime 20ms CodeModel.GetById 10ms app.highlight 54ms RepoModel.GetById 12ms app.codeStats 1ms

/mcs/class/System/Test/System.Net.Sockets/SocketTest.cs

https://bitbucket.org/danipen/mono
C# | 4309 lines | 3449 code | 679 blank | 181 comment | 58 complexity | 355a8255fc0c4f390e0aea77ac584ca7 MD5 | raw file

Large files files are truncated, but you can click here to view the full file

   1// System.Net.Sockets.SocketTest.cs
   2//
   3// Authors:
   4//    Brad Fitzpatrick (brad@danga.com)
   5//    Gonzalo Paniagua Javier (gonzalo@novell.com)
   6//
   7// (C) Copyright 2003 Brad Fitzpatrick
   8// Copyright (c) 2005 Novell, Inc. (http://www.novell.com)
   9//
  10
  11using System;
  12using System.Collections;
  13using System.Threading;
  14using System.Net;
  15using System.Net.Sockets;
  16using NUnit.Framework;
  17using System.IO;
  18
  19#if NET_2_0
  20using System.Collections.Generic;
  21#endif
  22
  23namespace MonoTests.System.Net.Sockets
  24{
  25	[TestFixture]
  26	public class SocketTest
  27	{
  28		// note: also used in SocketCas tests
  29		public const string BogusAddress = "192.168.244.244";
  30		public const int BogusPort = 23483;
  31
  32		[Test]
  33		public void ConnectIPAddressAny ()
  34		{
  35			IPEndPoint ep = new IPEndPoint (IPAddress.Any, 0);
  36
  37			/* UDP sockets use Any to disconnect
  38			try {
  39				using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp)) {
  40					s.Connect (ep);
  41					s.Close ();
  42				}
  43				Assert.Fail ("#1");
  44			} catch (SocketException ex) {
  45				Assert.AreEqual (10049, ex.ErrorCode, "#2");
  46			}
  47			*/
  48
  49			try {
  50				using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) {
  51					s.Connect (ep);
  52					s.Close ();
  53				}
  54				Assert.Fail ("#3");
  55			} catch (SocketException ex) {
  56				Assert.AreEqual (10049, ex.ErrorCode, "#4");
  57			}
  58		}
  59
  60		[Test]
  61		[Ignore ("Bug #75158")] // Looks like MS fails after the .ctor, when you try to use the socket
  62		public void IncompatibleAddress ()
  63		{
  64			IPEndPoint epIPv6 = new IPEndPoint (IPAddress.IPv6Any,
  65								0);
  66
  67			try {
  68				using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP)) {
  69					s.Connect (epIPv6);
  70					s.Close ();
  71				}
  72				Assert.Fail ("#1");
  73			} catch (SocketException ex) {
  74#if !NET_2_0
  75				// invalid argument
  76				int expectedError = 10022;
  77#else
  78				// address incompatible with protocol
  79				int expectedError = 10047;
  80#endif
  81				Assert.AreEqual (expectedError, ex.ErrorCode,
  82						"#2");
  83			}
  84		}
  85
  86		[Test]
  87		[Category ("InetAccess")]
  88		public void BogusEndConnect ()
  89		{
  90			IPAddress ipOne = IPAddress.Parse (BogusAddress);
  91			IPEndPoint ipEP = new IPEndPoint (ipOne, BogusPort);
  92			Socket sock = new Socket (ipEP.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
  93			IAsyncResult ar = sock.BeginConnect (ipEP, null, null);
  94
  95			try {
  96				// should raise an exception because connect was bogus
  97				sock.EndConnect (ar);
  98				Assert.Fail ("#1");
  99			} catch (SocketException ex) {
 100				// Actual error code depends on network configuration.
 101				var error = (SocketError) ex.ErrorCode;
 102				Assert.That (error == SocketError.TimedOut ||
 103				             error == SocketError.ConnectionRefused ||
 104				             error == SocketError.NetworkUnreachable ||
 105				             error == SocketError.HostUnreachable, "#2");
 106			}
 107		}
 108
 109		[Test]
 110		[ExpectedException (typeof (ArgumentNullException))]
 111		public void SelectEmpty ()
 112		{
 113			ArrayList list = new ArrayList ();
 114			Socket.Select (list, list, list, 1000);
 115		}
 116		
 117		private bool BlockingConnect (bool block)
 118		{
 119			IPEndPoint ep = new IPEndPoint(IPAddress.Loopback, 1234);
 120			Socket server = new Socket(AddressFamily.InterNetwork,
 121						   SocketType.Stream,
 122						   ProtocolType.Tcp);
 123			server.Bind(ep);
 124			server.Blocking=block;
 125
 126			server.Listen(0);
 127
 128			Socket conn = new Socket (AddressFamily.InterNetwork,
 129						  SocketType.Stream,
 130						  ProtocolType.Tcp);
 131			conn.Connect (ep);
 132
 133			Socket client = server.Accept();
 134			bool client_block = client.Blocking;
 135
 136			client.Close();
 137			conn.Close();
 138			server.Close();
 139			
 140			return(client_block);
 141		}
 142
 143		[Test]
 144		public void AcceptBlockingStatus()
 145		{
 146			bool block;
 147
 148			block = BlockingConnect(true);
 149			Assert.AreEqual (block, true, "BlockingStatus01");
 150
 151			block = BlockingConnect(false);
 152			Assert.AreEqual (block, false, "BlockingStatus02");
 153		}
 154
 155		static bool CFAConnected = false;
 156		static ManualResetEvent CFACalledBack;
 157		
 158		private static void CFACallback (IAsyncResult asyncResult)
 159		{
 160			Socket sock = (Socket)asyncResult.AsyncState;
 161			CFAConnected = sock.Connected;
 162			
 163			if (sock.Connected) {
 164				sock.EndConnect (asyncResult);
 165			}
 166
 167			CFACalledBack.Set ();
 168		}
 169
 170		[Test] // Connect (IPEndPoint)
 171		public void Connect1_RemoteEP_Null ()
 172		{
 173			Socket s = new Socket (AddressFamily.InterNetwork,
 174				SocketType.Stream, ProtocolType.Tcp);
 175			try {
 176				s.Connect ((IPEndPoint) null);
 177				Assert.Fail ("#1");
 178			} catch (ArgumentNullException ex) {
 179				Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
 180				Assert.IsNull (ex.InnerException, "#3");
 181				Assert.IsNotNull (ex.Message, "#4");
 182				Assert.AreEqual ("remoteEP", ex.ParamName, "#5");
 183			}
 184		}
 185
 186		[Test]
 187		public void ConnectFailAsync ()
 188		{
 189			Socket sock = new Socket (AddressFamily.InterNetwork,
 190						  SocketType.Stream,
 191						  ProtocolType.Tcp);
 192			sock.Blocking = false;
 193			CFACalledBack = new ManualResetEvent (false);
 194			CFACalledBack.Reset ();
 195
 196			/* Need a port that is not being used for
 197			 * anything...
 198			 */
 199			sock.BeginConnect (new IPEndPoint (IPAddress.Loopback,
 200							   114),
 201					   new AsyncCallback (CFACallback),
 202					   sock);
 203			CFACalledBack.WaitOne ();
 204
 205			Assert.AreEqual (CFAConnected, false, "ConnectFail");
 206		}
 207		
 208#if !TARGET_JVM
 209		[Test]
 210#if !NET_2_0
 211		[ExpectedException (typeof (ArgumentException))]
 212#endif
 213		public void SetSocketOptionBoolean ()
 214		{
 215			IPEndPoint ep = new IPEndPoint (IPAddress.Loopback, 1);
 216			Socket sock = new Socket (ep.Address.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
 217			try {
 218				sock.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.KeepAlive, true);
 219			} finally {
 220				sock.Close ();
 221			}
 222		}
 223#endif
 224		[Test]
 225		public void TestSelect1 ()
 226		{
 227			Socket srv = CreateServer ();
 228			ClientSocket clnt = new ClientSocket (srv.LocalEndPoint);
 229			Thread th = new Thread (new ThreadStart (clnt.ConnectSleepClose));
 230			Socket acc = null;
 231			try {
 232				th.Start ();
 233				acc = srv.Accept ();
 234				clnt.Write ();
 235				ArrayList list = new ArrayList ();
 236				ArrayList empty = new ArrayList ();
 237				list.Add (acc);
 238				Socket.Select (list, empty, empty, 100);
 239				Assert.AreEqual (0, empty.Count, "#01");
 240				Assert.AreEqual (1, list.Count, "#02");
 241				Socket.Select (empty, list, empty, 100);
 242				Assert.AreEqual (0, empty.Count, "#03");
 243				Assert.AreEqual (1, list.Count, "#04");
 244				Socket.Select (list, empty, empty, -1);
 245				Assert.AreEqual (0, empty.Count, "#05");
 246				Assert.AreEqual (1, list.Count, "#06");
 247				// Need to read the 10 bytes from the client to avoid a RST
 248				byte [] bytes = new byte [10];
 249				acc.Receive (bytes);
 250			} finally {
 251				if (acc != null)
 252					acc.Close ();
 253				srv.Close ();
 254			}
 255		}
 256
 257		static Socket CreateServer ()
 258		{
 259			Socket sock = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
 260			sock.Bind (new IPEndPoint (IPAddress.Loopback, 0));
 261			sock.Listen (1);
 262			return sock;
 263		}
 264
 265		class ClientSocket {
 266			Socket sock;
 267			EndPoint ep;
 268
 269			public ClientSocket (EndPoint ep)
 270			{
 271				this.ep = ep;
 272				sock = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
 273			}
 274
 275			public void ConnectSleepClose ()
 276			{
 277				sock.Connect (ep);
 278				Thread.Sleep (2000);
 279				sock.Close ();
 280			}
 281
 282			public void Write ()
 283			{
 284				byte [] b = new byte [10];
 285				sock.Send (b);
 286			}
 287		}
 288
 289		byte[] buf = new byte[100];
 290
 291		[Test]
 292		[ExpectedException (typeof (ObjectDisposedException))]
 293		public void Disposed2 ()
 294		{
 295			Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
 296			s.Close();
 297
 298			s.Blocking = true;
 299		}
 300
 301		[Test]
 302		[ExpectedException (typeof (ObjectDisposedException))]
 303		public void Disposed6 ()
 304		{
 305			Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
 306			s.Close();
 307
 308			s.Listen (5);
 309		}
 310
 311		[Test]
 312		[ExpectedException (typeof (ObjectDisposedException))]
 313		public void Disposed7 ()
 314		{
 315			Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
 316			s.Close();
 317
 318			s.Poll (100, 0);
 319		}
 320
 321		[Test]
 322		[ExpectedException (typeof (ObjectDisposedException))]
 323		public void Disposed15 ()
 324		{
 325			Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
 326			s.Close();
 327
 328			s.Send (buf);
 329		}
 330
 331		[Test]
 332		[ExpectedException (typeof (ObjectDisposedException))]
 333		public void Disposed16 ()
 334		{
 335			Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
 336			s.Close();
 337
 338			s.Send (buf, 0);
 339		}
 340
 341		[Test]
 342		[ExpectedException (typeof (ObjectDisposedException))]
 343		public void Disposed17 ()
 344		{
 345			Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
 346			s.Close();
 347
 348			s.Send (buf, 10, 0);
 349		}
 350
 351		[Test]
 352		[ExpectedException (typeof (ObjectDisposedException))]
 353		public void Disposed18 ()
 354		{
 355			Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
 356			s.Close();
 357
 358			s.Send (buf, 0, 10, 0);
 359		}
 360
 361		[Test]
 362		[ExpectedException (typeof (ObjectDisposedException))]
 363		public void Disposed19 ()
 364		{
 365			Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
 366			EndPoint ep = new IPEndPoint (IPAddress.Any, 31337);
 367			s.Close();
 368
 369			s.SendTo (buf, 0, ep);
 370		}
 371
 372		[Test]
 373		[ExpectedException (typeof (ObjectDisposedException))]
 374		public void Disposed20 ()
 375		{
 376			Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
 377			EndPoint ep = new IPEndPoint (IPAddress.Any, 31337);
 378			s.Close();
 379
 380			s.SendTo (buf, 10, 0, ep);
 381		}
 382
 383		[Test]
 384		[ExpectedException (typeof (ObjectDisposedException))]
 385		public void Disposed21 ()
 386		{
 387			Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
 388			EndPoint ep = new IPEndPoint (IPAddress.Any, 31337);
 389			s.Close();
 390
 391			s.SendTo (buf, 0, 10, 0, ep);
 392		}
 393
 394		[Test]
 395		[ExpectedException (typeof (ObjectDisposedException))]
 396		public void Disposed22 ()
 397		{
 398			Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
 399			EndPoint ep = new IPEndPoint (IPAddress.Any, 31337);
 400			s.Close();
 401
 402			s.SendTo (buf, ep);
 403		}
 404
 405		[Test]
 406		[ExpectedException (typeof (ObjectDisposedException))]
 407		public void Disposed23 ()
 408		{
 409			Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
 410			s.Close();
 411
 412			s.Shutdown (0);
 413		}
 414
 415		[Test]
 416		public void GetHashCodeTest ()
 417		{
 418			Socket server = new Socket (AddressFamily.InterNetwork,
 419				SocketType.Stream, ProtocolType.Tcp);
 420			IPEndPoint ep = new IPEndPoint (IPAddress.Loopback,
 421							9010);
 422			server.Bind (ep);
 423			server.Listen (1);
 424
 425			Socket client = new Socket (AddressFamily.InterNetwork, 
 426				SocketType.Stream, ProtocolType.Tcp);
 427			int hashcodeA = client.GetHashCode ();
 428			client.Connect (ep);
 429			int hashcodeB = client.GetHashCode ();
 430			Assert.AreEqual (hashcodeA, hashcodeB, "#1");
 431			client.Close ();
 432			int hashcodeC = client.GetHashCode ();
 433#if NET_2_0
 434			Assert.AreEqual (hashcodeB, hashcodeC, "#2");
 435#else
 436			Assert.IsFalse (hashcodeB == hashcodeC, "#2");
 437#endif
 438			server.Close ();
 439		}
 440
 441		static ManualResetEvent SocketError_event = new ManualResetEvent (false);
 442
 443		private static void SocketError_callback (IAsyncResult ar)
 444		{
 445			Socket sock = (Socket)ar.AsyncState;
 446			
 447			if(sock.Connected) {
 448				sock.EndConnect (ar);
 449			}
 450
 451			SocketError_event.Set ();
 452		}
 453
 454		[Test]
 455		public void SocketErrorTest ()
 456		{
 457			Socket sock = new Socket (AddressFamily.InterNetwork,
 458						  SocketType.Stream,
 459						  ProtocolType.Tcp);
 460			IPEndPoint ep = new IPEndPoint (IPAddress.Loopback,
 461							BogusPort);
 462			
 463			SocketError_event.Reset ();
 464
 465			sock.Blocking = false;
 466			sock.BeginConnect (ep, new AsyncCallback(SocketError_callback),
 467				sock);
 468
 469			if (SocketError_event.WaitOne (2000, false) == false) {
 470				Assert.Fail ("SocketError wait timed out");
 471			}
 472
 473			Assert.AreEqual (false, sock.Connected, "SocketError #1");
 474
 475			int error;
 476
 477			error = (int)sock.GetSocketOption (SocketOptionLevel.Socket, SocketOptionName.Error);
 478			Assert.AreEqual (10061, error, "SocketError #2");
 479
 480			error = (int)sock.GetSocketOption (SocketOptionLevel.Socket, SocketOptionName.Error);
 481			Assert.AreEqual (10061, error, "SocketError #3");
 482
 483			sock.Close ();
 484		}
 485		
 486
 487#if NET_2_0
 488		[Test]
 489		public void SocketInformationCtor ()
 490		{
 491		}
 492		
 493		[Test]
 494		public void DontFragmentDefaultTcp ()
 495		{
 496			Socket sock = new Socket (AddressFamily.InterNetwork,
 497						  SocketType.Stream,
 498						  ProtocolType.Tcp);
 499			
 500			Assert.AreEqual (false, sock.DontFragment, "DontFragmentDefaultTcp");
 501
 502			sock.Close ();
 503		}
 504
 505		[Test]
 506		[Category ("NotWorking")] // DontFragment doesn't work
 507		public void DontFragmentChangeTcp ()
 508		{
 509			Socket sock = new Socket (AddressFamily.InterNetwork,
 510						  SocketType.Stream,
 511						  ProtocolType.Tcp);
 512			
 513			sock.DontFragment = true;
 514			
 515			Assert.AreEqual (true, sock.DontFragment, "DontFragmentChangeTcp");
 516
 517			sock.Close ();
 518		}
 519		
 520		[Test]
 521		public void DontFragmentDefaultUdp ()
 522		{
 523			Socket sock = new Socket (AddressFamily.InterNetwork,
 524						  SocketType.Dgram,
 525						  ProtocolType.Udp);
 526			
 527			Assert.AreEqual (false, sock.DontFragment, "DontFragmentDefaultUdp");
 528
 529			sock.Close ();
 530		}
 531
 532		[Test]
 533		[Category ("NotWorking")] // DontFragment doesn't work
 534		public void DontFragmentChangeUdp ()
 535		{
 536			Socket sock = new Socket (AddressFamily.InterNetwork,
 537						  SocketType.Dgram,
 538						  ProtocolType.Udp);
 539			
 540			sock.DontFragment = true;
 541			
 542			Assert.AreEqual (true, sock.DontFragment, "DontFragmentChangeUdp");
 543
 544			sock.Close ();
 545		}
 546
 547		[Test]
 548		[ExpectedException (typeof(ObjectDisposedException))]
 549		public void DontFragmentClosed ()
 550		{
 551			Socket sock = new Socket (AddressFamily.InterNetwork,
 552						  SocketType.Stream,
 553						  ProtocolType.Tcp);
 554			
 555			sock.Close ();
 556			
 557			bool val = sock.DontFragment;
 558		}
 559		
 560		[Test]
 561		[Category ("NotWorking")] // Need to pick a non-IP AddressFamily that "works" on both mono and ms, this one only works on ms
 562		public void DontFragment ()
 563		{
 564			Socket sock = new Socket (AddressFamily.NetBios,
 565						  SocketType.Seqpacket,
 566						  ProtocolType.Unspecified);
 567			
 568			try {
 569				sock.DontFragment = true;
 570				Assert.Fail ("DontFragment #1");
 571			} catch (NotSupportedException) {
 572			} catch {
 573				Assert.Fail ("DontFragment #2");
 574			} finally {
 575				sock.Close ();
 576			}
 577		}
 578		
 579		[Test]
 580		public void EnableBroadcastDefaultTcp ()
 581		{
 582			Socket sock = new Socket (AddressFamily.InterNetwork,
 583						  SocketType.Stream,
 584						  ProtocolType.Tcp);
 585			
 586			try {
 587				bool value = sock.EnableBroadcast;
 588				Assert.Fail ("EnableBroadcastDefaultTcp #1");
 589			} catch (SocketException ex) {
 590				Assert.AreEqual (10042, ex.ErrorCode, "EnableBroadcastDefaultTcp #2");
 591			} catch {
 592				Assert.Fail ("EnableBroadcastDefaultTcp #2");
 593			} finally {
 594				sock.Close ();
 595			}
 596		}
 597
 598		[Test]
 599		public void EnableBroadcastDefaultUdp ()
 600		{
 601			Socket sock = new Socket (AddressFamily.InterNetwork,
 602						  SocketType.Dgram,
 603						  ProtocolType.Udp);
 604			
 605			Assert.AreEqual (false, sock.EnableBroadcast, "EnableBroadcastDefaultUdp");
 606
 607			sock.Close ();
 608		}
 609		
 610		[Test]
 611		public void EnableBroadcastChangeTcp ()
 612		{
 613			Socket sock = new Socket (AddressFamily.InterNetwork,
 614						  SocketType.Stream,
 615						  ProtocolType.Tcp);
 616			
 617			try {
 618				sock.EnableBroadcast = true;
 619				Assert.Fail ("EnableBroadcastChangeTcp #1");
 620			} catch (SocketException ex) {
 621				Assert.AreEqual (10042, ex.ErrorCode, "EnableBroadcastChangeTcp #2");
 622			} catch {
 623				Assert.Fail ("EnableBroadcastChangeTcp #2");
 624			} finally {
 625				sock.Close ();
 626			}
 627		}
 628		
 629		[Test]
 630		public void EnableBroadcastChangeUdp ()
 631		{
 632			Socket sock = new Socket (AddressFamily.InterNetwork,
 633						  SocketType.Dgram,
 634						  ProtocolType.Udp);
 635			
 636			sock.EnableBroadcast = true;
 637			
 638			Assert.AreEqual (true, sock.EnableBroadcast, "EnableBroadcastChangeUdp");
 639
 640			sock.Close ();
 641		}
 642
 643		[Test]
 644		[ExpectedException (typeof(ObjectDisposedException))]
 645		public void EnableBroadcastClosed ()
 646		{
 647			Socket sock = new Socket (AddressFamily.InterNetwork,
 648						  SocketType.Dgram,
 649						  ProtocolType.Udp);
 650			
 651			sock.Close ();
 652			
 653			bool val = sock.EnableBroadcast;
 654		}
 655
 656		/* Can't test the default for ExclusiveAddressUse as
 657		 * it's different on different versions and service
 658		 * packs of windows
 659		 */
 660		[Test]
 661		[Category ("NotWorking")] // Not supported on Linux
 662		public void ExclusiveAddressUseUnbound ()
 663		{
 664			Socket sock = new Socket (AddressFamily.InterNetwork,
 665						  SocketType.Stream,
 666						  ProtocolType.Tcp);
 667			
 668			sock.ExclusiveAddressUse = true;
 669			
 670			Assert.AreEqual (true, sock.ExclusiveAddressUse, "ExclusiveAddressUseUnbound");
 671			
 672			sock.Close ();
 673		}
 674
 675		[Test]
 676		[ExpectedException (typeof(InvalidOperationException))]
 677		[Category ("NotWorking")] // Not supported on Linux
 678		public void ExclusiveAddressUseBound ()
 679		{
 680			Socket sock = new Socket (AddressFamily.InterNetwork,
 681						  SocketType.Stream,
 682						  ProtocolType.Tcp);
 683			
 684			sock.Bind (new IPEndPoint (IPAddress.Any, 1235));
 685			sock.ExclusiveAddressUse = true;
 686			sock.Close ();
 687		}
 688
 689		[Test]
 690		[ExpectedException (typeof(ObjectDisposedException))]
 691		public void ExclusiveAddressUseClosed ()
 692		{
 693			Socket sock = new Socket (AddressFamily.InterNetwork,
 694						  SocketType.Stream,
 695						  ProtocolType.Tcp);
 696			
 697			sock.Close ();
 698			
 699			bool val = sock.ExclusiveAddressUse;
 700		}
 701		
 702		[Test]
 703		public void IsBoundTcp ()
 704		{
 705			Socket sock = new Socket (AddressFamily.InterNetwork,
 706						  SocketType.Stream,
 707						  ProtocolType.Tcp);
 708			IPEndPoint ep = new IPEndPoint (IPAddress.Loopback,
 709							BogusPort);
 710			
 711			Assert.AreEqual (false, sock.IsBound, "IsBoundTcp #1");
 712			
 713			sock.Bind (ep);
 714			Assert.AreEqual (true, sock.IsBound, "IsBoundTcp #2");
 715
 716			sock.Listen (1);
 717			
 718			Socket sock2 = new Socket (AddressFamily.InterNetwork,
 719						   SocketType.Stream,
 720						   ProtocolType.Tcp);
 721			
 722			Assert.AreEqual (false, sock2.IsBound, "IsBoundTcp #3");
 723			
 724			sock2.Connect (ep);
 725			Assert.AreEqual (true, sock2.IsBound, "IsBoundTcp #4");
 726			
 727			sock2.Close ();
 728			Assert.AreEqual (true, sock2.IsBound, "IsBoundTcp #5");
 729
 730			sock.Close ();
 731			Assert.AreEqual (true, sock.IsBound, "IsBoundTcp #6");
 732		}
 733
 734		[Test]
 735		public void IsBoundUdp ()
 736		{
 737			Socket sock = new Socket (AddressFamily.InterNetwork,
 738						  SocketType.Dgram,
 739						  ProtocolType.Udp);
 740			IPEndPoint ep = new IPEndPoint (IPAddress.Loopback,
 741							BogusPort);
 742			
 743			Assert.AreEqual (false, sock.IsBound, "IsBoundUdp #1");
 744			
 745			sock.Bind (ep);
 746			Assert.AreEqual (true, sock.IsBound, "IsBoundUdp #2");
 747			
 748			sock.Close ();
 749			Assert.AreEqual (true, sock.IsBound, "IsBoundUdp #3");
 750			
 751
 752			sock = new Socket (AddressFamily.InterNetwork,
 753					   SocketType.Dgram,
 754					   ProtocolType.Udp);
 755			
 756			Assert.AreEqual (false, sock.IsBound, "IsBoundUdp #4");
 757			
 758			sock.Connect (ep);
 759			Assert.AreEqual (true, sock.IsBound, "IsBoundUdp #5");
 760			
 761			sock.Close ();
 762			Assert.AreEqual (true, sock.IsBound, "IsBoundUdp #6");
 763		}
 764
 765		[Test]
 766		/* Should not throw an exception */
 767		public void IsBoundClosed ()
 768		{
 769			Socket sock = new Socket (AddressFamily.InterNetwork,
 770						  SocketType.Stream,
 771						  ProtocolType.Tcp);
 772			
 773			sock.Close ();
 774			
 775			bool val = sock.IsBound;
 776		}
 777		
 778		/* Nothing much to test for LingerState */
 779		
 780		[Test]
 781		public void MulticastLoopbackDefaultTcp ()
 782		{
 783			Socket sock = new Socket (AddressFamily.InterNetwork,
 784						  SocketType.Stream,
 785						  ProtocolType.Tcp);
 786			
 787			try {
 788				bool value = sock.MulticastLoopback;
 789				Assert.Fail ("MulticastLoopbackDefaultTcp #1");
 790			} catch (SocketException ex) {
 791				Assert.AreEqual (10042, ex.ErrorCode, "MulticastLoopbackDefaultTcp #2");
 792			} catch {
 793				Assert.Fail ("MulticastLoopbackDefaultTcp #2");
 794			} finally {
 795				sock.Close ();
 796			}
 797		}
 798
 799		[Test]
 800		public void MulticastLoopbackChangeTcp ()
 801		{
 802			Socket sock = new Socket (AddressFamily.InterNetwork,
 803						  SocketType.Stream,
 804						  ProtocolType.Tcp);
 805			
 806			try {
 807				sock.MulticastLoopback = false;
 808				Assert.Fail ("MulticastLoopbackChangeTcp #1");
 809			} catch (SocketException ex) {
 810				Assert.AreEqual (10042, ex.ErrorCode, "MulticastLoopbackChangeTcp #2");
 811			} catch {
 812				Assert.Fail ("MulticastLoopbackChangeTcp #2");
 813			} finally {
 814				sock.Close ();
 815			}
 816		}
 817		
 818		[Test]
 819		public void MulticastLoopbackDefaultUdp ()
 820		{
 821			Socket sock = new Socket (AddressFamily.InterNetwork,
 822						  SocketType.Dgram,
 823						  ProtocolType.Udp);
 824			
 825			Assert.AreEqual (true, sock.MulticastLoopback, "MulticastLoopbackDefaultUdp");
 826			
 827			sock.Close ();
 828		}
 829		
 830		[Test]
 831		public void MulticastLoopbackChangeUdp ()
 832		{
 833			Socket sock = new Socket (AddressFamily.InterNetwork,
 834						  SocketType.Dgram,
 835						  ProtocolType.Udp);
 836			
 837			sock.MulticastLoopback = false;
 838			
 839			Assert.AreEqual (false, sock.MulticastLoopback, "MulticastLoopbackChangeUdp");
 840			
 841			sock.Close ();
 842		}
 843
 844		[Test]
 845		[ExpectedException (typeof(ObjectDisposedException))]
 846		public void MulticastLoopbackClosed ()
 847		{
 848			Socket sock = new Socket (AddressFamily.InterNetwork,
 849						  SocketType.Stream,
 850						  ProtocolType.Tcp);
 851			
 852			sock.Close ();
 853			
 854			bool val = sock.MulticastLoopback;
 855		}
 856		
 857		/* OSSupportsIPv6 depends on the environment */
 858		
 859		[Test]
 860		[Category("NotWorking")] // We have different defaults for perf reasons
 861		public void ReceiveBufferSizeDefault ()
 862		{
 863			Socket sock = new Socket (AddressFamily.InterNetwork,
 864						  SocketType.Stream,
 865						  ProtocolType.Tcp);
 866			
 867			Assert.AreEqual (8192, sock.ReceiveBufferSize, "ReceiveBufferSizeDefault");
 868			
 869			sock.Close ();
 870		}
 871		
 872		[Test]
 873		[Category("NotWorking")] // We have different defaults for perf reasons
 874		public void ReceiveBufferSizeDefaultUdp ()
 875		{
 876			Socket sock = new Socket (AddressFamily.InterNetwork,
 877						  SocketType.Dgram,
 878						  ProtocolType.Udp);
 879			
 880			Assert.AreEqual (8192, sock.ReceiveBufferSize, "ReceiveBufferSizeDefaultUdp");
 881			
 882			sock.Close ();
 883		}
 884
 885		[Test]
 886		public void ReceiveBufferSizeChange ()
 887		{
 888			Socket sock = new Socket (AddressFamily.InterNetwork,
 889						  SocketType.Stream,
 890						  ProtocolType.Tcp);
 891			
 892			sock.ReceiveBufferSize = 16384;
 893			
 894			Assert.AreEqual (16384, sock.ReceiveBufferSize, "ReceiveBufferSizeChange");
 895			
 896			sock.Close ();
 897		}
 898
 899		[Test]
 900		[Category("NotWorking")] // We cannot totally remove buffers (minimum is set to 256
 901		public void BuffersCheck_None ()
 902		{
 903			using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) {
 904				int original = s.ReceiveBufferSize;
 905				s.ReceiveBufferSize = 0;
 906				Assert.AreEqual (0, s.ReceiveBufferSize, "ReceiveBufferSize " + original.ToString ());
 907
 908				original = s.SendBufferSize;
 909				s.SendBufferSize = 0;
 910				Assert.AreEqual (0, s.SendBufferSize, "SendBufferSize " + original.ToString ());
 911			}
 912		}
 913
 914		[Test]
 915		[ExpectedException (typeof(ObjectDisposedException))]
 916		public void ReceiveBufferSizeClosed ()
 917		{
 918			Socket sock = new Socket (AddressFamily.InterNetwork,
 919						  SocketType.Stream,
 920						  ProtocolType.Tcp);
 921			
 922			sock.Close ();
 923			
 924			int val = sock.ReceiveBufferSize;
 925		}
 926		
 927		[Test]
 928		[Category("NotWorking")] // We have different defaults for perf reasons
 929		public void SendBufferSizeDefault ()
 930		{
 931			Socket sock = new Socket (AddressFamily.InterNetwork,
 932						  SocketType.Stream,
 933						  ProtocolType.Tcp);
 934			
 935			Assert.AreEqual (8192, sock.SendBufferSize, "SendBufferSizeDefault");
 936			
 937			sock.Close ();
 938		}
 939		
 940		[Test]
 941		[Category("NotWorking")] // We have different defaults for perf reasons
 942		public void SendBufferSizeDefaultUdp ()
 943		{
 944			Socket sock = new Socket (AddressFamily.InterNetwork,
 945						  SocketType.Dgram,
 946						  ProtocolType.Udp);
 947			
 948			Assert.AreEqual (8192, sock.SendBufferSize, "SendBufferSizeDefaultUdp");
 949			
 950			sock.Close ();
 951		}
 952
 953		[Test]
 954		public void SendBufferSizeChange ()
 955		{
 956			Socket sock = new Socket (AddressFamily.InterNetwork,
 957						  SocketType.Stream,
 958						  ProtocolType.Tcp);
 959			
 960			sock.SendBufferSize = 16384;
 961			
 962			Assert.AreEqual (16384, sock.SendBufferSize, "SendBufferSizeChange");
 963			
 964			sock.Close ();
 965		}
 966
 967		[Test]
 968		[ExpectedException (typeof(ObjectDisposedException))]
 969		public void SendBufferSizeClosed ()
 970		{
 971			Socket sock = new Socket (AddressFamily.InterNetwork,
 972						  SocketType.Stream,
 973						  ProtocolType.Tcp);
 974			
 975			sock.Close ();
 976			
 977			int val = sock.SendBufferSize;
 978		}
 979		
 980		/* No test for TTL default as it's platform dependent */
 981		[Test]
 982		public void TtlChange ()
 983		{
 984			Socket sock = new Socket (AddressFamily.InterNetwork,
 985						  SocketType.Stream,
 986						  ProtocolType.Tcp);
 987			
 988			sock.Ttl = 255;
 989			
 990			Assert.AreEqual (255, sock.Ttl, "TtlChange");
 991			
 992			sock.Close ();
 993		}
 994
 995		[Test]
 996		[Category ("NotOnMac")] // Mac doesn't throw when overflowing the ttl
 997		public void TtlChangeOverflow ()
 998		{
 999			Socket sock = new Socket (AddressFamily.InterNetwork,
1000						  SocketType.Stream,
1001						  ProtocolType.Tcp);
1002			
1003			try {
1004				sock.Ttl = 256;
1005				Assert.Fail ("TtlChangeOverflow #1");
1006			} catch (SocketException ex) {
1007				Assert.AreEqual (10022, ex.ErrorCode,
1008						 "TtlChangeOverflow #2");
1009			} catch {
1010				Assert.Fail ("TtlChangeoverflow #3");
1011			} finally {
1012				sock.Close ();
1013			}
1014		}
1015		
1016/* Apparently you can set TTL=0 on the ms runtime!!
1017			try {
1018				sock.Ttl = 0;
1019				Assert.Fail ("TtlChangeOverflow #4");
1020			} catch (SocketException ex) {
1021				Assert.AreEqual (10022, ex.ErrorCode,
1022						 "TtlChangeOverflow #5");
1023			} catch {
1024				Assert.Fail ("TtlChangeOverflow #6");
1025			} finally {
1026				sock.Close ();
1027			}
1028*/
1029
1030		[Test]
1031		[ExpectedException (typeof(ObjectDisposedException))]
1032		public void TtlClosed ()
1033		{
1034			Socket sock = new Socket (AddressFamily.InterNetwork,
1035						  SocketType.Stream,
1036						  ProtocolType.Tcp);
1037			
1038			sock.Close ();
1039			
1040			int val = sock.Ttl;
1041		}
1042		
1043		[Test]
1044		public void UseOnlyOverlappedIODefault ()
1045		{
1046			Socket sock = new Socket (AddressFamily.InterNetwork,
1047						  SocketType.Stream,
1048						  ProtocolType.Tcp);
1049			
1050			Assert.AreEqual (false, sock.UseOnlyOverlappedIO, "UseOnlyOverlappedIODefault");
1051			
1052			sock.Close ();
1053		}
1054
1055		//
1056		// We need this because the Linux kernel in certain configurations
1057		// will end up rounding up the values passed on to the kernel
1058		// for socket send/receive timeouts.
1059		//
1060		int Approximate (int target, int value)
1061		{
1062			int epsilon = 10;
1063			
1064			if (value > target-10 && value < target+10)
1065				return target;
1066			return value;
1067		}
1068		
1069		[Test]
1070		public void UseOnlyOverlappedIOChange ()
1071		{
1072			Socket sock = new Socket (AddressFamily.InterNetwork,
1073						  SocketType.Stream,
1074						  ProtocolType.Tcp);
1075			
1076			sock.UseOnlyOverlappedIO = true;
1077			
1078			Assert.AreEqual (true, sock.UseOnlyOverlappedIO, "UseOnlyOverlappedIOChange");
1079			
1080			sock.Close ();
1081		}
1082
1083		[Test]
1084		/* Should not throw an exception */
1085		public void UseOnlyOverlappedIOClosed ()
1086		{
1087			Socket sock = new Socket (AddressFamily.InterNetwork,
1088						  SocketType.Stream,
1089						  ProtocolType.Tcp);
1090			
1091			sock.Close ();
1092			
1093			bool val = sock.UseOnlyOverlappedIO;
1094		}
1095		
1096		[Test]
1097		public void SendTimeoutDefault ()
1098		{
1099			Socket sock = new Socket (AddressFamily.InterNetwork,
1100						  SocketType.Stream,
1101						  ProtocolType.Tcp);
1102			
1103			Assert.AreEqual (0, sock.SendTimeout, "SendTimeoutDefault");
1104			
1105			sock.Close ();
1106		}
1107
1108		[Test]
1109		public void SendTimeoutChange ()
1110		{
1111			Socket sock = new Socket (AddressFamily.InterNetwork,
1112						  SocketType.Stream,
1113						  ProtocolType.Tcp);
1114			
1115			/* Should be rounded up to 500, according to
1116			 * the MSDN docs, but the MS runtime doesn't
1117			 */
1118			sock.SendTimeout = 50;
1119			Assert.AreEqual (50, Approximate (50, sock.SendTimeout), "SendTimeoutChange #1");
1120			
1121			sock.SendTimeout = 2000;
1122			Assert.AreEqual (2000, Approximate (2000, sock.SendTimeout), "SendTimeoutChange #2");
1123			
1124			sock.SendTimeout = 0;
1125			Assert.AreEqual (0, Approximate (0, sock.SendTimeout), "SendTimeoutChange #3");
1126			
1127			/* Should be the same as setting 0 */
1128			sock.SendTimeout = -1;
1129			Assert.AreEqual (0, sock.SendTimeout, "SendTimeoutChange #4");
1130
1131			sock.SendTimeout = 65536;
1132			Assert.AreEqual (65536, Approximate (65536, sock.SendTimeout), "SendTimeoutChange #5");
1133			
1134			try {
1135				sock.SendTimeout = -2;
1136				Assert.Fail ("SendTimeoutChange #8");
1137			} catch (ArgumentOutOfRangeException) {
1138			} catch {
1139				Assert.Fail ("SendTimeoutChange #9");
1140			} finally {
1141				sock.Close ();
1142			}
1143		}
1144
1145		[Test]
1146		[ExpectedException (typeof(ObjectDisposedException))]
1147		public void SendTimeoutClosed ()
1148		{
1149			Socket sock = new Socket (AddressFamily.InterNetwork,
1150						  SocketType.Stream,
1151						  ProtocolType.Tcp);
1152			
1153			sock.Close ();
1154			
1155			int val = sock.SendTimeout;
1156		}
1157		
1158		[Test]
1159		public void ReceiveTimeoutDefault ()
1160		{
1161			Socket sock = new Socket (AddressFamily.InterNetwork,
1162						  SocketType.Stream,
1163						  ProtocolType.Tcp);
1164			
1165			Assert.AreEqual (0, sock.ReceiveTimeout, "ReceiveTimeoutDefault");
1166			
1167			sock.Close ();
1168		}
1169
1170		[Test]
1171		public void ReceiveTimeoutChange ()
1172		{
1173			Socket sock = new Socket (AddressFamily.InterNetwork,
1174						  SocketType.Stream,
1175						  ProtocolType.Tcp);
1176			
1177			sock.ReceiveTimeout = 50;
1178			Assert.AreEqual (50, Approximate (50, sock.ReceiveTimeout), "ReceiveTimeoutChange #1");
1179			
1180			sock.ReceiveTimeout = 2000;
1181			Assert.AreEqual (2000, Approximate (2000, sock.ReceiveTimeout), "ReceiveTimeoutChange #2");
1182			
1183			sock.ReceiveTimeout = 0;
1184			Assert.AreEqual (0, sock.ReceiveTimeout, "ReceiveTimeoutChange #3");
1185			
1186			/* Should be the same as setting 0 */
1187			sock.ReceiveTimeout = -1;
1188			Assert.AreEqual (0, sock.ReceiveTimeout, "ReceiveTimeoutChange #4");
1189
1190			sock.ReceiveTimeout = 65536;
1191			Assert.AreEqual (65536, Approximate (65536, sock.ReceiveTimeout), "ReceiveTimeoutChange #5");
1192			
1193			try {
1194				sock.ReceiveTimeout = -2;
1195				Assert.Fail ("ReceiveTimeoutChange #8");
1196			} catch (ArgumentOutOfRangeException) {
1197			} catch {
1198				Assert.Fail ("ReceiveTimeoutChange #9");
1199			} finally {
1200				sock.Close ();
1201			}
1202		}
1203
1204		[Test]
1205		[ExpectedException (typeof(ObjectDisposedException))]
1206		public void ReceiveTimeoutClosed ()
1207		{
1208			Socket sock = new Socket (AddressFamily.InterNetwork,
1209						  SocketType.Stream,
1210						  ProtocolType.Tcp);
1211			
1212			sock.Close ();
1213			
1214			int val = sock.ReceiveTimeout;
1215		}
1216		
1217		[Test]
1218		public void NoDelayDefaultTcp ()
1219		{
1220			Socket sock = new Socket (AddressFamily.InterNetwork,
1221						  SocketType.Stream,
1222						  ProtocolType.Tcp);
1223			
1224			Assert.AreEqual (false, sock.NoDelay, "NoDelayDefaultTcp");
1225			
1226			sock.Close ();
1227		}
1228
1229		[Test]
1230		public void NoDelayChangeTcp ()
1231		{
1232			Socket sock = new Socket (AddressFamily.InterNetwork,
1233						  SocketType.Stream,
1234						  ProtocolType.Tcp);
1235			
1236			sock.NoDelay = true;
1237			
1238			Assert.AreEqual (true, sock.NoDelay, "NoDelayChangeTcp");
1239			
1240			sock.Close ();
1241		}
1242		
1243		[Test]
1244		public void NoDelayDefaultUdp ()
1245		{
1246			Socket sock = new Socket (AddressFamily.InterNetwork,
1247						  SocketType.Dgram,
1248						  ProtocolType.Udp);
1249			
1250			try {
1251				bool val = sock.NoDelay;
1252				Assert.Fail ("NoDelayDefaultUdp #1");
1253			} catch (SocketException ex) {
1254				Assert.AreEqual (10042, ex.ErrorCode,
1255						 "NoDelayDefaultUdp #2");
1256			} catch {
1257				Assert.Fail ("NoDelayDefaultUdp #3");
1258			} finally {
1259				sock.Close ();
1260			}
1261		}
1262
1263		[Test]
1264		public void NoDelayChangeUdp ()
1265		{
1266			Socket sock = new Socket (AddressFamily.InterNetwork,
1267						  SocketType.Dgram,
1268						  ProtocolType.Udp);
1269			
1270			try {
1271				sock.NoDelay = true;
1272				Assert.Fail ("NoDelayChangeUdp #1");
1273			} catch (SocketException ex) {
1274				Assert.AreEqual (10042, ex.ErrorCode,
1275						 "NoDelayChangeUdp #2");
1276			} catch {
1277				Assert.Fail ("NoDelayChangeUdp #3");
1278			} finally {
1279				sock.Close ();
1280			}
1281		}
1282		
1283		[Test]
1284		[ExpectedException (typeof(ObjectDisposedException))]
1285		public void NoDelayClosed ()
1286		{
1287			Socket sock = new Socket (AddressFamily.InterNetwork,
1288						  SocketType.Stream,
1289						  ProtocolType.Tcp);
1290			
1291			sock.Close ();
1292			
1293			bool val = sock.NoDelay;
1294		}
1295
1296		static bool BAAccepted = false;
1297		static Socket BASocket = null;
1298		static ManualResetEvent BACalledBack = new ManualResetEvent (false);
1299		
1300		private static void BACallback (IAsyncResult asyncResult)
1301		{
1302			Socket sock = (Socket)asyncResult.AsyncState;
1303			
1304			BASocket = sock.EndAccept (asyncResult);
1305			
1306			BAAccepted = true;
1307			BACalledBack.Set ();
1308		}
1309		
1310		[Test]
1311		[ExpectedException (typeof(InvalidOperationException))]
1312		public void BeginAcceptNotBound ()
1313		{
1314			Socket sock = new Socket (AddressFamily.InterNetwork,
1315						  SocketType.Stream,
1316						  ProtocolType.Tcp);
1317
1318			sock.BeginAccept (BACallback, sock);
1319			
1320			sock.Close ();
1321		}
1322		
1323		[Test]
1324		[ExpectedException (typeof(InvalidOperationException))]
1325		public void BeginAcceptNotListening ()
1326		{
1327			Socket sock = new Socket (AddressFamily.InterNetwork,
1328						  SocketType.Stream,
1329						  ProtocolType.Tcp);
1330
1331			sock.Bind (new IPEndPoint (IPAddress.Any, 1236));
1332			
1333			sock.BeginAccept (BACallback, sock);
1334			
1335			sock.Close ();
1336		}
1337
1338		[Test]
1339		public void BeginAccept ()
1340		{
1341			Socket sock = new Socket (AddressFamily.InterNetwork,
1342						  SocketType.Stream,
1343						  ProtocolType.Tcp);
1344			IPEndPoint ep = new IPEndPoint (IPAddress.Loopback,
1345							1237);
1346			
1347			sock.Bind (ep);
1348			sock.Listen (1);
1349			
1350			BACalledBack.Reset ();
1351			
1352			sock.BeginAccept (BACallback, sock);
1353
1354			Socket conn = new Socket (AddressFamily.InterNetwork,
1355						  SocketType.Stream,
1356						  ProtocolType.Tcp);
1357			
1358			conn.Connect (ep);
1359
1360			if (BACalledBack.WaitOne (2000, false) == false) {
1361				Assert.Fail ("BeginAccept wait timed out");
1362			}
1363			
1364			Assert.AreEqual (true, BAAccepted, "BeginAccept #1");
1365			Assert.AreEqual (true, BASocket.Connected, "BeginAccept #2");
1366			Assert.AreEqual (false, sock.Connected, "BeginAccept #3");
1367			Assert.AreEqual (true, conn.Connected, "BeginAccept #4");
1368
1369			BASocket.Close ();
1370			conn.Close ();
1371			sock.Close ();
1372		}
1373
1374		[Test]
1375		[ExpectedException (typeof(ObjectDisposedException))]
1376		public void BeginAcceptClosed ()
1377		{
1378			Socket sock = new Socket (AddressFamily.InterNetwork,
1379						  SocketType.Stream,
1380						  ProtocolType.Tcp);
1381			
1382			sock.Close ();
1383			
1384			sock.BeginAccept (BACallback, sock);
1385		}
1386
1387		static bool BADAccepted = false;
1388		static Socket BADSocket = null;
1389		static byte[] BADBytes;
1390		static int BADByteCount;
1391		static ManualResetEvent BADCalledBack = new ManualResetEvent (false);
1392		
1393		private static void BADCallback (IAsyncResult asyncResult)
1394		{
1395			Socket sock = (Socket)asyncResult.AsyncState;
1396			
1397			BADSocket = sock.EndAccept (out BADBytes,
1398						    out BADByteCount,
1399						    asyncResult);
1400			
1401			BADAccepted = true;
1402			BADCalledBack.Set ();
1403		}
1404
1405		[Test]
1406		public void BeginAcceptData ()
1407		{
1408			Socket sock = new Socket (AddressFamily.InterNetwork,
1409						  SocketType.Stream,
1410						  ProtocolType.Tcp);
1411			IPEndPoint ep = new IPEndPoint (IPAddress.Loopback,
1412							1238);
1413			
1414			sock.Bind (ep);
1415			sock.Listen (1);
1416			
1417			BADCalledBack.Reset ();
1418			
1419			sock.BeginAccept (256, BADCallback, sock);
1420
1421			Socket conn = new Socket (AddressFamily.InterNetwork,
1422						  SocketType.Stream,
1423						  ProtocolType.Tcp);
1424			byte[] send_bytes = new byte[] {10, 11, 12, 13};
1425			
1426			conn.Connect (ep);
1427			conn.Send (send_bytes);
1428
1429			if (BADCalledBack.WaitOne (2000, false) == false) {
1430				Assert.Fail ("BeginAcceptData wait timed out");
1431			}
1432			
1433			Assert.AreEqual (true, BADAccepted, "BeginAcceptData #1");
1434			Assert.AreEqual (true, BADSocket.Connected, "BeginAcceptData #2");
1435			Assert.AreEqual (false, sock.Connected, "BeginAcceptData #3");
1436			Assert.AreEqual (true, conn.Connected, "BeginAcceptData #4");
1437			Assert.AreEqual (send_bytes.Length, BADByteCount, "BeginAcceptData #5");
1438			
1439			/* The MS runtime gives the returned data in a
1440			 * much bigger array.  TODO: investigate
1441			 * whether it the size correlates to the first
1442			 * parameter in BeginAccept()
1443			 */
1444			Assert.IsFalse (BADBytes.Length == send_bytes.Length,
1445					"BeginAcceptData #6");
1446
1447			for(int i = 0; i < send_bytes.Length; i++) {
1448				Assert.AreEqual (send_bytes[i], BADBytes[i], "BeginAcceptData #" + (i+7).ToString ());
1449			}
1450
1451			BADSocket.Close ();
1452			conn.Close ();
1453			sock.Close ();
1454		}
1455
1456		[Test]
1457		[ExpectedException (typeof(ObjectDisposedException))]
1458		public void BeginAcceptDataClosed ()
1459		{
1460			Socket sock = new Socket (AddressFamily.InterNetwork,
1461						  SocketType.Stream,
1462						  ProtocolType.Tcp);
1463			
1464			sock.Close ();
1465			
1466			sock.BeginAccept (256, BADCallback, sock);
1467		}
1468
1469		[Test]
1470		public void BeginAcceptSocketUdp ()
1471		{
1472			Socket sock = new Socket (AddressFamily.InterNetwork,
1473						  SocketType.Stream,
1474						  ProtocolType.Tcp);
1475			Socket acc = new Socket (AddressFamily.InterNetwork,
1476						 SocketType.Dgram,
1477						 ProtocolType.Udp);
1478			
1479			IPEndPoint ep = new IPEndPoint (IPAddress.Loopback,
1480							1239);
1481			
1482			sock.Bind (ep);
1483			sock.Listen (1);
1484			
1485			try {
1486				sock.BeginAccept (acc, 256, BADCallback, sock);
1487				Assert.Fail ("BeginAcceptSocketUdp #1");
1488			} catch (SocketException ex) {
1489				Assert.AreEqual (10022, ex.ErrorCode, "BeginAcceptSocketUdp #2");
1490			} catch {
1491				Assert.Fail ("BeginAcceptSocketUdp #3");
1492			} finally {
1493				acc.Close ();
1494				sock.Close ();
1495			}
1496		}
1497		
1498		[Test]
1499		public void BeginAcceptSocketBound ()
1500		{
1501			Socket sock = new Socket (AddressFamily.InterNetwork,
1502						  SocketType.Stream,
1503						  ProtocolType.Tcp);
1504			Socket acc = new Socket (AddressFamily.InterNetwork,
1505						 SocketType.Stream,
1506						 ProtocolType.Tcp);
1507			
1508			IPEndPoint ep1 = new IPEndPoint (IPAddress.Loopback,
1509							 1240);
1510			
1511			IPEndPoint ep2 = new IPEndPoint (IPAddress.Loopback,
1512							 1241);
1513			
1514			sock.Bind (ep1);
1515			sock.Listen (1);
1516
1517			acc.Bind (ep2);
1518			
1519			try {
1520				sock.BeginAccept (acc, 256, BADCallback, sock);
1521				Assert.Fail ("BeginAcceptSocketBound #1");
1522			} catch (InvalidOperationException) {
1523			} catch {
1524				Assert.Fail ("BeginAcceptSocketBound #2");
1525			} finally {
1526				acc.Close ();
1527				sock.Close ();
1528			}
1529		}
1530		
1531		[Test]
1532		public void BeginAcceptSocket ()
1533		{
1534			Socket sock = new Socket (AddressFamily.InterNetwork,
1535						  SocketType.Stream,
1536						  ProtocolType.Tcp);
1537			Socket acc = new Socket (AddressFamily.InterNetwork,
1538						 SocketType.Stream,
1539						 ProtocolType.Tcp);
1540			
1541			IPEndPoint ep = new IPEndPoint (IPAddress.Loopback,
1542							1242);
1543			
1544			sock.Bind (ep);
1545			sock.Listen (1);
1546			
1547			BADCalledBack.Reset ();
1548			
1549			sock.BeginAccept (acc, 256, BADCallback, sock);
1550
1551			Socket conn = new Socket (AddressFamily.InterNetwork,
1552						  SocketType.Stream,
1553						  ProtocolType.Tcp);
1554			byte[] send_bytes = new byte[] {10, 11, 12, 13};
1555			
1556			conn.Connect (ep);
1557			conn.Send (send_bytes);
1558
1559			if (BADCalledBack.WaitOne (2000, false) == false) {
1560				Assert.Fail ("BeginAcceptSocket wait timed out");
1561			}
1562			
1563			Assert.AreEqual (true, BADAccepted, "BeginAcceptSocket #1");
1564			Assert.AreEqual (true, BADSocket.Connected, "BeginAcceptSocket #2");
1565			Assert.AreEqual (false, sock.Connected, "BeginAcceptSocket #3");
1566			Assert.AreEqual (true, conn.Connected, "BeginAcceptSocket #4");
1567			Assert.AreEqual (send_bytes.Length, BADByteCount, "BeginAcceptSocket #5");
1568			Assert.AreEqual (AddressFamily.InterNetwork, acc.AddressFamily, "BeginAcceptSocket #6");
1569			Assert.AreEqual (SocketType.Stream, acc.SocketType, "BeginAcceptSocket #7");
1570			Assert.AreEqual (ProtocolType.Tcp, acc.ProtocolType, "BeginAcceptSocket #8");
1571			Assert.AreEqual (conn.LocalEndPoint, acc.RemoteEndPoint, "BeginAcceptSocket #9");
1572			
1573			/* The MS runtime gives the returned data in a
1574			 * much bigger array.  TODO: investigate
1575			 * whether it the size correlates to the first
1576			 * parameter in BeginAccept()
1577			 */
1578			Assert.IsFalse (BADBytes.Length == send_bytes.Length,
1579					"BeginAcceptSocket #10");
1580
1581			for(int i = 0; i < send_bytes.Length; i++) {
1582				Assert.AreEqual (send_bytes[i], BADBytes[i], "BeginAcceptSocket #" + (i+11).ToString ());
1583			}
1584
1585			BADSocket.Close ();
1586			conn.Close ();
1587			acc.Close ();
1588			sock.Close ();
1589		}
1590
1591		[Test]
1592		public void BeginAcceptSocketClosed ()
1593		{
1594			Socket sock = new Socket (AddressFamily.InterNetwork,
1595						  SocketType.Stream,
1596						  ProtocolType.Tcp);
1597			Socket acc = new Socket (AddressFamily.InterNetwork,
1598						 SocketType.Stream,
1599						 ProtocolType.Tcp);
1600			
1601			sock.Close ();
1602			
1603			try {
1604				sock.BeginAccept (acc, 256, BADCallback, null);
1605				Assert.Fail ("BeginAcceptSocketClosed #1");
1606			} catch (ObjectDisposedException) {
1607			} catch {
1608				Assert.Fail ("BeginAcceptSocketClosed #2");
1609			} finally {
1610				acc.Close ();
1611			}
1612		}
1613
1614		[Test]
1615		public void BeginAcceptSocketAccClosed ()
1616		{
1617			Socket sock = new Socket (AddressFamily.InterNetwork,
1618						  SocketType.Stream,
1619						  ProtocolType.Tcp);
1620			Socket acc = new Socket (AddressFamily.InterNetwork,
1621						 SocketType.Stream,
1622						 ProtocolType.Tcp);
1623			IPEndPoint ep = new IPEndPoint (IPAddress.Loopback,
1624							1243);
1625
1626			sock.Bind (ep);
1627			sock.Listen (1);
1628			
1629			acc.Close ();
1630			
1631			BADCalledBack.Reset ();
1632			
1633			try {
1634				sock.BeginAccept (acc, 256, BADCallback, null);
1635				Assert.Fail ("BeginAcceptSocketAccClosed #1");
1636			} catch (ObjectDisposedException) {
1637			} catch {
1638				Assert.Fail ("BeginAcceptSocketAccClosed #2");
1639			} finally {
1640				sock.Close ();
1641			}
1642		}
1643		
1644		static bool BCConnected = false;
1645		static ManualResetEvent BCCalledBack = new ManualResetEvent (false);
1646		
1647		private static void BCCallback (IAsyncResult asyncResult)
1648		{
1649			Socket sock = (Socket)asyncResult.AsyncState;
1650			
1651			sock.EndConnect (asyncResult);
1652			BCConnected = true;
1653			
1654			BCCalledBack.Set ();
1655		}
1656		
1657		[Test]
1658		public void BeginConnectAddressPort ()
1659		{
1660			Socket sock = new Socket (AddressFamily.InterNetwork,
1661						  SocketType.Stream,
1662						  ProtocolType.Tcp);
1663			Socket listen = new Socket (AddressFamily.InterNetwork,
1664						    SocketType.Stream,
1665						    ProtocolType.Tcp);
1666			IPAddress ip = IPAddress.Loopback;
1667			IPEndPoint ep = new IPEndPoint (ip, 1244);
1668
1669			listen.Bind (ep);
1670			listen.Listen (1);
1671			
1672			BCCalledBack.Reset ();
1673			
1674			BCConnected = false;
1675			
1676			sock.BeginConnect (ip, 1244, BCCallback, sock);
1677
1678			if (BCCalledBack.WaitOne (2000, false) == false) {
1679				Assert.Fail ("BeginConnectAddressPort wait timed out");
1680			}
1681			
1682			Assert.AreEqual (true, BCConnected, "BeginConnectAddressPort #1");
1683			
1684			sock.Close ();
1685			listen.Close ();
1686		}
1687
1688		[Test]
1689		public void BeginConnectAddressPortNull ()
1690		{
1691			Socket sock = new Socket (AddressFamily.InterNetwork,
1692						  SocketType.Stream,
1693						  ProtocolType.Tcp);
1694			IPAddress ip = null;
1695
1696			try {
1697				sock.BeginConnect (ip, 1244, BCCallback,
1698						   sock);
1699				Assert.Fail ("BeginConnectAddressPortNull #1");
1700			} catch (ArgumentNullException) {
1701			} catch {
1702				Assert.Fail ("BeginConnectAddressPortNull #2");
1703			} finally {
1704				sock.Close ();
1705			}
1706		}
1707
1708		[Test]
1709		public void BeginConnectAddressPortListen ()
1710		{
1711			Socket sock = new Socket (AddressFamily.InterNetwork,
1712						  SocketType.Stream,
1713						  ProtocolType.Tcp);
1714			IPAddress ip = IPAddress.Loopback;
1715			IPEndPoint ep = new IPEndPoint (ip, 1245);
1716
1717			sock.Bind (ep);
1718			sock.Listen (1);
1719			
1720			try {
1721				sock.BeginConnect (ip, 1245, BCCallback, sock);
1722				Assert.Fail ("BeginConnectAddressPortListen #1");
1723			} catch (InvalidOperationException) {
1724			} catch {
1725				Assert.Fail ("BeginConnectAddressPortListen #2");
1726			} finally {
1727				sock.Close ();
1728			}
1729		}
1730		
1731		[Test]
1732		[ExpectedException (typeof(ObjectDisposedException))]
1733		public void BeginConnectAddressPortClosed ()
1734		{
1735			Socket sock = new Socket (AddressFamily.InterNetwork,
1736						  SocketType.Stream,
1737						  ProtocolType.Tcp);
1738			IPAddress ip = IPAddress.Loopback;
1739			
1740			sock.Close ();
1741			
1742			sock.BeginConnect (ip, 1244, BCCallback, sock);
1743		}
1744		
1745		[Test]
1746		[Category ("NotOnMac")]
1747		/*
1748		 * This is not a Mono bug.
1749		 * 
1750		 * By default, only 127.0.0.1 is enabled and you must explicitly
1751		 * enable additional addresses using 'sudo ifconfig lo0 alias 127.0.0.1'.
1752		 * 
1753		 * I tested this on Mac OS 10.7.4; a 'ping 127.0.0.2' does not work
1754		 * until I add that alias.
1755		 * 
1756		 */
1757		public void BeginConnectMultiple ()
1758		{
1759			Socket sock = new Socket (AddressFamily.InterNetwork,
1760						  SocketType.Stream,
1761						  ProtocolType.Tcp);
1762			Socket listen = new Socket (AddressFamily.InterNetwork,
1763						    SocketType.Stream,
1764						    ProtocolType.Tcp);
1765			IPEndPoint ep = new IPEndPoint (IPAddress.Loopback,
1766							1246);
1767			IPAddress[] ips = new IPAddress[4];
1768			
1769			ips[0] = IPAddress.Parse ("127.0.0.4");
1770			ips[1] = IPAddress.Parse ("127.0.0.3");
1771			ips[2] = IPAddress.Parse ("127.0.0.2");
1772			ips[3] = IPAddress.Parse ("127.0.0.1");
1773
1774			listen.Bind (ep);
1775			listen.Listen (1);
1776			
1777			BCCalledBack.Reset ();
1778			
1779			BCConnected = false;
1780			
1781			sock.BeginConnect (ips, 1246, BCCallback, sock);
1782			
1783			/* Longer wait here, because the ms runtime
1784			 * takes a lot longer to not connect
1785			 */
1786			if (BCCalledBack.WaitOne (10000, false) == false) {
1787				Assert.Fail ("BeginConnectMultiple wait failed");
1788			}
1789			
1790			Assert.AreEqual (true, BCConnected, "BeginConnectMultiple #1");
1791			Assert.AreEqual (AddressFamily.InterNetwork, sock.RemoteEndPoint.AddressFamily, "BeginConnectMultiple #2");
1792			IPEndPoint remep = (IPEndPoint)sock.RemoteEndPoint;
1793			
1794			Assert.AreEqual (IPAddress.Loopback, remep.Address, "BeginConnectMultiple #2");
1795			
1796			sock.Close ();
1797			listen.Close ();
1798		}
1799
1800		[Test]
1801		public void BeginConnectMultiple2 ()
1802		{
1803			Socket sock = new Socket (AddressFamily.InterNetwork,
1804						  SocketType.Stream,
1805						  ProtocolType.Tcp);
1806			Socket listen = new Socket (AddressFamily.InterNetwork,
1807						    SocketType.Stream,
1808						    ProtocolType.Tcp);
1809
1810			// Need at least two addresses.
1811			var ips = Dns.GetHostAddresses (string.Empty);
1812			if (ips.Length < 1)
1813				return;
1814
1815			var allIps = new IPAddress [ips.Length + 1];
1816			allIps [0] = IPAddress.Loopback;
1817			ips.CopyTo (allIps, 1);
1818
1819			/*
1820			 * Only bind to the loopback interface, so all the non-loopback
1821			 * IP addresses will fail.  BeginConnect()/EndConnect() should
1822			 * succeed it it can connect to at least one of the requested
1823			 * addresses.
1824			 */
1825			IPEndPoint ep = new IPEndPoint (IPAddress.Loopback, 1246);
1826
1827			listen.Bind (ep);
1828			listen.Listen (1);
1829			
1830			BCCalledBack.Reset ();
1831			
1832			BCConnected = false;
1833			
1834			sock.BeginConnect (allIps, 1246, BCCallback, sock);
1835			
1836			/* Longer wait here, because the ms runtime
1837			 * takes a lot longer to not connect
1838			 */
1839			if (BCCalledBack.WaitOne (10000, false) == false) {
1840				Assert.Fail ("BeginConnectMultiple2 wait failed");
1841			}
1842			
1843			Assert.AreEqual (true, BCConnected, "BeginConnectMultiple2 #1");
1844			Assert.AreEqual (AddressFamily.InterNetwork, sock.RemoteEndPoint.AddressFamily, "BeginConnectMultiple2 #2");
1845			IPEndPoint remep = (IPEndPoint)sock.RemoteEndPoint;
1846
1847			Assert.AreEqual (IPAddress.Loopback, remep.Address, "BeginConnectMultiple2 #2");
1848
1849			sock.Close ();
1850			listen.Close ();
1851		}
1852
1853
1854		[Test]
1855		public void BeginConnectMultipleNull ()
1856		{
1857			Socket sock = new Socket (AddressFamily.InterNetwork,
1858						  SocketType.Stream,
1859						  ProtocolType.Tcp);
1860			IPAddress[] ips = null;
1861			
1862			try {
1863				sock.BeginConnect (ips, 1246, BCCallback,
1864						   sock);
1865				Assert.Fail ("BeginConnectMultipleNull #1");
1866			} catch (ArgumentNullException) {
1867			} catch {
1868				Assert.Fail ("BeginConnectMultipleNull #2");
1869			} finally {
1870				sock.Close ();
1871			}
1872		}
1873
1874		[Test]
1875		public void BeginConnectMultipleListen ()
1876		{
1877			Socket sock = new Socket (AddressFamily.InterNetwork,
1878						  SocketType.Stream,
1879						  ProtocolType.Tcp);
1880			IPAddress[] ips = new IPAddress[4];
1881			IPEndPoint ep = new IPEndPoint (IPAddress.Loopback,
1882							1247);
1883			
1884			ips[0] = IPAddress.Parse ("127.0.0.4");
1885			ips[1] = IPAddress.Parse ("127.0.0.3");
1886			ips[2] = IPAddress.Parse ("127.0.0.2");
1887			ips[3] = IPAddress.Parse ("127.0.0.1");
1888			
1889			sock.Bind (ep);
1890			sock.Listen (1);
1891			
1892			try {
1893				sock.BeginConnect (ips, 1247, BCCallback,
1894						   sock);
1895				Assert.Fail ("BeginConnectMultipleListen #1");
1896			} catch (InvalidOperationException) {
1897			} catch {
1898				Assert.Fail ("BeginConnectMultipleListen #2");
1899			} finally {
1900				sock.Close ();
1901			}
1902		}
1903		
1904		[Test]
1905		[ExpectedException (typeof(ObjectDisposedException))]
1906		public void BeginConnectMultipleClosed ()
1907		{
1908			Socket sock = new Socket (AddressFamily.InterNetwork,
1909						  SocketType.Stream,
1910						  ProtocolType.Tcp);
1911			IPAddress[] ips = new IPAddress[4];
1912			
1913			ips[0] = IPAddress.Parse ("127.0.0.4");
1914			ips[1] = IPAddress.Parse ("127.0.0.3");
1915			ips[2] = IPAddress.Parse ("127.0.0.2");
1916			ips[3] = IPAddress.Parse ("127.0.0.1");
1917			
1918			sock.Close ();
1919			
1920			sock.BeginConnect (ips, 1247, BCCallback, sock);
1921		}
1922		
1923		[Test]
1924		public void BeginConnectHostPortNull ()
1925		{
1926			Socket sock = new Socket (AddressFamily.InterNetwork,
1927						  SocketType.Stream,
1928						  ProtocolType.Tcp);
1929			
1930			try {
1931				sock.BeginConnect ((string)null, 0,
1932						   BCCallback, sock);
1933				Assert.Fail ("BeginConnectHostPort #1");
1934			} catch (ArgumentNullException) {
1935			} catch {
1936				Assert.Fail ("BeginConnectHostPort #2");
1937			} finally {
1938				sock.Close ();
1939			}
1940		}
1941
1942		[Test]
1943		public void BeginConnectHostPortListen ()
1944		{
1945			Socket sock = new Socket (AddressFamily.InterNetwork,
1946						  SocketType.Stream,
1947						  ProtocolType.Tcp);
1948			IPAddress ip = IPAddress.Loopback;
1949			IPEndPoint ep = new IPEndPoint (ip, 1248);
1950			
1951			sock.Bind (ep);
1952			sock.Listen (1);
1953			
1954			try {
1955				sock.BeginConnect ("localhost", 1248,
1956						   BCCallback, sock);
1957				Assert.Fail ("BeginConnectHostPortListen #1");
1958			} catch (InvalidOperationException) {
1959			} catch {
1960				Assert.Fail ("BeginConnectHostPortListen #2");
1961			} finally {
1962				sock.Close ();
1963			}
1964		}
1965
1966		[Test]
1967		[Category ("NotWorking")] // Need to pick a non-IP AddressFamily that "works" on both mono and ms, this one only works on ms
1968		public void BeginConnectHostPortNotIP ()
1969		{
1970			Socket sock = new Socket (AddressFamily.NetBios,
1971						  SocketType.Seqpacket,
1972						  ProtocolType.Unspecified);
1973			
1974			try {
1975				sock.BeginConnect ("localhost", 0, BCCallback,
1976						   sock);
1977				Assert.Fail ("BeginConnectHostPortNotIP #1");
1978			} catch (NotSupportedException) {
1979			} catch {
1980				Assert.Fail ("BeginConnectHostPor…

Large files files are truncated, but you can click here to view the full file