SocketTest.cs 121 KB

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