SocketTest.cs 134 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677
  1. // System.Net.Sockets.SocketTest.cs
  2. //
  3. // Authors:
  4. // Brad Fitzpatrick ([email protected])
  5. // Gonzalo Paniagua Javier ([email protected])
  6. //
  7. // (C) Copyright 2003 Brad Fitzpatrick
  8. // Copyright (c) 2005 Novell, Inc. (http://www.novell.com)
  9. //
  10. using System;
  11. using System.Diagnostics;
  12. using System.Linq;
  13. using System.Collections;
  14. using System.Threading;
  15. using System.Reflection;
  16. using System.Text.RegularExpressions;
  17. using System.Threading.Tasks;
  18. using System.Net;
  19. using System.Net.Sockets;
  20. using NUnit.Framework;
  21. using System.IO;
  22. using System.Collections.Generic;
  23. using MonoTests.Helpers;
  24. namespace MonoTests.System.Net.Sockets
  25. {
  26. [TestFixture]
  27. public class SocketTest
  28. {
  29. // note: also used in SocketCas tests
  30. public const string BogusAddress = "192.168.244.244";
  31. public const int BogusPort = 23483;
  32. [Test]
  33. #if FEATURE_NO_BSD_SOCKETS
  34. [ExpectedException (typeof (PlatformNotSupportedException))]
  35. #endif
  36. public void ConnectIPAddressAny ()
  37. {
  38. IPEndPoint ep = new IPEndPoint (IPAddress.Any, NetworkHelpers.FindFreePort ());
  39. /* UDP sockets use Any to disconnect
  40. try {
  41. using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp)) {
  42. s.Connect (ep);
  43. s.Close ();
  44. }
  45. Assert.Fail ("#1");
  46. } catch (SocketException ex) {
  47. Assert.AreEqual (10049, ex.ErrorCode, "#2");
  48. }
  49. */
  50. try {
  51. using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) {
  52. s.Connect (ep);
  53. s.Close ();
  54. }
  55. Assert.Fail ("#3");
  56. } catch (SocketException ex) {
  57. Assert.AreEqual (10049, ex.ErrorCode, "#4");
  58. }
  59. }
  60. [Test]
  61. [Ignore ("Bug #75158")] // Looks like MS fails after the .ctor, when you try to use the socket
  62. public void IncompatibleAddress ()
  63. {
  64. IPEndPoint epIPv6 = new IPEndPoint (IPAddress.IPv6Any,
  65. NetworkHelpers.FindFreePort ());
  66. try {
  67. using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP)) {
  68. s.Connect (epIPv6);
  69. s.Close ();
  70. }
  71. Assert.Fail ("#1");
  72. } catch (SocketException ex) {
  73. // address incompatible with protocol
  74. int expectedError = 10047;
  75. Assert.AreEqual (expectedError, ex.ErrorCode,
  76. "#2");
  77. }
  78. }
  79. [Test]
  80. [Category ("InetAccess")]
  81. #if FEATURE_NO_BSD_SOCKETS
  82. [ExpectedException (typeof (PlatformNotSupportedException))]
  83. #endif
  84. public void BogusEndConnect ()
  85. {
  86. IPAddress ipOne = IPAddress.Parse (BogusAddress);
  87. IPEndPoint ipEP = new IPEndPoint (ipOne, BogusPort);
  88. Socket sock = new Socket (ipEP.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
  89. IAsyncResult ar = sock.BeginConnect (ipEP, null, null);
  90. try {
  91. // should raise an exception because connect was bogus
  92. sock.EndConnect (ar);
  93. Assert.Fail ("#1");
  94. } catch (SocketException ex) {
  95. // Actual error code depends on network configuration.
  96. var error = (SocketError) ex.ErrorCode;
  97. Assert.That (error == SocketError.TimedOut ||
  98. error == SocketError.ConnectionRefused ||
  99. error == SocketError.NetworkUnreachable ||
  100. error == SocketError.HostUnreachable, "#2");
  101. }
  102. }
  103. [Test]
  104. [ExpectedException (typeof (ArgumentNullException))]
  105. public void SelectEmpty ()
  106. {
  107. ArrayList list = new ArrayList ();
  108. Socket.Select (list, list, list, 1000);
  109. }
  110. private bool BlockingConnect (bool block, int port)
  111. {
  112. IPEndPoint ep = new IPEndPoint(IPAddress.Loopback, port);
  113. Socket server = new Socket(AddressFamily.InterNetwork,
  114. SocketType.Stream,
  115. ProtocolType.Tcp);
  116. server.Bind(ep);
  117. server.Blocking=block;
  118. server.Listen(0);
  119. Socket conn = new Socket (AddressFamily.InterNetwork,
  120. SocketType.Stream,
  121. ProtocolType.Tcp);
  122. conn.Connect (ep);
  123. Socket client = null;
  124. var sw = Stopwatch.StartNew ();
  125. while (sw.ElapsedMilliseconds < 100)
  126. {
  127. try {
  128. client = server.Accept();
  129. break;
  130. }
  131. catch (SocketException ex) {
  132. if (ex.SocketErrorCode == SocketError.WouldBlock)
  133. continue;
  134. throw;
  135. }
  136. }
  137. Assert.IsNotNull (client, "Couldn't accept a client connection within 100ms.");
  138. bool client_block = client.Blocking;
  139. client.Close();
  140. conn.Close();
  141. server.Close();
  142. return(client_block);
  143. }
  144. [Test]
  145. #if FEATURE_NO_BSD_SOCKETS
  146. [ExpectedException (typeof (PlatformNotSupportedException))]
  147. #endif
  148. public void AcceptBlockingStatus()
  149. {
  150. bool block;
  151. var port = NetworkHelpers.FindFreePort ();
  152. block = BlockingConnect(true, port);
  153. Assert.AreEqual (block, true, "BlockingStatus01");
  154. block = BlockingConnect(false, port);
  155. Assert.AreEqual (block, false, "BlockingStatus02");
  156. }
  157. static bool CFAConnected = false;
  158. static ManualResetEvent CFACalledBack;
  159. private static void CFACallback (IAsyncResult asyncResult)
  160. {
  161. Socket sock = (Socket)asyncResult.AsyncState;
  162. CFAConnected = sock.Connected;
  163. if (sock.Connected) {
  164. sock.EndConnect (asyncResult);
  165. }
  166. CFACalledBack.Set ();
  167. }
  168. [Test] // Connect (IPEndPoint)
  169. public void Connect1_RemoteEP_Null ()
  170. {
  171. Socket s = new Socket (AddressFamily.InterNetwork,
  172. SocketType.Stream, ProtocolType.Tcp);
  173. try {
  174. s.Connect ((IPEndPoint) null);
  175. Assert.Fail ("#1");
  176. } catch (ArgumentNullException ex) {
  177. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  178. Assert.IsNull (ex.InnerException, "#3");
  179. Assert.IsNotNull (ex.Message, "#4");
  180. Assert.AreEqual ("remoteEP", ex.ParamName, "#5");
  181. }
  182. }
  183. [Test]
  184. #if FEATURE_NO_BSD_SOCKETS
  185. [ExpectedException (typeof (PlatformNotSupportedException))]
  186. #endif
  187. public void ConnectFailAsync ()
  188. {
  189. Socket sock = new Socket (AddressFamily.InterNetwork,
  190. SocketType.Stream,
  191. ProtocolType.Tcp);
  192. sock.Blocking = false;
  193. CFACalledBack = new ManualResetEvent (false);
  194. CFACalledBack.Reset ();
  195. /* Need a port that is not being used for
  196. * anything...
  197. */
  198. sock.BeginConnect (new IPEndPoint (IPAddress.Loopback,
  199. NetworkHelpers.FindFreePort ()),
  200. new AsyncCallback (CFACallback),
  201. sock);
  202. CFACalledBack.WaitOne ();
  203. Assert.AreEqual (CFAConnected, false, "ConnectFail");
  204. }
  205. [Test]
  206. #if FEATURE_NO_BSD_SOCKETS
  207. [ExpectedException (typeof (PlatformNotSupportedException))]
  208. #endif
  209. public void SetSocketOptionBoolean ()
  210. {
  211. IPEndPoint ep = new IPEndPoint (IPAddress.Loopback, NetworkHelpers.FindFreePort ());
  212. Socket sock = new Socket (ep.Address.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
  213. try {
  214. sock.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.KeepAlive, true);
  215. } finally {
  216. sock.Close ();
  217. }
  218. }
  219. [Test]
  220. #if FEATURE_NO_BSD_SOCKETS
  221. [ExpectedException (typeof (PlatformNotSupportedException))]
  222. #endif
  223. public void TestSelect1 ()
  224. {
  225. Socket srv = CreateServer (NetworkHelpers.FindFreePort ());
  226. ClientSocket clnt = new ClientSocket (srv.LocalEndPoint);
  227. Thread th = new Thread (new ThreadStart (clnt.ConnectSleepClose));
  228. Socket acc = null;
  229. try {
  230. th.Start ();
  231. acc = srv.Accept ();
  232. clnt.Write ();
  233. ArrayList list = new ArrayList ();
  234. ArrayList empty = new ArrayList ();
  235. list.Add (acc);
  236. Socket.Select (list, empty, empty, 100);
  237. Assert.AreEqual (0, empty.Count, "#01");
  238. Assert.AreEqual (1, list.Count, "#02");
  239. Socket.Select (empty, list, empty, 100);
  240. Assert.AreEqual (0, empty.Count, "#03");
  241. Assert.AreEqual (1, list.Count, "#04");
  242. Socket.Select (list, empty, empty, -1);
  243. Assert.AreEqual (0, empty.Count, "#05");
  244. Assert.AreEqual (1, list.Count, "#06");
  245. // Need to read the 10 bytes from the client to avoid a RST
  246. byte [] bytes = new byte [10];
  247. acc.Receive (bytes);
  248. } finally {
  249. if (acc != null)
  250. acc.Close ();
  251. srv.Close ();
  252. }
  253. }
  254. static Socket CreateServer (int port)
  255. {
  256. Socket sock = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  257. sock.Bind (new IPEndPoint (IPAddress.Loopback, port));
  258. sock.Listen (1);
  259. return sock;
  260. }
  261. class ClientSocket {
  262. Socket sock;
  263. EndPoint ep;
  264. public ClientSocket (EndPoint ep)
  265. {
  266. this.ep = ep;
  267. sock = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  268. }
  269. public void ConnectSleepClose ()
  270. {
  271. sock.Connect (ep);
  272. Thread.Sleep (2000);
  273. sock.Close ();
  274. }
  275. public void Write ()
  276. {
  277. byte [] b = new byte [10];
  278. sock.Send (b);
  279. }
  280. }
  281. byte[] buf = new byte[100];
  282. [Test]
  283. [ExpectedException (typeof (ObjectDisposedException))]
  284. public void Disposed2 ()
  285. {
  286. Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
  287. s.Close();
  288. s.Blocking = true;
  289. }
  290. [Test]
  291. [ExpectedException (typeof (ObjectDisposedException))]
  292. public void Disposed6 ()
  293. {
  294. Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
  295. s.Close();
  296. s.Listen (5);
  297. }
  298. [Test]
  299. [ExpectedException (typeof (ObjectDisposedException))]
  300. public void Disposed7 ()
  301. {
  302. Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
  303. s.Close();
  304. s.Poll (100, 0);
  305. }
  306. [Test]
  307. [ExpectedException (typeof (ObjectDisposedException))]
  308. public void Disposed15 ()
  309. {
  310. Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
  311. s.Close();
  312. s.Send (buf);
  313. }
  314. [Test]
  315. [ExpectedException (typeof (ObjectDisposedException))]
  316. public void Disposed16 ()
  317. {
  318. Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
  319. s.Close();
  320. s.Send (buf, 0);
  321. }
  322. [Test]
  323. [ExpectedException (typeof (ObjectDisposedException))]
  324. public void Disposed17 ()
  325. {
  326. Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
  327. s.Close();
  328. s.Send (buf, 10, 0);
  329. }
  330. [Test]
  331. [ExpectedException (typeof (ObjectDisposedException))]
  332. public void Disposed18 ()
  333. {
  334. Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
  335. s.Close();
  336. s.Send (buf, 0, 10, 0);
  337. }
  338. [Test]
  339. #if FEATURE_NO_BSD_SOCKETS
  340. [ExpectedException (typeof (PlatformNotSupportedException))]
  341. #else
  342. [ExpectedException (typeof (ObjectDisposedException))]
  343. #endif
  344. public void Disposed19 ()
  345. {
  346. Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
  347. EndPoint ep = new IPEndPoint (IPAddress.Any, NetworkHelpers.FindFreePort ());
  348. s.Close();
  349. s.SendTo (buf, 0, ep);
  350. }
  351. [Test]
  352. #if FEATURE_NO_BSD_SOCKETS
  353. [ExpectedException (typeof (PlatformNotSupportedException))]
  354. #else
  355. [ExpectedException (typeof (ObjectDisposedException))]
  356. #endif
  357. public void Disposed20 ()
  358. {
  359. Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
  360. EndPoint ep = new IPEndPoint (IPAddress.Any, NetworkHelpers.FindFreePort ());
  361. s.Close();
  362. s.SendTo (buf, 10, 0, ep);
  363. }
  364. [Test]
  365. #if FEATURE_NO_BSD_SOCKETS
  366. [ExpectedException (typeof (PlatformNotSupportedException))]
  367. #else
  368. [ExpectedException (typeof (ObjectDisposedException))]
  369. #endif
  370. public void Disposed21 ()
  371. {
  372. Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
  373. EndPoint ep = new IPEndPoint (IPAddress.Any, NetworkHelpers.FindFreePort ());
  374. s.Close();
  375. s.SendTo (buf, 0, 10, 0, ep);
  376. }
  377. [Test]
  378. #if FEATURE_NO_BSD_SOCKETS
  379. [ExpectedException (typeof (PlatformNotSupportedException))]
  380. #else
  381. [ExpectedException (typeof (ObjectDisposedException))]
  382. #endif
  383. public void Disposed22 ()
  384. {
  385. Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
  386. EndPoint ep = new IPEndPoint (IPAddress.Any, NetworkHelpers.FindFreePort ());
  387. s.Close();
  388. s.SendTo (buf, ep);
  389. }
  390. [Test]
  391. [ExpectedException (typeof (ObjectDisposedException))]
  392. public void Disposed23 ()
  393. {
  394. Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
  395. s.Close();
  396. s.Shutdown (0);
  397. }
  398. [Test]
  399. #if FEATURE_NO_BSD_SOCKETS
  400. [ExpectedException (typeof (PlatformNotSupportedException))]
  401. #endif
  402. public void GetHashCodeTest ()
  403. {
  404. Socket server = new Socket (AddressFamily.InterNetwork,
  405. SocketType.Stream, ProtocolType.Tcp);
  406. IPEndPoint ep = new IPEndPoint (IPAddress.Loopback,
  407. NetworkHelpers.FindFreePort ());
  408. server.Bind (ep);
  409. server.Listen (1);
  410. Socket client = new Socket (AddressFamily.InterNetwork,
  411. SocketType.Stream, ProtocolType.Tcp);
  412. int hashcodeA = client.GetHashCode ();
  413. client.Connect (ep);
  414. int hashcodeB = client.GetHashCode ();
  415. Assert.AreEqual (hashcodeA, hashcodeB, "#1");
  416. client.Close ();
  417. int hashcodeC = client.GetHashCode ();
  418. Assert.AreEqual (hashcodeB, hashcodeC, "#2");
  419. server.Close ();
  420. }
  421. static ManualResetEvent SocketError_event = new ManualResetEvent (false);
  422. private static void SocketError_callback (IAsyncResult ar)
  423. {
  424. Socket sock = (Socket)ar.AsyncState;
  425. if(sock.Connected) {
  426. sock.EndConnect (ar);
  427. }
  428. SocketError_event.Set ();
  429. }
  430. [Test]
  431. [Category ("RequiresBSDSockets")] // This verifies particular error codes, which we don't care about when nothing's working anyway.
  432. public void SocketErrorTest ()
  433. {
  434. Socket sock = new Socket (AddressFamily.InterNetwork,
  435. SocketType.Stream,
  436. ProtocolType.Tcp);
  437. IPEndPoint ep = new IPEndPoint (IPAddress.Loopback,
  438. BogusPort);
  439. SocketError_event.Reset ();
  440. sock.Blocking = false;
  441. sock.BeginConnect (ep, new AsyncCallback(SocketError_callback),
  442. sock);
  443. if (SocketError_event.WaitOne (2000, false) == false) {
  444. Assert.Fail ("SocketError wait timed out");
  445. }
  446. Assert.AreEqual (false, sock.Connected, "SocketError #1");
  447. int error;
  448. error = (int)sock.GetSocketOption (SocketOptionLevel.Socket, SocketOptionName.Error);
  449. Assert.AreEqual (10061, error, "SocketError #2");
  450. error = (int)sock.GetSocketOption (SocketOptionLevel.Socket, SocketOptionName.Error);
  451. Assert.AreEqual (10061, error, "SocketError #3");
  452. sock.Close ();
  453. }
  454. [Test]
  455. public void SocketInformationCtor ()
  456. {
  457. }
  458. [Test]
  459. public void DontFragmentDefaultTcp ()
  460. {
  461. Socket sock = new Socket (AddressFamily.InterNetwork,
  462. SocketType.Stream,
  463. ProtocolType.Tcp);
  464. Assert.AreEqual (false, sock.DontFragment, "DontFragmentDefaultTcp");
  465. sock.Close ();
  466. }
  467. [Test]
  468. [Category ("NotWorking")] // DontFragment doesn't work
  469. public void DontFragmentChangeTcp ()
  470. {
  471. Socket sock = new Socket (AddressFamily.InterNetwork,
  472. SocketType.Stream,
  473. ProtocolType.Tcp);
  474. sock.DontFragment = true;
  475. Assert.AreEqual (true, sock.DontFragment, "DontFragmentChangeTcp");
  476. sock.Close ();
  477. }
  478. [Test]
  479. public void DontFragmentDefaultUdp ()
  480. {
  481. Socket sock = new Socket (AddressFamily.InterNetwork,
  482. SocketType.Dgram,
  483. ProtocolType.Udp);
  484. Assert.AreEqual (false, sock.DontFragment, "DontFragmentDefaultUdp");
  485. sock.Close ();
  486. }
  487. [Test]
  488. [Category ("NotWorking")] // DontFragment doesn't work
  489. public void DontFragmentChangeUdp ()
  490. {
  491. Socket sock = new Socket (AddressFamily.InterNetwork,
  492. SocketType.Dgram,
  493. ProtocolType.Udp);
  494. sock.DontFragment = true;
  495. Assert.AreEqual (true, sock.DontFragment, "DontFragmentChangeUdp");
  496. sock.Close ();
  497. }
  498. [Test]
  499. [ExpectedException (typeof(ObjectDisposedException))]
  500. public void DontFragmentClosed ()
  501. {
  502. Socket sock = new Socket (AddressFamily.InterNetwork,
  503. SocketType.Stream,
  504. ProtocolType.Tcp);
  505. sock.Close ();
  506. bool val = sock.DontFragment;
  507. }
  508. [Test]
  509. [Category ("NotWorking")] // Need to pick a non-IP AddressFamily that "works" on both mono and ms, this one only works on ms
  510. public void DontFragment ()
  511. {
  512. Socket sock = new Socket (AddressFamily.NetBios,
  513. SocketType.Seqpacket,
  514. ProtocolType.Unspecified);
  515. try {
  516. sock.DontFragment = true;
  517. Assert.Fail ("DontFragment #1");
  518. } catch (NotSupportedException) {
  519. } finally {
  520. sock.Close ();
  521. }
  522. }
  523. [Test]
  524. public void EnableBroadcastDefaultTcp ()
  525. {
  526. Socket sock = new Socket (AddressFamily.InterNetwork,
  527. SocketType.Stream,
  528. ProtocolType.Tcp);
  529. try {
  530. bool value = sock.EnableBroadcast;
  531. Assert.Fail ("EnableBroadcastDefaultTcp #1");
  532. } catch (SocketException ex) {
  533. Assert.AreEqual (10042, ex.ErrorCode, "EnableBroadcastDefaultTcp #2");
  534. } finally {
  535. sock.Close ();
  536. }
  537. }
  538. [Test]
  539. public void EnableBroadcastDefaultUdp ()
  540. {
  541. Socket sock = new Socket (AddressFamily.InterNetwork,
  542. SocketType.Dgram,
  543. ProtocolType.Udp);
  544. Assert.AreEqual (false, sock.EnableBroadcast, "EnableBroadcastDefaultUdp");
  545. sock.Close ();
  546. }
  547. [Test]
  548. public void EnableBroadcastChangeTcp ()
  549. {
  550. Socket sock = new Socket (AddressFamily.InterNetwork,
  551. SocketType.Stream,
  552. ProtocolType.Tcp);
  553. try {
  554. sock.EnableBroadcast = true;
  555. Assert.Fail ("EnableBroadcastChangeTcp #1");
  556. } catch (SocketException ex) {
  557. Assert.AreEqual (10042, ex.ErrorCode, "EnableBroadcastChangeTcp #2");
  558. } finally {
  559. sock.Close ();
  560. }
  561. }
  562. [Test]
  563. public void EnableBroadcastChangeUdp ()
  564. {
  565. Socket sock = new Socket (AddressFamily.InterNetwork,
  566. SocketType.Dgram,
  567. ProtocolType.Udp);
  568. sock.EnableBroadcast = true;
  569. Assert.AreEqual (true, sock.EnableBroadcast, "EnableBroadcastChangeUdp");
  570. sock.Close ();
  571. }
  572. [Test]
  573. [ExpectedException (typeof(ObjectDisposedException))]
  574. public void EnableBroadcastClosed ()
  575. {
  576. Socket sock = new Socket (AddressFamily.InterNetwork,
  577. SocketType.Dgram,
  578. ProtocolType.Udp);
  579. sock.Close ();
  580. bool val = sock.EnableBroadcast;
  581. }
  582. /* Can't test the default for ExclusiveAddressUse as
  583. * it's different on different versions and service
  584. * packs of windows
  585. */
  586. [Test]
  587. [Category ("NotWorking")] // Not supported on Linux
  588. public void ExclusiveAddressUseUnbound ()
  589. {
  590. Socket sock = new Socket (AddressFamily.InterNetwork,
  591. SocketType.Stream,
  592. ProtocolType.Tcp);
  593. sock.ExclusiveAddressUse = true;
  594. Assert.AreEqual (true, sock.ExclusiveAddressUse, "ExclusiveAddressUseUnbound");
  595. sock.Close ();
  596. }
  597. [Test]
  598. [ExpectedException (typeof(InvalidOperationException))]
  599. [Category ("NotWorking")] // Not supported on Linux
  600. public void ExclusiveAddressUseBound ()
  601. {
  602. Socket sock = new Socket (AddressFamily.InterNetwork,
  603. SocketType.Stream,
  604. ProtocolType.Tcp);
  605. sock.Bind (new IPEndPoint (IPAddress.Any, NetworkHelpers.FindFreePort ()));
  606. sock.ExclusiveAddressUse = true;
  607. sock.Close ();
  608. }
  609. [Test]
  610. [ExpectedException (typeof(ObjectDisposedException))]
  611. public void ExclusiveAddressUseClosed ()
  612. {
  613. Socket sock = new Socket (AddressFamily.InterNetwork,
  614. SocketType.Stream,
  615. ProtocolType.Tcp);
  616. sock.Close ();
  617. bool val = sock.ExclusiveAddressUse;
  618. }
  619. [Test]
  620. #if FEATURE_NO_BSD_SOCKETS
  621. [ExpectedException (typeof (PlatformNotSupportedException))]
  622. #endif
  623. public void IsBoundTcp ()
  624. {
  625. Socket sock = new Socket (AddressFamily.InterNetwork,
  626. SocketType.Stream,
  627. ProtocolType.Tcp);
  628. IPEndPoint ep = new IPEndPoint (IPAddress.Loopback,
  629. BogusPort);
  630. Assert.AreEqual (false, sock.IsBound, "IsBoundTcp #1");
  631. sock.Bind (ep);
  632. Assert.AreEqual (true, sock.IsBound, "IsBoundTcp #2");
  633. sock.Listen (1);
  634. Socket sock2 = new Socket (AddressFamily.InterNetwork,
  635. SocketType.Stream,
  636. ProtocolType.Tcp);
  637. Assert.AreEqual (false, sock2.IsBound, "IsBoundTcp #3");
  638. sock2.Connect (ep);
  639. Assert.AreEqual (true, sock2.IsBound, "IsBoundTcp #4");
  640. sock2.Close ();
  641. Assert.AreEqual (true, sock2.IsBound, "IsBoundTcp #5");
  642. sock.Close ();
  643. Assert.AreEqual (true, sock.IsBound, "IsBoundTcp #6");
  644. }
  645. [Test]
  646. #if FEATURE_NO_BSD_SOCKETS
  647. [ExpectedException (typeof (PlatformNotSupportedException))]
  648. #endif
  649. public void IsBoundUdp ()
  650. {
  651. Socket sock = new Socket (AddressFamily.InterNetwork,
  652. SocketType.Dgram,
  653. ProtocolType.Udp);
  654. IPEndPoint ep = new IPEndPoint (IPAddress.Loopback,
  655. BogusPort);
  656. Assert.AreEqual (false, sock.IsBound, "IsBoundUdp #1");
  657. sock.Bind (ep);
  658. Assert.AreEqual (true, sock.IsBound, "IsBoundUdp #2");
  659. sock.Close ();
  660. Assert.AreEqual (true, sock.IsBound, "IsBoundUdp #3");
  661. sock = new Socket (AddressFamily.InterNetwork,
  662. SocketType.Dgram,
  663. ProtocolType.Udp);
  664. Assert.AreEqual (false, sock.IsBound, "IsBoundUdp #4");
  665. sock.Connect (ep);
  666. Assert.AreEqual (true, sock.IsBound, "IsBoundUdp #5");
  667. sock.Close ();
  668. Assert.AreEqual (true, sock.IsBound, "IsBoundUdp #6");
  669. }
  670. [Test]
  671. /* Should not throw an exception */
  672. public void IsBoundClosed ()
  673. {
  674. Socket sock = new Socket (AddressFamily.InterNetwork,
  675. SocketType.Stream,
  676. ProtocolType.Tcp);
  677. sock.Close ();
  678. bool val = sock.IsBound;
  679. }
  680. /* Nothing much to test for LingerState */
  681. [Test]
  682. public void MulticastLoopbackDefaultTcp ()
  683. {
  684. Socket sock = new Socket (AddressFamily.InterNetwork,
  685. SocketType.Stream,
  686. ProtocolType.Tcp);
  687. try {
  688. bool value = sock.MulticastLoopback;
  689. Assert.Fail ("MulticastLoopbackDefaultTcp #1");
  690. } catch (SocketException ex) {
  691. Assert.AreEqual (10042, ex.ErrorCode, "MulticastLoopbackDefaultTcp #2");
  692. } finally {
  693. sock.Close ();
  694. }
  695. }
  696. [Test]
  697. public void MulticastLoopbackChangeTcp ()
  698. {
  699. Socket sock = new Socket (AddressFamily.InterNetwork,
  700. SocketType.Stream,
  701. ProtocolType.Tcp);
  702. try {
  703. sock.MulticastLoopback = false;
  704. Assert.Fail ("MulticastLoopbackChangeTcp #1");
  705. } catch (SocketException ex) {
  706. Assert.AreEqual (10042, ex.ErrorCode, "MulticastLoopbackChangeTcp #2");
  707. } finally {
  708. sock.Close ();
  709. }
  710. }
  711. [Test]
  712. public void MulticastLoopbackDefaultUdp ()
  713. {
  714. Socket sock = new Socket (AddressFamily.InterNetwork,
  715. SocketType.Dgram,
  716. ProtocolType.Udp);
  717. Assert.AreEqual (true, sock.MulticastLoopback, "MulticastLoopbackDefaultUdp");
  718. sock.Close ();
  719. }
  720. [Test]
  721. public void MulticastLoopbackChangeUdp ()
  722. {
  723. Socket sock = new Socket (AddressFamily.InterNetwork,
  724. SocketType.Dgram,
  725. ProtocolType.Udp);
  726. sock.MulticastLoopback = false;
  727. Assert.AreEqual (false, sock.MulticastLoopback, "MulticastLoopbackChangeUdp");
  728. sock.Close ();
  729. }
  730. [Test]
  731. [ExpectedException (typeof(ObjectDisposedException))]
  732. public void MulticastLoopbackClosed ()
  733. {
  734. Socket sock = new Socket (AddressFamily.InterNetwork,
  735. SocketType.Stream,
  736. ProtocolType.Tcp);
  737. sock.Close ();
  738. bool val = sock.MulticastLoopback;
  739. }
  740. /* OSSupportsIPv6 depends on the environment */
  741. [Test]
  742. [Category("NotWorking")] // We have different defaults for perf reasons
  743. public void ReceiveBufferSizeDefault ()
  744. {
  745. Socket sock = new Socket (AddressFamily.InterNetwork,
  746. SocketType.Stream,
  747. ProtocolType.Tcp);
  748. Assert.AreEqual (8192, sock.ReceiveBufferSize, "ReceiveBufferSizeDefault");
  749. sock.Close ();
  750. }
  751. [Test]
  752. [Category("NotWorking")] // We have different defaults for perf reasons
  753. public void ReceiveBufferSizeDefaultUdp ()
  754. {
  755. Socket sock = new Socket (AddressFamily.InterNetwork,
  756. SocketType.Dgram,
  757. ProtocolType.Udp);
  758. Assert.AreEqual (8192, sock.ReceiveBufferSize, "ReceiveBufferSizeDefaultUdp");
  759. sock.Close ();
  760. }
  761. [Test]
  762. public void ReceiveBufferSizeChange ()
  763. {
  764. Socket sock = new Socket (AddressFamily.InterNetwork,
  765. SocketType.Stream,
  766. ProtocolType.Tcp);
  767. sock.ReceiveBufferSize = 16384;
  768. Assert.AreEqual (16384, sock.ReceiveBufferSize, "ReceiveBufferSizeChange");
  769. sock.Close ();
  770. }
  771. [Test]
  772. [Category("NotWorking")] // We cannot totally remove buffers (minimum is set to 256
  773. public void BuffersCheck_None ()
  774. {
  775. using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) {
  776. int original = s.ReceiveBufferSize;
  777. s.ReceiveBufferSize = 0;
  778. Assert.AreEqual (0, s.ReceiveBufferSize, "ReceiveBufferSize " + original.ToString ());
  779. original = s.SendBufferSize;
  780. s.SendBufferSize = 0;
  781. Assert.AreEqual (0, s.SendBufferSize, "SendBufferSize " + original.ToString ());
  782. }
  783. }
  784. [Test]
  785. [ExpectedException (typeof(ObjectDisposedException))]
  786. public void ReceiveBufferSizeClosed ()
  787. {
  788. Socket sock = new Socket (AddressFamily.InterNetwork,
  789. SocketType.Stream,
  790. ProtocolType.Tcp);
  791. sock.Close ();
  792. int val = sock.ReceiveBufferSize;
  793. }
  794. [Test]
  795. [Category("NotWorking")] // We have different defaults for perf reasons
  796. public void SendBufferSizeDefault ()
  797. {
  798. Socket sock = new Socket (AddressFamily.InterNetwork,
  799. SocketType.Stream,
  800. ProtocolType.Tcp);
  801. Assert.AreEqual (8192, sock.SendBufferSize, "SendBufferSizeDefault");
  802. sock.Close ();
  803. }
  804. [Test]
  805. [Category("NotWorking")] // We have different defaults for perf reasons
  806. public void SendBufferSizeDefaultUdp ()
  807. {
  808. Socket sock = new Socket (AddressFamily.InterNetwork,
  809. SocketType.Dgram,
  810. ProtocolType.Udp);
  811. Assert.AreEqual (8192, sock.SendBufferSize, "SendBufferSizeDefaultUdp");
  812. sock.Close ();
  813. }
  814. [Test]
  815. public void SendBufferSizeChange ()
  816. {
  817. Socket sock = new Socket (AddressFamily.InterNetwork,
  818. SocketType.Stream,
  819. ProtocolType.Tcp);
  820. sock.SendBufferSize = 16384;
  821. Assert.AreEqual (16384, sock.SendBufferSize, "SendBufferSizeChange");
  822. sock.Close ();
  823. }
  824. [Test]
  825. [ExpectedException (typeof(ObjectDisposedException))]
  826. public void SendBufferSizeClosed ()
  827. {
  828. Socket sock = new Socket (AddressFamily.InterNetwork,
  829. SocketType.Stream,
  830. ProtocolType.Tcp);
  831. sock.Close ();
  832. int val = sock.SendBufferSize;
  833. }
  834. /* No test for TTL default as it's platform dependent */
  835. [Test]
  836. public void TtlChange ()
  837. {
  838. Socket sock = new Socket (AddressFamily.InterNetwork,
  839. SocketType.Stream,
  840. ProtocolType.Tcp);
  841. sock.Ttl = 255;
  842. Assert.AreEqual (255, sock.Ttl, "TtlChange");
  843. sock.Close ();
  844. }
  845. [Test]
  846. public void TtlChangeOverflow ()
  847. {
  848. Socket sock = new Socket (AddressFamily.InterNetwork,
  849. SocketType.Stream,
  850. ProtocolType.Tcp);
  851. try {
  852. sock.Ttl = 256;
  853. Assert.Fail ("TtlChangeOverflow #1");
  854. } catch (ArgumentOutOfRangeException ex) {
  855. Assert.AreEqual ("value", ex.ParamName,
  856. "TtlChangeOverflow #2");
  857. } finally {
  858. sock.Close ();
  859. }
  860. }
  861. /* Apparently you can set TTL=0 on the ms runtime!!
  862. try {
  863. sock.Ttl = 0;
  864. Assert.Fail ("TtlChangeOverflow #4");
  865. } catch (SocketException ex) {
  866. Assert.AreEqual (10022, ex.ErrorCode,
  867. "TtlChangeOverflow #5");
  868. } finally {
  869. sock.Close ();
  870. }
  871. */
  872. [Test]
  873. [ExpectedException (typeof(ObjectDisposedException))]
  874. public void TtlClosed ()
  875. {
  876. Socket sock = new Socket (AddressFamily.InterNetwork,
  877. SocketType.Stream,
  878. ProtocolType.Tcp);
  879. sock.Close ();
  880. int val = sock.Ttl;
  881. }
  882. [Test]
  883. public void UseOnlyOverlappedIODefault ()
  884. {
  885. Socket sock = new Socket (AddressFamily.InterNetwork,
  886. SocketType.Stream,
  887. ProtocolType.Tcp);
  888. Assert.AreEqual (false, sock.UseOnlyOverlappedIO, "UseOnlyOverlappedIODefault");
  889. sock.Close ();
  890. }
  891. //
  892. // We need this because the Linux kernel in certain configurations
  893. // will end up rounding up the values passed on to the kernel
  894. // for socket send/receive timeouts.
  895. //
  896. int Approximate (int target, int value)
  897. {
  898. int epsilon = 10;
  899. if (value > target-10 && value < target+10)
  900. return target;
  901. return value;
  902. }
  903. [Test]
  904. public void UseOnlyOverlappedIOChange ()
  905. {
  906. Socket sock = new Socket (AddressFamily.InterNetwork,
  907. SocketType.Stream,
  908. ProtocolType.Tcp);
  909. sock.UseOnlyOverlappedIO = true;
  910. Assert.AreEqual (true, sock.UseOnlyOverlappedIO, "UseOnlyOverlappedIOChange");
  911. sock.Close ();
  912. }
  913. [Test]
  914. /* Should not throw an exception */
  915. public void UseOnlyOverlappedIOClosed ()
  916. {
  917. Socket sock = new Socket (AddressFamily.InterNetwork,
  918. SocketType.Stream,
  919. ProtocolType.Tcp);
  920. sock.Close ();
  921. bool val = sock.UseOnlyOverlappedIO;
  922. }
  923. [Test]
  924. public void SendTimeoutDefault ()
  925. {
  926. Socket sock = new Socket (AddressFamily.InterNetwork,
  927. SocketType.Stream,
  928. ProtocolType.Tcp);
  929. Assert.AreEqual (0, sock.SendTimeout, "SendTimeoutDefault");
  930. sock.Close ();
  931. }
  932. [Test]
  933. public void SendTimeoutChange ()
  934. {
  935. Socket sock = new Socket (AddressFamily.InterNetwork,
  936. SocketType.Stream,
  937. ProtocolType.Tcp);
  938. /* Should be rounded up to 500, according to
  939. * the MSDN docs, but the MS runtime doesn't
  940. */
  941. sock.SendTimeout = 50;
  942. Assert.AreEqual (50, Approximate (50, sock.SendTimeout), "SendTimeoutChange #1");
  943. sock.SendTimeout = 2000;
  944. Assert.AreEqual (2000, Approximate (2000, sock.SendTimeout), "SendTimeoutChange #2");
  945. sock.SendTimeout = 0;
  946. Assert.AreEqual (0, Approximate (0, sock.SendTimeout), "SendTimeoutChange #3");
  947. /* Should be the same as setting 0 */
  948. sock.SendTimeout = -1;
  949. Assert.AreEqual (0, sock.SendTimeout, "SendTimeoutChange #4");
  950. sock.SendTimeout = 65536;
  951. Assert.AreEqual (65536, Approximate (65536, sock.SendTimeout), "SendTimeoutChange #5");
  952. try {
  953. sock.SendTimeout = -2;
  954. Assert.Fail ("SendTimeoutChange #8");
  955. } catch (ArgumentOutOfRangeException) {
  956. } finally {
  957. sock.Close ();
  958. }
  959. }
  960. [Test]
  961. [ExpectedException (typeof(ObjectDisposedException))]
  962. public void SendTimeoutClosed ()
  963. {
  964. Socket sock = new Socket (AddressFamily.InterNetwork,
  965. SocketType.Stream,
  966. ProtocolType.Tcp);
  967. sock.Close ();
  968. int val = sock.SendTimeout;
  969. }
  970. [Test]
  971. public void ReceiveTimeoutDefault ()
  972. {
  973. Socket sock = new Socket (AddressFamily.InterNetwork,
  974. SocketType.Stream,
  975. ProtocolType.Tcp);
  976. Assert.AreEqual (0, sock.ReceiveTimeout, "ReceiveTimeoutDefault");
  977. sock.Close ();
  978. }
  979. [Test]
  980. public void ReceiveTimeoutChange ()
  981. {
  982. Socket sock = new Socket (AddressFamily.InterNetwork,
  983. SocketType.Stream,
  984. ProtocolType.Tcp);
  985. sock.ReceiveTimeout = 50;
  986. Assert.AreEqual (50, Approximate (50, sock.ReceiveTimeout), "ReceiveTimeoutChange #1");
  987. sock.ReceiveTimeout = 2000;
  988. Assert.AreEqual (2000, Approximate (2000, sock.ReceiveTimeout), "ReceiveTimeoutChange #2");
  989. sock.ReceiveTimeout = 0;
  990. Assert.AreEqual (0, sock.ReceiveTimeout, "ReceiveTimeoutChange #3");
  991. /* Should be the same as setting 0 */
  992. sock.ReceiveTimeout = -1;
  993. Assert.AreEqual (0, sock.ReceiveTimeout, "ReceiveTimeoutChange #4");
  994. sock.ReceiveTimeout = 65536;
  995. Assert.AreEqual (65536, Approximate (65536, sock.ReceiveTimeout), "ReceiveTimeoutChange #5");
  996. try {
  997. sock.ReceiveTimeout = -2;
  998. Assert.Fail ("ReceiveTimeoutChange #8");
  999. } catch (ArgumentOutOfRangeException) {
  1000. } finally {
  1001. sock.Close ();
  1002. }
  1003. }
  1004. [Test]
  1005. [ExpectedException (typeof(ObjectDisposedException))]
  1006. public void ReceiveTimeoutClosed ()
  1007. {
  1008. Socket sock = new Socket (AddressFamily.InterNetwork,
  1009. SocketType.Stream,
  1010. ProtocolType.Tcp);
  1011. sock.Close ();
  1012. int val = sock.ReceiveTimeout;
  1013. }
  1014. [Test]
  1015. public void NoDelayDefaultTcp ()
  1016. {
  1017. Socket sock = new Socket (AddressFamily.InterNetwork,
  1018. SocketType.Stream,
  1019. ProtocolType.Tcp);
  1020. Assert.AreEqual (false, sock.NoDelay, "NoDelayDefaultTcp");
  1021. sock.Close ();
  1022. }
  1023. [Test]
  1024. public void NoDelayChangeTcp ()
  1025. {
  1026. Socket sock = new Socket (AddressFamily.InterNetwork,
  1027. SocketType.Stream,
  1028. ProtocolType.Tcp);
  1029. sock.NoDelay = true;
  1030. Assert.AreEqual (true, sock.NoDelay, "NoDelayChangeTcp");
  1031. sock.Close ();
  1032. }
  1033. [Test]
  1034. public void NoDelayDefaultUdp ()
  1035. {
  1036. Socket sock = new Socket (AddressFamily.InterNetwork,
  1037. SocketType.Dgram,
  1038. ProtocolType.Udp);
  1039. try {
  1040. bool val = sock.NoDelay;
  1041. Assert.Fail ("NoDelayDefaultUdp #1");
  1042. } catch (SocketException ex) {
  1043. Assert.AreEqual (10042, ex.ErrorCode,
  1044. "NoDelayDefaultUdp #2");
  1045. } finally {
  1046. sock.Close ();
  1047. }
  1048. }
  1049. [Test]
  1050. public void NoDelayChangeUdp ()
  1051. {
  1052. Socket sock = new Socket (AddressFamily.InterNetwork,
  1053. SocketType.Dgram,
  1054. ProtocolType.Udp);
  1055. try {
  1056. sock.NoDelay = true;
  1057. Assert.Fail ("NoDelayChangeUdp #1");
  1058. } catch (SocketException ex) {
  1059. Assert.AreEqual (10042, ex.ErrorCode,
  1060. "NoDelayChangeUdp #2");
  1061. } finally {
  1062. sock.Close ();
  1063. }
  1064. }
  1065. [Test]
  1066. [ExpectedException (typeof(ObjectDisposedException))]
  1067. public void NoDelayClosed ()
  1068. {
  1069. Socket sock = new Socket (AddressFamily.InterNetwork,
  1070. SocketType.Stream,
  1071. ProtocolType.Tcp);
  1072. sock.Close ();
  1073. bool val = sock.NoDelay;
  1074. }
  1075. static bool BAAccepted = false;
  1076. static Socket BASocket = null;
  1077. static ManualResetEvent BACalledBack = new ManualResetEvent (false);
  1078. private static void BACallback (IAsyncResult asyncResult)
  1079. {
  1080. Socket sock = (Socket)asyncResult.AsyncState;
  1081. BASocket = sock.EndAccept (asyncResult);
  1082. BAAccepted = true;
  1083. BACalledBack.Set ();
  1084. }
  1085. [Test]
  1086. [ExpectedException (typeof(InvalidOperationException))]
  1087. public void BeginAcceptNotBound ()
  1088. {
  1089. Socket sock = new Socket (AddressFamily.InterNetwork,
  1090. SocketType.Stream,
  1091. ProtocolType.Tcp);
  1092. sock.BeginAccept (BACallback, sock);
  1093. sock.Close ();
  1094. }
  1095. [Test]
  1096. #if FEATURE_NO_BSD_SOCKETS
  1097. [ExpectedException (typeof (PlatformNotSupportedException))]
  1098. #else
  1099. [ExpectedException (typeof(InvalidOperationException))]
  1100. #endif
  1101. public void BeginAcceptNotListening ()
  1102. {
  1103. Socket sock = new Socket (AddressFamily.InterNetwork,
  1104. SocketType.Stream,
  1105. ProtocolType.Tcp);
  1106. sock.Bind (new IPEndPoint (IPAddress.Any, NetworkHelpers.FindFreePort ()));
  1107. sock.BeginAccept (BACallback, sock);
  1108. sock.Close ();
  1109. }
  1110. [Test]
  1111. #if FEATURE_NO_BSD_SOCKETS
  1112. [ExpectedException (typeof (PlatformNotSupportedException))]
  1113. #endif
  1114. public void BeginAccept ()
  1115. {
  1116. Socket sock = new Socket (AddressFamily.InterNetwork,
  1117. SocketType.Stream,
  1118. ProtocolType.Tcp);
  1119. IPEndPoint ep = new IPEndPoint (IPAddress.Loopback,
  1120. NetworkHelpers.FindFreePort ());
  1121. sock.Bind (ep);
  1122. sock.Listen (1);
  1123. BACalledBack.Reset ();
  1124. sock.BeginAccept (BACallback, sock);
  1125. Socket conn = new Socket (AddressFamily.InterNetwork,
  1126. SocketType.Stream,
  1127. ProtocolType.Tcp);
  1128. conn.Connect (ep);
  1129. if (BACalledBack.WaitOne (2000, false) == false) {
  1130. Assert.Fail ("BeginAccept wait timed out");
  1131. }
  1132. Assert.AreEqual (true, BAAccepted, "BeginAccept #1");
  1133. Assert.AreEqual (true, BASocket.Connected, "BeginAccept #2");
  1134. Assert.AreEqual (false, sock.Connected, "BeginAccept #3");
  1135. Assert.AreEqual (true, conn.Connected, "BeginAccept #4");
  1136. BASocket.Close ();
  1137. conn.Close ();
  1138. sock.Close ();
  1139. }
  1140. [Test]
  1141. [ExpectedException (typeof(ObjectDisposedException))]
  1142. public void BeginAcceptClosed ()
  1143. {
  1144. Socket sock = new Socket (AddressFamily.InterNetwork,
  1145. SocketType.Stream,
  1146. ProtocolType.Tcp);
  1147. sock.Close ();
  1148. sock.BeginAccept (BACallback, sock);
  1149. }
  1150. static bool BADAccepted = false;
  1151. static Socket BADSocket = null;
  1152. static byte[] BADBytes;
  1153. static int BADByteCount;
  1154. static ManualResetEvent BADCalledBack = new ManualResetEvent (false);
  1155. private static void BADCallback (IAsyncResult asyncResult)
  1156. {
  1157. Socket sock = (Socket)asyncResult.AsyncState;
  1158. BADSocket = sock.EndAccept (out BADBytes,
  1159. out BADByteCount,
  1160. asyncResult);
  1161. BADAccepted = true;
  1162. BADCalledBack.Set ();
  1163. }
  1164. [Test]
  1165. #if FEATURE_NO_BSD_SOCKETS
  1166. [ExpectedException (typeof (PlatformNotSupportedException))]
  1167. #endif
  1168. public void BeginAcceptData ()
  1169. {
  1170. Socket sock = new Socket (AddressFamily.InterNetwork,
  1171. SocketType.Stream,
  1172. ProtocolType.Tcp);
  1173. IPEndPoint ep = new IPEndPoint (IPAddress.Loopback,
  1174. NetworkHelpers.FindFreePort ());
  1175. sock.Bind (ep);
  1176. sock.Listen (1);
  1177. BADCalledBack.Reset ();
  1178. sock.BeginAccept (256, BADCallback, sock);
  1179. Socket conn = new Socket (AddressFamily.InterNetwork,
  1180. SocketType.Stream,
  1181. ProtocolType.Tcp);
  1182. byte[] send_bytes = new byte[] {10, 11, 12, 13};
  1183. conn.Connect (ep);
  1184. conn.Send (send_bytes);
  1185. if (BADCalledBack.WaitOne (2000, false) == false) {
  1186. Assert.Fail ("BeginAcceptData wait timed out");
  1187. }
  1188. Assert.AreEqual (true, BADAccepted, "BeginAcceptData #1");
  1189. Assert.AreEqual (true, BADSocket.Connected, "BeginAcceptData #2");
  1190. Assert.AreEqual (false, sock.Connected, "BeginAcceptData #3");
  1191. Assert.AreEqual (true, conn.Connected, "BeginAcceptData #4");
  1192. Assert.AreEqual (send_bytes.Length, BADByteCount, "BeginAcceptData #5");
  1193. /* The MS runtime gives the returned data in a
  1194. * much bigger array. TODO: investigate
  1195. * whether it the size correlates to the first
  1196. * parameter in BeginAccept()
  1197. */
  1198. Assert.IsFalse (BADBytes.Length == send_bytes.Length,
  1199. "BeginAcceptData #6");
  1200. for(int i = 0; i < send_bytes.Length; i++) {
  1201. Assert.AreEqual (send_bytes[i], BADBytes[i], "BeginAcceptData #" + (i+7).ToString ());
  1202. }
  1203. BADSocket.Close ();
  1204. conn.Close ();
  1205. sock.Close ();
  1206. }
  1207. [Test]
  1208. [ExpectedException (typeof(ObjectDisposedException))]
  1209. public void BeginAcceptDataClosed ()
  1210. {
  1211. Socket sock = new Socket (AddressFamily.InterNetwork,
  1212. SocketType.Stream,
  1213. ProtocolType.Tcp);
  1214. sock.Close ();
  1215. sock.BeginAccept (256, BADCallback, sock);
  1216. }
  1217. [Test]
  1218. #if FEATURE_NO_BSD_SOCKETS
  1219. [ExpectedException (typeof (PlatformNotSupportedException))]
  1220. #endif
  1221. public void BeginAcceptSocketUdp ()
  1222. {
  1223. Socket sock = new Socket (AddressFamily.InterNetwork,
  1224. SocketType.Stream,
  1225. ProtocolType.Tcp);
  1226. Socket acc = new Socket (AddressFamily.InterNetwork,
  1227. SocketType.Dgram,
  1228. ProtocolType.Udp);
  1229. IPEndPoint ep = new IPEndPoint (IPAddress.Loopback,
  1230. NetworkHelpers.FindFreePort ());
  1231. sock.Bind (ep);
  1232. sock.Listen (1);
  1233. try {
  1234. sock.BeginAccept (acc, 256, BADCallback, sock);
  1235. Assert.Fail ("BeginAcceptSocketUdp #1");
  1236. } catch (SocketException ex) {
  1237. Assert.AreEqual (10022, ex.ErrorCode, "BeginAcceptSocketUdp #2");
  1238. } finally {
  1239. acc.Close ();
  1240. sock.Close ();
  1241. }
  1242. }
  1243. [Test]
  1244. #if FEATURE_NO_BSD_SOCKETS
  1245. [ExpectedException (typeof (PlatformNotSupportedException))]
  1246. #endif
  1247. public void BeginAcceptSocketBound ()
  1248. {
  1249. Socket sock = new Socket (AddressFamily.InterNetwork,
  1250. SocketType.Stream,
  1251. ProtocolType.Tcp);
  1252. Socket acc = new Socket (AddressFamily.InterNetwork,
  1253. SocketType.Stream,
  1254. ProtocolType.Tcp);
  1255. IPEndPoint ep1 = new IPEndPoint (IPAddress.Loopback,
  1256. NetworkHelpers.FindFreePort ());
  1257. IPEndPoint ep2 = new IPEndPoint (IPAddress.Loopback,
  1258. NetworkHelpers.FindFreePort ());
  1259. sock.Bind (ep1);
  1260. sock.Listen (1);
  1261. acc.Bind (ep2);
  1262. try {
  1263. sock.BeginAccept (acc, 256, BADCallback, sock);
  1264. Assert.Fail ("BeginAcceptSocketBound #1");
  1265. } catch (InvalidOperationException) {
  1266. } finally {
  1267. acc.Close ();
  1268. sock.Close ();
  1269. }
  1270. }
  1271. [Test]
  1272. #if FEATURE_NO_BSD_SOCKETS
  1273. [ExpectedException (typeof (PlatformNotSupportedException))]
  1274. #endif
  1275. public void BeginAcceptSocket ()
  1276. {
  1277. Socket sock = new Socket (AddressFamily.InterNetwork,
  1278. SocketType.Stream,
  1279. ProtocolType.Tcp);
  1280. Socket acc = new Socket (AddressFamily.InterNetwork,
  1281. SocketType.Stream,
  1282. ProtocolType.Tcp);
  1283. IPEndPoint ep = new IPEndPoint (IPAddress.Loopback,
  1284. NetworkHelpers.FindFreePort ());
  1285. sock.Bind (ep);
  1286. sock.Listen (1);
  1287. BADCalledBack.Reset ();
  1288. sock.BeginAccept (acc, 256, BADCallback, sock);
  1289. Socket conn = new Socket (AddressFamily.InterNetwork,
  1290. SocketType.Stream,
  1291. ProtocolType.Tcp);
  1292. byte[] send_bytes = new byte[] {10, 11, 12, 13};
  1293. conn.Connect (ep);
  1294. conn.Send (send_bytes);
  1295. if (BADCalledBack.WaitOne (2000, false) == false) {
  1296. Assert.Fail ("BeginAcceptSocket wait timed out");
  1297. }
  1298. Assert.AreEqual (true, BADAccepted, "BeginAcceptSocket #1");
  1299. Assert.AreEqual (true, BADSocket.Connected, "BeginAcceptSocket #2");
  1300. Assert.AreEqual (false, sock.Connected, "BeginAcceptSocket #3");
  1301. Assert.AreEqual (true, conn.Connected, "BeginAcceptSocket #4");
  1302. Assert.AreEqual (send_bytes.Length, BADByteCount, "BeginAcceptSocket #5");
  1303. Assert.AreEqual (AddressFamily.InterNetwork, acc.AddressFamily, "BeginAcceptSocket #6");
  1304. Assert.AreEqual (SocketType.Stream, acc.SocketType, "BeginAcceptSocket #7");
  1305. Assert.AreEqual (ProtocolType.Tcp, acc.ProtocolType, "BeginAcceptSocket #8");
  1306. Assert.AreEqual (conn.LocalEndPoint, acc.RemoteEndPoint, "BeginAcceptSocket #9");
  1307. /* The MS runtime gives the returned data in a
  1308. * much bigger array. TODO: investigate
  1309. * whether it the size correlates to the first
  1310. * parameter in BeginAccept()
  1311. */
  1312. Assert.IsFalse (BADBytes.Length == send_bytes.Length,
  1313. "BeginAcceptSocket #10");
  1314. for(int i = 0; i < send_bytes.Length; i++) {
  1315. Assert.AreEqual (send_bytes[i], BADBytes[i], "BeginAcceptSocket #" + (i+11).ToString ());
  1316. }
  1317. BADSocket.Close ();
  1318. conn.Close ();
  1319. acc.Close ();
  1320. sock.Close ();
  1321. }
  1322. [Test]
  1323. public void BeginAcceptSocketClosed ()
  1324. {
  1325. Socket sock = new Socket (AddressFamily.InterNetwork,
  1326. SocketType.Stream,
  1327. ProtocolType.Tcp);
  1328. Socket acc = new Socket (AddressFamily.InterNetwork,
  1329. SocketType.Stream,
  1330. ProtocolType.Tcp);
  1331. sock.Close ();
  1332. try {
  1333. sock.BeginAccept (acc, 256, BADCallback, null);
  1334. Assert.Fail ("BeginAcceptSocketClosed #1");
  1335. } catch (ObjectDisposedException) {
  1336. } finally {
  1337. acc.Close ();
  1338. }
  1339. }
  1340. [Test]
  1341. #if FEATURE_NO_BSD_SOCKETS
  1342. [ExpectedException (typeof (PlatformNotSupportedException))]
  1343. #endif
  1344. public void BeginAcceptSocketAccClosed ()
  1345. {
  1346. Socket sock = new Socket (AddressFamily.InterNetwork,
  1347. SocketType.Stream,
  1348. ProtocolType.Tcp);
  1349. Socket acc = new Socket (AddressFamily.InterNetwork,
  1350. SocketType.Stream,
  1351. ProtocolType.Tcp);
  1352. IPEndPoint ep = new IPEndPoint (IPAddress.Loopback,
  1353. NetworkHelpers.FindFreePort ());
  1354. sock.Bind (ep);
  1355. sock.Listen (1);
  1356. acc.Close ();
  1357. BADCalledBack.Reset ();
  1358. try {
  1359. sock.BeginAccept (acc, 256, BADCallback, null);
  1360. Assert.Fail ("BeginAcceptSocketAccClosed #1");
  1361. } catch (ObjectDisposedException) {
  1362. } finally {
  1363. sock.Close ();
  1364. }
  1365. }
  1366. static bool BCConnected = false;
  1367. static ManualResetEvent BCCalledBack = new ManualResetEvent (false);
  1368. private static void BCCallback (IAsyncResult asyncResult)
  1369. {
  1370. Socket sock = (Socket)asyncResult.AsyncState;
  1371. try {
  1372. sock.EndConnect (asyncResult);
  1373. } catch (Exception e) {
  1374. Console.WriteLine ("BCCallback exception:");
  1375. Console.WriteLine (e);
  1376. throw;
  1377. }
  1378. BCConnected = true;
  1379. BCCalledBack.Set ();
  1380. }
  1381. [Test]
  1382. #if FEATURE_NO_BSD_SOCKETS
  1383. [ExpectedException (typeof (PlatformNotSupportedException))]
  1384. #endif
  1385. public void BeginConnectAddressPort ()
  1386. {
  1387. Socket sock = new Socket (AddressFamily.InterNetwork,
  1388. SocketType.Stream,
  1389. ProtocolType.Tcp);
  1390. Socket listen = new Socket (AddressFamily.InterNetwork,
  1391. SocketType.Stream,
  1392. ProtocolType.Tcp);
  1393. IPAddress ip = IPAddress.Loopback;
  1394. IPEndPoint ep = new IPEndPoint (ip, NetworkHelpers.FindFreePort ());
  1395. listen.Bind (ep);
  1396. listen.Listen (1);
  1397. BCCalledBack.Reset ();
  1398. BCConnected = false;
  1399. sock.BeginConnect (ip, ep.Port, BCCallback, sock);
  1400. if (BCCalledBack.WaitOne (2000, false) == false) {
  1401. Assert.Fail ("BeginConnectAddressPort wait timed out");
  1402. }
  1403. Assert.AreEqual (true, BCConnected, "BeginConnectAddressPort #1");
  1404. sock.Close ();
  1405. listen.Close ();
  1406. }
  1407. [Test]
  1408. public void BeginConnectAddressPortNull ()
  1409. {
  1410. Socket sock = new Socket (AddressFamily.InterNetwork,
  1411. SocketType.Stream,
  1412. ProtocolType.Tcp);
  1413. IPAddress ip = null;
  1414. try {
  1415. sock.BeginConnect (ip, 1244, BCCallback,
  1416. sock);
  1417. Assert.Fail ("BeginConnectAddressPortNull #1");
  1418. } catch (ArgumentNullException) {
  1419. } finally {
  1420. sock.Close ();
  1421. }
  1422. }
  1423. [Test]
  1424. #if FEATURE_NO_BSD_SOCKETS
  1425. [ExpectedException (typeof (PlatformNotSupportedException))]
  1426. #endif
  1427. public void BeginConnectAddressPortListen ()
  1428. {
  1429. Socket sock = new Socket (AddressFamily.InterNetwork,
  1430. SocketType.Stream,
  1431. ProtocolType.Tcp);
  1432. IPAddress ip = IPAddress.Loopback;
  1433. IPEndPoint ep = new IPEndPoint (ip, NetworkHelpers.FindFreePort ());
  1434. sock.Bind (ep);
  1435. sock.Listen (1);
  1436. try {
  1437. sock.BeginConnect (ip, ep.Port, BCCallback, sock);
  1438. Assert.Fail ("BeginConnectAddressPortListen #1");
  1439. } catch (InvalidOperationException) {
  1440. } finally {
  1441. sock.Close ();
  1442. }
  1443. }
  1444. [Test]
  1445. [ExpectedException (typeof(ObjectDisposedException))]
  1446. public void BeginConnectAddressPortClosed ()
  1447. {
  1448. Socket sock = new Socket (AddressFamily.InterNetwork,
  1449. SocketType.Stream,
  1450. ProtocolType.Tcp);
  1451. IPAddress ip = IPAddress.Loopback;
  1452. sock.Close ();
  1453. sock.BeginConnect (ip, 1244, BCCallback, sock);
  1454. }
  1455. [Test]
  1456. [Category ("NotOnMac")]
  1457. /*
  1458. * This is not a Mono bug.
  1459. *
  1460. * By default, only 127.0.0.1 is enabled and you must explicitly
  1461. * enable additional addresses using 'sudo ifconfig lo0 alias 127.0.0.1'.
  1462. *
  1463. * I tested this on Mac OS 10.7.4; a 'ping 127.0.0.2' does not work
  1464. * until I add that alias.
  1465. *
  1466. */
  1467. public void BeginConnectMultiple ()
  1468. {
  1469. Socket sock = new Socket (AddressFamily.InterNetwork,
  1470. SocketType.Stream,
  1471. ProtocolType.Tcp);
  1472. Socket listen = new Socket (AddressFamily.InterNetwork,
  1473. SocketType.Stream,
  1474. ProtocolType.Tcp);
  1475. IPEndPoint ep = new IPEndPoint (IPAddress.Loopback,
  1476. NetworkHelpers.FindFreePort ());
  1477. IPAddress[] ips = new IPAddress[4];
  1478. ips[0] = IPAddress.Parse ("127.0.0.4");
  1479. ips[1] = IPAddress.Parse ("127.0.0.3");
  1480. ips[2] = IPAddress.Parse ("127.0.0.2");
  1481. ips[3] = IPAddress.Parse ("127.0.0.1");
  1482. listen.Bind (ep);
  1483. listen.Listen (1);
  1484. BCCalledBack.Reset ();
  1485. BCConnected = false;
  1486. sock.BeginConnect (ips, ep.Port, BCCallback, sock);
  1487. /* Longer wait here, because the ms runtime
  1488. * takes a lot longer to not connect
  1489. */
  1490. /*
  1491. if (BCCalledBack.WaitOne (30000, false) == false) {
  1492. Assert.Fail ("BeginConnectMultiple wait failed");
  1493. }
  1494. */
  1495. Assert.IsTrue (BCCalledBack.WaitOne (30000), "#0");
  1496. Assert.AreEqual (true, BCConnected, "BeginConnectMultiple #1");
  1497. Assert.AreEqual (AddressFamily.InterNetwork, sock.RemoteEndPoint.AddressFamily, "BeginConnectMultiple #2");
  1498. IPEndPoint remep = (IPEndPoint)sock.RemoteEndPoint;
  1499. Assert.AreEqual (IPAddress.Loopback, remep.Address, "BeginConnectMultiple #2");
  1500. sock.Close ();
  1501. listen.Close ();
  1502. }
  1503. [Test]
  1504. #if FEATURE_NO_BSD_SOCKETS
  1505. [ExpectedException (typeof (PlatformNotSupportedException))]
  1506. #endif
  1507. public void BeginConnectMultiple2 ()
  1508. {
  1509. Socket sock = new Socket (AddressFamily.InterNetwork,
  1510. SocketType.Stream,
  1511. ProtocolType.Tcp);
  1512. Socket listen = new Socket (AddressFamily.InterNetwork,
  1513. SocketType.Stream,
  1514. ProtocolType.Tcp);
  1515. // Need at least two addresses.
  1516. var ips = Dns.GetHostAddresses (string.Empty);
  1517. if (ips.Length < 1)
  1518. Assert.Ignore ("This test needs at least two IP addresses.");
  1519. var allIps = new IPAddress [ips.Length + 1];
  1520. allIps [0] = IPAddress.Loopback;
  1521. ips.CopyTo (allIps, 1);
  1522. /*
  1523. * Only bind to the loopback interface, so all the non-loopback
  1524. * IP addresses will fail. BeginConnect()/EndConnect() should
  1525. * succeed it it can connect to at least one of the requested
  1526. * addresses.
  1527. */
  1528. IPEndPoint ep = new IPEndPoint (IPAddress.Loopback, NetworkHelpers.FindFreePort ());
  1529. listen.Bind (ep);
  1530. listen.Listen (1);
  1531. BCCalledBack.Reset ();
  1532. BCConnected = false;
  1533. sock.BeginConnect (allIps, ep.Port, BCCallback, sock);
  1534. /* Longer wait here, because the ms runtime
  1535. * takes a lot longer to not connect
  1536. */
  1537. if (BCCalledBack.WaitOne (10000, false) == false) {
  1538. Assert.Fail ("BeginConnectMultiple2 wait failed");
  1539. }
  1540. Assert.AreEqual (true, BCConnected, "BeginConnectMultiple2 #1");
  1541. Assert.AreEqual (AddressFamily.InterNetwork, sock.RemoteEndPoint.AddressFamily, "BeginConnectMultiple2 #2");
  1542. IPEndPoint remep = (IPEndPoint)sock.RemoteEndPoint;
  1543. Assert.AreEqual (IPAddress.Loopback, remep.Address, "BeginConnectMultiple2 #2");
  1544. sock.Close ();
  1545. listen.Close ();
  1546. }
  1547. [Test]
  1548. public void BeginConnectMultipleNull ()
  1549. {
  1550. Socket sock = new Socket (AddressFamily.InterNetwork,
  1551. SocketType.Stream,
  1552. ProtocolType.Tcp);
  1553. IPAddress[] ips = null;
  1554. try {
  1555. sock.BeginConnect (ips, 1246, BCCallback,
  1556. sock);
  1557. Assert.Fail ("BeginConnectMultipleNull #1");
  1558. } catch (ArgumentNullException) {
  1559. } finally {
  1560. sock.Close ();
  1561. }
  1562. }
  1563. [Test]
  1564. #if FEATURE_NO_BSD_SOCKETS
  1565. [ExpectedException (typeof (PlatformNotSupportedException))]
  1566. #endif
  1567. public void BeginConnectMultipleListen ()
  1568. {
  1569. Socket sock = new Socket (AddressFamily.InterNetwork,
  1570. SocketType.Stream,
  1571. ProtocolType.Tcp);
  1572. IPAddress[] ips = new IPAddress[4];
  1573. IPEndPoint ep = new IPEndPoint (IPAddress.Loopback,
  1574. NetworkHelpers.FindFreePort ());
  1575. ips[0] = IPAddress.Parse ("127.0.0.4");
  1576. ips[1] = IPAddress.Parse ("127.0.0.3");
  1577. ips[2] = IPAddress.Parse ("127.0.0.2");
  1578. ips[3] = IPAddress.Parse ("127.0.0.1");
  1579. sock.Bind (ep);
  1580. sock.Listen (1);
  1581. try {
  1582. sock.BeginConnect (ips, ep.Port, BCCallback,
  1583. sock);
  1584. Assert.Fail ("BeginConnectMultipleListen #1");
  1585. } catch (InvalidOperationException) {
  1586. } finally {
  1587. sock.Close ();
  1588. }
  1589. }
  1590. [Test]
  1591. [ExpectedException (typeof(ObjectDisposedException))]
  1592. public void BeginConnectMultipleClosed ()
  1593. {
  1594. Socket sock = new Socket (AddressFamily.InterNetwork,
  1595. SocketType.Stream,
  1596. ProtocolType.Tcp);
  1597. IPAddress[] ips = new IPAddress[4];
  1598. ips[0] = IPAddress.Parse ("127.0.0.4");
  1599. ips[1] = IPAddress.Parse ("127.0.0.3");
  1600. ips[2] = IPAddress.Parse ("127.0.0.2");
  1601. ips[3] = IPAddress.Parse ("127.0.0.1");
  1602. sock.Close ();
  1603. sock.BeginConnect (ips, 1247, BCCallback, sock);
  1604. }
  1605. [Test]
  1606. public void BeginConnectHostPortNull ()
  1607. {
  1608. Socket sock = new Socket (AddressFamily.InterNetwork,
  1609. SocketType.Stream,
  1610. ProtocolType.Tcp);
  1611. try {
  1612. sock.BeginConnect ((string)null, 0,
  1613. BCCallback, sock);
  1614. Assert.Fail ("BeginConnectHostPort #1");
  1615. } catch (ArgumentNullException) {
  1616. } finally {
  1617. sock.Close ();
  1618. }
  1619. }
  1620. [Test]
  1621. #if FEATURE_NO_BSD_SOCKETS
  1622. [ExpectedException (typeof (PlatformNotSupportedException))]
  1623. #endif
  1624. public void BeginConnectHostPortListen ()
  1625. {
  1626. Socket sock = new Socket (AddressFamily.InterNetwork,
  1627. SocketType.Stream,
  1628. ProtocolType.Tcp);
  1629. IPAddress ip = IPAddress.Loopback;
  1630. IPEndPoint ep = new IPEndPoint (ip, NetworkHelpers.FindFreePort ());
  1631. sock.Bind (ep);
  1632. sock.Listen (1);
  1633. try {
  1634. sock.BeginConnect ("localhost", ep.Port,
  1635. BCCallback, sock);
  1636. Assert.Fail ("BeginConnectHostPortListen #1");
  1637. } catch (InvalidOperationException) {
  1638. } finally {
  1639. sock.Close ();
  1640. }
  1641. }
  1642. [Test]
  1643. [Category ("NotWorking")] // Need to pick a non-IP AddressFamily that "works" on both mono and ms, this one only works on ms
  1644. public void BeginConnectHostPortNotIP ()
  1645. {
  1646. Socket sock = new Socket (AddressFamily.NetBios,
  1647. SocketType.Seqpacket,
  1648. ProtocolType.Unspecified);
  1649. try {
  1650. sock.BeginConnect ("localhost", 0, BCCallback,
  1651. sock);
  1652. Assert.Fail ("BeginConnectHostPortNotIP #1");
  1653. } catch (NotSupportedException) {
  1654. } finally {
  1655. sock.Close ();
  1656. }
  1657. }
  1658. [Test]
  1659. [ExpectedException (typeof(ObjectDisposedException))]
  1660. public void BeginConnectHostPortClosed ()
  1661. {
  1662. Socket sock = new Socket (AddressFamily.InterNetwork,
  1663. SocketType.Stream,
  1664. ProtocolType.Tcp);
  1665. sock.Close ();
  1666. sock.BeginConnect ("localhost", 0, BCCallback, sock);
  1667. }
  1668. static bool BDDisconnected = false;
  1669. static ManualResetEvent BDCalledBack = new ManualResetEvent (false);
  1670. private static void BDCallback (IAsyncResult asyncResult)
  1671. {
  1672. Socket sock = (Socket)asyncResult.AsyncState;
  1673. sock.EndDisconnect (asyncResult);
  1674. BDDisconnected = true;
  1675. BDCalledBack.Set ();
  1676. }
  1677. [Test]
  1678. [Category ("NotDotNet")] // "Needs XP or later"
  1679. #if FEATURE_NO_BSD_SOCKETS
  1680. [ExpectedException (typeof (PlatformNotSupportedException))]
  1681. #endif
  1682. public void BeginDisconnect ()
  1683. {
  1684. Socket sock = new Socket (AddressFamily.InterNetwork,
  1685. SocketType.Stream,
  1686. ProtocolType.Tcp);
  1687. Socket listen = new Socket (AddressFamily.InterNetwork,
  1688. SocketType.Stream,
  1689. ProtocolType.Tcp);
  1690. IPAddress ip = IPAddress.Loopback;
  1691. IPEndPoint ep = new IPEndPoint (ip, NetworkHelpers.FindFreePort ());
  1692. listen.Bind (ep);
  1693. listen.Listen (1);
  1694. sock.Connect (ip, ep.Port);
  1695. Assert.AreEqual (true, sock.Connected, "BeginDisconnect #1");
  1696. sock.Shutdown (SocketShutdown.Both);
  1697. BDCalledBack.Reset ();
  1698. BDDisconnected = false;
  1699. sock.BeginDisconnect (false, BDCallback, sock);
  1700. if (BDCalledBack.WaitOne (2000, false) == false) {
  1701. Assert.Fail ("BeginDisconnect wait timed out");
  1702. }
  1703. Assert.AreEqual (true, BDDisconnected, "BeginDisconnect #2");
  1704. Assert.AreEqual (false, sock.Connected, "BeginDisconnect #3");
  1705. sock.Close ();
  1706. listen.Close ();
  1707. }
  1708. [Test]
  1709. public void BeginReceiveSocketError ()
  1710. {
  1711. }
  1712. [Test]
  1713. public void BeginReceiveGeneric ()
  1714. {
  1715. }
  1716. [Test]
  1717. public void BeginReceiveGenericSocketError ()
  1718. {
  1719. }
  1720. private static void BSCallback (IAsyncResult asyncResult)
  1721. {
  1722. Socket sock = (Socket)asyncResult.AsyncState;
  1723. sock.EndSend (asyncResult);
  1724. }
  1725. [Test]
  1726. public void BeginSendNotConnected ()
  1727. {
  1728. Socket sock = new Socket (AddressFamily.InterNetwork,
  1729. SocketType.Stream,
  1730. ProtocolType.Tcp);
  1731. byte[] send_bytes = new byte[] {10, 11, 12, 13};
  1732. try {
  1733. sock.BeginSend (send_bytes, 0,
  1734. send_bytes.Length,
  1735. SocketFlags.None, BSCallback,
  1736. sock);
  1737. Assert.Fail ("BeginSendNotConnected #1");
  1738. } catch (SocketException ex) {
  1739. Assert.AreEqual (10057, ex.ErrorCode, "BeginSendNotConnected #2");
  1740. } finally {
  1741. sock.Close ();
  1742. }
  1743. }
  1744. [Test]
  1745. public void BeginSendSocketError ()
  1746. {
  1747. }
  1748. [Test]
  1749. public void BeginSendGeneric ()
  1750. {
  1751. }
  1752. [Test]
  1753. public void BeginSendGenericSocketError ()
  1754. {
  1755. }
  1756. [Test]
  1757. #if FEATURE_NO_BSD_SOCKETS
  1758. [ExpectedException (typeof (PlatformNotSupportedException))]
  1759. #endif
  1760. public void BindTwice ()
  1761. {
  1762. Socket sock = new Socket (AddressFamily.InterNetwork,
  1763. SocketType.Stream,
  1764. ProtocolType.Tcp);
  1765. IPEndPoint ep1 = new IPEndPoint (IPAddress.Loopback,
  1766. NetworkHelpers.FindFreePort ());
  1767. IPEndPoint ep2 = new IPEndPoint (IPAddress.Loopback,
  1768. NetworkHelpers.FindFreePort ());
  1769. sock.Bind (ep1);
  1770. try {
  1771. sock.Bind (ep2);
  1772. Assert.Fail ("BindTwice #1");
  1773. } catch (SocketException ex) {
  1774. Assert.AreEqual (10022, ex.ErrorCode, "BindTwice #2");
  1775. } finally {
  1776. sock.Close ();
  1777. }
  1778. }
  1779. [Test]
  1780. #if FEATURE_NO_BSD_SOCKETS
  1781. [ExpectedException (typeof (PlatformNotSupportedException))]
  1782. #endif
  1783. public void Close ()
  1784. {
  1785. Socket sock = new Socket (AddressFamily.InterNetwork,
  1786. SocketType.Stream,
  1787. ProtocolType.Tcp);
  1788. Socket listen = new Socket (AddressFamily.InterNetwork,
  1789. SocketType.Stream,
  1790. ProtocolType.Tcp);
  1791. IPEndPoint ep = new IPEndPoint (IPAddress.Loopback,
  1792. NetworkHelpers.FindFreePort ());
  1793. listen.Bind (ep);
  1794. listen.Listen (1);
  1795. sock.Connect (ep);
  1796. Assert.AreEqual (true, sock.Connected, "Close #1");
  1797. sock.Close (2);
  1798. Thread.Sleep (3000);
  1799. Assert.AreEqual (false, sock.Connected, "Close #2");
  1800. listen.Close ();
  1801. }
  1802. [Test]
  1803. #if FEATURE_NO_BSD_SOCKETS
  1804. [ExpectedException (typeof (PlatformNotSupportedException))]
  1805. #endif
  1806. public void ConnectAddressPort ()
  1807. {
  1808. Socket sock = new Socket (AddressFamily.InterNetwork,
  1809. SocketType.Stream,
  1810. ProtocolType.Tcp);
  1811. Socket listen = new Socket (AddressFamily.InterNetwork,
  1812. SocketType.Stream,
  1813. ProtocolType.Tcp);
  1814. IPAddress ip = IPAddress.Loopback;
  1815. IPEndPoint ep = new IPEndPoint (ip, NetworkHelpers.FindFreePort ());
  1816. listen.Bind (ep);
  1817. listen.Listen (1);
  1818. sock.Connect (ip, ep.Port);
  1819. Assert.AreEqual (true, sock.Connected, "ConnectAddressPort #1");
  1820. sock.Close ();
  1821. listen.Close ();
  1822. }
  1823. [Test]
  1824. public void ConnectAddressPortNull ()
  1825. {
  1826. Socket sock = new Socket (AddressFamily.InterNetwork,
  1827. SocketType.Stream,
  1828. ProtocolType.Tcp);
  1829. IPAddress ip = null;
  1830. try {
  1831. sock.Connect (ip, 1249);
  1832. Assert.Fail ("ConnectAddressPortNull #1");
  1833. } catch (ArgumentNullException) {
  1834. } finally {
  1835. sock.Close ();
  1836. }
  1837. }
  1838. [Test]
  1839. #if FEATURE_NO_BSD_SOCKETS
  1840. [ExpectedException (typeof (PlatformNotSupportedException))]
  1841. #endif
  1842. public void ConnectAddressPortListen ()
  1843. {
  1844. Socket sock = new Socket (AddressFamily.InterNetwork,
  1845. SocketType.Stream,
  1846. ProtocolType.Tcp);
  1847. IPAddress ip = IPAddress.Loopback;
  1848. IPEndPoint ep = new IPEndPoint (ip, NetworkHelpers.FindFreePort ());
  1849. sock.Bind (ep);
  1850. sock.Listen (1);
  1851. try {
  1852. sock.Connect (ip, ep.Port);
  1853. Assert.Fail ("ConnectAddressPortListen #1");
  1854. } catch (InvalidOperationException) {
  1855. } finally {
  1856. sock.Close ();
  1857. }
  1858. }
  1859. [Test]
  1860. [ExpectedException (typeof(ObjectDisposedException))]
  1861. public void ConnectAddressPortClosed ()
  1862. {
  1863. Socket sock = new Socket (AddressFamily.InterNetwork,
  1864. SocketType.Stream,
  1865. ProtocolType.Tcp);
  1866. IPAddress ip = IPAddress.Loopback;
  1867. sock.Close ();
  1868. sock.Connect (ip, 1250);
  1869. }
  1870. [Test]
  1871. [Category ("NotOnMac")] // MacOSX trashes the fd after the failed connect attempt to 127.0.0.4
  1872. /*
  1873. * This is not a Mono bug.
  1874. *
  1875. * By default, only 127.0.0.1 is enabled and you must explicitly
  1876. * enable additional addresses using 'sudo ifconfig lo0 alias 127.0.0.1'.
  1877. *
  1878. * I tested this on Mac OS 10.7.4; a 'ping 127.0.0.2' does not work
  1879. * until I add that alias.
  1880. *
  1881. * However, after doing so, Mac OS treats these as separate addresses, ie. attempting
  1882. * to connect to 127.0.0.4 yields a connection refused.
  1883. *
  1884. * When using Connect(), the .NET runtime also throws an exception if connecting to
  1885. * any of the IP addresses fails. This is different with BeginConnect()/EndConnect()
  1886. * which succeeds when it can connect to at least one of the addresses.
  1887. *
  1888. */
  1889. public void ConnectMultiple ()
  1890. {
  1891. Socket sock = new Socket (AddressFamily.InterNetwork,
  1892. SocketType.Stream,
  1893. ProtocolType.Tcp);
  1894. Socket listen = new Socket (AddressFamily.InterNetwork,
  1895. SocketType.Stream,
  1896. ProtocolType.Tcp);
  1897. IPEndPoint ep = new IPEndPoint (IPAddress.Loopback,
  1898. NetworkHelpers.FindFreePort ());
  1899. IPAddress[] ips = new IPAddress[4];
  1900. ips[0] = IPAddress.Parse ("127.0.0.4");
  1901. ips[1] = IPAddress.Parse ("127.0.0.3");
  1902. ips[2] = IPAddress.Parse ("127.0.0.2");
  1903. ips[3] = IPAddress.Parse ("127.0.0.1");
  1904. listen.Bind (ep);
  1905. listen.Listen (1);
  1906. sock.Connect (ips, ep.Port);
  1907. Assert.AreEqual (true, sock.Connected, "ConnectMultiple #1");
  1908. Assert.AreEqual (AddressFamily.InterNetwork, sock.RemoteEndPoint.AddressFamily, "ConnectMultiple #2");
  1909. IPEndPoint remep = (IPEndPoint)sock.RemoteEndPoint;
  1910. Assert.AreEqual (IPAddress.Loopback, remep.Address, "ConnectMultiple #2");
  1911. sock.Close ();
  1912. listen.Close ();
  1913. }
  1914. [Test]
  1915. #if FEATURE_NO_BSD_SOCKETS
  1916. [ExpectedException (typeof (PlatformNotSupportedException))]
  1917. #endif
  1918. public void ConnectMultiple2 ()
  1919. {
  1920. Socket sock = new Socket (AddressFamily.InterNetwork,
  1921. SocketType.Stream,
  1922. ProtocolType.Tcp);
  1923. Socket listen = new Socket (AddressFamily.InterNetwork,
  1924. SocketType.Stream,
  1925. ProtocolType.Tcp);
  1926. // Need at least two addresses.
  1927. var ips = Dns.GetHostAddresses (string.Empty);
  1928. if (ips.Length < 1)
  1929. Assert.Ignore ("This test needs at least two IP addresses.");
  1930. var allIps = new IPAddress [ips.Length + 1];
  1931. allIps [0] = IPAddress.Loopback;
  1932. ips.CopyTo (allIps, 1);
  1933. /*
  1934. * Bind to IPAddress.Any; Connect() will fail unless it can
  1935. * connect to all the addresses in allIps.
  1936. */
  1937. IPEndPoint ep = new IPEndPoint (IPAddress.Any, NetworkHelpers.FindFreePort ());
  1938. listen.Bind (ep);
  1939. listen.Listen (1);
  1940. sock.Connect (allIps, ep.Port);
  1941. Assert.AreEqual (true, sock.Connected, "ConnectMultiple2 #1");
  1942. Assert.AreEqual (AddressFamily.InterNetwork, sock.RemoteEndPoint.AddressFamily, "ConnectMultiple2 #2");
  1943. IPEndPoint remep = (IPEndPoint)sock.RemoteEndPoint;
  1944. Assert.AreEqual (IPAddress.Loopback, remep.Address, "ConnectMultiple2 #3");
  1945. sock.Close ();
  1946. listen.Close ();
  1947. }
  1948. [Test]
  1949. public void ConnectMultipleNull ()
  1950. {
  1951. Socket sock = new Socket (AddressFamily.InterNetwork,
  1952. SocketType.Stream,
  1953. ProtocolType.Tcp);
  1954. IPAddress[] ips = null;
  1955. try {
  1956. sock.Connect (ips, 1251);
  1957. Assert.Fail ("ConnectMultipleNull #1");
  1958. } catch (ArgumentNullException) {
  1959. } finally {
  1960. sock.Close ();
  1961. }
  1962. }
  1963. [Test]
  1964. #if FEATURE_NO_BSD_SOCKETS
  1965. [ExpectedException (typeof (PlatformNotSupportedException))]
  1966. #endif
  1967. public void ConnectMultipleListen ()
  1968. {
  1969. Socket sock = new Socket (AddressFamily.InterNetwork,
  1970. SocketType.Stream,
  1971. ProtocolType.Tcp);
  1972. IPAddress[] ips = new IPAddress[4];
  1973. IPEndPoint ep = new IPEndPoint (IPAddress.Loopback,
  1974. NetworkHelpers.FindFreePort ());
  1975. ips[0] = IPAddress.Parse ("127.0.0.4");
  1976. ips[1] = IPAddress.Parse ("127.0.0.3");
  1977. ips[2] = IPAddress.Parse ("127.0.0.2");
  1978. ips[3] = IPAddress.Parse ("127.0.0.1");
  1979. sock.Bind (ep);
  1980. sock.Listen (1);
  1981. try {
  1982. sock.Connect (ips, ep.Port);
  1983. Assert.Fail ("ConnectMultipleListen #1");
  1984. } catch (InvalidOperationException) {
  1985. } finally {
  1986. sock.Close ();
  1987. }
  1988. }
  1989. [Test]
  1990. [ExpectedException (typeof(ObjectDisposedException))]
  1991. public void ConnectMultipleClosed ()
  1992. {
  1993. Socket sock = new Socket (AddressFamily.InterNetwork,
  1994. SocketType.Stream,
  1995. ProtocolType.Tcp);
  1996. IPAddress[] ips = new IPAddress[4];
  1997. ips[0] = IPAddress.Parse ("127.0.0.4");
  1998. ips[1] = IPAddress.Parse ("127.0.0.3");
  1999. ips[2] = IPAddress.Parse ("127.0.0.2");
  2000. ips[3] = IPAddress.Parse ("127.0.0.1");
  2001. sock.Close ();
  2002. sock.Connect (ips, 1252);
  2003. }
  2004. [Test]
  2005. public void ConnectHostPortNull ()
  2006. {
  2007. Socket sock = new Socket (AddressFamily.InterNetwork,
  2008. SocketType.Stream,
  2009. ProtocolType.Tcp);
  2010. try {
  2011. sock.Connect ((string)null, 0);
  2012. Assert.Fail ("ConnectHostPort #1");
  2013. } catch (ArgumentNullException) {
  2014. } finally {
  2015. sock.Close ();
  2016. }
  2017. }
  2018. [Test]
  2019. #if FEATURE_NO_BSD_SOCKETS
  2020. [ExpectedException (typeof (PlatformNotSupportedException))]
  2021. #endif
  2022. public void ConnectHostPortListen ()
  2023. {
  2024. Socket sock = new Socket (AddressFamily.InterNetwork,
  2025. SocketType.Stream,
  2026. ProtocolType.Tcp);
  2027. IPAddress ip = IPAddress.Loopback;
  2028. IPEndPoint ep = new IPEndPoint (ip, NetworkHelpers.FindFreePort ());
  2029. sock.Bind (ep);
  2030. sock.Listen (1);
  2031. try {
  2032. sock.Connect ("localhost", ep.Port);
  2033. Assert.Fail ("ConnectHostPortListen #1");
  2034. } catch (InvalidOperationException) {
  2035. } finally {
  2036. sock.Close ();
  2037. }
  2038. }
  2039. [Test]
  2040. [Category ("NotWorking")] // Need to pick a non-IP AddressFamily that "works" on both mono and ms, this one only works on ms
  2041. public void ConnectHostPortNotIP ()
  2042. {
  2043. Socket sock = new Socket (AddressFamily.NetBios,
  2044. SocketType.Seqpacket,
  2045. ProtocolType.Unspecified);
  2046. try {
  2047. sock.Connect ("localhost", 0);
  2048. Assert.Fail ("ConnectHostPortNotIP #1");
  2049. } catch (NotSupportedException) {
  2050. } finally {
  2051. sock.Close ();
  2052. }
  2053. }
  2054. [Test]
  2055. #if FEATURE_NO_BSD_SOCKETS
  2056. [ExpectedException (typeof (PlatformNotSupportedException))]
  2057. #else
  2058. [ExpectedException (typeof(ObjectDisposedException))]
  2059. #endif
  2060. public void ConnectHostPortClosed ()
  2061. {
  2062. Socket sock = new Socket (AddressFamily.InterNetwork,
  2063. SocketType.Stream,
  2064. ProtocolType.Tcp);
  2065. sock.Close ();
  2066. sock.Connect ("localhost", 0);
  2067. }
  2068. [Test]
  2069. [Category ("NotDotNet")] // "Needs XP or later"
  2070. #if FEATURE_NO_BSD_SOCKETS
  2071. [ExpectedException (typeof (PlatformNotSupportedException))]
  2072. #endif
  2073. public void Disconnect ()
  2074. {
  2075. Socket sock = new Socket (AddressFamily.InterNetwork,
  2076. SocketType.Stream,
  2077. ProtocolType.Tcp);
  2078. Socket listen = new Socket (AddressFamily.InterNetwork,
  2079. SocketType.Stream,
  2080. ProtocolType.Tcp);
  2081. IPAddress ip = IPAddress.Loopback;
  2082. IPEndPoint ep = new IPEndPoint (ip, NetworkHelpers.FindFreePort ());
  2083. listen.Bind (ep);
  2084. listen.Listen (1);
  2085. sock.Connect (ip, ep.Port);
  2086. Assert.AreEqual (true, sock.Connected, "Disconnect #1");
  2087. sock.Shutdown (SocketShutdown.Both);
  2088. sock.Disconnect (false);
  2089. Assert.AreEqual (false, sock.Connected, "BeginDisconnect #3");
  2090. sock.Close ();
  2091. listen.Close ();
  2092. }
  2093. [Test]
  2094. public void DuplicateAndClose ()
  2095. {
  2096. }
  2097. [Test]
  2098. public void IOControl ()
  2099. {
  2100. }
  2101. [Test]
  2102. public void TestDefaultsDualMode ()
  2103. {
  2104. using (var socket = new Socket (AddressFamily.InterNetworkV6, SocketType.Stream, ProtocolType.Tcp)){
  2105. Assert.IsTrue (socket.DualMode, "In Mono, DualMode must be true when constructing InterNetworkV6 sockets");
  2106. }
  2107. using (var socket = new Socket (SocketType.Stream, ProtocolType.Tcp)){
  2108. Assert.AreEqual (AddressFamily.InterNetworkV6, socket.AddressFamily, "When creating sockets of type stream/tcp, the address family should be InterNetworkV6");
  2109. Assert.IsTrue (socket.DualMode, "In Mono, DualMode must be true when constructing InterNetworkV6 sockets");
  2110. socket.DualMode = false;
  2111. Assert.IsFalse (socket.DualMode, "Setting of DualSocket should turn DualSockets off");
  2112. }
  2113. }
  2114. [Test]
  2115. #if FEATURE_NO_BSD_SOCKETS
  2116. [ExpectedException (typeof (PlatformNotSupportedException))]
  2117. #endif
  2118. public void ReceiveGeneric ()
  2119. {
  2120. int i;
  2121. IPEndPoint endpoint = new IPEndPoint(IPAddress.Loopback, NetworkHelpers.FindFreePort ());
  2122. Socket listensock = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  2123. listensock.Bind (endpoint);
  2124. listensock.Listen(1);
  2125. Socket sendsock = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  2126. sendsock.Connect(endpoint);
  2127. Socket clientsock = listensock.Accept();
  2128. byte[] sendbuf = new byte[256];
  2129. for(i = 0; i < 256; i++) {
  2130. sendbuf[i] = (byte)i;
  2131. }
  2132. for (i = 4; i < 6; i++) {
  2133. Assert.AreEqual (sendbuf[i], (byte)i,
  2134. "#1/" + i.ToString());
  2135. }
  2136. SocketError err;
  2137. sendsock.Send (sendbuf, 0, 256, SocketFlags.None,
  2138. out err);
  2139. byte[] recvbuf = new byte[256];
  2140. List<ArraySegment<byte>> recvbuflist = new List<ArraySegment<byte>>(2);
  2141. recvbuflist.Add(new ArraySegment<byte>(recvbuf, 4, 2));
  2142. recvbuflist.Add(new ArraySegment<byte>(recvbuf, 20, 230));
  2143. clientsock.Receive (recvbuflist);
  2144. /* recvbuf should now hold the first 2 bytes
  2145. * of sendbuf from pos 4, and the next 230
  2146. * bytes of sendbuf from pos 20
  2147. */
  2148. for (i = 0; i < 2; i++) {
  2149. Assert.AreEqual (sendbuf[i], recvbuf[i + 4],
  2150. "#2/" + i.ToString());
  2151. }
  2152. for (i = 2; i < 232; i++) {
  2153. Assert.AreEqual (sendbuf[i], recvbuf[i + 18],
  2154. "#2/" + i.ToString());
  2155. }
  2156. sendsock.Close ();
  2157. clientsock.Close ();
  2158. listensock.Close ();
  2159. }
  2160. [Test]
  2161. #if FEATURE_NO_BSD_SOCKETS
  2162. [ExpectedException (typeof (PlatformNotSupportedException))]
  2163. #endif
  2164. public void SendGeneric ()
  2165. {
  2166. int i;
  2167. IPEndPoint endpoint = new IPEndPoint(IPAddress.Loopback, NetworkHelpers.FindFreePort ());
  2168. Socket listensock = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  2169. listensock.Bind (endpoint);
  2170. listensock.Listen(1);
  2171. Socket sendsock = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  2172. sendsock.Connect(endpoint);
  2173. Socket clientsock = listensock.Accept();
  2174. byte[] sendbuf = new byte[256];
  2175. List<ArraySegment<byte>> sendbuflist = new List<ArraySegment<byte>>(2);
  2176. sendbuflist.Add(new ArraySegment<byte>(sendbuf, 4, 2));
  2177. sendbuflist.Add(new ArraySegment<byte>(sendbuf, 20, 230));
  2178. for(i = 0; i < 256; i++) {
  2179. sendbuf[i] = (byte)i;
  2180. }
  2181. for (i = 4; i < 6; i++) {
  2182. Assert.AreEqual (sendbuf[i], (byte)i,
  2183. "#1/" + i.ToString());
  2184. }
  2185. SocketError err;
  2186. sendsock.Send (sendbuflist, SocketFlags.None, out err);
  2187. byte[] recvbuf = new byte[256];
  2188. clientsock.Receive (recvbuf);
  2189. /* The first 2 bytes of recvbuf should now
  2190. * hold 2 bytes of sendbuf from pos 4, and the
  2191. * next 230 bytes of recvbuf should be sendbuf
  2192. * from pos 20
  2193. */
  2194. for (i = 0; i < 2; i++) {
  2195. Assert.AreEqual (recvbuf[i], sendbuf[i + 4],
  2196. "#2/" + i.ToString());
  2197. }
  2198. for (i = 2; i < 232; i++) {
  2199. Assert.AreEqual (recvbuf[i], sendbuf[i + 18],
  2200. "#2/" + i.ToString());
  2201. }
  2202. sendsock.Close ();
  2203. clientsock.Close ();
  2204. listensock.Close ();
  2205. }
  2206. [Test]
  2207. #if FEATURE_NO_BSD_SOCKETS
  2208. [ExpectedException (typeof (AggregateException))] // Something catches the PlatformNotSupportedException and re-throws an AggregateException
  2209. #endif
  2210. public void ConcurrentExceedSocketLimit ()
  2211. {
  2212. var tasks = new Task[4];
  2213. for (int i = 0; i < 4; i++) {
  2214. tasks[i] = Task.Factory.StartNew (() => SendGenericExceedBuffer ());
  2215. }
  2216. Task.WaitAll (tasks);
  2217. }
  2218. [Test]
  2219. #if FEATURE_NO_BSD_SOCKETS
  2220. [ExpectedException (typeof (PlatformNotSupportedException))]
  2221. #endif
  2222. public void SendGenericExceedBuffer ()
  2223. {
  2224. // Create a buffer larger than the default max.
  2225. const int BUFFER_SIZE = 65 * 1024;
  2226. int i;
  2227. IPEndPoint endpoint = new IPEndPoint(IPAddress.Loopback, NetworkHelpers.FindFreePort ());
  2228. Socket listensock = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  2229. listensock.Bind (endpoint);
  2230. listensock.Listen (1);
  2231. Socket sendsock = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  2232. sendsock.Connect (endpoint);
  2233. Socket clientsock = listensock.Accept ();
  2234. byte[] sendbuf = new byte[BUFFER_SIZE];
  2235. for (i = 0; i < BUFFER_SIZE; i++) {
  2236. sendbuf[i] = (byte)i;
  2237. }
  2238. Task sendTask = Task.Factory.StartNew(() => {
  2239. int sent = sendsock.Send (sendbuf);
  2240. Assert.AreEqual (BUFFER_SIZE, sent, "#1");
  2241. });
  2242. byte[] recvbuf = new byte[BUFFER_SIZE];
  2243. Task recvTask = Task.Factory.StartNew(() => {
  2244. int totalReceived = 0;
  2245. byte[] buffer = new byte[256];
  2246. while (totalReceived < sendbuf.Length) {
  2247. int recvd = clientsock.Receive (buffer, 0, buffer.Length, SocketFlags.None);
  2248. Array.Copy (buffer, 0, recvbuf, totalReceived, recvd);
  2249. totalReceived += recvd;
  2250. }
  2251. Assert.AreEqual (BUFFER_SIZE, totalReceived, "#2");
  2252. });
  2253. Assert.IsTrue (Task.WaitAll (new []{sendTask, recvTask}, 20 * 1000), "#2a");
  2254. for (i = 0; i < BUFFER_SIZE; i++) {
  2255. Assert.AreEqual (recvbuf[i], sendbuf[i],
  2256. "#3/" + i.ToString());
  2257. }
  2258. sendsock.Close ();
  2259. clientsock.Close ();
  2260. listensock.Close ();
  2261. }
  2262. [Test]
  2263. public void ListenNotBound ()
  2264. {
  2265. Socket sock = new Socket (AddressFamily.InterNetwork,
  2266. SocketType.Stream,
  2267. ProtocolType.Tcp);
  2268. try {
  2269. sock.Listen (1);
  2270. Assert.Fail ("ListenNotBound #1");
  2271. } catch (SocketException ex) {
  2272. Assert.AreEqual (10022, ex.ErrorCode, "ListenNotBound #2");
  2273. } finally {
  2274. sock.Close ();
  2275. }
  2276. }
  2277. static Socket CWRSocket;
  2278. static bool CWRReceiving = true;
  2279. static ManualResetEvent CWRReady = new ManualResetEvent (false);
  2280. private static void CWRReceiveThread ()
  2281. {
  2282. byte[] buf = new byte[256];
  2283. try {
  2284. CWRSocket.Receive (buf);
  2285. } catch (SocketException) {
  2286. CWRReceiving = false;
  2287. }
  2288. CWRReady.Set ();
  2289. }
  2290. [Test]
  2291. #if FEATURE_NO_BSD_SOCKETS
  2292. [ExpectedException (typeof (PlatformNotSupportedException))]
  2293. #endif
  2294. public void CloseWhileReceiving ()
  2295. {
  2296. CWRSocket = new Socket (AddressFamily.InterNetwork,
  2297. SocketType.Dgram,
  2298. ProtocolType.Udp);
  2299. CWRSocket.Bind (new IPEndPoint (IPAddress.Loopback,
  2300. NetworkHelpers.FindFreePort ()));
  2301. Thread recv_thread = new Thread (new ThreadStart (CWRReceiveThread));
  2302. CWRReady.Reset ();
  2303. recv_thread.Start ();
  2304. Thread.Sleep (250); /* Wait for the thread to be already receiving */
  2305. CWRSocket.Close ();
  2306. if (CWRReady.WaitOne (1000, false) == false) {
  2307. Assert.Fail ("CloseWhileReceiving wait timed out");
  2308. }
  2309. Assert.IsFalse (CWRReceiving);
  2310. }
  2311. static bool RRCLastRead = false;
  2312. static ManualResetEvent RRCReady = new ManualResetEvent (false);
  2313. private static void RRCClientThread (int port)
  2314. {
  2315. byte[] bytes = new byte[8];
  2316. int readbyte;
  2317. Socket sock = new Socket (AddressFamily.InterNetwork,
  2318. SocketType.Stream,
  2319. ProtocolType.Tcp);
  2320. sock.Connect (new IPEndPoint (IPAddress.Loopback,
  2321. port));
  2322. NetworkStream stream = new NetworkStream (sock);
  2323. readbyte = stream.ReadByte ();
  2324. Assert.AreEqual (0, readbyte, "ReceiveRemoteClosed #1");
  2325. stream.Read (bytes, 0, 0);
  2326. readbyte = stream.ReadByte ();
  2327. Assert.AreEqual (0, readbyte, "ReceiveRemoteClosed #2");
  2328. stream.Read (bytes, 0, 0);
  2329. readbyte = stream.ReadByte ();
  2330. Assert.AreEqual (-1, readbyte, "ReceiveRemoteClosed #3");
  2331. sock.Close ();
  2332. RRCLastRead = true;
  2333. RRCReady.Set ();
  2334. }
  2335. [Test] // Receive (Byte [])
  2336. public void Receive1_Buffer_Null ()
  2337. {
  2338. Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
  2339. ProtocolType.Tcp);
  2340. try {
  2341. s.Receive ((byte []) null);
  2342. Assert.Fail ("#1");
  2343. } catch (ArgumentNullException ex) {
  2344. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  2345. Assert.IsNull (ex.InnerException, "#3");
  2346. Assert.IsNotNull (ex.Message, "#4");
  2347. Assert.AreEqual ("buffer", ex.ParamName, "#5");
  2348. } finally {
  2349. s.Close ();
  2350. }
  2351. }
  2352. [Test] // Receive (Byte [])
  2353. public void Receive1_Socket_Closed ()
  2354. {
  2355. Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
  2356. ProtocolType.Tcp);
  2357. s.Close ();
  2358. try {
  2359. s.Receive ((byte []) null);
  2360. Assert.Fail ("#1");
  2361. } catch (ObjectDisposedException ex) {
  2362. // Cannot access a disposed object
  2363. Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
  2364. Assert.IsNull (ex.InnerException, "#3");
  2365. Assert.IsNotNull (ex.Message, "#4");
  2366. Assert.AreEqual (typeof (Socket).FullName, ex.ObjectName, "#5");
  2367. }
  2368. }
  2369. [Test] // Receive (Byte [], SocketFlags)
  2370. public void Receive2_Buffer_Null ()
  2371. {
  2372. Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
  2373. ProtocolType.Tcp);
  2374. try {
  2375. s.Receive ((byte []) null, (SocketFlags) 666);
  2376. Assert.Fail ("#1");
  2377. } catch (ArgumentNullException ex) {
  2378. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  2379. Assert.IsNull (ex.InnerException, "#3");
  2380. Assert.IsNotNull (ex.Message, "#4");
  2381. Assert.AreEqual ("buffer", ex.ParamName, "#5");
  2382. } finally {
  2383. s.Close ();
  2384. }
  2385. }
  2386. [Test] // Receive (Byte [], SocketFlags)
  2387. public void Receive2_Socket_Closed ()
  2388. {
  2389. Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
  2390. ProtocolType.Tcp);
  2391. s.Close ();
  2392. try {
  2393. s.Receive ((byte []) null, (SocketFlags) 666);
  2394. Assert.Fail ("#1");
  2395. } catch (ObjectDisposedException ex) {
  2396. // Cannot access a disposed object
  2397. Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
  2398. Assert.IsNull (ex.InnerException, "#3");
  2399. Assert.IsNotNull (ex.Message, "#4");
  2400. Assert.AreEqual (typeof (Socket).FullName, ex.ObjectName, "#5");
  2401. }
  2402. }
  2403. [Test] // Receive (Byte [], Int32, SocketFlags)
  2404. public void Receive3_Buffer_Null ()
  2405. {
  2406. Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
  2407. ProtocolType.Tcp);
  2408. try {
  2409. s.Receive ((byte []) null, 0, (SocketFlags) 666);
  2410. Assert.Fail ("#1");
  2411. } catch (ArgumentNullException ex) {
  2412. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  2413. Assert.IsNull (ex.InnerException, "#3");
  2414. Assert.IsNotNull (ex.Message, "#4");
  2415. Assert.AreEqual ("buffer", ex.ParamName, "#5");
  2416. } finally {
  2417. s.Close ();
  2418. }
  2419. }
  2420. [Test] // Receive (Byte [], Int32, SocketFlags)
  2421. public void Receive3_Socket_Closed ()
  2422. {
  2423. Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
  2424. ProtocolType.Tcp);
  2425. s.Close ();
  2426. try {
  2427. s.Receive ((byte []) null, 0, (SocketFlags) 666);
  2428. Assert.Fail ("#1");
  2429. } catch (ObjectDisposedException ex) {
  2430. // Cannot access a disposed object
  2431. Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
  2432. Assert.IsNull (ex.InnerException, "#3");
  2433. Assert.IsNotNull (ex.Message, "#4");
  2434. Assert.AreEqual (typeof (Socket).FullName, ex.ObjectName, "#5");
  2435. }
  2436. }
  2437. [Test] // Receive (Byte [], Int32, Int32, SocketFlags)
  2438. public void Receive4_Buffer_Null ()
  2439. {
  2440. Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
  2441. ProtocolType.Tcp);
  2442. try {
  2443. s.Receive ((byte []) null, 0, 0, (SocketFlags) 666);
  2444. Assert.Fail ("#1");
  2445. } catch (ArgumentNullException ex) {
  2446. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  2447. Assert.IsNull (ex.InnerException, "#3");
  2448. Assert.IsNotNull (ex.Message, "#4");
  2449. Assert.AreEqual ("buffer", ex.ParamName, "#5");
  2450. } finally {
  2451. s.Close ();
  2452. }
  2453. }
  2454. [Test] // Receive (Byte [], Int32, Int32, SocketFlags)
  2455. public void Receive4_Socket_Closed ()
  2456. {
  2457. Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
  2458. ProtocolType.Tcp);
  2459. s.Close ();
  2460. try {
  2461. s.Receive ((byte []) null, 0, 0, (SocketFlags) 666);
  2462. Assert.Fail ("#1");
  2463. } catch (ObjectDisposedException ex) {
  2464. // Cannot access a disposed object
  2465. Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
  2466. Assert.IsNull (ex.InnerException, "#3");
  2467. Assert.IsNotNull (ex.Message, "#4");
  2468. Assert.AreEqual (typeof (Socket).FullName, ex.ObjectName, "#5");
  2469. }
  2470. }
  2471. [Test] // Receive (Byte [], Int32, Int32, SocketFlags, out SocketError)
  2472. public void Receive5_Buffer_Null ()
  2473. {
  2474. Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
  2475. ProtocolType.Tcp);
  2476. SocketError error;
  2477. try {
  2478. s.Receive ((byte []) null, 0, 0, SocketFlags.None, out error);
  2479. Assert.Fail ("#1");
  2480. } catch (ArgumentNullException ex) {
  2481. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  2482. Assert.IsNull (ex.InnerException, "#3");
  2483. Assert.IsNotNull (ex.Message, "#4");
  2484. Assert.AreEqual ("buffer", ex.ParamName, "#5");
  2485. } finally {
  2486. s.Close ();
  2487. }
  2488. }
  2489. [Test] // Receive (Byte [], Int32, Int32, SocketFlags, out SocketError)
  2490. public void Receive5_Socket_Closed ()
  2491. {
  2492. Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
  2493. ProtocolType.Tcp);
  2494. s.Close ();
  2495. SocketError error;
  2496. try {
  2497. s.Receive ((byte []) null, 0, 0, SocketFlags.None, out error);
  2498. Assert.Fail ("#1");
  2499. } catch (ObjectDisposedException ex) {
  2500. // Cannot access a disposed object
  2501. Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
  2502. Assert.IsNull (ex.InnerException, "#3");
  2503. Assert.IsNotNull (ex.Message, "#4");
  2504. Assert.AreEqual (typeof (Socket).FullName, ex.ObjectName, "#5");
  2505. }
  2506. }
  2507. [Test] // Receive (IList<ArraySegment<Byte>>)
  2508. public void Receive6_Buffers_Null ()
  2509. {
  2510. Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
  2511. ProtocolType.Tcp);
  2512. try {
  2513. s.Receive ((IList<ArraySegment<byte>>) null);
  2514. Assert.Fail ("#1");
  2515. } catch (ArgumentNullException ex) {
  2516. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  2517. Assert.IsNull (ex.InnerException, "#3");
  2518. Assert.IsNotNull (ex.Message, "#4");
  2519. Assert.AreEqual ("buffers", ex.ParamName, "#5");
  2520. } finally {
  2521. s.Close ();
  2522. }
  2523. }
  2524. [Test] // Receive (IList<ArraySegment<Byte>>)
  2525. public void Receive6_Socket_Closed ()
  2526. {
  2527. Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
  2528. ProtocolType.Tcp);
  2529. s.Close ();
  2530. try {
  2531. s.Receive ((IList<ArraySegment<byte>>) null);
  2532. Assert.Fail ("#1");
  2533. } catch (ObjectDisposedException ex) {
  2534. // Cannot access a disposed object
  2535. Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
  2536. Assert.IsNull (ex.InnerException, "#3");
  2537. Assert.IsNotNull (ex.Message, "#4");
  2538. Assert.AreEqual (typeof (Socket).FullName, ex.ObjectName, "#5");
  2539. }
  2540. }
  2541. [Test] // Receive (IList<ArraySegment<Byte>>, SocketFlags)
  2542. public void Receive7_Buffers_Null ()
  2543. {
  2544. Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
  2545. ProtocolType.Tcp);
  2546. try {
  2547. s.Receive ((IList<ArraySegment<byte>>) null, (SocketFlags) 666);
  2548. Assert.Fail ("#1");
  2549. } catch (ArgumentNullException ex) {
  2550. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  2551. Assert.IsNull (ex.InnerException, "#3");
  2552. Assert.IsNotNull (ex.Message, "#4");
  2553. Assert.AreEqual ("buffers", ex.ParamName, "#5");
  2554. } finally {
  2555. s.Close ();
  2556. }
  2557. }
  2558. [Test] // Receive (IList<ArraySegment<Byte>>, SocketFlags)
  2559. public void Receive7_Socket_Closed ()
  2560. {
  2561. Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
  2562. ProtocolType.Tcp);
  2563. s.Close ();
  2564. try {
  2565. s.Receive ((IList<ArraySegment<byte>>) null, (SocketFlags) 666);
  2566. Assert.Fail ("#1");
  2567. } catch (ObjectDisposedException ex) {
  2568. // Cannot access a disposed object
  2569. Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
  2570. Assert.IsNull (ex.InnerException, "#3");
  2571. Assert.IsNotNull (ex.Message, "#4");
  2572. Assert.AreEqual (typeof (Socket).FullName, ex.ObjectName, "#5");
  2573. }
  2574. }
  2575. [Test] // Receive (IList<ArraySegment<Byte>>, SocketFlags, out SocketError)
  2576. public void Receive8_Buffers_Null ()
  2577. {
  2578. Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
  2579. ProtocolType.Tcp);
  2580. SocketError error;
  2581. try {
  2582. s.Receive ((IList<ArraySegment<byte>>) null, (SocketFlags) 666,
  2583. out error);
  2584. Assert.Fail ("#1");
  2585. } catch (ArgumentNullException ex) {
  2586. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  2587. Assert.IsNull (ex.InnerException, "#3");
  2588. Assert.IsNotNull (ex.Message, "#4");
  2589. Assert.AreEqual ("buffers", ex.ParamName, "#5");
  2590. } finally {
  2591. s.Close ();
  2592. }
  2593. }
  2594. [Test] // Receive (IList<ArraySegment<Byte>>, SocketFlags, out SocketError)
  2595. public void Receive8_Socket_Closed ()
  2596. {
  2597. Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
  2598. ProtocolType.Tcp);
  2599. s.Close ();
  2600. SocketError error;
  2601. try {
  2602. s.Receive ((IList<ArraySegment<byte>>) null, (SocketFlags) 666,
  2603. out error);
  2604. Assert.Fail ("#1");
  2605. } catch (ObjectDisposedException ex) {
  2606. // Cannot access a disposed object
  2607. Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
  2608. Assert.IsNull (ex.InnerException, "#3");
  2609. Assert.IsNotNull (ex.Message, "#4");
  2610. Assert.AreEqual (typeof (Socket).FullName, ex.ObjectName, "#5");
  2611. } finally {
  2612. s.Close ();
  2613. }
  2614. }
  2615. [Test] // ReceiveFrom (Byte [], ref EndPoint)
  2616. #if FEATURE_NO_BSD_SOCKETS
  2617. [ExpectedException (typeof (PlatformNotSupportedException))]
  2618. #endif
  2619. public void ReceiveFrom1_Buffer_Null ()
  2620. {
  2621. Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
  2622. ProtocolType.Tcp);
  2623. EndPoint remoteEP = new IPEndPoint (IPAddress.Loopback, NetworkHelpers.FindFreePort ());
  2624. try {
  2625. s.ReceiveFrom ((Byte []) null, ref remoteEP);
  2626. Assert.Fail ("#1");
  2627. } catch (ArgumentNullException ex) {
  2628. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  2629. Assert.IsNull (ex.InnerException, "#3");
  2630. Assert.IsNotNull (ex.Message, "#4");
  2631. Assert.AreEqual ("buffer", ex.ParamName, "#5");
  2632. } finally {
  2633. s.Close ();
  2634. }
  2635. }
  2636. [Test] // ReceiveFrom (Byte [], ref EndPoint)
  2637. public void ReceiveFrom1_RemoteEP_Null ()
  2638. {
  2639. Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
  2640. ProtocolType.Tcp);
  2641. byte [] buffer = new byte [0];
  2642. EndPoint remoteEP = null;
  2643. try {
  2644. s.ReceiveFrom (buffer, ref remoteEP);
  2645. Assert.Fail ("#1");
  2646. } catch (ArgumentNullException ex) {
  2647. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  2648. Assert.IsNull (ex.InnerException, "#3");
  2649. Assert.IsNotNull (ex.Message, "#4");
  2650. Assert.AreEqual ("remoteEP", ex.ParamName, "#5");
  2651. } finally {
  2652. s.Close ();
  2653. }
  2654. }
  2655. [Test] // ReceiveFrom (Byte [], ref EndPoint)
  2656. #if FEATURE_NO_BSD_SOCKETS
  2657. [ExpectedException (typeof (PlatformNotSupportedException))]
  2658. #endif
  2659. public void ReceiveFrom1_Socket_Closed ()
  2660. {
  2661. Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
  2662. ProtocolType.Tcp);
  2663. s.Close ();
  2664. EndPoint remoteEP = new IPEndPoint (IPAddress.Loopback, NetworkHelpers.FindFreePort ());
  2665. try {
  2666. s.ReceiveFrom ((Byte []) null, ref remoteEP);
  2667. Assert.Fail ("#1");
  2668. } catch (ObjectDisposedException ex) {
  2669. // Cannot access a disposed object
  2670. Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
  2671. Assert.IsNull (ex.InnerException, "#3");
  2672. Assert.IsNotNull (ex.Message, "#4");
  2673. Assert.AreEqual (typeof (Socket).FullName, ex.ObjectName, "#5");
  2674. }
  2675. }
  2676. [Test] // ReceiveFrom (Byte [], SocketFlags, ref EndPoint)
  2677. #if FEATURE_NO_BSD_SOCKETS
  2678. [ExpectedException (typeof (PlatformNotSupportedException))]
  2679. #endif
  2680. public void ReceiveFrom2_Buffer_Null ()
  2681. {
  2682. Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
  2683. ProtocolType.Tcp);
  2684. EndPoint remoteEP = new IPEndPoint (IPAddress.Loopback, NetworkHelpers.FindFreePort ());
  2685. try {
  2686. s.ReceiveFrom ((Byte []) null, (SocketFlags) 666, ref remoteEP);
  2687. Assert.Fail ("#1");
  2688. } catch (ArgumentNullException ex) {
  2689. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  2690. Assert.IsNull (ex.InnerException, "#3");
  2691. Assert.IsNotNull (ex.Message, "#4");
  2692. Assert.AreEqual ("buffer", ex.ParamName, "#5");
  2693. } finally {
  2694. s.Close ();
  2695. }
  2696. }
  2697. [Test] // ReceiveFrom (Byte [], SocketFlags, ref EndPoint)
  2698. public void ReceiveFrom2_RemoteEP_Null ()
  2699. {
  2700. Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
  2701. ProtocolType.Tcp);
  2702. byte [] buffer = new byte [5];
  2703. EndPoint remoteEP = null;
  2704. try {
  2705. s.ReceiveFrom (buffer, (SocketFlags) 666, ref remoteEP);
  2706. Assert.Fail ("#1");
  2707. } catch (ArgumentNullException ex) {
  2708. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  2709. Assert.IsNull (ex.InnerException, "#3");
  2710. Assert.IsNotNull (ex.Message, "#4");
  2711. Assert.AreEqual ("remoteEP", ex.ParamName, "#5");
  2712. } finally {
  2713. s.Close ();
  2714. }
  2715. }
  2716. [Test] // ReceiveFrom (Byte [], SocketFlags, ref EndPoint)
  2717. #if FEATURE_NO_BSD_SOCKETS
  2718. [ExpectedException (typeof (PlatformNotSupportedException))]
  2719. #endif
  2720. public void ReceiveFrom2_Socket_Closed ()
  2721. {
  2722. Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
  2723. ProtocolType.Tcp);
  2724. s.Close ();
  2725. EndPoint remoteEP = new IPEndPoint (IPAddress.Loopback, NetworkHelpers.FindFreePort ());
  2726. try {
  2727. s.ReceiveFrom ((Byte []) null, (SocketFlags) 666, ref remoteEP);
  2728. Assert.Fail ("#1");
  2729. } catch (ObjectDisposedException ex) {
  2730. // Cannot access a disposed object
  2731. Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
  2732. Assert.IsNull (ex.InnerException, "#3");
  2733. Assert.IsNotNull (ex.Message, "#4");
  2734. Assert.AreEqual (typeof (Socket).FullName, ex.ObjectName, "#5");
  2735. }
  2736. }
  2737. [Test] // ReceiveFrom (Byte [], Int32, SocketFlags, ref EndPoint)
  2738. #if FEATURE_NO_BSD_SOCKETS
  2739. [ExpectedException (typeof (PlatformNotSupportedException))]
  2740. #endif
  2741. public void ReceiveFrom3_Buffer_Null ()
  2742. {
  2743. Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
  2744. ProtocolType.Tcp);
  2745. EndPoint remoteEP = new IPEndPoint (IPAddress.Loopback, NetworkHelpers.FindFreePort ());
  2746. try {
  2747. s.ReceiveFrom ((Byte []) null, 0, (SocketFlags) 666,
  2748. ref remoteEP);
  2749. Assert.Fail ("#1");
  2750. } catch (ArgumentNullException ex) {
  2751. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  2752. Assert.IsNull (ex.InnerException, "#3");
  2753. Assert.IsNotNull (ex.Message, "#4");
  2754. Assert.AreEqual ("buffer", ex.ParamName, "#5");
  2755. } finally {
  2756. s.Close ();
  2757. }
  2758. }
  2759. [Test] // ReceiveFrom (Byte [], Int32, SocketFlags, ref EndPoint)
  2760. public void ReceiveFrom3_RemoteEP_Null ()
  2761. {
  2762. Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
  2763. ProtocolType.Tcp);
  2764. byte [] buffer = new byte [5];
  2765. EndPoint remoteEP = null;
  2766. try {
  2767. s.ReceiveFrom (buffer, buffer.Length, (SocketFlags) 666, ref remoteEP);
  2768. Assert.Fail ("#1");
  2769. } catch (ArgumentNullException ex) {
  2770. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  2771. Assert.IsNull (ex.InnerException, "#3");
  2772. Assert.IsNotNull (ex.Message, "#4");
  2773. Assert.AreEqual ("remoteEP", ex.ParamName, "#5");
  2774. } finally {
  2775. s.Close ();
  2776. }
  2777. }
  2778. [Test] // ReceiveFrom (Byte [], Int32, SocketFlags, ref EndPoint)
  2779. #if FEATURE_NO_BSD_SOCKETS
  2780. [ExpectedException (typeof (PlatformNotSupportedException))]
  2781. #endif
  2782. public void ReceiveFrom3_Size_OutOfRange ()
  2783. {
  2784. Socket s;
  2785. byte [] buffer = new byte [5];
  2786. EndPoint remoteEP = new IPEndPoint (IPAddress.Loopback, NetworkHelpers.FindFreePort ());
  2787. // size negative
  2788. s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
  2789. ProtocolType.Tcp);
  2790. try {
  2791. s.ReceiveFrom (buffer, -1, (SocketFlags) 666, ref remoteEP);
  2792. Assert.Fail ("#A1");
  2793. } catch (ArgumentOutOfRangeException ex) {
  2794. // Specified argument was out of the range of valid values
  2795. Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#A2");
  2796. Assert.IsNull (ex.InnerException, "#A3");
  2797. Assert.IsNotNull (ex.Message, "#A4");
  2798. Assert.AreEqual ("size", ex.ParamName, "#A5");
  2799. } finally {
  2800. s.Close ();
  2801. }
  2802. // size > buffer length
  2803. s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
  2804. ProtocolType.Tcp);
  2805. try {
  2806. s.ReceiveFrom (buffer, (buffer.Length + 1), (SocketFlags) 666,
  2807. ref remoteEP);
  2808. Assert.Fail ("#B1");
  2809. } catch (ArgumentOutOfRangeException ex) {
  2810. // Specified argument was out of the range of valid values
  2811. Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#B2");
  2812. Assert.IsNull (ex.InnerException, "#B3");
  2813. Assert.IsNotNull (ex.Message, "#B4");
  2814. Assert.AreEqual ("size", ex.ParamName, "#B5");
  2815. } finally {
  2816. s.Close ();
  2817. }
  2818. }
  2819. [Test] // ReceiveFrom (Byte [], Int32, SocketFlags, ref EndPoint)
  2820. #if FEATURE_NO_BSD_SOCKETS
  2821. [ExpectedException (typeof (PlatformNotSupportedException))]
  2822. #endif
  2823. public void ReceiveFrom3_Socket_Closed ()
  2824. {
  2825. Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
  2826. ProtocolType.Tcp);
  2827. s.Close ();
  2828. EndPoint remoteEP = new IPEndPoint (IPAddress.Loopback, NetworkHelpers.FindFreePort ());
  2829. try {
  2830. s.ReceiveFrom ((Byte []) null, -1, (SocketFlags) 666,
  2831. ref remoteEP);
  2832. Assert.Fail ("#1");
  2833. } catch (ObjectDisposedException ex) {
  2834. // Cannot access a disposed object
  2835. Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
  2836. Assert.IsNull (ex.InnerException, "#3");
  2837. Assert.IsNotNull (ex.Message, "#4");
  2838. Assert.AreEqual (typeof (Socket).FullName, ex.ObjectName, "#5");
  2839. }
  2840. }
  2841. [Test] // ReceiveFrom (Byte [], Int32, Int32, SocketFlags, EndPoint)
  2842. #if FEATURE_NO_BSD_SOCKETS
  2843. [ExpectedException (typeof (PlatformNotSupportedException))]
  2844. #endif
  2845. public void ReceiveFrom4_Buffer_Null ()
  2846. {
  2847. Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
  2848. ProtocolType.Tcp);
  2849. EndPoint remoteEP = new IPEndPoint (IPAddress.Loopback, NetworkHelpers.FindFreePort ());
  2850. try {
  2851. s.ReceiveFrom ((Byte []) null, -1, -1, (SocketFlags) 666,
  2852. ref remoteEP);
  2853. Assert.Fail ("#1");
  2854. } catch (ArgumentNullException ex) {
  2855. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  2856. Assert.IsNull (ex.InnerException, "#3");
  2857. Assert.IsNotNull (ex.Message, "#4");
  2858. Assert.AreEqual ("buffer", ex.ParamName, "#5");
  2859. }
  2860. }
  2861. [Test] // ReceiveFrom (Byte [], Int32, Int32, SocketFlags, EndPoint)
  2862. #if FEATURE_NO_BSD_SOCKETS
  2863. [ExpectedException (typeof (PlatformNotSupportedException))]
  2864. #endif
  2865. public void ReceiveFrom4_Offset_OutOfRange ()
  2866. {
  2867. Socket s;
  2868. byte [] buffer = new byte [5];
  2869. EndPoint remoteEP = new IPEndPoint (IPAddress.Loopback, NetworkHelpers.FindFreePort ());
  2870. // offset negative
  2871. s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
  2872. ProtocolType.Tcp);
  2873. try {
  2874. s.ReceiveFrom (buffer, -1, 0, (SocketFlags) 666,
  2875. ref remoteEP);
  2876. Assert.Fail ("#A1");
  2877. } catch (ArgumentOutOfRangeException ex) {
  2878. // Specified argument was out of the range of valid values
  2879. Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#A2");
  2880. Assert.IsNull (ex.InnerException, "#A3");
  2881. Assert.IsNotNull (ex.Message, "#A4");
  2882. Assert.AreEqual ("offset", ex.ParamName, "#A5");
  2883. } finally {
  2884. s.Close ();
  2885. }
  2886. // offset > buffer length
  2887. s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
  2888. ProtocolType.Tcp);
  2889. try {
  2890. s.ReceiveFrom (buffer, (buffer.Length + 1), 0, (SocketFlags) 666,
  2891. ref remoteEP);
  2892. Assert.Fail ("#B1");
  2893. } catch (ArgumentOutOfRangeException ex) {
  2894. // Specified argument was out of the range of valid values
  2895. Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#B2");
  2896. Assert.IsNull (ex.InnerException, "#B3");
  2897. Assert.IsNotNull (ex.Message, "#B4");
  2898. Assert.AreEqual ("offset", ex.ParamName, "#B5");
  2899. } finally {
  2900. s.Close ();
  2901. }
  2902. }
  2903. [Test] // ReceiveFrom (Byte [], Int32, Int32, SocketFlags, ref IPEndPoint)
  2904. public void ReceiveFrom4_RemoteEP_Null ()
  2905. {
  2906. Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
  2907. ProtocolType.Tcp);
  2908. byte [] buffer = new byte [5];
  2909. EndPoint remoteEP = null;
  2910. try {
  2911. s.ReceiveFrom (buffer, 0, buffer.Length, (SocketFlags) 666, ref remoteEP);
  2912. Assert.Fail ("#1");
  2913. } catch (ArgumentNullException ex) {
  2914. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  2915. Assert.IsNull (ex.InnerException, "#3");
  2916. Assert.IsNotNull (ex.Message, "#4");
  2917. Assert.AreEqual ("remoteEP", ex.ParamName, "#5");
  2918. } finally {
  2919. s.Close ();
  2920. }
  2921. }
  2922. [Test] // ReceiveFrom (Byte [], Int32, Int32, SocketFlags, EndPoint)
  2923. #if FEATURE_NO_BSD_SOCKETS
  2924. [ExpectedException (typeof (PlatformNotSupportedException))]
  2925. #endif
  2926. public void ReceiveFrom4_Size_OutOfRange ()
  2927. {
  2928. Socket s;
  2929. byte [] buffer = new byte [5];
  2930. EndPoint remoteEP = new IPEndPoint (IPAddress.Loopback, NetworkHelpers.FindFreePort ());
  2931. // size negative
  2932. s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
  2933. ProtocolType.Tcp);
  2934. try {
  2935. s.ReceiveFrom (buffer, 0, -1, (SocketFlags) 666,
  2936. ref remoteEP);
  2937. Assert.Fail ("#A1");
  2938. } catch (ArgumentOutOfRangeException ex) {
  2939. // Specified argument was out of the range of valid values
  2940. Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#A2");
  2941. Assert.IsNull (ex.InnerException, "#A3");
  2942. Assert.IsNotNull (ex.Message, "#A4");
  2943. Assert.AreEqual ("size", ex.ParamName, "#A5");
  2944. } finally {
  2945. s.Close ();
  2946. }
  2947. // size > buffer length
  2948. s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
  2949. ProtocolType.Tcp);
  2950. try {
  2951. s.ReceiveFrom (buffer, 0, (buffer.Length + 1), (SocketFlags) 666,
  2952. ref remoteEP);
  2953. Assert.Fail ("#B1");
  2954. } catch (ArgumentOutOfRangeException ex) {
  2955. // Specified argument was out of the range of valid values
  2956. Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#B2");
  2957. Assert.IsNull (ex.InnerException, "#B3");
  2958. Assert.IsNotNull (ex.Message, "#B4");
  2959. Assert.AreEqual ("size", ex.ParamName, "#B5");
  2960. } finally {
  2961. s.Close ();
  2962. }
  2963. // offset + size > buffer length
  2964. s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
  2965. ProtocolType.Tcp);
  2966. try {
  2967. s.ReceiveFrom (buffer, 2, 4, (SocketFlags) 666, ref remoteEP);
  2968. Assert.Fail ("#C1");
  2969. } catch (ArgumentOutOfRangeException ex) {
  2970. // Specified argument was out of the range of valid values
  2971. Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#C2");
  2972. Assert.IsNull (ex.InnerException, "#C3");
  2973. Assert.IsNotNull (ex.Message, "#C4");
  2974. Assert.AreEqual ("size", ex.ParamName, "#C5");
  2975. } finally {
  2976. s.Close ();
  2977. }
  2978. }
  2979. [Test] // ReceiveFrom (Byte [], Int32, Int32, SocketFlags, ref EndPoint)
  2980. #if FEATURE_NO_BSD_SOCKETS
  2981. [ExpectedException (typeof (PlatformNotSupportedException))]
  2982. #endif
  2983. public void ReceiveFrom4_Socket_Closed ()
  2984. {
  2985. Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
  2986. ProtocolType.Tcp);
  2987. s.Close ();
  2988. byte [] buffer = new byte [5];
  2989. EndPoint remoteEP = new IPEndPoint (IPAddress.Loopback, NetworkHelpers.FindFreePort ());
  2990. try {
  2991. s.ReceiveFrom (buffer, -1, -1, (SocketFlags) 666,
  2992. ref remoteEP);
  2993. Assert.Fail ("#1");
  2994. } catch (ObjectDisposedException ex) {
  2995. // Cannot access a disposed object
  2996. Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
  2997. Assert.IsNull (ex.InnerException, "#3");
  2998. Assert.IsNotNull (ex.Message, "#4");
  2999. Assert.AreEqual (typeof (Socket).FullName, ex.ObjectName, "#5");
  3000. }
  3001. }
  3002. [Test]
  3003. #if FEATURE_NO_BSD_SOCKETS
  3004. [ExpectedException (typeof (PlatformNotSupportedException))]
  3005. #endif
  3006. public void ReceiveRemoteClosed ()
  3007. {
  3008. var port = NetworkHelpers.FindFreePort ();
  3009. Socket sock = new Socket (AddressFamily.InterNetwork,
  3010. SocketType.Stream,
  3011. ProtocolType.Tcp);
  3012. sock.Bind (new IPEndPoint (IPAddress.Loopback, port));
  3013. sock.Listen (1);
  3014. RRCReady.Reset ();
  3015. Thread client_thread = new Thread (() => RRCClientThread (port));
  3016. client_thread.Start ();
  3017. Socket client = sock.Accept ();
  3018. NetworkStream stream = new NetworkStream (client);
  3019. stream.WriteByte (0x00);
  3020. stream.WriteByte (0x00);
  3021. client.Close ();
  3022. sock.Close ();
  3023. RRCReady.WaitOne (1000, false);
  3024. Assert.IsTrue (RRCLastRead);
  3025. }
  3026. //
  3027. // Test case for bug #471580
  3028. [Test]
  3029. #if FEATURE_NO_BSD_SOCKETS
  3030. [ExpectedException (typeof (PlatformNotSupportedException))]
  3031. #endif
  3032. public void UdpDoubleBind ()
  3033. {
  3034. using (Socket s = new Socket (AddressFamily.InterNetwork,
  3035. SocketType.Dgram, ProtocolType.Udp))
  3036. using (Socket ss = new Socket (AddressFamily.InterNetwork,
  3037. SocketType.Dgram, ProtocolType.Udp)) {
  3038. var supportsReuseAddress = true;
  3039. try {
  3040. s.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
  3041. } catch (SocketException e) {
  3042. // Exception is thrown when ReuseAddress is not supported
  3043. Assert.AreEqual ((int) SocketError.OperationNotSupported, e.NativeErrorCode,
  3044. "Expected SocketError.OperationNotSupported");
  3045. supportsReuseAddress = false;
  3046. }
  3047. var ep = new IPEndPoint (IPAddress.Any, NetworkHelpers.FindFreePort ());
  3048. s.Bind (ep);
  3049. if (supportsReuseAddress)
  3050. ss.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
  3051. try {
  3052. ss.Bind (new IPEndPoint (IPAddress.Any, ep.Port));
  3053. if (!supportsReuseAddress)
  3054. Assert.Fail ("Reusing address is not supported, exception was expected on second bind.");
  3055. } catch (SocketException e) {
  3056. }
  3057. }
  3058. }
  3059. // Test case for bug #31557
  3060. [Test]
  3061. #if FEATURE_NO_BSD_SOCKETS
  3062. [ExpectedException (typeof (PlatformNotSupportedException))]
  3063. #endif
  3064. public void TcpDoubleBind ()
  3065. {
  3066. using (Socket s = new Socket (AddressFamily.InterNetwork,
  3067. SocketType.Stream, ProtocolType.Tcp))
  3068. using (Socket ss = new Socket (AddressFamily.InterNetwork,
  3069. SocketType.Stream, ProtocolType.Tcp)) {
  3070. var supportsReuseAddress = true;
  3071. try {
  3072. s.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
  3073. } catch (SocketException e) {
  3074. // Exception is thrown when ReuseAddress is not supported
  3075. Assert.AreEqual ((int) SocketError.OperationNotSupported, e.NativeErrorCode,
  3076. "Expected SocketError.OperationNotSupported");
  3077. supportsReuseAddress = false;
  3078. }
  3079. var ep = new IPEndPoint (IPAddress.Any, NetworkHelpers.FindFreePort ());
  3080. s.Bind (ep);
  3081. s.Listen(1);
  3082. if (supportsReuseAddress)
  3083. ss.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
  3084. try {
  3085. ss.Bind (new IPEndPoint (IPAddress.Any, ep.Port));
  3086. ss.Listen(1);
  3087. if (!supportsReuseAddress)
  3088. Assert.Fail ("Reusing address is not supported, exception was expected on second bind.");
  3089. } catch (SocketException e) {
  3090. }
  3091. }
  3092. }
  3093. [Test]
  3094. [Category ("NotOnMac")]
  3095. public void ConnectedProperty ()
  3096. {
  3097. TcpListener listener = new TcpListener (IPAddress.Loopback, NetworkHelpers.FindFreePort ());
  3098. listener.Start();
  3099. Socket client = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  3100. client.Connect (IPAddress.Loopback, ((IPEndPoint)listener.LocalEndpoint).Port);
  3101. Socket server = listener.AcceptSocket ();
  3102. try {
  3103. server.EndSend(server.BeginSend (new byte[10], 0, 10, SocketFlags.None, null, null));
  3104. client.Close ();
  3105. try {
  3106. server.EndReceive (server.BeginReceive (new byte[10], 0, 10, SocketFlags.None, null, null));
  3107. } catch {
  3108. }
  3109. Assert.IsTrue (!client.Connected);
  3110. Assert.IsTrue (!server.Connected);
  3111. } finally {
  3112. listener.Stop ();
  3113. client.Close ();
  3114. server.Close ();
  3115. }
  3116. }
  3117. [Test] // GetSocketOption (SocketOptionLevel, SocketOptionName)
  3118. public void GetSocketOption1_Socket_Closed ()
  3119. {
  3120. Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  3121. s.Close ();
  3122. try {
  3123. s.GetSocketOption (0, 0);
  3124. Assert.Fail ("#1");
  3125. } catch (ObjectDisposedException ex) {
  3126. // Cannot access a disposed object
  3127. Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
  3128. Assert.IsNull (ex.InnerException, "#3");
  3129. Assert.IsNotNull (ex.Message, "#4");
  3130. Assert.AreEqual (typeof (Socket).FullName, ex.ObjectName, "#5");
  3131. }
  3132. }
  3133. [Test] // GetSocketOption (SocketOptionLevel, SocketOptionName, Byte [])
  3134. public void GetSocketOption2_OptionValue_Null ()
  3135. {
  3136. Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  3137. try {
  3138. s.GetSocketOption (SocketOptionLevel.Socket, SocketOptionName.Linger,
  3139. (byte []) null);
  3140. Assert.Fail ("#1");
  3141. } catch (SocketException ex) {
  3142. // The system detected an invalid pointer address in attempting
  3143. // to use a pointer argument in a call
  3144. Assert.AreEqual (typeof (SocketException), ex.GetType (), "#2");
  3145. Assert.AreEqual (10014, ex.ErrorCode, "#3");
  3146. Assert.IsNull (ex.InnerException, "#4");
  3147. Assert.IsNotNull (ex.Message, "#5");
  3148. Assert.AreEqual (10014, ex.NativeErrorCode, "#6");
  3149. Assert.AreEqual (SocketError.Fault, ex.SocketErrorCode, "#7");
  3150. }
  3151. }
  3152. [Test] // GetSocketOption (SocketOptionLevel, SocketOptionName, Byte [])
  3153. public void GetSocketOption2_Socket_Closed ()
  3154. {
  3155. Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  3156. s.Close ();
  3157. try {
  3158. s.GetSocketOption (0, 0, (byte []) null);
  3159. Assert.Fail ("#1");
  3160. } catch (ObjectDisposedException ex) {
  3161. // Cannot access a disposed object
  3162. Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
  3163. Assert.IsNull (ex.InnerException, "#3");
  3164. Assert.IsNotNull (ex.Message, "#4");
  3165. Assert.AreEqual (typeof (Socket).FullName, ex.ObjectName, "#5");
  3166. }
  3167. }
  3168. [Test] // GetSocketOption (SocketOptionLevel, SocketOptionName, Int32)
  3169. public void GetSocketOption3_Socket_Closed ()
  3170. {
  3171. Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  3172. s.Close ();
  3173. try {
  3174. s.GetSocketOption (0, 0, 0);
  3175. Assert.Fail ("#1");
  3176. } catch (ObjectDisposedException ex) {
  3177. // Cannot access a disposed object
  3178. Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
  3179. Assert.IsNull (ex.InnerException, "#3");
  3180. Assert.IsNotNull (ex.Message, "#4");
  3181. Assert.AreEqual (typeof (Socket).FullName, ex.ObjectName, "#5");
  3182. }
  3183. }
  3184. [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Byte [])
  3185. public void SetSocketOption1_DontLinger ()
  3186. {
  3187. using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) {
  3188. s.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.DontLinger,
  3189. new byte [] { 0x00 });
  3190. s.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.DontLinger,
  3191. new byte [] { 0x01 });
  3192. }
  3193. }
  3194. [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Byte [])
  3195. public void SetSocketOption1_DontLinger_Null ()
  3196. {
  3197. using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) {
  3198. try {
  3199. s.SetSocketOption (SocketOptionLevel.Socket,
  3200. SocketOptionName.DontLinger, (byte []) null);
  3201. Assert.Fail ("#1");
  3202. } catch (SocketException ex) {
  3203. // The system detected an invalid pointer address in attempting
  3204. // to use a pointer argument in a call
  3205. Assert.AreEqual (typeof (SocketException), ex.GetType (), "#2");
  3206. Assert.AreEqual (10014, ex.ErrorCode, "#3");
  3207. Assert.IsNull (ex.InnerException, "#4");
  3208. Assert.IsNotNull (ex.Message, "#5");
  3209. Assert.AreEqual (10014, ex.NativeErrorCode, "#6");
  3210. Assert.AreEqual (SocketError.Fault, ex.SocketErrorCode, "#7");
  3211. }
  3212. }
  3213. }
  3214. [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Byte [])
  3215. public void SetSocketOption1_Linger_Null ()
  3216. {
  3217. using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) {
  3218. try {
  3219. s.SetSocketOption (SocketOptionLevel.Socket,
  3220. SocketOptionName.DontLinger, (byte []) null);
  3221. Assert.Fail ("#1");
  3222. } catch (SocketException ex) {
  3223. // The system detected an invalid pointer address in attempting
  3224. // to use a pointer argument in a call
  3225. Assert.AreEqual (typeof (SocketException), ex.GetType (), "#2");
  3226. Assert.AreEqual (10014, ex.ErrorCode, "#3");
  3227. Assert.IsNull (ex.InnerException, "#4");
  3228. Assert.IsNotNull (ex.Message, "#5");
  3229. Assert.AreEqual (10014, ex.NativeErrorCode, "#6");
  3230. Assert.AreEqual (SocketError.Fault, ex.SocketErrorCode, "#7");
  3231. }
  3232. }
  3233. }
  3234. [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Byte [])
  3235. public void SetSocketOption1_Socket_Close ()
  3236. {
  3237. Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  3238. s.Close ();
  3239. try {
  3240. s.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.DontLinger,
  3241. new byte [] { 0x00 });
  3242. Assert.Fail ("#1");
  3243. } catch (ObjectDisposedException ex) {
  3244. Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
  3245. Assert.IsNull (ex.InnerException, "#3");
  3246. Assert.IsNotNull (ex.Message, "#4");
  3247. Assert.AreEqual (typeof (Socket).FullName, ex.ObjectName, "#5");
  3248. }
  3249. }
  3250. [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Int32)
  3251. public void SetSocketOption2_DontLinger ()
  3252. {
  3253. using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) {
  3254. s.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.DontLinger, 0);
  3255. s.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.DontLinger, 5);
  3256. }
  3257. }
  3258. [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Int32)
  3259. [Category ("NotWorking")]
  3260. public void SetSocketOption2_Linger ()
  3261. {
  3262. using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) {
  3263. s.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.Linger, 0);
  3264. s.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.Linger, 5);
  3265. }
  3266. }
  3267. [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Int32)
  3268. public void SetSocketOption2_Socket_Closed ()
  3269. {
  3270. Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  3271. s.Close ();
  3272. try {
  3273. s.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.DontLinger, 0);
  3274. Assert.Fail ("#1");
  3275. } catch (ObjectDisposedException ex) {
  3276. // Cannot access a disposed object
  3277. Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
  3278. Assert.IsNull (ex.InnerException, "#3");
  3279. Assert.IsNotNull (ex.Message, "#4");
  3280. Assert.AreEqual (typeof (Socket).FullName, ex.ObjectName, "#5");
  3281. }
  3282. }
  3283. [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Object)
  3284. #if FEATURE_NO_BSD_SOCKETS
  3285. [ExpectedException (typeof (PlatformNotSupportedException))]
  3286. #endif
  3287. public void SetSocketOption3_AddMembershipIPv4_IPv6MulticastOption ()
  3288. {
  3289. IPAddress mcast_addr = IPAddress.Parse ("239.255.255.250");
  3290. using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp)) {
  3291. s.Bind (new IPEndPoint (IPAddress.Any, NetworkHelpers.FindFreePort ()));
  3292. try {
  3293. s.SetSocketOption (SocketOptionLevel.IP, SocketOptionName.AddMembership,
  3294. new IPv6MulticastOption (mcast_addr));
  3295. Assert.Fail ("#1");
  3296. } catch (ArgumentException ex) {
  3297. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
  3298. Assert.IsNull (ex.InnerException, "#3");
  3299. Assert.IsNotNull (ex.Message, "#4");
  3300. // The specified value is not a valid 'MulticastOption'
  3301. Assert.IsTrue (ex.Message.IndexOf ("'MulticastOption'") != -1, "#5:" + ex.Message);
  3302. Assert.AreEqual ("optionValue", ex.ParamName, "#6");
  3303. }
  3304. }
  3305. }
  3306. [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Object)
  3307. #if FEATURE_NO_BSD_SOCKETS
  3308. [ExpectedException (typeof (PlatformNotSupportedException))]
  3309. #endif
  3310. public void SetSocketOption3_AddMembershipIPv4_MulticastOption ()
  3311. {
  3312. IPAddress mcast_addr = IPAddress.Parse ("239.255.255.250");
  3313. using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp)) {
  3314. s.Bind (new IPEndPoint (IPAddress.Any, NetworkHelpers.FindFreePort ()));
  3315. s.SetSocketOption (SocketOptionLevel.IP, SocketOptionName.AddMembership,
  3316. new MulticastOption (mcast_addr));
  3317. }
  3318. }
  3319. [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Object)
  3320. [Category ("NotWorking")]
  3321. public void SetSocketOption3_AddMembershipIPv4_Socket_NotBound ()
  3322. {
  3323. IPAddress mcast_addr = IPAddress.Parse ("239.255.255.250");
  3324. Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
  3325. try {
  3326. s.SetSocketOption (SocketOptionLevel.IP, SocketOptionName.AddMembership,
  3327. new MulticastOption (mcast_addr));
  3328. Assert.Fail ("#1");
  3329. } catch (SocketException ex) {
  3330. // An invalid argument was supplied
  3331. Assert.AreEqual (typeof (SocketException), ex.GetType (), "#2");
  3332. Assert.AreEqual (10022, ex.ErrorCode, "#3");
  3333. Assert.IsNull (ex.InnerException, "#4");
  3334. Assert.IsNotNull (ex.Message, "#5");
  3335. Assert.AreEqual (10022, ex.NativeErrorCode, "#6");
  3336. Assert.AreEqual (SocketError.InvalidArgument, ex.SocketErrorCode, "#7");
  3337. } finally {
  3338. s.Close ();
  3339. }
  3340. }
  3341. [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Object)
  3342. #if FEATURE_NO_BSD_SOCKETS
  3343. [ExpectedException (typeof (PlatformNotSupportedException))]
  3344. #endif
  3345. public void SetSocketOption3_AddMembershipIPv6_IPv6MulticastOption ()
  3346. {
  3347. if (!Socket.OSSupportsIPv6)
  3348. Assert.Ignore ("IPv6 not enabled.");
  3349. IPAddress mcast_addr = IPAddress.Parse ("ff02::1");
  3350. using (Socket s = new Socket (AddressFamily.InterNetworkV6, SocketType.Dgram, ProtocolType.Udp)) {
  3351. s.Bind (new IPEndPoint (IPAddress.IPv6Any, NetworkHelpers.FindFreePort ()));
  3352. s.SetSocketOption (SocketOptionLevel.IPv6, SocketOptionName.AddMembership,
  3353. new IPv6MulticastOption (mcast_addr));
  3354. }
  3355. }
  3356. [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Object)
  3357. #if FEATURE_NO_BSD_SOCKETS
  3358. [ExpectedException (typeof (PlatformNotSupportedException))]
  3359. #endif
  3360. public void SetSocketOption3_AddMembershipIPv6_MulticastOption ()
  3361. {
  3362. if (!Socket.OSSupportsIPv6)
  3363. Assert.Ignore ("IPv6 not enabled.");
  3364. IPAddress mcast_addr = IPAddress.Parse ("ff02::1");
  3365. using (Socket s = new Socket (AddressFamily.InterNetworkV6, SocketType.Dgram, ProtocolType.Udp)) {
  3366. s.Bind (new IPEndPoint (IPAddress.IPv6Any, NetworkHelpers.FindFreePort ()));
  3367. try {
  3368. s.SetSocketOption (SocketOptionLevel.IPv6, SocketOptionName.AddMembership,
  3369. new MulticastOption (mcast_addr));
  3370. Assert.Fail ("#1");
  3371. } catch (ArgumentException ex) {
  3372. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
  3373. Assert.IsNull (ex.InnerException, "#3");
  3374. Assert.IsNotNull (ex.Message, "#4");
  3375. // The specified value is not a valid 'IPv6MulticastOption'
  3376. Assert.IsTrue (ex.Message.IndexOf ("'IPv6MulticastOption'") != -1, "#5:" + ex.Message);
  3377. Assert.AreEqual ("optionValue", ex.ParamName, "#6");
  3378. }
  3379. }
  3380. }
  3381. [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Object)
  3382. [Category ("NotWorking")]
  3383. public void SetSocketOption3_AddMembershipIPv6_Socket_NotBound ()
  3384. {
  3385. IPAddress mcast_addr = IPAddress.Parse ("ff02::1");
  3386. Socket s = new Socket (AddressFamily.InterNetworkV6, SocketType.Dgram, ProtocolType.Udp);
  3387. try {
  3388. s.SetSocketOption (SocketOptionLevel.IPv6, SocketOptionName.AddMembership,
  3389. new IPv6MulticastOption (mcast_addr));
  3390. Assert.Fail ("#1");
  3391. } catch (SocketException ex) {
  3392. // An invalid argument was supplied
  3393. Assert.AreEqual (typeof (SocketException), ex.GetType (), "#2");
  3394. Assert.AreEqual (10022, ex.ErrorCode, "#3");
  3395. Assert.IsNull (ex.InnerException, "#4");
  3396. Assert.IsNotNull (ex.Message, "#5");
  3397. Assert.AreEqual (10022, ex.NativeErrorCode, "#6");
  3398. Assert.AreEqual (SocketError.InvalidArgument, ex.SocketErrorCode, "#7");
  3399. } finally {
  3400. s.Close ();
  3401. }
  3402. }
  3403. [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Object)
  3404. public void SetSocketOption3_DontLinger_Boolean ()
  3405. {
  3406. using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) {
  3407. try {
  3408. s.SetSocketOption (SocketOptionLevel.Socket,
  3409. SocketOptionName.DontLinger, (object) false);
  3410. Assert.Fail ("#1");
  3411. } catch (ArgumentException ex) {
  3412. // The specified value is not valid
  3413. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
  3414. Assert.IsNull (ex.InnerException, "#3");
  3415. Assert.IsNotNull (ex.Message, "#4");
  3416. Assert.AreEqual ("optionValue", ex.ParamName, "#5");
  3417. }
  3418. }
  3419. }
  3420. [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Object)
  3421. public void SetSocketOption3_DontLinger_Int32 ()
  3422. {
  3423. using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) {
  3424. try {
  3425. s.SetSocketOption (SocketOptionLevel.Socket,
  3426. SocketOptionName.DontLinger, (object) 0);
  3427. Assert.Fail ("#1");
  3428. } catch (ArgumentException ex) {
  3429. // The specified value is not valid
  3430. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
  3431. Assert.IsNull (ex.InnerException, "#3");
  3432. Assert.IsNotNull (ex.Message, "#4");
  3433. Assert.AreEqual ("optionValue", ex.ParamName, "#5");
  3434. }
  3435. }
  3436. }
  3437. [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Object)
  3438. public void SetSocketOption3_DontLinger_LingerOption ()
  3439. {
  3440. using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) {
  3441. try {
  3442. s.SetSocketOption (SocketOptionLevel.Socket,
  3443. SocketOptionName.DontLinger, new LingerOption (true, 1000));
  3444. Assert.Fail ("#1");
  3445. } catch (ArgumentException ex) {
  3446. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
  3447. Assert.IsNull (ex.InnerException, "#3");
  3448. // The specified value is not valid
  3449. Assert.IsNotNull (ex.Message, "#4");
  3450. Assert.AreEqual ("optionValue", ex.ParamName, "#5");
  3451. }
  3452. }
  3453. }
  3454. [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Object)
  3455. public void SetSocketOption3_Linger_Boolean ()
  3456. {
  3457. using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) {
  3458. try {
  3459. s.SetSocketOption (SocketOptionLevel.Socket,
  3460. SocketOptionName.Linger, (object) false);
  3461. Assert.Fail ("#1");
  3462. } catch (ArgumentException ex) {
  3463. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
  3464. Assert.IsNull (ex.InnerException, "#3");
  3465. // The specified value is not valid
  3466. Assert.IsNotNull (ex.Message, "#4");
  3467. Assert.AreEqual ("optionValue", ex.ParamName, "#5");
  3468. }
  3469. }
  3470. }
  3471. [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Object)
  3472. public void SetSocketOption3_Linger_Int32 ()
  3473. {
  3474. using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) {
  3475. try {
  3476. s.SetSocketOption (SocketOptionLevel.Socket,
  3477. SocketOptionName.Linger, (object) 0);
  3478. Assert.Fail ("#1");
  3479. } catch (ArgumentException ex) {
  3480. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
  3481. Assert.IsNull (ex.InnerException, "#3");
  3482. // The specified value is not valid
  3483. Assert.IsNotNull (ex.Message, "#4");
  3484. Assert.AreEqual ("optionValue", ex.ParamName, "#5");
  3485. }
  3486. }
  3487. }
  3488. [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Object)
  3489. public void SetSocketOption3_Linger_LingerOption ()
  3490. {
  3491. using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) {
  3492. s.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.Linger,
  3493. new LingerOption (false, 0));
  3494. s.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.Linger,
  3495. new LingerOption (true, 0));
  3496. s.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.Linger,
  3497. new LingerOption (false, 1000));
  3498. s.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.Linger,
  3499. new LingerOption (true, 1000));
  3500. }
  3501. }
  3502. [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Object)
  3503. #if FEATURE_NO_BSD_SOCKETS
  3504. [ExpectedException (typeof (PlatformNotSupportedException))]
  3505. #endif
  3506. public void SetSocketOption3_DropMembershipIPv4_IPv6MulticastOption ()
  3507. {
  3508. IPAddress mcast_addr = IPAddress.Parse ("239.255.255.250");
  3509. using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp)) {
  3510. s.Bind (new IPEndPoint (IPAddress.Any, NetworkHelpers.FindFreePort ()));
  3511. s.SetSocketOption (SocketOptionLevel.IP, SocketOptionName.AddMembership,
  3512. new MulticastOption (mcast_addr));
  3513. try {
  3514. s.SetSocketOption (SocketOptionLevel.IP, SocketOptionName.DropMembership,
  3515. new IPv6MulticastOption (mcast_addr));
  3516. Assert.Fail ("#1");
  3517. } catch (ArgumentException ex) {
  3518. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
  3519. Assert.IsNull (ex.InnerException, "#3");
  3520. Assert.IsNotNull (ex.Message, "#4");
  3521. // The specified value is not a valid 'MulticastOption'
  3522. Assert.IsTrue (ex.Message.IndexOf ("'MulticastOption'") != -1, "#5:" + ex.Message);
  3523. Assert.AreEqual ("optionValue", ex.ParamName, "#6");
  3524. }
  3525. }
  3526. }
  3527. [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Object)
  3528. #if FEATURE_NO_BSD_SOCKETS
  3529. [ExpectedException (typeof (PlatformNotSupportedException))]
  3530. #endif
  3531. public void SetSocketOption3_DropMembershipIPv4_MulticastOption ()
  3532. {
  3533. IPAddress mcast_addr = IPAddress.Parse ("239.255.255.250");
  3534. using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp)) {
  3535. MulticastOption option = new MulticastOption (mcast_addr);
  3536. s.Bind (new IPEndPoint (IPAddress.Any, NetworkHelpers.FindFreePort ()));
  3537. s.SetSocketOption (SocketOptionLevel.IP, SocketOptionName.AddMembership,
  3538. option);
  3539. s.SetSocketOption (SocketOptionLevel.IP, SocketOptionName.DropMembership,
  3540. option);
  3541. }
  3542. }
  3543. [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Object)
  3544. [Category ("NotWorking")]
  3545. public void SetSocketOption3_DropMembershipIPv4_Socket_NotBound ()
  3546. {
  3547. IPAddress mcast_addr = IPAddress.Parse ("239.255.255.250");
  3548. Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
  3549. try {
  3550. s.SetSocketOption (SocketOptionLevel.IP, SocketOptionName.DropMembership,
  3551. new MulticastOption (mcast_addr));
  3552. Assert.Fail ("#1");
  3553. } catch (SocketException ex) {
  3554. // An invalid argument was supplied
  3555. Assert.AreEqual (typeof (SocketException), ex.GetType (), "#2");
  3556. Assert.AreEqual (10022, ex.ErrorCode, "#3");
  3557. Assert.IsNull (ex.InnerException, "#4");
  3558. Assert.IsNotNull (ex.Message, "#5");
  3559. Assert.AreEqual (10022, ex.NativeErrorCode, "#6");
  3560. Assert.AreEqual (SocketError.InvalidArgument, ex.SocketErrorCode, "#7");
  3561. } finally {
  3562. s.Close ();
  3563. }
  3564. }
  3565. [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Object)
  3566. #if FEATURE_NO_BSD_SOCKETS
  3567. [ExpectedException (typeof (PlatformNotSupportedException))]
  3568. #endif
  3569. public void SetSocketOption3_DropMembershipIPv6_IPv6MulticastOption ()
  3570. {
  3571. if (!Socket.OSSupportsIPv6)
  3572. Assert.Ignore ("IPv6 not enabled.");
  3573. using (Socket s = new Socket (AddressFamily.InterNetworkV6, SocketType.Dgram, ProtocolType.Udp)) {
  3574. IPv6MulticastOption option = new IPv6MulticastOption (
  3575. IPAddress.Parse ("ff02::1"));
  3576. s.Bind (new IPEndPoint (IPAddress.IPv6Any, 1902));
  3577. s.SetSocketOption (SocketOptionLevel.IPv6, SocketOptionName.AddMembership,
  3578. option);
  3579. s.SetSocketOption (SocketOptionLevel.IPv6, SocketOptionName.DropMembership,
  3580. option);
  3581. }
  3582. }
  3583. [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Object)
  3584. #if FEATURE_NO_BSD_SOCKETS
  3585. [ExpectedException (typeof (PlatformNotSupportedException))]
  3586. #endif
  3587. public void SetSocketOption3_DropMembershipIPv6_MulticastOption ()
  3588. {
  3589. if (!Socket.OSSupportsIPv6)
  3590. Assert.Ignore ("IPv6 not enabled.");
  3591. IPAddress mcast_addr = IPAddress.Parse ("ff02::1");
  3592. using (Socket s = new Socket (AddressFamily.InterNetworkV6, SocketType.Dgram, ProtocolType.Udp)) {
  3593. s.Bind (new IPEndPoint (IPAddress.IPv6Any, NetworkHelpers.FindFreePort ()));
  3594. s.SetSocketOption (SocketOptionLevel.IPv6, SocketOptionName.AddMembership,
  3595. new IPv6MulticastOption (mcast_addr));
  3596. try {
  3597. s.SetSocketOption (SocketOptionLevel.IPv6, SocketOptionName.DropMembership,
  3598. new MulticastOption (mcast_addr));
  3599. Assert.Fail ("#1");
  3600. } catch (ArgumentException ex) {
  3601. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
  3602. Assert.IsNull (ex.InnerException, "#3");
  3603. Assert.IsNotNull (ex.Message, "#4");
  3604. // The specified value is not a valid 'IPv6MulticastOption'
  3605. Assert.IsTrue (ex.Message.IndexOf ("'IPv6MulticastOption'") != -1, "#5:" + ex.Message);
  3606. Assert.AreEqual ("optionValue", ex.ParamName, "#6");
  3607. }
  3608. }
  3609. }
  3610. [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Object)
  3611. [Category ("NotWorking")]
  3612. public void SetSocketOption3_DropMembershipIPv6_Socket_NotBound ()
  3613. {
  3614. IPAddress mcast_addr = IPAddress.Parse ("ff02::1");
  3615. Socket s = new Socket (AddressFamily.InterNetworkV6, SocketType.Dgram, ProtocolType.Udp);
  3616. try {
  3617. s.SetSocketOption (SocketOptionLevel.IPv6, SocketOptionName.DropMembership,
  3618. new IPv6MulticastOption (mcast_addr));
  3619. Assert.Fail ("#1");
  3620. } catch (SocketException ex) {
  3621. // An invalid argument was supplied
  3622. Assert.AreEqual (typeof (SocketException), ex.GetType (), "#2");
  3623. Assert.AreEqual (10022, ex.ErrorCode, "#3");
  3624. Assert.IsNull (ex.InnerException, "#4");
  3625. Assert.IsNotNull (ex.Message, "#5");
  3626. Assert.AreEqual (10022, ex.NativeErrorCode, "#6");
  3627. Assert.AreEqual (SocketError.InvalidArgument, ex.SocketErrorCode, "#7");
  3628. } finally {
  3629. s.Close ();
  3630. }
  3631. }
  3632. [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Object)
  3633. public void SetSocketOption3_OptionValue_Null ()
  3634. {
  3635. using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) {
  3636. try {
  3637. s.SetSocketOption (SocketOptionLevel.Socket,
  3638. SocketOptionName.Linger, (object) null);
  3639. Assert.Fail ("#1");
  3640. } catch (ArgumentNullException ex) {
  3641. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  3642. Assert.IsNull (ex.InnerException, "#3");
  3643. Assert.IsNotNull (ex.Message, "#4");
  3644. Assert.AreEqual ("optionValue", ex.ParamName, "#5");
  3645. }
  3646. }
  3647. }
  3648. [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Object)
  3649. public void SetSocketOption3_Socket_Closed ()
  3650. {
  3651. Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  3652. s.Close ();
  3653. try {
  3654. s.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.Linger,
  3655. new LingerOption (false, 0));
  3656. Assert.Fail ("#1");
  3657. } catch (ObjectDisposedException ex) {
  3658. // Cannot access a disposed object
  3659. Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
  3660. Assert.IsNull (ex.InnerException, "#3");
  3661. Assert.IsNotNull (ex.Message, "#4");
  3662. Assert.AreEqual (typeof (Socket).FullName, ex.ObjectName, "#5");
  3663. }
  3664. }
  3665. [Test]
  3666. [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.
  3667. public void SetSocketOption_MulticastInterfaceIndex_Any ()
  3668. {
  3669. IPAddress ip = IPAddress.Parse ("239.255.255.250");
  3670. int index = 0;
  3671. using (Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp))
  3672. {
  3673. s.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.MulticastInterface, IPAddress.HostToNetworkOrder(index));
  3674. s.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, new MulticastOption(ip, index));
  3675. }
  3676. }
  3677. [Test]
  3678. [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.
  3679. public void SetSocketOption_MulticastInterfaceIndex_Loopback ()
  3680. {
  3681. IPAddress ip = IPAddress.Parse ("239.255.255.250");
  3682. int index = 1;
  3683. using (Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp))
  3684. {
  3685. s.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.MulticastInterface, IPAddress.HostToNetworkOrder(index));
  3686. s.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, new MulticastOption(ip, index));
  3687. }
  3688. }
  3689. [Test]
  3690. public void SetSocketOption_MulticastInterfaceIndex_Invalid ()
  3691. {
  3692. IPAddress ip = IPAddress.Parse ("239.255.255.250");
  3693. int index = 31415;
  3694. using (Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp))
  3695. {
  3696. try
  3697. {
  3698. s.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.MulticastInterface, IPAddress.HostToNetworkOrder(index));
  3699. Assert.Fail ("#1");
  3700. }
  3701. catch
  3702. {}
  3703. try
  3704. {
  3705. s.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, new MulticastOption(ip, index));
  3706. Assert.Fail ("#2");
  3707. }
  3708. catch
  3709. {}
  3710. }
  3711. }
  3712. [Test]
  3713. #if FEATURE_NO_BSD_SOCKETS
  3714. [ExpectedException (typeof (PlatformNotSupportedException))]
  3715. #endif
  3716. public void Shutdown_NoConnect ()
  3717. {
  3718. Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  3719. s.Bind (new IPEndPoint (IPAddress.Loopback, NetworkHelpers.FindFreePort ()));
  3720. s.Listen (1);
  3721. try {
  3722. s.Shutdown (SocketShutdown.Both);
  3723. Assert.Fail ("#1");
  3724. } catch (SocketException exc) {
  3725. Assert.AreEqual (10057, exc.ErrorCode, "#2");
  3726. } finally {
  3727. s.Close ();
  3728. }
  3729. }
  3730. [Test]
  3731. [ExpectedException (typeof (NullReferenceException))]
  3732. public void ReceiveAsync_Null ()
  3733. {
  3734. using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) {
  3735. s.ReceiveAsync (null);
  3736. }
  3737. }
  3738. [Test]
  3739. [ExpectedException (typeof (NullReferenceException))]
  3740. public void ReceiveAsync_Default ()
  3741. {
  3742. using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) {
  3743. SocketAsyncEventArgs saea = new SocketAsyncEventArgs ();
  3744. s.ReceiveAsync (saea);
  3745. }
  3746. }
  3747. [Test]
  3748. [ExpectedException (typeof (NullReferenceException))]
  3749. public void ReceiveAsync_NullBuffer ()
  3750. {
  3751. using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) {
  3752. SocketAsyncEventArgs saea = new SocketAsyncEventArgs ();
  3753. saea.SetBuffer (null, 0, 0);
  3754. s.ReceiveAsync (null);
  3755. }
  3756. }
  3757. [Test]
  3758. [ExpectedException (typeof (ObjectDisposedException))]
  3759. public void ReceiveAsync_ClosedSocket ()
  3760. {
  3761. Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  3762. s.Close ();
  3763. s.ReceiveAsync (null);
  3764. }
  3765. [Test]
  3766. [ExpectedException (typeof (NullReferenceException))]
  3767. public void SendAsync_Null ()
  3768. {
  3769. using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) {
  3770. s.SendAsync (null);
  3771. }
  3772. }
  3773. [Test]
  3774. [ExpectedException (typeof (NullReferenceException))]
  3775. public void SendAsync_Default ()
  3776. {
  3777. using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) {
  3778. SocketAsyncEventArgs saea = new SocketAsyncEventArgs ();
  3779. s.SendAsync (saea);
  3780. }
  3781. }
  3782. [Test]
  3783. [ExpectedException (typeof (NullReferenceException))]
  3784. public void SendAsync_NullBuffer ()
  3785. {
  3786. using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) {
  3787. SocketAsyncEventArgs saea = new SocketAsyncEventArgs ();
  3788. saea.SetBuffer (null, 0, 0);
  3789. s.SendAsync (null);
  3790. }
  3791. }
  3792. [Test]
  3793. [ExpectedException (typeof (ObjectDisposedException))]
  3794. public void SendAsync_ClosedSocket ()
  3795. {
  3796. Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  3797. s.Close ();
  3798. s.SendAsync (null);
  3799. }
  3800. [Test]
  3801. #if FEATURE_NO_BSD_SOCKETS
  3802. [ExpectedException (typeof (PlatformNotSupportedException))]
  3803. #endif
  3804. public void SendAsyncFile ()
  3805. {
  3806. Socket serverSocket = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  3807. serverSocket.Bind (new IPEndPoint (IPAddress.Loopback, 0));
  3808. serverSocket.Listen (1);
  3809. var mReceived = new ManualResetEvent (false);
  3810. serverSocket.BeginAccept (AsyncCall => {
  3811. byte[] bytes = new byte [1024];
  3812. Socket listener = (Socket)AsyncCall.AsyncState;
  3813. Socket client = listener.EndAccept (AsyncCall);
  3814. client.Receive (bytes, bytes.Length, 0);
  3815. client.Close ();
  3816. mReceived.Set ();
  3817. }, serverSocket);
  3818. Socket clientSocket = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  3819. clientSocket.Connect (serverSocket.LocalEndPoint);
  3820. clientSocket.NoDelay = true;
  3821. // Initialize buffer used to create testing file
  3822. var buffer = new byte [1024];
  3823. for (int i = 0; i < 1024; ++i)
  3824. buffer [i] = (byte) (i % 256);
  3825. string temp = Path.GetTempFileName ();
  3826. try {
  3827. // Testing file creation
  3828. using (StreamWriter sw = new StreamWriter (temp)) {
  3829. sw.Write (buffer);
  3830. }
  3831. var mSent = new ManualResetEvent (false);
  3832. // Async Send File to server
  3833. clientSocket.BeginSendFile(temp, (ar) => {
  3834. Socket client = (Socket) ar.AsyncState;
  3835. client.EndSendFile (ar);
  3836. mSent.Set ();
  3837. }, clientSocket);
  3838. if (!mSent.WaitOne (1500))
  3839. throw new TimeoutException ();
  3840. if (!mReceived.WaitOne (1500))
  3841. throw new TimeoutException ();
  3842. } finally {
  3843. if (File.Exists (temp))
  3844. File.Delete (temp);
  3845. clientSocket.Close ();
  3846. serverSocket.Close ();
  3847. }
  3848. }
  3849. [Test]
  3850. #if FEATURE_NO_BSD_SOCKETS
  3851. [ExpectedException (typeof (PlatformNotSupportedException))]
  3852. #endif
  3853. public void ConnectToIPV4EndPointUsingDualModelSocket () {
  3854. /*
  3855. * IPv6 DualMode sockets are defaults in Mono. Explicitly specify that
  3856. * anyways in this test to make it more interoparable with .NET where
  3857. * IPv6 and DualMode needs to be specified.
  3858. */
  3859. using (var server = new Socket (AddressFamily.InterNetworkV6, SocketType.Stream, ProtocolType.Tcp)) {
  3860. var host = new IPEndPoint (IPAddress.Loopback, NetworkHelpers.FindFreePort ());
  3861. server.DualMode = true;
  3862. server.Bind (host);
  3863. /*
  3864. * Nothing to Accept the connect - we need a backlog to make sure we don't get
  3865. Connection refused.
  3866. */
  3867. server.Listen (3);
  3868. var ep = server.LocalEndPoint as IPEndPoint;
  3869. var client = new Socket (AddressFamily.InterNetworkV6, SocketType.Stream, ProtocolType.Tcp);
  3870. client.DualMode = true;
  3871. client.Connect (ep);
  3872. client.Disconnect (false);
  3873. client.Close ();
  3874. client = new Socket (AddressFamily.InterNetworkV6, SocketType.Stream, ProtocolType.Tcp);
  3875. client.DualMode = true;
  3876. client.Connect (IPAddress.Loopback, ep.Port);
  3877. client.Disconnect (false);
  3878. client.Close ();
  3879. client = new Socket (AddressFamily.InterNetworkV6, SocketType.Stream, ProtocolType.Tcp);
  3880. client.DualMode = true;
  3881. client.Connect (new [] { IPAddress.Loopback }, ep.Port);
  3882. client.Disconnect (false);
  3883. client.Close ();
  3884. }
  3885. }
  3886. [Test]
  3887. #if FEATURE_NO_BSD_SOCKETS
  3888. [ExpectedException (typeof (PlatformNotSupportedException))]
  3889. #endif
  3890. public void BeginConnectToIPV4EndPointUsingDualModelSocket () {
  3891. /*
  3892. * IPv6 DualMode sockets are defaults in Mono. Explicitly specify that
  3893. * anyways in this test to make it more interoparable with .NET where
  3894. * IPv6 and DualMode needs to be specified.
  3895. */
  3896. using (var server = new Socket (AddressFamily.InterNetworkV6, SocketType.Stream, ProtocolType.Tcp))
  3897. {
  3898. var host = new IPEndPoint (IPAddress.Loopback, NetworkHelpers.FindFreePort ());
  3899. server.DualMode = true;
  3900. server.Bind (host);
  3901. server.Listen (10);
  3902. var ep = server.LocalEndPoint as IPEndPoint;
  3903. BCCalledBack.Reset ();
  3904. var client = new Socket (AddressFamily.InterNetworkV6, SocketType.Stream, ProtocolType.Tcp);
  3905. client.DualMode = true;
  3906. var ar1 = client.BeginConnect (ep, BCCallback, client);
  3907. Assert.IsTrue (BCCalledBack.WaitOne (10000), "#1");
  3908. client.Disconnect (false);
  3909. client.Close ();
  3910. BCCalledBack.Reset ();
  3911. client = new Socket (AddressFamily.InterNetworkV6, SocketType.Stream, ProtocolType.Tcp);
  3912. client.DualMode = true;
  3913. var ar2 = client.BeginConnect (IPAddress.Loopback, ep.Port, BCCallback, client);
  3914. Assert.IsTrue (BCCalledBack.WaitOne (10000), "#2");
  3915. client.Disconnect (false);
  3916. client.Close ();
  3917. BCCalledBack.Reset ();
  3918. client = new Socket (AddressFamily.InterNetworkV6, SocketType.Stream, ProtocolType.Tcp);
  3919. client.DualMode = true;
  3920. var ar3 = client.BeginConnect (new [] {IPAddress.Loopback}, ep.Port, BCCallback, client);
  3921. Assert.IsTrue (BCCalledBack.WaitOne (10000), "#2");
  3922. client.Disconnect (false);
  3923. client.Close();
  3924. }
  3925. }
  3926. [Test]
  3927. #if FEATURE_NO_BSD_SOCKETS
  3928. [ExpectedException (typeof (PlatformNotSupportedException))]
  3929. #endif
  3930. public void UdpMulticasTimeToLive ()
  3931. {
  3932. /* see https://bugzilla.xamarin.com/show_bug.cgi?id=36941 */
  3933. using (Socket socket = new Socket (AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp)) {
  3934. IPEndPoint end_point = new IPEndPoint (IPAddress.Any, NetworkHelpers.FindFreePort ());
  3935. socket.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, 1);
  3936. socket.Bind (end_point);
  3937. socket.SetSocketOption (SocketOptionLevel.IP, SocketOptionName.MulticastTimeToLive, 19);
  3938. }
  3939. }
  3940. [Test] // Covers 41616
  3941. public void ConnectAsyncUnhandledEx ()
  3942. {
  3943. var mre = new ManualResetEvent (false);
  3944. var endPoint = new IPEndPoint(0,0);
  3945. var socket = new Socket(endPoint.AddressFamily, SocketType.Stream, ProtocolType.Unspecified);
  3946. var socketArgs = new SocketAsyncEventArgs();
  3947. socketArgs.RemoteEndPoint = endPoint;
  3948. socketArgs.Completed += (sender, e) => mre.Set ();
  3949. socket.ConnectAsync (socketArgs);
  3950. Assert.IsTrue (mre.WaitOne (1000), "ConnectedAsync timeout");
  3951. }
  3952. }
  3953. }