SocketTest.cs 136 KB

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