SocketTest.cs 134 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671
  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. public void ConcurrentExceedSocketLimit ()
  2208. {
  2209. var tasks = new Task[4];
  2210. for (int i = 0; i < 4; i++) {
  2211. tasks[i] = Task.Factory.StartNew (() => SendGenericExceedBuffer ());
  2212. }
  2213. Task.WaitAll (tasks);
  2214. }
  2215. [Test]
  2216. public void SendGenericExceedBuffer ()
  2217. {
  2218. // Create a buffer larger than the default max.
  2219. const int BUFFER_SIZE = 256 * 256 * 65;
  2220. int i;
  2221. IPEndPoint endpoint = new IPEndPoint(IPAddress.Loopback, NetworkHelpers.FindFreePort ());
  2222. Socket listensock = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  2223. listensock.Bind (endpoint);
  2224. listensock.Listen (1);
  2225. Socket sendsock = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  2226. sendsock.Connect (endpoint);
  2227. Socket clientsock = listensock.Accept ();
  2228. byte[] sendbuf = new byte[BUFFER_SIZE];
  2229. for (i = 0; i < BUFFER_SIZE; i++) {
  2230. sendbuf[i] = (byte)i;
  2231. }
  2232. Task sendTask = Task.Factory.StartNew(() => {
  2233. int sent = sendsock.Send (sendbuf);
  2234. Assert.AreEqual (BUFFER_SIZE, sent, "#1");
  2235. });
  2236. byte[] recvbuf = new byte[BUFFER_SIZE];
  2237. Task recvTask = Task.Factory.StartNew(() => {
  2238. int totalReceived = 0;
  2239. byte[] buffer = new byte[256];
  2240. while (totalReceived < sendbuf.Length) {
  2241. int recvd = clientsock.Receive (buffer, 0, buffer.Length, SocketFlags.None);
  2242. Array.Copy (buffer, 0, recvbuf, totalReceived, recvd);
  2243. totalReceived += recvd;
  2244. }
  2245. Assert.AreEqual (BUFFER_SIZE, totalReceived, "#2");
  2246. });
  2247. Assert.IsTrue (Task.WaitAll (new []{sendTask, recvTask}, 15 * 1000), "#2a");
  2248. for (i = 0; i < BUFFER_SIZE; i++) {
  2249. Assert.AreEqual (recvbuf[i], sendbuf[i],
  2250. "#3/" + i.ToString());
  2251. }
  2252. sendsock.Close ();
  2253. clientsock.Close ();
  2254. listensock.Close ();
  2255. }
  2256. [Test]
  2257. public void ListenNotBound ()
  2258. {
  2259. Socket sock = new Socket (AddressFamily.InterNetwork,
  2260. SocketType.Stream,
  2261. ProtocolType.Tcp);
  2262. try {
  2263. sock.Listen (1);
  2264. Assert.Fail ("ListenNotBound #1");
  2265. } catch (SocketException ex) {
  2266. Assert.AreEqual (10022, ex.ErrorCode, "ListenNotBound #2");
  2267. } finally {
  2268. sock.Close ();
  2269. }
  2270. }
  2271. static Socket CWRSocket;
  2272. static bool CWRReceiving = true;
  2273. static ManualResetEvent CWRReady = new ManualResetEvent (false);
  2274. private static void CWRReceiveThread ()
  2275. {
  2276. byte[] buf = new byte[256];
  2277. try {
  2278. CWRSocket.Receive (buf);
  2279. } catch (SocketException) {
  2280. CWRReceiving = false;
  2281. }
  2282. CWRReady.Set ();
  2283. }
  2284. [Test]
  2285. #if FEATURE_NO_BSD_SOCKETS
  2286. [ExpectedException (typeof (PlatformNotSupportedException))]
  2287. #endif
  2288. public void CloseWhileReceiving ()
  2289. {
  2290. CWRSocket = new Socket (AddressFamily.InterNetwork,
  2291. SocketType.Dgram,
  2292. ProtocolType.Udp);
  2293. CWRSocket.Bind (new IPEndPoint (IPAddress.Loopback,
  2294. NetworkHelpers.FindFreePort ()));
  2295. Thread recv_thread = new Thread (new ThreadStart (CWRReceiveThread));
  2296. CWRReady.Reset ();
  2297. recv_thread.Start ();
  2298. Thread.Sleep (250); /* Wait for the thread to be already receiving */
  2299. CWRSocket.Close ();
  2300. if (CWRReady.WaitOne (1000, false) == false) {
  2301. Assert.Fail ("CloseWhileReceiving wait timed out");
  2302. }
  2303. Assert.IsFalse (CWRReceiving);
  2304. }
  2305. static bool RRCLastRead = false;
  2306. static ManualResetEvent RRCReady = new ManualResetEvent (false);
  2307. private static void RRCClientThread (int port)
  2308. {
  2309. byte[] bytes = new byte[8];
  2310. int readbyte;
  2311. Socket sock = new Socket (AddressFamily.InterNetwork,
  2312. SocketType.Stream,
  2313. ProtocolType.Tcp);
  2314. sock.Connect (new IPEndPoint (IPAddress.Loopback,
  2315. port));
  2316. NetworkStream stream = new NetworkStream (sock);
  2317. readbyte = stream.ReadByte ();
  2318. Assert.AreEqual (0, readbyte, "ReceiveRemoteClosed #1");
  2319. stream.Read (bytes, 0, 0);
  2320. readbyte = stream.ReadByte ();
  2321. Assert.AreEqual (0, readbyte, "ReceiveRemoteClosed #2");
  2322. stream.Read (bytes, 0, 0);
  2323. readbyte = stream.ReadByte ();
  2324. Assert.AreEqual (-1, readbyte, "ReceiveRemoteClosed #3");
  2325. sock.Close ();
  2326. RRCLastRead = true;
  2327. RRCReady.Set ();
  2328. }
  2329. [Test] // Receive (Byte [])
  2330. public void Receive1_Buffer_Null ()
  2331. {
  2332. Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
  2333. ProtocolType.Tcp);
  2334. try {
  2335. s.Receive ((byte []) null);
  2336. Assert.Fail ("#1");
  2337. } catch (ArgumentNullException ex) {
  2338. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  2339. Assert.IsNull (ex.InnerException, "#3");
  2340. Assert.IsNotNull (ex.Message, "#4");
  2341. Assert.AreEqual ("buffer", ex.ParamName, "#5");
  2342. } finally {
  2343. s.Close ();
  2344. }
  2345. }
  2346. [Test] // Receive (Byte [])
  2347. public void Receive1_Socket_Closed ()
  2348. {
  2349. Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
  2350. ProtocolType.Tcp);
  2351. s.Close ();
  2352. try {
  2353. s.Receive ((byte []) null);
  2354. Assert.Fail ("#1");
  2355. } catch (ObjectDisposedException ex) {
  2356. // Cannot access a disposed object
  2357. Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
  2358. Assert.IsNull (ex.InnerException, "#3");
  2359. Assert.IsNotNull (ex.Message, "#4");
  2360. Assert.AreEqual (typeof (Socket).FullName, ex.ObjectName, "#5");
  2361. }
  2362. }
  2363. [Test] // Receive (Byte [], SocketFlags)
  2364. public void Receive2_Buffer_Null ()
  2365. {
  2366. Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
  2367. ProtocolType.Tcp);
  2368. try {
  2369. s.Receive ((byte []) null, (SocketFlags) 666);
  2370. Assert.Fail ("#1");
  2371. } catch (ArgumentNullException ex) {
  2372. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  2373. Assert.IsNull (ex.InnerException, "#3");
  2374. Assert.IsNotNull (ex.Message, "#4");
  2375. Assert.AreEqual ("buffer", ex.ParamName, "#5");
  2376. } finally {
  2377. s.Close ();
  2378. }
  2379. }
  2380. [Test] // Receive (Byte [], SocketFlags)
  2381. public void Receive2_Socket_Closed ()
  2382. {
  2383. Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
  2384. ProtocolType.Tcp);
  2385. s.Close ();
  2386. try {
  2387. s.Receive ((byte []) null, (SocketFlags) 666);
  2388. Assert.Fail ("#1");
  2389. } catch (ObjectDisposedException ex) {
  2390. // Cannot access a disposed object
  2391. Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
  2392. Assert.IsNull (ex.InnerException, "#3");
  2393. Assert.IsNotNull (ex.Message, "#4");
  2394. Assert.AreEqual (typeof (Socket).FullName, ex.ObjectName, "#5");
  2395. }
  2396. }
  2397. [Test] // Receive (Byte [], Int32, SocketFlags)
  2398. public void Receive3_Buffer_Null ()
  2399. {
  2400. Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
  2401. ProtocolType.Tcp);
  2402. try {
  2403. s.Receive ((byte []) null, 0, (SocketFlags) 666);
  2404. Assert.Fail ("#1");
  2405. } catch (ArgumentNullException ex) {
  2406. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  2407. Assert.IsNull (ex.InnerException, "#3");
  2408. Assert.IsNotNull (ex.Message, "#4");
  2409. Assert.AreEqual ("buffer", ex.ParamName, "#5");
  2410. } finally {
  2411. s.Close ();
  2412. }
  2413. }
  2414. [Test] // Receive (Byte [], Int32, SocketFlags)
  2415. public void Receive3_Socket_Closed ()
  2416. {
  2417. Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
  2418. ProtocolType.Tcp);
  2419. s.Close ();
  2420. try {
  2421. s.Receive ((byte []) null, 0, (SocketFlags) 666);
  2422. Assert.Fail ("#1");
  2423. } catch (ObjectDisposedException ex) {
  2424. // Cannot access a disposed object
  2425. Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
  2426. Assert.IsNull (ex.InnerException, "#3");
  2427. Assert.IsNotNull (ex.Message, "#4");
  2428. Assert.AreEqual (typeof (Socket).FullName, ex.ObjectName, "#5");
  2429. }
  2430. }
  2431. [Test] // Receive (Byte [], Int32, Int32, SocketFlags)
  2432. public void Receive4_Buffer_Null ()
  2433. {
  2434. Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
  2435. ProtocolType.Tcp);
  2436. try {
  2437. s.Receive ((byte []) null, 0, 0, (SocketFlags) 666);
  2438. Assert.Fail ("#1");
  2439. } catch (ArgumentNullException ex) {
  2440. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  2441. Assert.IsNull (ex.InnerException, "#3");
  2442. Assert.IsNotNull (ex.Message, "#4");
  2443. Assert.AreEqual ("buffer", ex.ParamName, "#5");
  2444. } finally {
  2445. s.Close ();
  2446. }
  2447. }
  2448. [Test] // Receive (Byte [], Int32, Int32, SocketFlags)
  2449. public void Receive4_Socket_Closed ()
  2450. {
  2451. Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
  2452. ProtocolType.Tcp);
  2453. s.Close ();
  2454. try {
  2455. s.Receive ((byte []) null, 0, 0, (SocketFlags) 666);
  2456. Assert.Fail ("#1");
  2457. } catch (ObjectDisposedException ex) {
  2458. // Cannot access a disposed object
  2459. Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
  2460. Assert.IsNull (ex.InnerException, "#3");
  2461. Assert.IsNotNull (ex.Message, "#4");
  2462. Assert.AreEqual (typeof (Socket).FullName, ex.ObjectName, "#5");
  2463. }
  2464. }
  2465. [Test] // Receive (Byte [], Int32, Int32, SocketFlags, out SocketError)
  2466. public void Receive5_Buffer_Null ()
  2467. {
  2468. Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
  2469. ProtocolType.Tcp);
  2470. SocketError error;
  2471. try {
  2472. s.Receive ((byte []) null, 0, 0, SocketFlags.None, out error);
  2473. Assert.Fail ("#1");
  2474. } catch (ArgumentNullException ex) {
  2475. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  2476. Assert.IsNull (ex.InnerException, "#3");
  2477. Assert.IsNotNull (ex.Message, "#4");
  2478. Assert.AreEqual ("buffer", ex.ParamName, "#5");
  2479. } finally {
  2480. s.Close ();
  2481. }
  2482. }
  2483. [Test] // Receive (Byte [], Int32, Int32, SocketFlags, out SocketError)
  2484. public void Receive5_Socket_Closed ()
  2485. {
  2486. Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
  2487. ProtocolType.Tcp);
  2488. s.Close ();
  2489. SocketError error;
  2490. try {
  2491. s.Receive ((byte []) null, 0, 0, SocketFlags.None, out error);
  2492. Assert.Fail ("#1");
  2493. } catch (ObjectDisposedException ex) {
  2494. // Cannot access a disposed object
  2495. Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
  2496. Assert.IsNull (ex.InnerException, "#3");
  2497. Assert.IsNotNull (ex.Message, "#4");
  2498. Assert.AreEqual (typeof (Socket).FullName, ex.ObjectName, "#5");
  2499. }
  2500. }
  2501. [Test] // Receive (IList<ArraySegment<Byte>>)
  2502. public void Receive6_Buffers_Null ()
  2503. {
  2504. Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
  2505. ProtocolType.Tcp);
  2506. try {
  2507. s.Receive ((IList<ArraySegment<byte>>) null);
  2508. Assert.Fail ("#1");
  2509. } catch (ArgumentNullException ex) {
  2510. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  2511. Assert.IsNull (ex.InnerException, "#3");
  2512. Assert.IsNotNull (ex.Message, "#4");
  2513. Assert.AreEqual ("buffers", ex.ParamName, "#5");
  2514. } finally {
  2515. s.Close ();
  2516. }
  2517. }
  2518. [Test] // Receive (IList<ArraySegment<Byte>>)
  2519. public void Receive6_Socket_Closed ()
  2520. {
  2521. Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
  2522. ProtocolType.Tcp);
  2523. s.Close ();
  2524. try {
  2525. s.Receive ((IList<ArraySegment<byte>>) null);
  2526. Assert.Fail ("#1");
  2527. } catch (ObjectDisposedException ex) {
  2528. // Cannot access a disposed object
  2529. Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
  2530. Assert.IsNull (ex.InnerException, "#3");
  2531. Assert.IsNotNull (ex.Message, "#4");
  2532. Assert.AreEqual (typeof (Socket).FullName, ex.ObjectName, "#5");
  2533. }
  2534. }
  2535. [Test] // Receive (IList<ArraySegment<Byte>>, SocketFlags)
  2536. public void Receive7_Buffers_Null ()
  2537. {
  2538. Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
  2539. ProtocolType.Tcp);
  2540. try {
  2541. s.Receive ((IList<ArraySegment<byte>>) null, (SocketFlags) 666);
  2542. Assert.Fail ("#1");
  2543. } catch (ArgumentNullException ex) {
  2544. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  2545. Assert.IsNull (ex.InnerException, "#3");
  2546. Assert.IsNotNull (ex.Message, "#4");
  2547. Assert.AreEqual ("buffers", ex.ParamName, "#5");
  2548. } finally {
  2549. s.Close ();
  2550. }
  2551. }
  2552. [Test] // Receive (IList<ArraySegment<Byte>>, SocketFlags)
  2553. public void Receive7_Socket_Closed ()
  2554. {
  2555. Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
  2556. ProtocolType.Tcp);
  2557. s.Close ();
  2558. try {
  2559. s.Receive ((IList<ArraySegment<byte>>) null, (SocketFlags) 666);
  2560. Assert.Fail ("#1");
  2561. } catch (ObjectDisposedException ex) {
  2562. // Cannot access a disposed object
  2563. Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
  2564. Assert.IsNull (ex.InnerException, "#3");
  2565. Assert.IsNotNull (ex.Message, "#4");
  2566. Assert.AreEqual (typeof (Socket).FullName, ex.ObjectName, "#5");
  2567. }
  2568. }
  2569. [Test] // Receive (IList<ArraySegment<Byte>>, SocketFlags, out SocketError)
  2570. public void Receive8_Buffers_Null ()
  2571. {
  2572. Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
  2573. ProtocolType.Tcp);
  2574. SocketError error;
  2575. try {
  2576. s.Receive ((IList<ArraySegment<byte>>) null, (SocketFlags) 666,
  2577. out error);
  2578. Assert.Fail ("#1");
  2579. } catch (ArgumentNullException ex) {
  2580. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  2581. Assert.IsNull (ex.InnerException, "#3");
  2582. Assert.IsNotNull (ex.Message, "#4");
  2583. Assert.AreEqual ("buffers", ex.ParamName, "#5");
  2584. } finally {
  2585. s.Close ();
  2586. }
  2587. }
  2588. [Test] // Receive (IList<ArraySegment<Byte>>, SocketFlags, out SocketError)
  2589. public void Receive8_Socket_Closed ()
  2590. {
  2591. Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
  2592. ProtocolType.Tcp);
  2593. s.Close ();
  2594. SocketError error;
  2595. try {
  2596. s.Receive ((IList<ArraySegment<byte>>) null, (SocketFlags) 666,
  2597. out error);
  2598. Assert.Fail ("#1");
  2599. } catch (ObjectDisposedException ex) {
  2600. // Cannot access a disposed object
  2601. Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
  2602. Assert.IsNull (ex.InnerException, "#3");
  2603. Assert.IsNotNull (ex.Message, "#4");
  2604. Assert.AreEqual (typeof (Socket).FullName, ex.ObjectName, "#5");
  2605. } finally {
  2606. s.Close ();
  2607. }
  2608. }
  2609. [Test] // ReceiveFrom (Byte [], ref EndPoint)
  2610. #if FEATURE_NO_BSD_SOCKETS
  2611. [ExpectedException (typeof (PlatformNotSupportedException))]
  2612. #endif
  2613. public void ReceiveFrom1_Buffer_Null ()
  2614. {
  2615. Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
  2616. ProtocolType.Tcp);
  2617. EndPoint remoteEP = new IPEndPoint (IPAddress.Loopback, NetworkHelpers.FindFreePort ());
  2618. try {
  2619. s.ReceiveFrom ((Byte []) null, ref remoteEP);
  2620. Assert.Fail ("#1");
  2621. } catch (ArgumentNullException ex) {
  2622. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  2623. Assert.IsNull (ex.InnerException, "#3");
  2624. Assert.IsNotNull (ex.Message, "#4");
  2625. Assert.AreEqual ("buffer", ex.ParamName, "#5");
  2626. } finally {
  2627. s.Close ();
  2628. }
  2629. }
  2630. [Test] // ReceiveFrom (Byte [], ref EndPoint)
  2631. public void ReceiveFrom1_RemoteEP_Null ()
  2632. {
  2633. Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
  2634. ProtocolType.Tcp);
  2635. byte [] buffer = new byte [0];
  2636. EndPoint remoteEP = null;
  2637. try {
  2638. s.ReceiveFrom (buffer, ref remoteEP);
  2639. Assert.Fail ("#1");
  2640. } catch (ArgumentNullException ex) {
  2641. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  2642. Assert.IsNull (ex.InnerException, "#3");
  2643. Assert.IsNotNull (ex.Message, "#4");
  2644. Assert.AreEqual ("remoteEP", ex.ParamName, "#5");
  2645. } finally {
  2646. s.Close ();
  2647. }
  2648. }
  2649. [Test] // ReceiveFrom (Byte [], ref EndPoint)
  2650. #if FEATURE_NO_BSD_SOCKETS
  2651. [ExpectedException (typeof (PlatformNotSupportedException))]
  2652. #endif
  2653. public void ReceiveFrom1_Socket_Closed ()
  2654. {
  2655. Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
  2656. ProtocolType.Tcp);
  2657. s.Close ();
  2658. EndPoint remoteEP = new IPEndPoint (IPAddress.Loopback, NetworkHelpers.FindFreePort ());
  2659. try {
  2660. s.ReceiveFrom ((Byte []) null, ref remoteEP);
  2661. Assert.Fail ("#1");
  2662. } catch (ObjectDisposedException ex) {
  2663. // Cannot access a disposed object
  2664. Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
  2665. Assert.IsNull (ex.InnerException, "#3");
  2666. Assert.IsNotNull (ex.Message, "#4");
  2667. Assert.AreEqual (typeof (Socket).FullName, ex.ObjectName, "#5");
  2668. }
  2669. }
  2670. [Test] // ReceiveFrom (Byte [], SocketFlags, ref EndPoint)
  2671. #if FEATURE_NO_BSD_SOCKETS
  2672. [ExpectedException (typeof (PlatformNotSupportedException))]
  2673. #endif
  2674. public void ReceiveFrom2_Buffer_Null ()
  2675. {
  2676. Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
  2677. ProtocolType.Tcp);
  2678. EndPoint remoteEP = new IPEndPoint (IPAddress.Loopback, NetworkHelpers.FindFreePort ());
  2679. try {
  2680. s.ReceiveFrom ((Byte []) null, (SocketFlags) 666, ref remoteEP);
  2681. Assert.Fail ("#1");
  2682. } catch (ArgumentNullException ex) {
  2683. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  2684. Assert.IsNull (ex.InnerException, "#3");
  2685. Assert.IsNotNull (ex.Message, "#4");
  2686. Assert.AreEqual ("buffer", ex.ParamName, "#5");
  2687. } finally {
  2688. s.Close ();
  2689. }
  2690. }
  2691. [Test] // ReceiveFrom (Byte [], SocketFlags, ref EndPoint)
  2692. public void ReceiveFrom2_RemoteEP_Null ()
  2693. {
  2694. Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
  2695. ProtocolType.Tcp);
  2696. byte [] buffer = new byte [5];
  2697. EndPoint remoteEP = null;
  2698. try {
  2699. s.ReceiveFrom (buffer, (SocketFlags) 666, ref remoteEP);
  2700. Assert.Fail ("#1");
  2701. } catch (ArgumentNullException ex) {
  2702. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  2703. Assert.IsNull (ex.InnerException, "#3");
  2704. Assert.IsNotNull (ex.Message, "#4");
  2705. Assert.AreEqual ("remoteEP", ex.ParamName, "#5");
  2706. } finally {
  2707. s.Close ();
  2708. }
  2709. }
  2710. [Test] // ReceiveFrom (Byte [], SocketFlags, ref EndPoint)
  2711. #if FEATURE_NO_BSD_SOCKETS
  2712. [ExpectedException (typeof (PlatformNotSupportedException))]
  2713. #endif
  2714. public void ReceiveFrom2_Socket_Closed ()
  2715. {
  2716. Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
  2717. ProtocolType.Tcp);
  2718. s.Close ();
  2719. EndPoint remoteEP = new IPEndPoint (IPAddress.Loopback, NetworkHelpers.FindFreePort ());
  2720. try {
  2721. s.ReceiveFrom ((Byte []) null, (SocketFlags) 666, ref remoteEP);
  2722. Assert.Fail ("#1");
  2723. } catch (ObjectDisposedException ex) {
  2724. // Cannot access a disposed object
  2725. Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
  2726. Assert.IsNull (ex.InnerException, "#3");
  2727. Assert.IsNotNull (ex.Message, "#4");
  2728. Assert.AreEqual (typeof (Socket).FullName, ex.ObjectName, "#5");
  2729. }
  2730. }
  2731. [Test] // ReceiveFrom (Byte [], Int32, SocketFlags, ref EndPoint)
  2732. #if FEATURE_NO_BSD_SOCKETS
  2733. [ExpectedException (typeof (PlatformNotSupportedException))]
  2734. #endif
  2735. public void ReceiveFrom3_Buffer_Null ()
  2736. {
  2737. Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
  2738. ProtocolType.Tcp);
  2739. EndPoint remoteEP = new IPEndPoint (IPAddress.Loopback, NetworkHelpers.FindFreePort ());
  2740. try {
  2741. s.ReceiveFrom ((Byte []) null, 0, (SocketFlags) 666,
  2742. ref remoteEP);
  2743. Assert.Fail ("#1");
  2744. } catch (ArgumentNullException ex) {
  2745. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  2746. Assert.IsNull (ex.InnerException, "#3");
  2747. Assert.IsNotNull (ex.Message, "#4");
  2748. Assert.AreEqual ("buffer", ex.ParamName, "#5");
  2749. } finally {
  2750. s.Close ();
  2751. }
  2752. }
  2753. [Test] // ReceiveFrom (Byte [], Int32, SocketFlags, ref EndPoint)
  2754. public void ReceiveFrom3_RemoteEP_Null ()
  2755. {
  2756. Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
  2757. ProtocolType.Tcp);
  2758. byte [] buffer = new byte [5];
  2759. EndPoint remoteEP = null;
  2760. try {
  2761. s.ReceiveFrom (buffer, buffer.Length, (SocketFlags) 666, ref remoteEP);
  2762. Assert.Fail ("#1");
  2763. } catch (ArgumentNullException ex) {
  2764. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  2765. Assert.IsNull (ex.InnerException, "#3");
  2766. Assert.IsNotNull (ex.Message, "#4");
  2767. Assert.AreEqual ("remoteEP", ex.ParamName, "#5");
  2768. } finally {
  2769. s.Close ();
  2770. }
  2771. }
  2772. [Test] // ReceiveFrom (Byte [], Int32, SocketFlags, ref EndPoint)
  2773. #if FEATURE_NO_BSD_SOCKETS
  2774. [ExpectedException (typeof (PlatformNotSupportedException))]
  2775. #endif
  2776. public void ReceiveFrom3_Size_OutOfRange ()
  2777. {
  2778. Socket s;
  2779. byte [] buffer = new byte [5];
  2780. EndPoint remoteEP = new IPEndPoint (IPAddress.Loopback, NetworkHelpers.FindFreePort ());
  2781. // size negative
  2782. s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
  2783. ProtocolType.Tcp);
  2784. try {
  2785. s.ReceiveFrom (buffer, -1, (SocketFlags) 666, ref remoteEP);
  2786. Assert.Fail ("#A1");
  2787. } catch (ArgumentOutOfRangeException ex) {
  2788. // Specified argument was out of the range of valid values
  2789. Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#A2");
  2790. Assert.IsNull (ex.InnerException, "#A3");
  2791. Assert.IsNotNull (ex.Message, "#A4");
  2792. Assert.AreEqual ("size", ex.ParamName, "#A5");
  2793. } finally {
  2794. s.Close ();
  2795. }
  2796. // size > buffer length
  2797. s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
  2798. ProtocolType.Tcp);
  2799. try {
  2800. s.ReceiveFrom (buffer, (buffer.Length + 1), (SocketFlags) 666,
  2801. ref remoteEP);
  2802. Assert.Fail ("#B1");
  2803. } catch (ArgumentOutOfRangeException ex) {
  2804. // Specified argument was out of the range of valid values
  2805. Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#B2");
  2806. Assert.IsNull (ex.InnerException, "#B3");
  2807. Assert.IsNotNull (ex.Message, "#B4");
  2808. Assert.AreEqual ("size", ex.ParamName, "#B5");
  2809. } finally {
  2810. s.Close ();
  2811. }
  2812. }
  2813. [Test] // ReceiveFrom (Byte [], Int32, SocketFlags, ref EndPoint)
  2814. #if FEATURE_NO_BSD_SOCKETS
  2815. [ExpectedException (typeof (PlatformNotSupportedException))]
  2816. #endif
  2817. public void ReceiveFrom3_Socket_Closed ()
  2818. {
  2819. Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
  2820. ProtocolType.Tcp);
  2821. s.Close ();
  2822. EndPoint remoteEP = new IPEndPoint (IPAddress.Loopback, NetworkHelpers.FindFreePort ());
  2823. try {
  2824. s.ReceiveFrom ((Byte []) null, -1, (SocketFlags) 666,
  2825. ref remoteEP);
  2826. Assert.Fail ("#1");
  2827. } catch (ObjectDisposedException ex) {
  2828. // Cannot access a disposed object
  2829. Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
  2830. Assert.IsNull (ex.InnerException, "#3");
  2831. Assert.IsNotNull (ex.Message, "#4");
  2832. Assert.AreEqual (typeof (Socket).FullName, ex.ObjectName, "#5");
  2833. }
  2834. }
  2835. [Test] // ReceiveFrom (Byte [], Int32, Int32, SocketFlags, EndPoint)
  2836. #if FEATURE_NO_BSD_SOCKETS
  2837. [ExpectedException (typeof (PlatformNotSupportedException))]
  2838. #endif
  2839. public void ReceiveFrom4_Buffer_Null ()
  2840. {
  2841. Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
  2842. ProtocolType.Tcp);
  2843. EndPoint remoteEP = new IPEndPoint (IPAddress.Loopback, NetworkHelpers.FindFreePort ());
  2844. try {
  2845. s.ReceiveFrom ((Byte []) null, -1, -1, (SocketFlags) 666,
  2846. ref remoteEP);
  2847. Assert.Fail ("#1");
  2848. } catch (ArgumentNullException ex) {
  2849. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  2850. Assert.IsNull (ex.InnerException, "#3");
  2851. Assert.IsNotNull (ex.Message, "#4");
  2852. Assert.AreEqual ("buffer", ex.ParamName, "#5");
  2853. }
  2854. }
  2855. [Test] // ReceiveFrom (Byte [], Int32, Int32, SocketFlags, EndPoint)
  2856. #if FEATURE_NO_BSD_SOCKETS
  2857. [ExpectedException (typeof (PlatformNotSupportedException))]
  2858. #endif
  2859. public void ReceiveFrom4_Offset_OutOfRange ()
  2860. {
  2861. Socket s;
  2862. byte [] buffer = new byte [5];
  2863. EndPoint remoteEP = new IPEndPoint (IPAddress.Loopback, NetworkHelpers.FindFreePort ());
  2864. // offset negative
  2865. s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
  2866. ProtocolType.Tcp);
  2867. try {
  2868. s.ReceiveFrom (buffer, -1, 0, (SocketFlags) 666,
  2869. ref remoteEP);
  2870. Assert.Fail ("#A1");
  2871. } catch (ArgumentOutOfRangeException ex) {
  2872. // Specified argument was out of the range of valid values
  2873. Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#A2");
  2874. Assert.IsNull (ex.InnerException, "#A3");
  2875. Assert.IsNotNull (ex.Message, "#A4");
  2876. Assert.AreEqual ("offset", ex.ParamName, "#A5");
  2877. } finally {
  2878. s.Close ();
  2879. }
  2880. // offset > buffer length
  2881. s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
  2882. ProtocolType.Tcp);
  2883. try {
  2884. s.ReceiveFrom (buffer, (buffer.Length + 1), 0, (SocketFlags) 666,
  2885. ref remoteEP);
  2886. Assert.Fail ("#B1");
  2887. } catch (ArgumentOutOfRangeException ex) {
  2888. // Specified argument was out of the range of valid values
  2889. Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#B2");
  2890. Assert.IsNull (ex.InnerException, "#B3");
  2891. Assert.IsNotNull (ex.Message, "#B4");
  2892. Assert.AreEqual ("offset", ex.ParamName, "#B5");
  2893. } finally {
  2894. s.Close ();
  2895. }
  2896. }
  2897. [Test] // ReceiveFrom (Byte [], Int32, Int32, SocketFlags, ref IPEndPoint)
  2898. public void ReceiveFrom4_RemoteEP_Null ()
  2899. {
  2900. Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
  2901. ProtocolType.Tcp);
  2902. byte [] buffer = new byte [5];
  2903. EndPoint remoteEP = null;
  2904. try {
  2905. s.ReceiveFrom (buffer, 0, buffer.Length, (SocketFlags) 666, ref remoteEP);
  2906. Assert.Fail ("#1");
  2907. } catch (ArgumentNullException ex) {
  2908. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  2909. Assert.IsNull (ex.InnerException, "#3");
  2910. Assert.IsNotNull (ex.Message, "#4");
  2911. Assert.AreEqual ("remoteEP", ex.ParamName, "#5");
  2912. } finally {
  2913. s.Close ();
  2914. }
  2915. }
  2916. [Test] // ReceiveFrom (Byte [], Int32, Int32, SocketFlags, EndPoint)
  2917. #if FEATURE_NO_BSD_SOCKETS
  2918. [ExpectedException (typeof (PlatformNotSupportedException))]
  2919. #endif
  2920. public void ReceiveFrom4_Size_OutOfRange ()
  2921. {
  2922. Socket s;
  2923. byte [] buffer = new byte [5];
  2924. EndPoint remoteEP = new IPEndPoint (IPAddress.Loopback, NetworkHelpers.FindFreePort ());
  2925. // size negative
  2926. s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
  2927. ProtocolType.Tcp);
  2928. try {
  2929. s.ReceiveFrom (buffer, 0, -1, (SocketFlags) 666,
  2930. ref remoteEP);
  2931. Assert.Fail ("#A1");
  2932. } catch (ArgumentOutOfRangeException ex) {
  2933. // Specified argument was out of the range of valid values
  2934. Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#A2");
  2935. Assert.IsNull (ex.InnerException, "#A3");
  2936. Assert.IsNotNull (ex.Message, "#A4");
  2937. Assert.AreEqual ("size", ex.ParamName, "#A5");
  2938. } finally {
  2939. s.Close ();
  2940. }
  2941. // size > buffer length
  2942. s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
  2943. ProtocolType.Tcp);
  2944. try {
  2945. s.ReceiveFrom (buffer, 0, (buffer.Length + 1), (SocketFlags) 666,
  2946. ref remoteEP);
  2947. Assert.Fail ("#B1");
  2948. } catch (ArgumentOutOfRangeException ex) {
  2949. // Specified argument was out of the range of valid values
  2950. Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#B2");
  2951. Assert.IsNull (ex.InnerException, "#B3");
  2952. Assert.IsNotNull (ex.Message, "#B4");
  2953. Assert.AreEqual ("size", ex.ParamName, "#B5");
  2954. } finally {
  2955. s.Close ();
  2956. }
  2957. // offset + size > buffer length
  2958. s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
  2959. ProtocolType.Tcp);
  2960. try {
  2961. s.ReceiveFrom (buffer, 2, 4, (SocketFlags) 666, ref remoteEP);
  2962. Assert.Fail ("#C1");
  2963. } catch (ArgumentOutOfRangeException ex) {
  2964. // Specified argument was out of the range of valid values
  2965. Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#C2");
  2966. Assert.IsNull (ex.InnerException, "#C3");
  2967. Assert.IsNotNull (ex.Message, "#C4");
  2968. Assert.AreEqual ("size", ex.ParamName, "#C5");
  2969. } finally {
  2970. s.Close ();
  2971. }
  2972. }
  2973. [Test] // ReceiveFrom (Byte [], Int32, Int32, SocketFlags, ref EndPoint)
  2974. #if FEATURE_NO_BSD_SOCKETS
  2975. [ExpectedException (typeof (PlatformNotSupportedException))]
  2976. #endif
  2977. public void ReceiveFrom4_Socket_Closed ()
  2978. {
  2979. Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
  2980. ProtocolType.Tcp);
  2981. s.Close ();
  2982. byte [] buffer = new byte [5];
  2983. EndPoint remoteEP = new IPEndPoint (IPAddress.Loopback, NetworkHelpers.FindFreePort ());
  2984. try {
  2985. s.ReceiveFrom (buffer, -1, -1, (SocketFlags) 666,
  2986. ref remoteEP);
  2987. Assert.Fail ("#1");
  2988. } catch (ObjectDisposedException ex) {
  2989. // Cannot access a disposed object
  2990. Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
  2991. Assert.IsNull (ex.InnerException, "#3");
  2992. Assert.IsNotNull (ex.Message, "#4");
  2993. Assert.AreEqual (typeof (Socket).FullName, ex.ObjectName, "#5");
  2994. }
  2995. }
  2996. [Test]
  2997. #if FEATURE_NO_BSD_SOCKETS
  2998. [ExpectedException (typeof (PlatformNotSupportedException))]
  2999. #endif
  3000. public void ReceiveRemoteClosed ()
  3001. {
  3002. var port = NetworkHelpers.FindFreePort ();
  3003. Socket sock = new Socket (AddressFamily.InterNetwork,
  3004. SocketType.Stream,
  3005. ProtocolType.Tcp);
  3006. sock.Bind (new IPEndPoint (IPAddress.Loopback, port));
  3007. sock.Listen (1);
  3008. RRCReady.Reset ();
  3009. Thread client_thread = new Thread (() => RRCClientThread (port));
  3010. client_thread.Start ();
  3011. Socket client = sock.Accept ();
  3012. NetworkStream stream = new NetworkStream (client);
  3013. stream.WriteByte (0x00);
  3014. stream.WriteByte (0x00);
  3015. client.Close ();
  3016. sock.Close ();
  3017. RRCReady.WaitOne (1000, false);
  3018. Assert.IsTrue (RRCLastRead);
  3019. }
  3020. //
  3021. // Test case for bug #471580
  3022. [Test]
  3023. #if FEATURE_NO_BSD_SOCKETS
  3024. [ExpectedException (typeof (PlatformNotSupportedException))]
  3025. #endif
  3026. public void UdpDoubleBind ()
  3027. {
  3028. using (Socket s = new Socket (AddressFamily.InterNetwork,
  3029. SocketType.Dgram, ProtocolType.Udp))
  3030. using (Socket ss = new Socket (AddressFamily.InterNetwork,
  3031. SocketType.Dgram, ProtocolType.Udp)) {
  3032. var supportsReuseAddress = true;
  3033. try {
  3034. s.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
  3035. } catch (SocketException e) {
  3036. // Exception is thrown when ReuseAddress is not supported
  3037. Assert.AreEqual ((int) SocketError.OperationNotSupported, e.NativeErrorCode,
  3038. "Expected SocketError.OperationNotSupported");
  3039. supportsReuseAddress = false;
  3040. }
  3041. var ep = new IPEndPoint (IPAddress.Any, NetworkHelpers.FindFreePort ());
  3042. s.Bind (ep);
  3043. if (supportsReuseAddress)
  3044. ss.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
  3045. try {
  3046. ss.Bind (new IPEndPoint (IPAddress.Any, ep.Port));
  3047. if (!supportsReuseAddress)
  3048. Assert.Fail ("Reusing address is not supported, exception was expected on second bind.");
  3049. } catch (SocketException e) {
  3050. }
  3051. }
  3052. }
  3053. // Test case for bug #31557
  3054. [Test]
  3055. #if FEATURE_NO_BSD_SOCKETS
  3056. [ExpectedException (typeof (PlatformNotSupportedException))]
  3057. #endif
  3058. public void TcpDoubleBind ()
  3059. {
  3060. using (Socket s = new Socket (AddressFamily.InterNetwork,
  3061. SocketType.Stream, ProtocolType.Tcp))
  3062. using (Socket ss = new Socket (AddressFamily.InterNetwork,
  3063. SocketType.Stream, ProtocolType.Tcp)) {
  3064. var supportsReuseAddress = true;
  3065. try {
  3066. s.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
  3067. } catch (SocketException e) {
  3068. // Exception is thrown when ReuseAddress is not supported
  3069. Assert.AreEqual ((int) SocketError.OperationNotSupported, e.NativeErrorCode,
  3070. "Expected SocketError.OperationNotSupported");
  3071. supportsReuseAddress = false;
  3072. }
  3073. var ep = new IPEndPoint (IPAddress.Any, NetworkHelpers.FindFreePort ());
  3074. s.Bind (ep);
  3075. s.Listen(1);
  3076. if (supportsReuseAddress)
  3077. ss.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
  3078. try {
  3079. ss.Bind (new IPEndPoint (IPAddress.Any, ep.Port));
  3080. ss.Listen(1);
  3081. if (!supportsReuseAddress)
  3082. Assert.Fail ("Reusing address is not supported, exception was expected on second bind.");
  3083. } catch (SocketException e) {
  3084. }
  3085. }
  3086. }
  3087. [Test]
  3088. [Category ("NotOnMac")]
  3089. public void ConnectedProperty ()
  3090. {
  3091. TcpListener listener = new TcpListener (IPAddress.Loopback, NetworkHelpers.FindFreePort ());
  3092. listener.Start();
  3093. Socket client = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  3094. client.Connect (IPAddress.Loopback, ((IPEndPoint)listener.LocalEndpoint).Port);
  3095. Socket server = listener.AcceptSocket ();
  3096. try {
  3097. server.EndSend(server.BeginSend (new byte[10], 0, 10, SocketFlags.None, null, null));
  3098. client.Close ();
  3099. try {
  3100. server.EndReceive (server.BeginReceive (new byte[10], 0, 10, SocketFlags.None, null, null));
  3101. } catch {
  3102. }
  3103. Assert.IsTrue (!client.Connected);
  3104. Assert.IsTrue (!server.Connected);
  3105. } finally {
  3106. listener.Stop ();
  3107. client.Close ();
  3108. server.Close ();
  3109. }
  3110. }
  3111. [Test] // GetSocketOption (SocketOptionLevel, SocketOptionName)
  3112. public void GetSocketOption1_Socket_Closed ()
  3113. {
  3114. Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  3115. s.Close ();
  3116. try {
  3117. s.GetSocketOption (0, 0);
  3118. Assert.Fail ("#1");
  3119. } catch (ObjectDisposedException ex) {
  3120. // Cannot access a disposed object
  3121. Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
  3122. Assert.IsNull (ex.InnerException, "#3");
  3123. Assert.IsNotNull (ex.Message, "#4");
  3124. Assert.AreEqual (typeof (Socket).FullName, ex.ObjectName, "#5");
  3125. }
  3126. }
  3127. [Test] // GetSocketOption (SocketOptionLevel, SocketOptionName, Byte [])
  3128. public void GetSocketOption2_OptionValue_Null ()
  3129. {
  3130. Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  3131. try {
  3132. s.GetSocketOption (SocketOptionLevel.Socket, SocketOptionName.Linger,
  3133. (byte []) null);
  3134. Assert.Fail ("#1");
  3135. } catch (SocketException ex) {
  3136. // The system detected an invalid pointer address in attempting
  3137. // to use a pointer argument in a call
  3138. Assert.AreEqual (typeof (SocketException), ex.GetType (), "#2");
  3139. Assert.AreEqual (10014, ex.ErrorCode, "#3");
  3140. Assert.IsNull (ex.InnerException, "#4");
  3141. Assert.IsNotNull (ex.Message, "#5");
  3142. Assert.AreEqual (10014, ex.NativeErrorCode, "#6");
  3143. Assert.AreEqual (SocketError.Fault, ex.SocketErrorCode, "#7");
  3144. }
  3145. }
  3146. [Test] // GetSocketOption (SocketOptionLevel, SocketOptionName, Byte [])
  3147. public void GetSocketOption2_Socket_Closed ()
  3148. {
  3149. Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  3150. s.Close ();
  3151. try {
  3152. s.GetSocketOption (0, 0, (byte []) null);
  3153. Assert.Fail ("#1");
  3154. } catch (ObjectDisposedException ex) {
  3155. // Cannot access a disposed object
  3156. Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
  3157. Assert.IsNull (ex.InnerException, "#3");
  3158. Assert.IsNotNull (ex.Message, "#4");
  3159. Assert.AreEqual (typeof (Socket).FullName, ex.ObjectName, "#5");
  3160. }
  3161. }
  3162. [Test] // GetSocketOption (SocketOptionLevel, SocketOptionName, Int32)
  3163. public void GetSocketOption3_Socket_Closed ()
  3164. {
  3165. Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  3166. s.Close ();
  3167. try {
  3168. s.GetSocketOption (0, 0, 0);
  3169. Assert.Fail ("#1");
  3170. } catch (ObjectDisposedException ex) {
  3171. // Cannot access a disposed object
  3172. Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
  3173. Assert.IsNull (ex.InnerException, "#3");
  3174. Assert.IsNotNull (ex.Message, "#4");
  3175. Assert.AreEqual (typeof (Socket).FullName, ex.ObjectName, "#5");
  3176. }
  3177. }
  3178. [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Byte [])
  3179. public void SetSocketOption1_DontLinger ()
  3180. {
  3181. using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) {
  3182. s.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.DontLinger,
  3183. new byte [] { 0x00 });
  3184. s.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.DontLinger,
  3185. new byte [] { 0x01 });
  3186. }
  3187. }
  3188. [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Byte [])
  3189. public void SetSocketOption1_DontLinger_Null ()
  3190. {
  3191. using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) {
  3192. try {
  3193. s.SetSocketOption (SocketOptionLevel.Socket,
  3194. SocketOptionName.DontLinger, (byte []) null);
  3195. Assert.Fail ("#1");
  3196. } catch (SocketException ex) {
  3197. // The system detected an invalid pointer address in attempting
  3198. // to use a pointer argument in a call
  3199. Assert.AreEqual (typeof (SocketException), ex.GetType (), "#2");
  3200. Assert.AreEqual (10014, ex.ErrorCode, "#3");
  3201. Assert.IsNull (ex.InnerException, "#4");
  3202. Assert.IsNotNull (ex.Message, "#5");
  3203. Assert.AreEqual (10014, ex.NativeErrorCode, "#6");
  3204. Assert.AreEqual (SocketError.Fault, ex.SocketErrorCode, "#7");
  3205. }
  3206. }
  3207. }
  3208. [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Byte [])
  3209. public void SetSocketOption1_Linger_Null ()
  3210. {
  3211. using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) {
  3212. try {
  3213. s.SetSocketOption (SocketOptionLevel.Socket,
  3214. SocketOptionName.DontLinger, (byte []) null);
  3215. Assert.Fail ("#1");
  3216. } catch (SocketException ex) {
  3217. // The system detected an invalid pointer address in attempting
  3218. // to use a pointer argument in a call
  3219. Assert.AreEqual (typeof (SocketException), ex.GetType (), "#2");
  3220. Assert.AreEqual (10014, ex.ErrorCode, "#3");
  3221. Assert.IsNull (ex.InnerException, "#4");
  3222. Assert.IsNotNull (ex.Message, "#5");
  3223. Assert.AreEqual (10014, ex.NativeErrorCode, "#6");
  3224. Assert.AreEqual (SocketError.Fault, ex.SocketErrorCode, "#7");
  3225. }
  3226. }
  3227. }
  3228. [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Byte [])
  3229. public void SetSocketOption1_Socket_Close ()
  3230. {
  3231. Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  3232. s.Close ();
  3233. try {
  3234. s.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.DontLinger,
  3235. new byte [] { 0x00 });
  3236. Assert.Fail ("#1");
  3237. } catch (ObjectDisposedException ex) {
  3238. Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
  3239. Assert.IsNull (ex.InnerException, "#3");
  3240. Assert.IsNotNull (ex.Message, "#4");
  3241. Assert.AreEqual (typeof (Socket).FullName, ex.ObjectName, "#5");
  3242. }
  3243. }
  3244. [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Int32)
  3245. public void SetSocketOption2_DontLinger ()
  3246. {
  3247. using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) {
  3248. s.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.DontLinger, 0);
  3249. s.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.DontLinger, 5);
  3250. }
  3251. }
  3252. [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Int32)
  3253. [Category ("NotWorking")]
  3254. public void SetSocketOption2_Linger ()
  3255. {
  3256. using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) {
  3257. s.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.Linger, 0);
  3258. s.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.Linger, 5);
  3259. }
  3260. }
  3261. [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Int32)
  3262. public void SetSocketOption2_Socket_Closed ()
  3263. {
  3264. Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  3265. s.Close ();
  3266. try {
  3267. s.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.DontLinger, 0);
  3268. Assert.Fail ("#1");
  3269. } catch (ObjectDisposedException ex) {
  3270. // Cannot access a disposed object
  3271. Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
  3272. Assert.IsNull (ex.InnerException, "#3");
  3273. Assert.IsNotNull (ex.Message, "#4");
  3274. Assert.AreEqual (typeof (Socket).FullName, ex.ObjectName, "#5");
  3275. }
  3276. }
  3277. [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Object)
  3278. #if FEATURE_NO_BSD_SOCKETS
  3279. [ExpectedException (typeof (PlatformNotSupportedException))]
  3280. #endif
  3281. public void SetSocketOption3_AddMembershipIPv4_IPv6MulticastOption ()
  3282. {
  3283. IPAddress mcast_addr = IPAddress.Parse ("239.255.255.250");
  3284. using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp)) {
  3285. s.Bind (new IPEndPoint (IPAddress.Any, NetworkHelpers.FindFreePort ()));
  3286. try {
  3287. s.SetSocketOption (SocketOptionLevel.IP, SocketOptionName.AddMembership,
  3288. new IPv6MulticastOption (mcast_addr));
  3289. Assert.Fail ("#1");
  3290. } catch (ArgumentException ex) {
  3291. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
  3292. Assert.IsNull (ex.InnerException, "#3");
  3293. Assert.IsNotNull (ex.Message, "#4");
  3294. // The specified value is not a valid 'MulticastOption'
  3295. Assert.IsTrue (ex.Message.IndexOf ("'MulticastOption'") != -1, "#5:" + ex.Message);
  3296. Assert.AreEqual ("optionValue", ex.ParamName, "#6");
  3297. }
  3298. }
  3299. }
  3300. [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Object)
  3301. #if FEATURE_NO_BSD_SOCKETS
  3302. [ExpectedException (typeof (PlatformNotSupportedException))]
  3303. #endif
  3304. public void SetSocketOption3_AddMembershipIPv4_MulticastOption ()
  3305. {
  3306. IPAddress mcast_addr = IPAddress.Parse ("239.255.255.250");
  3307. using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp)) {
  3308. s.Bind (new IPEndPoint (IPAddress.Any, NetworkHelpers.FindFreePort ()));
  3309. s.SetSocketOption (SocketOptionLevel.IP, SocketOptionName.AddMembership,
  3310. new MulticastOption (mcast_addr));
  3311. }
  3312. }
  3313. [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Object)
  3314. [Category ("NotWorking")]
  3315. public void SetSocketOption3_AddMembershipIPv4_Socket_NotBound ()
  3316. {
  3317. IPAddress mcast_addr = IPAddress.Parse ("239.255.255.250");
  3318. Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
  3319. try {
  3320. s.SetSocketOption (SocketOptionLevel.IP, SocketOptionName.AddMembership,
  3321. new MulticastOption (mcast_addr));
  3322. Assert.Fail ("#1");
  3323. } catch (SocketException ex) {
  3324. // An invalid argument was supplied
  3325. Assert.AreEqual (typeof (SocketException), ex.GetType (), "#2");
  3326. Assert.AreEqual (10022, ex.ErrorCode, "#3");
  3327. Assert.IsNull (ex.InnerException, "#4");
  3328. Assert.IsNotNull (ex.Message, "#5");
  3329. Assert.AreEqual (10022, ex.NativeErrorCode, "#6");
  3330. Assert.AreEqual (SocketError.InvalidArgument, ex.SocketErrorCode, "#7");
  3331. } finally {
  3332. s.Close ();
  3333. }
  3334. }
  3335. [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Object)
  3336. #if FEATURE_NO_BSD_SOCKETS
  3337. [ExpectedException (typeof (PlatformNotSupportedException))]
  3338. #endif
  3339. public void SetSocketOption3_AddMembershipIPv6_IPv6MulticastOption ()
  3340. {
  3341. if (!Socket.OSSupportsIPv6)
  3342. Assert.Ignore ("IPv6 not enabled.");
  3343. IPAddress mcast_addr = IPAddress.Parse ("ff02::1");
  3344. using (Socket s = new Socket (AddressFamily.InterNetworkV6, SocketType.Dgram, ProtocolType.Udp)) {
  3345. s.Bind (new IPEndPoint (IPAddress.IPv6Any, NetworkHelpers.FindFreePort ()));
  3346. s.SetSocketOption (SocketOptionLevel.IPv6, SocketOptionName.AddMembership,
  3347. new IPv6MulticastOption (mcast_addr));
  3348. }
  3349. }
  3350. [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Object)
  3351. #if FEATURE_NO_BSD_SOCKETS
  3352. [ExpectedException (typeof (PlatformNotSupportedException))]
  3353. #endif
  3354. public void SetSocketOption3_AddMembershipIPv6_MulticastOption ()
  3355. {
  3356. if (!Socket.OSSupportsIPv6)
  3357. Assert.Ignore ("IPv6 not enabled.");
  3358. IPAddress mcast_addr = IPAddress.Parse ("ff02::1");
  3359. using (Socket s = new Socket (AddressFamily.InterNetworkV6, SocketType.Dgram, ProtocolType.Udp)) {
  3360. s.Bind (new IPEndPoint (IPAddress.IPv6Any, NetworkHelpers.FindFreePort ()));
  3361. try {
  3362. s.SetSocketOption (SocketOptionLevel.IPv6, SocketOptionName.AddMembership,
  3363. new MulticastOption (mcast_addr));
  3364. Assert.Fail ("#1");
  3365. } catch (ArgumentException ex) {
  3366. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
  3367. Assert.IsNull (ex.InnerException, "#3");
  3368. Assert.IsNotNull (ex.Message, "#4");
  3369. // The specified value is not a valid 'IPv6MulticastOption'
  3370. Assert.IsTrue (ex.Message.IndexOf ("'IPv6MulticastOption'") != -1, "#5:" + ex.Message);
  3371. Assert.AreEqual ("optionValue", ex.ParamName, "#6");
  3372. }
  3373. }
  3374. }
  3375. [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Object)
  3376. [Category ("NotWorking")]
  3377. public void SetSocketOption3_AddMembershipIPv6_Socket_NotBound ()
  3378. {
  3379. IPAddress mcast_addr = IPAddress.Parse ("ff02::1");
  3380. Socket s = new Socket (AddressFamily.InterNetworkV6, SocketType.Dgram, ProtocolType.Udp);
  3381. try {
  3382. s.SetSocketOption (SocketOptionLevel.IPv6, SocketOptionName.AddMembership,
  3383. new IPv6MulticastOption (mcast_addr));
  3384. Assert.Fail ("#1");
  3385. } catch (SocketException ex) {
  3386. // An invalid argument was supplied
  3387. Assert.AreEqual (typeof (SocketException), ex.GetType (), "#2");
  3388. Assert.AreEqual (10022, ex.ErrorCode, "#3");
  3389. Assert.IsNull (ex.InnerException, "#4");
  3390. Assert.IsNotNull (ex.Message, "#5");
  3391. Assert.AreEqual (10022, ex.NativeErrorCode, "#6");
  3392. Assert.AreEqual (SocketError.InvalidArgument, ex.SocketErrorCode, "#7");
  3393. } finally {
  3394. s.Close ();
  3395. }
  3396. }
  3397. [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Object)
  3398. public void SetSocketOption3_DontLinger_Boolean ()
  3399. {
  3400. using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) {
  3401. try {
  3402. s.SetSocketOption (SocketOptionLevel.Socket,
  3403. SocketOptionName.DontLinger, (object) false);
  3404. Assert.Fail ("#1");
  3405. } catch (ArgumentException ex) {
  3406. // The specified value is not valid
  3407. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
  3408. Assert.IsNull (ex.InnerException, "#3");
  3409. Assert.IsNotNull (ex.Message, "#4");
  3410. Assert.AreEqual ("optionValue", ex.ParamName, "#5");
  3411. }
  3412. }
  3413. }
  3414. [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Object)
  3415. public void SetSocketOption3_DontLinger_Int32 ()
  3416. {
  3417. using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) {
  3418. try {
  3419. s.SetSocketOption (SocketOptionLevel.Socket,
  3420. SocketOptionName.DontLinger, (object) 0);
  3421. Assert.Fail ("#1");
  3422. } catch (ArgumentException ex) {
  3423. // The specified value is not valid
  3424. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
  3425. Assert.IsNull (ex.InnerException, "#3");
  3426. Assert.IsNotNull (ex.Message, "#4");
  3427. Assert.AreEqual ("optionValue", ex.ParamName, "#5");
  3428. }
  3429. }
  3430. }
  3431. [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Object)
  3432. public void SetSocketOption3_DontLinger_LingerOption ()
  3433. {
  3434. using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) {
  3435. try {
  3436. s.SetSocketOption (SocketOptionLevel.Socket,
  3437. SocketOptionName.DontLinger, new LingerOption (true, 1000));
  3438. Assert.Fail ("#1");
  3439. } catch (ArgumentException ex) {
  3440. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
  3441. Assert.IsNull (ex.InnerException, "#3");
  3442. // The specified value is not valid
  3443. Assert.IsNotNull (ex.Message, "#4");
  3444. Assert.AreEqual ("optionValue", ex.ParamName, "#5");
  3445. }
  3446. }
  3447. }
  3448. [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Object)
  3449. public void SetSocketOption3_Linger_Boolean ()
  3450. {
  3451. using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) {
  3452. try {
  3453. s.SetSocketOption (SocketOptionLevel.Socket,
  3454. SocketOptionName.Linger, (object) false);
  3455. Assert.Fail ("#1");
  3456. } catch (ArgumentException ex) {
  3457. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
  3458. Assert.IsNull (ex.InnerException, "#3");
  3459. // The specified value is not valid
  3460. Assert.IsNotNull (ex.Message, "#4");
  3461. Assert.AreEqual ("optionValue", ex.ParamName, "#5");
  3462. }
  3463. }
  3464. }
  3465. [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Object)
  3466. public void SetSocketOption3_Linger_Int32 ()
  3467. {
  3468. using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) {
  3469. try {
  3470. s.SetSocketOption (SocketOptionLevel.Socket,
  3471. SocketOptionName.Linger, (object) 0);
  3472. Assert.Fail ("#1");
  3473. } catch (ArgumentException ex) {
  3474. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
  3475. Assert.IsNull (ex.InnerException, "#3");
  3476. // The specified value is not valid
  3477. Assert.IsNotNull (ex.Message, "#4");
  3478. Assert.AreEqual ("optionValue", ex.ParamName, "#5");
  3479. }
  3480. }
  3481. }
  3482. [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Object)
  3483. public void SetSocketOption3_Linger_LingerOption ()
  3484. {
  3485. using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) {
  3486. s.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.Linger,
  3487. new LingerOption (false, 0));
  3488. s.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.Linger,
  3489. new LingerOption (true, 0));
  3490. s.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.Linger,
  3491. new LingerOption (false, 1000));
  3492. s.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.Linger,
  3493. new LingerOption (true, 1000));
  3494. }
  3495. }
  3496. [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Object)
  3497. #if FEATURE_NO_BSD_SOCKETS
  3498. [ExpectedException (typeof (PlatformNotSupportedException))]
  3499. #endif
  3500. public void SetSocketOption3_DropMembershipIPv4_IPv6MulticastOption ()
  3501. {
  3502. IPAddress mcast_addr = IPAddress.Parse ("239.255.255.250");
  3503. using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp)) {
  3504. s.Bind (new IPEndPoint (IPAddress.Any, NetworkHelpers.FindFreePort ()));
  3505. s.SetSocketOption (SocketOptionLevel.IP, SocketOptionName.AddMembership,
  3506. new MulticastOption (mcast_addr));
  3507. try {
  3508. s.SetSocketOption (SocketOptionLevel.IP, SocketOptionName.DropMembership,
  3509. new IPv6MulticastOption (mcast_addr));
  3510. Assert.Fail ("#1");
  3511. } catch (ArgumentException ex) {
  3512. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
  3513. Assert.IsNull (ex.InnerException, "#3");
  3514. Assert.IsNotNull (ex.Message, "#4");
  3515. // The specified value is not a valid 'MulticastOption'
  3516. Assert.IsTrue (ex.Message.IndexOf ("'MulticastOption'") != -1, "#5:" + ex.Message);
  3517. Assert.AreEqual ("optionValue", ex.ParamName, "#6");
  3518. }
  3519. }
  3520. }
  3521. [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Object)
  3522. #if FEATURE_NO_BSD_SOCKETS
  3523. [ExpectedException (typeof (PlatformNotSupportedException))]
  3524. #endif
  3525. public void SetSocketOption3_DropMembershipIPv4_MulticastOption ()
  3526. {
  3527. IPAddress mcast_addr = IPAddress.Parse ("239.255.255.250");
  3528. using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp)) {
  3529. MulticastOption option = new MulticastOption (mcast_addr);
  3530. s.Bind (new IPEndPoint (IPAddress.Any, NetworkHelpers.FindFreePort ()));
  3531. s.SetSocketOption (SocketOptionLevel.IP, SocketOptionName.AddMembership,
  3532. option);
  3533. s.SetSocketOption (SocketOptionLevel.IP, SocketOptionName.DropMembership,
  3534. option);
  3535. }
  3536. }
  3537. [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Object)
  3538. [Category ("NotWorking")]
  3539. public void SetSocketOption3_DropMembershipIPv4_Socket_NotBound ()
  3540. {
  3541. IPAddress mcast_addr = IPAddress.Parse ("239.255.255.250");
  3542. Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
  3543. try {
  3544. s.SetSocketOption (SocketOptionLevel.IP, SocketOptionName.DropMembership,
  3545. new MulticastOption (mcast_addr));
  3546. Assert.Fail ("#1");
  3547. } catch (SocketException ex) {
  3548. // An invalid argument was supplied
  3549. Assert.AreEqual (typeof (SocketException), ex.GetType (), "#2");
  3550. Assert.AreEqual (10022, ex.ErrorCode, "#3");
  3551. Assert.IsNull (ex.InnerException, "#4");
  3552. Assert.IsNotNull (ex.Message, "#5");
  3553. Assert.AreEqual (10022, ex.NativeErrorCode, "#6");
  3554. Assert.AreEqual (SocketError.InvalidArgument, ex.SocketErrorCode, "#7");
  3555. } finally {
  3556. s.Close ();
  3557. }
  3558. }
  3559. [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Object)
  3560. #if FEATURE_NO_BSD_SOCKETS
  3561. [ExpectedException (typeof (PlatformNotSupportedException))]
  3562. #endif
  3563. public void SetSocketOption3_DropMembershipIPv6_IPv6MulticastOption ()
  3564. {
  3565. if (!Socket.OSSupportsIPv6)
  3566. Assert.Ignore ("IPv6 not enabled.");
  3567. using (Socket s = new Socket (AddressFamily.InterNetworkV6, SocketType.Dgram, ProtocolType.Udp)) {
  3568. IPv6MulticastOption option = new IPv6MulticastOption (
  3569. IPAddress.Parse ("ff02::1"));
  3570. s.Bind (new IPEndPoint (IPAddress.IPv6Any, 1902));
  3571. s.SetSocketOption (SocketOptionLevel.IPv6, SocketOptionName.AddMembership,
  3572. option);
  3573. s.SetSocketOption (SocketOptionLevel.IPv6, SocketOptionName.DropMembership,
  3574. option);
  3575. }
  3576. }
  3577. [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Object)
  3578. #if FEATURE_NO_BSD_SOCKETS
  3579. [ExpectedException (typeof (PlatformNotSupportedException))]
  3580. #endif
  3581. public void SetSocketOption3_DropMembershipIPv6_MulticastOption ()
  3582. {
  3583. if (!Socket.OSSupportsIPv6)
  3584. Assert.Ignore ("IPv6 not enabled.");
  3585. IPAddress mcast_addr = IPAddress.Parse ("ff02::1");
  3586. using (Socket s = new Socket (AddressFamily.InterNetworkV6, SocketType.Dgram, ProtocolType.Udp)) {
  3587. s.Bind (new IPEndPoint (IPAddress.IPv6Any, NetworkHelpers.FindFreePort ()));
  3588. s.SetSocketOption (SocketOptionLevel.IPv6, SocketOptionName.AddMembership,
  3589. new IPv6MulticastOption (mcast_addr));
  3590. try {
  3591. s.SetSocketOption (SocketOptionLevel.IPv6, SocketOptionName.DropMembership,
  3592. new MulticastOption (mcast_addr));
  3593. Assert.Fail ("#1");
  3594. } catch (ArgumentException ex) {
  3595. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
  3596. Assert.IsNull (ex.InnerException, "#3");
  3597. Assert.IsNotNull (ex.Message, "#4");
  3598. // The specified value is not a valid 'IPv6MulticastOption'
  3599. Assert.IsTrue (ex.Message.IndexOf ("'IPv6MulticastOption'") != -1, "#5:" + ex.Message);
  3600. Assert.AreEqual ("optionValue", ex.ParamName, "#6");
  3601. }
  3602. }
  3603. }
  3604. [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Object)
  3605. [Category ("NotWorking")]
  3606. public void SetSocketOption3_DropMembershipIPv6_Socket_NotBound ()
  3607. {
  3608. IPAddress mcast_addr = IPAddress.Parse ("ff02::1");
  3609. Socket s = new Socket (AddressFamily.InterNetworkV6, SocketType.Dgram, ProtocolType.Udp);
  3610. try {
  3611. s.SetSocketOption (SocketOptionLevel.IPv6, SocketOptionName.DropMembership,
  3612. new IPv6MulticastOption (mcast_addr));
  3613. Assert.Fail ("#1");
  3614. } catch (SocketException ex) {
  3615. // An invalid argument was supplied
  3616. Assert.AreEqual (typeof (SocketException), ex.GetType (), "#2");
  3617. Assert.AreEqual (10022, ex.ErrorCode, "#3");
  3618. Assert.IsNull (ex.InnerException, "#4");
  3619. Assert.IsNotNull (ex.Message, "#5");
  3620. Assert.AreEqual (10022, ex.NativeErrorCode, "#6");
  3621. Assert.AreEqual (SocketError.InvalidArgument, ex.SocketErrorCode, "#7");
  3622. } finally {
  3623. s.Close ();
  3624. }
  3625. }
  3626. [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Object)
  3627. public void SetSocketOption3_OptionValue_Null ()
  3628. {
  3629. using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) {
  3630. try {
  3631. s.SetSocketOption (SocketOptionLevel.Socket,
  3632. SocketOptionName.Linger, (object) null);
  3633. Assert.Fail ("#1");
  3634. } catch (ArgumentNullException ex) {
  3635. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  3636. Assert.IsNull (ex.InnerException, "#3");
  3637. Assert.IsNotNull (ex.Message, "#4");
  3638. Assert.AreEqual ("optionValue", ex.ParamName, "#5");
  3639. }
  3640. }
  3641. }
  3642. [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Object)
  3643. public void SetSocketOption3_Socket_Closed ()
  3644. {
  3645. Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  3646. s.Close ();
  3647. try {
  3648. s.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.Linger,
  3649. new LingerOption (false, 0));
  3650. Assert.Fail ("#1");
  3651. } catch (ObjectDisposedException ex) {
  3652. // Cannot access a disposed object
  3653. Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
  3654. Assert.IsNull (ex.InnerException, "#3");
  3655. Assert.IsNotNull (ex.Message, "#4");
  3656. Assert.AreEqual (typeof (Socket).FullName, ex.ObjectName, "#5");
  3657. }
  3658. }
  3659. [Test]
  3660. [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.
  3661. public void SetSocketOption_MulticastInterfaceIndex_Any ()
  3662. {
  3663. IPAddress ip = IPAddress.Parse ("239.255.255.250");
  3664. int index = 0;
  3665. using (Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp))
  3666. {
  3667. s.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.MulticastInterface, IPAddress.HostToNetworkOrder(index));
  3668. s.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, new MulticastOption(ip, index));
  3669. }
  3670. }
  3671. [Test]
  3672. [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.
  3673. public void SetSocketOption_MulticastInterfaceIndex_Loopback ()
  3674. {
  3675. IPAddress ip = IPAddress.Parse ("239.255.255.250");
  3676. int index = 1;
  3677. using (Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp))
  3678. {
  3679. s.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.MulticastInterface, IPAddress.HostToNetworkOrder(index));
  3680. s.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, new MulticastOption(ip, index));
  3681. }
  3682. }
  3683. [Test]
  3684. public void SetSocketOption_MulticastInterfaceIndex_Invalid ()
  3685. {
  3686. IPAddress ip = IPAddress.Parse ("239.255.255.250");
  3687. int index = 31415;
  3688. using (Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp))
  3689. {
  3690. try
  3691. {
  3692. s.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.MulticastInterface, IPAddress.HostToNetworkOrder(index));
  3693. Assert.Fail ("#1");
  3694. }
  3695. catch
  3696. {}
  3697. try
  3698. {
  3699. s.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, new MulticastOption(ip, index));
  3700. Assert.Fail ("#2");
  3701. }
  3702. catch
  3703. {}
  3704. }
  3705. }
  3706. [Test]
  3707. #if FEATURE_NO_BSD_SOCKETS
  3708. [ExpectedException (typeof (PlatformNotSupportedException))]
  3709. #endif
  3710. public void Shutdown_NoConnect ()
  3711. {
  3712. Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  3713. s.Bind (new IPEndPoint (IPAddress.Loopback, NetworkHelpers.FindFreePort ()));
  3714. s.Listen (1);
  3715. try {
  3716. s.Shutdown (SocketShutdown.Both);
  3717. Assert.Fail ("#1");
  3718. } catch (SocketException exc) {
  3719. Assert.AreEqual (10057, exc.ErrorCode, "#2");
  3720. } finally {
  3721. s.Close ();
  3722. }
  3723. }
  3724. [Test]
  3725. [ExpectedException (typeof (NullReferenceException))]
  3726. public void ReceiveAsync_Null ()
  3727. {
  3728. using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) {
  3729. s.ReceiveAsync (null);
  3730. }
  3731. }
  3732. [Test]
  3733. [ExpectedException (typeof (NullReferenceException))]
  3734. public void ReceiveAsync_Default ()
  3735. {
  3736. using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) {
  3737. SocketAsyncEventArgs saea = new SocketAsyncEventArgs ();
  3738. s.ReceiveAsync (saea);
  3739. }
  3740. }
  3741. [Test]
  3742. [ExpectedException (typeof (NullReferenceException))]
  3743. public void ReceiveAsync_NullBuffer ()
  3744. {
  3745. using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) {
  3746. SocketAsyncEventArgs saea = new SocketAsyncEventArgs ();
  3747. saea.SetBuffer (null, 0, 0);
  3748. s.ReceiveAsync (null);
  3749. }
  3750. }
  3751. [Test]
  3752. [ExpectedException (typeof (ObjectDisposedException))]
  3753. public void ReceiveAsync_ClosedSocket ()
  3754. {
  3755. Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  3756. s.Close ();
  3757. s.ReceiveAsync (null);
  3758. }
  3759. [Test]
  3760. [ExpectedException (typeof (NullReferenceException))]
  3761. public void SendAsync_Null ()
  3762. {
  3763. using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) {
  3764. s.SendAsync (null);
  3765. }
  3766. }
  3767. [Test]
  3768. [ExpectedException (typeof (NullReferenceException))]
  3769. public void SendAsync_Default ()
  3770. {
  3771. using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) {
  3772. SocketAsyncEventArgs saea = new SocketAsyncEventArgs ();
  3773. s.SendAsync (saea);
  3774. }
  3775. }
  3776. [Test]
  3777. [ExpectedException (typeof (NullReferenceException))]
  3778. public void SendAsync_NullBuffer ()
  3779. {
  3780. using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) {
  3781. SocketAsyncEventArgs saea = new SocketAsyncEventArgs ();
  3782. saea.SetBuffer (null, 0, 0);
  3783. s.SendAsync (null);
  3784. }
  3785. }
  3786. [Test]
  3787. [ExpectedException (typeof (ObjectDisposedException))]
  3788. public void SendAsync_ClosedSocket ()
  3789. {
  3790. Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  3791. s.Close ();
  3792. s.SendAsync (null);
  3793. }
  3794. [Test]
  3795. #if FEATURE_NO_BSD_SOCKETS
  3796. [ExpectedException (typeof (PlatformNotSupportedException))]
  3797. #endif
  3798. public void SendAsyncFile ()
  3799. {
  3800. Socket serverSocket = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  3801. serverSocket.Bind (new IPEndPoint (IPAddress.Loopback, 0));
  3802. serverSocket.Listen (1);
  3803. var mReceived = new ManualResetEvent (false);
  3804. serverSocket.BeginAccept (AsyncCall => {
  3805. byte[] bytes = new byte [1024];
  3806. Socket listener = (Socket)AsyncCall.AsyncState;
  3807. Socket client = listener.EndAccept (AsyncCall);
  3808. client.Receive (bytes, bytes.Length, 0);
  3809. client.Close ();
  3810. mReceived.Set ();
  3811. }, serverSocket);
  3812. Socket clientSocket = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  3813. clientSocket.Connect (serverSocket.LocalEndPoint);
  3814. clientSocket.NoDelay = true;
  3815. // Initialize buffer used to create testing file
  3816. var buffer = new byte [1024];
  3817. for (int i = 0; i < 1024; ++i)
  3818. buffer [i] = (byte) (i % 256);
  3819. string temp = Path.GetTempFileName ();
  3820. try {
  3821. // Testing file creation
  3822. using (StreamWriter sw = new StreamWriter (temp)) {
  3823. sw.Write (buffer);
  3824. }
  3825. var mSent = new ManualResetEvent (false);
  3826. // Async Send File to server
  3827. clientSocket.BeginSendFile(temp, (ar) => {
  3828. Socket client = (Socket) ar.AsyncState;
  3829. client.EndSendFile (ar);
  3830. mSent.Set ();
  3831. }, clientSocket);
  3832. if (!mSent.WaitOne (1500))
  3833. throw new TimeoutException ();
  3834. if (!mReceived.WaitOne (1500))
  3835. throw new TimeoutException ();
  3836. } finally {
  3837. if (File.Exists (temp))
  3838. File.Delete (temp);
  3839. clientSocket.Close ();
  3840. serverSocket.Close ();
  3841. }
  3842. }
  3843. [Test]
  3844. #if FEATURE_NO_BSD_SOCKETS
  3845. [ExpectedException (typeof (PlatformNotSupportedException))]
  3846. #endif
  3847. public void ConnectToIPV4EndPointUsingDualModelSocket () {
  3848. /*
  3849. * IPv6 DualMode sockets are defaults in Mono. Explicitly specify that
  3850. * anyways in this test to make it more interoparable with .NET where
  3851. * IPv6 and DualMode needs to be specified.
  3852. */
  3853. using (var server = new Socket (AddressFamily.InterNetworkV6, SocketType.Stream, ProtocolType.Tcp)) {
  3854. var host = new IPEndPoint (IPAddress.Loopback, NetworkHelpers.FindFreePort ());
  3855. server.DualMode = true;
  3856. server.Bind (host);
  3857. /*
  3858. * Nothing to Accept the connect - we need a backlog to make sure we don't get
  3859. Connection refused.
  3860. */
  3861. server.Listen (3);
  3862. var ep = server.LocalEndPoint as IPEndPoint;
  3863. var client = new Socket (AddressFamily.InterNetworkV6, SocketType.Stream, ProtocolType.Tcp);
  3864. client.DualMode = true;
  3865. client.Connect (ep);
  3866. client.Disconnect (false);
  3867. client.Close ();
  3868. client = new Socket (AddressFamily.InterNetworkV6, SocketType.Stream, ProtocolType.Tcp);
  3869. client.DualMode = true;
  3870. client.Connect (IPAddress.Loopback, ep.Port);
  3871. client.Disconnect (false);
  3872. client.Close ();
  3873. client = new Socket (AddressFamily.InterNetworkV6, SocketType.Stream, ProtocolType.Tcp);
  3874. client.DualMode = true;
  3875. client.Connect (new [] { IPAddress.Loopback }, ep.Port);
  3876. client.Disconnect (false);
  3877. client.Close ();
  3878. }
  3879. }
  3880. [Test]
  3881. #if FEATURE_NO_BSD_SOCKETS
  3882. [ExpectedException (typeof (PlatformNotSupportedException))]
  3883. #endif
  3884. public void BeginConnectToIPV4EndPointUsingDualModelSocket () {
  3885. /*
  3886. * IPv6 DualMode sockets are defaults in Mono. Explicitly specify that
  3887. * anyways in this test to make it more interoparable with .NET where
  3888. * IPv6 and DualMode needs to be specified.
  3889. */
  3890. using (var server = new Socket (AddressFamily.InterNetworkV6, SocketType.Stream, ProtocolType.Tcp))
  3891. {
  3892. var host = new IPEndPoint (IPAddress.Loopback, NetworkHelpers.FindFreePort ());
  3893. server.DualMode = true;
  3894. server.Bind (host);
  3895. server.Listen (10);
  3896. var ep = server.LocalEndPoint as IPEndPoint;
  3897. BCCalledBack.Reset ();
  3898. var client = new Socket (AddressFamily.InterNetworkV6, SocketType.Stream, ProtocolType.Tcp);
  3899. client.DualMode = true;
  3900. var ar1 = client.BeginConnect (ep, BCCallback, client);
  3901. Assert.IsTrue (BCCalledBack.WaitOne (10000), "#1");
  3902. client.Disconnect (false);
  3903. client.Close ();
  3904. BCCalledBack.Reset ();
  3905. client = new Socket (AddressFamily.InterNetworkV6, SocketType.Stream, ProtocolType.Tcp);
  3906. client.DualMode = true;
  3907. var ar2 = client.BeginConnect (IPAddress.Loopback, ep.Port, BCCallback, client);
  3908. Assert.IsTrue (BCCalledBack.WaitOne (10000), "#2");
  3909. client.Disconnect (false);
  3910. client.Close ();
  3911. BCCalledBack.Reset ();
  3912. client = new Socket (AddressFamily.InterNetworkV6, SocketType.Stream, ProtocolType.Tcp);
  3913. client.DualMode = true;
  3914. var ar3 = client.BeginConnect (new [] {IPAddress.Loopback}, ep.Port, BCCallback, client);
  3915. Assert.IsTrue (BCCalledBack.WaitOne (10000), "#2");
  3916. client.Disconnect (false);
  3917. client.Close();
  3918. }
  3919. }
  3920. [Test]
  3921. #if FEATURE_NO_BSD_SOCKETS
  3922. [ExpectedException (typeof (PlatformNotSupportedException))]
  3923. #endif
  3924. public void UdpMulticasTimeToLive ()
  3925. {
  3926. /* see https://bugzilla.xamarin.com/show_bug.cgi?id=36941 */
  3927. using (Socket socket = new Socket (AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp)) {
  3928. IPEndPoint end_point = new IPEndPoint (IPAddress.Any, NetworkHelpers.FindFreePort ());
  3929. socket.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, 1);
  3930. socket.Bind (end_point);
  3931. socket.SetSocketOption (SocketOptionLevel.IP, SocketOptionName.MulticastTimeToLive, 19);
  3932. }
  3933. }
  3934. [Test] // Covers 41616
  3935. public void ConnectAsyncUnhandledEx ()
  3936. {
  3937. var mre = new ManualResetEvent (false);
  3938. var endPoint = new IPEndPoint(0,0);
  3939. var socket = new Socket(endPoint.AddressFamily, SocketType.Stream, ProtocolType.Unspecified);
  3940. var socketArgs = new SocketAsyncEventArgs();
  3941. socketArgs.RemoteEndPoint = endPoint;
  3942. socketArgs.Completed += (sender, e) => mre.Set ();
  3943. socket.ConnectAsync (socketArgs);
  3944. Assert.IsTrue (mre.WaitOne (1000), "ConnectedAsync timeout");
  3945. }
  3946. }
  3947. }