SocketTest.cs 124 KB

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