SocketCas.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. //
  2. // SocketCas.cs - CAS unit tests for System.Net.WebRequest class
  3. //
  4. // Author:
  5. // Sebastien Pouliot <[email protected]>
  6. //
  7. // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
  8. //
  9. using NUnit.Framework;
  10. using System;
  11. using System.IO;
  12. using System.Net;
  13. using System.Net.Sockets;
  14. using System.Security;
  15. using System.Security.Permissions;
  16. using System.Text;
  17. using System.Threading;
  18. using MonoTests.System.Net.Sockets;
  19. namespace MonoCasTests.System.Net.Sockets {
  20. [TestFixture]
  21. [Category ("CAS")]
  22. public class SocketCas {
  23. private const int timeout = 30000;
  24. static ManualResetEvent reset;
  25. private string message;
  26. static Socket socket;
  27. static EndPoint ep;
  28. [TestFixtureSetUp]
  29. public void FixtureSetUp ()
  30. {
  31. reset = new ManualResetEvent (false);
  32. IPHostEntry host = Dns.Resolve ("www.google.com");
  33. IPAddress ip = host.AddressList[0];
  34. ep = new IPEndPoint (ip, 80);
  35. socket = new Socket (ip.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
  36. socket.Connect (ep);
  37. }
  38. [TestFixtureTearDown]
  39. public void FixtureTearDown ()
  40. {
  41. reset.Close ();
  42. }
  43. [SetUp]
  44. public void SetUp ()
  45. {
  46. if (!SecurityManager.SecurityEnabled)
  47. Assert.Ignore ("SecurityManager.SecurityEnabled is OFF");
  48. }
  49. // async tests (for stack propagation)
  50. private void AcceptCallback (IAsyncResult ar)
  51. {
  52. try {
  53. // can we do something bad here ?
  54. Assert.IsNotNull (Environment.GetEnvironmentVariable ("USERNAME"));
  55. message = "Expected a SecurityException";
  56. }
  57. catch (SecurityException) {
  58. message = null;
  59. reset.Set ();
  60. }
  61. catch (Exception e) {
  62. message = e.ToString ();
  63. }
  64. }
  65. [Test]
  66. [EnvironmentPermission (SecurityAction.Deny, Read = "USERNAME")]
  67. [Category ("InetAccess")]
  68. public void AsyncAccept ()
  69. {
  70. IPEndPoint ep = new IPEndPoint (IPAddress.Loopback, 16279);
  71. Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  72. s.Bind (ep);
  73. s.Listen (0);
  74. message = "AsyncAccept";
  75. reset.Reset ();
  76. IAsyncResult r = s.BeginAccept (new AsyncCallback (AcceptCallback), s);
  77. Assert.IsNotNull (r, "IAsyncResult");
  78. Socket c = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  79. c.Connect (ep);
  80. if (!reset.WaitOne (timeout, true))
  81. Assert.Ignore ("Timeout");
  82. Assert.IsNull (message, message);
  83. }
  84. private void ConnectCallback (IAsyncResult ar)
  85. {
  86. Socket s = (Socket)ar.AsyncState;
  87. s.EndConnect (ar);
  88. try {
  89. // can we do something bad here ?
  90. Assert.IsNotNull (Environment.GetEnvironmentVariable ("USERNAME"));
  91. message = "Expected a SecurityException";
  92. }
  93. catch (SecurityException) {
  94. message = null;
  95. reset.Set ();
  96. }
  97. catch (Exception e) {
  98. message = e.ToString ();
  99. }
  100. }
  101. [Test]
  102. [EnvironmentPermission (SecurityAction.Deny, Read = "USERNAME")]
  103. [Category ("InetAccess")]
  104. public void AsyncConnect ()
  105. {
  106. message = "AsyncConnect";
  107. reset.Reset ();
  108. Socket s = new Socket (ep.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
  109. IAsyncResult r = s.BeginConnect (ep, new AsyncCallback (ConnectCallback), s);
  110. Assert.IsNotNull (r, "IAsyncResult");
  111. if (!reset.WaitOne (timeout, true))
  112. Assert.Ignore ("Timeout");
  113. Assert.IsNull (message, message);
  114. }
  115. private void ReceiveCallback (IAsyncResult ar)
  116. {
  117. Socket s = (Socket)ar.AsyncState;
  118. s.EndReceive (ar);
  119. try {
  120. // can we do something bad here ?
  121. Assert.IsNotNull (Environment.GetEnvironmentVariable ("USERNAME"));
  122. message = "Expected a SecurityException";
  123. }
  124. catch (SecurityException) {
  125. message = null;
  126. reset.Set ();
  127. }
  128. catch (Exception e) {
  129. message = e.ToString ();
  130. }
  131. }
  132. [Test]
  133. [EnvironmentPermission (SecurityAction.Deny, Read = "USERNAME")]
  134. [Category ("InetAccess")]
  135. public void AsyncReceive ()
  136. {
  137. message = "AsyncReceive";
  138. reset.Reset ();
  139. NetworkStream ns = new NetworkStream (socket, false);
  140. StreamWriter sw = new StreamWriter (ns);
  141. sw.Write ("GET / HTTP/1.0\n\n");
  142. sw.Flush ();
  143. IAsyncResult r = socket.BeginReceive (new byte[1024], 0, 1024,
  144. SocketFlags.None, new AsyncCallback (ReceiveCallback), socket);
  145. Assert.IsNotNull (r, "IAsyncResult");
  146. if (!reset.WaitOne (timeout, true))
  147. Assert.Ignore ("Timeout");
  148. Assert.IsNull (message, message);
  149. }
  150. private void ReceiveFromCallback (IAsyncResult ar)
  151. {
  152. Socket s = (Socket)ar.AsyncState;
  153. s.EndReceiveFrom (ar, ref ep);
  154. try {
  155. // can we do something bad here ?
  156. Assert.IsNotNull (Environment.GetEnvironmentVariable ("USERNAME"));
  157. message = "Expected a SecurityException";
  158. }
  159. catch (SecurityException) {
  160. message = null;
  161. reset.Set ();
  162. }
  163. catch (Exception e) {
  164. message = e.ToString ();
  165. }
  166. }
  167. [Test]
  168. [EnvironmentPermission (SecurityAction.Deny, Read = "USERNAME")]
  169. [Category ("InetAccess")]
  170. public void AsyncReceiveFrom ()
  171. {
  172. message = "AsyncReceiveFrom";
  173. reset.Reset ();
  174. NetworkStream ns = new NetworkStream (socket, false);
  175. StreamWriter sw = new StreamWriter (ns);
  176. sw.Write ("GET / HTTP/1.0\n\n");
  177. sw.Flush ();
  178. IAsyncResult r = socket.BeginReceiveFrom (new byte[1024], 0, 1024,
  179. SocketFlags.None, ref ep, new AsyncCallback (ReceiveFromCallback), socket);
  180. Assert.IsNotNull (r, "IAsyncResult");
  181. if (!reset.WaitOne (timeout, true))
  182. Assert.Ignore ("Timeout");
  183. Assert.IsNull (message, message);
  184. }
  185. private void SendCallback (IAsyncResult ar)
  186. {
  187. Socket s = (Socket)ar.AsyncState;
  188. s.EndSend (ar);
  189. try {
  190. // can we do something bad here ?
  191. Assert.IsNotNull (Environment.GetEnvironmentVariable ("USERNAME"));
  192. message = "Expected a SecurityException";
  193. }
  194. catch (SecurityException) {
  195. message = null;
  196. reset.Set ();
  197. }
  198. catch (Exception e) {
  199. message = e.ToString ();
  200. }
  201. }
  202. [Test]
  203. [EnvironmentPermission (SecurityAction.Deny, Read = "USERNAME")]
  204. [Category ("InetAccess")]
  205. public void AsyncSend ()
  206. {
  207. message = "AsyncSend";
  208. reset.Reset ();
  209. byte[] get = Encoding.ASCII.GetBytes ("GET / HTTP/1.0\n\n");
  210. IAsyncResult r = socket.BeginSend (get, 0, get.Length, SocketFlags.None,
  211. new AsyncCallback (SendCallback), socket);
  212. Assert.IsNotNull (r, "IAsyncResult");
  213. if (!reset.WaitOne (timeout, true))
  214. Assert.Ignore ("Timeout");
  215. Assert.IsNull (message, message);
  216. }
  217. private void SendToCallback (IAsyncResult ar)
  218. {
  219. Socket s = (Socket)ar.AsyncState;
  220. s.EndSendTo (ar);
  221. try {
  222. // can we do something bad here ?
  223. Assert.IsNotNull (Environment.GetEnvironmentVariable ("USERNAME"));
  224. message = "Expected a SecurityException";
  225. }
  226. catch (SecurityException) {
  227. message = null;
  228. reset.Set ();
  229. }
  230. catch (Exception e) {
  231. message = e.ToString ();
  232. }
  233. }
  234. [Test]
  235. [EnvironmentPermission (SecurityAction.Deny, Read = "USERNAME")]
  236. [Category ("InetAccess")]
  237. public void AsyncSendTo ()
  238. {
  239. message = "AsyncSendTo";
  240. reset.Reset ();
  241. byte[] get = Encoding.ASCII.GetBytes ("GET / HTTP/1.0\n\n");
  242. IAsyncResult r = socket.BeginSendTo (get, 0, get.Length, SocketFlags.None,
  243. ep, new AsyncCallback (SendToCallback), socket);
  244. Assert.IsNotNull (r, "IAsyncResult");
  245. if (!reset.WaitOne (timeout, true))
  246. Assert.Ignore ("Timeout");
  247. Assert.IsNull (message, message);
  248. }
  249. }
  250. }