SocketTest.cs 124 KB

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