| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679 |
- // System.Net.Sockets.SocketTest.cs
- //
- // Authors:
- // Brad Fitzpatrick ([email protected])
- // Gonzalo Paniagua Javier ([email protected])
- //
- // (C) Copyright 2003 Brad Fitzpatrick
- // Copyright (c) 2005 Novell, Inc. (http://www.novell.com)
- //
- using System;
- using System.Collections;
- using System.Threading;
- using System.Net;
- using System.Net.Sockets;
- using NUnit.Framework;
- 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]
- public void ConnectIPAddressAny ()
- {
- IPEndPoint ep = new IPEndPoint (IPAddress.Any, 0);
- try {
- using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp)) {
- s.Connect (ep);
- s.Close ();
- }
- Assert.Fail ("#1");
- } catch (SocketException ex) {
- Assert.AreEqual (10049, ex.ErrorCode, "#2");
- }
- try {
- using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) {
- s.Connect (ep);
- s.Close ();
- }
- Assert.Fail ("#3");
- } catch (SocketException ex) {
- Assert.AreEqual (10049, ex.ErrorCode, "#4");
- }
- }
- [Test]
- [Ignore ("Bug #75158")]
- public void IncompatibleAddress ()
- {
- IPEndPoint epIPv6 = new IPEndPoint (IPAddress.IPv6Any,
- 0);
- try {
- using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP)) {
- s.Connect (epIPv6);
- s.Close ();
- }
- Assert.Fail ("#1");
- } catch (SocketException ex) {
- #if !NET_2_0
- // invalid argument
- int expectedError = 10022;
- #else
- // address incompatible with protocol
- int expectedError = 10047;
- #endif
- Assert.AreEqual (expectedError, ex.ErrorCode,
- "#2");
- }
- }
- [Test]
- [Category ("InetAccess")]
- public void EndConnect ()
- {
- 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;
- try {
- sock.EndConnect (ar); // should raise an exception because connect was bogus
- } catch {
- gotException = true;
- }
- Assertion.AssertEquals ("A01", gotException, true);
- }
- [Test]
- [ExpectedException (typeof (ArgumentNullException))]
- public void SelectEmpty ()
- {
- ArrayList list = new ArrayList ();
- Socket.Select (list, list, list, 1000);
- }
-
- private bool BlockingConnect (bool block)
- {
- IPEndPoint ep = new IPEndPoint(IPAddress.Loopback, 1234);
- Socket server = new Socket(AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
- server.Bind(ep);
- server.Blocking=block;
- server.Listen(0);
- Socket conn = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
- conn.Connect (ep);
- Socket client = server.Accept();
- bool client_block = client.Blocking;
- client.Close();
- conn.Close();
- server.Close();
-
- return(client_block);
- }
- [Test]
- public void AcceptBlockingStatus()
- {
- bool block;
- block = BlockingConnect(true);
- Assertion.AssertEquals ("BlockingStatus01",
- block, true);
- block = BlockingConnect(false);
- Assertion.AssertEquals ("BlockingStatus02",
- block, false);
- }
- static bool CFAConnected = false;
- static ManualResetEvent CFACalledBack;
-
- private static void CFACallback (IAsyncResult asyncResult)
- {
- Socket sock = (Socket)asyncResult.AsyncState;
- CFAConnected = sock.Connected;
-
- if (sock.Connected) {
- sock.EndConnect (asyncResult);
- }
- CFACalledBack.Set ();
- }
- [Test]
- public void ConnectFailAsync ()
- {
- Socket sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
- sock.Blocking = false;
- CFACalledBack = new ManualResetEvent (false);
- CFACalledBack.Reset ();
- /* Need a port that is not being used for
- * anything...
- */
- sock.BeginConnect (new IPEndPoint (IPAddress.Loopback,
- 114),
- new AsyncCallback (CFACallback),
- sock);
- CFACalledBack.WaitOne ();
- Assertion.AssertEquals ("ConnectFail", CFAConnected,
- false);
- }
-
- #if !TARGET_JVM
- [Test]
- #if !NET_2_0
- [ExpectedException (typeof (ArgumentException))]
- #endif
- public void SetSocketOptionBoolean ()
- {
- IPEndPoint ep = new IPEndPoint (IPAddress.Loopback, 1);
- Socket sock = new Socket (ep.Address.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
- try {
- sock.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.KeepAlive, true);
- } finally {
- sock.Close ();
- }
- }
- #endif
- [Test]
- public void TestSelect1 ()
- {
- Socket srv = CreateServer ();
- ClientSocket clnt = new ClientSocket (srv.LocalEndPoint);
- Thread th = new Thread (new ThreadStart (clnt.ConnectSleepClose));
- Socket acc = null;
- try {
- th.Start ();
- acc = srv.Accept ();
- clnt.Write ();
- ArrayList list = new ArrayList ();
- ArrayList empty = new ArrayList ();
- list.Add (acc);
- Socket.Select (list, empty, empty, 100);
- Assertion.AssertEquals ("#01", 0, empty.Count);
- Assertion.AssertEquals ("#02", 1, list.Count);
- Socket.Select (empty, list, empty, 100);
- Assertion.AssertEquals ("#03", 0, empty.Count);
- Assertion.AssertEquals ("#04", 1, list.Count);
- Socket.Select (list, empty, empty, -1);
- Assertion.AssertEquals ("#05", 0, empty.Count);
- Assertion.AssertEquals ("#06", 1, list.Count);
- } finally {
- if (acc != null)
- acc.Close ();
- srv.Close ();
- }
- }
- static Socket CreateServer ()
- {
- Socket sock = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
- sock.Bind (new IPEndPoint (IPAddress.Loopback, 0));
- sock.Listen (1);
- return sock;
- }
- class ClientSocket {
- Socket sock;
- EndPoint ep;
- public ClientSocket (EndPoint ep)
- {
- this.ep = ep;
- sock = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
- }
- public void ConnectSleepClose ()
- {
- sock.Connect (ep);
- Thread.Sleep (2000);
- sock.Close ();
- }
- public void Write ()
- {
- byte [] b = new byte [10];
- sock.Send (b);
- }
- }
- byte[] buf = new byte[100];
- [Test]
- [ExpectedException (typeof (ObjectDisposedException))]
- public void Disposed1 ()
- {
- Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
- EndPoint ep = new IPEndPoint (IPAddress.Any, 31337);
- s.Close();
- s.ReceiveFrom (buf, ref ep);
- }
- [Test]
- [ExpectedException (typeof (ObjectDisposedException))]
- public void Disposed2 ()
- {
- Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
- s.Close();
- s.Blocking = true;
- }
- [Test]
- [ExpectedException (typeof (ObjectDisposedException))]
- public void Disposed3 ()
- {
- Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
- s.Close();
- s.GetSocketOption (0, 0);
- }
- [Test]
- [ExpectedException (typeof (ObjectDisposedException))]
- public void Disposed4 ()
- {
- Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
- s.Close();
- s.GetSocketOption (0, 0, null);
- }
- [Test]
- [ExpectedException (typeof (ObjectDisposedException))]
- public void Disposed5 ()
- {
- Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
- s.Close();
- s.GetSocketOption (0, 0, 0);
- }
- [Test]
- [ExpectedException (typeof (ObjectDisposedException))]
- public void Disposed6 ()
- {
- Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
- s.Close();
- s.Listen (5);
- }
- [Test]
- [ExpectedException (typeof (ObjectDisposedException))]
- public void Disposed7 ()
- {
- Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
- s.Close();
- s.Poll (100, 0);
- }
- [Test]
- [ExpectedException (typeof (ObjectDisposedException))]
- public void Disposed8 ()
- {
- Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
- s.Close();
- s.Receive (buf);
- }
- [Test]
- [ExpectedException (typeof (ObjectDisposedException))]
- public void Disposed9 ()
- {
- Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
- s.Close();
- s.Receive (buf, 0);
- }
- [Test]
- [ExpectedException (typeof (ObjectDisposedException))]
- public void Disposed10 ()
- {
- Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
- s.Close();
- s.Receive (buf, 10, 0);
- }
- [Test]
- [ExpectedException (typeof (ObjectDisposedException))]
- public void Disposed11 ()
- {
- Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
- EndPoint ep = new IPEndPoint (IPAddress.Any, 31337);
- s.Close();
- s.Receive (buf, 0, 10, 0);
- }
- [Test]
- [ExpectedException (typeof (ObjectDisposedException))]
- public void Disposed12 ()
- {
- Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
- EndPoint ep = new IPEndPoint (IPAddress.Any, 31337);
- s.Close();
- s.ReceiveFrom (buf, 0, ref ep);
- }
- [Test]
- [ExpectedException (typeof (ObjectDisposedException))]
- public void Disposed13 ()
- {
- Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
- EndPoint ep = new IPEndPoint (IPAddress.Any, 31337);
- s.Close();
- s.ReceiveFrom (buf, 10, 0, ref ep);
- }
- [Test]
- [ExpectedException (typeof (ObjectDisposedException))]
- public void Disposed14 ()
- {
- Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
- EndPoint ep = new IPEndPoint (IPAddress.Any, 31337);
- s.Close();
- s.ReceiveFrom (buf, 0, 10, 0, ref ep);
- }
- [Test]
- [ExpectedException (typeof (ObjectDisposedException))]
- public void Disposed15 ()
- {
- Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
- s.Close();
- s.Send (buf);
- }
- [Test]
- [ExpectedException (typeof (ObjectDisposedException))]
- public void Disposed16 ()
- {
- Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
- s.Close();
- s.Send (buf, 0);
- }
- [Test]
- [ExpectedException (typeof (ObjectDisposedException))]
- public void Disposed17 ()
- {
- Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
- s.Close();
- s.Send (buf, 10, 0);
- }
- [Test]
- [ExpectedException (typeof (ObjectDisposedException))]
- public void Disposed18 ()
- {
- Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
- EndPoint ep = new IPEndPoint (IPAddress.Any, 31337);
- s.Close();
- s.Send (buf, 0, 10, 0);
- }
- [Test]
- [ExpectedException (typeof (ObjectDisposedException))]
- public void Disposed19 ()
- {
- Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
- EndPoint ep = new IPEndPoint (IPAddress.Any, 31337);
- s.Close();
- s.SendTo (buf, 0, ep);
- }
- [Test]
- [ExpectedException (typeof (ObjectDisposedException))]
- public void Disposed20 ()
- {
- Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
- EndPoint ep = new IPEndPoint (IPAddress.Any, 31337);
- s.Close();
- s.SendTo (buf, 10, 0, ep);
- }
- [Test]
- [ExpectedException (typeof (ObjectDisposedException))]
- public void Disposed21 ()
- {
- Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
- EndPoint ep = new IPEndPoint (IPAddress.Any, 31337);
- s.Close();
- s.SendTo (buf, 0, 10, 0, ep);
- }
- [Test]
- [ExpectedException (typeof (ObjectDisposedException))]
- public void Disposed22 ()
- {
- Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
- EndPoint ep = new IPEndPoint (IPAddress.Any, 31337);
- s.Close();
- s.SendTo (buf, ep);
- }
- [Test]
- [ExpectedException (typeof (ObjectDisposedException))]
- public void Disposed23 ()
- {
- Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
- EndPoint ep = new IPEndPoint (IPAddress.Any, 31337);
- s.Close();
- s.Shutdown (0);
- }
- static ManualResetEvent SocketError_event = new ManualResetEvent (false);
- private static void SocketError_callback (IAsyncResult ar)
- {
- Socket sock = (Socket)ar.AsyncState;
-
- if(sock.Connected) {
- sock.EndConnect (ar);
- }
- SocketError_event.Set ();
- }
- [Test]
- public void SocketError ()
- {
- Socket sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
- IPEndPoint ep = new IPEndPoint (IPAddress.Loopback,
- BogusPort);
-
- SocketError_event.Reset ();
- sock.Blocking = false;
- sock.BeginConnect (ep, SocketError_callback,
- sock);
- if (SocketError_event.WaitOne (2000, false) == false) {
- Assert.Fail ("SocketError wait timed out");
- }
- Assertion.AssertEquals ("SocketError #1", false,
- sock.Connected);
- int error;
- error = (int)sock.GetSocketOption (SocketOptionLevel.Socket, SocketOptionName.Error);
- Assertion.AssertEquals ("SocketError #2", 10061,
- error);
- error = (int)sock.GetSocketOption (SocketOptionLevel.Socket, SocketOptionName.Error);
- Assertion.AssertEquals ("SocketError #3", 10061,
- error);
- sock.Close ();
- }
-
- #if NET_2_0
- [Test]
- public void SocketInformationCtor ()
- {
- }
-
- [Test]
- public void DontFragmentDefaultTcp ()
- {
- Socket sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
-
- Assertion.AssertEquals ("DontFragmentDefaultTcp",
- false, sock.DontFragment);
- sock.Close ();
- }
- [Test]
- public void DontFragmentChangeTcp ()
- {
- Socket sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
-
- sock.DontFragment = true;
-
- Assertion.AssertEquals ("DontFragmentChangeTcp",
- true, sock.DontFragment);
- sock.Close ();
- }
-
- [Test]
- public void DontFragmentDefaultUdp ()
- {
- Socket sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Dgram,
- ProtocolType.Udp);
-
- Assertion.AssertEquals ("DontFragmentDefaultUdp",
- false, sock.DontFragment);
- sock.Close ();
- }
- [Test]
- public void DontFragmentChangeUdp ()
- {
- Socket sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Dgram,
- ProtocolType.Udp);
-
- sock.DontFragment = true;
-
- Assertion.AssertEquals ("DontFragmentChangeUdp",
- true, sock.DontFragment);
- sock.Close ();
- }
- [Test]
- [ExpectedException (typeof(ObjectDisposedException))]
- public void DontFragmentClosed ()
- {
- Socket sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
-
- sock.Close ();
-
- bool val = sock.DontFragment;
- }
-
- [Test]
- [Category ("NotWorking")] // Need to pick a non-IP AddressFamily that "works" on both mono and ms, this one only works on ms
- public void DontFragment ()
- {
- Socket sock = new Socket (AddressFamily.NetBios,
- SocketType.Seqpacket,
- ProtocolType.Unspecified);
-
- try {
- sock.DontFragment = true;
- Assert.Fail ("DontFragment #1");
- } catch (NotSupportedException) {
- } catch {
- Assert.Fail ("DontFragment #2");
- } finally {
- sock.Close ();
- }
- }
-
- [Test]
- public void EnableBroadcastDefaultTcp ()
- {
- Socket sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
-
- try {
- bool value = sock.EnableBroadcast;
- Assert.Fail ("EnableBroadcastDefaultTcp #1");
- } catch (SocketException ex) {
- Assert.AreEqual (10042, ex.ErrorCode, "EnableBroadcastDefaultTcp #2");
- } catch {
- Assert.Fail ("EnableBroadcastDefaultTcp #2");
- } finally {
- sock.Close ();
- }
- }
- [Test]
- public void EnableBroadcastDefaultUdp ()
- {
- Socket sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Dgram,
- ProtocolType.Udp);
-
- Assertion.AssertEquals ("EnableBroadcastDefaultUdp",
- false, sock.EnableBroadcast);
- sock.Close ();
- }
-
- [Test]
- public void EnableBroadcastChangeTcp ()
- {
- Socket sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
-
- try {
- sock.EnableBroadcast = true;
- Assert.Fail ("EnableBroadcastChangeTcp #1");
- } catch (SocketException ex) {
- Assert.AreEqual (10042, ex.ErrorCode, "EnableBroadcastChangeTcp #2");
- } catch {
- Assert.Fail ("EnableBroadcastChangeTcp #2");
- } finally {
- sock.Close ();
- }
- }
-
- [Test]
- public void EnableBroadcastChangeUdp ()
- {
- Socket sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Dgram,
- ProtocolType.Udp);
-
- sock.EnableBroadcast = true;
-
- Assertion.AssertEquals ("EnableBroadcastChangeUdp",
- true, sock.EnableBroadcast);
- sock.Close ();
- }
- [Test]
- [ExpectedException (typeof(ObjectDisposedException))]
- public void EnableBroadcastClosed ()
- {
- Socket sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Dgram,
- ProtocolType.Udp);
-
- sock.Close ();
-
- bool val = sock.EnableBroadcast;
- }
- /* Can't test the default for ExclusiveAddressUse as
- * it's different on different versions and service
- * packs of windows
- */
- [Test]
- [Category ("NotWorking")] // Not supported on Linux
- public void ExclusiveAddressUseUnbound ()
- {
- Socket sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
-
- sock.ExclusiveAddressUse = true;
-
- Assertion.AssertEquals ("ExclusiveAddressUseUnbound",
- true,
- sock.ExclusiveAddressUse);
-
- sock.Close ();
- }
- [Test]
- [ExpectedException (typeof(InvalidOperationException))]
- [Category ("NotWorking")] // Not supported on Linux
- public void ExclusiveAddressUseBound ()
- {
- Socket sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
-
- sock.Bind (new IPEndPoint (IPAddress.Any, 1235));
- sock.ExclusiveAddressUse = true;
- sock.Close ();
- }
- [Test]
- [ExpectedException (typeof(ObjectDisposedException))]
- public void ExclusiveAddressUseClosed ()
- {
- Socket sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
-
- sock.Close ();
-
- bool val = sock.ExclusiveAddressUse;
- }
-
- [Test]
- public void IsBoundTcp ()
- {
- Socket sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
- IPEndPoint ep = new IPEndPoint (IPAddress.Loopback,
- BogusPort);
-
- Assertion.AssertEquals ("IsBoundTcp #1", false,
- sock.IsBound);
-
- sock.Bind (ep);
- Assertion.AssertEquals ("IsBoundTcp #2", true,
- sock.IsBound);
- sock.Listen (1);
-
- Socket sock2 = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
-
- Assertion.AssertEquals ("IsBoundTcp #3", false,
- sock2.IsBound);
-
- sock2.Connect (ep);
- Assertion.AssertEquals ("IsBoundTcp #4", true,
- sock2.IsBound);
-
- sock2.Close ();
- Assertion.AssertEquals ("IsBoundTcp #5", true,
- sock2.IsBound);
- sock.Close ();
- Assertion.AssertEquals ("IsBoundTcp #6", true,
- sock.IsBound);
- }
- [Test]
- public void IsBoundUdp ()
- {
- Socket sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Dgram,
- ProtocolType.Udp);
- IPEndPoint ep = new IPEndPoint (IPAddress.Loopback,
- BogusPort);
-
- Assertion.AssertEquals ("IsBoundUdp #1", false,
- sock.IsBound);
-
- sock.Bind (ep);
- Assertion.AssertEquals ("IsBoundUdp #2", true,
- sock.IsBound);
-
- sock.Close ();
- Assertion.AssertEquals ("IsBoundUdp #3", true,
- sock.IsBound);
-
- sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Dgram,
- ProtocolType.Udp);
-
- Assertion.AssertEquals ("IsBoundUdp #4", false,
- sock.IsBound);
-
- sock.Connect (ep);
- Assertion.AssertEquals ("IsBoundUdp #5", true,
- sock.IsBound);
-
- sock.Close ();
- Assertion.AssertEquals ("IsBoundUdp #6", true,
- sock.IsBound);
- }
- [Test]
- /* Should not throw an exception */
- public void IsBoundClosed ()
- {
- Socket sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
-
- sock.Close ();
-
- bool val = sock.IsBound;
- }
-
- /* Nothing much to test for LingerState */
-
- [Test]
- public void MulticastLoopbackDefaultTcp ()
- {
- Socket sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
-
- try {
- bool value = sock.MulticastLoopback;
- Assert.Fail ("MulticastLoopbackDefaultTcp #1");
- } catch (SocketException ex) {
- Assert.AreEqual (10042, ex.ErrorCode, "MulticastLoopbackDefaultTcp #2");
- } catch {
- Assert.Fail ("MulticastLoopbackDefaultTcp #2");
- } finally {
- sock.Close ();
- }
- }
- [Test]
- public void MulticastLoopbackChangeTcp ()
- {
- Socket sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
-
- try {
- sock.MulticastLoopback = false;
- Assert.Fail ("MulticastLoopbackChangeTcp #1");
- } catch (SocketException ex) {
- Assert.AreEqual (10042, ex.ErrorCode, "MulticastLoopbackChangeTcp #2");
- } catch {
- Assert.Fail ("MulticastLoopbackChangeTcp #2");
- } finally {
- sock.Close ();
- }
- }
-
- [Test]
- public void MulticastLoopbackDefaultUdp ()
- {
- Socket sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Dgram,
- ProtocolType.Udp);
-
- Assertion.AssertEquals ("MulticastLoopbackDefaultUdp",
- true, sock.MulticastLoopback);
-
- sock.Close ();
- }
-
- [Test]
- public void MulticastLoopbackChangeUdp ()
- {
- Socket sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Dgram,
- ProtocolType.Udp);
-
- sock.MulticastLoopback = false;
-
- Assertion.AssertEquals ("MulticastLoopbackChangeUdp",
- false, sock.MulticastLoopback);
-
- sock.Close ();
- }
- [Test]
- [ExpectedException (typeof(ObjectDisposedException))]
- public void MulticastLoopbackClosed ()
- {
- Socket sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
-
- sock.Close ();
-
- bool val = sock.MulticastLoopback;
- }
-
- /* OSSupportsIPv6 depends on the environment */
-
- [Test]
- public void ReceiveBufferSizeDefault ()
- {
- Socket sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
-
- Assertion.AssertEquals ("ReceiveBufferSizeDefault",
- 8192, sock.ReceiveBufferSize);
-
- sock.Close ();
- }
-
- [Test]
- public void ReceiveBufferSizeDefaultUdp ()
- {
- Socket sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Dgram,
- ProtocolType.Udp);
-
- Assertion.AssertEquals ("ReceiveBufferSizeDefaultUdp",
- 8192, sock.ReceiveBufferSize);
-
- sock.Close ();
- }
- [Test]
- public void ReceiveBufferSizeChange ()
- {
- Socket sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
-
- sock.ReceiveBufferSize = 16384;
-
- Assertion.AssertEquals ("ReceiveBufferSizeChange",
- 16384, sock.ReceiveBufferSize);
-
- sock.Close ();
- }
- [Test]
- [ExpectedException (typeof(ObjectDisposedException))]
- public void ReceiveBufferSizeClosed ()
- {
- Socket sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
-
- sock.Close ();
-
- int val = sock.ReceiveBufferSize;
- }
-
- [Test]
- public void SendBufferSizeDefault ()
- {
- Socket sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
-
- Assertion.AssertEquals ("SendBufferSizeDefault",
- 8192, sock.SendBufferSize);
-
- sock.Close ();
- }
-
- [Test]
- public void SendBufferSizeDefaultUdp ()
- {
- Socket sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Dgram,
- ProtocolType.Udp);
-
- Assertion.AssertEquals ("SendBufferSizeDefaultUdp",
- 8192, sock.SendBufferSize);
-
- sock.Close ();
- }
- [Test]
- public void SendBufferSizeChange ()
- {
- Socket sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
-
- sock.SendBufferSize = 16384;
-
- Assertion.AssertEquals ("SendBufferSizeChange",
- 16384, sock.SendBufferSize);
-
- sock.Close ();
- }
- [Test]
- [ExpectedException (typeof(ObjectDisposedException))]
- public void SendBufferSizeClosed ()
- {
- Socket sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
-
- sock.Close ();
-
- int val = sock.SendBufferSize;
- }
-
- /* No test for TTL default as it's platform dependent */
- [Test]
- public void TtlChange ()
- {
- Socket sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
-
- sock.Ttl = 255;
-
- Assertion.AssertEquals ("TtlChange", 255, sock.Ttl);
-
- sock.Close ();
- }
- [Test]
- public void TtlChangeOverflow ()
- {
- Socket sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
-
- try {
- sock.Ttl = 256;
- Assert.Fail ("TtlChangeOverflow #1");
- } catch (SocketException ex) {
- Assert.AreEqual (10022, ex.ErrorCode,
- "TtlChangeOverflow #2");
- } catch {
- Assert.Fail ("TtlChangeoverflow #3");
- } finally {
- sock.Close ();
- }
- }
-
- /* Apparently you can set TTL=0 on the ms runtime!!
- try {
- sock.Ttl = 0;
- Assert.Fail ("TtlChangeOverflow #4");
- } catch (SocketException ex) {
- Assert.AreEqual (10022, ex.ErrorCode,
- "TtlChangeOverflow #5");
- } catch {
- Assert.Fail ("TtlChangeOverflow #6");
- } finally {
- sock.Close ();
- }
- */
- [Test]
- [ExpectedException (typeof(ObjectDisposedException))]
- public void TtlClosed ()
- {
- Socket sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
-
- sock.Close ();
-
- int val = sock.Ttl;
- }
-
- [Test]
- public void UseOnlyOverlappedIODefault ()
- {
- Socket sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
-
- Assertion.AssertEquals ("UseOnlyOverlappedIODefault",
- false,
- sock.UseOnlyOverlappedIO);
-
- sock.Close ();
- }
- //
- // We need this because the Linux kernel in certain configurations
- // will end up rounding up the values passed on to the kernel
- // for socket send/receive timeouts.
- //
- int Approximate (int target, int value)
- {
- int epsilon = 10;
-
- if (value > target-10 && value < target+10)
- return target;
- return value;
- }
-
- [Test]
- public void UseOnlyOverlappedIOChange ()
- {
- Socket sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
-
- sock.UseOnlyOverlappedIO = true;
-
- Assertion.AssertEquals ("UseOnlyOverlappedIOChange",
- true,
- sock.UseOnlyOverlappedIO);
-
- sock.Close ();
- }
- [Test]
- /* Should not throw an exception */
- public void UseOnlyOverlappedIOClosed ()
- {
- Socket sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
-
- sock.Close ();
-
- bool val = sock.UseOnlyOverlappedIO;
- }
-
- [Test]
- public void SendTimeoutDefault ()
- {
- Socket sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
-
- Assertion.AssertEquals ("SendTimeoutDefault",
- 0, sock.SendTimeout);
-
- sock.Close ();
- }
- [Test]
- public void SendTimeoutChange ()
- {
- Socket sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
-
- /* Should be rounded up to 500, according to
- * the MSDN docs, but the MS runtime doesn't
- */
- sock.SendTimeout = 50;
- Assertion.AssertEquals ("SendTimeoutChange #1",
- 50, Approximate (50, sock.SendTimeout));
-
- sock.SendTimeout = 2000;
- Assertion.AssertEquals ("SendTimeoutChange #2",
- 2000, Approximate (2000, sock.SendTimeout));
-
- sock.SendTimeout = 0;
- Assertion.AssertEquals ("SendTimeoutChange #3",
- 0, Approximate (0, sock.SendTimeout));
-
- /* Should be the same as setting 0 */
- sock.SendTimeout = -1;
- Assertion.AssertEquals ("SendTimeoutChange #4",
- 0, sock.SendTimeout);
- sock.SendTimeout = 65536;
- Assertion.AssertEquals ("SendTimeoutChange #5",
- 65536, Approximate (65536, sock.SendTimeout));
-
- try {
- sock.SendTimeout = -2;
- Assert.Fail ("SendTimeoutChange #8");
- } catch (ArgumentOutOfRangeException) {
- } catch {
- Assert.Fail ("SendTimeoutChange #9");
- } finally {
- sock.Close ();
- }
- }
- [Test]
- [ExpectedException (typeof(ObjectDisposedException))]
- public void SendTimeoutClosed ()
- {
- Socket sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
-
- sock.Close ();
-
- int val = sock.SendTimeout;
- }
-
- [Test]
- public void ReceiveTimeoutDefault ()
- {
- Socket sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
-
- Assertion.AssertEquals ("ReceiveTimeoutDefault",
- 0, sock.ReceiveTimeout);
-
- sock.Close ();
- }
- [Test]
- public void ReceiveTimeoutChange ()
- {
- Socket sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
-
- sock.ReceiveTimeout = 50;
- Assertion.AssertEquals ("ReceiveTimeoutChange #1",
- 50, Approximate (50, sock.ReceiveTimeout));
-
- sock.ReceiveTimeout = 2000;
- Assertion.AssertEquals ("ReceiveTimeoutChange #2",
- 2000, Approximate (2000, sock.ReceiveTimeout));
-
- sock.ReceiveTimeout = 0;
- Assertion.AssertEquals ("ReceiveTimeoutChange #3",
- 0, sock.ReceiveTimeout);
-
- /* Should be the same as setting 0 */
- sock.ReceiveTimeout = -1;
- Assertion.AssertEquals ("ReceiveTimeoutChange #4",
- 0, sock.ReceiveTimeout);
- sock.ReceiveTimeout = 65536;
- Assertion.AssertEquals ("ReceiveTimeoutChange #5",
- 65536, Approximate (65536, sock.ReceiveTimeout));
-
- try {
- sock.ReceiveTimeout = -2;
- Assert.Fail ("ReceiveTimeoutChange #8");
- } catch (ArgumentOutOfRangeException) {
- } catch {
- Assert.Fail ("ReceiveTimeoutChange #9");
- } finally {
- sock.Close ();
- }
- }
- [Test]
- [ExpectedException (typeof(ObjectDisposedException))]
- public void ReceiveTimeoutClosed ()
- {
- Socket sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
-
- sock.Close ();
-
- int val = sock.ReceiveTimeout;
- }
-
- [Test]
- public void NoDelayDefaultTcp ()
- {
- Socket sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
-
- Assertion.AssertEquals ("NoDelayDefaultTcp", false,
- sock.NoDelay);
-
- sock.Close ();
- }
- [Test]
- public void NoDelayChangeTcp ()
- {
- Socket sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
-
- sock.NoDelay = true;
-
- Assertion.AssertEquals ("NoDelayChangeTcp", true,
- sock.NoDelay);
-
- sock.Close ();
- }
-
- [Test]
- public void NoDelayDefaultUdp ()
- {
- Socket sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Dgram,
- ProtocolType.Udp);
-
- try {
- bool val = sock.NoDelay;
- Assert.Fail ("NoDelayDefaultUdp #1");
- } catch (SocketException ex) {
- Assert.AreEqual (10042, ex.ErrorCode,
- "NoDelayDefaultUdp #2");
- } catch {
- Assert.Fail ("NoDelayDefaultUdp #3");
- } finally {
- sock.Close ();
- }
- }
- [Test]
- public void NoDelayChangeUdp ()
- {
- Socket sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Dgram,
- ProtocolType.Udp);
-
- try {
- sock.NoDelay = true;
- Assert.Fail ("NoDelayChangeUdp #1");
- } catch (SocketException ex) {
- Assert.AreEqual (10042, ex.ErrorCode,
- "NoDelayChangeUdp #2");
- } catch {
- Assert.Fail ("NoDelayChangeUdp #3");
- } finally {
- sock.Close ();
- }
- }
-
- [Test]
- [ExpectedException (typeof(ObjectDisposedException))]
- public void NoDelayClosed ()
- {
- Socket sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
-
- sock.Close ();
-
- bool val = sock.NoDelay;
- }
- static bool BAAccepted = false;
- static Socket BASocket = null;
- static ManualResetEvent BACalledBack = new ManualResetEvent (false);
-
- private static void BACallback (IAsyncResult asyncResult)
- {
- Socket sock = (Socket)asyncResult.AsyncState;
-
- BASocket = sock.EndAccept (asyncResult);
-
- BAAccepted = true;
- BACalledBack.Set ();
- }
-
- [Test]
- [ExpectedException (typeof(InvalidOperationException))]
- public void BeginAcceptNotBound ()
- {
- Socket sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
- sock.BeginAccept (BACallback, sock);
-
- sock.Close ();
- }
-
- [Test]
- [ExpectedException (typeof(InvalidOperationException))]
- public void BeginAcceptNotListening ()
- {
- Socket sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
- sock.Bind (new IPEndPoint (IPAddress.Any, 1236));
-
- sock.BeginAccept (BACallback, sock);
-
- sock.Close ();
- }
- [Test]
- public void BeginAccept ()
- {
- Socket sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
- IPEndPoint ep = new IPEndPoint (IPAddress.Loopback,
- 1237);
-
- sock.Bind (ep);
- sock.Listen (1);
-
- BACalledBack.Reset ();
-
- sock.BeginAccept (BACallback, sock);
- Socket conn = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
-
- conn.Connect (ep);
- if (BACalledBack.WaitOne (2000, false) == false) {
- Assert.Fail ("BeginAccept wait timed out");
- }
-
- Assertion.AssertEquals ("BeginAccept #1", true,
- BAAccepted);
- Assertion.AssertEquals ("BeginAccept #2", true,
- BASocket.Connected);
- Assertion.AssertEquals ("BeginAccept #3", false,
- sock.Connected);
- Assertion.AssertEquals ("BeginAccept #4", true,
- conn.Connected);
- BASocket.Close ();
- conn.Close ();
- sock.Close ();
- }
- [Test]
- [ExpectedException (typeof(ObjectDisposedException))]
- public void BeginAcceptClosed ()
- {
- Socket sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
-
- sock.Close ();
-
- sock.BeginAccept (BACallback, sock);
- }
- static bool BADAccepted = false;
- static Socket BADSocket = null;
- static byte[] BADBytes;
- static int BADByteCount;
- static ManualResetEvent BADCalledBack = new ManualResetEvent (false);
-
- private static void BADCallback (IAsyncResult asyncResult)
- {
- Socket sock = (Socket)asyncResult.AsyncState;
-
- BADSocket = sock.EndAccept (out BADBytes,
- out BADByteCount,
- asyncResult);
-
- BADAccepted = true;
- BADCalledBack.Set ();
- }
- [Test]
- public void BeginAcceptData ()
- {
- Socket sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
- IPEndPoint ep = new IPEndPoint (IPAddress.Loopback,
- 1238);
-
- sock.Bind (ep);
- sock.Listen (1);
-
- BADCalledBack.Reset ();
-
- sock.BeginAccept (256, BADCallback, sock);
- Socket conn = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
- byte[] send_bytes = new byte[] {10, 11, 12, 13};
-
- conn.Connect (ep);
- conn.Send (send_bytes);
- if (BADCalledBack.WaitOne (2000, false) == false) {
- Assert.Fail ("BeginAcceptData wait timed out");
- }
-
- Assertion.AssertEquals ("BeginAcceptData #1", true,
- BADAccepted);
- Assertion.AssertEquals ("BeginAcceptData #2", true,
- BADSocket.Connected);
- Assertion.AssertEquals ("BeginAcceptData #3", false,
- sock.Connected);
- Assertion.AssertEquals ("BeginAcceptData #4", true,
- conn.Connected);
- Assertion.AssertEquals ("BeginAcceptData #5",
- send_bytes.Length,
- BADByteCount);
-
- /* The MS runtime gives the returned data in a
- * much bigger array. TODO: investigate
- * whether it the size correlates to the first
- * parameter in BeginAccept()
- */
- Assert.IsFalse (BADBytes.Length == send_bytes.Length,
- "BeginAcceptData #6");
- for(int i = 0; i < send_bytes.Length; i++) {
- Assertion.AssertEquals ("BeginAcceptData #" + (i+7).ToString (), send_bytes[i], BADBytes[i]);
- }
- BADSocket.Close ();
- conn.Close ();
- sock.Close ();
- }
- [Test]
- [ExpectedException (typeof(ObjectDisposedException))]
- public void BeginAcceptDataClosed ()
- {
- Socket sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
-
- sock.Close ();
-
- sock.BeginAccept (256, BADCallback, sock);
- }
- [Test]
- public void BeginAcceptSocketUdp ()
- {
- Socket sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
- Socket acc = new Socket (AddressFamily.InterNetwork,
- SocketType.Dgram,
- ProtocolType.Udp);
-
- IPEndPoint ep = new IPEndPoint (IPAddress.Loopback,
- 1239);
-
- sock.Bind (ep);
- sock.Listen (1);
-
- try {
- sock.BeginAccept (acc, 256, BADCallback, sock);
- Assert.Fail ("BeginAcceptSocketUdp #1");
- } catch (SocketException ex) {
- Assertion.AssertEquals ("BeginAcceptSocketUdp #2", 10022, ex.ErrorCode);
- } catch {
- Assert.Fail ("BeginAcceptSocketUdp #3");
- } finally {
- acc.Close ();
- sock.Close ();
- }
- }
-
- [Test]
- public void BeginAcceptSocketBound ()
- {
- Socket sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
- Socket acc = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
-
- IPEndPoint ep1 = new IPEndPoint (IPAddress.Loopback,
- 1240);
-
- IPEndPoint ep2 = new IPEndPoint (IPAddress.Loopback,
- 1241);
-
- sock.Bind (ep1);
- sock.Listen (1);
- acc.Bind (ep2);
-
- try {
- sock.BeginAccept (acc, 256, BADCallback, sock);
- Assert.Fail ("BeginAcceptSocketBound #1");
- } catch (InvalidOperationException) {
- } catch {
- Assert.Fail ("BeginAcceptSocketBound #2");
- } finally {
- acc.Close ();
- sock.Close ();
- }
- }
-
- [Test]
- public void BeginAcceptSocket ()
- {
- Socket sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
- Socket acc = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
-
- IPEndPoint ep = new IPEndPoint (IPAddress.Loopback,
- 1242);
-
- sock.Bind (ep);
- sock.Listen (1);
-
- BADCalledBack.Reset ();
-
- sock.BeginAccept (acc, 256, BADCallback, sock);
- Socket conn = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
- byte[] send_bytes = new byte[] {10, 11, 12, 13};
-
- conn.Connect (ep);
- conn.Send (send_bytes);
- if (BADCalledBack.WaitOne (2000, false) == false) {
- Assert.Fail ("BeginAcceptSocket wait timed out");
- }
-
- Assertion.AssertEquals ("BeginAcceptSocket #1", true,
- BADAccepted);
- Assertion.AssertEquals ("BeginAcceptSocket #2", true,
- BADSocket.Connected);
- Assertion.AssertEquals ("BeginAcceptSocket #3", false,
- sock.Connected);
- Assertion.AssertEquals ("BeginAcceptSocket #4", true,
- conn.Connected);
- Assertion.AssertEquals ("BeginAcceptSocket #5",
- send_bytes.Length,
- BADByteCount);
- Assertion.AssertEquals ("BeginAcceptSocket #6",
- AddressFamily.InterNetwork,
- acc.AddressFamily);
- Assertion.AssertEquals ("BeginAcceptSocket #7",
- SocketType.Stream,
- acc.SocketType);
- Assertion.AssertEquals ("BeginAcceptSocket #8",
- ProtocolType.Tcp,
- acc.ProtocolType);
- Assertion.AssertEquals ("BeginAcceptSocket #9",
- conn.LocalEndPoint,
- acc.RemoteEndPoint);
-
- /* The MS runtime gives the returned data in a
- * much bigger array. TODO: investigate
- * whether it the size correlates to the first
- * parameter in BeginAccept()
- */
- Assert.IsFalse (BADBytes.Length == send_bytes.Length,
- "BeginAcceptSocket #10");
- for(int i = 0; i < send_bytes.Length; i++) {
- Assertion.AssertEquals ("BeginAcceptSocket #" + (i+11).ToString (), send_bytes[i], BADBytes[i]);
- }
- BADSocket.Close ();
- conn.Close ();
- acc.Close ();
- sock.Close ();
- }
- [Test]
- public void BeginAcceptSocketClosed ()
- {
- Socket sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
- Socket acc = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
-
- sock.Close ();
-
- try {
- sock.BeginAccept (acc, 256, BADCallback, null);
- Assert.Fail ("BeginAcceptSocketClosed #1");
- } catch (ObjectDisposedException) {
- } catch {
- Assert.Fail ("BeginAcceptSocketClosed #2");
- } finally {
- acc.Close ();
- }
- }
- [Test]
- public void BeginAcceptSocketAccClosed ()
- {
- Socket sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
- Socket acc = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
- IPEndPoint ep = new IPEndPoint (IPAddress.Loopback,
- 1243);
- sock.Bind (ep);
- sock.Listen (1);
-
- acc.Close ();
-
- BADCalledBack.Reset ();
-
- try {
- sock.BeginAccept (acc, 256, BADCallback, null);
- Assert.Fail ("BeginAcceptSocketAccClosed #1");
- } catch (ObjectDisposedException) {
- } catch {
- Assert.Fail ("BeginAcceptSocketAccClosed #2");
- } finally {
- sock.Close ();
- }
- }
-
- static bool BCConnected = false;
- static ManualResetEvent BCCalledBack = new ManualResetEvent (false);
-
- private static void BCCallback (IAsyncResult asyncResult)
- {
- Socket sock = (Socket)asyncResult.AsyncState;
-
- sock.EndConnect (asyncResult);
- BCConnected = true;
-
- BCCalledBack.Set ();
- }
-
- [Test]
- public void BeginConnectAddressPort ()
- {
- Socket sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
- Socket listen = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
- IPAddress ip = IPAddress.Loopback;
- IPEndPoint ep = new IPEndPoint (ip, 1244);
- listen.Bind (ep);
- listen.Listen (1);
-
- BCCalledBack.Reset ();
-
- BCConnected = false;
-
- sock.BeginConnect (ip, 1244, BCCallback, sock);
- if (BCCalledBack.WaitOne (2000, false) == false) {
- Assert.Fail ("BeginConnectAddressPort wait timed out");
- }
-
- Assertion.AssertEquals ("BeginConnectAddressPort #1",
- true, BCConnected);
-
- sock.Close ();
- listen.Close ();
- }
- [Test]
- public void BeginConnectAddressPortNull ()
- {
- Socket sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
- IPAddress ip = null;
- try {
- sock.BeginConnect (ip, 1244, BCCallback,
- sock);
- Assert.Fail ("BeginConnectAddressPortNull #1");
- } catch (ArgumentNullException) {
- } catch {
- Assert.Fail ("BeginConnectAddressPortNull #2");
- } finally {
- sock.Close ();
- }
- }
- [Test]
- public void BeginConnectAddressPortListen ()
- {
- Socket sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
- IPAddress ip = IPAddress.Loopback;
- IPEndPoint ep = new IPEndPoint (ip, 1245);
- sock.Bind (ep);
- sock.Listen (1);
-
- try {
- sock.BeginConnect (ip, 1245, BCCallback, sock);
- Assert.Fail ("BeginConnectAddressPortListen #1");
- } catch (InvalidOperationException) {
- } catch {
- Assert.Fail ("BeginConnectAddressPortListen #2");
- } finally {
- sock.Close ();
- }
- }
-
- [Test]
- [ExpectedException (typeof(ObjectDisposedException))]
- public void BeginConnectAddressPortClosed ()
- {
- Socket sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
- IPAddress ip = IPAddress.Loopback;
-
- sock.Close ();
-
- sock.BeginConnect (ip, 1244, BCCallback, sock);
- }
-
- [Test]
- public void BeginConnectMultiple ()
- {
- Socket sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
- Socket listen = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
- IPEndPoint ep = new IPEndPoint (IPAddress.Loopback,
- 1246);
- IPAddress[] ips = new IPAddress[4];
-
- ips[0] = IPAddress.Parse ("127.0.0.4");
- ips[1] = IPAddress.Parse ("127.0.0.3");
- ips[2] = IPAddress.Parse ("127.0.0.2");
- ips[3] = IPAddress.Parse ("127.0.0.1");
- listen.Bind (ep);
- listen.Listen (1);
-
- BCCalledBack.Reset ();
-
- BCConnected = false;
-
- sock.BeginConnect (ips, 1246, BCCallback, sock);
-
- /* Longer wait here, because the ms runtime
- * takes a lot longer to not connect
- */
- if (BCCalledBack.WaitOne (10000, false) == false) {
- Assert.Fail ("BeginConnectMultiple wait failed");
- }
-
- Assertion.AssertEquals ("BeginConnectMultiple #1",
- true, BCConnected);
- Assertion.AssertEquals ("BeginConnectMultiple #2",
- AddressFamily.InterNetwork,
- sock.RemoteEndPoint.AddressFamily);
- IPEndPoint remep = (IPEndPoint)sock.RemoteEndPoint;
-
- Assertion.AssertEquals ("BeginConnectMultiple #2",
- IPAddress.Loopback,
- remep.Address);
-
- sock.Close ();
- listen.Close ();
- }
- [Test]
- public void BeginConnectMultipleNull ()
- {
- Socket sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
- IPAddress[] ips = null;
-
- try {
- sock.BeginConnect (ips, 1246, BCCallback,
- sock);
- Assert.Fail ("BeginConnectMultipleNull #1");
- } catch (ArgumentNullException) {
- } catch {
- Assert.Fail ("BeginConnectMultipleNull #2");
- } finally {
- sock.Close ();
- }
- }
- [Test]
- public void BeginConnectMultipleListen ()
- {
- Socket sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
- IPAddress[] ips = new IPAddress[4];
- IPEndPoint ep = new IPEndPoint (IPAddress.Loopback,
- 1247);
-
- ips[0] = IPAddress.Parse ("127.0.0.4");
- ips[1] = IPAddress.Parse ("127.0.0.3");
- ips[2] = IPAddress.Parse ("127.0.0.2");
- ips[3] = IPAddress.Parse ("127.0.0.1");
-
- sock.Bind (ep);
- sock.Listen (1);
-
- try {
- sock.BeginConnect (ips, 1247, BCCallback,
- sock);
- Assert.Fail ("BeginConnectMultipleListen #1");
- } catch (InvalidOperationException) {
- } catch {
- Assert.Fail ("BeginConnectMultipleListen #2");
- } finally {
- sock.Close ();
- }
- }
-
- [Test]
- [ExpectedException (typeof(ObjectDisposedException))]
- public void BeginConnectMultipleClosed ()
- {
- Socket sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
- IPAddress[] ips = new IPAddress[4];
-
- ips[0] = IPAddress.Parse ("127.0.0.4");
- ips[1] = IPAddress.Parse ("127.0.0.3");
- ips[2] = IPAddress.Parse ("127.0.0.2");
- ips[3] = IPAddress.Parse ("127.0.0.1");
-
- sock.Close ();
-
- sock.BeginConnect (ips, 1247, BCCallback, sock);
- }
-
- [Test]
- public void BeginConnectHostPortNull ()
- {
- Socket sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
-
- try {
- sock.BeginConnect ((string)null, 0,
- BCCallback, sock);
- Assert.Fail ("BeginConnectHostPort #1");
- } catch (ArgumentNullException) {
- } catch {
- Assert.Fail ("BeginConnectHostPort #2");
- } finally {
- sock.Close ();
- }
- }
- [Test]
- public void BeginConnectHostPortListen ()
- {
- Socket sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
- IPAddress ip = IPAddress.Loopback;
- IPEndPoint ep = new IPEndPoint (ip, 1248);
-
- sock.Bind (ep);
- sock.Listen (1);
-
- try {
- sock.BeginConnect ("localhost", 1248,
- BCCallback, sock);
- Assert.Fail ("BeginConnectHostPortListen #1");
- } catch (InvalidOperationException) {
- } catch {
- Assert.Fail ("BeginConnectHostPortListen #2");
- } finally {
- sock.Close ();
- }
- }
- [Test]
- [Category ("NotWorking")] // Need to pick a non-IP AddressFamily that "works" on both mono and ms, this one only works on ms
- public void BeginConnectHostPortNotIP ()
- {
- Socket sock = new Socket (AddressFamily.NetBios,
- SocketType.Seqpacket,
- ProtocolType.Unspecified);
-
- try {
- sock.BeginConnect ("localhost", 0, BCCallback,
- sock);
- Assert.Fail ("BeginConnectHostPortNotIP #1");
- } catch (NotSupportedException) {
- } catch {
- Assert.Fail ("BeginConnectHostPortNotIP #2");
- } finally {
- sock.Close ();
- }
- }
- [Test]
- [ExpectedException (typeof(ObjectDisposedException))]
- public void BeginConnectHostPortClosed ()
- {
- Socket sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
-
- sock.Close ();
-
- sock.BeginConnect ("localhost", 0, BCCallback, sock);
- }
-
- static bool BDDisconnected = false;
- static ManualResetEvent BDCalledBack = new ManualResetEvent (false);
-
- private static void BDCallback (IAsyncResult asyncResult)
- {
- Socket sock = (Socket)asyncResult.AsyncState;
-
- sock.EndDisconnect (asyncResult);
- BDDisconnected = true;
-
- BDCalledBack.Set ();
- }
-
- [Test]
- [Category ("NotDotNet")] // "Needs XP or later"
- public void BeginDisconnect ()
- {
- Socket sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
- Socket listen = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
- IPAddress ip = IPAddress.Loopback;
- IPEndPoint ep = new IPEndPoint (ip, 1254);
-
- listen.Bind (ep);
- listen.Listen (1);
-
- sock.Connect (ip, 1254);
-
- Assertion.AssertEquals ("BeginDisconnect #1", true,
- sock.Connected);
-
- sock.Shutdown (SocketShutdown.Both);
- BDCalledBack.Reset ();
- BDDisconnected = false;
-
- sock.BeginDisconnect (false, BDCallback, sock);
-
- if (BDCalledBack.WaitOne (2000, false) == false) {
- Assert.Fail ("BeginDisconnect wait timed out");
- }
-
- Assertion.AssertEquals ("BeginDisconnect #2", true,
- BDDisconnected);
- Assertion.AssertEquals ("BeginDisconnect #3", false,
- sock.Connected);
-
- sock.Close ();
- listen.Close ();
- }
-
- [Test]
- public void BeginReceiveSocketError ()
- {
- }
-
- [Test]
- public void BeginReceiveGeneric ()
- {
- }
-
- [Test]
- public void BeginReceiveGenericSocketError ()
- {
- }
-
- private static void BSCallback (IAsyncResult asyncResult)
- {
- Socket sock = (Socket)asyncResult.AsyncState;
-
- sock.EndSend (asyncResult);
- }
-
- [Test]
- public void BeginSendNotConnected ()
- {
- Socket sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
-
- byte[] send_bytes = new byte[] {10, 11, 12, 13};
-
- try {
- sock.BeginSend (send_bytes, 0,
- send_bytes.Length,
- SocketFlags.None, BSCallback,
- sock);
- Assert.Fail ("BeginSendNotConnected #1");
- } catch (SocketException ex) {
- Assertion.AssertEquals ("BeginSendNotConnected #2", 10057, ex.ErrorCode);
- } catch {
- Assert.Fail ("BeginSendNotConnected #3");
- } finally {
- sock.Close ();
- }
- }
-
- [Test]
- public void BeginSendSocketError ()
- {
- }
-
- [Test]
- public void BeginSendGeneric ()
- {
- }
-
- [Test]
- public void BeginSendGenericSocketError ()
- {
- }
-
- [Test]
- public void BindTwice ()
- {
- Socket sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
- IPEndPoint ep1 = new IPEndPoint (IPAddress.Loopback,
- 1256);
- IPEndPoint ep2 = new IPEndPoint (IPAddress.Loopback,
- 1257);
-
- sock.Bind (ep1);
-
- try {
- sock.Bind (ep2);
- Assert.Fail ("BindTwice #1");
- } catch (SocketException ex) {
- Assertion.AssertEquals ("BindTwice #2",
- 10022, ex.ErrorCode);
- } catch {
- Assert.Fail ("BindTwice #3");
- } finally {
- sock.Close ();
- }
- }
-
- [Test]
- public void Close ()
- {
- Socket sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
- Socket listen = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
- IPEndPoint ep = new IPEndPoint (IPAddress.Loopback,
- 1258);
-
- listen.Bind (ep);
- listen.Listen (1);
-
- sock.Connect (ep);
- Assertion.AssertEquals ("Close #1", true,
- sock.Connected);
-
- sock.Close (2);
-
- Thread.Sleep (3000);
-
- Assertion.AssertEquals ("Close #2", false,
- sock.Connected);
-
- listen.Close ();
- }
-
- [Test]
- public void ConnectAddressPort ()
- {
- Socket sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
- Socket listen = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
- IPAddress ip = IPAddress.Loopback;
- IPEndPoint ep = new IPEndPoint (ip, 1249);
- listen.Bind (ep);
- listen.Listen (1);
-
- sock.Connect (ip, 1249);
-
- Assertion.AssertEquals ("ConnectAddressPort #1",
- true, sock.Connected);
-
- sock.Close ();
- listen.Close ();
- }
- [Test]
- public void ConnectAddressPortNull ()
- {
- Socket sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
- IPAddress ip = null;
- try {
- sock.Connect (ip, 1249);
- Assert.Fail ("ConnectAddressPortNull #1");
- } catch (ArgumentNullException) {
- } catch {
- Assert.Fail ("ConnectAddressPortNull #2");
- } finally {
- sock.Close ();
- }
- }
- [Test]
- public void ConnectAddressPortListen ()
- {
- Socket sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
- IPAddress ip = IPAddress.Loopback;
- IPEndPoint ep = new IPEndPoint (ip, 1250);
- sock.Bind (ep);
- sock.Listen (1);
-
- try {
- sock.Connect (ip, 1250);
- Assert.Fail ("ConnectAddressPortListen #1");
- } catch (InvalidOperationException) {
- } catch {
- Assert.Fail ("ConnectAddressPortListen #2");
- } finally {
- sock.Close ();
- }
- }
-
- [Test]
- [ExpectedException (typeof(ObjectDisposedException))]
- public void ConnectAddressPortClosed ()
- {
- Socket sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
- IPAddress ip = IPAddress.Loopback;
-
- sock.Close ();
-
- sock.Connect (ip, 1250);
- }
-
- [Test]
- public void ConnectMultiple ()
- {
- Socket sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
- Socket listen = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
- IPEndPoint ep = new IPEndPoint (IPAddress.Loopback,
- 1251);
- IPAddress[] ips = new IPAddress[4];
-
- ips[0] = IPAddress.Parse ("127.0.0.4");
- ips[1] = IPAddress.Parse ("127.0.0.3");
- ips[2] = IPAddress.Parse ("127.0.0.2");
- ips[3] = IPAddress.Parse ("127.0.0.1");
- listen.Bind (ep);
- listen.Listen (1);
-
- sock.Connect (ips, 1251);
-
- Assertion.AssertEquals ("ConnectMultiple #1",
- true, sock.Connected);
- Assertion.AssertEquals ("ConnectMultiple #2",
- AddressFamily.InterNetwork,
- sock.RemoteEndPoint.AddressFamily);
- IPEndPoint remep = (IPEndPoint)sock.RemoteEndPoint;
-
- Assertion.AssertEquals ("ConnectMultiple #2",
- IPAddress.Loopback,
- remep.Address);
-
- sock.Close ();
- listen.Close ();
- }
- [Test]
- public void ConnectMultipleNull ()
- {
- Socket sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
- IPAddress[] ips = null;
-
- try {
- sock.Connect (ips, 1251);
- Assert.Fail ("ConnectMultipleNull #1");
- } catch (ArgumentNullException) {
- } catch {
- Assert.Fail ("ConnectMultipleNull #2");
- } finally {
- sock.Close ();
- }
- }
- [Test]
- public void ConnectMultipleListen ()
- {
- Socket sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
- IPAddress[] ips = new IPAddress[4];
- IPEndPoint ep = new IPEndPoint (IPAddress.Loopback,
- 1252);
-
- ips[0] = IPAddress.Parse ("127.0.0.4");
- ips[1] = IPAddress.Parse ("127.0.0.3");
- ips[2] = IPAddress.Parse ("127.0.0.2");
- ips[3] = IPAddress.Parse ("127.0.0.1");
-
- sock.Bind (ep);
- sock.Listen (1);
-
- try {
- sock.Connect (ips, 1252);
- Assert.Fail ("ConnectMultipleListen #1");
- } catch (InvalidOperationException) {
- } catch {
- Assert.Fail ("ConnectMultipleListen #2");
- } finally {
- sock.Close ();
- }
- }
-
- [Test]
- [ExpectedException (typeof(ObjectDisposedException))]
- public void ConnectMultipleClosed ()
- {
- Socket sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
- IPAddress[] ips = new IPAddress[4];
-
- ips[0] = IPAddress.Parse ("127.0.0.4");
- ips[1] = IPAddress.Parse ("127.0.0.3");
- ips[2] = IPAddress.Parse ("127.0.0.2");
- ips[3] = IPAddress.Parse ("127.0.0.1");
-
- sock.Close ();
-
- sock.Connect (ips, 1252);
- }
-
- [Test]
- public void ConnectHostPortNull ()
- {
- Socket sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
-
- try {
- sock.Connect ((string)null, 0);
- Assert.Fail ("ConnectHostPort #1");
- } catch (ArgumentNullException) {
- } catch {
- Assert.Fail ("ConnectHostPort #2");
- } finally {
- sock.Close ();
- }
- }
- [Test]
- public void ConnectHostPortListen ()
- {
- Socket sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
- IPAddress ip = IPAddress.Loopback;
- IPEndPoint ep = new IPEndPoint (ip, 1253);
-
- sock.Bind (ep);
- sock.Listen (1);
-
- try {
- sock.Connect ("localhost", 1253);
- Assert.Fail ("ConnectHostPortListen #1");
- } catch (InvalidOperationException) {
- } catch {
- Assert.Fail ("ConnectHostPortListen #2");
- } finally {
- sock.Close ();
- }
- }
- [Test]
- [Category ("NotWorking")] // Need to pick a non-IP AddressFamily that "works" on both mono and ms, this one only works on ms
- public void ConnectHostPortNotIP ()
- {
- Socket sock = new Socket (AddressFamily.NetBios,
- SocketType.Seqpacket,
- ProtocolType.Unspecified);
-
- try {
- sock.Connect ("localhost", 0);
- Assert.Fail ("ConnectHostPortNotIP #1");
- } catch (NotSupportedException) {
- } catch {
- Assert.Fail ("ConnectHostPortNotIP #2");
- } finally {
- sock.Close ();
- }
- }
- [Test]
- [ExpectedException (typeof(ObjectDisposedException))]
- public void ConnectHostPortClosed ()
- {
- Socket sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
-
- sock.Close ();
-
- sock.Connect ("localhost", 0);
- }
-
- [Test]
- [Category ("NotDotNet")] // "Needs XP or later"
- public void Disconnect ()
- {
- Socket sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
- Socket listen = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
- IPAddress ip = IPAddress.Loopback;
- IPEndPoint ep = new IPEndPoint (ip, 1255);
-
- listen.Bind (ep);
- listen.Listen (1);
-
- sock.Connect (ip, 1255);
-
- Assertion.AssertEquals ("Disconnect #1", true,
- sock.Connected);
-
- sock.Shutdown (SocketShutdown.Both);
- sock.Disconnect (false);
- Assertion.AssertEquals ("BeginDisconnect #3", false,
- sock.Connected);
-
- sock.Close ();
- listen.Close ();
- }
-
- [Test]
- public void DuplicateAndClose ()
- {
- }
-
- [Test]
- public void IOControl ()
- {
- }
-
- [Test]
- public void ReceiveGeneric ()
- {
- }
-
- [Test]
- public void ReceiveGenericSocketFlags ()
- {
- }
-
- [Test]
- public void ReceiveGenericSocketFlagsSocketError ()
- {
- }
-
- [Test]
- public void SendGeneric ()
- {
- }
-
- [Test]
- public void SendGenericSocketFlags ()
- {
- }
-
- [Test]
- public void SendGenericSocketFlagsSocketError ()
- {
- }
- [Test]
- public void ListenNotBound ()
- {
- Socket sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
-
- try {
- sock.Listen (1);
- Assert.Fail ("ListenNotBound #1");
- } catch (SocketException ex) {
- Assertion.AssertEquals ("ListenNotBound #2",
- 10022, ex.ErrorCode);
- } catch {
- Assert.Fail ("ListenNotBound #3");
- } finally {
- sock.Close ();
- }
- }
- #endif
- static Socket CWRSocket;
- static bool CWRReceiving = true;
- static ManualResetEvent CWRReady = new ManualResetEvent (false);
-
- private static void CWRReceiveThread ()
- {
- byte[] buf = new byte[256];
-
- try {
- CWRSocket.Receive (buf);
- } catch (SocketException) {
- CWRReceiving = false;
- }
- CWRReady.Set ();
- }
-
- [Test]
- public void CloseWhileReceiving ()
- {
- CWRSocket = new Socket (AddressFamily.InterNetwork,
- SocketType.Dgram,
- ProtocolType.Udp);
- CWRSocket.Bind (new IPEndPoint (IPAddress.Loopback,
- 1256));
-
- Thread recv_thread = new Thread (new ThreadStart (CWRReceiveThread));
- CWRReady.Reset ();
- recv_thread.Start ();
- Thread.Sleep (250); /* Wait for the thread to be already receiving */
- CWRSocket.Close ();
- if (CWRReady.WaitOne (1000, false) == false) {
- Assert.Fail ("CloseWhileReceiving wait timed out");
- }
-
- Assert.IsFalse (CWRReceiving);
- }
- static bool RRCLastRead = false;
- static ManualResetEvent RRCReady = new ManualResetEvent (false);
-
- private static void RRCClientThread ()
- {
- byte[] bytes = new byte[8];
- int readbyte;
-
- Socket sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
- sock.Connect (new IPEndPoint (IPAddress.Loopback,
- 1257));
-
- NetworkStream stream = new NetworkStream (sock);
- readbyte = stream.ReadByte ();
- Assertion.AssertEquals ("ReceiveRemoteClosed #1",
- 0, readbyte);
-
- stream.Read (bytes, 0, 0);
- readbyte = stream.ReadByte ();
- Assertion.AssertEquals ("ReceiveRemoteClosed #2",
- 0, readbyte);
-
- stream.Read (bytes, 0, 0);
- readbyte = stream.ReadByte ();
- Assertion.AssertEquals ("ReceiveRemoteClosed #3",
- -1, readbyte);
- sock.Close ();
- RRCLastRead = true;
- RRCReady.Set ();
- }
-
- [Test]
- public void ReceiveRemoteClosed ()
- {
- Socket sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
- sock.Bind (new IPEndPoint (IPAddress.Loopback, 1257));
- sock.Listen (1);
-
- RRCReady.Reset ();
- Thread client_thread = new Thread (new ThreadStart (RRCClientThread));
- client_thread.Start ();
-
- Socket client = sock.Accept ();
- NetworkStream stream = new NetworkStream (client);
- stream.WriteByte (0x00);
- stream.WriteByte (0x00);
- client.Close ();
- sock.Close ();
- RRCReady.WaitOne (1000, false);
- Assert.IsTrue (RRCLastRead);
- }
- }
- }
|