| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468 |
- // System.Net.Sockets.SocketTest.cs
- //
- // Authors:
- // Brad Fitzpatrick ([email protected])
- // Gonzalo Paniagua Javier ([email protected])
- //
- // (C) Copyright 2003 Brad Fitzpatrick
- // Copyright (c) 2005 Novell, Inc. (http://www.novell.com)
- //
- using System;
- using System.Diagnostics;
- using System.Linq;
- using System.Collections;
- using System.Threading;
- using System.Reflection;
- using System.Text.RegularExpressions;
- using System.Threading.Tasks;
- using System.Net;
- using System.Net.Sockets;
- using NUnit.Framework;
- using System.IO;
- using System.Collections.Generic;
- using MonoTests.Helpers;
- namespace MonoTests.System.Net.Sockets
- {
- [TestFixture]
- [Category ("RequiresBSDSockets")]
- public class SocketTest
- {
- // note: also used in SocketCas tests
- public const string BogusAddress = "192.168.244.244";
- public const int BogusPort = 23483;
- [Test]
- public void ConnectIPAddressAny ()
- {
- IPEndPoint ep = new IPEndPoint (IPAddress.Any, NetworkHelpers.FindFreePort ());
- /* UDP sockets use Any to disconnect
- try {
- using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp)) {
- s.Connect (ep);
- s.Close ();
- }
- Assert.Fail ("#1");
- } catch (SocketException ex) {
- Assert.AreEqual (10049, ex.ErrorCode, "#2");
- }
- */
- try {
- using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) {
- s.Connect (ep);
- s.Close ();
- }
- Assert.Fail ("#3");
- } catch (SocketException ex) {
- Assert.AreEqual (10049, ex.ErrorCode, "#4");
- }
- }
- [Test]
- [Ignore ("Bug #75158")] // Looks like MS fails after the .ctor, when you try to use the socket
- public void IncompatibleAddress ()
- {
- IPEndPoint epIPv6 = new IPEndPoint (IPAddress.IPv6Any,
- NetworkHelpers.FindFreePort ());
- try {
- using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP)) {
- s.Connect (epIPv6);
- s.Close ();
- }
- Assert.Fail ("#1");
- } catch (SocketException ex) {
- // address incompatible with protocol
- int expectedError = 10047;
- Assert.AreEqual (expectedError, ex.ErrorCode,
- "#2");
- }
- }
- [Test]
- [Category ("InetAccess")]
- public void BogusEndConnect ()
- {
- IPAddress ipOne = IPAddress.Parse (BogusAddress);
- IPEndPoint ipEP = new IPEndPoint (ipOne, BogusPort);
- Socket sock = new Socket (ipEP.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
- IAsyncResult ar = sock.BeginConnect (ipEP, null, null);
- try {
- // should raise an exception because connect was bogus
- sock.EndConnect (ar);
- Assert.Fail ("#1");
- } catch (SocketException ex) {
- // Actual error code depends on network configuration.
- var error = (SocketError) ex.ErrorCode;
- Assert.That (error == SocketError.TimedOut ||
- error == SocketError.ConnectionRefused ||
- error == SocketError.NetworkUnreachable ||
- error == SocketError.HostUnreachable, "#2");
- }
- }
- [Test]
- [ExpectedException (typeof (ArgumentNullException))]
- public void SelectEmpty ()
- {
- ArrayList list = new ArrayList ();
- Socket.Select (list, list, list, 1000);
- }
-
- private bool BlockingConnect (bool block, int port)
- {
- IPEndPoint ep = new IPEndPoint(IPAddress.Loopback, port);
- Socket server = new Socket(AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
- server.Bind(ep);
- server.Blocking=block;
- server.Listen(0);
- Socket conn = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
- conn.Connect (ep);
- Socket client = null;
- var sw = Stopwatch.StartNew ();
- while (sw.ElapsedMilliseconds < 100)
- {
- try {
- client = server.Accept();
- break;
- }
- catch (SocketException ex) {
- if (ex.SocketErrorCode == SocketError.WouldBlock)
- continue;
- throw;
- }
- }
- Assert.IsNotNull (client, "Couldn't accept a client connection within 100ms.");
- bool client_block = client.Blocking;
- client.Close();
- conn.Close();
- server.Close();
-
- return(client_block);
- }
- [Test]
- public void AcceptBlockingStatus()
- {
- bool block;
- var port = NetworkHelpers.FindFreePort ();
-
- block = BlockingConnect(true, port);
- Assert.AreEqual (block, true, "BlockingStatus01");
- block = BlockingConnect(false, port);
- Assert.AreEqual (block, false, "BlockingStatus02");
- }
- static bool CFAConnected = false;
- static ManualResetEvent CFACalledBack;
-
- private static void CFACallback (IAsyncResult asyncResult)
- {
- Socket sock = (Socket)asyncResult.AsyncState;
- CFAConnected = sock.Connected;
-
- if (sock.Connected) {
- sock.EndConnect (asyncResult);
- }
- CFACalledBack.Set ();
- }
- [Test] // Connect (IPEndPoint)
- public void Connect1_RemoteEP_Null ()
- {
- Socket s = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream, ProtocolType.Tcp);
- try {
- s.Connect ((IPEndPoint) null);
- Assert.Fail ("#1");
- } catch (ArgumentNullException ex) {
- Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
- Assert.IsNull (ex.InnerException, "#3");
- Assert.IsNotNull (ex.Message, "#4");
- Assert.AreEqual ("remoteEP", ex.ParamName, "#5");
- }
- }
- [Test]
- public void ConnectFailAsync ()
- {
- Socket sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
- sock.Blocking = false;
- CFACalledBack = new ManualResetEvent (false);
- CFACalledBack.Reset ();
- /* Need a port that is not being used for
- * anything...
- */
- sock.BeginConnect (new IPEndPoint (IPAddress.Loopback,
- NetworkHelpers.FindFreePort ()),
- new AsyncCallback (CFACallback),
- sock);
- CFACalledBack.WaitOne ();
- Assert.AreEqual (CFAConnected, false, "ConnectFail");
- }
-
- [Test]
- public void SetSocketOptionBoolean ()
- {
- IPEndPoint ep = new IPEndPoint (IPAddress.Loopback, NetworkHelpers.FindFreePort ());
- Socket sock = new Socket (ep.Address.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
- try {
- sock.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.KeepAlive, true);
- } finally {
- sock.Close ();
- }
- }
- [Test]
- public void TestSelect1 ()
- {
- Socket srv = CreateServer (NetworkHelpers.FindFreePort ());
- ClientSocket clnt = new ClientSocket (srv.LocalEndPoint);
- Thread th = new Thread (new ThreadStart (clnt.ConnectSleepClose));
- Socket acc = null;
- try {
- th.Start ();
- acc = srv.Accept ();
- clnt.Write ();
- ArrayList list = new ArrayList ();
- ArrayList empty = new ArrayList ();
- list.Add (acc);
- Socket.Select (list, empty, empty, 100);
- Assert.AreEqual (0, empty.Count, "#01");
- Assert.AreEqual (1, list.Count, "#02");
- Socket.Select (empty, list, empty, 100);
- Assert.AreEqual (0, empty.Count, "#03");
- Assert.AreEqual (1, list.Count, "#04");
- Socket.Select (list, empty, empty, -1);
- Assert.AreEqual (0, empty.Count, "#05");
- Assert.AreEqual (1, list.Count, "#06");
- // Need to read the 10 bytes from the client to avoid a RST
- byte [] bytes = new byte [10];
- acc.Receive (bytes);
- } finally {
- if (acc != null)
- acc.Close ();
- srv.Close ();
- }
- }
- static Socket CreateServer (int port)
- {
- Socket sock = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
- sock.Bind (new IPEndPoint (IPAddress.Loopback, port));
- sock.Listen (1);
- return sock;
- }
- class ClientSocket {
- Socket sock;
- EndPoint ep;
- public ClientSocket (EndPoint ep)
- {
- this.ep = ep;
- sock = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
- }
- public void ConnectSleepClose ()
- {
- sock.Connect (ep);
- Thread.Sleep (2000);
- sock.Close ();
- }
- public void Write ()
- {
- byte [] b = new byte [10];
- sock.Send (b);
- }
- }
- byte[] buf = new byte[100];
- [Test]
- [ExpectedException (typeof (ObjectDisposedException))]
- public void Disposed2 ()
- {
- Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
- s.Close();
- s.Blocking = true;
- }
- [Test]
- [ExpectedException (typeof (ObjectDisposedException))]
- public void Disposed6 ()
- {
- Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
- s.Close();
- s.Listen (5);
- }
- [Test]
- [ExpectedException (typeof (ObjectDisposedException))]
- public void Disposed7 ()
- {
- Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
- s.Close();
- s.Poll (100, 0);
- }
- [Test]
- [ExpectedException (typeof (ObjectDisposedException))]
- public void Disposed15 ()
- {
- Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
- s.Close();
- s.Send (buf);
- }
- [Test]
- [ExpectedException (typeof (ObjectDisposedException))]
- public void Disposed16 ()
- {
- Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
- s.Close();
- s.Send (buf, 0);
- }
- [Test]
- [ExpectedException (typeof (ObjectDisposedException))]
- public void Disposed17 ()
- {
- Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
- s.Close();
- s.Send (buf, 10, 0);
- }
- [Test]
- [ExpectedException (typeof (ObjectDisposedException))]
- public void Disposed18 ()
- {
- Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
- s.Close();
- s.Send (buf, 0, 10, 0);
- }
- [Test]
- [ExpectedException (typeof (ObjectDisposedException))]
- public void Disposed19 ()
- {
- Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
- EndPoint ep = new IPEndPoint (IPAddress.Any, NetworkHelpers.FindFreePort ());
- s.Close();
- s.SendTo (buf, 0, ep);
- }
- [Test]
- [ExpectedException (typeof (ObjectDisposedException))]
- public void Disposed20 ()
- {
- Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
- EndPoint ep = new IPEndPoint (IPAddress.Any, NetworkHelpers.FindFreePort ());
- s.Close();
- s.SendTo (buf, 10, 0, ep);
- }
- [Test]
- [ExpectedException (typeof (ObjectDisposedException))]
- public void Disposed21 ()
- {
- Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
- EndPoint ep = new IPEndPoint (IPAddress.Any, NetworkHelpers.FindFreePort ());
- s.Close();
- s.SendTo (buf, 0, 10, 0, ep);
- }
- [Test]
- [ExpectedException (typeof (ObjectDisposedException))]
- public void Disposed22 ()
- {
- Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
- EndPoint ep = new IPEndPoint (IPAddress.Any, NetworkHelpers.FindFreePort ());
- s.Close();
- s.SendTo (buf, ep);
- }
- [Test]
- [ExpectedException (typeof (ObjectDisposedException))]
- public void Disposed23 ()
- {
- Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
- s.Close();
- s.Shutdown (0);
- }
- [Test]
- public void GetHashCodeTest ()
- {
- Socket server = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream, ProtocolType.Tcp);
- IPEndPoint ep = new IPEndPoint (IPAddress.Loopback,
- NetworkHelpers.FindFreePort ());
- server.Bind (ep);
- server.Listen (1);
- Socket client = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream, ProtocolType.Tcp);
- int hashcodeA = client.GetHashCode ();
- client.Connect (ep);
- int hashcodeB = client.GetHashCode ();
- Assert.AreEqual (hashcodeA, hashcodeB, "#1");
- client.Close ();
- int hashcodeC = client.GetHashCode ();
- Assert.AreEqual (hashcodeB, hashcodeC, "#2");
- server.Close ();
- }
- static ManualResetEvent SocketError_event = new ManualResetEvent (false);
- private static void SocketError_callback (IAsyncResult ar)
- {
- Socket sock = (Socket)ar.AsyncState;
-
- if(sock.Connected) {
- sock.EndConnect (ar);
- }
- SocketError_event.Set ();
- }
- [Test]
- public void SocketErrorTest ()
- {
- Socket sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
- IPEndPoint ep = new IPEndPoint (IPAddress.Loopback,
- BogusPort);
-
- SocketError_event.Reset ();
- sock.Blocking = false;
- sock.BeginConnect (ep, new AsyncCallback(SocketError_callback),
- sock);
- if (SocketError_event.WaitOne (2000, false) == false) {
- Assert.Fail ("SocketError wait timed out");
- }
- Assert.AreEqual (false, sock.Connected, "SocketError #1");
- int error;
- error = (int)sock.GetSocketOption (SocketOptionLevel.Socket, SocketOptionName.Error);
- Assert.AreEqual (10061, error, "SocketError #2");
- error = (int)sock.GetSocketOption (SocketOptionLevel.Socket, SocketOptionName.Error);
- Assert.AreEqual (10061, error, "SocketError #3");
- sock.Close ();
- }
-
- [Test]
- public void SocketInformationCtor ()
- {
- }
-
- [Test]
- public void DontFragmentDefaultTcp ()
- {
- Socket sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
-
- Assert.AreEqual (false, sock.DontFragment, "DontFragmentDefaultTcp");
- sock.Close ();
- }
- [Test]
- [Category ("NotWorking")] // DontFragment doesn't work
- public void DontFragmentChangeTcp ()
- {
- Socket sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
-
- sock.DontFragment = true;
-
- Assert.AreEqual (true, sock.DontFragment, "DontFragmentChangeTcp");
- sock.Close ();
- }
-
- [Test]
- public void DontFragmentDefaultUdp ()
- {
- Socket sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Dgram,
- ProtocolType.Udp);
-
- Assert.AreEqual (false, sock.DontFragment, "DontFragmentDefaultUdp");
- sock.Close ();
- }
- [Test]
- [Category ("NotWorking")] // DontFragment doesn't work
- public void DontFragmentChangeUdp ()
- {
- Socket sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Dgram,
- ProtocolType.Udp);
-
- sock.DontFragment = true;
-
- Assert.AreEqual (true, sock.DontFragment, "DontFragmentChangeUdp");
- sock.Close ();
- }
- [Test]
- [ExpectedException (typeof(ObjectDisposedException))]
- public void DontFragmentClosed ()
- {
- Socket sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
-
- sock.Close ();
-
- bool val = sock.DontFragment;
- }
-
- [Test]
- [Category ("NotWorking")] // Need to pick a non-IP AddressFamily that "works" on both mono and ms, this one only works on ms
- public void DontFragment ()
- {
- Socket sock = new Socket (AddressFamily.NetBios,
- SocketType.Seqpacket,
- ProtocolType.Unspecified);
-
- try {
- sock.DontFragment = true;
- Assert.Fail ("DontFragment #1");
- } catch (NotSupportedException) {
- } finally {
- sock.Close ();
- }
- }
-
- [Test]
- public void EnableBroadcastDefaultTcp ()
- {
- Socket sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
-
- try {
- bool value = sock.EnableBroadcast;
- Assert.Fail ("EnableBroadcastDefaultTcp #1");
- } catch (SocketException ex) {
- Assert.AreEqual (10042, ex.ErrorCode, "EnableBroadcastDefaultTcp #2");
- } finally {
- sock.Close ();
- }
- }
- [Test]
- public void EnableBroadcastDefaultUdp ()
- {
- Socket sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Dgram,
- ProtocolType.Udp);
-
- Assert.AreEqual (false, sock.EnableBroadcast, "EnableBroadcastDefaultUdp");
- sock.Close ();
- }
-
- [Test]
- public void EnableBroadcastChangeTcp ()
- {
- Socket sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
-
- try {
- sock.EnableBroadcast = true;
- Assert.Fail ("EnableBroadcastChangeTcp #1");
- } catch (SocketException ex) {
- Assert.AreEqual (10042, ex.ErrorCode, "EnableBroadcastChangeTcp #2");
- } finally {
- sock.Close ();
- }
- }
-
- [Test]
- public void EnableBroadcastChangeUdp ()
- {
- Socket sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Dgram,
- ProtocolType.Udp);
-
- sock.EnableBroadcast = true;
-
- Assert.AreEqual (true, sock.EnableBroadcast, "EnableBroadcastChangeUdp");
- sock.Close ();
- }
- [Test]
- [ExpectedException (typeof(ObjectDisposedException))]
- public void EnableBroadcastClosed ()
- {
- Socket sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Dgram,
- ProtocolType.Udp);
-
- sock.Close ();
-
- bool val = sock.EnableBroadcast;
- }
- /* Can't test the default for ExclusiveAddressUse as
- * it's different on different versions and service
- * packs of windows
- */
- [Test]
- [Category ("NotWorking")] // Not supported on Linux
- public void ExclusiveAddressUseUnbound ()
- {
- Socket sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
-
- sock.ExclusiveAddressUse = true;
-
- Assert.AreEqual (true, sock.ExclusiveAddressUse, "ExclusiveAddressUseUnbound");
-
- sock.Close ();
- }
- [Test]
- [ExpectedException (typeof(InvalidOperationException))]
- [Category ("NotWorking")] // Not supported on Linux
- public void ExclusiveAddressUseBound ()
- {
- Socket sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
-
- sock.Bind (new IPEndPoint (IPAddress.Any, NetworkHelpers.FindFreePort ()));
- sock.ExclusiveAddressUse = true;
- sock.Close ();
- }
- [Test]
- [ExpectedException (typeof(ObjectDisposedException))]
- public void ExclusiveAddressUseClosed ()
- {
- Socket sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
-
- sock.Close ();
-
- bool val = sock.ExclusiveAddressUse;
- }
-
- [Test]
- public void IsBoundTcp ()
- {
- Socket sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
- IPEndPoint ep = new IPEndPoint (IPAddress.Loopback,
- BogusPort);
-
- Assert.AreEqual (false, sock.IsBound, "IsBoundTcp #1");
-
- sock.Bind (ep);
- Assert.AreEqual (true, sock.IsBound, "IsBoundTcp #2");
- sock.Listen (1);
-
- Socket sock2 = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
-
- Assert.AreEqual (false, sock2.IsBound, "IsBoundTcp #3");
-
- sock2.Connect (ep);
- Assert.AreEqual (true, sock2.IsBound, "IsBoundTcp #4");
-
- sock2.Close ();
- Assert.AreEqual (true, sock2.IsBound, "IsBoundTcp #5");
- sock.Close ();
- Assert.AreEqual (true, sock.IsBound, "IsBoundTcp #6");
- }
- [Test]
- public void IsBoundUdp ()
- {
- Socket sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Dgram,
- ProtocolType.Udp);
- IPEndPoint ep = new IPEndPoint (IPAddress.Loopback,
- BogusPort);
-
- Assert.AreEqual (false, sock.IsBound, "IsBoundUdp #1");
-
- sock.Bind (ep);
- Assert.AreEqual (true, sock.IsBound, "IsBoundUdp #2");
-
- sock.Close ();
- Assert.AreEqual (true, sock.IsBound, "IsBoundUdp #3");
-
- sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Dgram,
- ProtocolType.Udp);
-
- Assert.AreEqual (false, sock.IsBound, "IsBoundUdp #4");
-
- sock.Connect (ep);
- Assert.AreEqual (true, sock.IsBound, "IsBoundUdp #5");
-
- sock.Close ();
- Assert.AreEqual (true, sock.IsBound, "IsBoundUdp #6");
- }
- [Test]
- /* Should not throw an exception */
- public void IsBoundClosed ()
- {
- Socket sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
-
- sock.Close ();
-
- bool val = sock.IsBound;
- }
-
- /* Nothing much to test for LingerState */
-
- [Test]
- public void MulticastLoopbackDefaultTcp ()
- {
- Socket sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
-
- try {
- bool value = sock.MulticastLoopback;
- Assert.Fail ("MulticastLoopbackDefaultTcp #1");
- } catch (SocketException ex) {
- Assert.AreEqual (10042, ex.ErrorCode, "MulticastLoopbackDefaultTcp #2");
- } finally {
- sock.Close ();
- }
- }
- [Test]
- public void MulticastLoopbackChangeTcp ()
- {
- Socket sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
-
- try {
- sock.MulticastLoopback = false;
- Assert.Fail ("MulticastLoopbackChangeTcp #1");
- } catch (SocketException ex) {
- Assert.AreEqual (10042, ex.ErrorCode, "MulticastLoopbackChangeTcp #2");
- } finally {
- sock.Close ();
- }
- }
-
- [Test]
- public void MulticastLoopbackDefaultUdp ()
- {
- Socket sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Dgram,
- ProtocolType.Udp);
-
- Assert.AreEqual (true, sock.MulticastLoopback, "MulticastLoopbackDefaultUdp");
-
- sock.Close ();
- }
-
- [Test]
- public void MulticastLoopbackChangeUdp ()
- {
- Socket sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Dgram,
- ProtocolType.Udp);
-
- sock.MulticastLoopback = false;
-
- Assert.AreEqual (false, sock.MulticastLoopback, "MulticastLoopbackChangeUdp");
-
- sock.Close ();
- }
- [Test]
- [ExpectedException (typeof(ObjectDisposedException))]
- public void MulticastLoopbackClosed ()
- {
- Socket sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
-
- sock.Close ();
-
- bool val = sock.MulticastLoopback;
- }
-
- /* OSSupportsIPv6 depends on the environment */
-
- [Test]
- [Category("NotWorking")] // We have different defaults for perf reasons
- public void ReceiveBufferSizeDefault ()
- {
- Socket sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
-
- Assert.AreEqual (8192, sock.ReceiveBufferSize, "ReceiveBufferSizeDefault");
-
- sock.Close ();
- }
-
- [Test]
- [Category("NotWorking")] // We have different defaults for perf reasons
- public void ReceiveBufferSizeDefaultUdp ()
- {
- Socket sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Dgram,
- ProtocolType.Udp);
-
- Assert.AreEqual (8192, sock.ReceiveBufferSize, "ReceiveBufferSizeDefaultUdp");
-
- sock.Close ();
- }
- [Test]
- public void ReceiveBufferSizeChange ()
- {
- Socket sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
-
- sock.ReceiveBufferSize = 16384;
-
- Assert.AreEqual (16384, sock.ReceiveBufferSize, "ReceiveBufferSizeChange");
-
- sock.Close ();
- }
- [Test]
- [Category("NotWorking")] // We cannot totally remove buffers (minimum is set to 256
- public void BuffersCheck_None ()
- {
- using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) {
- int original = s.ReceiveBufferSize;
- s.ReceiveBufferSize = 0;
- Assert.AreEqual (0, s.ReceiveBufferSize, "ReceiveBufferSize " + original.ToString ());
- original = s.SendBufferSize;
- s.SendBufferSize = 0;
- Assert.AreEqual (0, s.SendBufferSize, "SendBufferSize " + original.ToString ());
- }
- }
- [Test]
- [ExpectedException (typeof(ObjectDisposedException))]
- public void ReceiveBufferSizeClosed ()
- {
- Socket sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
-
- sock.Close ();
-
- int val = sock.ReceiveBufferSize;
- }
-
- [Test]
- [Category("NotWorking")] // We have different defaults for perf reasons
- public void SendBufferSizeDefault ()
- {
- Socket sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
-
- Assert.AreEqual (8192, sock.SendBufferSize, "SendBufferSizeDefault");
-
- sock.Close ();
- }
-
- [Test]
- [Category("NotWorking")] // We have different defaults for perf reasons
- public void SendBufferSizeDefaultUdp ()
- {
- Socket sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Dgram,
- ProtocolType.Udp);
-
- Assert.AreEqual (8192, sock.SendBufferSize, "SendBufferSizeDefaultUdp");
-
- sock.Close ();
- }
- [Test]
- public void SendBufferSizeChange ()
- {
- Socket sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
-
- sock.SendBufferSize = 16384;
-
- Assert.AreEqual (16384, sock.SendBufferSize, "SendBufferSizeChange");
-
- sock.Close ();
- }
- [Test]
- [ExpectedException (typeof(ObjectDisposedException))]
- public void SendBufferSizeClosed ()
- {
- Socket sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
-
- sock.Close ();
-
- int val = sock.SendBufferSize;
- }
-
- /* No test for TTL default as it's platform dependent */
- [Test]
- public void TtlChange ()
- {
- Socket sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
-
- sock.Ttl = 255;
-
- Assert.AreEqual (255, sock.Ttl, "TtlChange");
-
- sock.Close ();
- }
- [Test]
- public void TtlChangeOverflow ()
- {
- Socket sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
-
- try {
- sock.Ttl = 256;
- Assert.Fail ("TtlChangeOverflow #1");
- } catch (ArgumentOutOfRangeException ex) {
- Assert.AreEqual ("value", ex.ParamName,
- "TtlChangeOverflow #2");
- } finally {
- sock.Close ();
- }
- }
-
- /* Apparently you can set TTL=0 on the ms runtime!!
- try {
- sock.Ttl = 0;
- Assert.Fail ("TtlChangeOverflow #4");
- } catch (SocketException ex) {
- Assert.AreEqual (10022, ex.ErrorCode,
- "TtlChangeOverflow #5");
- } finally {
- sock.Close ();
- }
- */
- [Test]
- [ExpectedException (typeof(ObjectDisposedException))]
- public void TtlClosed ()
- {
- Socket sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
-
- sock.Close ();
-
- int val = sock.Ttl;
- }
-
- [Test]
- public void UseOnlyOverlappedIODefault ()
- {
- Socket sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
-
- Assert.AreEqual (false, sock.UseOnlyOverlappedIO, "UseOnlyOverlappedIODefault");
-
- sock.Close ();
- }
- //
- // We need this because the Linux kernel in certain configurations
- // will end up rounding up the values passed on to the kernel
- // for socket send/receive timeouts.
- //
- int Approximate (int target, int value)
- {
- int epsilon = 10;
-
- if (value > target-10 && value < target+10)
- return target;
- return value;
- }
-
- [Test]
- public void UseOnlyOverlappedIOChange ()
- {
- Socket sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
-
- sock.UseOnlyOverlappedIO = true;
-
- Assert.AreEqual (true, sock.UseOnlyOverlappedIO, "UseOnlyOverlappedIOChange");
-
- sock.Close ();
- }
- [Test]
- /* Should not throw an exception */
- public void UseOnlyOverlappedIOClosed ()
- {
- Socket sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
-
- sock.Close ();
-
- bool val = sock.UseOnlyOverlappedIO;
- }
-
- [Test]
- public void SendTimeoutDefault ()
- {
- Socket sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
-
- Assert.AreEqual (0, sock.SendTimeout, "SendTimeoutDefault");
-
- sock.Close ();
- }
- [Test]
- public void SendTimeoutChange ()
- {
- Socket sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
-
- /* Should be rounded up to 500, according to
- * the MSDN docs, but the MS runtime doesn't
- */
- sock.SendTimeout = 50;
- Assert.AreEqual (50, Approximate (50, sock.SendTimeout), "SendTimeoutChange #1");
-
- sock.SendTimeout = 2000;
- Assert.AreEqual (2000, Approximate (2000, sock.SendTimeout), "SendTimeoutChange #2");
-
- sock.SendTimeout = 0;
- Assert.AreEqual (0, Approximate (0, sock.SendTimeout), "SendTimeoutChange #3");
-
- /* Should be the same as setting 0 */
- sock.SendTimeout = -1;
- Assert.AreEqual (0, sock.SendTimeout, "SendTimeoutChange #4");
- sock.SendTimeout = 65536;
- Assert.AreEqual (65536, Approximate (65536, sock.SendTimeout), "SendTimeoutChange #5");
-
- try {
- sock.SendTimeout = -2;
- Assert.Fail ("SendTimeoutChange #8");
- } catch (ArgumentOutOfRangeException) {
- } finally {
- sock.Close ();
- }
- }
- [Test]
- [ExpectedException (typeof(ObjectDisposedException))]
- public void SendTimeoutClosed ()
- {
- Socket sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
-
- sock.Close ();
-
- int val = sock.SendTimeout;
- }
-
- [Test]
- public void ReceiveTimeoutDefault ()
- {
- Socket sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
-
- Assert.AreEqual (0, sock.ReceiveTimeout, "ReceiveTimeoutDefault");
-
- sock.Close ();
- }
- [Test]
- public void ReceiveTimeoutChange ()
- {
- Socket sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
-
- sock.ReceiveTimeout = 50;
- Assert.AreEqual (50, Approximate (50, sock.ReceiveTimeout), "ReceiveTimeoutChange #1");
-
- sock.ReceiveTimeout = 2000;
- Assert.AreEqual (2000, Approximate (2000, sock.ReceiveTimeout), "ReceiveTimeoutChange #2");
-
- sock.ReceiveTimeout = 0;
- Assert.AreEqual (0, sock.ReceiveTimeout, "ReceiveTimeoutChange #3");
-
- /* Should be the same as setting 0 */
- sock.ReceiveTimeout = -1;
- Assert.AreEqual (0, sock.ReceiveTimeout, "ReceiveTimeoutChange #4");
- sock.ReceiveTimeout = 65536;
- Assert.AreEqual (65536, Approximate (65536, sock.ReceiveTimeout), "ReceiveTimeoutChange #5");
-
- try {
- sock.ReceiveTimeout = -2;
- Assert.Fail ("ReceiveTimeoutChange #8");
- } catch (ArgumentOutOfRangeException) {
- } finally {
- sock.Close ();
- }
- }
- [Test]
- [ExpectedException (typeof(ObjectDisposedException))]
- public void ReceiveTimeoutClosed ()
- {
- Socket sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
-
- sock.Close ();
-
- int val = sock.ReceiveTimeout;
- }
-
- [Test]
- public void NoDelayDefaultTcp ()
- {
- Socket sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
- Assert.AreEqual (false, sock.NoDelay, "NoDelayDefaultTcp");
- sock.Close ();
- }
- [Test]
- public void NoDelayChangeTcp ()
- {
- Socket sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
-
- sock.NoDelay = true;
-
- Assert.AreEqual (true, sock.NoDelay, "NoDelayChangeTcp");
-
- sock.Close ();
- }
-
- [Test]
- public void NoDelayDefaultUdp ()
- {
- Socket sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Dgram,
- ProtocolType.Udp);
-
- try {
- bool val = sock.NoDelay;
- Assert.Fail ("NoDelayDefaultUdp #1");
- } catch (SocketException ex) {
- Assert.AreEqual (10042, ex.ErrorCode,
- "NoDelayDefaultUdp #2");
- } finally {
- sock.Close ();
- }
- }
- [Test]
- public void NoDelayChangeUdp ()
- {
- Socket sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Dgram,
- ProtocolType.Udp);
-
- try {
- sock.NoDelay = true;
- Assert.Fail ("NoDelayChangeUdp #1");
- } catch (SocketException ex) {
- Assert.AreEqual (10042, ex.ErrorCode,
- "NoDelayChangeUdp #2");
- } finally {
- sock.Close ();
- }
- }
-
- [Test]
- [ExpectedException (typeof(ObjectDisposedException))]
- public void NoDelayClosed ()
- {
- Socket sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
-
- sock.Close ();
-
- bool val = sock.NoDelay;
- }
- static bool BAAccepted = false;
- static Socket BASocket = null;
- static ManualResetEvent BACalledBack = new ManualResetEvent (false);
-
- private static void BACallback (IAsyncResult asyncResult)
- {
- Socket sock = (Socket)asyncResult.AsyncState;
-
- BASocket = sock.EndAccept (asyncResult);
-
- BAAccepted = true;
- BACalledBack.Set ();
- }
-
- [Test]
- [ExpectedException (typeof(InvalidOperationException))]
- public void BeginAcceptNotBound ()
- {
- Socket sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
- sock.BeginAccept (BACallback, sock);
-
- sock.Close ();
- }
-
- [Test]
- [ExpectedException (typeof(InvalidOperationException))]
- public void BeginAcceptNotListening ()
- {
- Socket sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
- sock.Bind (new IPEndPoint (IPAddress.Any, NetworkHelpers.FindFreePort ()));
-
- sock.BeginAccept (BACallback, sock);
-
- sock.Close ();
- }
- [Test]
- public void BeginAccept ()
- {
- Socket sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
- IPEndPoint ep = new IPEndPoint (IPAddress.Loopback,
- NetworkHelpers.FindFreePort ());
-
- sock.Bind (ep);
- sock.Listen (1);
-
- BACalledBack.Reset ();
-
- sock.BeginAccept (BACallback, sock);
- Socket conn = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
-
- conn.Connect (ep);
- if (BACalledBack.WaitOne (2000, false) == false) {
- Assert.Fail ("BeginAccept wait timed out");
- }
-
- Assert.AreEqual (true, BAAccepted, "BeginAccept #1");
- Assert.AreEqual (true, BASocket.Connected, "BeginAccept #2");
- Assert.AreEqual (false, sock.Connected, "BeginAccept #3");
- Assert.AreEqual (true, conn.Connected, "BeginAccept #4");
- BASocket.Close ();
- conn.Close ();
- sock.Close ();
- }
- [Test]
- [ExpectedException (typeof(ObjectDisposedException))]
- public void BeginAcceptClosed ()
- {
- Socket sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
-
- sock.Close ();
-
- sock.BeginAccept (BACallback, sock);
- }
- static bool BADAccepted = false;
- static Socket BADSocket = null;
- static byte[] BADBytes;
- static int BADByteCount;
- static ManualResetEvent BADCalledBack = new ManualResetEvent (false);
-
- private static void BADCallback (IAsyncResult asyncResult)
- {
- Socket sock = (Socket)asyncResult.AsyncState;
-
- BADSocket = sock.EndAccept (out BADBytes,
- out BADByteCount,
- asyncResult);
-
- BADAccepted = true;
- BADCalledBack.Set ();
- }
- [Test]
- public void BeginAcceptData ()
- {
- Socket sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
- IPEndPoint ep = new IPEndPoint (IPAddress.Loopback,
- NetworkHelpers.FindFreePort ());
-
- sock.Bind (ep);
- sock.Listen (1);
-
- BADCalledBack.Reset ();
-
- sock.BeginAccept (256, BADCallback, sock);
- Socket conn = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
- byte[] send_bytes = new byte[] {10, 11, 12, 13};
-
- conn.Connect (ep);
- conn.Send (send_bytes);
- if (BADCalledBack.WaitOne (2000, false) == false) {
- Assert.Fail ("BeginAcceptData wait timed out");
- }
-
- Assert.AreEqual (true, BADAccepted, "BeginAcceptData #1");
- Assert.AreEqual (true, BADSocket.Connected, "BeginAcceptData #2");
- Assert.AreEqual (false, sock.Connected, "BeginAcceptData #3");
- Assert.AreEqual (true, conn.Connected, "BeginAcceptData #4");
- Assert.AreEqual (send_bytes.Length, BADByteCount, "BeginAcceptData #5");
-
- /* The MS runtime gives the returned data in a
- * much bigger array. TODO: investigate
- * whether it the size correlates to the first
- * parameter in BeginAccept()
- */
- Assert.IsFalse (BADBytes.Length == send_bytes.Length,
- "BeginAcceptData #6");
- for(int i = 0; i < send_bytes.Length; i++) {
- Assert.AreEqual (send_bytes[i], BADBytes[i], "BeginAcceptData #" + (i+7).ToString ());
- }
- BADSocket.Close ();
- conn.Close ();
- sock.Close ();
- }
- [Test]
- [ExpectedException (typeof(ObjectDisposedException))]
- public void BeginAcceptDataClosed ()
- {
- Socket sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
-
- sock.Close ();
-
- sock.BeginAccept (256, BADCallback, sock);
- }
- [Test]
- public void BeginAcceptSocketUdp ()
- {
- Socket sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
- Socket acc = new Socket (AddressFamily.InterNetwork,
- SocketType.Dgram,
- ProtocolType.Udp);
-
- IPEndPoint ep = new IPEndPoint (IPAddress.Loopback,
- NetworkHelpers.FindFreePort ());
-
- sock.Bind (ep);
- sock.Listen (1);
-
- try {
- sock.BeginAccept (acc, 256, BADCallback, sock);
- Assert.Fail ("BeginAcceptSocketUdp #1");
- } catch (SocketException ex) {
- Assert.AreEqual (10022, ex.ErrorCode, "BeginAcceptSocketUdp #2");
- } finally {
- acc.Close ();
- sock.Close ();
- }
- }
-
- [Test]
- public void BeginAcceptSocketBound ()
- {
- Socket sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
- Socket acc = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
-
- IPEndPoint ep1 = new IPEndPoint (IPAddress.Loopback,
- NetworkHelpers.FindFreePort ());
-
- IPEndPoint ep2 = new IPEndPoint (IPAddress.Loopback,
- NetworkHelpers.FindFreePort ());
-
- sock.Bind (ep1);
- sock.Listen (1);
- acc.Bind (ep2);
-
- try {
- sock.BeginAccept (acc, 256, BADCallback, sock);
- Assert.Fail ("BeginAcceptSocketBound #1");
- } catch (InvalidOperationException) {
- } finally {
- acc.Close ();
- sock.Close ();
- }
- }
-
- [Test]
- public void BeginAcceptSocket ()
- {
- Socket sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
- Socket acc = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
-
- IPEndPoint ep = new IPEndPoint (IPAddress.Loopback,
- NetworkHelpers.FindFreePort ());
-
- sock.Bind (ep);
- sock.Listen (1);
-
- BADCalledBack.Reset ();
-
- sock.BeginAccept (acc, 256, BADCallback, sock);
- Socket conn = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
- byte[] send_bytes = new byte[] {10, 11, 12, 13};
-
- conn.Connect (ep);
- conn.Send (send_bytes);
- if (BADCalledBack.WaitOne (2000, false) == false) {
- Assert.Fail ("BeginAcceptSocket wait timed out");
- }
-
- Assert.AreEqual (true, BADAccepted, "BeginAcceptSocket #1");
- Assert.AreEqual (true, BADSocket.Connected, "BeginAcceptSocket #2");
- Assert.AreEqual (false, sock.Connected, "BeginAcceptSocket #3");
- Assert.AreEqual (true, conn.Connected, "BeginAcceptSocket #4");
- Assert.AreEqual (send_bytes.Length, BADByteCount, "BeginAcceptSocket #5");
- Assert.AreEqual (AddressFamily.InterNetwork, acc.AddressFamily, "BeginAcceptSocket #6");
- Assert.AreEqual (SocketType.Stream, acc.SocketType, "BeginAcceptSocket #7");
- Assert.AreEqual (ProtocolType.Tcp, acc.ProtocolType, "BeginAcceptSocket #8");
- Assert.AreEqual (conn.LocalEndPoint, acc.RemoteEndPoint, "BeginAcceptSocket #9");
-
- /* The MS runtime gives the returned data in a
- * much bigger array. TODO: investigate
- * whether it the size correlates to the first
- * parameter in BeginAccept()
- */
- Assert.IsFalse (BADBytes.Length == send_bytes.Length,
- "BeginAcceptSocket #10");
- for(int i = 0; i < send_bytes.Length; i++) {
- Assert.AreEqual (send_bytes[i], BADBytes[i], "BeginAcceptSocket #" + (i+11).ToString ());
- }
- BADSocket.Close ();
- conn.Close ();
- acc.Close ();
- sock.Close ();
- }
- [Test]
- public void BeginAcceptSocketClosed ()
- {
- Socket sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
- Socket acc = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
-
- sock.Close ();
-
- try {
- sock.BeginAccept (acc, 256, BADCallback, null);
- Assert.Fail ("BeginAcceptSocketClosed #1");
- } catch (ObjectDisposedException) {
- } finally {
- acc.Close ();
- }
- }
- [Test]
- public void BeginAcceptSocketAccClosed ()
- {
- Socket sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
- Socket acc = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
- IPEndPoint ep = new IPEndPoint (IPAddress.Loopback,
- NetworkHelpers.FindFreePort ());
- sock.Bind (ep);
- sock.Listen (1);
-
- acc.Close ();
-
- BADCalledBack.Reset ();
-
- try {
- sock.BeginAccept (acc, 256, BADCallback, null);
- Assert.Fail ("BeginAcceptSocketAccClosed #1");
- } catch (ObjectDisposedException) {
- } finally {
- sock.Close ();
- }
- }
-
- static bool BCConnected = false;
- static ManualResetEvent BCCalledBack = new ManualResetEvent (false);
-
- private static void BCCallback (IAsyncResult asyncResult)
- {
- Socket sock = (Socket)asyncResult.AsyncState;
-
- try {
- sock.EndConnect (asyncResult);
- } catch (Exception e) {
- Console.WriteLine ("BCCallback exception:");
- Console.WriteLine (e);
- throw;
- }
- BCConnected = true;
-
- BCCalledBack.Set ();
- }
-
- [Test]
- public void BeginConnectAddressPort ()
- {
- Socket sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
- Socket listen = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
- IPAddress ip = IPAddress.Loopback;
- IPEndPoint ep = new IPEndPoint (ip, NetworkHelpers.FindFreePort ());
- listen.Bind (ep);
- listen.Listen (1);
-
- BCCalledBack.Reset ();
-
- BCConnected = false;
-
- sock.BeginConnect (ip, ep.Port, BCCallback, sock);
- if (BCCalledBack.WaitOne (2000, false) == false) {
- Assert.Fail ("BeginConnectAddressPort wait timed out");
- }
-
- Assert.AreEqual (true, BCConnected, "BeginConnectAddressPort #1");
-
- sock.Close ();
- listen.Close ();
- }
- [Test]
- public void BeginConnectAddressPortNull ()
- {
- Socket sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
- IPAddress ip = null;
- try {
- sock.BeginConnect (ip, 1244, BCCallback,
- sock);
- Assert.Fail ("BeginConnectAddressPortNull #1");
- } catch (ArgumentNullException) {
- } finally {
- sock.Close ();
- }
- }
- [Test]
- public void BeginConnectAddressPortListen ()
- {
- Socket sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
- IPAddress ip = IPAddress.Loopback;
- IPEndPoint ep = new IPEndPoint (ip, NetworkHelpers.FindFreePort ());
- sock.Bind (ep);
- sock.Listen (1);
-
- try {
- sock.BeginConnect (ip, ep.Port, BCCallback, sock);
- Assert.Fail ("BeginConnectAddressPortListen #1");
- } catch (InvalidOperationException) {
- } finally {
- sock.Close ();
- }
- }
-
- [Test]
- [ExpectedException (typeof(ObjectDisposedException))]
- public void BeginConnectAddressPortClosed ()
- {
- Socket sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
- IPAddress ip = IPAddress.Loopback;
-
- sock.Close ();
-
- sock.BeginConnect (ip, 1244, BCCallback, sock);
- }
-
- [Test]
- [Category ("NotOnMac")]
- /*
- * This is not a Mono bug.
- *
- * By default, only 127.0.0.1 is enabled and you must explicitly
- * enable additional addresses using 'sudo ifconfig lo0 alias 127.0.0.1'.
- *
- * I tested this on Mac OS 10.7.4; a 'ping 127.0.0.2' does not work
- * until I add that alias.
- *
- */
- public void BeginConnectMultiple ()
- {
- Socket sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
- Socket listen = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
- IPEndPoint ep = new IPEndPoint (IPAddress.Loopback,
- NetworkHelpers.FindFreePort ());
- IPAddress[] ips = new IPAddress[4];
-
- ips[0] = IPAddress.Parse ("127.0.0.4");
- ips[1] = IPAddress.Parse ("127.0.0.3");
- ips[2] = IPAddress.Parse ("127.0.0.2");
- ips[3] = IPAddress.Parse ("127.0.0.1");
- listen.Bind (ep);
- listen.Listen (1);
-
- BCCalledBack.Reset ();
-
- BCConnected = false;
-
- sock.BeginConnect (ips, ep.Port, BCCallback, sock);
-
- /* Longer wait here, because the ms runtime
- * takes a lot longer to not connect
- */
- /*
- if (BCCalledBack.WaitOne (30000, false) == false) {
- Assert.Fail ("BeginConnectMultiple wait failed");
- }
- */
- Assert.IsTrue (BCCalledBack.WaitOne (30000), "#0");
-
- Assert.AreEqual (true, BCConnected, "BeginConnectMultiple #1");
- Assert.AreEqual (AddressFamily.InterNetwork, sock.RemoteEndPoint.AddressFamily, "BeginConnectMultiple #2");
- IPEndPoint remep = (IPEndPoint)sock.RemoteEndPoint;
-
- Assert.AreEqual (IPAddress.Loopback, remep.Address, "BeginConnectMultiple #2");
-
- sock.Close ();
- listen.Close ();
- }
- [Test]
- public void BeginConnectMultiple2 ()
- {
- Socket sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
- Socket listen = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
- // Need at least two addresses.
- var ips = Dns.GetHostAddresses (string.Empty);
- if (ips.Length < 1)
- Assert.Ignore ("This test needs at least two IP addresses.");
- var allIps = new IPAddress [ips.Length + 1];
- allIps [0] = IPAddress.Loopback;
- ips.CopyTo (allIps, 1);
- /*
- * Only bind to the loopback interface, so all the non-loopback
- * IP addresses will fail. BeginConnect()/EndConnect() should
- * succeed it it can connect to at least one of the requested
- * addresses.
- */
- IPEndPoint ep = new IPEndPoint (IPAddress.Loopback, NetworkHelpers.FindFreePort ());
- listen.Bind (ep);
- listen.Listen (1);
-
- BCCalledBack.Reset ();
-
- BCConnected = false;
-
- sock.BeginConnect (allIps, ep.Port, BCCallback, sock);
-
- /* Longer wait here, because the ms runtime
- * takes a lot longer to not connect
- */
- if (BCCalledBack.WaitOne (10000, false) == false) {
- Assert.Fail ("BeginConnectMultiple2 wait failed");
- }
-
- Assert.AreEqual (true, BCConnected, "BeginConnectMultiple2 #1");
- Assert.AreEqual (AddressFamily.InterNetwork, sock.RemoteEndPoint.AddressFamily, "BeginConnectMultiple2 #2");
- IPEndPoint remep = (IPEndPoint)sock.RemoteEndPoint;
- Assert.AreEqual (IPAddress.Loopback, remep.Address, "BeginConnectMultiple2 #2");
- sock.Close ();
- listen.Close ();
- }
- [Test]
- public void BeginConnectMultipleNull ()
- {
- Socket sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
- IPAddress[] ips = null;
-
- try {
- sock.BeginConnect (ips, 1246, BCCallback,
- sock);
- Assert.Fail ("BeginConnectMultipleNull #1");
- } catch (ArgumentNullException) {
- } finally {
- sock.Close ();
- }
- }
- [Test]
- public void BeginConnectMultipleListen ()
- {
- Socket sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
- IPAddress[] ips = new IPAddress[4];
- IPEndPoint ep = new IPEndPoint (IPAddress.Loopback,
- NetworkHelpers.FindFreePort ());
-
- ips[0] = IPAddress.Parse ("127.0.0.4");
- ips[1] = IPAddress.Parse ("127.0.0.3");
- ips[2] = IPAddress.Parse ("127.0.0.2");
- ips[3] = IPAddress.Parse ("127.0.0.1");
-
- sock.Bind (ep);
- sock.Listen (1);
-
- try {
- sock.BeginConnect (ips, ep.Port, BCCallback,
- sock);
- Assert.Fail ("BeginConnectMultipleListen #1");
- } catch (InvalidOperationException) {
- } finally {
- sock.Close ();
- }
- }
-
- [Test]
- [ExpectedException (typeof(ObjectDisposedException))]
- public void BeginConnectMultipleClosed ()
- {
- Socket sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
- IPAddress[] ips = new IPAddress[4];
-
- ips[0] = IPAddress.Parse ("127.0.0.4");
- ips[1] = IPAddress.Parse ("127.0.0.3");
- ips[2] = IPAddress.Parse ("127.0.0.2");
- ips[3] = IPAddress.Parse ("127.0.0.1");
-
- sock.Close ();
-
- sock.BeginConnect (ips, 1247, BCCallback, sock);
- }
-
- [Test]
- public void BeginConnectHostPortNull ()
- {
- Socket sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
-
- try {
- sock.BeginConnect ((string)null, 0,
- BCCallback, sock);
- Assert.Fail ("BeginConnectHostPort #1");
- } catch (ArgumentNullException) {
- } finally {
- sock.Close ();
- }
- }
- [Test]
- public void BeginConnectHostPortListen ()
- {
- Socket sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
- IPAddress ip = IPAddress.Loopback;
- IPEndPoint ep = new IPEndPoint (ip, NetworkHelpers.FindFreePort ());
-
- sock.Bind (ep);
- sock.Listen (1);
-
- try {
- sock.BeginConnect ("localhost", ep.Port,
- BCCallback, sock);
- Assert.Fail ("BeginConnectHostPortListen #1");
- } catch (InvalidOperationException) {
- } finally {
- sock.Close ();
- }
- }
- [Test]
- [Category ("NotWorking")] // Need to pick a non-IP AddressFamily that "works" on both mono and ms, this one only works on ms
- public void BeginConnectHostPortNotIP ()
- {
- Socket sock = new Socket (AddressFamily.NetBios,
- SocketType.Seqpacket,
- ProtocolType.Unspecified);
-
- try {
- sock.BeginConnect ("localhost", 0, BCCallback,
- sock);
- Assert.Fail ("BeginConnectHostPortNotIP #1");
- } catch (NotSupportedException) {
- } finally {
- sock.Close ();
- }
- }
- [Test]
- [ExpectedException (typeof(ObjectDisposedException))]
- public void BeginConnectHostPortClosed ()
- {
- Socket sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
-
- sock.Close ();
-
- sock.BeginConnect ("localhost", 0, BCCallback, sock);
- }
-
- static bool BDDisconnected = false;
- static ManualResetEvent BDCalledBack = new ManualResetEvent (false);
-
- private static void BDCallback (IAsyncResult asyncResult)
- {
- Socket sock = (Socket)asyncResult.AsyncState;
-
- sock.EndDisconnect (asyncResult);
- BDDisconnected = true;
-
- BDCalledBack.Set ();
- }
-
- [Test]
- [Category ("NotDotNet")] // "Needs XP or later"
- public void BeginDisconnect ()
- {
- Socket sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
- Socket listen = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
- IPAddress ip = IPAddress.Loopback;
- IPEndPoint ep = new IPEndPoint (ip, NetworkHelpers.FindFreePort ());
-
- listen.Bind (ep);
- listen.Listen (1);
-
- sock.Connect (ip, ep.Port);
-
- Assert.AreEqual (true, sock.Connected, "BeginDisconnect #1");
-
- sock.Shutdown (SocketShutdown.Both);
- BDCalledBack.Reset ();
- BDDisconnected = false;
-
- sock.BeginDisconnect (false, BDCallback, sock);
-
- if (BDCalledBack.WaitOne (2000, false) == false) {
- Assert.Fail ("BeginDisconnect wait timed out");
- }
-
- Assert.AreEqual (true, BDDisconnected, "BeginDisconnect #2");
- Assert.AreEqual (false, sock.Connected, "BeginDisconnect #3");
-
- sock.Close ();
- listen.Close ();
- }
-
- [Test]
- public void BeginReceiveSocketError ()
- {
- }
-
- [Test]
- public void BeginReceiveGeneric ()
- {
- }
-
- [Test]
- public void BeginReceiveGenericSocketError ()
- {
- }
-
- private static void BSCallback (IAsyncResult asyncResult)
- {
- Socket sock = (Socket)asyncResult.AsyncState;
-
- sock.EndSend (asyncResult);
- }
-
- [Test]
- public void BeginSendNotConnected ()
- {
- Socket sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
-
- byte[] send_bytes = new byte[] {10, 11, 12, 13};
-
- try {
- sock.BeginSend (send_bytes, 0,
- send_bytes.Length,
- SocketFlags.None, BSCallback,
- sock);
- Assert.Fail ("BeginSendNotConnected #1");
- } catch (SocketException ex) {
- Assert.AreEqual (10057, ex.ErrorCode, "BeginSendNotConnected #2");
- } finally {
- sock.Close ();
- }
- }
-
- [Test]
- public void BeginSendSocketError ()
- {
- }
-
- [Test]
- public void BeginSendGeneric ()
- {
- }
-
- [Test]
- public void BeginSendGenericSocketError ()
- {
- }
-
- [Test]
- public void BindTwice ()
- {
- Socket sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
- IPEndPoint ep1 = new IPEndPoint (IPAddress.Loopback,
- NetworkHelpers.FindFreePort ());
- IPEndPoint ep2 = new IPEndPoint (IPAddress.Loopback,
- NetworkHelpers.FindFreePort ());
-
- sock.Bind (ep1);
-
- try {
- sock.Bind (ep2);
- Assert.Fail ("BindTwice #1");
- } catch (SocketException ex) {
- Assert.AreEqual (10022, ex.ErrorCode, "BindTwice #2");
- } finally {
- sock.Close ();
- }
- }
-
- [Test]
- public void Close ()
- {
- Socket sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
- Socket listen = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
- IPEndPoint ep = new IPEndPoint (IPAddress.Loopback,
- NetworkHelpers.FindFreePort ());
-
- listen.Bind (ep);
- listen.Listen (1);
-
- sock.Connect (ep);
- Assert.AreEqual (true, sock.Connected, "Close #1");
-
- sock.Close (2);
-
- Thread.Sleep (3000);
-
- Assert.AreEqual (false, sock.Connected, "Close #2");
-
- listen.Close ();
- }
-
- [Test]
- public void ConnectAddressPort ()
- {
- Socket sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
- Socket listen = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
- IPAddress ip = IPAddress.Loopback;
- IPEndPoint ep = new IPEndPoint (ip, NetworkHelpers.FindFreePort ());
- listen.Bind (ep);
- listen.Listen (1);
-
- sock.Connect (ip, ep.Port);
-
- Assert.AreEqual (true, sock.Connected, "ConnectAddressPort #1");
-
- sock.Close ();
- listen.Close ();
- }
- [Test]
- public void ConnectAddressPortNull ()
- {
- Socket sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
- IPAddress ip = null;
- try {
- sock.Connect (ip, 1249);
- Assert.Fail ("ConnectAddressPortNull #1");
- } catch (ArgumentNullException) {
- } finally {
- sock.Close ();
- }
- }
- [Test]
- public void ConnectAddressPortListen ()
- {
- Socket sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
- IPAddress ip = IPAddress.Loopback;
- IPEndPoint ep = new IPEndPoint (ip, NetworkHelpers.FindFreePort ());
- sock.Bind (ep);
- sock.Listen (1);
-
- try {
- sock.Connect (ip, ep.Port);
- Assert.Fail ("ConnectAddressPortListen #1");
- } catch (InvalidOperationException) {
- } finally {
- sock.Close ();
- }
- }
-
- [Test]
- [ExpectedException (typeof(ObjectDisposedException))]
- public void ConnectAddressPortClosed ()
- {
- Socket sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
- IPAddress ip = IPAddress.Loopback;
-
- sock.Close ();
-
- sock.Connect (ip, 1250);
- }
-
- [Test]
- [Category ("NotOnMac")] // MacOSX trashes the fd after the failed connect attempt to 127.0.0.4
- /*
- * This is not a Mono bug.
- *
- * By default, only 127.0.0.1 is enabled and you must explicitly
- * enable additional addresses using 'sudo ifconfig lo0 alias 127.0.0.1'.
- *
- * I tested this on Mac OS 10.7.4; a 'ping 127.0.0.2' does not work
- * until I add that alias.
- *
- * However, after doing so, Mac OS treats these as separate addresses, ie. attempting
- * to connect to 127.0.0.4 yields a connection refused.
- *
- * When using Connect(), the .NET runtime also throws an exception if connecting to
- * any of the IP addresses fails. This is different with BeginConnect()/EndConnect()
- * which succeeds when it can connect to at least one of the addresses.
- *
- */
- public void ConnectMultiple ()
- {
- Socket sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
- Socket listen = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
- IPEndPoint ep = new IPEndPoint (IPAddress.Loopback,
- NetworkHelpers.FindFreePort ());
- IPAddress[] ips = new IPAddress[4];
-
- ips[0] = IPAddress.Parse ("127.0.0.4");
- ips[1] = IPAddress.Parse ("127.0.0.3");
- ips[2] = IPAddress.Parse ("127.0.0.2");
- ips[3] = IPAddress.Parse ("127.0.0.1");
- listen.Bind (ep);
- listen.Listen (1);
-
- sock.Connect (ips, ep.Port);
-
- Assert.AreEqual (true, sock.Connected, "ConnectMultiple #1");
- Assert.AreEqual (AddressFamily.InterNetwork, sock.RemoteEndPoint.AddressFamily, "ConnectMultiple #2");
- IPEndPoint remep = (IPEndPoint)sock.RemoteEndPoint;
-
- Assert.AreEqual (IPAddress.Loopback, remep.Address, "ConnectMultiple #2");
-
- sock.Close ();
- listen.Close ();
- }
- [Test]
- public void ConnectMultiple2 ()
- {
- Socket sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
- Socket listen = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
- // Need at least two addresses.
- var ips = Dns.GetHostAddresses (string.Empty);
- if (ips.Length < 1)
- Assert.Ignore ("This test needs at least two IP addresses.");
- var allIps = new IPAddress [ips.Length + 1];
- allIps [0] = IPAddress.Loopback;
- ips.CopyTo (allIps, 1);
- /*
- * Bind to IPAddress.Any; Connect() will fail unless it can
- * connect to all the addresses in allIps.
- */
- IPEndPoint ep = new IPEndPoint (IPAddress.Any, NetworkHelpers.FindFreePort ());
- listen.Bind (ep);
- listen.Listen (1);
-
- sock.Connect (allIps, ep.Port);
-
- Assert.AreEqual (true, sock.Connected, "ConnectMultiple2 #1");
- Assert.AreEqual (AddressFamily.InterNetwork, sock.RemoteEndPoint.AddressFamily, "ConnectMultiple2 #2");
- IPEndPoint remep = (IPEndPoint)sock.RemoteEndPoint;
- Assert.AreEqual (IPAddress.Loopback, remep.Address, "ConnectMultiple2 #3");
-
- sock.Close ();
- listen.Close ();
- }
- [Test]
- public void ConnectMultipleNull ()
- {
- Socket sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
- IPAddress[] ips = null;
-
- try {
- sock.Connect (ips, 1251);
- Assert.Fail ("ConnectMultipleNull #1");
- } catch (ArgumentNullException) {
- } finally {
- sock.Close ();
- }
- }
- [Test]
- public void ConnectMultipleListen ()
- {
- Socket sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
- IPAddress[] ips = new IPAddress[4];
- IPEndPoint ep = new IPEndPoint (IPAddress.Loopback,
- NetworkHelpers.FindFreePort ());
-
- ips[0] = IPAddress.Parse ("127.0.0.4");
- ips[1] = IPAddress.Parse ("127.0.0.3");
- ips[2] = IPAddress.Parse ("127.0.0.2");
- ips[3] = IPAddress.Parse ("127.0.0.1");
-
- sock.Bind (ep);
- sock.Listen (1);
-
- try {
- sock.Connect (ips, ep.Port);
- Assert.Fail ("ConnectMultipleListen #1");
- } catch (InvalidOperationException) {
- } finally {
- sock.Close ();
- }
- }
-
- [Test]
- [ExpectedException (typeof(ObjectDisposedException))]
- public void ConnectMultipleClosed ()
- {
- Socket sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
- IPAddress[] ips = new IPAddress[4];
-
- ips[0] = IPAddress.Parse ("127.0.0.4");
- ips[1] = IPAddress.Parse ("127.0.0.3");
- ips[2] = IPAddress.Parse ("127.0.0.2");
- ips[3] = IPAddress.Parse ("127.0.0.1");
-
- sock.Close ();
-
- sock.Connect (ips, 1252);
- }
-
- [Test]
- public void ConnectHostPortNull ()
- {
- Socket sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
-
- try {
- sock.Connect ((string)null, 0);
- Assert.Fail ("ConnectHostPort #1");
- } catch (ArgumentNullException) {
- } finally {
- sock.Close ();
- }
- }
- [Test]
- public void ConnectHostPortListen ()
- {
- Socket sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
- IPAddress ip = IPAddress.Loopback;
- IPEndPoint ep = new IPEndPoint (ip, NetworkHelpers.FindFreePort ());
-
- sock.Bind (ep);
- sock.Listen (1);
-
- try {
- sock.Connect ("localhost", ep.Port);
- Assert.Fail ("ConnectHostPortListen #1");
- } catch (InvalidOperationException) {
- } finally {
- sock.Close ();
- }
- }
- [Test]
- [Category ("NotWorking")] // Need to pick a non-IP AddressFamily that "works" on both mono and ms, this one only works on ms
- public void ConnectHostPortNotIP ()
- {
- Socket sock = new Socket (AddressFamily.NetBios,
- SocketType.Seqpacket,
- ProtocolType.Unspecified);
-
- try {
- sock.Connect ("localhost", 0);
- Assert.Fail ("ConnectHostPortNotIP #1");
- } catch (NotSupportedException) {
- } finally {
- sock.Close ();
- }
- }
- [Test]
- [ExpectedException (typeof(ObjectDisposedException))]
- public void ConnectHostPortClosed ()
- {
- Socket sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
-
- sock.Close ();
-
- sock.Connect ("localhost", 0);
- }
-
- [Test]
- [Category ("NotDotNet")] // "Needs XP or later"
- public void Disconnect ()
- {
- Socket sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
- Socket listen = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
- IPAddress ip = IPAddress.Loopback;
- IPEndPoint ep = new IPEndPoint (ip, NetworkHelpers.FindFreePort ());
-
- listen.Bind (ep);
- listen.Listen (1);
-
- sock.Connect (ip, ep.Port);
-
- Assert.AreEqual (true, sock.Connected, "Disconnect #1");
-
- sock.Shutdown (SocketShutdown.Both);
- sock.Disconnect (false);
- Assert.AreEqual (false, sock.Connected, "BeginDisconnect #3");
-
- sock.Close ();
- listen.Close ();
- }
-
- [Test]
- public void DuplicateAndClose ()
- {
- }
-
- [Test]
- public void IOControl ()
- {
- }
- [Test]
- public void TestDefaultsDualMode ()
- {
- using (var socket = new Socket (AddressFamily.InterNetworkV6, SocketType.Stream, ProtocolType.Tcp)){
- Assert.IsTrue (socket.DualMode, "In Mono, DualMode must be true when constructing InterNetworkV6 sockets");
- }
- using (var socket = new Socket (SocketType.Stream, ProtocolType.Tcp)){
- Assert.AreEqual (AddressFamily.InterNetworkV6, socket.AddressFamily, "When creating sockets of type stream/tcp, the address family should be InterNetworkV6");
- Assert.IsTrue (socket.DualMode, "In Mono, DualMode must be true when constructing InterNetworkV6 sockets");
- socket.DualMode = false;
- Assert.IsFalse (socket.DualMode, "Setting of DualSocket should turn DualSockets off");
- }
-
- }
-
- [Test]
- public void ReceiveGeneric ()
- {
- int i;
- IPEndPoint endpoint = new IPEndPoint(IPAddress.Loopback, NetworkHelpers.FindFreePort ());
- Socket listensock = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
- listensock.Bind (endpoint);
- listensock.Listen(1);
- Socket sendsock = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
- sendsock.Connect(endpoint);
- Socket clientsock = listensock.Accept();
-
- byte[] sendbuf = new byte[256];
- for(i = 0; i < 256; i++) {
- sendbuf[i] = (byte)i;
- }
- for (i = 4; i < 6; i++) {
- Assert.AreEqual (sendbuf[i], (byte)i,
- "#1/" + i.ToString());
- }
- SocketError err;
- sendsock.Send (sendbuf, 0, 256, SocketFlags.None,
- out err);
- byte[] recvbuf = new byte[256];
- List<ArraySegment<byte>> recvbuflist = new List<ArraySegment<byte>>(2);
- recvbuflist.Add(new ArraySegment<byte>(recvbuf, 4, 2));
- recvbuflist.Add(new ArraySegment<byte>(recvbuf, 20, 230));
-
- clientsock.Receive (recvbuflist);
- /* recvbuf should now hold the first 2 bytes
- * of sendbuf from pos 4, and the next 230
- * bytes of sendbuf from pos 20
- */
- for (i = 0; i < 2; i++) {
- Assert.AreEqual (sendbuf[i], recvbuf[i + 4],
- "#2/" + i.ToString());
- }
- for (i = 2; i < 232; i++) {
- Assert.AreEqual (sendbuf[i], recvbuf[i + 18],
- "#2/" + i.ToString());
- }
- sendsock.Close ();
- clientsock.Close ();
- listensock.Close ();
- }
-
- [Test]
- public void SendGeneric ()
- {
- int i;
- IPEndPoint endpoint = new IPEndPoint(IPAddress.Loopback, NetworkHelpers.FindFreePort ());
- Socket listensock = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
- listensock.Bind (endpoint);
- listensock.Listen(1);
- Socket sendsock = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
- sendsock.Connect(endpoint);
- Socket clientsock = listensock.Accept();
- byte[] sendbuf = new byte[256];
- List<ArraySegment<byte>> sendbuflist = new List<ArraySegment<byte>>(2);
- sendbuflist.Add(new ArraySegment<byte>(sendbuf, 4, 2));
- sendbuflist.Add(new ArraySegment<byte>(sendbuf, 20, 230));
- for(i = 0; i < 256; i++) {
- sendbuf[i] = (byte)i;
- }
- for (i = 4; i < 6; i++) {
- Assert.AreEqual (sendbuf[i], (byte)i,
- "#1/" + i.ToString());
- }
- SocketError err;
- sendsock.Send (sendbuflist, SocketFlags.None, out err);
-
- byte[] recvbuf = new byte[256];
- clientsock.Receive (recvbuf);
- /* The first 2 bytes of recvbuf should now
- * hold 2 bytes of sendbuf from pos 4, and the
- * next 230 bytes of recvbuf should be sendbuf
- * from pos 20
- */
- for (i = 0; i < 2; i++) {
- Assert.AreEqual (recvbuf[i], sendbuf[i + 4],
- "#2/" + i.ToString());
- }
- for (i = 2; i < 232; i++) {
- Assert.AreEqual (recvbuf[i], sendbuf[i + 18],
- "#2/" + i.ToString());
- }
- sendsock.Close ();
- clientsock.Close ();
- listensock.Close ();
- }
- [Test]
- public void ConcurrentExceedSocketLimit ()
- {
- var tasks = new Task[4];
- for (int i = 0; i < 4; i++) {
- tasks[i] = Task.Factory.StartNew (() => SendGenericExceedBuffer ());
- }
- Task.WaitAll (tasks);
- }
- [Test]
- public void SendGenericExceedBuffer ()
- {
- // Create a buffer larger than the default max.
- const int BUFFER_SIZE = 256 * 256 * 65;
- int i;
- IPEndPoint endpoint = new IPEndPoint(IPAddress.Loopback, NetworkHelpers.FindFreePort ());
- Socket listensock = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
- listensock.Bind (endpoint);
- listensock.Listen (1);
- Socket sendsock = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
- sendsock.Connect (endpoint);
- Socket clientsock = listensock.Accept ();
- byte[] sendbuf = new byte[BUFFER_SIZE];
- for (i = 0; i < BUFFER_SIZE; i++) {
- sendbuf[i] = (byte)i;
- }
- Task sendTask = Task.Factory.StartNew(() => {
- int sent = sendsock.Send (sendbuf);
- Assert.AreEqual (BUFFER_SIZE, sent, "#1");
- });
- byte[] recvbuf = new byte[BUFFER_SIZE];
- Task recvTask = Task.Factory.StartNew(() => {
- int totalReceived = 0;
- byte[] buffer = new byte[256];
- while (totalReceived < sendbuf.Length) {
- int recvd = clientsock.Receive (buffer, 0, buffer.Length, SocketFlags.None);
- buffer.CopyTo (recvbuf, totalReceived);
- totalReceived += recvd;
- }
- Assert.AreEqual (BUFFER_SIZE, totalReceived, "#2");
- });
- Task.WaitAll (new []{sendTask, recvTask});
- for (i = 0; i < BUFFER_SIZE; i++) {
- Assert.AreEqual (recvbuf[i], sendbuf[i],
- "#3/" + i.ToString());
- }
- sendsock.Close ();
- clientsock.Close ();
- listensock.Close ();
- }
- [Test]
- public void ListenNotBound ()
- {
- Socket sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
-
- try {
- sock.Listen (1);
- Assert.Fail ("ListenNotBound #1");
- } catch (SocketException ex) {
- Assert.AreEqual (10022, ex.ErrorCode, "ListenNotBound #2");
- } finally {
- sock.Close ();
- }
- }
- static Socket CWRSocket;
- static bool CWRReceiving = true;
- static ManualResetEvent CWRReady = new ManualResetEvent (false);
-
- private static void CWRReceiveThread ()
- {
- byte[] buf = new byte[256];
-
- try {
- CWRSocket.Receive (buf);
- } catch (SocketException) {
- CWRReceiving = false;
- }
- CWRReady.Set ();
- }
-
- [Test]
- public void CloseWhileReceiving ()
- {
- CWRSocket = new Socket (AddressFamily.InterNetwork,
- SocketType.Dgram,
- ProtocolType.Udp);
- CWRSocket.Bind (new IPEndPoint (IPAddress.Loopback,
- NetworkHelpers.FindFreePort ()));
-
- Thread recv_thread = new Thread (new ThreadStart (CWRReceiveThread));
- CWRReady.Reset ();
- recv_thread.Start ();
- Thread.Sleep (250); /* Wait for the thread to be already receiving */
- CWRSocket.Close ();
- if (CWRReady.WaitOne (1000, false) == false) {
- Assert.Fail ("CloseWhileReceiving wait timed out");
- }
-
- Assert.IsFalse (CWRReceiving);
- }
- static bool RRCLastRead = false;
- static ManualResetEvent RRCReady = new ManualResetEvent (false);
-
- private static void RRCClientThread (int port)
- {
- byte[] bytes = new byte[8];
- int readbyte;
-
- Socket sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
- sock.Connect (new IPEndPoint (IPAddress.Loopback,
- port));
-
- NetworkStream stream = new NetworkStream (sock);
- readbyte = stream.ReadByte ();
- Assert.AreEqual (0, readbyte, "ReceiveRemoteClosed #1");
-
- stream.Read (bytes, 0, 0);
- readbyte = stream.ReadByte ();
- Assert.AreEqual (0, readbyte, "ReceiveRemoteClosed #2");
-
- stream.Read (bytes, 0, 0);
- readbyte = stream.ReadByte ();
- Assert.AreEqual (-1, readbyte, "ReceiveRemoteClosed #3");
- sock.Close ();
- RRCLastRead = true;
- RRCReady.Set ();
- }
- [Test] // Receive (Byte [])
- public void Receive1_Buffer_Null ()
- {
- Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
- ProtocolType.Tcp);
- try {
- s.Receive ((byte []) null);
- Assert.Fail ("#1");
- } catch (ArgumentNullException ex) {
- Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
- Assert.IsNull (ex.InnerException, "#3");
- Assert.IsNotNull (ex.Message, "#4");
- Assert.AreEqual ("buffer", ex.ParamName, "#5");
- } finally {
- s.Close ();
- }
- }
- [Test] // Receive (Byte [])
- public void Receive1_Socket_Closed ()
- {
- Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
- ProtocolType.Tcp);
- s.Close ();
- try {
- s.Receive ((byte []) null);
- Assert.Fail ("#1");
- } catch (ObjectDisposedException ex) {
- // Cannot access a disposed object
- Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
- Assert.IsNull (ex.InnerException, "#3");
- Assert.IsNotNull (ex.Message, "#4");
- Assert.AreEqual (typeof (Socket).FullName, ex.ObjectName, "#5");
- }
- }
- [Test] // Receive (Byte [], SocketFlags)
- public void Receive2_Buffer_Null ()
- {
- Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
- ProtocolType.Tcp);
- try {
- s.Receive ((byte []) null, (SocketFlags) 666);
- Assert.Fail ("#1");
- } catch (ArgumentNullException ex) {
- Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
- Assert.IsNull (ex.InnerException, "#3");
- Assert.IsNotNull (ex.Message, "#4");
- Assert.AreEqual ("buffer", ex.ParamName, "#5");
- } finally {
- s.Close ();
- }
- }
- [Test] // Receive (Byte [], SocketFlags)
- public void Receive2_Socket_Closed ()
- {
- Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
- ProtocolType.Tcp);
- s.Close ();
- try {
- s.Receive ((byte []) null, (SocketFlags) 666);
- Assert.Fail ("#1");
- } catch (ObjectDisposedException ex) {
- // Cannot access a disposed object
- Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
- Assert.IsNull (ex.InnerException, "#3");
- Assert.IsNotNull (ex.Message, "#4");
- Assert.AreEqual (typeof (Socket).FullName, ex.ObjectName, "#5");
- }
- }
- [Test] // Receive (Byte [], Int32, SocketFlags)
- public void Receive3_Buffer_Null ()
- {
- Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
- ProtocolType.Tcp);
- try {
- s.Receive ((byte []) null, 0, (SocketFlags) 666);
- Assert.Fail ("#1");
- } catch (ArgumentNullException ex) {
- Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
- Assert.IsNull (ex.InnerException, "#3");
- Assert.IsNotNull (ex.Message, "#4");
- Assert.AreEqual ("buffer", ex.ParamName, "#5");
- } finally {
- s.Close ();
- }
- }
- [Test] // Receive (Byte [], Int32, SocketFlags)
- public void Receive3_Socket_Closed ()
- {
- Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
- ProtocolType.Tcp);
- s.Close ();
- try {
- s.Receive ((byte []) null, 0, (SocketFlags) 666);
- Assert.Fail ("#1");
- } catch (ObjectDisposedException ex) {
- // Cannot access a disposed object
- Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
- Assert.IsNull (ex.InnerException, "#3");
- Assert.IsNotNull (ex.Message, "#4");
- Assert.AreEqual (typeof (Socket).FullName, ex.ObjectName, "#5");
- }
- }
- [Test] // Receive (Byte [], Int32, Int32, SocketFlags)
- public void Receive4_Buffer_Null ()
- {
- Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
- ProtocolType.Tcp);
- try {
- s.Receive ((byte []) null, 0, 0, (SocketFlags) 666);
- Assert.Fail ("#1");
- } catch (ArgumentNullException ex) {
- Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
- Assert.IsNull (ex.InnerException, "#3");
- Assert.IsNotNull (ex.Message, "#4");
- Assert.AreEqual ("buffer", ex.ParamName, "#5");
- } finally {
- s.Close ();
- }
- }
- [Test] // Receive (Byte [], Int32, Int32, SocketFlags)
- public void Receive4_Socket_Closed ()
- {
- Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
- ProtocolType.Tcp);
- s.Close ();
- try {
- s.Receive ((byte []) null, 0, 0, (SocketFlags) 666);
- Assert.Fail ("#1");
- } catch (ObjectDisposedException ex) {
- // Cannot access a disposed object
- Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
- Assert.IsNull (ex.InnerException, "#3");
- Assert.IsNotNull (ex.Message, "#4");
- Assert.AreEqual (typeof (Socket).FullName, ex.ObjectName, "#5");
- }
- }
- [Test] // Receive (Byte [], Int32, Int32, SocketFlags, out SocketError)
- public void Receive5_Buffer_Null ()
- {
- Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
- ProtocolType.Tcp);
- SocketError error;
- try {
- s.Receive ((byte []) null, 0, 0, SocketFlags.None, out error);
- Assert.Fail ("#1");
- } catch (ArgumentNullException ex) {
- Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
- Assert.IsNull (ex.InnerException, "#3");
- Assert.IsNotNull (ex.Message, "#4");
- Assert.AreEqual ("buffer", ex.ParamName, "#5");
- } finally {
- s.Close ();
- }
- }
- [Test] // Receive (Byte [], Int32, Int32, SocketFlags, out SocketError)
- public void Receive5_Socket_Closed ()
- {
- Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
- ProtocolType.Tcp);
- s.Close ();
- SocketError error;
- try {
- s.Receive ((byte []) null, 0, 0, SocketFlags.None, out error);
- Assert.Fail ("#1");
- } catch (ObjectDisposedException ex) {
- // Cannot access a disposed object
- Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
- Assert.IsNull (ex.InnerException, "#3");
- Assert.IsNotNull (ex.Message, "#4");
- Assert.AreEqual (typeof (Socket).FullName, ex.ObjectName, "#5");
- }
- }
- [Test] // Receive (IList<ArraySegment<Byte>>)
- public void Receive6_Buffers_Null ()
- {
- Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
- ProtocolType.Tcp);
- try {
- s.Receive ((IList<ArraySegment<byte>>) null);
- Assert.Fail ("#1");
- } catch (ArgumentNullException ex) {
- Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
- Assert.IsNull (ex.InnerException, "#3");
- Assert.IsNotNull (ex.Message, "#4");
- Assert.AreEqual ("buffers", ex.ParamName, "#5");
- } finally {
- s.Close ();
- }
- }
- [Test] // Receive (IList<ArraySegment<Byte>>)
- public void Receive6_Socket_Closed ()
- {
- Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
- ProtocolType.Tcp);
- s.Close ();
- try {
- s.Receive ((IList<ArraySegment<byte>>) null);
- Assert.Fail ("#1");
- } catch (ObjectDisposedException ex) {
- // Cannot access a disposed object
- Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
- Assert.IsNull (ex.InnerException, "#3");
- Assert.IsNotNull (ex.Message, "#4");
- Assert.AreEqual (typeof (Socket).FullName, ex.ObjectName, "#5");
- }
- }
- [Test] // Receive (IList<ArraySegment<Byte>>, SocketFlags)
- public void Receive7_Buffers_Null ()
- {
- Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
- ProtocolType.Tcp);
- try {
- s.Receive ((IList<ArraySegment<byte>>) null, (SocketFlags) 666);
- Assert.Fail ("#1");
- } catch (ArgumentNullException ex) {
- Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
- Assert.IsNull (ex.InnerException, "#3");
- Assert.IsNotNull (ex.Message, "#4");
- Assert.AreEqual ("buffers", ex.ParamName, "#5");
- } finally {
- s.Close ();
- }
- }
- [Test] // Receive (IList<ArraySegment<Byte>>, SocketFlags)
- public void Receive7_Socket_Closed ()
- {
- Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
- ProtocolType.Tcp);
- s.Close ();
- try {
- s.Receive ((IList<ArraySegment<byte>>) null, (SocketFlags) 666);
- Assert.Fail ("#1");
- } catch (ObjectDisposedException ex) {
- // Cannot access a disposed object
- Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
- Assert.IsNull (ex.InnerException, "#3");
- Assert.IsNotNull (ex.Message, "#4");
- Assert.AreEqual (typeof (Socket).FullName, ex.ObjectName, "#5");
- }
- }
- [Test] // Receive (IList<ArraySegment<Byte>>, SocketFlags, out SocketError)
- public void Receive8_Buffers_Null ()
- {
- Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
- ProtocolType.Tcp);
- SocketError error;
- try {
- s.Receive ((IList<ArraySegment<byte>>) null, (SocketFlags) 666,
- out error);
- Assert.Fail ("#1");
- } catch (ArgumentNullException ex) {
- Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
- Assert.IsNull (ex.InnerException, "#3");
- Assert.IsNotNull (ex.Message, "#4");
- Assert.AreEqual ("buffers", ex.ParamName, "#5");
- } finally {
- s.Close ();
- }
- }
- [Test] // Receive (IList<ArraySegment<Byte>>, SocketFlags, out SocketError)
- public void Receive8_Socket_Closed ()
- {
- Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
- ProtocolType.Tcp);
- s.Close ();
- SocketError error;
- try {
- s.Receive ((IList<ArraySegment<byte>>) null, (SocketFlags) 666,
- out error);
- Assert.Fail ("#1");
- } catch (ObjectDisposedException ex) {
- // Cannot access a disposed object
- Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
- Assert.IsNull (ex.InnerException, "#3");
- Assert.IsNotNull (ex.Message, "#4");
- Assert.AreEqual (typeof (Socket).FullName, ex.ObjectName, "#5");
- } finally {
- s.Close ();
- }
- }
- [Test] // ReceiveFrom (Byte [], ref EndPoint)
- public void ReceiveFrom1_Buffer_Null ()
- {
- Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
- ProtocolType.Tcp);
- EndPoint remoteEP = new IPEndPoint (IPAddress.Loopback, NetworkHelpers.FindFreePort ());
- try {
- s.ReceiveFrom ((Byte []) null, ref remoteEP);
- Assert.Fail ("#1");
- } catch (ArgumentNullException ex) {
- Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
- Assert.IsNull (ex.InnerException, "#3");
- Assert.IsNotNull (ex.Message, "#4");
- Assert.AreEqual ("buffer", ex.ParamName, "#5");
- } finally {
- s.Close ();
- }
- }
- [Test] // ReceiveFrom (Byte [], ref EndPoint)
- public void ReceiveFrom1_RemoteEP_Null ()
- {
- Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
- ProtocolType.Tcp);
- byte [] buffer = new byte [0];
- EndPoint remoteEP = null;
- try {
- s.ReceiveFrom (buffer, ref remoteEP);
- Assert.Fail ("#1");
- } catch (ArgumentNullException ex) {
- Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
- Assert.IsNull (ex.InnerException, "#3");
- Assert.IsNotNull (ex.Message, "#4");
- Assert.AreEqual ("remoteEP", ex.ParamName, "#5");
- } finally {
- s.Close ();
- }
- }
- [Test] // ReceiveFrom (Byte [], ref EndPoint)
- public void ReceiveFrom1_Socket_Closed ()
- {
- Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
- ProtocolType.Tcp);
- s.Close ();
- EndPoint remoteEP = new IPEndPoint (IPAddress.Loopback, NetworkHelpers.FindFreePort ());
- try {
- s.ReceiveFrom ((Byte []) null, ref remoteEP);
- Assert.Fail ("#1");
- } catch (ObjectDisposedException ex) {
- // Cannot access a disposed object
- Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
- Assert.IsNull (ex.InnerException, "#3");
- Assert.IsNotNull (ex.Message, "#4");
- Assert.AreEqual (typeof (Socket).FullName, ex.ObjectName, "#5");
- }
- }
- [Test] // ReceiveFrom (Byte [], SocketFlags, ref EndPoint)
- public void ReceiveFrom2_Buffer_Null ()
- {
- Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
- ProtocolType.Tcp);
- EndPoint remoteEP = new IPEndPoint (IPAddress.Loopback, NetworkHelpers.FindFreePort ());
- try {
- s.ReceiveFrom ((Byte []) null, (SocketFlags) 666, ref remoteEP);
- Assert.Fail ("#1");
- } catch (ArgumentNullException ex) {
- Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
- Assert.IsNull (ex.InnerException, "#3");
- Assert.IsNotNull (ex.Message, "#4");
- Assert.AreEqual ("buffer", ex.ParamName, "#5");
- } finally {
- s.Close ();
- }
- }
- [Test] // ReceiveFrom (Byte [], SocketFlags, ref EndPoint)
- public void ReceiveFrom2_RemoteEP_Null ()
- {
- Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
- ProtocolType.Tcp);
- byte [] buffer = new byte [5];
- EndPoint remoteEP = null;
- try {
- s.ReceiveFrom (buffer, (SocketFlags) 666, ref remoteEP);
- Assert.Fail ("#1");
- } catch (ArgumentNullException ex) {
- Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
- Assert.IsNull (ex.InnerException, "#3");
- Assert.IsNotNull (ex.Message, "#4");
- Assert.AreEqual ("remoteEP", ex.ParamName, "#5");
- } finally {
- s.Close ();
- }
- }
- [Test] // ReceiveFrom (Byte [], SocketFlags, ref EndPoint)
- public void ReceiveFrom2_Socket_Closed ()
- {
- Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
- ProtocolType.Tcp);
- s.Close ();
- EndPoint remoteEP = new IPEndPoint (IPAddress.Loopback, NetworkHelpers.FindFreePort ());
- try {
- s.ReceiveFrom ((Byte []) null, (SocketFlags) 666, ref remoteEP);
- Assert.Fail ("#1");
- } catch (ObjectDisposedException ex) {
- // Cannot access a disposed object
- Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
- Assert.IsNull (ex.InnerException, "#3");
- Assert.IsNotNull (ex.Message, "#4");
- Assert.AreEqual (typeof (Socket).FullName, ex.ObjectName, "#5");
- }
- }
- [Test] // ReceiveFrom (Byte [], Int32, SocketFlags, ref EndPoint)
- public void ReceiveFrom3_Buffer_Null ()
- {
- Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
- ProtocolType.Tcp);
- EndPoint remoteEP = new IPEndPoint (IPAddress.Loopback, NetworkHelpers.FindFreePort ());
- try {
- s.ReceiveFrom ((Byte []) null, 0, (SocketFlags) 666,
- ref remoteEP);
- Assert.Fail ("#1");
- } catch (ArgumentNullException ex) {
- Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
- Assert.IsNull (ex.InnerException, "#3");
- Assert.IsNotNull (ex.Message, "#4");
- Assert.AreEqual ("buffer", ex.ParamName, "#5");
- } finally {
- s.Close ();
- }
- }
- [Test] // ReceiveFrom (Byte [], Int32, SocketFlags, ref EndPoint)
- public void ReceiveFrom3_RemoteEP_Null ()
- {
- Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
- ProtocolType.Tcp);
- byte [] buffer = new byte [5];
- EndPoint remoteEP = null;
- try {
- s.ReceiveFrom (buffer, buffer.Length, (SocketFlags) 666, ref remoteEP);
- Assert.Fail ("#1");
- } catch (ArgumentNullException ex) {
- Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
- Assert.IsNull (ex.InnerException, "#3");
- Assert.IsNotNull (ex.Message, "#4");
- Assert.AreEqual ("remoteEP", ex.ParamName, "#5");
- } finally {
- s.Close ();
- }
- }
- [Test] // ReceiveFrom (Byte [], Int32, SocketFlags, ref EndPoint)
- public void ReceiveFrom3_Size_OutOfRange ()
- {
- Socket s;
- byte [] buffer = new byte [5];
- EndPoint remoteEP = new IPEndPoint (IPAddress.Loopback, NetworkHelpers.FindFreePort ());
- // size negative
- s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
- ProtocolType.Tcp);
- try {
- s.ReceiveFrom (buffer, -1, (SocketFlags) 666, ref remoteEP);
- Assert.Fail ("#A1");
- } catch (ArgumentOutOfRangeException ex) {
- // Specified argument was out of the range of valid values
- Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#A2");
- Assert.IsNull (ex.InnerException, "#A3");
- Assert.IsNotNull (ex.Message, "#A4");
- Assert.AreEqual ("size", ex.ParamName, "#A5");
- } finally {
- s.Close ();
- }
- // size > buffer length
- s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
- ProtocolType.Tcp);
- try {
- s.ReceiveFrom (buffer, (buffer.Length + 1), (SocketFlags) 666,
- ref remoteEP);
- Assert.Fail ("#B1");
- } catch (ArgumentOutOfRangeException ex) {
- // Specified argument was out of the range of valid values
- Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#B2");
- Assert.IsNull (ex.InnerException, "#B3");
- Assert.IsNotNull (ex.Message, "#B4");
- Assert.AreEqual ("size", ex.ParamName, "#B5");
- } finally {
- s.Close ();
- }
- }
- [Test] // ReceiveFrom (Byte [], Int32, SocketFlags, ref EndPoint)
- public void ReceiveFrom3_Socket_Closed ()
- {
- Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
- ProtocolType.Tcp);
- s.Close ();
- EndPoint remoteEP = new IPEndPoint (IPAddress.Loopback, NetworkHelpers.FindFreePort ());
- try {
- s.ReceiveFrom ((Byte []) null, -1, (SocketFlags) 666,
- ref remoteEP);
- Assert.Fail ("#1");
- } catch (ObjectDisposedException ex) {
- // Cannot access a disposed object
- Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
- Assert.IsNull (ex.InnerException, "#3");
- Assert.IsNotNull (ex.Message, "#4");
- Assert.AreEqual (typeof (Socket).FullName, ex.ObjectName, "#5");
- }
- }
- [Test] // ReceiveFrom (Byte [], Int32, Int32, SocketFlags, EndPoint)
- public void ReceiveFrom4_Buffer_Null ()
- {
- Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
- ProtocolType.Tcp);
- EndPoint remoteEP = new IPEndPoint (IPAddress.Loopback, NetworkHelpers.FindFreePort ());
- try {
- s.ReceiveFrom ((Byte []) null, -1, -1, (SocketFlags) 666,
- ref remoteEP);
- Assert.Fail ("#1");
- } catch (ArgumentNullException ex) {
- Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
- Assert.IsNull (ex.InnerException, "#3");
- Assert.IsNotNull (ex.Message, "#4");
- Assert.AreEqual ("buffer", ex.ParamName, "#5");
- }
- }
- [Test] // ReceiveFrom (Byte [], Int32, Int32, SocketFlags, EndPoint)
- public void ReceiveFrom4_Offset_OutOfRange ()
- {
- Socket s;
- byte [] buffer = new byte [5];
- EndPoint remoteEP = new IPEndPoint (IPAddress.Loopback, NetworkHelpers.FindFreePort ());
- // offset negative
- s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
- ProtocolType.Tcp);
- try {
- s.ReceiveFrom (buffer, -1, 0, (SocketFlags) 666,
- ref remoteEP);
- Assert.Fail ("#A1");
- } catch (ArgumentOutOfRangeException ex) {
- // Specified argument was out of the range of valid values
- Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#A2");
- Assert.IsNull (ex.InnerException, "#A3");
- Assert.IsNotNull (ex.Message, "#A4");
- Assert.AreEqual ("offset", ex.ParamName, "#A5");
- } finally {
- s.Close ();
- }
- // offset > buffer length
- s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
- ProtocolType.Tcp);
- try {
- s.ReceiveFrom (buffer, (buffer.Length + 1), 0, (SocketFlags) 666,
- ref remoteEP);
- Assert.Fail ("#B1");
- } catch (ArgumentOutOfRangeException ex) {
- // Specified argument was out of the range of valid values
- Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#B2");
- Assert.IsNull (ex.InnerException, "#B3");
- Assert.IsNotNull (ex.Message, "#B4");
- Assert.AreEqual ("offset", ex.ParamName, "#B5");
- } finally {
- s.Close ();
- }
- }
- [Test] // ReceiveFrom (Byte [], Int32, Int32, SocketFlags, ref IPEndPoint)
- public void ReceiveFrom4_RemoteEP_Null ()
- {
- Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
- ProtocolType.Tcp);
- byte [] buffer = new byte [5];
- EndPoint remoteEP = null;
- try {
- s.ReceiveFrom (buffer, 0, buffer.Length, (SocketFlags) 666, ref remoteEP);
- Assert.Fail ("#1");
- } catch (ArgumentNullException ex) {
- Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
- Assert.IsNull (ex.InnerException, "#3");
- Assert.IsNotNull (ex.Message, "#4");
- Assert.AreEqual ("remoteEP", ex.ParamName, "#5");
- } finally {
- s.Close ();
- }
- }
- [Test] // ReceiveFrom (Byte [], Int32, Int32, SocketFlags, EndPoint)
- public void ReceiveFrom4_Size_OutOfRange ()
- {
- Socket s;
- byte [] buffer = new byte [5];
- EndPoint remoteEP = new IPEndPoint (IPAddress.Loopback, NetworkHelpers.FindFreePort ());
- // size negative
- s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
- ProtocolType.Tcp);
- try {
- s.ReceiveFrom (buffer, 0, -1, (SocketFlags) 666,
- ref remoteEP);
- Assert.Fail ("#A1");
- } catch (ArgumentOutOfRangeException ex) {
- // Specified argument was out of the range of valid values
- Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#A2");
- Assert.IsNull (ex.InnerException, "#A3");
- Assert.IsNotNull (ex.Message, "#A4");
- Assert.AreEqual ("size", ex.ParamName, "#A5");
- } finally {
- s.Close ();
- }
- // size > buffer length
- s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
- ProtocolType.Tcp);
- try {
- s.ReceiveFrom (buffer, 0, (buffer.Length + 1), (SocketFlags) 666,
- ref remoteEP);
- Assert.Fail ("#B1");
- } catch (ArgumentOutOfRangeException ex) {
- // Specified argument was out of the range of valid values
- Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#B2");
- Assert.IsNull (ex.InnerException, "#B3");
- Assert.IsNotNull (ex.Message, "#B4");
- Assert.AreEqual ("size", ex.ParamName, "#B5");
- } finally {
- s.Close ();
- }
- // offset + size > buffer length
- s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
- ProtocolType.Tcp);
- try {
- s.ReceiveFrom (buffer, 2, 4, (SocketFlags) 666, ref remoteEP);
- Assert.Fail ("#C1");
- } catch (ArgumentOutOfRangeException ex) {
- // Specified argument was out of the range of valid values
- Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#C2");
- Assert.IsNull (ex.InnerException, "#C3");
- Assert.IsNotNull (ex.Message, "#C4");
- Assert.AreEqual ("size", ex.ParamName, "#C5");
- } finally {
- s.Close ();
- }
- }
- [Test] // ReceiveFrom (Byte [], Int32, Int32, SocketFlags, ref EndPoint)
- public void ReceiveFrom4_Socket_Closed ()
- {
- Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
- ProtocolType.Tcp);
- s.Close ();
- byte [] buffer = new byte [5];
- EndPoint remoteEP = new IPEndPoint (IPAddress.Loopback, NetworkHelpers.FindFreePort ());
- try {
- s.ReceiveFrom (buffer, -1, -1, (SocketFlags) 666,
- ref remoteEP);
- Assert.Fail ("#1");
- } catch (ObjectDisposedException ex) {
- // Cannot access a disposed object
- Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
- Assert.IsNull (ex.InnerException, "#3");
- Assert.IsNotNull (ex.Message, "#4");
- Assert.AreEqual (typeof (Socket).FullName, ex.ObjectName, "#5");
- }
- }
- [Test]
- public void ReceiveRemoteClosed ()
- {
- var port = NetworkHelpers.FindFreePort ();
- Socket sock = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
- sock.Bind (new IPEndPoint (IPAddress.Loopback, port));
- sock.Listen (1);
-
- RRCReady.Reset ();
- Thread client_thread = new Thread (() => RRCClientThread (port));
- client_thread.Start ();
-
- Socket client = sock.Accept ();
- NetworkStream stream = new NetworkStream (client);
- stream.WriteByte (0x00);
- stream.WriteByte (0x00);
- client.Close ();
- sock.Close ();
- RRCReady.WaitOne (1000, false);
- Assert.IsTrue (RRCLastRead);
- }
- //
- // Test case for bug #471580
- [Test]
- public void UdpDoubleBind ()
- {
- using (Socket s = new Socket (AddressFamily.InterNetwork,
- SocketType.Dgram, ProtocolType.Udp))
- using (Socket ss = new Socket (AddressFamily.InterNetwork,
- SocketType.Dgram, ProtocolType.Udp)) {
- var supportsReuseAddress = true;
- try {
- s.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
- } catch (SocketException e) {
- // Exception is thrown when ReuseAddress is not supported
- Assert.AreEqual ((int) SocketError.OperationNotSupported, e.NativeErrorCode,
- "Expected SocketError.OperationNotSupported");
- supportsReuseAddress = false;
- }
- var ep = new IPEndPoint (IPAddress.Any, NetworkHelpers.FindFreePort ());
- s.Bind (ep);
- if (supportsReuseAddress)
- ss.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
- try {
- ss.Bind (new IPEndPoint (IPAddress.Any, ep.Port));
- if (!supportsReuseAddress)
- Assert.Fail ("Reusing address is not supported, exception was expected on second bind.");
- } catch (SocketException e) {
- }
- }
- }
- // Test case for bug #31557
- [Test]
- public void TcpDoubleBind ()
- {
- using (Socket s = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream, ProtocolType.Tcp))
- using (Socket ss = new Socket (AddressFamily.InterNetwork,
- SocketType.Stream, ProtocolType.Tcp)) {
- var supportsReuseAddress = true;
- try {
- s.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
- } catch (SocketException e) {
- // Exception is thrown when ReuseAddress is not supported
- Assert.AreEqual ((int) SocketError.OperationNotSupported, e.NativeErrorCode,
- "Expected SocketError.OperationNotSupported");
- supportsReuseAddress = false;
- }
- var ep = new IPEndPoint (IPAddress.Any, NetworkHelpers.FindFreePort ());
- s.Bind (ep);
- s.Listen(1);
- if (supportsReuseAddress)
- ss.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
- try {
- ss.Bind (new IPEndPoint (IPAddress.Any, ep.Port));
- ss.Listen(1);
- if (!supportsReuseAddress)
- Assert.Fail ("Reusing address is not supported, exception was expected on second bind.");
- } catch (SocketException e) {
- }
- }
- }
- [Test]
- [Category ("NotOnMac")]
- public void ConnectedProperty ()
- {
- TcpListener listener = new TcpListener (IPAddress.Loopback, NetworkHelpers.FindFreePort ());
- listener.Start();
- Socket client = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
- client.Connect (IPAddress.Loopback, ((IPEndPoint)listener.LocalEndpoint).Port);
- Socket server = listener.AcceptSocket ();
- try {
- server.EndSend(server.BeginSend (new byte[10], 0, 10, SocketFlags.None, null, null));
- client.Close ();
- try {
- server.EndReceive (server.BeginReceive (new byte[10], 0, 10, SocketFlags.None, null, null));
- } catch {
- }
- Assert.IsTrue (!client.Connected);
- Assert.IsTrue (!server.Connected);
- } finally {
- listener.Stop ();
- client.Close ();
- server.Close ();
- }
- }
- [Test] // GetSocketOption (SocketOptionLevel, SocketOptionName)
- public void GetSocketOption1_Socket_Closed ()
- {
- Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
- s.Close ();
- try {
- s.GetSocketOption (0, 0);
- Assert.Fail ("#1");
- } catch (ObjectDisposedException ex) {
- // Cannot access a disposed object
- Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
- Assert.IsNull (ex.InnerException, "#3");
- Assert.IsNotNull (ex.Message, "#4");
- Assert.AreEqual (typeof (Socket).FullName, ex.ObjectName, "#5");
- }
- }
- [Test] // GetSocketOption (SocketOptionLevel, SocketOptionName, Byte [])
- public void GetSocketOption2_OptionValue_Null ()
- {
- Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
- try {
- s.GetSocketOption (SocketOptionLevel.Socket, SocketOptionName.Linger,
- (byte []) null);
- Assert.Fail ("#1");
- } catch (SocketException ex) {
- // The system detected an invalid pointer address in attempting
- // to use a pointer argument in a call
- Assert.AreEqual (typeof (SocketException), ex.GetType (), "#2");
- Assert.AreEqual (10014, ex.ErrorCode, "#3");
- Assert.IsNull (ex.InnerException, "#4");
- Assert.IsNotNull (ex.Message, "#5");
- Assert.AreEqual (10014, ex.NativeErrorCode, "#6");
- Assert.AreEqual (SocketError.Fault, ex.SocketErrorCode, "#7");
- }
- }
- [Test] // GetSocketOption (SocketOptionLevel, SocketOptionName, Byte [])
- public void GetSocketOption2_Socket_Closed ()
- {
- Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
- s.Close ();
- try {
- s.GetSocketOption (0, 0, (byte []) null);
- Assert.Fail ("#1");
- } catch (ObjectDisposedException ex) {
- // Cannot access a disposed object
- Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
- Assert.IsNull (ex.InnerException, "#3");
- Assert.IsNotNull (ex.Message, "#4");
- Assert.AreEqual (typeof (Socket).FullName, ex.ObjectName, "#5");
- }
- }
- [Test] // GetSocketOption (SocketOptionLevel, SocketOptionName, Int32)
- public void GetSocketOption3_Socket_Closed ()
- {
- Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
- s.Close ();
- try {
- s.GetSocketOption (0, 0, 0);
- Assert.Fail ("#1");
- } catch (ObjectDisposedException ex) {
- // Cannot access a disposed object
- Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
- Assert.IsNull (ex.InnerException, "#3");
- Assert.IsNotNull (ex.Message, "#4");
- Assert.AreEqual (typeof (Socket).FullName, ex.ObjectName, "#5");
- }
- }
- [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Byte [])
- public void SetSocketOption1_DontLinger ()
- {
- using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) {
- s.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.DontLinger,
- new byte [] { 0x00 });
- s.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.DontLinger,
- new byte [] { 0x01 });
- }
- }
- [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Byte [])
- public void SetSocketOption1_DontLinger_Null ()
- {
- using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) {
- try {
- s.SetSocketOption (SocketOptionLevel.Socket,
- SocketOptionName.DontLinger, (byte []) null);
- Assert.Fail ("#1");
- } catch (SocketException ex) {
- // The system detected an invalid pointer address in attempting
- // to use a pointer argument in a call
- Assert.AreEqual (typeof (SocketException), ex.GetType (), "#2");
- Assert.AreEqual (10014, ex.ErrorCode, "#3");
- Assert.IsNull (ex.InnerException, "#4");
- Assert.IsNotNull (ex.Message, "#5");
- Assert.AreEqual (10014, ex.NativeErrorCode, "#6");
- Assert.AreEqual (SocketError.Fault, ex.SocketErrorCode, "#7");
- }
- }
- }
- [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Byte [])
- public void SetSocketOption1_Linger_Null ()
- {
- using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) {
- try {
- s.SetSocketOption (SocketOptionLevel.Socket,
- SocketOptionName.DontLinger, (byte []) null);
- Assert.Fail ("#1");
- } catch (SocketException ex) {
- // The system detected an invalid pointer address in attempting
- // to use a pointer argument in a call
- Assert.AreEqual (typeof (SocketException), ex.GetType (), "#2");
- Assert.AreEqual (10014, ex.ErrorCode, "#3");
- Assert.IsNull (ex.InnerException, "#4");
- Assert.IsNotNull (ex.Message, "#5");
- Assert.AreEqual (10014, ex.NativeErrorCode, "#6");
- Assert.AreEqual (SocketError.Fault, ex.SocketErrorCode, "#7");
- }
- }
- }
- [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Byte [])
- public void SetSocketOption1_Socket_Close ()
- {
- Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
- s.Close ();
- try {
- s.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.DontLinger,
- new byte [] { 0x00 });
- Assert.Fail ("#1");
- } catch (ObjectDisposedException ex) {
- Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
- Assert.IsNull (ex.InnerException, "#3");
- Assert.IsNotNull (ex.Message, "#4");
- Assert.AreEqual (typeof (Socket).FullName, ex.ObjectName, "#5");
- }
- }
- [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Int32)
- public void SetSocketOption2_DontLinger ()
- {
- using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) {
- s.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.DontLinger, 0);
- s.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.DontLinger, 5);
- }
- }
- [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Int32)
- [Category ("NotWorking")]
- public void SetSocketOption2_Linger ()
- {
- using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) {
- s.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.Linger, 0);
- s.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.Linger, 5);
- }
- }
- [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Int32)
- public void SetSocketOption2_Socket_Closed ()
- {
- Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
- s.Close ();
- try {
- s.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.DontLinger, 0);
- Assert.Fail ("#1");
- } catch (ObjectDisposedException ex) {
- // Cannot access a disposed object
- Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
- Assert.IsNull (ex.InnerException, "#3");
- Assert.IsNotNull (ex.Message, "#4");
- Assert.AreEqual (typeof (Socket).FullName, ex.ObjectName, "#5");
- }
- }
- [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Object)
- public void SetSocketOption3_AddMembershipIPv4_IPv6MulticastOption ()
- {
- IPAddress mcast_addr = IPAddress.Parse ("239.255.255.250");
- using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp)) {
- s.Bind (new IPEndPoint (IPAddress.Any, NetworkHelpers.FindFreePort ()));
- try {
- s.SetSocketOption (SocketOptionLevel.IP, SocketOptionName.AddMembership,
- new IPv6MulticastOption (mcast_addr));
- Assert.Fail ("#1");
- } catch (ArgumentException ex) {
- Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
- Assert.IsNull (ex.InnerException, "#3");
- Assert.IsNotNull (ex.Message, "#4");
- // The specified value is not a valid 'MulticastOption'
- Assert.IsTrue (ex.Message.IndexOf ("'MulticastOption'") != -1, "#5:" + ex.Message);
- Assert.AreEqual ("optionValue", ex.ParamName, "#6");
- }
- }
- }
- [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Object)
- public void SetSocketOption3_AddMembershipIPv4_MulticastOption ()
- {
- IPAddress mcast_addr = IPAddress.Parse ("239.255.255.250");
- using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp)) {
- s.Bind (new IPEndPoint (IPAddress.Any, NetworkHelpers.FindFreePort ()));
- s.SetSocketOption (SocketOptionLevel.IP, SocketOptionName.AddMembership,
- new MulticastOption (mcast_addr));
- }
- }
- [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Object)
- [Category ("NotWorking")]
- public void SetSocketOption3_AddMembershipIPv4_Socket_NotBound ()
- {
- IPAddress mcast_addr = IPAddress.Parse ("239.255.255.250");
- Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
- try {
- s.SetSocketOption (SocketOptionLevel.IP, SocketOptionName.AddMembership,
- new MulticastOption (mcast_addr));
- Assert.Fail ("#1");
- } catch (SocketException ex) {
- // An invalid argument was supplied
- Assert.AreEqual (typeof (SocketException), ex.GetType (), "#2");
- Assert.AreEqual (10022, ex.ErrorCode, "#3");
- Assert.IsNull (ex.InnerException, "#4");
- Assert.IsNotNull (ex.Message, "#5");
- Assert.AreEqual (10022, ex.NativeErrorCode, "#6");
- Assert.AreEqual (SocketError.InvalidArgument, ex.SocketErrorCode, "#7");
- } finally {
- s.Close ();
- }
- }
- [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Object)
- public void SetSocketOption3_AddMembershipIPv6_IPv6MulticastOption ()
- {
- if (!Socket.OSSupportsIPv6)
- Assert.Ignore ("IPv6 not enabled.");
- IPAddress mcast_addr = IPAddress.Parse ("ff02::1");
- using (Socket s = new Socket (AddressFamily.InterNetworkV6, SocketType.Dgram, ProtocolType.Udp)) {
- s.Bind (new IPEndPoint (IPAddress.IPv6Any, NetworkHelpers.FindFreePort ()));
- s.SetSocketOption (SocketOptionLevel.IPv6, SocketOptionName.AddMembership,
- new IPv6MulticastOption (mcast_addr));
- }
- }
- [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Object)
- public void SetSocketOption3_AddMembershipIPv6_MulticastOption ()
- {
- if (!Socket.OSSupportsIPv6)
- Assert.Ignore ("IPv6 not enabled.");
- IPAddress mcast_addr = IPAddress.Parse ("ff02::1");
- using (Socket s = new Socket (AddressFamily.InterNetworkV6, SocketType.Dgram, ProtocolType.Udp)) {
- s.Bind (new IPEndPoint (IPAddress.IPv6Any, NetworkHelpers.FindFreePort ()));
- try {
- s.SetSocketOption (SocketOptionLevel.IPv6, SocketOptionName.AddMembership,
- new MulticastOption (mcast_addr));
- Assert.Fail ("#1");
- } catch (ArgumentException ex) {
- Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
- Assert.IsNull (ex.InnerException, "#3");
- Assert.IsNotNull (ex.Message, "#4");
- // The specified value is not a valid 'IPv6MulticastOption'
- Assert.IsTrue (ex.Message.IndexOf ("'IPv6MulticastOption'") != -1, "#5:" + ex.Message);
- Assert.AreEqual ("optionValue", ex.ParamName, "#6");
- }
- }
- }
- [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Object)
- [Category ("NotWorking")]
- public void SetSocketOption3_AddMembershipIPv6_Socket_NotBound ()
- {
- IPAddress mcast_addr = IPAddress.Parse ("ff02::1");
- Socket s = new Socket (AddressFamily.InterNetworkV6, SocketType.Dgram, ProtocolType.Udp);
- try {
- s.SetSocketOption (SocketOptionLevel.IPv6, SocketOptionName.AddMembership,
- new IPv6MulticastOption (mcast_addr));
- Assert.Fail ("#1");
- } catch (SocketException ex) {
- // An invalid argument was supplied
- Assert.AreEqual (typeof (SocketException), ex.GetType (), "#2");
- Assert.AreEqual (10022, ex.ErrorCode, "#3");
- Assert.IsNull (ex.InnerException, "#4");
- Assert.IsNotNull (ex.Message, "#5");
- Assert.AreEqual (10022, ex.NativeErrorCode, "#6");
- Assert.AreEqual (SocketError.InvalidArgument, ex.SocketErrorCode, "#7");
- } finally {
- s.Close ();
- }
- }
- [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Object)
- public void SetSocketOption3_DontLinger_Boolean ()
- {
- using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) {
- try {
- s.SetSocketOption (SocketOptionLevel.Socket,
- SocketOptionName.DontLinger, (object) false);
- Assert.Fail ("#1");
- } catch (ArgumentException ex) {
- // The specified value is not valid
- Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
- Assert.IsNull (ex.InnerException, "#3");
- Assert.IsNotNull (ex.Message, "#4");
- Assert.AreEqual ("optionValue", ex.ParamName, "#5");
- }
- }
- }
- [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Object)
- public void SetSocketOption3_DontLinger_Int32 ()
- {
- using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) {
- try {
- s.SetSocketOption (SocketOptionLevel.Socket,
- SocketOptionName.DontLinger, (object) 0);
- Assert.Fail ("#1");
- } catch (ArgumentException ex) {
- // The specified value is not valid
- Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
- Assert.IsNull (ex.InnerException, "#3");
- Assert.IsNotNull (ex.Message, "#4");
- Assert.AreEqual ("optionValue", ex.ParamName, "#5");
- }
- }
- }
- [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Object)
- public void SetSocketOption3_DontLinger_LingerOption ()
- {
- using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) {
- try {
- s.SetSocketOption (SocketOptionLevel.Socket,
- SocketOptionName.DontLinger, new LingerOption (true, 1000));
- Assert.Fail ("#1");
- } catch (ArgumentException ex) {
- Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
- Assert.IsNull (ex.InnerException, "#3");
- // The specified value is not valid
- Assert.IsNotNull (ex.Message, "#4");
- Assert.AreEqual ("optionValue", ex.ParamName, "#5");
- }
- }
- }
- [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Object)
- public void SetSocketOption3_Linger_Boolean ()
- {
- using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) {
- try {
- s.SetSocketOption (SocketOptionLevel.Socket,
- SocketOptionName.Linger, (object) false);
- Assert.Fail ("#1");
- } catch (ArgumentException ex) {
- Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
- Assert.IsNull (ex.InnerException, "#3");
- // The specified value is not valid
- Assert.IsNotNull (ex.Message, "#4");
- Assert.AreEqual ("optionValue", ex.ParamName, "#5");
- }
- }
- }
- [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Object)
- public void SetSocketOption3_Linger_Int32 ()
- {
- using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) {
- try {
- s.SetSocketOption (SocketOptionLevel.Socket,
- SocketOptionName.Linger, (object) 0);
- Assert.Fail ("#1");
- } catch (ArgumentException ex) {
- Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
- Assert.IsNull (ex.InnerException, "#3");
- // The specified value is not valid
- Assert.IsNotNull (ex.Message, "#4");
- Assert.AreEqual ("optionValue", ex.ParamName, "#5");
- }
- }
- }
- [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Object)
- public void SetSocketOption3_Linger_LingerOption ()
- {
- using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) {
- s.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.Linger,
- new LingerOption (false, 0));
- s.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.Linger,
- new LingerOption (true, 0));
- s.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.Linger,
- new LingerOption (false, 1000));
- s.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.Linger,
- new LingerOption (true, 1000));
- }
- }
- [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Object)
- public void SetSocketOption3_DropMembershipIPv4_IPv6MulticastOption ()
- {
- IPAddress mcast_addr = IPAddress.Parse ("239.255.255.250");
- using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp)) {
- s.Bind (new IPEndPoint (IPAddress.Any, NetworkHelpers.FindFreePort ()));
- s.SetSocketOption (SocketOptionLevel.IP, SocketOptionName.AddMembership,
- new MulticastOption (mcast_addr));
- try {
- s.SetSocketOption (SocketOptionLevel.IP, SocketOptionName.DropMembership,
- new IPv6MulticastOption (mcast_addr));
- Assert.Fail ("#1");
- } catch (ArgumentException ex) {
- Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
- Assert.IsNull (ex.InnerException, "#3");
- Assert.IsNotNull (ex.Message, "#4");
- // The specified value is not a valid 'MulticastOption'
- Assert.IsTrue (ex.Message.IndexOf ("'MulticastOption'") != -1, "#5:" + ex.Message);
- Assert.AreEqual ("optionValue", ex.ParamName, "#6");
- }
- }
- }
- [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Object)
- public void SetSocketOption3_DropMembershipIPv4_MulticastOption ()
- {
- IPAddress mcast_addr = IPAddress.Parse ("239.255.255.250");
- using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp)) {
- MulticastOption option = new MulticastOption (mcast_addr);
- s.Bind (new IPEndPoint (IPAddress.Any, NetworkHelpers.FindFreePort ()));
- s.SetSocketOption (SocketOptionLevel.IP, SocketOptionName.AddMembership,
- option);
- s.SetSocketOption (SocketOptionLevel.IP, SocketOptionName.DropMembership,
- option);
- }
- }
- [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Object)
- [Category ("NotWorking")]
- public void SetSocketOption3_DropMembershipIPv4_Socket_NotBound ()
- {
- IPAddress mcast_addr = IPAddress.Parse ("239.255.255.250");
- Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
- try {
- s.SetSocketOption (SocketOptionLevel.IP, SocketOptionName.DropMembership,
- new MulticastOption (mcast_addr));
- Assert.Fail ("#1");
- } catch (SocketException ex) {
- // An invalid argument was supplied
- Assert.AreEqual (typeof (SocketException), ex.GetType (), "#2");
- Assert.AreEqual (10022, ex.ErrorCode, "#3");
- Assert.IsNull (ex.InnerException, "#4");
- Assert.IsNotNull (ex.Message, "#5");
- Assert.AreEqual (10022, ex.NativeErrorCode, "#6");
- Assert.AreEqual (SocketError.InvalidArgument, ex.SocketErrorCode, "#7");
- } finally {
- s.Close ();
- }
- }
- [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Object)
- public void SetSocketOption3_DropMembershipIPv6_IPv6MulticastOption ()
- {
- if (!Socket.OSSupportsIPv6)
- Assert.Ignore ("IPv6 not enabled.");
- using (Socket s = new Socket (AddressFamily.InterNetworkV6, SocketType.Dgram, ProtocolType.Udp)) {
- IPv6MulticastOption option = new IPv6MulticastOption (
- IPAddress.Parse ("ff02::1"));
- s.Bind (new IPEndPoint (IPAddress.IPv6Any, 1902));
- s.SetSocketOption (SocketOptionLevel.IPv6, SocketOptionName.AddMembership,
- option);
- s.SetSocketOption (SocketOptionLevel.IPv6, SocketOptionName.DropMembership,
- option);
- }
- }
- [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Object)
- public void SetSocketOption3_DropMembershipIPv6_MulticastOption ()
- {
- if (!Socket.OSSupportsIPv6)
- Assert.Ignore ("IPv6 not enabled.");
- IPAddress mcast_addr = IPAddress.Parse ("ff02::1");
- using (Socket s = new Socket (AddressFamily.InterNetworkV6, SocketType.Dgram, ProtocolType.Udp)) {
- s.Bind (new IPEndPoint (IPAddress.IPv6Any, NetworkHelpers.FindFreePort ()));
- s.SetSocketOption (SocketOptionLevel.IPv6, SocketOptionName.AddMembership,
- new IPv6MulticastOption (mcast_addr));
- try {
- s.SetSocketOption (SocketOptionLevel.IPv6, SocketOptionName.DropMembership,
- new MulticastOption (mcast_addr));
- Assert.Fail ("#1");
- } catch (ArgumentException ex) {
- Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
- Assert.IsNull (ex.InnerException, "#3");
- Assert.IsNotNull (ex.Message, "#4");
- // The specified value is not a valid 'IPv6MulticastOption'
- Assert.IsTrue (ex.Message.IndexOf ("'IPv6MulticastOption'") != -1, "#5:" + ex.Message);
- Assert.AreEqual ("optionValue", ex.ParamName, "#6");
- }
- }
- }
- [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Object)
- [Category ("NotWorking")]
- public void SetSocketOption3_DropMembershipIPv6_Socket_NotBound ()
- {
- IPAddress mcast_addr = IPAddress.Parse ("ff02::1");
- Socket s = new Socket (AddressFamily.InterNetworkV6, SocketType.Dgram, ProtocolType.Udp);
- try {
- s.SetSocketOption (SocketOptionLevel.IPv6, SocketOptionName.DropMembership,
- new IPv6MulticastOption (mcast_addr));
- Assert.Fail ("#1");
- } catch (SocketException ex) {
- // An invalid argument was supplied
- Assert.AreEqual (typeof (SocketException), ex.GetType (), "#2");
- Assert.AreEqual (10022, ex.ErrorCode, "#3");
- Assert.IsNull (ex.InnerException, "#4");
- Assert.IsNotNull (ex.Message, "#5");
- Assert.AreEqual (10022, ex.NativeErrorCode, "#6");
- Assert.AreEqual (SocketError.InvalidArgument, ex.SocketErrorCode, "#7");
- } finally {
- s.Close ();
- }
- }
- [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Object)
- public void SetSocketOption3_OptionValue_Null ()
- {
- using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) {
- try {
- s.SetSocketOption (SocketOptionLevel.Socket,
- SocketOptionName.Linger, (object) null);
- Assert.Fail ("#1");
- } catch (ArgumentNullException ex) {
- Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
- Assert.IsNull (ex.InnerException, "#3");
- Assert.IsNotNull (ex.Message, "#4");
- Assert.AreEqual ("optionValue", ex.ParamName, "#5");
- }
- }
- }
- [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Object)
- public void SetSocketOption3_Socket_Closed ()
- {
- Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
- s.Close ();
- try {
- s.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.Linger,
- new LingerOption (false, 0));
- Assert.Fail ("#1");
- } catch (ObjectDisposedException ex) {
- // Cannot access a disposed object
- Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
- Assert.IsNull (ex.InnerException, "#3");
- Assert.IsNotNull (ex.Message, "#4");
- Assert.AreEqual (typeof (Socket).FullName, ex.ObjectName, "#5");
- }
- }
- [Test]
- public void SetSocketOption_MulticastInterfaceIndex_Any ()
- {
- IPAddress ip = IPAddress.Parse ("239.255.255.250");
- int index = 0;
- using (Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp))
- {
- s.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.MulticastInterface, IPAddress.HostToNetworkOrder(index));
- s.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, new MulticastOption(ip, index));
- }
- }
- [Test]
- public void SetSocketOption_MulticastInterfaceIndex_Loopback ()
- {
- IPAddress ip = IPAddress.Parse ("239.255.255.250");
- int index = 1;
- using (Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp))
- {
- s.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.MulticastInterface, IPAddress.HostToNetworkOrder(index));
- s.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, new MulticastOption(ip, index));
- }
- }
- [Test]
- public void SetSocketOption_MulticastInterfaceIndex_Invalid ()
- {
- IPAddress ip = IPAddress.Parse ("239.255.255.250");
- int index = 31415;
- using (Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp))
- {
- try
- {
- s.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.MulticastInterface, IPAddress.HostToNetworkOrder(index));
- Assert.Fail ("#1");
- }
- catch
- {}
- try
- {
- s.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, new MulticastOption(ip, index));
- Assert.Fail ("#2");
- }
- catch
- {}
- }
- }
- [Test]
- public void Shutdown_NoConnect ()
- {
- Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
- s.Bind (new IPEndPoint (IPAddress.Loopback, NetworkHelpers.FindFreePort ()));
- s.Listen (1);
- try {
- s.Shutdown (SocketShutdown.Both);
- Assert.Fail ("#1");
- } catch (SocketException exc) {
- Assert.AreEqual (10057, exc.ErrorCode, "#2");
- } finally {
- s.Close ();
- }
- }
- [Test]
- [ExpectedException (typeof (NullReferenceException))]
- public void ReceiveAsync_Null ()
- {
- using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) {
- s.ReceiveAsync (null);
- }
- }
- [Test]
- [ExpectedException (typeof (NullReferenceException))]
- public void ReceiveAsync_Default ()
- {
- using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) {
- SocketAsyncEventArgs saea = new SocketAsyncEventArgs ();
- s.ReceiveAsync (saea);
- }
- }
- [Test]
- [ExpectedException (typeof (NullReferenceException))]
- public void ReceiveAsync_NullBuffer ()
- {
- using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) {
- SocketAsyncEventArgs saea = new SocketAsyncEventArgs ();
- saea.SetBuffer (null, 0, 0);
- s.ReceiveAsync (null);
- }
- }
- [Test]
- [ExpectedException (typeof (ObjectDisposedException))]
- public void ReceiveAsync_ClosedSocket ()
- {
- Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
- s.Close ();
- s.ReceiveAsync (null);
- }
- [Test]
- [ExpectedException (typeof (NullReferenceException))]
- public void SendAsync_Null ()
- {
- using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) {
- s.SendAsync (null);
- }
- }
- [Test]
- [ExpectedException (typeof (NullReferenceException))]
- public void SendAsync_Default ()
- {
- using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) {
- SocketAsyncEventArgs saea = new SocketAsyncEventArgs ();
- s.SendAsync (saea);
- }
- }
- [Test]
- [ExpectedException (typeof (NullReferenceException))]
- public void SendAsync_NullBuffer ()
- {
- using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) {
- SocketAsyncEventArgs saea = new SocketAsyncEventArgs ();
- saea.SetBuffer (null, 0, 0);
- s.SendAsync (null);
- }
- }
- [Test]
- [ExpectedException (typeof (ObjectDisposedException))]
- public void SendAsync_ClosedSocket ()
- {
- Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
- s.Close ();
- s.SendAsync (null);
- }
-
- [Test]
- public void SendAsyncFile ()
- {
- Socket serverSocket = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
- serverSocket.Bind (new IPEndPoint (IPAddress.Loopback, 0));
- serverSocket.Listen (1);
- var mReceived = new ManualResetEvent (false);
- serverSocket.BeginAccept (AsyncCall => {
- byte[] bytes = new byte [1024];
- Socket listener = (Socket)AsyncCall.AsyncState;
- Socket client = listener.EndAccept (AsyncCall);
- client.Receive (bytes, bytes.Length, 0);
- client.Close ();
- mReceived.Set ();
- }, serverSocket);
-
- Socket clientSocket = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
- clientSocket.Connect (serverSocket.LocalEndPoint);
- clientSocket.NoDelay = true;
-
- // Initialize buffer used to create testing file
- var buffer = new byte [1024];
- for (int i = 0; i < 1024; ++i)
- buffer [i] = (byte) (i % 256);
-
- string temp = Path.GetTempFileName ();
- try {
- // Testing file creation
- using (StreamWriter sw = new StreamWriter (temp)) {
- sw.Write (buffer);
- }
- var mSent = new ManualResetEvent (false);
- // Async Send File to server
- clientSocket.BeginSendFile(temp, (ar) => {
- Socket client = (Socket) ar.AsyncState;
- client.EndSendFile (ar);
- mSent.Set ();
- }, clientSocket);
- if (!mSent.WaitOne (1500))
- throw new TimeoutException ();
- if (!mReceived.WaitOne (1500))
- throw new TimeoutException ();
- } finally {
- if (File.Exists (temp))
- File.Delete (temp);
-
- clientSocket.Close ();
- serverSocket.Close ();
- }
- }
-
- [Test]
- public void ConnectToIPV4EndPointUsingDualModelSocket () {
- /*
- * IPv6 DualMode sockets are defaults in Mono. Explicitly specify that
- * anyways in this test to make it more interoparable with .NET where
- * IPv6 and DualMode needs to be specified.
- */
- using (var server = new Socket (AddressFamily.InterNetworkV6, SocketType.Stream, ProtocolType.Tcp)) {
- var host = new IPEndPoint (IPAddress.Loopback, NetworkHelpers.FindFreePort ());
- server.DualMode = true;
- server.Bind (host);
- /*
- * Nothing to Accept the connect - we need a backlog to make sure we don't get
- Connection refused.
- */
- server.Listen (3);
-
- var ep = server.LocalEndPoint as IPEndPoint;
- var client = new Socket (AddressFamily.InterNetworkV6, SocketType.Stream, ProtocolType.Tcp);
- client.DualMode = true;
- client.Connect (ep);
- client.Disconnect (false);
- client.Close ();
- client = new Socket (AddressFamily.InterNetworkV6, SocketType.Stream, ProtocolType.Tcp);
- client.DualMode = true;
- client.Connect (IPAddress.Loopback, ep.Port);
- client.Disconnect (false);
- client.Close ();
- client = new Socket (AddressFamily.InterNetworkV6, SocketType.Stream, ProtocolType.Tcp);
- client.DualMode = true;
- client.Connect (new [] { IPAddress.Loopback }, ep.Port);
- client.Disconnect (false);
- client.Close ();
- }
- }
-
- [Test]
- public void BeginConnectToIPV4EndPointUsingDualModelSocket () {
- /*
- * IPv6 DualMode sockets are defaults in Mono. Explicitly specify that
- * anyways in this test to make it more interoparable with .NET where
- * IPv6 and DualMode needs to be specified.
- */
- using (var server = new Socket (AddressFamily.InterNetworkV6, SocketType.Stream, ProtocolType.Tcp))
- {
- var host = new IPEndPoint (IPAddress.Loopback, NetworkHelpers.FindFreePort ());
- server.DualMode = true;
- server.Bind (host);
- server.Listen (10);
-
- var ep = server.LocalEndPoint as IPEndPoint;
- BCCalledBack.Reset ();
- var client = new Socket (AddressFamily.InterNetworkV6, SocketType.Stream, ProtocolType.Tcp);
- client.DualMode = true;
- var ar1 = client.BeginConnect (ep, BCCallback, client);
- Assert.IsTrue (BCCalledBack.WaitOne (10000), "#1");
- client.Disconnect (false);
- client.Close ();
- BCCalledBack.Reset ();
- client = new Socket (AddressFamily.InterNetworkV6, SocketType.Stream, ProtocolType.Tcp);
- client.DualMode = true;
- var ar2 = client.BeginConnect (IPAddress.Loopback, ep.Port, BCCallback, client);
- Assert.IsTrue (BCCalledBack.WaitOne (10000), "#2");
- client.Disconnect (false);
- client.Close ();
- BCCalledBack.Reset ();
- client = new Socket (AddressFamily.InterNetworkV6, SocketType.Stream, ProtocolType.Tcp);
- client.DualMode = true;
- var ar3 = client.BeginConnect (new [] {IPAddress.Loopback}, ep.Port, BCCallback, client);
- Assert.IsTrue (BCCalledBack.WaitOne (10000), "#2");
- client.Disconnect (false);
- client.Close();
- }
- }
- [Test]
- public void UdpMulticasTimeToLive ()
- {
- /* see https://bugzilla.xamarin.com/show_bug.cgi?id=36941 */
- using (Socket socket = new Socket (AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp)) {
- IPEndPoint end_point = new IPEndPoint (IPAddress.Any, NetworkHelpers.FindFreePort ());
- socket.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, 1);
- socket.Bind (end_point);
- socket.SetSocketOption (SocketOptionLevel.IP, SocketOptionName.MulticastTimeToLive, 19);
- }
- }
- [Test] // Covers 41616
- public void ConnectAsyncUnhandledEx ()
- {
- var mre = new ManualResetEvent (false);
- var endPoint = new IPEndPoint(0,0);
- var socket = new Socket(endPoint.AddressFamily, SocketType.Stream, ProtocolType.Unspecified);
- var socketArgs = new SocketAsyncEventArgs();
- socketArgs.RemoteEndPoint = endPoint;
- socketArgs.Completed += (sender, e) => mre.Set ();
- socket.ConnectAsync (socketArgs);
- Assert.IsTrue (mre.WaitOne (1000), "ConnectedAsync timeout");
- }
- }
- }
|