SocketTest.cs 121 KB

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