| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119 |
- // 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;
- #if NET_2_0
- using System.Collections.Generic;
- #endif
- 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);
- /* UDP sockets use Any to disconnect
- 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")] // Looks like MS fails after the .ctor, when you try to use the socket
- 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);
- try {
- // should raise an exception because connect was bogus
- sock.EndConnect (ar);
- Assert.Fail ("#1");
- } catch (SocketException ex) {
- Assert.AreEqual (10060, ex.ErrorCode, "#2");
- }
- }
- [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);
- Assert.AreEqual (block, true, "BlockingStatus01");
- block = BlockingConnect(false);
- Assert.AreEqual (block, false, "BlockingStatus02");
- }
- 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] // Connect (IPEndPoint)
- public void Connect1_RemoteEP_Null ()
- {
- Socket s = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream, ProtocolType.Tcp);
- try {
- s.Connect ((IPEndPoint) null);
- Assert.Fail ("#1");
- } catch (ArgumentNullException ex) {
- Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
- Assert.IsNull (ex.InnerException, "#3");
- Assert.IsNotNull (ex.Message, "#4");
- Assert.AreEqual ("remoteEP", ex.ParamName, "#5");
- }
- }
- [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 ();
- Assert.AreEqual (CFAConnected, false, "ConnectFail");
- }
-
- #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);
- Assert.AreEqual (0, empty.Count, "#01");
- Assert.AreEqual (1, list.Count, "#02");
- Socket.Select (empty, list, empty, 100);
- Assert.AreEqual (0, empty.Count, "#03");
- Assert.AreEqual (1, list.Count, "#04");
- Socket.Select (list, empty, empty, -1);
- Assert.AreEqual (0, empty.Count, "#05");
- Assert.AreEqual (1, list.Count, "#06");
- // Need to read the 10 bytes from the client to avoid a RST
- byte [] bytes = new byte [10];
- acc.Receive (bytes);
- } 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 Disposed2 ()
- {
- Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
- s.Close();
- s.Blocking = true;
- }
- [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 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);
- 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);
- s.Close();
- s.Shutdown (0);
- }
- [Test]
- public void GetHashCodeTest ()
- {
- Socket server = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream, ProtocolType.Tcp);
- IPEndPoint ep = new IPEndPoint (IPAddress.Loopback,
- 9010);
- server.Bind (ep);
- server.Listen (1);
- Socket client = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream, ProtocolType.Tcp);
- int hashcodeA = client.GetHashCode ();
- client.Connect (ep);
- int hashcodeB = client.GetHashCode ();
- Assert.AreEqual (hashcodeA, hashcodeB, "#1");
- client.Close ();
- int hashcodeC = client.GetHashCode ();
- #if NET_2_0
- Assert.AreEqual (hashcodeB, hashcodeC, "#2");
- #else
- Assert.IsFalse (hashcodeB == hashcodeC, "#2");
- #endif
- server.Close ();
- }
- 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 SocketErrorTest ()
- {
- 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, new AsyncCallback(SocketError_callback),
- sock);
- if (SocketError_event.WaitOne (2000, false) == false) {
- Assert.Fail ("SocketError wait timed out");
- }
- Assert.AreEqual (false, sock.Connected, "SocketError #1");
- int error;
- error = (int)sock.GetSocketOption (SocketOptionLevel.Socket, SocketOptionName.Error);
- Assert.AreEqual (10061, error, "SocketError #2");
- error = (int)sock.GetSocketOption (SocketOptionLevel.Socket, SocketOptionName.Error);
- Assert.AreEqual (10061, error, "SocketError #3");
- sock.Close ();
- }
-
- #if NET_2_0
- [Test]
- public void SocketInformationCtor ()
- {
- }
-
- [Test]
- public void DontFragmentDefaultTcp ()
- {
- Socket sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
-
- Assert.AreEqual (false, sock.DontFragment, "DontFragmentDefaultTcp");
- sock.Close ();
- }
- [Test]
- [Category ("NotOnMac")] // DontFragment doesn't work on Mac
- public void DontFragmentChangeTcp ()
- {
- Socket sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
-
- sock.DontFragment = true;
-
- Assert.AreEqual (true, sock.DontFragment, "DontFragmentChangeTcp");
- sock.Close ();
- }
-
- [Test]
- public void DontFragmentDefaultUdp ()
- {
- Socket sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Dgram,
- ProtocolType.Udp);
-
- Assert.AreEqual (false, sock.DontFragment, "DontFragmentDefaultUdp");
- sock.Close ();
- }
- [Test]
- [Category ("NotOnMac")] // DontFragment doesn't work on Mac
- public void DontFragmentChangeUdp ()
- {
- Socket sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Dgram,
- ProtocolType.Udp);
-
- sock.DontFragment = true;
-
- Assert.AreEqual (true, sock.DontFragment, "DontFragmentChangeUdp");
- 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);
-
- Assert.AreEqual (false, sock.EnableBroadcast, "EnableBroadcastDefaultUdp");
- 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;
-
- Assert.AreEqual (true, sock.EnableBroadcast, "EnableBroadcastChangeUdp");
- 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;
-
- Assert.AreEqual (true, sock.ExclusiveAddressUse, "ExclusiveAddressUseUnbound");
-
- 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);
-
- Assert.AreEqual (false, sock.IsBound, "IsBoundTcp #1");
-
- sock.Bind (ep);
- Assert.AreEqual (true, sock.IsBound, "IsBoundTcp #2");
- sock.Listen (1);
-
- Socket sock2 = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
-
- Assert.AreEqual (false, sock2.IsBound, "IsBoundTcp #3");
-
- sock2.Connect (ep);
- Assert.AreEqual (true, sock2.IsBound, "IsBoundTcp #4");
-
- sock2.Close ();
- Assert.AreEqual (true, sock2.IsBound, "IsBoundTcp #5");
- sock.Close ();
- Assert.AreEqual (true, sock.IsBound, "IsBoundTcp #6");
- }
- [Test]
- public void IsBoundUdp ()
- {
- Socket sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Dgram,
- ProtocolType.Udp);
- IPEndPoint ep = new IPEndPoint (IPAddress.Loopback,
- BogusPort);
-
- Assert.AreEqual (false, sock.IsBound, "IsBoundUdp #1");
-
- sock.Bind (ep);
- Assert.AreEqual (true, sock.IsBound, "IsBoundUdp #2");
-
- sock.Close ();
- Assert.AreEqual (true, sock.IsBound, "IsBoundUdp #3");
-
- sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Dgram,
- ProtocolType.Udp);
-
- Assert.AreEqual (false, sock.IsBound, "IsBoundUdp #4");
-
- sock.Connect (ep);
- Assert.AreEqual (true, sock.IsBound, "IsBoundUdp #5");
-
- sock.Close ();
- Assert.AreEqual (true, sock.IsBound, "IsBoundUdp #6");
- }
- [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);
-
- Assert.AreEqual (true, sock.MulticastLoopback, "MulticastLoopbackDefaultUdp");
-
- sock.Close ();
- }
-
- [Test]
- public void MulticastLoopbackChangeUdp ()
- {
- Socket sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Dgram,
- ProtocolType.Udp);
-
- sock.MulticastLoopback = false;
-
- Assert.AreEqual (false, sock.MulticastLoopback, "MulticastLoopbackChangeUdp");
-
- 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]
- [Category("NotWorking")] // We have different defaults for perf reasons
- public void ReceiveBufferSizeDefault ()
- {
- Socket sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
-
- Assert.AreEqual (8192, sock.ReceiveBufferSize, "ReceiveBufferSizeDefault");
-
- sock.Close ();
- }
-
- [Test]
- [Category("NotWorking")] // We have different defaults for perf reasons
- public void ReceiveBufferSizeDefaultUdp ()
- {
- Socket sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Dgram,
- ProtocolType.Udp);
-
- Assert.AreEqual (8192, sock.ReceiveBufferSize, "ReceiveBufferSizeDefaultUdp");
-
- sock.Close ();
- }
- [Test]
- public void ReceiveBufferSizeChange ()
- {
- Socket sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
-
- sock.ReceiveBufferSize = 16384;
-
- Assert.AreEqual (16384, sock.ReceiveBufferSize, "ReceiveBufferSizeChange");
-
- sock.Close ();
- }
- [Test]
- [Category("NotWorking")] // We cannot totally remove buffers (minimum is set to 256
- public void BuffersCheck_None ()
- {
- using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) {
- int original = s.ReceiveBufferSize;
- s.ReceiveBufferSize = 0;
- Assert.AreEqual (0, s.ReceiveBufferSize, "ReceiveBufferSize " + original.ToString ());
- original = s.SendBufferSize;
- s.SendBufferSize = 0;
- Assert.AreEqual (0, s.SendBufferSize, "SendBufferSize " + original.ToString ());
- }
- }
- [Test]
- [ExpectedException (typeof(ObjectDisposedException))]
- public void ReceiveBufferSizeClosed ()
- {
- Socket sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
-
- sock.Close ();
-
- int val = sock.ReceiveBufferSize;
- }
-
- [Test]
- [Category("NotWorking")] // We have different defaults for perf reasons
- public void SendBufferSizeDefault ()
- {
- Socket sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
-
- Assert.AreEqual (8192, sock.SendBufferSize, "SendBufferSizeDefault");
-
- sock.Close ();
- }
-
- [Test]
- [Category("NotWorking")] // We have different defaults for perf reasons
- public void SendBufferSizeDefaultUdp ()
- {
- Socket sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Dgram,
- ProtocolType.Udp);
-
- Assert.AreEqual (8192, sock.SendBufferSize, "SendBufferSizeDefaultUdp");
-
- sock.Close ();
- }
- [Test]
- public void SendBufferSizeChange ()
- {
- Socket sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
-
- sock.SendBufferSize = 16384;
-
- Assert.AreEqual (16384, sock.SendBufferSize, "SendBufferSizeChange");
-
- 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;
-
- Assert.AreEqual (255, sock.Ttl, "TtlChange");
-
- sock.Close ();
- }
- [Test]
- [Category ("NotOnMac")] // Mac doesn't throw when overflowing the ttl
- 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);
-
- Assert.AreEqual (false, sock.UseOnlyOverlappedIO, "UseOnlyOverlappedIODefault");
-
- 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;
-
- Assert.AreEqual (true, sock.UseOnlyOverlappedIO, "UseOnlyOverlappedIOChange");
-
- 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);
-
- Assert.AreEqual (0, sock.SendTimeout, "SendTimeoutDefault");
-
- 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;
- Assert.AreEqual (50, Approximate (50, sock.SendTimeout), "SendTimeoutChange #1");
-
- sock.SendTimeout = 2000;
- Assert.AreEqual (2000, Approximate (2000, sock.SendTimeout), "SendTimeoutChange #2");
-
- sock.SendTimeout = 0;
- Assert.AreEqual (0, Approximate (0, sock.SendTimeout), "SendTimeoutChange #3");
-
- /* Should be the same as setting 0 */
- sock.SendTimeout = -1;
- Assert.AreEqual (0, sock.SendTimeout, "SendTimeoutChange #4");
- sock.SendTimeout = 65536;
- Assert.AreEqual (65536, Approximate (65536, sock.SendTimeout), "SendTimeoutChange #5");
-
- 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);
-
- Assert.AreEqual (0, sock.ReceiveTimeout, "ReceiveTimeoutDefault");
-
- sock.Close ();
- }
- [Test]
- public void ReceiveTimeoutChange ()
- {
- Socket sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
-
- sock.ReceiveTimeout = 50;
- Assert.AreEqual (50, Approximate (50, sock.ReceiveTimeout), "ReceiveTimeoutChange #1");
-
- sock.ReceiveTimeout = 2000;
- Assert.AreEqual (2000, Approximate (2000, sock.ReceiveTimeout), "ReceiveTimeoutChange #2");
-
- sock.ReceiveTimeout = 0;
- Assert.AreEqual (0, sock.ReceiveTimeout, "ReceiveTimeoutChange #3");
-
- /* Should be the same as setting 0 */
- sock.ReceiveTimeout = -1;
- Assert.AreEqual (0, sock.ReceiveTimeout, "ReceiveTimeoutChange #4");
- sock.ReceiveTimeout = 65536;
- Assert.AreEqual (65536, Approximate (65536, sock.ReceiveTimeout), "ReceiveTimeoutChange #5");
-
- 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);
-
- Assert.AreEqual (false, sock.NoDelay, "NoDelayDefaultTcp");
-
- sock.Close ();
- }
- [Test]
- public void NoDelayChangeTcp ()
- {
- Socket sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
-
- sock.NoDelay = true;
-
- Assert.AreEqual (true, sock.NoDelay, "NoDelayChangeTcp");
-
- 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");
- }
-
- Assert.AreEqual (true, BAAccepted, "BeginAccept #1");
- Assert.AreEqual (true, BASocket.Connected, "BeginAccept #2");
- Assert.AreEqual (false, sock.Connected, "BeginAccept #3");
- Assert.AreEqual (true, conn.Connected, "BeginAccept #4");
- 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");
- }
-
- Assert.AreEqual (true, BADAccepted, "BeginAcceptData #1");
- Assert.AreEqual (true, BADSocket.Connected, "BeginAcceptData #2");
- Assert.AreEqual (false, sock.Connected, "BeginAcceptData #3");
- Assert.AreEqual (true, conn.Connected, "BeginAcceptData #4");
- Assert.AreEqual (send_bytes.Length, BADByteCount, "BeginAcceptData #5");
-
- /* 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++) {
- Assert.AreEqual (send_bytes[i], BADBytes[i], "BeginAcceptData #" + (i+7).ToString ());
- }
- 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) {
- Assert.AreEqual (10022, ex.ErrorCode, "BeginAcceptSocketUdp #2");
- } 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");
- }
-
- Assert.AreEqual (true, BADAccepted, "BeginAcceptSocket #1");
- Assert.AreEqual (true, BADSocket.Connected, "BeginAcceptSocket #2");
- Assert.AreEqual (false, sock.Connected, "BeginAcceptSocket #3");
- Assert.AreEqual (true, conn.Connected, "BeginAcceptSocket #4");
- Assert.AreEqual (send_bytes.Length, BADByteCount, "BeginAcceptSocket #5");
- Assert.AreEqual (AddressFamily.InterNetwork, acc.AddressFamily, "BeginAcceptSocket #6");
- Assert.AreEqual (SocketType.Stream, acc.SocketType, "BeginAcceptSocket #7");
- Assert.AreEqual (ProtocolType.Tcp, acc.ProtocolType, "BeginAcceptSocket #8");
- Assert.AreEqual (conn.LocalEndPoint, acc.RemoteEndPoint, "BeginAcceptSocket #9");
-
- /* 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++) {
- Assert.AreEqual (send_bytes[i], BADBytes[i], "BeginAcceptSocket #" + (i+11).ToString ());
- }
- 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");
- }
-
- Assert.AreEqual (true, BCConnected, "BeginConnectAddressPort #1");
-
- 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]
- [Category ("NotOnMac")]
- 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");
- }
-
- Assert.AreEqual (true, BCConnected, "BeginConnectMultiple #1");
- Assert.AreEqual (AddressFamily.InterNetwork, sock.RemoteEndPoint.AddressFamily, "BeginConnectMultiple #2");
- IPEndPoint remep = (IPEndPoint)sock.RemoteEndPoint;
-
- Assert.AreEqual (IPAddress.Loopback, remep.Address, "BeginConnectMultiple #2");
-
- 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);
-
- Assert.AreEqual (true, sock.Connected, "BeginDisconnect #1");
-
- 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");
- }
-
- Assert.AreEqual (true, BDDisconnected, "BeginDisconnect #2");
- Assert.AreEqual (false, sock.Connected, "BeginDisconnect #3");
-
- 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) {
- Assert.AreEqual (10057, ex.ErrorCode, "BeginSendNotConnected #2");
- } 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) {
- Assert.AreEqual (10022, ex.ErrorCode, "BindTwice #2");
- } 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);
- Assert.AreEqual (true, sock.Connected, "Close #1");
-
- sock.Close (2);
-
- Thread.Sleep (3000);
-
- Assert.AreEqual (false, sock.Connected, "Close #2");
-
- 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);
-
- Assert.AreEqual (true, sock.Connected, "ConnectAddressPort #1");
-
- 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]
- [Category ("NotOnMac")] // MacOSX trashes the fd after the failed connect attempt to 127.0.0.4
- 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);
-
- Assert.AreEqual (true, sock.Connected, "ConnectMultiple #1");
- Assert.AreEqual (AddressFamily.InterNetwork, sock.RemoteEndPoint.AddressFamily, "ConnectMultiple #2");
- IPEndPoint remep = (IPEndPoint)sock.RemoteEndPoint;
-
- Assert.AreEqual (IPAddress.Loopback, remep.Address, "ConnectMultiple #2");
-
- 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);
-
- Assert.AreEqual (true, sock.Connected, "Disconnect #1");
-
- sock.Shutdown (SocketShutdown.Both);
- sock.Disconnect (false);
- Assert.AreEqual (false, sock.Connected, "BeginDisconnect #3");
-
- sock.Close ();
- listen.Close ();
- }
-
- [Test]
- public void DuplicateAndClose ()
- {
- }
-
- [Test]
- public void IOControl ()
- {
- }
-
- [Test]
- public void ReceiveGeneric ()
- {
- int i;
- IPEndPoint endpoint = new IPEndPoint(IPAddress.Loopback, 1258);
- Socket listensock = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
- listensock.Bind (endpoint);
- listensock.Listen(1);
- Socket sendsock = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
- sendsock.Connect(endpoint);
- Socket clientsock = listensock.Accept();
-
- byte[] sendbuf = new byte[256];
- for(i = 0; i < 256; i++) {
- sendbuf[i] = (byte)i;
- }
- for (i = 4; i < 6; i++) {
- Assert.AreEqual (sendbuf[i], (byte)i,
- "#1/" + i.ToString());
- }
- SocketError err;
- sendsock.Send (sendbuf, 0, 256, SocketFlags.None,
- out err);
- byte[] recvbuf = new byte[256];
- List<ArraySegment<byte>> recvbuflist = new List<ArraySegment<byte>>(2);
- recvbuflist.Add(new ArraySegment<byte>(recvbuf, 4, 2));
- recvbuflist.Add(new ArraySegment<byte>(recvbuf, 20, 230));
-
- clientsock.Receive (recvbuflist);
- /* recvbuf should now hold the first 2 bytes
- * of sendbuf from pos 4, and the next 230
- * bytes of sendbuf from pos 20
- */
- for (i = 0; i < 2; i++) {
- Assert.AreEqual (sendbuf[i], recvbuf[i + 4],
- "#2/" + i.ToString());
- }
- for (i = 2; i < 232; i++) {
- Assert.AreEqual (sendbuf[i], recvbuf[i + 18],
- "#2/" + i.ToString());
- }
- sendsock.Close ();
- clientsock.Close ();
- listensock.Close ();
- }
-
- [Test]
- public void SendGeneric ()
- {
- int i;
- IPEndPoint endpoint = new IPEndPoint(IPAddress.Loopback, 1259);
- Socket listensock = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
- listensock.Bind (endpoint);
- listensock.Listen(1);
- Socket sendsock = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
- sendsock.Connect(endpoint);
- Socket clientsock = listensock.Accept();
- byte[] sendbuf = new byte[256];
- List<ArraySegment<byte>> sendbuflist = new List<ArraySegment<byte>>(2);
- sendbuflist.Add(new ArraySegment<byte>(sendbuf, 4, 2));
- sendbuflist.Add(new ArraySegment<byte>(sendbuf, 20, 230));
- for(i = 0; i < 256; i++) {
- sendbuf[i] = (byte)i;
- }
- for (i = 4; i < 6; i++) {
- Assert.AreEqual (sendbuf[i], (byte)i,
- "#1/" + i.ToString());
- }
- SocketError err;
- sendsock.Send (sendbuflist, SocketFlags.None, out err);
-
- byte[] recvbuf = new byte[256];
- clientsock.Receive (recvbuf);
- /* The first 2 bytes of recvbuf should now
- * hold 2 bytes of sendbuf from pos 4, and the
- * next 230 bytes of recvbuf should be sendbuf
- * from pos 20
- */
- for (i = 0; i < 2; i++) {
- Assert.AreEqual (recvbuf[i], sendbuf[i + 4],
- "#2/" + i.ToString());
- }
- for (i = 2; i < 232; i++) {
- Assert.AreEqual (recvbuf[i], sendbuf[i + 18],
- "#2/" + i.ToString());
- }
- sendsock.Close ();
- clientsock.Close ();
- listensock.Close ();
- }
- [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) {
- Assert.AreEqual (10022, ex.ErrorCode, "ListenNotBound #2");
- } 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 ();
- Assert.AreEqual (0, readbyte, "ReceiveRemoteClosed #1");
-
- stream.Read (bytes, 0, 0);
- readbyte = stream.ReadByte ();
- Assert.AreEqual (0, readbyte, "ReceiveRemoteClosed #2");
-
- stream.Read (bytes, 0, 0);
- readbyte = stream.ReadByte ();
- Assert.AreEqual (-1, readbyte, "ReceiveRemoteClosed #3");
- sock.Close ();
- RRCLastRead = true;
- RRCReady.Set ();
- }
- [Test] // Receive (Byte [])
- public void Receive1_Buffer_Null ()
- {
- Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
- ProtocolType.Tcp);
- try {
- s.Receive ((byte []) null);
- Assert.Fail ("#1");
- } catch (ArgumentNullException ex) {
- Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
- Assert.IsNull (ex.InnerException, "#3");
- Assert.IsNotNull (ex.Message, "#4");
- Assert.AreEqual ("buffer", ex.ParamName, "#5");
- } finally {
- s.Close ();
- }
- }
- [Test] // Receive (Byte [])
- public void Receive1_Socket_Closed ()
- {
- Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
- ProtocolType.Tcp);
- s.Close ();
- try {
- s.Receive ((byte []) null);
- Assert.Fail ("#1");
- } catch (ObjectDisposedException ex) {
- // Cannot access a disposed object
- Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
- Assert.IsNull (ex.InnerException, "#3");
- Assert.IsNotNull (ex.Message, "#4");
- Assert.AreEqual (typeof (Socket).FullName, ex.ObjectName, "#5");
- }
- }
- [Test] // Receive (Byte [], SocketFlags)
- public void Receive2_Buffer_Null ()
- {
- Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
- ProtocolType.Tcp);
- try {
- s.Receive ((byte []) null, (SocketFlags) 666);
- Assert.Fail ("#1");
- } catch (ArgumentNullException ex) {
- Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
- Assert.IsNull (ex.InnerException, "#3");
- Assert.IsNotNull (ex.Message, "#4");
- Assert.AreEqual ("buffer", ex.ParamName, "#5");
- } finally {
- s.Close ();
- }
- }
- [Test] // Receive (Byte [], SocketFlags)
- public void Receive2_Socket_Closed ()
- {
- Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
- ProtocolType.Tcp);
- s.Close ();
- try {
- s.Receive ((byte []) null, (SocketFlags) 666);
- Assert.Fail ("#1");
- } catch (ObjectDisposedException ex) {
- // Cannot access a disposed object
- Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
- Assert.IsNull (ex.InnerException, "#3");
- Assert.IsNotNull (ex.Message, "#4");
- Assert.AreEqual (typeof (Socket).FullName, ex.ObjectName, "#5");
- }
- }
- [Test] // Receive (Byte [], Int32, SocketFlags)
- public void Receive3_Buffer_Null ()
- {
- Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
- ProtocolType.Tcp);
- try {
- s.Receive ((byte []) null, 0, (SocketFlags) 666);
- Assert.Fail ("#1");
- } catch (ArgumentNullException ex) {
- Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
- Assert.IsNull (ex.InnerException, "#3");
- Assert.IsNotNull (ex.Message, "#4");
- Assert.AreEqual ("buffer", ex.ParamName, "#5");
- } finally {
- s.Close ();
- }
- }
- [Test] // Receive (Byte [], Int32, SocketFlags)
- public void Receive3_Socket_Closed ()
- {
- Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
- ProtocolType.Tcp);
- s.Close ();
- try {
- s.Receive ((byte []) null, 0, (SocketFlags) 666);
- Assert.Fail ("#1");
- } catch (ObjectDisposedException ex) {
- // Cannot access a disposed object
- Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
- Assert.IsNull (ex.InnerException, "#3");
- Assert.IsNotNull (ex.Message, "#4");
- Assert.AreEqual (typeof (Socket).FullName, ex.ObjectName, "#5");
- }
- }
- [Test] // Receive (Byte [], Int32, Int32, SocketFlags)
- public void Receive4_Buffer_Null ()
- {
- Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
- ProtocolType.Tcp);
- try {
- s.Receive ((byte []) null, 0, 0, (SocketFlags) 666);
- Assert.Fail ("#1");
- } catch (ArgumentNullException ex) {
- Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
- Assert.IsNull (ex.InnerException, "#3");
- Assert.IsNotNull (ex.Message, "#4");
- Assert.AreEqual ("buffer", ex.ParamName, "#5");
- } finally {
- s.Close ();
- }
- }
- [Test] // Receive (Byte [], Int32, Int32, SocketFlags)
- public void Receive4_Socket_Closed ()
- {
- Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
- ProtocolType.Tcp);
- s.Close ();
- try {
- s.Receive ((byte []) null, 0, 0, (SocketFlags) 666);
- Assert.Fail ("#1");
- } catch (ObjectDisposedException ex) {
- // Cannot access a disposed object
- Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
- Assert.IsNull (ex.InnerException, "#3");
- Assert.IsNotNull (ex.Message, "#4");
- Assert.AreEqual (typeof (Socket).FullName, ex.ObjectName, "#5");
- }
- }
- #if NET_2_0
- [Test] // Receive (Byte [], Int32, Int32, SocketFlags, out SocketError)
- public void Receive5_Buffer_Null ()
- {
- Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
- ProtocolType.Tcp);
- SocketError error;
- try {
- s.Receive ((byte []) null, 0, 0, SocketFlags.None, out error);
- Assert.Fail ("#1");
- } catch (ArgumentNullException ex) {
- Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
- Assert.IsNull (ex.InnerException, "#3");
- Assert.IsNotNull (ex.Message, "#4");
- Assert.AreEqual ("buffer", ex.ParamName, "#5");
- } finally {
- s.Close ();
- }
- }
- [Test] // Receive (Byte [], Int32, Int32, SocketFlags, out SocketError)
- public void Receive5_Socket_Closed ()
- {
- Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
- ProtocolType.Tcp);
- s.Close ();
- SocketError error;
- try {
- s.Receive ((byte []) null, 0, 0, SocketFlags.None, out error);
- Assert.Fail ("#1");
- } catch (ObjectDisposedException ex) {
- // Cannot access a disposed object
- Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
- Assert.IsNull (ex.InnerException, "#3");
- Assert.IsNotNull (ex.Message, "#4");
- Assert.AreEqual (typeof (Socket).FullName, ex.ObjectName, "#5");
- }
- }
- [Test] // Receive (IList<ArraySegment<Byte>>)
- public void Receive6_Buffers_Null ()
- {
- Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
- ProtocolType.Tcp);
- try {
- s.Receive ((IList<ArraySegment<byte>>) null);
- Assert.Fail ("#1");
- } catch (ArgumentNullException ex) {
- Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
- Assert.IsNull (ex.InnerException, "#3");
- Assert.IsNotNull (ex.Message, "#4");
- Assert.AreEqual ("buffers", ex.ParamName, "#5");
- } finally {
- s.Close ();
- }
- }
- [Test] // Receive (IList<ArraySegment<Byte>>)
- public void Receive6_Socket_Closed ()
- {
- Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
- ProtocolType.Tcp);
- s.Close ();
- try {
- s.Receive ((IList<ArraySegment<byte>>) null);
- Assert.Fail ("#1");
- } catch (ObjectDisposedException ex) {
- // Cannot access a disposed object
- Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
- Assert.IsNull (ex.InnerException, "#3");
- Assert.IsNotNull (ex.Message, "#4");
- Assert.AreEqual (typeof (Socket).FullName, ex.ObjectName, "#5");
- }
- }
- [Test] // Receive (IList<ArraySegment<Byte>>, SocketFlags)
- public void Receive7_Buffers_Null ()
- {
- Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
- ProtocolType.Tcp);
- try {
- s.Receive ((IList<ArraySegment<byte>>) null, (SocketFlags) 666);
- Assert.Fail ("#1");
- } catch (ArgumentNullException ex) {
- Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
- Assert.IsNull (ex.InnerException, "#3");
- Assert.IsNotNull (ex.Message, "#4");
- Assert.AreEqual ("buffers", ex.ParamName, "#5");
- } finally {
- s.Close ();
- }
- }
- [Test] // Receive (IList<ArraySegment<Byte>>, SocketFlags)
- public void Receive7_Socket_Closed ()
- {
- Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
- ProtocolType.Tcp);
- s.Close ();
- try {
- s.Receive ((IList<ArraySegment<byte>>) null, (SocketFlags) 666);
- Assert.Fail ("#1");
- } catch (ObjectDisposedException ex) {
- // Cannot access a disposed object
- Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
- Assert.IsNull (ex.InnerException, "#3");
- Assert.IsNotNull (ex.Message, "#4");
- Assert.AreEqual (typeof (Socket).FullName, ex.ObjectName, "#5");
- }
- }
- [Test] // Receive (IList<ArraySegment<Byte>>, SocketFlags, out SocketError)
- public void Receive8_Buffers_Null ()
- {
- Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
- ProtocolType.Tcp);
- SocketError error;
- try {
- s.Receive ((IList<ArraySegment<byte>>) null, (SocketFlags) 666,
- out error);
- Assert.Fail ("#1");
- } catch (ArgumentNullException ex) {
- Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
- Assert.IsNull (ex.InnerException, "#3");
- Assert.IsNotNull (ex.Message, "#4");
- Assert.AreEqual ("buffers", ex.ParamName, "#5");
- } finally {
- s.Close ();
- }
- }
- [Test] // Receive (IList<ArraySegment<Byte>>, SocketFlags, out SocketError)
- public void Receive8_Socket_Closed ()
- {
- Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
- ProtocolType.Tcp);
- s.Close ();
- SocketError error;
- try {
- s.Receive ((IList<ArraySegment<byte>>) null, (SocketFlags) 666,
- out error);
- Assert.Fail ("#1");
- } catch (ObjectDisposedException ex) {
- // Cannot access a disposed object
- Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
- Assert.IsNull (ex.InnerException, "#3");
- Assert.IsNotNull (ex.Message, "#4");
- Assert.AreEqual (typeof (Socket).FullName, ex.ObjectName, "#5");
- } finally {
- s.Close ();
- }
- }
- #endif
- [Test] // ReceiveFrom (Byte [], ref EndPoint)
- public void ReceiveFrom1_Buffer_Null ()
- {
- Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
- ProtocolType.Tcp);
- EndPoint remoteEP = new IPEndPoint (IPAddress.Loopback, 8001);
- try {
- s.ReceiveFrom ((Byte []) null, ref remoteEP);
- Assert.Fail ("#1");
- } catch (ArgumentNullException ex) {
- Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
- Assert.IsNull (ex.InnerException, "#3");
- Assert.IsNotNull (ex.Message, "#4");
- Assert.AreEqual ("buffer", ex.ParamName, "#5");
- } finally {
- s.Close ();
- }
- }
- [Test] // ReceiveFrom (Byte [], ref EndPoint)
- public void ReceiveFrom1_RemoteEP_Null ()
- {
- Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
- ProtocolType.Tcp);
- byte [] buffer = new byte [0];
- EndPoint remoteEP = null;
- try {
- s.ReceiveFrom (buffer, ref remoteEP);
- Assert.Fail ("#1");
- } catch (ArgumentNullException ex) {
- Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
- Assert.IsNull (ex.InnerException, "#3");
- Assert.IsNotNull (ex.Message, "#4");
- Assert.AreEqual ("remoteEP", ex.ParamName, "#5");
- } finally {
- s.Close ();
- }
- }
- [Test] // ReceiveFrom (Byte [], ref EndPoint)
- public void ReceiveFrom1_Socket_Closed ()
- {
- Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
- ProtocolType.Tcp);
- s.Close ();
- EndPoint remoteEP = new IPEndPoint (IPAddress.Loopback, 8001);
- try {
- s.ReceiveFrom ((Byte []) null, ref remoteEP);
- Assert.Fail ("#1");
- } catch (ObjectDisposedException ex) {
- // Cannot access a disposed object
- Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
- Assert.IsNull (ex.InnerException, "#3");
- Assert.IsNotNull (ex.Message, "#4");
- Assert.AreEqual (typeof (Socket).FullName, ex.ObjectName, "#5");
- }
- }
- [Test] // ReceiveFrom (Byte [], SocketFlags, ref EndPoint)
- public void ReceiveFrom2_Buffer_Null ()
- {
- Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
- ProtocolType.Tcp);
- EndPoint remoteEP = new IPEndPoint (IPAddress.Loopback, 8001);
- try {
- s.ReceiveFrom ((Byte []) null, (SocketFlags) 666, ref remoteEP);
- Assert.Fail ("#1");
- } catch (ArgumentNullException ex) {
- Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
- Assert.IsNull (ex.InnerException, "#3");
- Assert.IsNotNull (ex.Message, "#4");
- Assert.AreEqual ("buffer", ex.ParamName, "#5");
- } finally {
- s.Close ();
- }
- }
- [Test] // ReceiveFrom (Byte [], SocketFlags, ref EndPoint)
- public void ReceiveFrom2_RemoteEP_Null ()
- {
- Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
- ProtocolType.Tcp);
- byte [] buffer = new byte [5];
- EndPoint remoteEP = null;
- try {
- s.ReceiveFrom (buffer, (SocketFlags) 666, ref remoteEP);
- Assert.Fail ("#1");
- } catch (ArgumentNullException ex) {
- Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
- Assert.IsNull (ex.InnerException, "#3");
- Assert.IsNotNull (ex.Message, "#4");
- Assert.AreEqual ("remoteEP", ex.ParamName, "#5");
- } finally {
- s.Close ();
- }
- }
- [Test] // ReceiveFrom (Byte [], SocketFlags, ref EndPoint)
- public void ReceiveFrom2_Socket_Closed ()
- {
- Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
- ProtocolType.Tcp);
- s.Close ();
- EndPoint remoteEP = new IPEndPoint (IPAddress.Loopback, 8001);
- try {
- s.ReceiveFrom ((Byte []) null, (SocketFlags) 666, ref remoteEP);
- Assert.Fail ("#1");
- } catch (ObjectDisposedException ex) {
- // Cannot access a disposed object
- Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
- Assert.IsNull (ex.InnerException, "#3");
- Assert.IsNotNull (ex.Message, "#4");
- Assert.AreEqual (typeof (Socket).FullName, ex.ObjectName, "#5");
- }
- }
- [Test] // ReceiveFrom (Byte [], Int32, SocketFlags, ref EndPoint)
- public void ReceiveFrom3_Buffer_Null ()
- {
- Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
- ProtocolType.Tcp);
- EndPoint remoteEP = new IPEndPoint (IPAddress.Loopback, 8001);
- try {
- s.ReceiveFrom ((Byte []) null, -1, (SocketFlags) 666,
- ref remoteEP);
- Assert.Fail ("#1");
- } catch (ArgumentNullException ex) {
- Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
- Assert.IsNull (ex.InnerException, "#3");
- Assert.IsNotNull (ex.Message, "#4");
- Assert.AreEqual ("buffer", ex.ParamName, "#5");
- } finally {
- s.Close ();
- }
- }
- [Test] // ReceiveFrom (Byte [], Int32, SocketFlags, ref EndPoint)
- public void ReceiveFrom3_RemoteEP_Null ()
- {
- Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
- ProtocolType.Tcp);
- byte [] buffer = new byte [5];
- EndPoint remoteEP = null;
- try {
- s.ReceiveFrom (buffer, -1, (SocketFlags) 666, ref remoteEP);
- Assert.Fail ("#1");
- } catch (ArgumentNullException ex) {
- Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
- Assert.IsNull (ex.InnerException, "#3");
- Assert.IsNotNull (ex.Message, "#4");
- Assert.AreEqual ("remoteEP", ex.ParamName, "#5");
- } finally {
- s.Close ();
- }
- }
- [Test] // ReceiveFrom (Byte [], Int32, SocketFlags, ref EndPoint)
- public void ReceiveFrom3_Size_OutOfRange ()
- {
- Socket s;
- byte [] buffer = new byte [5];
- EndPoint remoteEP = new IPEndPoint (IPAddress.Loopback, 8001);
- // size negative
- s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
- ProtocolType.Tcp);
- try {
- s.ReceiveFrom (buffer, -1, (SocketFlags) 666, ref remoteEP);
- Assert.Fail ("#A1");
- } catch (ArgumentOutOfRangeException ex) {
- // Specified argument was out of the range of valid values
- Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#A2");
- Assert.IsNull (ex.InnerException, "#A3");
- Assert.IsNotNull (ex.Message, "#A4");
- Assert.AreEqual ("size", ex.ParamName, "#A5");
- } finally {
- s.Close ();
- }
- // size > buffer length
- s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
- ProtocolType.Tcp);
- try {
- s.ReceiveFrom (buffer, (buffer.Length + 1), (SocketFlags) 666,
- ref remoteEP);
- Assert.Fail ("#B1");
- } catch (ArgumentOutOfRangeException ex) {
- // Specified argument was out of the range of valid values
- Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#B2");
- Assert.IsNull (ex.InnerException, "#B3");
- Assert.IsNotNull (ex.Message, "#B4");
- Assert.AreEqual ("size", ex.ParamName, "#B5");
- } finally {
- s.Close ();
- }
- }
- [Test] // ReceiveFrom (Byte [], Int32, SocketFlags, ref EndPoint)
- public void ReceiveFrom3_Socket_Closed ()
- {
- Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
- ProtocolType.Tcp);
- s.Close ();
- EndPoint remoteEP = new IPEndPoint (IPAddress.Loopback, 8001);
- try {
- s.ReceiveFrom ((Byte []) null, -1, (SocketFlags) 666,
- ref remoteEP);
- Assert.Fail ("#1");
- } catch (ObjectDisposedException ex) {
- // Cannot access a disposed object
- Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
- Assert.IsNull (ex.InnerException, "#3");
- Assert.IsNotNull (ex.Message, "#4");
- Assert.AreEqual (typeof (Socket).FullName, ex.ObjectName, "#5");
- }
- }
- [Test] // ReceiveFrom (Byte [], Int32, Int32, SocketFlags, EndPoint)
- public void ReceiveFrom4_Buffer_Null ()
- {
- Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
- ProtocolType.Tcp);
- EndPoint remoteEP = new IPEndPoint (IPAddress.Loopback, 8001);
- try {
- s.ReceiveFrom ((Byte []) null, -1, -1, (SocketFlags) 666,
- ref remoteEP);
- Assert.Fail ("#1");
- } catch (ArgumentNullException ex) {
- Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
- Assert.IsNull (ex.InnerException, "#3");
- Assert.IsNotNull (ex.Message, "#4");
- Assert.AreEqual ("buffer", ex.ParamName, "#5");
- }
- }
- [Test] // ReceiveFrom (Byte [], Int32, Int32, SocketFlags, EndPoint)
- public void ReceiveFrom4_Offset_OutOfRange ()
- {
- Socket s;
- byte [] buffer = new byte [5];
- EndPoint remoteEP = new IPEndPoint (IPAddress.Loopback, 8001);
- // offset negative
- s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
- ProtocolType.Tcp);
- try {
- s.ReceiveFrom (buffer, -1, 0, (SocketFlags) 666,
- ref remoteEP);
- Assert.Fail ("#A1");
- } catch (ArgumentOutOfRangeException ex) {
- // Specified argument was out of the range of valid values
- Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#A2");
- Assert.IsNull (ex.InnerException, "#A3");
- Assert.IsNotNull (ex.Message, "#A4");
- Assert.AreEqual ("offset", ex.ParamName, "#A5");
- } finally {
- s.Close ();
- }
- // offset > buffer length
- s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
- ProtocolType.Tcp);
- try {
- s.ReceiveFrom (buffer, (buffer.Length + 1), 0, (SocketFlags) 666,
- ref remoteEP);
- Assert.Fail ("#B1");
- } catch (ArgumentOutOfRangeException ex) {
- // Specified argument was out of the range of valid values
- Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#B2");
- Assert.IsNull (ex.InnerException, "#B3");
- Assert.IsNotNull (ex.Message, "#B4");
- Assert.AreEqual ("offset", ex.ParamName, "#B5");
- } finally {
- s.Close ();
- }
- }
- [Test] // ReceiveFrom (Byte [], Int32, Int32, SocketFlags, ref IPEndPoint)
- public void ReceiveFrom4_RemoteEP_Null ()
- {
- Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
- ProtocolType.Tcp);
- byte [] buffer = new byte [5];
- EndPoint remoteEP = null;
- try {
- s.ReceiveFrom (buffer, -1, -1, (SocketFlags) 666, ref remoteEP);
- Assert.Fail ("#1");
- } catch (ArgumentNullException ex) {
- Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
- Assert.IsNull (ex.InnerException, "#3");
- Assert.IsNotNull (ex.Message, "#4");
- Assert.AreEqual ("remoteEP", ex.ParamName, "#5");
- } finally {
- s.Close ();
- }
- }
- [Test] // ReceiveFrom (Byte [], Int32, Int32, SocketFlags, EndPoint)
- public void ReceiveFrom4_Size_OutOfRange ()
- {
- Socket s;
- byte [] buffer = new byte [5];
- EndPoint remoteEP = new IPEndPoint (IPAddress.Loopback, 8001);
- // size negative
- s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
- ProtocolType.Tcp);
- try {
- s.ReceiveFrom (buffer, 0, -1, (SocketFlags) 666,
- ref remoteEP);
- Assert.Fail ("#A1");
- } catch (ArgumentOutOfRangeException ex) {
- // Specified argument was out of the range of valid values
- Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#A2");
- Assert.IsNull (ex.InnerException, "#A3");
- Assert.IsNotNull (ex.Message, "#A4");
- Assert.AreEqual ("size", ex.ParamName, "#A5");
- } finally {
- s.Close ();
- }
- // size > buffer length
- s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
- ProtocolType.Tcp);
- try {
- s.ReceiveFrom (buffer, 0, (buffer.Length + 1), (SocketFlags) 666,
- ref remoteEP);
- Assert.Fail ("#B1");
- } catch (ArgumentOutOfRangeException ex) {
- // Specified argument was out of the range of valid values
- Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#B2");
- Assert.IsNull (ex.InnerException, "#B3");
- Assert.IsNotNull (ex.Message, "#B4");
- Assert.AreEqual ("size", ex.ParamName, "#B5");
- } finally {
- s.Close ();
- }
- // offset + size > buffer length
- s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
- ProtocolType.Tcp);
- try {
- s.ReceiveFrom (buffer, 2, 4, (SocketFlags) 666, ref remoteEP);
- Assert.Fail ("#C1");
- } catch (ArgumentOutOfRangeException ex) {
- // Specified argument was out of the range of valid values
- Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#C2");
- Assert.IsNull (ex.InnerException, "#C3");
- Assert.IsNotNull (ex.Message, "#C4");
- Assert.AreEqual ("size", ex.ParamName, "#C5");
- } finally {
- s.Close ();
- }
- }
- [Test] // ReceiveFrom (Byte [], Int32, Int32, SocketFlags, ref EndPoint)
- public void ReceiveFrom4_Socket_Closed ()
- {
- Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
- ProtocolType.Tcp);
- s.Close ();
- byte [] buffer = new byte [5];
- EndPoint remoteEP = new IPEndPoint (IPAddress.Loopback, 8001);
- try {
- s.ReceiveFrom (buffer, -1, -1, (SocketFlags) 666,
- ref remoteEP);
- Assert.Fail ("#1");
- } catch (ObjectDisposedException ex) {
- // Cannot access a disposed object
- Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
- Assert.IsNull (ex.InnerException, "#3");
- Assert.IsNotNull (ex.Message, "#4");
- Assert.AreEqual (typeof (Socket).FullName, ex.ObjectName, "#5");
- }
- }
- [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);
- }
- //
- // Test case for bug #471580
- [Test]
- public void UdpDoubleBind ()
- {
- Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
- s.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, 1);
-
- s.Bind (new IPEndPoint (IPAddress.Any, 12345));
-
- Socket ss = new Socket (AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
- ss.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, 1);
-
- ss.Bind (new IPEndPoint (IPAddress.Any, 12345));
- // If we make it this far, we succeeded.
-
- ss.Close ();
- s.Close ();
- }
-
- #if NET_2_0
- [Test]
- [Category ("NotOnMac")]
- public void ConnectedProperty ()
- {
- TcpListener listener = new TcpListener (IPAddress.Loopback, 23456);
- listener.Start();
- Socket client = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
- client.Connect (IPAddress.Loopback, 23456);
- Socket server = listener.AcceptSocket ();
- try {
- server.EndSend(server.BeginSend (new byte[10], 0, 10, SocketFlags.None, null, null));
- client.Close ();
- try {
- server.EndReceive (server.BeginReceive (new byte[10], 0, 10, SocketFlags.None, null, null));
- } catch {
- }
- Assert.IsTrue (!client.Connected);
- Assert.IsTrue (!server.Connected);
- } finally {
- listener.Stop ();
- client.Close ();
- server.Close ();
- }
- }
- #endif
- [Test] // GetSocketOption (SocketOptionLevel, SocketOptionName)
- public void GetSocketOption1_Socket_Closed ()
- {
- Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
- s.Close ();
- try {
- s.GetSocketOption (0, 0);
- Assert.Fail ("#1");
- } catch (ObjectDisposedException ex) {
- // Cannot access a disposed object
- Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
- Assert.IsNull (ex.InnerException, "#3");
- Assert.IsNotNull (ex.Message, "#4");
- Assert.AreEqual (typeof (Socket).FullName, ex.ObjectName, "#5");
- }
- }
- [Test] // GetSocketOption (SocketOptionLevel, SocketOptionName, Byte [])
- public void GetSocketOption2_OptionValue_Null ()
- {
- Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
- try {
- s.GetSocketOption (SocketOptionLevel.Socket, SocketOptionName.Linger,
- (byte []) null);
- Assert.Fail ("#1");
- } catch (SocketException ex) {
- // The system detected an invalid pointer address in attempting
- // to use a pointer argument in a call
- Assert.AreEqual (typeof (SocketException), ex.GetType (), "#2");
- Assert.AreEqual (10014, ex.ErrorCode, "#3");
- Assert.IsNull (ex.InnerException, "#4");
- Assert.IsNotNull (ex.Message, "#5");
- Assert.AreEqual (10014, ex.NativeErrorCode, "#6");
- #if NET_2_0
- Assert.AreEqual (SocketError.Fault, ex.SocketErrorCode, "#7");
- #endif
- }
- }
- [Test] // GetSocketOption (SocketOptionLevel, SocketOptionName, Byte [])
- public void GetSocketOption2_Socket_Closed ()
- {
- Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
- s.Close ();
- try {
- s.GetSocketOption (0, 0, (byte []) null);
- Assert.Fail ("#1");
- } catch (ObjectDisposedException ex) {
- // Cannot access a disposed object
- Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
- Assert.IsNull (ex.InnerException, "#3");
- Assert.IsNotNull (ex.Message, "#4");
- Assert.AreEqual (typeof (Socket).FullName, ex.ObjectName, "#5");
- }
- }
- [Test] // GetSocketOption (SocketOptionLevel, SocketOptionName, Int32)
- public void GetSocketOption3_Socket_Closed ()
- {
- Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
- s.Close ();
- try {
- s.GetSocketOption (0, 0, 0);
- Assert.Fail ("#1");
- } catch (ObjectDisposedException ex) {
- // Cannot access a disposed object
- Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
- Assert.IsNull (ex.InnerException, "#3");
- Assert.IsNotNull (ex.Message, "#4");
- Assert.AreEqual (typeof (Socket).FullName, ex.ObjectName, "#5");
- }
- }
- [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Byte [])
- public void SetSocketOption1_DontLinger ()
- {
- using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) {
- s.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.DontLinger,
- new byte [] { 0x00 });
- s.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.DontLinger,
- new byte [] { 0x01 });
- }
- }
- [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Byte [])
- public void SetSocketOption1_DontLinger_Null ()
- {
- using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) {
- try {
- s.SetSocketOption (SocketOptionLevel.Socket,
- SocketOptionName.DontLinger, (byte []) null);
- Assert.Fail ("#1");
- } catch (SocketException ex) {
- // The system detected an invalid pointer address in attempting
- // to use a pointer argument in a call
- Assert.AreEqual (typeof (SocketException), ex.GetType (), "#2");
- Assert.AreEqual (10014, ex.ErrorCode, "#3");
- Assert.IsNull (ex.InnerException, "#4");
- Assert.IsNotNull (ex.Message, "#5");
- Assert.AreEqual (10014, ex.NativeErrorCode, "#6");
- #if NET_2_0
- Assert.AreEqual (SocketError.Fault, ex.SocketErrorCode, "#7");
- #endif
- }
- }
- }
- [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Byte [])
- public void SetSocketOption1_Linger_Null ()
- {
- using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) {
- try {
- s.SetSocketOption (SocketOptionLevel.Socket,
- SocketOptionName.DontLinger, (byte []) null);
- Assert.Fail ("#1");
- } catch (SocketException ex) {
- // The system detected an invalid pointer address in attempting
- // to use a pointer argument in a call
- Assert.AreEqual (typeof (SocketException), ex.GetType (), "#2");
- Assert.AreEqual (10014, ex.ErrorCode, "#3");
- Assert.IsNull (ex.InnerException, "#4");
- Assert.IsNotNull (ex.Message, "#5");
- Assert.AreEqual (10014, ex.NativeErrorCode, "#6");
- #if NET_2_0
- Assert.AreEqual (SocketError.Fault, ex.SocketErrorCode, "#7");
- #endif
- }
- }
- }
- [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Byte [])
- public void SetSocketOption1_Socket_Close ()
- {
- Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
- s.Close ();
- try {
- s.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.DontLinger,
- new byte [] { 0x00 });
- Assert.Fail ("#1");
- } catch (ObjectDisposedException ex) {
- Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
- Assert.IsNull (ex.InnerException, "#3");
- Assert.IsNotNull (ex.Message, "#4");
- Assert.AreEqual (typeof (Socket).FullName, ex.ObjectName, "#5");
- }
- }
- [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Int32)
- public void SetSocketOption2_DontLinger ()
- {
- using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) {
- s.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.DontLinger, 0);
- s.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.DontLinger, 5);
- }
- }
- [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Int32)
- [Category ("NotWorking")]
- public void SetSocketOption2_Linger ()
- {
- using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) {
- s.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.Linger, 0);
- s.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.Linger, 5);
- }
- }
- [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Int32)
- public void SetSocketOption2_Socket_Closed ()
- {
- Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
- s.Close ();
- try {
- s.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.DontLinger, 0);
- Assert.Fail ("#1");
- } catch (ObjectDisposedException ex) {
- // Cannot access a disposed object
- Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
- Assert.IsNull (ex.InnerException, "#3");
- Assert.IsNotNull (ex.Message, "#4");
- Assert.AreEqual (typeof (Socket).FullName, ex.ObjectName, "#5");
- }
- }
- [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Object)
- public void SetSocketOption3_AddMembershipIPv4_IPv6MulticastOption ()
- {
- IPAddress mcast_addr = IPAddress.Parse ("239.255.255.250");
- using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp)) {
- s.Bind (new IPEndPoint (IPAddress.Any, 1901));
- try {
- s.SetSocketOption (SocketOptionLevel.IP, SocketOptionName.AddMembership,
- new IPv6MulticastOption (mcast_addr));
- Assert.Fail ("#1");
- } catch (ArgumentException ex) {
- Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
- Assert.IsNull (ex.InnerException, "#3");
- Assert.IsNotNull (ex.Message, "#4");
- #if NET_2_0
- // The specified value is not a valid 'MulticastOption'
- Assert.IsTrue (ex.Message.IndexOf ("'MulticastOption'") != -1, "#5:" + ex.Message);
- Assert.AreEqual ("optionValue", ex.ParamName, "#6");
- #else
- Assert.AreEqual ("optionValue", ex.Message, "#5");
- Assert.IsNull (ex.ParamName, "#6");
- #endif
- }
- }
- }
- [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Object)
- public void SetSocketOption3_AddMembershipIPv4_MulticastOption ()
- {
- IPAddress mcast_addr = IPAddress.Parse ("239.255.255.250");
- using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp)) {
- s.Bind (new IPEndPoint (IPAddress.Any, 1901));
- s.SetSocketOption (SocketOptionLevel.IP, SocketOptionName.AddMembership,
- new MulticastOption (mcast_addr));
- }
- }
- [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Object)
- [Category ("NotWorking")]
- public void SetSocketOption3_AddMembershipIPv4_Socket_NotBound ()
- {
- IPAddress mcast_addr = IPAddress.Parse ("239.255.255.250");
- Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
- try {
- s.SetSocketOption (SocketOptionLevel.IP, SocketOptionName.AddMembership,
- new MulticastOption (mcast_addr));
- Assert.Fail ("#1");
- } catch (SocketException ex) {
- // An invalid argument was supplied
- Assert.AreEqual (typeof (SocketException), ex.GetType (), "#2");
- Assert.AreEqual (10022, ex.ErrorCode, "#3");
- Assert.IsNull (ex.InnerException, "#4");
- Assert.IsNotNull (ex.Message, "#5");
- Assert.AreEqual (10022, ex.NativeErrorCode, "#6");
- #if NET_2_0
- Assert.AreEqual (SocketError.InvalidArgument, ex.SocketErrorCode, "#7");
- #endif
- } finally {
- s.Close ();
- }
- }
- [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Object)
- [Category ("NotOnMac")]
- public void SetSocketOption3_AddMembershipIPv6_IPv6MulticastOption ()
- {
- #if NET_2_0
- if (!Socket.OSSupportsIPv6)
- #else
- if (!Socket.SupportsIPv6)
- #endif
- Assert.Ignore ("IPv6 not enabled.");
- IPAddress mcast_addr = IPAddress.Parse ("ff02::1");
- using (Socket s = new Socket (AddressFamily.InterNetworkV6, SocketType.Dgram, ProtocolType.Udp)) {
- s.Bind (new IPEndPoint (IPAddress.IPv6Any, 1902));
- s.SetSocketOption (SocketOptionLevel.IPv6, SocketOptionName.AddMembership,
- new IPv6MulticastOption (mcast_addr));
- }
- }
- [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Object)
- public void SetSocketOption3_AddMembershipIPv6_MulticastOption ()
- {
- #if NET_2_0
- if (!Socket.OSSupportsIPv6)
- #else
- if (!Socket.SupportsIPv6)
- #endif
- Assert.Ignore ("IPv6 not enabled.");
- IPAddress mcast_addr = IPAddress.Parse ("ff02::1");
- using (Socket s = new Socket (AddressFamily.InterNetworkV6, SocketType.Dgram, ProtocolType.Udp)) {
- s.Bind (new IPEndPoint (IPAddress.IPv6Any, 1902));
- try {
- s.SetSocketOption (SocketOptionLevel.IPv6, SocketOptionName.AddMembership,
- new MulticastOption (mcast_addr));
- Assert.Fail ("#1");
- } catch (ArgumentException ex) {
- Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
- Assert.IsNull (ex.InnerException, "#3");
- Assert.IsNotNull (ex.Message, "#4");
- #if NET_2_0
- // The specified value is not a valid 'IPv6MulticastOption'
- Assert.IsTrue (ex.Message.IndexOf ("'IPv6MulticastOption'") != -1, "#5:" + ex.Message);
- Assert.AreEqual ("optionValue", ex.ParamName, "#6");
- #else
- Assert.AreEqual ("optionValue", ex.Message, "#5");
- Assert.IsNull (ex.ParamName, "#6");
- #endif
- }
- }
- }
- [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Object)
- [Category ("NotWorking")]
- public void SetSocketOption3_AddMembershipIPv6_Socket_NotBound ()
- {
- IPAddress mcast_addr = IPAddress.Parse ("ff02::1");
- Socket s = new Socket (AddressFamily.InterNetworkV6, SocketType.Dgram, ProtocolType.Udp);
- try {
- s.SetSocketOption (SocketOptionLevel.IPv6, SocketOptionName.AddMembership,
- new IPv6MulticastOption (mcast_addr));
- Assert.Fail ("#1");
- } catch (SocketException ex) {
- // An invalid argument was supplied
- Assert.AreEqual (typeof (SocketException), ex.GetType (), "#2");
- Assert.AreEqual (10022, ex.ErrorCode, "#3");
- Assert.IsNull (ex.InnerException, "#4");
- Assert.IsNotNull (ex.Message, "#5");
- Assert.AreEqual (10022, ex.NativeErrorCode, "#6");
- #if NET_2_0
- Assert.AreEqual (SocketError.InvalidArgument, ex.SocketErrorCode, "#7");
- #endif
- } finally {
- s.Close ();
- }
- }
- [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Object)
- public void SetSocketOption3_DontLinger_Boolean ()
- {
- using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) {
- try {
- s.SetSocketOption (SocketOptionLevel.Socket,
- SocketOptionName.DontLinger, (object) false);
- Assert.Fail ("#1");
- } catch (ArgumentException ex) {
- // The specified value is not valid
- Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
- Assert.IsNull (ex.InnerException, "#3");
- #if NET_2_0
- Assert.IsNotNull (ex.Message, "#4");
- Assert.AreEqual ("optionValue", ex.ParamName, "#5");
- #else
- Assert.AreEqual ("optionValue", ex.Message, "#4");
- Assert.IsNull (ex.ParamName, "#5");
- #endif
- }
- }
- }
- [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Object)
- public void SetSocketOption3_DontLinger_Int32 ()
- {
- using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) {
- try {
- s.SetSocketOption (SocketOptionLevel.Socket,
- SocketOptionName.DontLinger, (object) 0);
- Assert.Fail ("#1");
- } catch (ArgumentException ex) {
- // The specified value is not valid
- Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
- Assert.IsNull (ex.InnerException, "#3");
- #if NET_2_0
- Assert.IsNotNull (ex.Message, "#4");
- Assert.AreEqual ("optionValue", ex.ParamName, "#5");
- #else
- Assert.AreEqual ("optionValue", ex.Message, "#4");
- Assert.IsNull (ex.ParamName, "#5");
- #endif
- }
- }
- }
- [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Object)
- public void SetSocketOption3_DontLinger_LingerOption ()
- {
- using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) {
- try {
- s.SetSocketOption (SocketOptionLevel.Socket,
- SocketOptionName.DontLinger, new LingerOption (true, 1000));
- Assert.Fail ("#1");
- } catch (ArgumentException ex) {
- Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
- Assert.IsNull (ex.InnerException, "#3");
- #if NET_2_0
- // The specified value is not valid
- Assert.IsNotNull (ex.Message, "#4");
- Assert.AreEqual ("optionValue", ex.ParamName, "#5");
- #else
- Assert.AreEqual ("optionValue", ex.Message, "#4");
- Assert.IsNull (ex.ParamName, "#5");
- #endif
- }
- }
- }
- [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Object)
- public void SetSocketOption3_Linger_Boolean ()
- {
- using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) {
- try {
- s.SetSocketOption (SocketOptionLevel.Socket,
- SocketOptionName.Linger, (object) false);
- Assert.Fail ("#1");
- } catch (ArgumentException ex) {
- Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
- Assert.IsNull (ex.InnerException, "#3");
- #if NET_2_0
- // The specified value is not valid
- Assert.IsNotNull (ex.Message, "#4");
- Assert.AreEqual ("optionValue", ex.ParamName, "#5");
- #else
- Assert.AreEqual ("optionValue", ex.Message, "#4");
- Assert.IsNull (ex.ParamName, "#5");
- #endif
- }
- }
- }
- [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Object)
- public void SetSocketOption3_Linger_Int32 ()
- {
- using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) {
- try {
- s.SetSocketOption (SocketOptionLevel.Socket,
- SocketOptionName.Linger, (object) 0);
- Assert.Fail ("#1");
- } catch (ArgumentException ex) {
- Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
- Assert.IsNull (ex.InnerException, "#3");
- #if NET_2_0
- // The specified value is not valid
- Assert.IsNotNull (ex.Message, "#4");
- Assert.AreEqual ("optionValue", ex.ParamName, "#5");
- #else
- Assert.AreEqual ("optionValue", ex.Message, "#4");
- Assert.IsNull (ex.ParamName, "#5");
- #endif
- }
- }
- }
- [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Object)
- public void SetSocketOption3_Linger_LingerOption ()
- {
- using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) {
- s.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.Linger,
- new LingerOption (false, 0));
- s.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.Linger,
- new LingerOption (true, 0));
- s.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.Linger,
- new LingerOption (false, 1000));
- s.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.Linger,
- new LingerOption (true, 1000));
- }
- }
- [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Object)
- public void SetSocketOption3_DropMembershipIPv4_IPv6MulticastOption ()
- {
- IPAddress mcast_addr = IPAddress.Parse ("239.255.255.250");
- using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp)) {
- s.Bind (new IPEndPoint (IPAddress.Any, 1901));
- s.SetSocketOption (SocketOptionLevel.IP, SocketOptionName.AddMembership,
- new MulticastOption (mcast_addr));
- try {
- s.SetSocketOption (SocketOptionLevel.IP, SocketOptionName.DropMembership,
- new IPv6MulticastOption (mcast_addr));
- Assert.Fail ("#1");
- } catch (ArgumentException ex) {
- Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
- Assert.IsNull (ex.InnerException, "#3");
- Assert.IsNotNull (ex.Message, "#4");
- #if NET_2_0
- // The specified value is not a valid 'MulticastOption'
- Assert.IsTrue (ex.Message.IndexOf ("'MulticastOption'") != -1, "#5:" + ex.Message);
- Assert.AreEqual ("optionValue", ex.ParamName, "#6");
- #else
- Assert.AreEqual ("optionValue", ex.Message, "#5");
- Assert.IsNull (ex.ParamName, "#6");
- #endif
- }
- }
- }
- [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Object)
- public void SetSocketOption3_DropMembershipIPv4_MulticastOption ()
- {
- IPAddress mcast_addr = IPAddress.Parse ("239.255.255.250");
- using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp)) {
- MulticastOption option = new MulticastOption (mcast_addr);
- s.Bind (new IPEndPoint (IPAddress.Any, 1901));
- s.SetSocketOption (SocketOptionLevel.IP, SocketOptionName.AddMembership,
- option);
- s.SetSocketOption (SocketOptionLevel.IP, SocketOptionName.DropMembership,
- option);
- }
- }
- [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Object)
- [Category ("NotWorking")]
- public void SetSocketOption3_DropMembershipIPv4_Socket_NotBound ()
- {
- IPAddress mcast_addr = IPAddress.Parse ("239.255.255.250");
- Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
- try {
- s.SetSocketOption (SocketOptionLevel.IP, SocketOptionName.DropMembership,
- new MulticastOption (mcast_addr));
- Assert.Fail ("#1");
- } catch (SocketException ex) {
- // An invalid argument was supplied
- Assert.AreEqual (typeof (SocketException), ex.GetType (), "#2");
- Assert.AreEqual (10022, ex.ErrorCode, "#3");
- Assert.IsNull (ex.InnerException, "#4");
- Assert.IsNotNull (ex.Message, "#5");
- Assert.AreEqual (10022, ex.NativeErrorCode, "#6");
- #if NET_2_0
- Assert.AreEqual (SocketError.InvalidArgument, ex.SocketErrorCode, "#7");
- #endif
- } finally {
- s.Close ();
- }
- }
- [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Object)
- [Category ("NotOnMac")]
- public void SetSocketOption3_DropMembershipIPv6_IPv6MulticastOption ()
- {
- #if NET_2_0
- if (!Socket.OSSupportsIPv6)
- #else
- if (!Socket.SupportsIPv6)
- #endif
- Assert.Ignore ("IPv6 not enabled.");
- using (Socket s = new Socket (AddressFamily.InterNetworkV6, SocketType.Dgram, ProtocolType.Udp)) {
- IPv6MulticastOption option = new IPv6MulticastOption (
- IPAddress.Parse ("ff02::1"));
- s.Bind (new IPEndPoint (IPAddress.IPv6Any, 1902));
- s.SetSocketOption (SocketOptionLevel.IPv6, SocketOptionName.AddMembership,
- option);
- s.SetSocketOption (SocketOptionLevel.IPv6, SocketOptionName.DropMembership,
- option);
- }
- }
- [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Object)
- [Category ("NotOnMac")]
- public void SetSocketOption3_DropMembershipIPv6_MulticastOption ()
- {
- #if NET_2_0
- if (!Socket.OSSupportsIPv6)
- #else
- if (!Socket.SupportsIPv6)
- #endif
- Assert.Ignore ("IPv6 not enabled.");
- IPAddress mcast_addr = IPAddress.Parse ("ff02::1");
- using (Socket s = new Socket (AddressFamily.InterNetworkV6, SocketType.Dgram, ProtocolType.Udp)) {
- s.Bind (new IPEndPoint (IPAddress.IPv6Any, 1902));
- s.SetSocketOption (SocketOptionLevel.IPv6, SocketOptionName.AddMembership,
- new IPv6MulticastOption (mcast_addr));
- try {
- s.SetSocketOption (SocketOptionLevel.IPv6, SocketOptionName.DropMembership,
- new MulticastOption (mcast_addr));
- Assert.Fail ("#1");
- } catch (ArgumentException ex) {
- Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
- Assert.IsNull (ex.InnerException, "#3");
- Assert.IsNotNull (ex.Message, "#4");
- #if NET_2_0
- // The specified value is not a valid 'IPv6MulticastOption'
- Assert.IsTrue (ex.Message.IndexOf ("'IPv6MulticastOption'") != -1, "#5:" + ex.Message);
- Assert.AreEqual ("optionValue", ex.ParamName, "#6");
- #else
- Assert.AreEqual ("optionValue", ex.Message, "#5");
- Assert.IsNull (ex.ParamName, "#6");
- #endif
- }
- }
- }
- [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Object)
- [Category ("NotWorking")]
- public void SetSocketOption3_DropMembershipIPv6_Socket_NotBound ()
- {
- IPAddress mcast_addr = IPAddress.Parse ("ff02::1");
- Socket s = new Socket (AddressFamily.InterNetworkV6, SocketType.Dgram, ProtocolType.Udp);
- try {
- s.SetSocketOption (SocketOptionLevel.IPv6, SocketOptionName.DropMembership,
- new IPv6MulticastOption (mcast_addr));
- Assert.Fail ("#1");
- } catch (SocketException ex) {
- // An invalid argument was supplied
- Assert.AreEqual (typeof (SocketException), ex.GetType (), "#2");
- Assert.AreEqual (10022, ex.ErrorCode, "#3");
- Assert.IsNull (ex.InnerException, "#4");
- Assert.IsNotNull (ex.Message, "#5");
- Assert.AreEqual (10022, ex.NativeErrorCode, "#6");
- #if NET_2_0
- Assert.AreEqual (SocketError.InvalidArgument, ex.SocketErrorCode, "#7");
- #endif
- } finally {
- s.Close ();
- }
- }
- [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Object)
- public void SetSocketOption3_OptionValue_Null ()
- {
- using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) {
- try {
- s.SetSocketOption (SocketOptionLevel.Socket,
- SocketOptionName.Linger, (object) null);
- Assert.Fail ("#1");
- } catch (ArgumentNullException ex) {
- Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
- Assert.IsNull (ex.InnerException, "#3");
- Assert.IsNotNull (ex.Message, "#4");
- Assert.AreEqual ("optionValue", ex.ParamName, "#5");
- }
- }
- }
- [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Object)
- public void SetSocketOption3_Socket_Closed ()
- {
- Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
- s.Close ();
- try {
- s.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.Linger,
- new LingerOption (false, 0));
- Assert.Fail ("#1");
- } catch (ObjectDisposedException ex) {
- // Cannot access a disposed object
- Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
- Assert.IsNull (ex.InnerException, "#3");
- Assert.IsNotNull (ex.Message, "#4");
- Assert.AreEqual (typeof (Socket).FullName, ex.ObjectName, "#5");
- }
- }
- [Test]
- public void Shutdown_NoConnect ()
- {
- Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
- s.Bind (new IPEndPoint (IPAddress.Loopback, 0));
- s.Listen (1);
- try {
- s.Shutdown (SocketShutdown.Both);
- Assert.Fail ("#1");
- } catch (SocketException exc) {
- Assert.AreEqual (10057, exc.ErrorCode, "#2");
- } finally {
- s.Close ();
- }
- }
- [Test]
- [ExpectedException (typeof (NullReferenceException))]
- public void ReceiveAsync_Null ()
- {
- using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) {
- s.ReceiveAsync (null);
- }
- }
- [Test]
- [ExpectedException (typeof (NullReferenceException))]
- public void ReceiveAsync_Default ()
- {
- using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) {
- SocketAsyncEventArgs saea = new SocketAsyncEventArgs ();
- s.ReceiveAsync (saea);
- }
- }
- [Test]
- [ExpectedException (typeof (NullReferenceException))]
- public void ReceiveAsync_NullBuffer ()
- {
- using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) {
- SocketAsyncEventArgs saea = new SocketAsyncEventArgs ();
- saea.SetBuffer (null, 0, 0);
- s.ReceiveAsync (null);
- }
- }
- [Test]
- [ExpectedException (typeof (ObjectDisposedException))]
- public void ReceiveAsync_ClosedSocket ()
- {
- Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
- s.Close ();
- s.ReceiveAsync (null);
- }
- [Test]
- [ExpectedException (typeof (NullReferenceException))]
- public void SendAsync_Null ()
- {
- using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) {
- s.SendAsync (null);
- }
- }
- [Test]
- [ExpectedException (typeof (NullReferenceException))]
- public void SendAsync_Default ()
- {
- using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) {
- SocketAsyncEventArgs saea = new SocketAsyncEventArgs ();
- s.SendAsync (saea);
- }
- }
- [Test]
- [ExpectedException (typeof (NullReferenceException))]
- public void SendAsync_NullBuffer ()
- {
- using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) {
- SocketAsyncEventArgs saea = new SocketAsyncEventArgs ();
- saea.SetBuffer (null, 0, 0);
- s.SendAsync (null);
- }
- }
- [Test]
- [ExpectedException (typeof (ObjectDisposedException))]
- public void SendAsync_ClosedSocket ()
- {
- Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
- s.Close ();
- s.SendAsync (null);
- }
- }
- }
|