SocketTest.cs 138 KB

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