SocketTest.cs 62 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679
  1. // System.Net.Sockets.SocketTest.cs
  2. //
  3. // Authors:
  4. // Brad Fitzpatrick ([email protected])
  5. // Gonzalo Paniagua Javier ([email protected])
  6. //
  7. // (C) Copyright 2003 Brad Fitzpatrick
  8. // Copyright (c) 2005 Novell, Inc. (http://www.novell.com)
  9. //
  10. using System;
  11. using System.Collections;
  12. using System.Threading;
  13. using System.Net;
  14. using System.Net.Sockets;
  15. using NUnit.Framework;
  16. namespace MonoTests.System.Net.Sockets
  17. {
  18. [TestFixture]
  19. public class SocketTest
  20. {
  21. // note: also used in SocketCas tests
  22. public const string BogusAddress = "192.168.244.244";
  23. public const int BogusPort = 23483;
  24. [Test]
  25. public void ConnectIPAddressAny ()
  26. {
  27. IPEndPoint ep = new IPEndPoint (IPAddress.Any, 0);
  28. try {
  29. using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp)) {
  30. s.Connect (ep);
  31. s.Close ();
  32. }
  33. Assert.Fail ("#1");
  34. } catch (SocketException ex) {
  35. Assert.AreEqual (10049, ex.ErrorCode, "#2");
  36. }
  37. try {
  38. using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) {
  39. s.Connect (ep);
  40. s.Close ();
  41. }
  42. Assert.Fail ("#3");
  43. } catch (SocketException ex) {
  44. Assert.AreEqual (10049, ex.ErrorCode, "#4");
  45. }
  46. }
  47. [Test]
  48. [Ignore ("Bug #75158")]
  49. public void IncompatibleAddress ()
  50. {
  51. IPEndPoint epIPv6 = new IPEndPoint (IPAddress.IPv6Any,
  52. 0);
  53. try {
  54. using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP)) {
  55. s.Connect (epIPv6);
  56. s.Close ();
  57. }
  58. Assert.Fail ("#1");
  59. } catch (SocketException ex) {
  60. #if !NET_2_0
  61. // invalid argument
  62. int expectedError = 10022;
  63. #else
  64. // address incompatible with protocol
  65. int expectedError = 10047;
  66. #endif
  67. Assert.AreEqual (expectedError, ex.ErrorCode,
  68. "#2");
  69. }
  70. }
  71. [Test]
  72. [Category ("InetAccess")]
  73. public void EndConnect ()
  74. {
  75. IPAddress ipOne = IPAddress.Parse (BogusAddress);
  76. IPEndPoint ipEP = new IPEndPoint (ipOne, BogusPort);
  77. Socket sock = new Socket (ipEP.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
  78. IAsyncResult ar = sock.BeginConnect (ipEP, null, null);
  79. bool gotException = false;
  80. try {
  81. sock.EndConnect (ar); // should raise an exception because connect was bogus
  82. } catch {
  83. gotException = true;
  84. }
  85. Assertion.AssertEquals ("A01", gotException, true);
  86. }
  87. [Test]
  88. [ExpectedException (typeof (ArgumentNullException))]
  89. public void SelectEmpty ()
  90. {
  91. ArrayList list = new ArrayList ();
  92. Socket.Select (list, list, list, 1000);
  93. }
  94. private bool BlockingConnect (bool block)
  95. {
  96. IPEndPoint ep = new IPEndPoint(IPAddress.Loopback, 1234);
  97. Socket server = new Socket(AddressFamily.InterNetwork,
  98. SocketType.Stream,
  99. ProtocolType.Tcp);
  100. server.Bind(ep);
  101. server.Blocking=block;
  102. server.Listen(0);
  103. Socket conn = new Socket (AddressFamily.InterNetwork,
  104. SocketType.Stream,
  105. ProtocolType.Tcp);
  106. conn.Connect (ep);
  107. Socket client = server.Accept();
  108. bool client_block = client.Blocking;
  109. client.Close();
  110. conn.Close();
  111. server.Close();
  112. return(client_block);
  113. }
  114. [Test]
  115. public void AcceptBlockingStatus()
  116. {
  117. bool block;
  118. block = BlockingConnect(true);
  119. Assertion.AssertEquals ("BlockingStatus01",
  120. block, true);
  121. block = BlockingConnect(false);
  122. Assertion.AssertEquals ("BlockingStatus02",
  123. block, false);
  124. }
  125. static bool CFAConnected = false;
  126. static ManualResetEvent CFACalledBack;
  127. private static void CFACallback (IAsyncResult asyncResult)
  128. {
  129. Socket sock = (Socket)asyncResult.AsyncState;
  130. CFAConnected = sock.Connected;
  131. if (sock.Connected) {
  132. sock.EndConnect (asyncResult);
  133. }
  134. CFACalledBack.Set ();
  135. }
  136. [Test]
  137. public void ConnectFailAsync ()
  138. {
  139. Socket sock = new Socket (AddressFamily.InterNetwork,
  140. SocketType.Stream,
  141. ProtocolType.Tcp);
  142. sock.Blocking = false;
  143. CFACalledBack = new ManualResetEvent (false);
  144. CFACalledBack.Reset ();
  145. /* Need a port that is not being used for
  146. * anything...
  147. */
  148. sock.BeginConnect (new IPEndPoint (IPAddress.Loopback,
  149. 114),
  150. new AsyncCallback (CFACallback),
  151. sock);
  152. CFACalledBack.WaitOne ();
  153. Assertion.AssertEquals ("ConnectFail", CFAConnected,
  154. false);
  155. }
  156. #if !TARGET_JVM
  157. [Test]
  158. #if !NET_2_0
  159. [ExpectedException (typeof (ArgumentException))]
  160. #endif
  161. public void SetSocketOptionBoolean ()
  162. {
  163. IPEndPoint ep = new IPEndPoint (IPAddress.Loopback, 1);
  164. Socket sock = new Socket (ep.Address.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
  165. try {
  166. sock.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.KeepAlive, true);
  167. } finally {
  168. sock.Close ();
  169. }
  170. }
  171. #endif
  172. [Test]
  173. public void TestSelect1 ()
  174. {
  175. Socket srv = CreateServer ();
  176. ClientSocket clnt = new ClientSocket (srv.LocalEndPoint);
  177. Thread th = new Thread (new ThreadStart (clnt.ConnectSleepClose));
  178. Socket acc = null;
  179. try {
  180. th.Start ();
  181. acc = srv.Accept ();
  182. clnt.Write ();
  183. ArrayList list = new ArrayList ();
  184. ArrayList empty = new ArrayList ();
  185. list.Add (acc);
  186. Socket.Select (list, empty, empty, 100);
  187. Assertion.AssertEquals ("#01", 0, empty.Count);
  188. Assertion.AssertEquals ("#02", 1, list.Count);
  189. Socket.Select (empty, list, empty, 100);
  190. Assertion.AssertEquals ("#03", 0, empty.Count);
  191. Assertion.AssertEquals ("#04", 1, list.Count);
  192. Socket.Select (list, empty, empty, -1);
  193. Assertion.AssertEquals ("#05", 0, empty.Count);
  194. Assertion.AssertEquals ("#06", 1, list.Count);
  195. } finally {
  196. if (acc != null)
  197. acc.Close ();
  198. srv.Close ();
  199. }
  200. }
  201. static Socket CreateServer ()
  202. {
  203. Socket sock = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  204. sock.Bind (new IPEndPoint (IPAddress.Loopback, 0));
  205. sock.Listen (1);
  206. return sock;
  207. }
  208. class ClientSocket {
  209. Socket sock;
  210. EndPoint ep;
  211. public ClientSocket (EndPoint ep)
  212. {
  213. this.ep = ep;
  214. sock = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  215. }
  216. public void ConnectSleepClose ()
  217. {
  218. sock.Connect (ep);
  219. Thread.Sleep (2000);
  220. sock.Close ();
  221. }
  222. public void Write ()
  223. {
  224. byte [] b = new byte [10];
  225. sock.Send (b);
  226. }
  227. }
  228. byte[] buf = new byte[100];
  229. [Test]
  230. [ExpectedException (typeof (ObjectDisposedException))]
  231. public void Disposed1 ()
  232. {
  233. Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
  234. EndPoint ep = new IPEndPoint (IPAddress.Any, 31337);
  235. s.Close();
  236. s.ReceiveFrom (buf, ref ep);
  237. }
  238. [Test]
  239. [ExpectedException (typeof (ObjectDisposedException))]
  240. public void Disposed2 ()
  241. {
  242. Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
  243. s.Close();
  244. s.Blocking = true;
  245. }
  246. [Test]
  247. [ExpectedException (typeof (ObjectDisposedException))]
  248. public void Disposed3 ()
  249. {
  250. Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
  251. s.Close();
  252. s.GetSocketOption (0, 0);
  253. }
  254. [Test]
  255. [ExpectedException (typeof (ObjectDisposedException))]
  256. public void Disposed4 ()
  257. {
  258. Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
  259. s.Close();
  260. s.GetSocketOption (0, 0, null);
  261. }
  262. [Test]
  263. [ExpectedException (typeof (ObjectDisposedException))]
  264. public void Disposed5 ()
  265. {
  266. Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
  267. s.Close();
  268. s.GetSocketOption (0, 0, 0);
  269. }
  270. [Test]
  271. [ExpectedException (typeof (ObjectDisposedException))]
  272. public void Disposed6 ()
  273. {
  274. Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
  275. s.Close();
  276. s.Listen (5);
  277. }
  278. [Test]
  279. [ExpectedException (typeof (ObjectDisposedException))]
  280. public void Disposed7 ()
  281. {
  282. Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
  283. s.Close();
  284. s.Poll (100, 0);
  285. }
  286. [Test]
  287. [ExpectedException (typeof (ObjectDisposedException))]
  288. public void Disposed8 ()
  289. {
  290. Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
  291. s.Close();
  292. s.Receive (buf);
  293. }
  294. [Test]
  295. [ExpectedException (typeof (ObjectDisposedException))]
  296. public void Disposed9 ()
  297. {
  298. Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
  299. s.Close();
  300. s.Receive (buf, 0);
  301. }
  302. [Test]
  303. [ExpectedException (typeof (ObjectDisposedException))]
  304. public void Disposed10 ()
  305. {
  306. Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
  307. s.Close();
  308. s.Receive (buf, 10, 0);
  309. }
  310. [Test]
  311. [ExpectedException (typeof (ObjectDisposedException))]
  312. public void Disposed11 ()
  313. {
  314. Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
  315. EndPoint ep = new IPEndPoint (IPAddress.Any, 31337);
  316. s.Close();
  317. s.Receive (buf, 0, 10, 0);
  318. }
  319. [Test]
  320. [ExpectedException (typeof (ObjectDisposedException))]
  321. public void Disposed12 ()
  322. {
  323. Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
  324. EndPoint ep = new IPEndPoint (IPAddress.Any, 31337);
  325. s.Close();
  326. s.ReceiveFrom (buf, 0, ref ep);
  327. }
  328. [Test]
  329. [ExpectedException (typeof (ObjectDisposedException))]
  330. public void Disposed13 ()
  331. {
  332. Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
  333. EndPoint ep = new IPEndPoint (IPAddress.Any, 31337);
  334. s.Close();
  335. s.ReceiveFrom (buf, 10, 0, ref ep);
  336. }
  337. [Test]
  338. [ExpectedException (typeof (ObjectDisposedException))]
  339. public void Disposed14 ()
  340. {
  341. Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
  342. EndPoint ep = new IPEndPoint (IPAddress.Any, 31337);
  343. s.Close();
  344. s.ReceiveFrom (buf, 0, 10, 0, ref ep);
  345. }
  346. [Test]
  347. [ExpectedException (typeof (ObjectDisposedException))]
  348. public void Disposed15 ()
  349. {
  350. Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
  351. s.Close();
  352. s.Send (buf);
  353. }
  354. [Test]
  355. [ExpectedException (typeof (ObjectDisposedException))]
  356. public void Disposed16 ()
  357. {
  358. Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
  359. s.Close();
  360. s.Send (buf, 0);
  361. }
  362. [Test]
  363. [ExpectedException (typeof (ObjectDisposedException))]
  364. public void Disposed17 ()
  365. {
  366. Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
  367. s.Close();
  368. s.Send (buf, 10, 0);
  369. }
  370. [Test]
  371. [ExpectedException (typeof (ObjectDisposedException))]
  372. public void Disposed18 ()
  373. {
  374. Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
  375. EndPoint ep = new IPEndPoint (IPAddress.Any, 31337);
  376. s.Close();
  377. s.Send (buf, 0, 10, 0);
  378. }
  379. [Test]
  380. [ExpectedException (typeof (ObjectDisposedException))]
  381. public void Disposed19 ()
  382. {
  383. Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
  384. EndPoint ep = new IPEndPoint (IPAddress.Any, 31337);
  385. s.Close();
  386. s.SendTo (buf, 0, ep);
  387. }
  388. [Test]
  389. [ExpectedException (typeof (ObjectDisposedException))]
  390. public void Disposed20 ()
  391. {
  392. Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
  393. EndPoint ep = new IPEndPoint (IPAddress.Any, 31337);
  394. s.Close();
  395. s.SendTo (buf, 10, 0, ep);
  396. }
  397. [Test]
  398. [ExpectedException (typeof (ObjectDisposedException))]
  399. public void Disposed21 ()
  400. {
  401. Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
  402. EndPoint ep = new IPEndPoint (IPAddress.Any, 31337);
  403. s.Close();
  404. s.SendTo (buf, 0, 10, 0, ep);
  405. }
  406. [Test]
  407. [ExpectedException (typeof (ObjectDisposedException))]
  408. public void Disposed22 ()
  409. {
  410. Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
  411. EndPoint ep = new IPEndPoint (IPAddress.Any, 31337);
  412. s.Close();
  413. s.SendTo (buf, ep);
  414. }
  415. [Test]
  416. [ExpectedException (typeof (ObjectDisposedException))]
  417. public void Disposed23 ()
  418. {
  419. Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
  420. EndPoint ep = new IPEndPoint (IPAddress.Any, 31337);
  421. s.Close();
  422. s.Shutdown (0);
  423. }
  424. static ManualResetEvent SocketError_event = new ManualResetEvent (false);
  425. private static void SocketError_callback (IAsyncResult ar)
  426. {
  427. Socket sock = (Socket)ar.AsyncState;
  428. if(sock.Connected) {
  429. sock.EndConnect (ar);
  430. }
  431. SocketError_event.Set ();
  432. }
  433. [Test]
  434. public void SocketError ()
  435. {
  436. Socket sock = new Socket (AddressFamily.InterNetwork,
  437. SocketType.Stream,
  438. ProtocolType.Tcp);
  439. IPEndPoint ep = new IPEndPoint (IPAddress.Loopback,
  440. BogusPort);
  441. SocketError_event.Reset ();
  442. sock.Blocking = false;
  443. sock.BeginConnect (ep, SocketError_callback,
  444. sock);
  445. if (SocketError_event.WaitOne (2000, false) == false) {
  446. Assert.Fail ("SocketError wait timed out");
  447. }
  448. Assertion.AssertEquals ("SocketError #1", false,
  449. sock.Connected);
  450. int error;
  451. error = (int)sock.GetSocketOption (SocketOptionLevel.Socket, SocketOptionName.Error);
  452. Assertion.AssertEquals ("SocketError #2", 10061,
  453. error);
  454. error = (int)sock.GetSocketOption (SocketOptionLevel.Socket, SocketOptionName.Error);
  455. Assertion.AssertEquals ("SocketError #3", 10061,
  456. error);
  457. sock.Close ();
  458. }
  459. #if NET_2_0
  460. [Test]
  461. public void SocketInformationCtor ()
  462. {
  463. }
  464. [Test]
  465. public void DontFragmentDefaultTcp ()
  466. {
  467. Socket sock = new Socket (AddressFamily.InterNetwork,
  468. SocketType.Stream,
  469. ProtocolType.Tcp);
  470. Assertion.AssertEquals ("DontFragmentDefaultTcp",
  471. false, sock.DontFragment);
  472. sock.Close ();
  473. }
  474. [Test]
  475. public void DontFragmentChangeTcp ()
  476. {
  477. Socket sock = new Socket (AddressFamily.InterNetwork,
  478. SocketType.Stream,
  479. ProtocolType.Tcp);
  480. sock.DontFragment = true;
  481. Assertion.AssertEquals ("DontFragmentChangeTcp",
  482. true, sock.DontFragment);
  483. sock.Close ();
  484. }
  485. [Test]
  486. public void DontFragmentDefaultUdp ()
  487. {
  488. Socket sock = new Socket (AddressFamily.InterNetwork,
  489. SocketType.Dgram,
  490. ProtocolType.Udp);
  491. Assertion.AssertEquals ("DontFragmentDefaultUdp",
  492. false, sock.DontFragment);
  493. sock.Close ();
  494. }
  495. [Test]
  496. public void DontFragmentChangeUdp ()
  497. {
  498. Socket sock = new Socket (AddressFamily.InterNetwork,
  499. SocketType.Dgram,
  500. ProtocolType.Udp);
  501. sock.DontFragment = true;
  502. Assertion.AssertEquals ("DontFragmentChangeUdp",
  503. true, sock.DontFragment);
  504. sock.Close ();
  505. }
  506. [Test]
  507. [ExpectedException (typeof(ObjectDisposedException))]
  508. public void DontFragmentClosed ()
  509. {
  510. Socket sock = new Socket (AddressFamily.InterNetwork,
  511. SocketType.Stream,
  512. ProtocolType.Tcp);
  513. sock.Close ();
  514. bool val = sock.DontFragment;
  515. }
  516. [Test]
  517. [Category ("NotWorking")] // Need to pick a non-IP AddressFamily that "works" on both mono and ms, this one only works on ms
  518. public void DontFragment ()
  519. {
  520. Socket sock = new Socket (AddressFamily.NetBios,
  521. SocketType.Seqpacket,
  522. ProtocolType.Unspecified);
  523. try {
  524. sock.DontFragment = true;
  525. Assert.Fail ("DontFragment #1");
  526. } catch (NotSupportedException) {
  527. } catch {
  528. Assert.Fail ("DontFragment #2");
  529. } finally {
  530. sock.Close ();
  531. }
  532. }
  533. [Test]
  534. public void EnableBroadcastDefaultTcp ()
  535. {
  536. Socket sock = new Socket (AddressFamily.InterNetwork,
  537. SocketType.Stream,
  538. ProtocolType.Tcp);
  539. try {
  540. bool value = sock.EnableBroadcast;
  541. Assert.Fail ("EnableBroadcastDefaultTcp #1");
  542. } catch (SocketException ex) {
  543. Assert.AreEqual (10042, ex.ErrorCode, "EnableBroadcastDefaultTcp #2");
  544. } catch {
  545. Assert.Fail ("EnableBroadcastDefaultTcp #2");
  546. } finally {
  547. sock.Close ();
  548. }
  549. }
  550. [Test]
  551. public void EnableBroadcastDefaultUdp ()
  552. {
  553. Socket sock = new Socket (AddressFamily.InterNetwork,
  554. SocketType.Dgram,
  555. ProtocolType.Udp);
  556. Assertion.AssertEquals ("EnableBroadcastDefaultUdp",
  557. false, sock.EnableBroadcast);
  558. sock.Close ();
  559. }
  560. [Test]
  561. public void EnableBroadcastChangeTcp ()
  562. {
  563. Socket sock = new Socket (AddressFamily.InterNetwork,
  564. SocketType.Stream,
  565. ProtocolType.Tcp);
  566. try {
  567. sock.EnableBroadcast = true;
  568. Assert.Fail ("EnableBroadcastChangeTcp #1");
  569. } catch (SocketException ex) {
  570. Assert.AreEqual (10042, ex.ErrorCode, "EnableBroadcastChangeTcp #2");
  571. } catch {
  572. Assert.Fail ("EnableBroadcastChangeTcp #2");
  573. } finally {
  574. sock.Close ();
  575. }
  576. }
  577. [Test]
  578. public void EnableBroadcastChangeUdp ()
  579. {
  580. Socket sock = new Socket (AddressFamily.InterNetwork,
  581. SocketType.Dgram,
  582. ProtocolType.Udp);
  583. sock.EnableBroadcast = true;
  584. Assertion.AssertEquals ("EnableBroadcastChangeUdp",
  585. true, sock.EnableBroadcast);
  586. sock.Close ();
  587. }
  588. [Test]
  589. [ExpectedException (typeof(ObjectDisposedException))]
  590. public void EnableBroadcastClosed ()
  591. {
  592. Socket sock = new Socket (AddressFamily.InterNetwork,
  593. SocketType.Dgram,
  594. ProtocolType.Udp);
  595. sock.Close ();
  596. bool val = sock.EnableBroadcast;
  597. }
  598. /* Can't test the default for ExclusiveAddressUse as
  599. * it's different on different versions and service
  600. * packs of windows
  601. */
  602. [Test]
  603. [Category ("NotWorking")] // Not supported on Linux
  604. public void ExclusiveAddressUseUnbound ()
  605. {
  606. Socket sock = new Socket (AddressFamily.InterNetwork,
  607. SocketType.Stream,
  608. ProtocolType.Tcp);
  609. sock.ExclusiveAddressUse = true;
  610. Assertion.AssertEquals ("ExclusiveAddressUseUnbound",
  611. true,
  612. sock.ExclusiveAddressUse);
  613. sock.Close ();
  614. }
  615. [Test]
  616. [ExpectedException (typeof(InvalidOperationException))]
  617. [Category ("NotWorking")] // Not supported on Linux
  618. public void ExclusiveAddressUseBound ()
  619. {
  620. Socket sock = new Socket (AddressFamily.InterNetwork,
  621. SocketType.Stream,
  622. ProtocolType.Tcp);
  623. sock.Bind (new IPEndPoint (IPAddress.Any, 1235));
  624. sock.ExclusiveAddressUse = true;
  625. sock.Close ();
  626. }
  627. [Test]
  628. [ExpectedException (typeof(ObjectDisposedException))]
  629. public void ExclusiveAddressUseClosed ()
  630. {
  631. Socket sock = new Socket (AddressFamily.InterNetwork,
  632. SocketType.Stream,
  633. ProtocolType.Tcp);
  634. sock.Close ();
  635. bool val = sock.ExclusiveAddressUse;
  636. }
  637. [Test]
  638. public void IsBoundTcp ()
  639. {
  640. Socket sock = new Socket (AddressFamily.InterNetwork,
  641. SocketType.Stream,
  642. ProtocolType.Tcp);
  643. IPEndPoint ep = new IPEndPoint (IPAddress.Loopback,
  644. BogusPort);
  645. Assertion.AssertEquals ("IsBoundTcp #1", false,
  646. sock.IsBound);
  647. sock.Bind (ep);
  648. Assertion.AssertEquals ("IsBoundTcp #2", true,
  649. sock.IsBound);
  650. sock.Listen (1);
  651. Socket sock2 = new Socket (AddressFamily.InterNetwork,
  652. SocketType.Stream,
  653. ProtocolType.Tcp);
  654. Assertion.AssertEquals ("IsBoundTcp #3", false,
  655. sock2.IsBound);
  656. sock2.Connect (ep);
  657. Assertion.AssertEquals ("IsBoundTcp #4", true,
  658. sock2.IsBound);
  659. sock2.Close ();
  660. Assertion.AssertEquals ("IsBoundTcp #5", true,
  661. sock2.IsBound);
  662. sock.Close ();
  663. Assertion.AssertEquals ("IsBoundTcp #6", true,
  664. sock.IsBound);
  665. }
  666. [Test]
  667. public void IsBoundUdp ()
  668. {
  669. Socket sock = new Socket (AddressFamily.InterNetwork,
  670. SocketType.Dgram,
  671. ProtocolType.Udp);
  672. IPEndPoint ep = new IPEndPoint (IPAddress.Loopback,
  673. BogusPort);
  674. Assertion.AssertEquals ("IsBoundUdp #1", false,
  675. sock.IsBound);
  676. sock.Bind (ep);
  677. Assertion.AssertEquals ("IsBoundUdp #2", true,
  678. sock.IsBound);
  679. sock.Close ();
  680. Assertion.AssertEquals ("IsBoundUdp #3", true,
  681. sock.IsBound);
  682. sock = new Socket (AddressFamily.InterNetwork,
  683. SocketType.Dgram,
  684. ProtocolType.Udp);
  685. Assertion.AssertEquals ("IsBoundUdp #4", false,
  686. sock.IsBound);
  687. sock.Connect (ep);
  688. Assertion.AssertEquals ("IsBoundUdp #5", true,
  689. sock.IsBound);
  690. sock.Close ();
  691. Assertion.AssertEquals ("IsBoundUdp #6", true,
  692. sock.IsBound);
  693. }
  694. [Test]
  695. /* Should not throw an exception */
  696. public void IsBoundClosed ()
  697. {
  698. Socket sock = new Socket (AddressFamily.InterNetwork,
  699. SocketType.Stream,
  700. ProtocolType.Tcp);
  701. sock.Close ();
  702. bool val = sock.IsBound;
  703. }
  704. /* Nothing much to test for LingerState */
  705. [Test]
  706. public void MulticastLoopbackDefaultTcp ()
  707. {
  708. Socket sock = new Socket (AddressFamily.InterNetwork,
  709. SocketType.Stream,
  710. ProtocolType.Tcp);
  711. try {
  712. bool value = sock.MulticastLoopback;
  713. Assert.Fail ("MulticastLoopbackDefaultTcp #1");
  714. } catch (SocketException ex) {
  715. Assert.AreEqual (10042, ex.ErrorCode, "MulticastLoopbackDefaultTcp #2");
  716. } catch {
  717. Assert.Fail ("MulticastLoopbackDefaultTcp #2");
  718. } finally {
  719. sock.Close ();
  720. }
  721. }
  722. [Test]
  723. public void MulticastLoopbackChangeTcp ()
  724. {
  725. Socket sock = new Socket (AddressFamily.InterNetwork,
  726. SocketType.Stream,
  727. ProtocolType.Tcp);
  728. try {
  729. sock.MulticastLoopback = false;
  730. Assert.Fail ("MulticastLoopbackChangeTcp #1");
  731. } catch (SocketException ex) {
  732. Assert.AreEqual (10042, ex.ErrorCode, "MulticastLoopbackChangeTcp #2");
  733. } catch {
  734. Assert.Fail ("MulticastLoopbackChangeTcp #2");
  735. } finally {
  736. sock.Close ();
  737. }
  738. }
  739. [Test]
  740. public void MulticastLoopbackDefaultUdp ()
  741. {
  742. Socket sock = new Socket (AddressFamily.InterNetwork,
  743. SocketType.Dgram,
  744. ProtocolType.Udp);
  745. Assertion.AssertEquals ("MulticastLoopbackDefaultUdp",
  746. true, sock.MulticastLoopback);
  747. sock.Close ();
  748. }
  749. [Test]
  750. public void MulticastLoopbackChangeUdp ()
  751. {
  752. Socket sock = new Socket (AddressFamily.InterNetwork,
  753. SocketType.Dgram,
  754. ProtocolType.Udp);
  755. sock.MulticastLoopback = false;
  756. Assertion.AssertEquals ("MulticastLoopbackChangeUdp",
  757. false, sock.MulticastLoopback);
  758. sock.Close ();
  759. }
  760. [Test]
  761. [ExpectedException (typeof(ObjectDisposedException))]
  762. public void MulticastLoopbackClosed ()
  763. {
  764. Socket sock = new Socket (AddressFamily.InterNetwork,
  765. SocketType.Stream,
  766. ProtocolType.Tcp);
  767. sock.Close ();
  768. bool val = sock.MulticastLoopback;
  769. }
  770. /* OSSupportsIPv6 depends on the environment */
  771. [Test]
  772. public void ReceiveBufferSizeDefault ()
  773. {
  774. Socket sock = new Socket (AddressFamily.InterNetwork,
  775. SocketType.Stream,
  776. ProtocolType.Tcp);
  777. Assertion.AssertEquals ("ReceiveBufferSizeDefault",
  778. 8192, sock.ReceiveBufferSize);
  779. sock.Close ();
  780. }
  781. [Test]
  782. public void ReceiveBufferSizeDefaultUdp ()
  783. {
  784. Socket sock = new Socket (AddressFamily.InterNetwork,
  785. SocketType.Dgram,
  786. ProtocolType.Udp);
  787. Assertion.AssertEquals ("ReceiveBufferSizeDefaultUdp",
  788. 8192, sock.ReceiveBufferSize);
  789. sock.Close ();
  790. }
  791. [Test]
  792. public void ReceiveBufferSizeChange ()
  793. {
  794. Socket sock = new Socket (AddressFamily.InterNetwork,
  795. SocketType.Stream,
  796. ProtocolType.Tcp);
  797. sock.ReceiveBufferSize = 16384;
  798. Assertion.AssertEquals ("ReceiveBufferSizeChange",
  799. 16384, sock.ReceiveBufferSize);
  800. sock.Close ();
  801. }
  802. [Test]
  803. [ExpectedException (typeof(ObjectDisposedException))]
  804. public void ReceiveBufferSizeClosed ()
  805. {
  806. Socket sock = new Socket (AddressFamily.InterNetwork,
  807. SocketType.Stream,
  808. ProtocolType.Tcp);
  809. sock.Close ();
  810. int val = sock.ReceiveBufferSize;
  811. }
  812. [Test]
  813. public void SendBufferSizeDefault ()
  814. {
  815. Socket sock = new Socket (AddressFamily.InterNetwork,
  816. SocketType.Stream,
  817. ProtocolType.Tcp);
  818. Assertion.AssertEquals ("SendBufferSizeDefault",
  819. 8192, sock.SendBufferSize);
  820. sock.Close ();
  821. }
  822. [Test]
  823. public void SendBufferSizeDefaultUdp ()
  824. {
  825. Socket sock = new Socket (AddressFamily.InterNetwork,
  826. SocketType.Dgram,
  827. ProtocolType.Udp);
  828. Assertion.AssertEquals ("SendBufferSizeDefaultUdp",
  829. 8192, sock.SendBufferSize);
  830. sock.Close ();
  831. }
  832. [Test]
  833. public void SendBufferSizeChange ()
  834. {
  835. Socket sock = new Socket (AddressFamily.InterNetwork,
  836. SocketType.Stream,
  837. ProtocolType.Tcp);
  838. sock.SendBufferSize = 16384;
  839. Assertion.AssertEquals ("SendBufferSizeChange",
  840. 16384, sock.SendBufferSize);
  841. sock.Close ();
  842. }
  843. [Test]
  844. [ExpectedException (typeof(ObjectDisposedException))]
  845. public void SendBufferSizeClosed ()
  846. {
  847. Socket sock = new Socket (AddressFamily.InterNetwork,
  848. SocketType.Stream,
  849. ProtocolType.Tcp);
  850. sock.Close ();
  851. int val = sock.SendBufferSize;
  852. }
  853. /* No test for TTL default as it's platform dependent */
  854. [Test]
  855. public void TtlChange ()
  856. {
  857. Socket sock = new Socket (AddressFamily.InterNetwork,
  858. SocketType.Stream,
  859. ProtocolType.Tcp);
  860. sock.Ttl = 255;
  861. Assertion.AssertEquals ("TtlChange", 255, sock.Ttl);
  862. sock.Close ();
  863. }
  864. [Test]
  865. public void TtlChangeOverflow ()
  866. {
  867. Socket sock = new Socket (AddressFamily.InterNetwork,
  868. SocketType.Stream,
  869. ProtocolType.Tcp);
  870. try {
  871. sock.Ttl = 256;
  872. Assert.Fail ("TtlChangeOverflow #1");
  873. } catch (SocketException ex) {
  874. Assert.AreEqual (10022, ex.ErrorCode,
  875. "TtlChangeOverflow #2");
  876. } catch {
  877. Assert.Fail ("TtlChangeoverflow #3");
  878. } finally {
  879. sock.Close ();
  880. }
  881. }
  882. /* Apparently you can set TTL=0 on the ms runtime!!
  883. try {
  884. sock.Ttl = 0;
  885. Assert.Fail ("TtlChangeOverflow #4");
  886. } catch (SocketException ex) {
  887. Assert.AreEqual (10022, ex.ErrorCode,
  888. "TtlChangeOverflow #5");
  889. } catch {
  890. Assert.Fail ("TtlChangeOverflow #6");
  891. } finally {
  892. sock.Close ();
  893. }
  894. */
  895. [Test]
  896. [ExpectedException (typeof(ObjectDisposedException))]
  897. public void TtlClosed ()
  898. {
  899. Socket sock = new Socket (AddressFamily.InterNetwork,
  900. SocketType.Stream,
  901. ProtocolType.Tcp);
  902. sock.Close ();
  903. int val = sock.Ttl;
  904. }
  905. [Test]
  906. public void UseOnlyOverlappedIODefault ()
  907. {
  908. Socket sock = new Socket (AddressFamily.InterNetwork,
  909. SocketType.Stream,
  910. ProtocolType.Tcp);
  911. Assertion.AssertEquals ("UseOnlyOverlappedIODefault",
  912. false,
  913. sock.UseOnlyOverlappedIO);
  914. sock.Close ();
  915. }
  916. //
  917. // We need this because the Linux kernel in certain configurations
  918. // will end up rounding up the values passed on to the kernel
  919. // for socket send/receive timeouts.
  920. //
  921. int Approximate (int target, int value)
  922. {
  923. int epsilon = 10;
  924. if (value > target-10 && value < target+10)
  925. return target;
  926. return value;
  927. }
  928. [Test]
  929. public void UseOnlyOverlappedIOChange ()
  930. {
  931. Socket sock = new Socket (AddressFamily.InterNetwork,
  932. SocketType.Stream,
  933. ProtocolType.Tcp);
  934. sock.UseOnlyOverlappedIO = true;
  935. Assertion.AssertEquals ("UseOnlyOverlappedIOChange",
  936. true,
  937. sock.UseOnlyOverlappedIO);
  938. sock.Close ();
  939. }
  940. [Test]
  941. /* Should not throw an exception */
  942. public void UseOnlyOverlappedIOClosed ()
  943. {
  944. Socket sock = new Socket (AddressFamily.InterNetwork,
  945. SocketType.Stream,
  946. ProtocolType.Tcp);
  947. sock.Close ();
  948. bool val = sock.UseOnlyOverlappedIO;
  949. }
  950. [Test]
  951. public void SendTimeoutDefault ()
  952. {
  953. Socket sock = new Socket (AddressFamily.InterNetwork,
  954. SocketType.Stream,
  955. ProtocolType.Tcp);
  956. Assertion.AssertEquals ("SendTimeoutDefault",
  957. 0, sock.SendTimeout);
  958. sock.Close ();
  959. }
  960. [Test]
  961. public void SendTimeoutChange ()
  962. {
  963. Socket sock = new Socket (AddressFamily.InterNetwork,
  964. SocketType.Stream,
  965. ProtocolType.Tcp);
  966. /* Should be rounded up to 500, according to
  967. * the MSDN docs, but the MS runtime doesn't
  968. */
  969. sock.SendTimeout = 50;
  970. Assertion.AssertEquals ("SendTimeoutChange #1",
  971. 50, Approximate (50, sock.SendTimeout));
  972. sock.SendTimeout = 2000;
  973. Assertion.AssertEquals ("SendTimeoutChange #2",
  974. 2000, Approximate (2000, sock.SendTimeout));
  975. sock.SendTimeout = 0;
  976. Assertion.AssertEquals ("SendTimeoutChange #3",
  977. 0, Approximate (0, sock.SendTimeout));
  978. /* Should be the same as setting 0 */
  979. sock.SendTimeout = -1;
  980. Assertion.AssertEquals ("SendTimeoutChange #4",
  981. 0, sock.SendTimeout);
  982. sock.SendTimeout = 65536;
  983. Assertion.AssertEquals ("SendTimeoutChange #5",
  984. 65536, Approximate (65536, sock.SendTimeout));
  985. try {
  986. sock.SendTimeout = -2;
  987. Assert.Fail ("SendTimeoutChange #8");
  988. } catch (ArgumentOutOfRangeException) {
  989. } catch {
  990. Assert.Fail ("SendTimeoutChange #9");
  991. } finally {
  992. sock.Close ();
  993. }
  994. }
  995. [Test]
  996. [ExpectedException (typeof(ObjectDisposedException))]
  997. public void SendTimeoutClosed ()
  998. {
  999. Socket sock = new Socket (AddressFamily.InterNetwork,
  1000. SocketType.Stream,
  1001. ProtocolType.Tcp);
  1002. sock.Close ();
  1003. int val = sock.SendTimeout;
  1004. }
  1005. [Test]
  1006. public void ReceiveTimeoutDefault ()
  1007. {
  1008. Socket sock = new Socket (AddressFamily.InterNetwork,
  1009. SocketType.Stream,
  1010. ProtocolType.Tcp);
  1011. Assertion.AssertEquals ("ReceiveTimeoutDefault",
  1012. 0, sock.ReceiveTimeout);
  1013. sock.Close ();
  1014. }
  1015. [Test]
  1016. public void ReceiveTimeoutChange ()
  1017. {
  1018. Socket sock = new Socket (AddressFamily.InterNetwork,
  1019. SocketType.Stream,
  1020. ProtocolType.Tcp);
  1021. sock.ReceiveTimeout = 50;
  1022. Assertion.AssertEquals ("ReceiveTimeoutChange #1",
  1023. 50, Approximate (50, sock.ReceiveTimeout));
  1024. sock.ReceiveTimeout = 2000;
  1025. Assertion.AssertEquals ("ReceiveTimeoutChange #2",
  1026. 2000, Approximate (2000, sock.ReceiveTimeout));
  1027. sock.ReceiveTimeout = 0;
  1028. Assertion.AssertEquals ("ReceiveTimeoutChange #3",
  1029. 0, sock.ReceiveTimeout);
  1030. /* Should be the same as setting 0 */
  1031. sock.ReceiveTimeout = -1;
  1032. Assertion.AssertEquals ("ReceiveTimeoutChange #4",
  1033. 0, sock.ReceiveTimeout);
  1034. sock.ReceiveTimeout = 65536;
  1035. Assertion.AssertEquals ("ReceiveTimeoutChange #5",
  1036. 65536, Approximate (65536, sock.ReceiveTimeout));
  1037. try {
  1038. sock.ReceiveTimeout = -2;
  1039. Assert.Fail ("ReceiveTimeoutChange #8");
  1040. } catch (ArgumentOutOfRangeException) {
  1041. } catch {
  1042. Assert.Fail ("ReceiveTimeoutChange #9");
  1043. } finally {
  1044. sock.Close ();
  1045. }
  1046. }
  1047. [Test]
  1048. [ExpectedException (typeof(ObjectDisposedException))]
  1049. public void ReceiveTimeoutClosed ()
  1050. {
  1051. Socket sock = new Socket (AddressFamily.InterNetwork,
  1052. SocketType.Stream,
  1053. ProtocolType.Tcp);
  1054. sock.Close ();
  1055. int val = sock.ReceiveTimeout;
  1056. }
  1057. [Test]
  1058. public void NoDelayDefaultTcp ()
  1059. {
  1060. Socket sock = new Socket (AddressFamily.InterNetwork,
  1061. SocketType.Stream,
  1062. ProtocolType.Tcp);
  1063. Assertion.AssertEquals ("NoDelayDefaultTcp", false,
  1064. sock.NoDelay);
  1065. sock.Close ();
  1066. }
  1067. [Test]
  1068. public void NoDelayChangeTcp ()
  1069. {
  1070. Socket sock = new Socket (AddressFamily.InterNetwork,
  1071. SocketType.Stream,
  1072. ProtocolType.Tcp);
  1073. sock.NoDelay = true;
  1074. Assertion.AssertEquals ("NoDelayChangeTcp", true,
  1075. sock.NoDelay);
  1076. sock.Close ();
  1077. }
  1078. [Test]
  1079. public void NoDelayDefaultUdp ()
  1080. {
  1081. Socket sock = new Socket (AddressFamily.InterNetwork,
  1082. SocketType.Dgram,
  1083. ProtocolType.Udp);
  1084. try {
  1085. bool val = sock.NoDelay;
  1086. Assert.Fail ("NoDelayDefaultUdp #1");
  1087. } catch (SocketException ex) {
  1088. Assert.AreEqual (10042, ex.ErrorCode,
  1089. "NoDelayDefaultUdp #2");
  1090. } catch {
  1091. Assert.Fail ("NoDelayDefaultUdp #3");
  1092. } finally {
  1093. sock.Close ();
  1094. }
  1095. }
  1096. [Test]
  1097. public void NoDelayChangeUdp ()
  1098. {
  1099. Socket sock = new Socket (AddressFamily.InterNetwork,
  1100. SocketType.Dgram,
  1101. ProtocolType.Udp);
  1102. try {
  1103. sock.NoDelay = true;
  1104. Assert.Fail ("NoDelayChangeUdp #1");
  1105. } catch (SocketException ex) {
  1106. Assert.AreEqual (10042, ex.ErrorCode,
  1107. "NoDelayChangeUdp #2");
  1108. } catch {
  1109. Assert.Fail ("NoDelayChangeUdp #3");
  1110. } finally {
  1111. sock.Close ();
  1112. }
  1113. }
  1114. [Test]
  1115. [ExpectedException (typeof(ObjectDisposedException))]
  1116. public void NoDelayClosed ()
  1117. {
  1118. Socket sock = new Socket (AddressFamily.InterNetwork,
  1119. SocketType.Stream,
  1120. ProtocolType.Tcp);
  1121. sock.Close ();
  1122. bool val = sock.NoDelay;
  1123. }
  1124. static bool BAAccepted = false;
  1125. static Socket BASocket = null;
  1126. static ManualResetEvent BACalledBack = new ManualResetEvent (false);
  1127. private static void BACallback (IAsyncResult asyncResult)
  1128. {
  1129. Socket sock = (Socket)asyncResult.AsyncState;
  1130. BASocket = sock.EndAccept (asyncResult);
  1131. BAAccepted = true;
  1132. BACalledBack.Set ();
  1133. }
  1134. [Test]
  1135. [ExpectedException (typeof(InvalidOperationException))]
  1136. public void BeginAcceptNotBound ()
  1137. {
  1138. Socket sock = new Socket (AddressFamily.InterNetwork,
  1139. SocketType.Stream,
  1140. ProtocolType.Tcp);
  1141. sock.BeginAccept (BACallback, sock);
  1142. sock.Close ();
  1143. }
  1144. [Test]
  1145. [ExpectedException (typeof(InvalidOperationException))]
  1146. public void BeginAcceptNotListening ()
  1147. {
  1148. Socket sock = new Socket (AddressFamily.InterNetwork,
  1149. SocketType.Stream,
  1150. ProtocolType.Tcp);
  1151. sock.Bind (new IPEndPoint (IPAddress.Any, 1236));
  1152. sock.BeginAccept (BACallback, sock);
  1153. sock.Close ();
  1154. }
  1155. [Test]
  1156. public void BeginAccept ()
  1157. {
  1158. Socket sock = new Socket (AddressFamily.InterNetwork,
  1159. SocketType.Stream,
  1160. ProtocolType.Tcp);
  1161. IPEndPoint ep = new IPEndPoint (IPAddress.Loopback,
  1162. 1237);
  1163. sock.Bind (ep);
  1164. sock.Listen (1);
  1165. BACalledBack.Reset ();
  1166. sock.BeginAccept (BACallback, sock);
  1167. Socket conn = new Socket (AddressFamily.InterNetwork,
  1168. SocketType.Stream,
  1169. ProtocolType.Tcp);
  1170. conn.Connect (ep);
  1171. if (BACalledBack.WaitOne (2000, false) == false) {
  1172. Assert.Fail ("BeginAccept wait timed out");
  1173. }
  1174. Assertion.AssertEquals ("BeginAccept #1", true,
  1175. BAAccepted);
  1176. Assertion.AssertEquals ("BeginAccept #2", true,
  1177. BASocket.Connected);
  1178. Assertion.AssertEquals ("BeginAccept #3", false,
  1179. sock.Connected);
  1180. Assertion.AssertEquals ("BeginAccept #4", true,
  1181. conn.Connected);
  1182. BASocket.Close ();
  1183. conn.Close ();
  1184. sock.Close ();
  1185. }
  1186. [Test]
  1187. [ExpectedException (typeof(ObjectDisposedException))]
  1188. public void BeginAcceptClosed ()
  1189. {
  1190. Socket sock = new Socket (AddressFamily.InterNetwork,
  1191. SocketType.Stream,
  1192. ProtocolType.Tcp);
  1193. sock.Close ();
  1194. sock.BeginAccept (BACallback, sock);
  1195. }
  1196. static bool BADAccepted = false;
  1197. static Socket BADSocket = null;
  1198. static byte[] BADBytes;
  1199. static int BADByteCount;
  1200. static ManualResetEvent BADCalledBack = new ManualResetEvent (false);
  1201. private static void BADCallback (IAsyncResult asyncResult)
  1202. {
  1203. Socket sock = (Socket)asyncResult.AsyncState;
  1204. BADSocket = sock.EndAccept (out BADBytes,
  1205. out BADByteCount,
  1206. asyncResult);
  1207. BADAccepted = true;
  1208. BADCalledBack.Set ();
  1209. }
  1210. [Test]
  1211. public void BeginAcceptData ()
  1212. {
  1213. Socket sock = new Socket (AddressFamily.InterNetwork,
  1214. SocketType.Stream,
  1215. ProtocolType.Tcp);
  1216. IPEndPoint ep = new IPEndPoint (IPAddress.Loopback,
  1217. 1238);
  1218. sock.Bind (ep);
  1219. sock.Listen (1);
  1220. BADCalledBack.Reset ();
  1221. sock.BeginAccept (256, BADCallback, sock);
  1222. Socket conn = new Socket (AddressFamily.InterNetwork,
  1223. SocketType.Stream,
  1224. ProtocolType.Tcp);
  1225. byte[] send_bytes = new byte[] {10, 11, 12, 13};
  1226. conn.Connect (ep);
  1227. conn.Send (send_bytes);
  1228. if (BADCalledBack.WaitOne (2000, false) == false) {
  1229. Assert.Fail ("BeginAcceptData wait timed out");
  1230. }
  1231. Assertion.AssertEquals ("BeginAcceptData #1", true,
  1232. BADAccepted);
  1233. Assertion.AssertEquals ("BeginAcceptData #2", true,
  1234. BADSocket.Connected);
  1235. Assertion.AssertEquals ("BeginAcceptData #3", false,
  1236. sock.Connected);
  1237. Assertion.AssertEquals ("BeginAcceptData #4", true,
  1238. conn.Connected);
  1239. Assertion.AssertEquals ("BeginAcceptData #5",
  1240. send_bytes.Length,
  1241. BADByteCount);
  1242. /* The MS runtime gives the returned data in a
  1243. * much bigger array. TODO: investigate
  1244. * whether it the size correlates to the first
  1245. * parameter in BeginAccept()
  1246. */
  1247. Assert.IsFalse (BADBytes.Length == send_bytes.Length,
  1248. "BeginAcceptData #6");
  1249. for(int i = 0; i < send_bytes.Length; i++) {
  1250. Assertion.AssertEquals ("BeginAcceptData #" + (i+7).ToString (), send_bytes[i], BADBytes[i]);
  1251. }
  1252. BADSocket.Close ();
  1253. conn.Close ();
  1254. sock.Close ();
  1255. }
  1256. [Test]
  1257. [ExpectedException (typeof(ObjectDisposedException))]
  1258. public void BeginAcceptDataClosed ()
  1259. {
  1260. Socket sock = new Socket (AddressFamily.InterNetwork,
  1261. SocketType.Stream,
  1262. ProtocolType.Tcp);
  1263. sock.Close ();
  1264. sock.BeginAccept (256, BADCallback, sock);
  1265. }
  1266. [Test]
  1267. public void BeginAcceptSocketUdp ()
  1268. {
  1269. Socket sock = new Socket (AddressFamily.InterNetwork,
  1270. SocketType.Stream,
  1271. ProtocolType.Tcp);
  1272. Socket acc = new Socket (AddressFamily.InterNetwork,
  1273. SocketType.Dgram,
  1274. ProtocolType.Udp);
  1275. IPEndPoint ep = new IPEndPoint (IPAddress.Loopback,
  1276. 1239);
  1277. sock.Bind (ep);
  1278. sock.Listen (1);
  1279. try {
  1280. sock.BeginAccept (acc, 256, BADCallback, sock);
  1281. Assert.Fail ("BeginAcceptSocketUdp #1");
  1282. } catch (SocketException ex) {
  1283. Assertion.AssertEquals ("BeginAcceptSocketUdp #2", 10022, ex.ErrorCode);
  1284. } catch {
  1285. Assert.Fail ("BeginAcceptSocketUdp #3");
  1286. } finally {
  1287. acc.Close ();
  1288. sock.Close ();
  1289. }
  1290. }
  1291. [Test]
  1292. public void BeginAcceptSocketBound ()
  1293. {
  1294. Socket sock = new Socket (AddressFamily.InterNetwork,
  1295. SocketType.Stream,
  1296. ProtocolType.Tcp);
  1297. Socket acc = new Socket (AddressFamily.InterNetwork,
  1298. SocketType.Stream,
  1299. ProtocolType.Tcp);
  1300. IPEndPoint ep1 = new IPEndPoint (IPAddress.Loopback,
  1301. 1240);
  1302. IPEndPoint ep2 = new IPEndPoint (IPAddress.Loopback,
  1303. 1241);
  1304. sock.Bind (ep1);
  1305. sock.Listen (1);
  1306. acc.Bind (ep2);
  1307. try {
  1308. sock.BeginAccept (acc, 256, BADCallback, sock);
  1309. Assert.Fail ("BeginAcceptSocketBound #1");
  1310. } catch (InvalidOperationException) {
  1311. } catch {
  1312. Assert.Fail ("BeginAcceptSocketBound #2");
  1313. } finally {
  1314. acc.Close ();
  1315. sock.Close ();
  1316. }
  1317. }
  1318. [Test]
  1319. public void BeginAcceptSocket ()
  1320. {
  1321. Socket sock = new Socket (AddressFamily.InterNetwork,
  1322. SocketType.Stream,
  1323. ProtocolType.Tcp);
  1324. Socket acc = new Socket (AddressFamily.InterNetwork,
  1325. SocketType.Stream,
  1326. ProtocolType.Tcp);
  1327. IPEndPoint ep = new IPEndPoint (IPAddress.Loopback,
  1328. 1242);
  1329. sock.Bind (ep);
  1330. sock.Listen (1);
  1331. BADCalledBack.Reset ();
  1332. sock.BeginAccept (acc, 256, BADCallback, sock);
  1333. Socket conn = new Socket (AddressFamily.InterNetwork,
  1334. SocketType.Stream,
  1335. ProtocolType.Tcp);
  1336. byte[] send_bytes = new byte[] {10, 11, 12, 13};
  1337. conn.Connect (ep);
  1338. conn.Send (send_bytes);
  1339. if (BADCalledBack.WaitOne (2000, false) == false) {
  1340. Assert.Fail ("BeginAcceptSocket wait timed out");
  1341. }
  1342. Assertion.AssertEquals ("BeginAcceptSocket #1", true,
  1343. BADAccepted);
  1344. Assertion.AssertEquals ("BeginAcceptSocket #2", true,
  1345. BADSocket.Connected);
  1346. Assertion.AssertEquals ("BeginAcceptSocket #3", false,
  1347. sock.Connected);
  1348. Assertion.AssertEquals ("BeginAcceptSocket #4", true,
  1349. conn.Connected);
  1350. Assertion.AssertEquals ("BeginAcceptSocket #5",
  1351. send_bytes.Length,
  1352. BADByteCount);
  1353. Assertion.AssertEquals ("BeginAcceptSocket #6",
  1354. AddressFamily.InterNetwork,
  1355. acc.AddressFamily);
  1356. Assertion.AssertEquals ("BeginAcceptSocket #7",
  1357. SocketType.Stream,
  1358. acc.SocketType);
  1359. Assertion.AssertEquals ("BeginAcceptSocket #8",
  1360. ProtocolType.Tcp,
  1361. acc.ProtocolType);
  1362. Assertion.AssertEquals ("BeginAcceptSocket #9",
  1363. conn.LocalEndPoint,
  1364. acc.RemoteEndPoint);
  1365. /* The MS runtime gives the returned data in a
  1366. * much bigger array. TODO: investigate
  1367. * whether it the size correlates to the first
  1368. * parameter in BeginAccept()
  1369. */
  1370. Assert.IsFalse (BADBytes.Length == send_bytes.Length,
  1371. "BeginAcceptSocket #10");
  1372. for(int i = 0; i < send_bytes.Length; i++) {
  1373. Assertion.AssertEquals ("BeginAcceptSocket #" + (i+11).ToString (), send_bytes[i], BADBytes[i]);
  1374. }
  1375. BADSocket.Close ();
  1376. conn.Close ();
  1377. acc.Close ();
  1378. sock.Close ();
  1379. }
  1380. [Test]
  1381. public void BeginAcceptSocketClosed ()
  1382. {
  1383. Socket sock = new Socket (AddressFamily.InterNetwork,
  1384. SocketType.Stream,
  1385. ProtocolType.Tcp);
  1386. Socket acc = new Socket (AddressFamily.InterNetwork,
  1387. SocketType.Stream,
  1388. ProtocolType.Tcp);
  1389. sock.Close ();
  1390. try {
  1391. sock.BeginAccept (acc, 256, BADCallback, null);
  1392. Assert.Fail ("BeginAcceptSocketClosed #1");
  1393. } catch (ObjectDisposedException) {
  1394. } catch {
  1395. Assert.Fail ("BeginAcceptSocketClosed #2");
  1396. } finally {
  1397. acc.Close ();
  1398. }
  1399. }
  1400. [Test]
  1401. public void BeginAcceptSocketAccClosed ()
  1402. {
  1403. Socket sock = new Socket (AddressFamily.InterNetwork,
  1404. SocketType.Stream,
  1405. ProtocolType.Tcp);
  1406. Socket acc = new Socket (AddressFamily.InterNetwork,
  1407. SocketType.Stream,
  1408. ProtocolType.Tcp);
  1409. IPEndPoint ep = new IPEndPoint (IPAddress.Loopback,
  1410. 1243);
  1411. sock.Bind (ep);
  1412. sock.Listen (1);
  1413. acc.Close ();
  1414. BADCalledBack.Reset ();
  1415. try {
  1416. sock.BeginAccept (acc, 256, BADCallback, null);
  1417. Assert.Fail ("BeginAcceptSocketAccClosed #1");
  1418. } catch (ObjectDisposedException) {
  1419. } catch {
  1420. Assert.Fail ("BeginAcceptSocketAccClosed #2");
  1421. } finally {
  1422. sock.Close ();
  1423. }
  1424. }
  1425. static bool BCConnected = false;
  1426. static ManualResetEvent BCCalledBack = new ManualResetEvent (false);
  1427. private static void BCCallback (IAsyncResult asyncResult)
  1428. {
  1429. Socket sock = (Socket)asyncResult.AsyncState;
  1430. sock.EndConnect (asyncResult);
  1431. BCConnected = true;
  1432. BCCalledBack.Set ();
  1433. }
  1434. [Test]
  1435. public void BeginConnectAddressPort ()
  1436. {
  1437. Socket sock = new Socket (AddressFamily.InterNetwork,
  1438. SocketType.Stream,
  1439. ProtocolType.Tcp);
  1440. Socket listen = new Socket (AddressFamily.InterNetwork,
  1441. SocketType.Stream,
  1442. ProtocolType.Tcp);
  1443. IPAddress ip = IPAddress.Loopback;
  1444. IPEndPoint ep = new IPEndPoint (ip, 1244);
  1445. listen.Bind (ep);
  1446. listen.Listen (1);
  1447. BCCalledBack.Reset ();
  1448. BCConnected = false;
  1449. sock.BeginConnect (ip, 1244, BCCallback, sock);
  1450. if (BCCalledBack.WaitOne (2000, false) == false) {
  1451. Assert.Fail ("BeginConnectAddressPort wait timed out");
  1452. }
  1453. Assertion.AssertEquals ("BeginConnectAddressPort #1",
  1454. true, BCConnected);
  1455. sock.Close ();
  1456. listen.Close ();
  1457. }
  1458. [Test]
  1459. public void BeginConnectAddressPortNull ()
  1460. {
  1461. Socket sock = new Socket (AddressFamily.InterNetwork,
  1462. SocketType.Stream,
  1463. ProtocolType.Tcp);
  1464. IPAddress ip = null;
  1465. try {
  1466. sock.BeginConnect (ip, 1244, BCCallback,
  1467. sock);
  1468. Assert.Fail ("BeginConnectAddressPortNull #1");
  1469. } catch (ArgumentNullException) {
  1470. } catch {
  1471. Assert.Fail ("BeginConnectAddressPortNull #2");
  1472. } finally {
  1473. sock.Close ();
  1474. }
  1475. }
  1476. [Test]
  1477. public void BeginConnectAddressPortListen ()
  1478. {
  1479. Socket sock = new Socket (AddressFamily.InterNetwork,
  1480. SocketType.Stream,
  1481. ProtocolType.Tcp);
  1482. IPAddress ip = IPAddress.Loopback;
  1483. IPEndPoint ep = new IPEndPoint (ip, 1245);
  1484. sock.Bind (ep);
  1485. sock.Listen (1);
  1486. try {
  1487. sock.BeginConnect (ip, 1245, BCCallback, sock);
  1488. Assert.Fail ("BeginConnectAddressPortListen #1");
  1489. } catch (InvalidOperationException) {
  1490. } catch {
  1491. Assert.Fail ("BeginConnectAddressPortListen #2");
  1492. } finally {
  1493. sock.Close ();
  1494. }
  1495. }
  1496. [Test]
  1497. [ExpectedException (typeof(ObjectDisposedException))]
  1498. public void BeginConnectAddressPortClosed ()
  1499. {
  1500. Socket sock = new Socket (AddressFamily.InterNetwork,
  1501. SocketType.Stream,
  1502. ProtocolType.Tcp);
  1503. IPAddress ip = IPAddress.Loopback;
  1504. sock.Close ();
  1505. sock.BeginConnect (ip, 1244, BCCallback, sock);
  1506. }
  1507. [Test]
  1508. public void BeginConnectMultiple ()
  1509. {
  1510. Socket sock = new Socket (AddressFamily.InterNetwork,
  1511. SocketType.Stream,
  1512. ProtocolType.Tcp);
  1513. Socket listen = new Socket (AddressFamily.InterNetwork,
  1514. SocketType.Stream,
  1515. ProtocolType.Tcp);
  1516. IPEndPoint ep = new IPEndPoint (IPAddress.Loopback,
  1517. 1246);
  1518. IPAddress[] ips = new IPAddress[4];
  1519. ips[0] = IPAddress.Parse ("127.0.0.4");
  1520. ips[1] = IPAddress.Parse ("127.0.0.3");
  1521. ips[2] = IPAddress.Parse ("127.0.0.2");
  1522. ips[3] = IPAddress.Parse ("127.0.0.1");
  1523. listen.Bind (ep);
  1524. listen.Listen (1);
  1525. BCCalledBack.Reset ();
  1526. BCConnected = false;
  1527. sock.BeginConnect (ips, 1246, BCCallback, sock);
  1528. /* Longer wait here, because the ms runtime
  1529. * takes a lot longer to not connect
  1530. */
  1531. if (BCCalledBack.WaitOne (10000, false) == false) {
  1532. Assert.Fail ("BeginConnectMultiple wait failed");
  1533. }
  1534. Assertion.AssertEquals ("BeginConnectMultiple #1",
  1535. true, BCConnected);
  1536. Assertion.AssertEquals ("BeginConnectMultiple #2",
  1537. AddressFamily.InterNetwork,
  1538. sock.RemoteEndPoint.AddressFamily);
  1539. IPEndPoint remep = (IPEndPoint)sock.RemoteEndPoint;
  1540. Assertion.AssertEquals ("BeginConnectMultiple #2",
  1541. IPAddress.Loopback,
  1542. remep.Address);
  1543. sock.Close ();
  1544. listen.Close ();
  1545. }
  1546. [Test]
  1547. public void BeginConnectMultipleNull ()
  1548. {
  1549. Socket sock = new Socket (AddressFamily.InterNetwork,
  1550. SocketType.Stream,
  1551. ProtocolType.Tcp);
  1552. IPAddress[] ips = null;
  1553. try {
  1554. sock.BeginConnect (ips, 1246, BCCallback,
  1555. sock);
  1556. Assert.Fail ("BeginConnectMultipleNull #1");
  1557. } catch (ArgumentNullException) {
  1558. } catch {
  1559. Assert.Fail ("BeginConnectMultipleNull #2");
  1560. } finally {
  1561. sock.Close ();
  1562. }
  1563. }
  1564. [Test]
  1565. public void BeginConnectMultipleListen ()
  1566. {
  1567. Socket sock = new Socket (AddressFamily.InterNetwork,
  1568. SocketType.Stream,
  1569. ProtocolType.Tcp);
  1570. IPAddress[] ips = new IPAddress[4];
  1571. IPEndPoint ep = new IPEndPoint (IPAddress.Loopback,
  1572. 1247);
  1573. ips[0] = IPAddress.Parse ("127.0.0.4");
  1574. ips[1] = IPAddress.Parse ("127.0.0.3");
  1575. ips[2] = IPAddress.Parse ("127.0.0.2");
  1576. ips[3] = IPAddress.Parse ("127.0.0.1");
  1577. sock.Bind (ep);
  1578. sock.Listen (1);
  1579. try {
  1580. sock.BeginConnect (ips, 1247, BCCallback,
  1581. sock);
  1582. Assert.Fail ("BeginConnectMultipleListen #1");
  1583. } catch (InvalidOperationException) {
  1584. } catch {
  1585. Assert.Fail ("BeginConnectMultipleListen #2");
  1586. } finally {
  1587. sock.Close ();
  1588. }
  1589. }
  1590. [Test]
  1591. [ExpectedException (typeof(ObjectDisposedException))]
  1592. public void BeginConnectMultipleClosed ()
  1593. {
  1594. Socket sock = new Socket (AddressFamily.InterNetwork,
  1595. SocketType.Stream,
  1596. ProtocolType.Tcp);
  1597. IPAddress[] ips = new IPAddress[4];
  1598. ips[0] = IPAddress.Parse ("127.0.0.4");
  1599. ips[1] = IPAddress.Parse ("127.0.0.3");
  1600. ips[2] = IPAddress.Parse ("127.0.0.2");
  1601. ips[3] = IPAddress.Parse ("127.0.0.1");
  1602. sock.Close ();
  1603. sock.BeginConnect (ips, 1247, BCCallback, sock);
  1604. }
  1605. [Test]
  1606. public void BeginConnectHostPortNull ()
  1607. {
  1608. Socket sock = new Socket (AddressFamily.InterNetwork,
  1609. SocketType.Stream,
  1610. ProtocolType.Tcp);
  1611. try {
  1612. sock.BeginConnect ((string)null, 0,
  1613. BCCallback, sock);
  1614. Assert.Fail ("BeginConnectHostPort #1");
  1615. } catch (ArgumentNullException) {
  1616. } catch {
  1617. Assert.Fail ("BeginConnectHostPort #2");
  1618. } finally {
  1619. sock.Close ();
  1620. }
  1621. }
  1622. [Test]
  1623. public void BeginConnectHostPortListen ()
  1624. {
  1625. Socket sock = new Socket (AddressFamily.InterNetwork,
  1626. SocketType.Stream,
  1627. ProtocolType.Tcp);
  1628. IPAddress ip = IPAddress.Loopback;
  1629. IPEndPoint ep = new IPEndPoint (ip, 1248);
  1630. sock.Bind (ep);
  1631. sock.Listen (1);
  1632. try {
  1633. sock.BeginConnect ("localhost", 1248,
  1634. BCCallback, sock);
  1635. Assert.Fail ("BeginConnectHostPortListen #1");
  1636. } catch (InvalidOperationException) {
  1637. } catch {
  1638. Assert.Fail ("BeginConnectHostPortListen #2");
  1639. } finally {
  1640. sock.Close ();
  1641. }
  1642. }
  1643. [Test]
  1644. [Category ("NotWorking")] // Need to pick a non-IP AddressFamily that "works" on both mono and ms, this one only works on ms
  1645. public void BeginConnectHostPortNotIP ()
  1646. {
  1647. Socket sock = new Socket (AddressFamily.NetBios,
  1648. SocketType.Seqpacket,
  1649. ProtocolType.Unspecified);
  1650. try {
  1651. sock.BeginConnect ("localhost", 0, BCCallback,
  1652. sock);
  1653. Assert.Fail ("BeginConnectHostPortNotIP #1");
  1654. } catch (NotSupportedException) {
  1655. } catch {
  1656. Assert.Fail ("BeginConnectHostPortNotIP #2");
  1657. } finally {
  1658. sock.Close ();
  1659. }
  1660. }
  1661. [Test]
  1662. [ExpectedException (typeof(ObjectDisposedException))]
  1663. public void BeginConnectHostPortClosed ()
  1664. {
  1665. Socket sock = new Socket (AddressFamily.InterNetwork,
  1666. SocketType.Stream,
  1667. ProtocolType.Tcp);
  1668. sock.Close ();
  1669. sock.BeginConnect ("localhost", 0, BCCallback, sock);
  1670. }
  1671. static bool BDDisconnected = false;
  1672. static ManualResetEvent BDCalledBack = new ManualResetEvent (false);
  1673. private static void BDCallback (IAsyncResult asyncResult)
  1674. {
  1675. Socket sock = (Socket)asyncResult.AsyncState;
  1676. sock.EndDisconnect (asyncResult);
  1677. BDDisconnected = true;
  1678. BDCalledBack.Set ();
  1679. }
  1680. [Test]
  1681. [Category ("NotDotNet")] // "Needs XP or later"
  1682. public void BeginDisconnect ()
  1683. {
  1684. Socket sock = new Socket (AddressFamily.InterNetwork,
  1685. SocketType.Stream,
  1686. ProtocolType.Tcp);
  1687. Socket listen = new Socket (AddressFamily.InterNetwork,
  1688. SocketType.Stream,
  1689. ProtocolType.Tcp);
  1690. IPAddress ip = IPAddress.Loopback;
  1691. IPEndPoint ep = new IPEndPoint (ip, 1254);
  1692. listen.Bind (ep);
  1693. listen.Listen (1);
  1694. sock.Connect (ip, 1254);
  1695. Assertion.AssertEquals ("BeginDisconnect #1", true,
  1696. sock.Connected);
  1697. sock.Shutdown (SocketShutdown.Both);
  1698. BDCalledBack.Reset ();
  1699. BDDisconnected = false;
  1700. sock.BeginDisconnect (false, BDCallback, sock);
  1701. if (BDCalledBack.WaitOne (2000, false) == false) {
  1702. Assert.Fail ("BeginDisconnect wait timed out");
  1703. }
  1704. Assertion.AssertEquals ("BeginDisconnect #2", true,
  1705. BDDisconnected);
  1706. Assertion.AssertEquals ("BeginDisconnect #3", false,
  1707. sock.Connected);
  1708. sock.Close ();
  1709. listen.Close ();
  1710. }
  1711. [Test]
  1712. public void BeginReceiveSocketError ()
  1713. {
  1714. }
  1715. [Test]
  1716. public void BeginReceiveGeneric ()
  1717. {
  1718. }
  1719. [Test]
  1720. public void BeginReceiveGenericSocketError ()
  1721. {
  1722. }
  1723. private static void BSCallback (IAsyncResult asyncResult)
  1724. {
  1725. Socket sock = (Socket)asyncResult.AsyncState;
  1726. sock.EndSend (asyncResult);
  1727. }
  1728. [Test]
  1729. public void BeginSendNotConnected ()
  1730. {
  1731. Socket sock = new Socket (AddressFamily.InterNetwork,
  1732. SocketType.Stream,
  1733. ProtocolType.Tcp);
  1734. byte[] send_bytes = new byte[] {10, 11, 12, 13};
  1735. try {
  1736. sock.BeginSend (send_bytes, 0,
  1737. send_bytes.Length,
  1738. SocketFlags.None, BSCallback,
  1739. sock);
  1740. Assert.Fail ("BeginSendNotConnected #1");
  1741. } catch (SocketException ex) {
  1742. Assertion.AssertEquals ("BeginSendNotConnected #2", 10057, ex.ErrorCode);
  1743. } catch {
  1744. Assert.Fail ("BeginSendNotConnected #3");
  1745. } finally {
  1746. sock.Close ();
  1747. }
  1748. }
  1749. [Test]
  1750. public void BeginSendSocketError ()
  1751. {
  1752. }
  1753. [Test]
  1754. public void BeginSendGeneric ()
  1755. {
  1756. }
  1757. [Test]
  1758. public void BeginSendGenericSocketError ()
  1759. {
  1760. }
  1761. [Test]
  1762. public void BindTwice ()
  1763. {
  1764. Socket sock = new Socket (AddressFamily.InterNetwork,
  1765. SocketType.Stream,
  1766. ProtocolType.Tcp);
  1767. IPEndPoint ep1 = new IPEndPoint (IPAddress.Loopback,
  1768. 1256);
  1769. IPEndPoint ep2 = new IPEndPoint (IPAddress.Loopback,
  1770. 1257);
  1771. sock.Bind (ep1);
  1772. try {
  1773. sock.Bind (ep2);
  1774. Assert.Fail ("BindTwice #1");
  1775. } catch (SocketException ex) {
  1776. Assertion.AssertEquals ("BindTwice #2",
  1777. 10022, ex.ErrorCode);
  1778. } catch {
  1779. Assert.Fail ("BindTwice #3");
  1780. } finally {
  1781. sock.Close ();
  1782. }
  1783. }
  1784. [Test]
  1785. public void Close ()
  1786. {
  1787. Socket sock = new Socket (AddressFamily.InterNetwork,
  1788. SocketType.Stream,
  1789. ProtocolType.Tcp);
  1790. Socket listen = new Socket (AddressFamily.InterNetwork,
  1791. SocketType.Stream,
  1792. ProtocolType.Tcp);
  1793. IPEndPoint ep = new IPEndPoint (IPAddress.Loopback,
  1794. 1258);
  1795. listen.Bind (ep);
  1796. listen.Listen (1);
  1797. sock.Connect (ep);
  1798. Assertion.AssertEquals ("Close #1", true,
  1799. sock.Connected);
  1800. sock.Close (2);
  1801. Thread.Sleep (3000);
  1802. Assertion.AssertEquals ("Close #2", false,
  1803. sock.Connected);
  1804. listen.Close ();
  1805. }
  1806. [Test]
  1807. public void ConnectAddressPort ()
  1808. {
  1809. Socket sock = new Socket (AddressFamily.InterNetwork,
  1810. SocketType.Stream,
  1811. ProtocolType.Tcp);
  1812. Socket listen = new Socket (AddressFamily.InterNetwork,
  1813. SocketType.Stream,
  1814. ProtocolType.Tcp);
  1815. IPAddress ip = IPAddress.Loopback;
  1816. IPEndPoint ep = new IPEndPoint (ip, 1249);
  1817. listen.Bind (ep);
  1818. listen.Listen (1);
  1819. sock.Connect (ip, 1249);
  1820. Assertion.AssertEquals ("ConnectAddressPort #1",
  1821. true, sock.Connected);
  1822. sock.Close ();
  1823. listen.Close ();
  1824. }
  1825. [Test]
  1826. public void ConnectAddressPortNull ()
  1827. {
  1828. Socket sock = new Socket (AddressFamily.InterNetwork,
  1829. SocketType.Stream,
  1830. ProtocolType.Tcp);
  1831. IPAddress ip = null;
  1832. try {
  1833. sock.Connect (ip, 1249);
  1834. Assert.Fail ("ConnectAddressPortNull #1");
  1835. } catch (ArgumentNullException) {
  1836. } catch {
  1837. Assert.Fail ("ConnectAddressPortNull #2");
  1838. } finally {
  1839. sock.Close ();
  1840. }
  1841. }
  1842. [Test]
  1843. public void ConnectAddressPortListen ()
  1844. {
  1845. Socket sock = new Socket (AddressFamily.InterNetwork,
  1846. SocketType.Stream,
  1847. ProtocolType.Tcp);
  1848. IPAddress ip = IPAddress.Loopback;
  1849. IPEndPoint ep = new IPEndPoint (ip, 1250);
  1850. sock.Bind (ep);
  1851. sock.Listen (1);
  1852. try {
  1853. sock.Connect (ip, 1250);
  1854. Assert.Fail ("ConnectAddressPortListen #1");
  1855. } catch (InvalidOperationException) {
  1856. } catch {
  1857. Assert.Fail ("ConnectAddressPortListen #2");
  1858. } finally {
  1859. sock.Close ();
  1860. }
  1861. }
  1862. [Test]
  1863. [ExpectedException (typeof(ObjectDisposedException))]
  1864. public void ConnectAddressPortClosed ()
  1865. {
  1866. Socket sock = new Socket (AddressFamily.InterNetwork,
  1867. SocketType.Stream,
  1868. ProtocolType.Tcp);
  1869. IPAddress ip = IPAddress.Loopback;
  1870. sock.Close ();
  1871. sock.Connect (ip, 1250);
  1872. }
  1873. [Test]
  1874. public void ConnectMultiple ()
  1875. {
  1876. Socket sock = new Socket (AddressFamily.InterNetwork,
  1877. SocketType.Stream,
  1878. ProtocolType.Tcp);
  1879. Socket listen = new Socket (AddressFamily.InterNetwork,
  1880. SocketType.Stream,
  1881. ProtocolType.Tcp);
  1882. IPEndPoint ep = new IPEndPoint (IPAddress.Loopback,
  1883. 1251);
  1884. IPAddress[] ips = new IPAddress[4];
  1885. ips[0] = IPAddress.Parse ("127.0.0.4");
  1886. ips[1] = IPAddress.Parse ("127.0.0.3");
  1887. ips[2] = IPAddress.Parse ("127.0.0.2");
  1888. ips[3] = IPAddress.Parse ("127.0.0.1");
  1889. listen.Bind (ep);
  1890. listen.Listen (1);
  1891. sock.Connect (ips, 1251);
  1892. Assertion.AssertEquals ("ConnectMultiple #1",
  1893. true, sock.Connected);
  1894. Assertion.AssertEquals ("ConnectMultiple #2",
  1895. AddressFamily.InterNetwork,
  1896. sock.RemoteEndPoint.AddressFamily);
  1897. IPEndPoint remep = (IPEndPoint)sock.RemoteEndPoint;
  1898. Assertion.AssertEquals ("ConnectMultiple #2",
  1899. IPAddress.Loopback,
  1900. remep.Address);
  1901. sock.Close ();
  1902. listen.Close ();
  1903. }
  1904. [Test]
  1905. public void ConnectMultipleNull ()
  1906. {
  1907. Socket sock = new Socket (AddressFamily.InterNetwork,
  1908. SocketType.Stream,
  1909. ProtocolType.Tcp);
  1910. IPAddress[] ips = null;
  1911. try {
  1912. sock.Connect (ips, 1251);
  1913. Assert.Fail ("ConnectMultipleNull #1");
  1914. } catch (ArgumentNullException) {
  1915. } catch {
  1916. Assert.Fail ("ConnectMultipleNull #2");
  1917. } finally {
  1918. sock.Close ();
  1919. }
  1920. }
  1921. [Test]
  1922. public void ConnectMultipleListen ()
  1923. {
  1924. Socket sock = new Socket (AddressFamily.InterNetwork,
  1925. SocketType.Stream,
  1926. ProtocolType.Tcp);
  1927. IPAddress[] ips = new IPAddress[4];
  1928. IPEndPoint ep = new IPEndPoint (IPAddress.Loopback,
  1929. 1252);
  1930. ips[0] = IPAddress.Parse ("127.0.0.4");
  1931. ips[1] = IPAddress.Parse ("127.0.0.3");
  1932. ips[2] = IPAddress.Parse ("127.0.0.2");
  1933. ips[3] = IPAddress.Parse ("127.0.0.1");
  1934. sock.Bind (ep);
  1935. sock.Listen (1);
  1936. try {
  1937. sock.Connect (ips, 1252);
  1938. Assert.Fail ("ConnectMultipleListen #1");
  1939. } catch (InvalidOperationException) {
  1940. } catch {
  1941. Assert.Fail ("ConnectMultipleListen #2");
  1942. } finally {
  1943. sock.Close ();
  1944. }
  1945. }
  1946. [Test]
  1947. [ExpectedException (typeof(ObjectDisposedException))]
  1948. public void ConnectMultipleClosed ()
  1949. {
  1950. Socket sock = new Socket (AddressFamily.InterNetwork,
  1951. SocketType.Stream,
  1952. ProtocolType.Tcp);
  1953. IPAddress[] ips = new IPAddress[4];
  1954. ips[0] = IPAddress.Parse ("127.0.0.4");
  1955. ips[1] = IPAddress.Parse ("127.0.0.3");
  1956. ips[2] = IPAddress.Parse ("127.0.0.2");
  1957. ips[3] = IPAddress.Parse ("127.0.0.1");
  1958. sock.Close ();
  1959. sock.Connect (ips, 1252);
  1960. }
  1961. [Test]
  1962. public void ConnectHostPortNull ()
  1963. {
  1964. Socket sock = new Socket (AddressFamily.InterNetwork,
  1965. SocketType.Stream,
  1966. ProtocolType.Tcp);
  1967. try {
  1968. sock.Connect ((string)null, 0);
  1969. Assert.Fail ("ConnectHostPort #1");
  1970. } catch (ArgumentNullException) {
  1971. } catch {
  1972. Assert.Fail ("ConnectHostPort #2");
  1973. } finally {
  1974. sock.Close ();
  1975. }
  1976. }
  1977. [Test]
  1978. public void ConnectHostPortListen ()
  1979. {
  1980. Socket sock = new Socket (AddressFamily.InterNetwork,
  1981. SocketType.Stream,
  1982. ProtocolType.Tcp);
  1983. IPAddress ip = IPAddress.Loopback;
  1984. IPEndPoint ep = new IPEndPoint (ip, 1253);
  1985. sock.Bind (ep);
  1986. sock.Listen (1);
  1987. try {
  1988. sock.Connect ("localhost", 1253);
  1989. Assert.Fail ("ConnectHostPortListen #1");
  1990. } catch (InvalidOperationException) {
  1991. } catch {
  1992. Assert.Fail ("ConnectHostPortListen #2");
  1993. } finally {
  1994. sock.Close ();
  1995. }
  1996. }
  1997. [Test]
  1998. [Category ("NotWorking")] // Need to pick a non-IP AddressFamily that "works" on both mono and ms, this one only works on ms
  1999. public void ConnectHostPortNotIP ()
  2000. {
  2001. Socket sock = new Socket (AddressFamily.NetBios,
  2002. SocketType.Seqpacket,
  2003. ProtocolType.Unspecified);
  2004. try {
  2005. sock.Connect ("localhost", 0);
  2006. Assert.Fail ("ConnectHostPortNotIP #1");
  2007. } catch (NotSupportedException) {
  2008. } catch {
  2009. Assert.Fail ("ConnectHostPortNotIP #2");
  2010. } finally {
  2011. sock.Close ();
  2012. }
  2013. }
  2014. [Test]
  2015. [ExpectedException (typeof(ObjectDisposedException))]
  2016. public void ConnectHostPortClosed ()
  2017. {
  2018. Socket sock = new Socket (AddressFamily.InterNetwork,
  2019. SocketType.Stream,
  2020. ProtocolType.Tcp);
  2021. sock.Close ();
  2022. sock.Connect ("localhost", 0);
  2023. }
  2024. [Test]
  2025. [Category ("NotDotNet")] // "Needs XP or later"
  2026. public void Disconnect ()
  2027. {
  2028. Socket sock = new Socket (AddressFamily.InterNetwork,
  2029. SocketType.Stream,
  2030. ProtocolType.Tcp);
  2031. Socket listen = new Socket (AddressFamily.InterNetwork,
  2032. SocketType.Stream,
  2033. ProtocolType.Tcp);
  2034. IPAddress ip = IPAddress.Loopback;
  2035. IPEndPoint ep = new IPEndPoint (ip, 1255);
  2036. listen.Bind (ep);
  2037. listen.Listen (1);
  2038. sock.Connect (ip, 1255);
  2039. Assertion.AssertEquals ("Disconnect #1", true,
  2040. sock.Connected);
  2041. sock.Shutdown (SocketShutdown.Both);
  2042. sock.Disconnect (false);
  2043. Assertion.AssertEquals ("BeginDisconnect #3", false,
  2044. sock.Connected);
  2045. sock.Close ();
  2046. listen.Close ();
  2047. }
  2048. [Test]
  2049. public void DuplicateAndClose ()
  2050. {
  2051. }
  2052. [Test]
  2053. public void IOControl ()
  2054. {
  2055. }
  2056. [Test]
  2057. public void ReceiveGeneric ()
  2058. {
  2059. }
  2060. [Test]
  2061. public void ReceiveGenericSocketFlags ()
  2062. {
  2063. }
  2064. [Test]
  2065. public void ReceiveGenericSocketFlagsSocketError ()
  2066. {
  2067. }
  2068. [Test]
  2069. public void SendGeneric ()
  2070. {
  2071. }
  2072. [Test]
  2073. public void SendGenericSocketFlags ()
  2074. {
  2075. }
  2076. [Test]
  2077. public void SendGenericSocketFlagsSocketError ()
  2078. {
  2079. }
  2080. [Test]
  2081. public void ListenNotBound ()
  2082. {
  2083. Socket sock = new Socket (AddressFamily.InterNetwork,
  2084. SocketType.Stream,
  2085. ProtocolType.Tcp);
  2086. try {
  2087. sock.Listen (1);
  2088. Assert.Fail ("ListenNotBound #1");
  2089. } catch (SocketException ex) {
  2090. Assertion.AssertEquals ("ListenNotBound #2",
  2091. 10022, ex.ErrorCode);
  2092. } catch {
  2093. Assert.Fail ("ListenNotBound #3");
  2094. } finally {
  2095. sock.Close ();
  2096. }
  2097. }
  2098. #endif
  2099. static Socket CWRSocket;
  2100. static bool CWRReceiving = true;
  2101. static ManualResetEvent CWRReady = new ManualResetEvent (false);
  2102. private static void CWRReceiveThread ()
  2103. {
  2104. byte[] buf = new byte[256];
  2105. try {
  2106. CWRSocket.Receive (buf);
  2107. } catch (SocketException) {
  2108. CWRReceiving = false;
  2109. }
  2110. CWRReady.Set ();
  2111. }
  2112. [Test]
  2113. public void CloseWhileReceiving ()
  2114. {
  2115. CWRSocket = new Socket (AddressFamily.InterNetwork,
  2116. SocketType.Dgram,
  2117. ProtocolType.Udp);
  2118. CWRSocket.Bind (new IPEndPoint (IPAddress.Loopback,
  2119. 1256));
  2120. Thread recv_thread = new Thread (new ThreadStart (CWRReceiveThread));
  2121. CWRReady.Reset ();
  2122. recv_thread.Start ();
  2123. Thread.Sleep (250); /* Wait for the thread to be already receiving */
  2124. CWRSocket.Close ();
  2125. if (CWRReady.WaitOne (1000, false) == false) {
  2126. Assert.Fail ("CloseWhileReceiving wait timed out");
  2127. }
  2128. Assert.IsFalse (CWRReceiving);
  2129. }
  2130. static bool RRCLastRead = false;
  2131. static ManualResetEvent RRCReady = new ManualResetEvent (false);
  2132. private static void RRCClientThread ()
  2133. {
  2134. byte[] bytes = new byte[8];
  2135. int readbyte;
  2136. Socket sock = new Socket (AddressFamily.InterNetwork,
  2137. SocketType.Stream,
  2138. ProtocolType.Tcp);
  2139. sock.Connect (new IPEndPoint (IPAddress.Loopback,
  2140. 1257));
  2141. NetworkStream stream = new NetworkStream (sock);
  2142. readbyte = stream.ReadByte ();
  2143. Assertion.AssertEquals ("ReceiveRemoteClosed #1",
  2144. 0, readbyte);
  2145. stream.Read (bytes, 0, 0);
  2146. readbyte = stream.ReadByte ();
  2147. Assertion.AssertEquals ("ReceiveRemoteClosed #2",
  2148. 0, readbyte);
  2149. stream.Read (bytes, 0, 0);
  2150. readbyte = stream.ReadByte ();
  2151. Assertion.AssertEquals ("ReceiveRemoteClosed #3",
  2152. -1, readbyte);
  2153. sock.Close ();
  2154. RRCLastRead = true;
  2155. RRCReady.Set ();
  2156. }
  2157. [Test]
  2158. public void ReceiveRemoteClosed ()
  2159. {
  2160. Socket sock = new Socket (AddressFamily.InterNetwork,
  2161. SocketType.Stream,
  2162. ProtocolType.Tcp);
  2163. sock.Bind (new IPEndPoint (IPAddress.Loopback, 1257));
  2164. sock.Listen (1);
  2165. RRCReady.Reset ();
  2166. Thread client_thread = new Thread (new ThreadStart (RRCClientThread));
  2167. client_thread.Start ();
  2168. Socket client = sock.Accept ();
  2169. NetworkStream stream = new NetworkStream (client);
  2170. stream.WriteByte (0x00);
  2171. stream.WriteByte (0x00);
  2172. client.Close ();
  2173. sock.Close ();
  2174. RRCReady.WaitOne (1000, false);
  2175. Assert.IsTrue (RRCLastRead);
  2176. }
  2177. }
  2178. }