SocketTest.cs 125 KB

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