SocketTest.cs 113 KB

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