SocketTest.cs 127 KB

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