SocketTest.cs 110 KB

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