| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736 |
- // 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.Diagnostics;
- using System.Linq;
- using System.Collections;
- using System.Threading;
- using System.Reflection;
- using System.Text.RegularExpressions;
- using System.Threading.Tasks;
- using System.Net;
- using System.Net.Sockets;
- using NUnit.Framework;
- using System.IO;
- using System.Collections.Generic;
- using MonoTests.Helpers;
- namespace MonoTests.System.Net.Sockets
- {
- [TestFixture]
- public class SocketTest
- {
- public const string BogusAddress = "192.168.244.244";
- [Test]
- #if FEATURE_NO_BSD_SOCKETS
- [ExpectedException (typeof (PlatformNotSupportedException))]
- #endif
- public void ConnectIPAddressAny ()
- {
- IPEndPoint ep = new IPEndPoint (IPAddress.Any, NetworkHelpers.FindFreePort ());
- /* 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) {
- // address incompatible with protocol
- int expectedError = 10047;
- Assert.AreEqual (expectedError, ex.ErrorCode,
- "#2");
- }
- }
- [Test]
- [Category ("NotWorking")]
- [Category ("InetAccess")]
- #if FEATURE_NO_BSD_SOCKETS
- [ExpectedException (typeof (PlatformNotSupportedException))]
- #endif
- public void BogusEndConnect ()
- {
- IPAddress ipOne = IPAddress.Parse (BogusAddress);
- IPEndPoint ipEP = new IPEndPoint (ipOne, NetworkHelpers.FindFreePort ());
- 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) {
- // Actual error code depends on network configuration.
- var error = (SocketError) ex.ErrorCode;
- Assert.That (error == SocketError.TimedOut ||
- error == SocketError.ConnectionRefused ||
- error == SocketError.NetworkUnreachable ||
- error == SocketError.HostUnreachable, "#2");
- }
- }
- [Test]
- [ExpectedException (typeof (ArgumentNullException))]
- public void SelectEmpty ()
- {
- ArrayList list = new ArrayList ();
- Socket.Select (list, list, list, 1000);
- }
-
- private bool BlockingConnect (bool block, ref int port)
- {
- IPEndPoint ep = new IPEndPoint(IPAddress.Loopback, port);
- Socket server = new Socket(AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
- server.Bind(ep);
- server.Blocking=block;
- ep = (IPEndPoint) server.LocalEndPoint;
- port = ep.Port;
- server.Listen(0);
- Socket conn = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
- conn.Connect (ep);
- Socket client = null;
- var sw = Stopwatch.StartNew ();
- while (sw.ElapsedMilliseconds < 100)
- {
- try {
- client = server.Accept();
- break;
- }
- catch (SocketException ex) {
- if (ex.SocketErrorCode == SocketError.WouldBlock)
- continue;
- throw;
- }
- }
- Assert.IsNotNull (client, "Couldn't accept a client connection within 100ms.");
- bool client_block = client.Blocking;
- client.Close();
- conn.Close();
- server.Close();
-
- return(client_block);
- }
- [Test]
- #if FEATURE_NO_BSD_SOCKETS
- [ExpectedException (typeof (PlatformNotSupportedException))]
- #endif
- [Ignore ("https://github.com/mono/mono/issues/6513")] // frequently fails on ARM
- public void AcceptBlockingStatus()
- {
- bool block;
- int port = 0;
-
- block = BlockingConnect(true, ref port);
- Assert.AreEqual (block, true, "BlockingStatus01");
- block = BlockingConnect(false, ref port);
- 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]
- #if FEATURE_NO_BSD_SOCKETS
- [ExpectedException (typeof (PlatformNotSupportedException))]
- #endif
- 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,
- NetworkHelpers.FindFreePort ()),
- new AsyncCallback (CFACallback),
- sock);
- CFACalledBack.WaitOne ();
- Assert.AreEqual (CFAConnected, false, "ConnectFail");
- }
-
- [Test]
- #if FEATURE_NO_BSD_SOCKETS
- [ExpectedException (typeof (PlatformNotSupportedException))]
- #endif
- public void SetSocketOptionBoolean ()
- {
- IPEndPoint ep = new IPEndPoint (IPAddress.Loopback, 0);
- Socket sock = new Socket (ep.Address.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
- try {
- sock.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.KeepAlive, true);
- } finally {
- sock.Close ();
- }
- }
- [Test]
- #if FEATURE_NO_BSD_SOCKETS
- [ExpectedException (typeof (PlatformNotSupportedException))]
- #endif
- public void TestSelect1 ()
- {
- Socket srv = CreateServer (0);
- ClientSocket clnt = new ClientSocket (srv.LocalEndPoint);
- Socket acc = null;
- try {
- clnt.Connect ();
- clnt.Write ();
- acc = srv.Accept ();
- 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 ();
- clnt.Close ();
- }
- }
- static Socket CreateServer (int port)
- {
- Socket sock = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
- sock.Bind (new IPEndPoint (IPAddress.Loopback, port));
- 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 Connect ()
- {
- sock.Connect (ep);
- }
- public void Close ()
- {
- 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]
- #if FEATURE_NO_BSD_SOCKETS
- [ExpectedException (typeof (PlatformNotSupportedException))]
- #else
- [ExpectedException (typeof (ObjectDisposedException))]
- #endif
- public void Disposed19 ()
- {
- Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
- EndPoint ep = new IPEndPoint (IPAddress.Any, NetworkHelpers.FindFreePort ());
- s.Close();
- s.SendTo (buf, 0, ep);
- }
- [Test]
- #if FEATURE_NO_BSD_SOCKETS
- [ExpectedException (typeof (PlatformNotSupportedException))]
- #else
- [ExpectedException (typeof (ObjectDisposedException))]
- #endif
- public void Disposed20 ()
- {
- Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
- EndPoint ep = new IPEndPoint (IPAddress.Any, NetworkHelpers.FindFreePort ());
- s.Close();
- s.SendTo (buf, 10, 0, ep);
- }
- [Test]
- #if FEATURE_NO_BSD_SOCKETS
- [ExpectedException (typeof (PlatformNotSupportedException))]
- #else
- [ExpectedException (typeof (ObjectDisposedException))]
- #endif
- public void Disposed21 ()
- {
- Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
- EndPoint ep = new IPEndPoint (IPAddress.Any, NetworkHelpers.FindFreePort ());
- s.Close();
- s.SendTo (buf, 0, 10, 0, ep);
- }
- [Test]
- #if FEATURE_NO_BSD_SOCKETS
- [ExpectedException (typeof (PlatformNotSupportedException))]
- #else
- [ExpectedException (typeof (ObjectDisposedException))]
- #endif
- public void Disposed22 ()
- {
- Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
- EndPoint ep = new IPEndPoint (IPAddress.Any, NetworkHelpers.FindFreePort ());
- 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]
- #if FEATURE_NO_BSD_SOCKETS
- [ExpectedException (typeof (PlatformNotSupportedException))]
- #endif
- public void GetHashCodeTest ()
- {
- Socket server = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream, ProtocolType.Tcp);
- server.Bind (IPAddress.Loopback, out IPEndPoint 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 ();
- Assert.AreEqual (hashcodeB, hashcodeC, "#2");
- 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]
- [Category ("RequiresBSDSockets")] // This verifies particular error codes, which we don't care about when nothing's working anyway.
- public void SocketErrorTest ()
- {
- Socket sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
- IPEndPoint ep = NetworkHelpers.LocalEphemeralEndPoint ();
-
- 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 ();
- }
-
- [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 ("NotWorking")] // DontFragment doesn't work
- 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 ("NotWorking")] // DontFragment doesn't work
- 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) {
- } 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");
- } 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");
- } 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, NetworkHelpers.FindFreePort ()));
- 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]
- #if FEATURE_NO_BSD_SOCKETS
- [ExpectedException (typeof (PlatformNotSupportedException))]
- #endif
- public void IsBoundTcp ()
- {
- Socket sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
-
- Assert.AreEqual (false, sock.IsBound, "IsBoundTcp #1");
-
- sock.Bind (IPAddress.Loopback, out IPEndPoint 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]
- #if FEATURE_NO_BSD_SOCKETS
- [ExpectedException (typeof (PlatformNotSupportedException))]
- #endif
- public void IsBoundUdp ()
- {
- Socket sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Dgram,
- ProtocolType.Udp);
-
- Assert.AreEqual (false, sock.IsBound, "IsBoundUdp #1");
-
- sock.Bind (IPAddress.Loopback, out IPEndPoint 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");
- } 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");
- } 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]
- public void TtlChangeOverflow ()
- {
- Socket sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
-
- try {
- sock.Ttl = 256;
- Assert.Fail ("TtlChangeOverflow #1");
- } catch (ArgumentOutOfRangeException ex) {
- Assert.AreEqual ("value", ex.ParamName,
- "TtlChangeOverflow #2");
- } 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");
- } 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) {
- } 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) {
- } 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");
- } 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");
- } 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]
- #if FEATURE_NO_BSD_SOCKETS
- [ExpectedException (typeof (PlatformNotSupportedException))]
- #else
- [ExpectedException (typeof(InvalidOperationException))]
- #endif
- public void BeginAcceptNotListening ()
- {
- Socket sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
- sock.Bind (new IPEndPoint (IPAddress.Any, 0));
-
- sock.BeginAccept (BACallback, sock);
-
- sock.Close ();
- }
- [Test]
- #if FEATURE_NO_BSD_SOCKETS
- [ExpectedException (typeof (PlatformNotSupportedException))]
- #endif
- public void BeginAccept ()
- {
- Socket sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
-
- sock.Bind (IPAddress.Loopback, out IPEndPoint 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]
- #if FEATURE_NO_BSD_SOCKETS
- [ExpectedException (typeof (PlatformNotSupportedException))]
- #endif
- public void BeginAcceptData ()
- {
- Socket sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
-
- sock.Bind (IPAddress.Loopback, out IPEndPoint 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]
- #if FEATURE_NO_BSD_SOCKETS
- [ExpectedException (typeof (PlatformNotSupportedException))]
- #endif
- public void BeginAcceptSocketUdp ()
- {
- Socket sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
- Socket acc = new Socket (AddressFamily.InterNetwork,
- SocketType.Dgram,
- ProtocolType.Udp);
-
- sock.Bind (IPAddress.Loopback, out int _);
- sock.Listen (1);
-
- try {
- sock.BeginAccept (acc, 256, BADCallback, sock);
- Assert.Fail ("BeginAcceptSocketUdp #1");
- } catch (SocketException ex) {
- Assert.AreEqual (10022, ex.ErrorCode, "BeginAcceptSocketUdp #2");
- } finally {
- acc.Close ();
- sock.Close ();
- }
- }
-
- [Test]
- #if FEATURE_NO_BSD_SOCKETS
- [ExpectedException (typeof (PlatformNotSupportedException))]
- #endif
- public void BeginAcceptSocketBound ()
- {
- Socket sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
- Socket acc = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
-
- sock.Bind (IPAddress.Loopback, out IPEndPoint ep1);
- sock.Listen (1);
- acc.Bind (IPAddress.Loopback, out IPEndPoint ep2);
-
- try {
- sock.BeginAccept (acc, 256, BADCallback, sock);
- Assert.Fail ("BeginAcceptSocketBound #1");
- } catch (InvalidOperationException) {
- } finally {
- acc.Close ();
- sock.Close ();
- }
- }
-
- [Test]
- #if FEATURE_NO_BSD_SOCKETS
- [ExpectedException (typeof (PlatformNotSupportedException))]
- #endif
- public void BeginAcceptSocket ()
- {
- Socket sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
- Socket acc = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
-
- sock.Bind (IPAddress.Loopback, out IPEndPoint 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) {
- } finally {
- acc.Close ();
- }
- }
- [Test]
- #if FEATURE_NO_BSD_SOCKETS
- [ExpectedException (typeof (PlatformNotSupportedException))]
- #endif
- public void BeginAcceptSocketAccClosed ()
- {
- Socket sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
- Socket acc = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
- sock.Bind (IPAddress.Loopback, out int _);
- sock.Listen (1);
-
- acc.Close ();
-
- BADCalledBack.Reset ();
-
- try {
- sock.BeginAccept (acc, 256, BADCallback, null);
- Assert.Fail ("BeginAcceptSocketAccClosed #1");
- } catch (ObjectDisposedException) {
- } finally {
- sock.Close ();
- }
- }
-
- static bool BCConnected = false;
- static ManualResetEvent BCCalledBack = new ManualResetEvent (false);
-
- private static void BCCallback (IAsyncResult asyncResult)
- {
- Socket sock = (Socket)asyncResult.AsyncState;
-
- try {
- sock.EndConnect (asyncResult);
- } catch (Exception e) {
- Console.WriteLine ("BCCallback exception:");
- Console.WriteLine (e);
- throw;
- }
- BCConnected = true;
-
- BCCalledBack.Set ();
- }
-
- [Test]
- #if FEATURE_NO_BSD_SOCKETS
- [ExpectedException (typeof (PlatformNotSupportedException))]
- #endif
- 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;
- listen.Bind (ip, out IPEndPoint ep);
- listen.Listen (1);
-
- BCCalledBack.Reset ();
-
- BCConnected = false;
-
- sock.BeginConnect (ip, ep.Port, 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]
- #if FEATURE_NO_BSD_SOCKETS
- [ExpectedException (typeof (PlatformNotSupportedException))]
- #endif
- public void BeginConnectAddressPortNull ()
- {
- var port = NetworkHelpers.FindFreePort ();
- Socket sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
- IPAddress ip = null;
- try {
- sock.BeginConnect (ip, port, BCCallback,
- sock);
- Assert.Fail ("BeginConnectAddressPortNull #1");
- } catch (ArgumentNullException) {
- } finally {
- sock.Close ();
- }
- }
- [Test]
- #if FEATURE_NO_BSD_SOCKETS
- [ExpectedException (typeof (PlatformNotSupportedException))]
- #endif
- public void BeginConnectAddressPortListen ()
- {
- Socket sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
- IPAddress ip = IPAddress.Loopback;
- sock.Bind (ip, out IPEndPoint ep);
- sock.Listen (1);
-
- try {
- sock.BeginConnect (ip, ep.Port, BCCallback, sock);
- Assert.Fail ("BeginConnectAddressPortListen #1");
- } catch (InvalidOperationException) {
- } finally {
- sock.Close ();
- }
- }
-
- [Test]
- #if FEATURE_NO_BSD_SOCKETS
- [ExpectedException (typeof (PlatformNotSupportedException))]
- #else
- [ExpectedException (typeof(ObjectDisposedException))]
- #endif
- public void BeginConnectAddressPortClosed ()
- {
- var port = NetworkHelpers.FindFreePort ();
- Socket sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
- IPAddress ip = IPAddress.Loopback;
-
- sock.Close ();
-
- sock.BeginConnect (ip, port, BCCallback, sock);
- }
-
- [Test]
- [Category ("NotOnMac")]
- /*
- * This is not a Mono bug.
- *
- * By default, only 127.0.0.1 is enabled and you must explicitly
- * enable additional addresses using 'sudo ifconfig lo0 alias 127.0.0.1'.
- *
- * I tested this on Mac OS 10.7.4; a 'ping 127.0.0.2' does not work
- * until I add that alias.
- *
- */
- public void BeginConnectMultiple ()
- {
- Socket sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
- Socket listen = 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");
- listen.Bind (IPAddress.Loopback, out IPEndPoint ep);
- listen.Listen (1);
-
- BCCalledBack.Reset ();
-
- BCConnected = false;
-
- sock.BeginConnect (ips, ep.Port, BCCallback, sock);
-
- /* Longer wait here, because the ms runtime
- * takes a lot longer to not connect
- */
- /*
- if (BCCalledBack.WaitOne (30000, false) == false) {
- Assert.Fail ("BeginConnectMultiple wait failed");
- }
- */
- Assert.IsTrue (BCCalledBack.WaitOne (30000), "#0");
-
- 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]
- #if FEATURE_NO_BSD_SOCKETS
- [ExpectedException (typeof (PlatformNotSupportedException))]
- #endif
- public void BeginConnectMultiple2 ()
- {
- Socket sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
- Socket listen = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
- // Need at least two addresses.
- var ips = Dns.GetHostAddresses ("localhost");
- if (ips.Length < 1)
- Assert.Ignore ("This test needs at least two IP addresses.");
- var allIps = new IPAddress [ips.Length + 1];
- allIps [0] = IPAddress.Loopback;
- ips.CopyTo (allIps, 1);
- /*
- * Only bind to the loopback interface, so all the non-loopback
- * IP addresses will fail. BeginConnect()/EndConnect() should
- * succeed it it can connect to at least one of the requested
- * addresses.
- */
- listen.Bind (IPAddress.Loopback, out IPEndPoint ep);
- listen.Listen (1);
-
- BCCalledBack.Reset ();
-
- BCConnected = false;
-
- sock.BeginConnect (allIps, ep.Port, BCCallback, sock);
-
- /* Longer wait here, because the ms runtime
- * takes a lot longer to not connect
- */
- if (BCCalledBack.WaitOne (10000, false) == false) {
- Assert.Fail ("BeginConnectMultiple2 wait failed");
- }
-
- Assert.AreEqual (true, BCConnected, "BeginConnectMultiple2 #1");
- Assert.AreEqual (AddressFamily.InterNetwork, sock.RemoteEndPoint.AddressFamily, "BeginConnectMultiple2 #2");
- IPEndPoint remep = (IPEndPoint)sock.RemoteEndPoint;
- Assert.AreEqual (IPAddress.Loopback, remep.Address, "BeginConnectMultiple2 #2");
- sock.Close ();
- listen.Close ();
- }
- [Test]
- #if FEATURE_NO_BSD_SOCKETS
- [ExpectedException (typeof (PlatformNotSupportedException))]
- #endif
- public void BeginConnectMultipleNull ()
- {
- var port = NetworkHelpers.FindFreePort ();
- Socket sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
- IPAddress[] ips = null;
-
- try {
- sock.BeginConnect (ips, port, BCCallback,
- sock);
- Assert.Fail ("BeginConnectMultipleNull #1");
- } catch (ArgumentNullException) {
- } finally {
- sock.Close ();
- }
- }
- [Test]
- #if FEATURE_NO_BSD_SOCKETS
- [ExpectedException (typeof (PlatformNotSupportedException))]
- #endif
- public void BeginConnectMultipleListen ()
- {
- 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.Bind (IPAddress.Loopback, out IPEndPoint ep);
- sock.Listen (1);
-
- try {
- sock.BeginConnect (ips, ep.Port, BCCallback,
- sock);
- Assert.Fail ("BeginConnectMultipleListen #1");
- } catch (InvalidOperationException) {
- } finally {
- sock.Close ();
- }
- }
-
- [Test]
- #if FEATURE_NO_BSD_SOCKETS
- [ExpectedException (typeof (PlatformNotSupportedException))]
- #else
- [ExpectedException (typeof(ObjectDisposedException))]
- #endif
- public void BeginConnectMultipleClosed ()
- {
- var port = NetworkHelpers.FindFreePort ();
- 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, port, 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) {
- } finally {
- sock.Close ();
- }
- }
- [Test]
- #if FEATURE_NO_BSD_SOCKETS
- [ExpectedException (typeof (PlatformNotSupportedException))]
- #endif
- public void BeginConnectHostPortListen ()
- {
- Socket sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
- IPAddress ip = IPAddress.Loopback;
-
- sock.Bind (ip, out IPEndPoint ep);
- sock.Listen (1);
-
- try {
- sock.BeginConnect ("localhost", ep.Port,
- BCCallback, sock);
- Assert.Fail ("BeginConnectHostPortListen #1");
- } catch (InvalidOperationException) {
- } 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) {
- } 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"
- #if FEATURE_NO_BSD_SOCKETS
- [ExpectedException (typeof (PlatformNotSupportedException))]
- #endif
- 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;
-
- listen.Bind (ip, out IPEndPoint ep);
- listen.Listen (1);
-
- sock.Connect (ip, ep.Port);
-
- 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");
- } finally {
- sock.Close ();
- }
- }
-
- [Test]
- public void BeginSendSocketError ()
- {
- }
-
- [Test]
- public void BeginSendGeneric ()
- {
- }
-
- [Test]
- public void BeginSendGenericSocketError ()
- {
- }
-
- [Test]
- #if FEATURE_NO_BSD_SOCKETS
- [ExpectedException (typeof (PlatformNotSupportedException))]
- #endif
- public void BindTwice ()
- {
- Socket sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
-
- sock.Bind (IPAddress.Loopback, out IPEndPoint ep1);
-
- try {
- sock.Bind (IPAddress.Loopback, out IPEndPoint ep2);
- Assert.Fail ("BindTwice #1");
- } catch (SocketException ex) {
- Assert.AreEqual (10022, ex.ErrorCode, "BindTwice #2");
- } finally {
- sock.Close ();
- }
- }
-
- [Test]
- #if FEATURE_NO_BSD_SOCKETS
- [ExpectedException (typeof (PlatformNotSupportedException))]
- #endif
- public void Close ()
- {
- Socket sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
- Socket listen = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
-
- listen.Bind (IPAddress.Loopback, out IPEndPoint 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]
- #if FEATURE_NO_BSD_SOCKETS
- [ExpectedException (typeof (PlatformNotSupportedException))]
- #endif
- 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;
- listen.Bind (ip, out IPEndPoint ep);
- listen.Listen (1);
-
- sock.Connect (ip, ep.Port);
-
- Assert.AreEqual (true, sock.Connected, "ConnectAddressPort #1");
-
- sock.Close ();
- listen.Close ();
- }
- [Test]
- #if FEATURE_NO_BSD_SOCKETS
- [ExpectedException (typeof (PlatformNotSupportedException))]
- #endif
- public void ConnectAddressPortNull ()
- {
- var port = NetworkHelpers.FindFreePort ();
- Socket sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
- IPAddress ip = null;
- try {
- sock.Connect (ip, port);
- Assert.Fail ("ConnectAddressPortNull #1");
- } catch (ArgumentNullException) {
- } finally {
- sock.Close ();
- }
- }
- [Test]
- #if FEATURE_NO_BSD_SOCKETS
- [ExpectedException (typeof (PlatformNotSupportedException))]
- #endif
- public void ConnectAddressPortListen ()
- {
- Socket sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
- IPAddress ip = IPAddress.Loopback;
- sock.Bind (ip, out IPEndPoint ep);
- sock.Listen (1);
-
- try {
- sock.Connect (ip, ep.Port);
- Assert.Fail ("ConnectAddressPortListen #1");
- } catch (InvalidOperationException) {
- } finally {
- sock.Close ();
- }
- }
-
- [Test]
- #if FEATURE_NO_BSD_SOCKETS
- [ExpectedException (typeof (PlatformNotSupportedException))]
- #else
- [ExpectedException (typeof(ObjectDisposedException))]
- #endif
- public void ConnectAddressPortClosed ()
- {
- var port = NetworkHelpers.FindFreePort ();
- Socket sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
- IPAddress ip = IPAddress.Loopback;
-
- sock.Close ();
-
- sock.Connect (ip, port);
- }
-
- [Test]
- [Category ("NotOnMac")] // MacOSX trashes the fd after the failed connect attempt to 127.0.0.4
- /*
- * This is not a Mono bug.
- *
- * By default, only 127.0.0.1 is enabled and you must explicitly
- * enable additional addresses using 'sudo ifconfig lo0 alias 127.0.0.1'.
- *
- * I tested this on Mac OS 10.7.4; a 'ping 127.0.0.2' does not work
- * until I add that alias.
- *
- * However, after doing so, Mac OS treats these as separate addresses, ie. attempting
- * to connect to 127.0.0.4 yields a connection refused.
- *
- * When using Connect(), the .NET runtime also throws an exception if connecting to
- * any of the IP addresses fails. This is different with BeginConnect()/EndConnect()
- * which succeeds when it can connect to at least one of the addresses.
- *
- */
- public void ConnectMultiple ()
- {
- Socket sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
- Socket listen = 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");
- listen.Bind (IPAddress.Loopback, out IPEndPoint ep);
- listen.Listen (1);
-
- sock.Connect (ips, ep.Port);
-
- 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]
- #if FEATURE_NO_BSD_SOCKETS
- [ExpectedException (typeof (PlatformNotSupportedException))]
- #endif
- public void ConnectMultiple2 ()
- {
- Socket sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
- Socket listen = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
- // Need at least two addresses.
- var ips = Dns.GetHostAddresses ("localhost");
- if (ips.Length < 1)
- Assert.Ignore ("This test needs at least two IP addresses.");
- var allIps = new IPAddress [ips.Length + 1];
- allIps [0] = IPAddress.Loopback;
- ips.CopyTo (allIps, 1);
- /*
- * Bind to IPAddress.Any; Connect() will fail unless it can
- * connect to all the addresses in allIps.
- */
- listen.Bind (IPAddress.Any, out IPEndPoint ep);
- listen.Listen (1);
-
- sock.Connect (allIps, ep.Port);
-
- Assert.AreEqual (true, sock.Connected, "ConnectMultiple2 #1");
- Assert.AreEqual (AddressFamily.InterNetwork, sock.RemoteEndPoint.AddressFamily, "ConnectMultiple2 #2");
- IPEndPoint remep = (IPEndPoint)sock.RemoteEndPoint;
- Assert.AreEqual (IPAddress.Loopback, remep.Address, "ConnectMultiple2 #3");
-
- sock.Close ();
- listen.Close ();
- }
- [Test]
- #if FEATURE_NO_BSD_SOCKETS
- [ExpectedException (typeof (PlatformNotSupportedException))]
- #endif
- public void ConnectMultipleNull ()
- {
- var port = NetworkHelpers.FindFreePort ();
- Socket sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
- IPAddress[] ips = null;
-
- try {
- sock.Connect (ips, port);
- Assert.Fail ("ConnectMultipleNull #1");
- } catch (ArgumentNullException) {
- } finally {
- sock.Close ();
- }
- }
- [Test]
- #if FEATURE_NO_BSD_SOCKETS
- [ExpectedException (typeof (PlatformNotSupportedException))]
- #endif
- public void ConnectMultipleListen ()
- {
- 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.Bind (IPAddress.Loopback, out IPEndPoint ep);
- sock.Listen (1);
-
- try {
- sock.Connect (ips, ep.Port);
- Assert.Fail ("ConnectMultipleListen #1");
- } catch (InvalidOperationException) {
- } finally {
- sock.Close ();
- }
- }
-
- [Test]
- #if FEATURE_NO_BSD_SOCKETS
- [ExpectedException (typeof (PlatformNotSupportedException))]
- #else
- [ExpectedException (typeof(ObjectDisposedException))]
- #endif
- public void ConnectMultipleClosed ()
- {
- var port = NetworkHelpers.FindFreePort ();
- 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, port);
- }
-
- [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) {
- } finally {
- sock.Close ();
- }
- }
- [Test]
- #if FEATURE_NO_BSD_SOCKETS
- [ExpectedException (typeof (PlatformNotSupportedException))]
- #endif
- public void ConnectHostPortListen ()
- {
- Socket sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
- IPAddress ip = IPAddress.Loopback;
-
- sock.Bind (ip, out IPEndPoint ep);
- sock.Listen (1);
-
- try {
- sock.Connect ("localhost", ep.Port);
- Assert.Fail ("ConnectHostPortListen #1");
- } catch (InvalidOperationException) {
- } 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) {
- } finally {
- sock.Close ();
- }
- }
- [Test]
- #if FEATURE_NO_BSD_SOCKETS
- [ExpectedException (typeof (PlatformNotSupportedException))]
- #else
- [ExpectedException (typeof(ObjectDisposedException))]
- #endif
- 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"
- #if FEATURE_NO_BSD_SOCKETS
- [ExpectedException (typeof (PlatformNotSupportedException))]
- #endif
- 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;
-
- listen.Bind (ip, out IPEndPoint ep);
- listen.Listen (1);
-
- sock.Connect (ip, ep.Port);
-
- 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 TestDefaultsDualMode ()
- {
- using (var socket = new Socket (AddressFamily.InterNetworkV6, SocketType.Stream, ProtocolType.Tcp)){
- Assert.IsTrue (socket.DualMode, "In Mono, DualMode must be true when constructing InterNetworkV6 sockets");
- }
- using (var socket = new Socket (SocketType.Stream, ProtocolType.Tcp)){
- Assert.AreEqual (AddressFamily.InterNetworkV6, socket.AddressFamily, "When creating sockets of type stream/tcp, the address family should be InterNetworkV6");
- Assert.IsTrue (socket.DualMode, "In Mono, DualMode must be true when constructing InterNetworkV6 sockets");
- socket.DualMode = false;
- Assert.IsFalse (socket.DualMode, "Setting of DualSocket should turn DualSockets off");
- }
-
- }
-
- [Test]
- #if FEATURE_NO_BSD_SOCKETS
- [ExpectedException (typeof (PlatformNotSupportedException))]
- #endif
- public void ReceiveGeneric ()
- {
- int i;
- Socket listensock = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
- listensock.Bind (IPAddress.Loopback, out IPEndPoint 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]
- #if FEATURE_NO_BSD_SOCKETS
- [ExpectedException (typeof (PlatformNotSupportedException))]
- #endif
- public void SendGeneric ()
- {
- int i;
- Socket listensock = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
- listensock.Bind (IPAddress.Loopback, out IPEndPoint 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]
- #if FEATURE_NO_BSD_SOCKETS
- [ExpectedException (typeof (AggregateException))] // Something catches the PlatformNotSupportedException and re-throws an AggregateException
- #endif
- public void ConcurrentExceedSocketLimit ()
- {
- var tasks = new Task[4];
- for (int i = 0; i < 4; i++) {
- tasks[i] = Task.Factory.StartNew (() => SendGenericExceedBuffer ());
- }
- Task.WaitAll (tasks);
- }
- [Test]
- #if FEATURE_NO_BSD_SOCKETS
- [ExpectedException (typeof (PlatformNotSupportedException))]
- #endif
- public void SendGenericExceedBuffer ()
- {
- // Create a buffer larger than the default max.
- const int BUFFER_SIZE = 65 * 1024;
- int i;
- Socket listensock = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
- listensock.Bind (IPAddress.Loopback, out IPEndPoint 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[BUFFER_SIZE];
- for (i = 0; i < BUFFER_SIZE; i++) {
- sendbuf[i] = (byte)i;
- }
- Task sendTask = Task.Factory.StartNew(() => {
- int sent = sendsock.Send (sendbuf);
- Assert.AreEqual (BUFFER_SIZE, sent, "#1");
- });
- byte[] recvbuf = new byte[BUFFER_SIZE];
- Task recvTask = Task.Factory.StartNew(() => {
- int totalReceived = 0;
- byte[] buffer = new byte[256];
- while (totalReceived < sendbuf.Length) {
- int recvd = clientsock.Receive (buffer, 0, buffer.Length, SocketFlags.None);
- Array.Copy (buffer, 0, recvbuf, totalReceived, recvd);
- totalReceived += recvd;
- }
- Assert.AreEqual (BUFFER_SIZE, totalReceived, "#2");
- });
- Assert.IsTrue (Task.WaitAll (new []{sendTask, recvTask}, 20 * 1000), "#2a");
- for (i = 0; i < BUFFER_SIZE; i++) {
- Assert.AreEqual (recvbuf[i], sendbuf[i],
- "#3/" + 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");
- } finally {
- sock.Close ();
- }
- }
- 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]
- #if FEATURE_NO_BSD_SOCKETS
- [ExpectedException (typeof (PlatformNotSupportedException))]
- #endif
- public void CloseWhileReceiving ()
- {
- CWRSocket = new Socket (AddressFamily.InterNetwork,
- SocketType.Dgram,
- ProtocolType.Udp);
- CWRSocket.Bind (IPAddress.Loopback, out int _);
-
- 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 (int port)
- {
- byte[] bytes = new byte[8];
- int readbyte;
-
- Socket sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
- sock.Connect (new IPEndPoint (IPAddress.Loopback,
- port));
-
- 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");
- }
- }
- [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 ();
- }
- }
- [Test] // ReceiveFrom (Byte [], ref EndPoint)
- #if FEATURE_NO_BSD_SOCKETS
- [ExpectedException (typeof (PlatformNotSupportedException))]
- #endif
- public void ReceiveFrom1_Buffer_Null ()
- {
- Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
- ProtocolType.Tcp);
- EndPoint remoteEP = new IPEndPoint (IPAddress.Loopback, NetworkHelpers.FindFreePort ());
- 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)
- #if FEATURE_NO_BSD_SOCKETS
- [ExpectedException (typeof (PlatformNotSupportedException))]
- #endif
- public void ReceiveFrom1_Socket_Closed ()
- {
- Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
- ProtocolType.Tcp);
- s.Close ();
- EndPoint remoteEP = new IPEndPoint (IPAddress.Loopback, NetworkHelpers.FindFreePort ());
- 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)
- #if FEATURE_NO_BSD_SOCKETS
- [ExpectedException (typeof (PlatformNotSupportedException))]
- #endif
- public void ReceiveFrom2_Buffer_Null ()
- {
- Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
- ProtocolType.Tcp);
- EndPoint remoteEP = new IPEndPoint (IPAddress.Loopback, NetworkHelpers.FindFreePort ());
- 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)
- #if FEATURE_NO_BSD_SOCKETS
- [ExpectedException (typeof (PlatformNotSupportedException))]
- #endif
- public void ReceiveFrom2_Socket_Closed ()
- {
- Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
- ProtocolType.Tcp);
- s.Close ();
- EndPoint remoteEP = new IPEndPoint (IPAddress.Loopback, NetworkHelpers.FindFreePort ());
- 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)
- #if FEATURE_NO_BSD_SOCKETS
- [ExpectedException (typeof (PlatformNotSupportedException))]
- #endif
- public void ReceiveFrom3_Buffer_Null ()
- {
- Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
- ProtocolType.Tcp);
- EndPoint remoteEP = new IPEndPoint (IPAddress.Loopback, NetworkHelpers.FindFreePort ());
- try {
- s.ReceiveFrom ((Byte []) null, 0, (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, buffer.Length, (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)
- #if FEATURE_NO_BSD_SOCKETS
- [ExpectedException (typeof (PlatformNotSupportedException))]
- #endif
- public void ReceiveFrom3_Size_OutOfRange ()
- {
- Socket s;
- byte [] buffer = new byte [5];
- EndPoint remoteEP = new IPEndPoint (IPAddress.Loopback, NetworkHelpers.FindFreePort ());
- // 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)
- #if FEATURE_NO_BSD_SOCKETS
- [ExpectedException (typeof (PlatformNotSupportedException))]
- #endif
- public void ReceiveFrom3_Socket_Closed ()
- {
- Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
- ProtocolType.Tcp);
- s.Close ();
- EndPoint remoteEP = new IPEndPoint (IPAddress.Loopback, NetworkHelpers.FindFreePort ());
- 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)
- #if FEATURE_NO_BSD_SOCKETS
- [ExpectedException (typeof (PlatformNotSupportedException))]
- #endif
- public void ReceiveFrom4_Buffer_Null ()
- {
- Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
- ProtocolType.Tcp);
- EndPoint remoteEP = new IPEndPoint (IPAddress.Loopback, NetworkHelpers.FindFreePort ());
- 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)
- #if FEATURE_NO_BSD_SOCKETS
- [ExpectedException (typeof (PlatformNotSupportedException))]
- #endif
- public void ReceiveFrom4_Offset_OutOfRange ()
- {
- Socket s;
- byte [] buffer = new byte [5];
- EndPoint remoteEP = new IPEndPoint (IPAddress.Loopback, NetworkHelpers.FindFreePort ());
- // 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, 0, buffer.Length, (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)
- #if FEATURE_NO_BSD_SOCKETS
- [ExpectedException (typeof (PlatformNotSupportedException))]
- #endif
- public void ReceiveFrom4_Size_OutOfRange ()
- {
- Socket s;
- byte [] buffer = new byte [5];
- EndPoint remoteEP = new IPEndPoint (IPAddress.Loopback, NetworkHelpers.FindFreePort ());
- // 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)
- #if FEATURE_NO_BSD_SOCKETS
- [ExpectedException (typeof (PlatformNotSupportedException))]
- #endif
- 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, NetworkHelpers.FindFreePort ());
- 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]
- #if FEATURE_NO_BSD_SOCKETS
- [ExpectedException (typeof (PlatformNotSupportedException))]
- #endif
- public void ReceiveRemoteClosed ()
- {
- Socket sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
- sock.Bind (IPAddress.Loopback, out int port);
- sock.Listen (1);
-
- RRCReady.Reset ();
- Thread client_thread = new Thread (() => RRCClientThread (port));
- 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]
- #if FEATURE_NO_BSD_SOCKETS
- [ExpectedException (typeof (PlatformNotSupportedException))]
- #endif
- public void UdpDoubleBind ()
- {
- using (Socket s = new Socket (AddressFamily.InterNetwork,
- SocketType.Dgram, ProtocolType.Udp))
- using (Socket ss = new Socket (AddressFamily.InterNetwork,
- SocketType.Dgram, ProtocolType.Udp)) {
- var supportsReuseAddress = true;
- try {
- s.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
- } catch (SocketException e) {
- // Exception is thrown when ReuseAddress is not supported
- Assert.AreEqual ((int) SocketError.OperationNotSupported, e.NativeErrorCode,
- "Expected SocketError.OperationNotSupported");
- supportsReuseAddress = false;
- }
- s.Bind (IPAddress.Any, out IPEndPoint ep);
- if (supportsReuseAddress)
- ss.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
- try {
- ss.Bind (new IPEndPoint (IPAddress.Any, ep.Port));
- if (!supportsReuseAddress)
- Assert.Fail ("Reusing address is not supported, exception was expected on second bind.");
- } catch (SocketException e) {
- }
- }
- }
- // Test case for bug #31557
- [Test]
- #if FEATURE_NO_BSD_SOCKETS
- [ExpectedException (typeof (PlatformNotSupportedException))]
- #endif
- public void TcpDoubleBind ()
- {
- using (Socket s = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream, ProtocolType.Tcp))
- using (Socket ss = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream, ProtocolType.Tcp)) {
- var supportsReuseAddress = true;
- try {
- s.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
- } catch (SocketException e) {
- // Exception is thrown when ReuseAddress is not supported
- Assert.AreEqual ((int) SocketError.OperationNotSupported, e.NativeErrorCode,
- "Expected SocketError.OperationNotSupported");
- supportsReuseAddress = false;
- }
- s.Bind (IPAddress.Any, out IPEndPoint ep);
- s.Listen(1);
- if (supportsReuseAddress)
- ss.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
- try {
- ss.Bind (new IPEndPoint (IPAddress.Any, ep.Port));
- ss.Listen(1);
- if (!supportsReuseAddress)
- Assert.Fail ("Reusing address is not supported, exception was expected on second bind.");
- } catch (SocketException e) {
- }
- }
- }
- // Test case for https://bugzilla.novell.com/show_bug.cgi?id=443346
- // See also https://bugzilla.xamarin.com/show_bug.cgi?id=52157
- [Test]
- #if FEATURE_NO_BSD_SOCKETS
- [ExpectedException (typeof (PlatformNotSupportedException))]
- #endif
- public void ConnectedProperty ()
- {
- var listener = NetworkHelpers.CreateAndStartTcpListener (out int port);
- var client = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
- client.Connect (IPAddress.Loopback, port);
- var server = listener.AcceptSocket ();
- const int blobSize = 2048;
- // Small send/recv buffers so that our send operation will fill them
- server.ReceiveBufferSize = server.SendBufferSize = 256;
- client.ReceiveBufferSize = client.SendBufferSize = 256;
- // Setting this linger state causes shutdown to be a hard close (RST if send data is pending)
- server.LingerState = client.LingerState = new LingerOption (true, 0);
- // KeepAlive being set introduces another opportunity for a RST according to some documentation
- server.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.KeepAlive, true);
- client.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.KeepAlive, true);
- try
- {
- // Fill the send buffer
- client.Send (new byte[blobSize]);
- // Now shut down the socket. Because of the linger state this will hard close and send RST.
- client.Shutdown (SocketShutdown.Both);
- client.Close ();
- Assert.IsFalse (client.Connected);
- Assert.IsTrue (server.Connected);
- var isDead = false;
- try
- {
- // On Windows this BeginSend succeeds... it really shouldn't and a sync send fails.
- // On Linux this fails with a ConnectionReset.
- var ar = server.BeginSend (new byte[blobSize], 0, blobSize, SocketFlags.None, null, null);
- // On Mac we get an erroneous WSAESHUTDOWN (10058) here
- server.EndSend (ar);
- }
- catch (SocketException se)
- {
- isDead = true;
- if (
- (se.SocketErrorCode != SocketError.ConnectionReset) &&
- (se.SocketErrorCode != SocketError.Shutdown)
- )
- throw;
- }
- if (!isDead)
- {
- try
- {
- // On Windows this second send operation will fail with a ConnectionReset.
- var ar = server.BeginSend (new byte[blobSize], 0, blobSize, SocketFlags.None, null, null);
- server.EndSend (ar);
- }
- catch (SocketException se)
- {
- if (se.SocketErrorCode != SocketError.ConnectionReset)
- throw;
- }
- }
- Assert.IsFalse (client.Connected);
- Assert.IsFalse (server.Connected);
- }
- finally
- {
- listener.Stop ();
- client.Close ();
- server.Close ();
- }
- }
- [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");
- Assert.AreEqual (SocketError.Fault, ex.SocketErrorCode, "#7");
- }
- }
- [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");
- Assert.AreEqual (SocketError.Fault, ex.SocketErrorCode, "#7");
- }
- }
- }
- [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");
- Assert.AreEqual (SocketError.Fault, ex.SocketErrorCode, "#7");
- }
- }
- }
- [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)
- #if FEATURE_NO_BSD_SOCKETS
- [ExpectedException (typeof (PlatformNotSupportedException))]
- #endif
- 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, 0));
- 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");
- // The specified value is not a valid 'MulticastOption'
- Assert.IsTrue (ex.Message.IndexOf ("'MulticastOption'") != -1, "#5:" + ex.Message);
- Assert.AreEqual ("optionValue", ex.ParamName, "#6");
- }
- }
- }
- [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Object)
- #if FEATURE_NO_BSD_SOCKETS
- [ExpectedException (typeof (PlatformNotSupportedException))]
- #endif
- 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, 0));
- 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");
- Assert.AreEqual (SocketError.InvalidArgument, ex.SocketErrorCode, "#7");
- } finally {
- s.Close ();
- }
- }
- [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Object)
- #if FEATURE_NO_BSD_SOCKETS
- [ExpectedException (typeof (PlatformNotSupportedException))]
- #endif
- public void SetSocketOption3_AddMembershipIPv6_IPv6MulticastOption ()
- {
- if (!Socket.OSSupportsIPv6)
- 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, 0));
- s.SetSocketOption (SocketOptionLevel.IPv6, SocketOptionName.AddMembership,
- new IPv6MulticastOption (mcast_addr));
- }
- }
- [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Object)
- #if FEATURE_NO_BSD_SOCKETS
- [ExpectedException (typeof (PlatformNotSupportedException))]
- #endif
- public void SetSocketOption3_AddMembershipIPv6_MulticastOption ()
- {
- if (!Socket.OSSupportsIPv6)
- 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, 0));
- 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");
- // The specified value is not a valid 'IPv6MulticastOption'
- Assert.IsTrue (ex.Message.IndexOf ("'IPv6MulticastOption'") != -1, "#5:" + ex.Message);
- Assert.AreEqual ("optionValue", ex.ParamName, "#6");
- }
- }
- }
- [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");
- Assert.AreEqual (SocketError.InvalidArgument, ex.SocketErrorCode, "#7");
- } 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");
- Assert.IsNotNull (ex.Message, "#4");
- Assert.AreEqual ("optionValue", ex.ParamName, "#5");
- }
- }
- }
- [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");
- Assert.IsNotNull (ex.Message, "#4");
- Assert.AreEqual ("optionValue", ex.ParamName, "#5");
- }
- }
- }
- [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");
- // The specified value is not valid
- Assert.IsNotNull (ex.Message, "#4");
- Assert.AreEqual ("optionValue", ex.ParamName, "#5");
- }
- }
- }
- [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");
- // The specified value is not valid
- Assert.IsNotNull (ex.Message, "#4");
- Assert.AreEqual ("optionValue", ex.ParamName, "#5");
- }
- }
- }
- [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");
- // The specified value is not valid
- Assert.IsNotNull (ex.Message, "#4");
- Assert.AreEqual ("optionValue", ex.ParamName, "#5");
- }
- }
- }
- [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)
- #if FEATURE_NO_BSD_SOCKETS
- [ExpectedException (typeof (PlatformNotSupportedException))]
- #endif
- 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, 0));
- 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");
- // The specified value is not a valid 'MulticastOption'
- Assert.IsTrue (ex.Message.IndexOf ("'MulticastOption'") != -1, "#5:" + ex.Message);
- Assert.AreEqual ("optionValue", ex.ParamName, "#6");
- }
- }
- }
- [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Object)
- #if FEATURE_NO_BSD_SOCKETS
- [ExpectedException (typeof (PlatformNotSupportedException))]
- #endif
- 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, 0));
- 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");
- Assert.AreEqual (SocketError.InvalidArgument, ex.SocketErrorCode, "#7");
- } finally {
- s.Close ();
- }
- }
- [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Object)
- #if FEATURE_NO_BSD_SOCKETS
- [ExpectedException (typeof (PlatformNotSupportedException))]
- #endif
- public void SetSocketOption3_DropMembershipIPv6_IPv6MulticastOption ()
- {
- if (!Socket.OSSupportsIPv6)
- 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, 0));
- s.SetSocketOption (SocketOptionLevel.IPv6, SocketOptionName.AddMembership,
- option);
- s.SetSocketOption (SocketOptionLevel.IPv6, SocketOptionName.DropMembership,
- option);
- }
- }
- [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Object)
- #if FEATURE_NO_BSD_SOCKETS
- [ExpectedException (typeof (PlatformNotSupportedException))]
- #endif
- public void SetSocketOption3_DropMembershipIPv6_MulticastOption ()
- {
- if (!Socket.OSSupportsIPv6)
- 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, 0));
- 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");
- // The specified value is not a valid 'IPv6MulticastOption'
- Assert.IsTrue (ex.Message.IndexOf ("'IPv6MulticastOption'") != -1, "#5:" + ex.Message);
- Assert.AreEqual ("optionValue", ex.ParamName, "#6");
- }
- }
- }
- [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");
- Assert.AreEqual (SocketError.InvalidArgument, ex.SocketErrorCode, "#7");
- } 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]
- [Category ("RequiresBSDSockets")] // on watchOS device this happens: System.Net.Sockets.SocketException : The requested address is not valid in this context. This situation is too complex to detect and throw a PlatformNotSupportedException, so just ignore it.
- public void SetSocketOption_MulticastInterfaceIndex_Any ()
- {
- IPAddress ip = IPAddress.Parse ("239.255.255.250");
- int index = 0;
- using (Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp))
- {
- s.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.MulticastInterface, IPAddress.HostToNetworkOrder(index));
- s.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, new MulticastOption(ip, index));
- }
- }
- [Test]
- [Category ("RequiresBSDSockets")] // on watchOS device this happens: System.Net.Sockets.SocketException : The requested address is not valid in this context. This situation is too complex to detect and throw a PlatformNotSupportedException, so just ignore it.
- public void SetSocketOption_MulticastInterfaceIndex_Loopback ()
- {
- IPAddress ip = IPAddress.Parse ("239.255.255.250");
- int index = 1;
- using (Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp))
- {
- s.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.MulticastInterface, IPAddress.HostToNetworkOrder(index));
- s.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, new MulticastOption(ip, index));
- }
- }
- [Test]
- public void SetSocketOption_MulticastInterfaceIndex_Invalid ()
- {
- IPAddress ip = IPAddress.Parse ("239.255.255.250");
- int index = 31415;
- using (Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp))
- {
- try
- {
- s.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.MulticastInterface, IPAddress.HostToNetworkOrder(index));
- Assert.Fail ("#1");
- }
- catch
- {}
- try
- {
- s.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, new MulticastOption(ip, index));
- Assert.Fail ("#2");
- }
- catch
- {}
- }
- }
- [Test]
- #if FEATURE_NO_BSD_SOCKETS
- [ExpectedException (typeof (PlatformNotSupportedException))]
- #endif
- 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);
- }
-
- [Test]
- #if FEATURE_NO_BSD_SOCKETS
- [ExpectedException (typeof (PlatformNotSupportedException))]
- #endif
- [Ignore ("https://bugzilla.xamarin.com/show_bug.cgi?id=43172")]
- public void SendAsyncFile ()
- {
- Socket serverSocket = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
- serverSocket.Bind (new IPEndPoint (IPAddress.Loopback, 0));
- serverSocket.Listen (1);
- var mReceived = new ManualResetEvent (false);
- serverSocket.BeginAccept (AsyncCall => {
- byte[] bytes = new byte [1024];
- Socket listener = (Socket)AsyncCall.AsyncState;
- Socket client = listener.EndAccept (AsyncCall);
- client.Receive (bytes, bytes.Length, 0);
- client.Close ();
- mReceived.Set ();
- }, serverSocket);
-
- Socket clientSocket = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
- clientSocket.Connect (serverSocket.LocalEndPoint);
- clientSocket.NoDelay = true;
-
- // Initialize buffer used to create testing file
- var buffer = new byte [1024];
- for (int i = 0; i < 1024; ++i)
- buffer [i] = (byte) (i % 256);
-
- string temp = Path.GetTempFileName ();
- try {
- // Testing file creation
- using (StreamWriter sw = new StreamWriter (temp)) {
- sw.Write (buffer);
- }
- var mSent = new ManualResetEvent (false);
- // Async Send File to server
- clientSocket.BeginSendFile(temp, (ar) => {
- Socket client = (Socket) ar.AsyncState;
- client.EndSendFile (ar);
- mSent.Set ();
- }, clientSocket);
- Assert.IsTrue (mSent.WaitOne (5000), "#1");
- Assert.IsTrue (mReceived.WaitOne (5000), "#2");
- } finally {
- if (File.Exists (temp))
- File.Delete (temp);
-
- clientSocket.Close ();
- serverSocket.Close ();
- }
- }
-
- [Test]
- #if FEATURE_NO_BSD_SOCKETS
- [ExpectedException (typeof (PlatformNotSupportedException))]
- #endif
- public void ConnectToIPV4EndPointUsingDualModelSocket () {
- /*
- * IPv6 DualMode sockets are defaults in Mono. Explicitly specify that
- * anyways in this test to make it more interoparable with .NET where
- * IPv6 and DualMode needs to be specified.
- */
- using (var server = new Socket (AddressFamily.InterNetworkV6, SocketType.Stream, ProtocolType.Tcp)) {
- server.DualMode = true;
- server.Bind (IPAddress.Loopback, out int _);
- /*
- * Nothing to Accept the connect - we need a backlog to make sure we don't get
- Connection refused.
- */
- server.Listen (3);
-
- var ep = server.LocalEndPoint as IPEndPoint;
- var client = new Socket (AddressFamily.InterNetworkV6, SocketType.Stream, ProtocolType.Tcp);
- client.DualMode = true;
- client.Connect (ep);
- client.Disconnect (false);
- client.Close ();
- client = new Socket (AddressFamily.InterNetworkV6, SocketType.Stream, ProtocolType.Tcp);
- client.DualMode = true;
- client.Connect (IPAddress.Loopback, ep.Port);
- client.Disconnect (false);
- client.Close ();
- client = new Socket (AddressFamily.InterNetworkV6, SocketType.Stream, ProtocolType.Tcp);
- client.DualMode = true;
- client.Connect (new [] { IPAddress.Loopback }, ep.Port);
- client.Disconnect (false);
- client.Close ();
- }
- }
-
- [Test]
- #if FEATURE_NO_BSD_SOCKETS
- [ExpectedException (typeof (PlatformNotSupportedException))]
- #endif
- public void BeginConnectToIPV4EndPointUsingDualModelSocket () {
- /*
- * IPv6 DualMode sockets are defaults in Mono. Explicitly specify that
- * anyways in this test to make it more interoparable with .NET where
- * IPv6 and DualMode needs to be specified.
- */
- using (var server = new Socket (AddressFamily.InterNetworkV6, SocketType.Stream, ProtocolType.Tcp))
- {
- server.DualMode = true;
- server.Bind (IPAddress.Loopback, out int _);
- server.Listen (10);
-
- var ep = server.LocalEndPoint as IPEndPoint;
- BCCalledBack.Reset ();
- var client = new Socket (AddressFamily.InterNetworkV6, SocketType.Stream, ProtocolType.Tcp);
- client.DualMode = true;
- var ar1 = client.BeginConnect (ep, BCCallback, client);
- Assert.IsTrue (BCCalledBack.WaitOne (10000), "#1");
- Assert.AreEqual(server.AddressFamily, client.RemoteEndPoint.AddressFamily, "#2");
- Assert.AreEqual(server.AddressFamily, client.LocalEndPoint.AddressFamily, "#3");
- client.Disconnect (false);
- client.Close ();
- BCCalledBack.Reset ();
- client = new Socket (AddressFamily.InterNetworkV6, SocketType.Stream, ProtocolType.Tcp);
- client.DualMode = true;
- var ar2 = client.BeginConnect (IPAddress.Loopback, ep.Port, BCCallback, client);
- Assert.IsTrue (BCCalledBack.WaitOne (10000), "#4");
- Assert.AreEqual(server.AddressFamily, client.RemoteEndPoint.AddressFamily, "#5");
- Assert.AreEqual(server.AddressFamily, client.LocalEndPoint.AddressFamily, "#6");
- client.Disconnect (false);
- client.Close ();
- BCCalledBack.Reset ();
- client = new Socket (AddressFamily.InterNetworkV6, SocketType.Stream, ProtocolType.Tcp);
- client.DualMode = true;
- var ar3 = client.BeginConnect (new [] {IPAddress.Loopback}, ep.Port, BCCallback, client);
- Assert.IsTrue (BCCalledBack.WaitOne (10000), "#7");
- Assert.AreEqual(server.AddressFamily, client.RemoteEndPoint.AddressFamily, "#8");
- Assert.AreEqual(server.AddressFamily, client.LocalEndPoint.AddressFamily, "#9");
- client.Disconnect (false);
- client.Close();
- }
- }
- [Test]
- #if FEATURE_NO_BSD_SOCKETS
- [ExpectedException (typeof (PlatformNotSupportedException))]
- #endif
- public void UdpMulticasTimeToLive ()
- {
- /* see https://bugzilla.xamarin.com/show_bug.cgi?id=36941 */
- using (Socket socket = new Socket (AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp)) {
- socket.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, 1);
- socket.Bind (IPAddress.Any, out int _);
- socket.SetSocketOption (SocketOptionLevel.IP, SocketOptionName.MulticastTimeToLive, 19);
- }
- }
- [Test] // Covers 41616
- public void ConnectAsyncUnhandledEx ()
- {
- var mre = new ManualResetEvent (false);
- var endPoint = new IPEndPoint(0,0);
- var socket = new Socket(endPoint.AddressFamily, SocketType.Stream, ProtocolType.Unspecified);
- var socketArgs = new SocketAsyncEventArgs();
- socketArgs.RemoteEndPoint = endPoint;
- socketArgs.Completed += (sender, e) => mre.Set ();
- socket.ConnectAsync (socketArgs);
- Assert.IsTrue (mre.WaitOne (1000), "ConnectedAsync timeout");
- }
- [Test] // Covers https://bugzilla.xamarin.com/show_bug.cgi?id=52549
- public void SocketMismatchProtocol ()
- {
- try {
- using (Socket socket = new Socket (AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Tcp));
- Assert.Fail ("#1");
- } catch (SocketException e) {
- // Only work on OSX
- // Assert.AreEqual(SocketError.ProtocolType, e.SocketErrorCode, "#2");
- }
- try {
- using (Socket socket = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Udp));
- Assert.Fail ("#3");
- } catch (SocketException e) {
- // Only work on OSX
- // Assert.AreEqual(SocketError.ProtocolType, e.SocketErrorCode, "#4");
- }
- }
- }
- }
|