SocketTest.cs 119 KB

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