SocketCas.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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. public void AsyncAccept ()
  68. {
  69. IPEndPoint ep = new IPEndPoint (IPAddress.Loopback, 16279);
  70. Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  71. s.Bind (ep);
  72. s.Listen (0);
  73. message = "AsyncAccept";
  74. reset.Reset ();
  75. IAsyncResult r = s.BeginAccept (new AsyncCallback (AcceptCallback), s);
  76. Assert.IsNotNull (r, "IAsyncResult");
  77. Socket c = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  78. c.Connect (ep);
  79. if (!reset.WaitOne (timeout, true))
  80. Assert.Ignore ("Timeout");
  81. Assert.IsNull (message, message);
  82. }
  83. private void ConnectCallback (IAsyncResult ar)
  84. {
  85. Socket s = (Socket)ar.AsyncState;
  86. s.EndConnect (ar);
  87. try {
  88. // can we do something bad here ?
  89. Assert.IsNotNull (Environment.GetEnvironmentVariable ("USERNAME"));
  90. message = "Expected a SecurityException";
  91. }
  92. catch (SecurityException) {
  93. message = null;
  94. reset.Set ();
  95. }
  96. catch (Exception e) {
  97. message = e.ToString ();
  98. }
  99. }
  100. [Test]
  101. [EnvironmentPermission (SecurityAction.Deny, Read = "USERNAME")]
  102. public void AsyncConnect ()
  103. {
  104. message = "AsyncConnect";
  105. reset.Reset ();
  106. Socket s = new Socket (ep.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
  107. IAsyncResult r = s.BeginConnect (ep, new AsyncCallback (ConnectCallback), s);
  108. Assert.IsNotNull (r, "IAsyncResult");
  109. if (!reset.WaitOne (timeout, true))
  110. Assert.Ignore ("Timeout");
  111. Assert.IsNull (message, message);
  112. }
  113. private void ReceiveCallback (IAsyncResult ar)
  114. {
  115. Socket s = (Socket)ar.AsyncState;
  116. s.EndReceive (ar);
  117. try {
  118. // can we do something bad here ?
  119. Assert.IsNotNull (Environment.GetEnvironmentVariable ("USERNAME"));
  120. message = "Expected a SecurityException";
  121. }
  122. catch (SecurityException) {
  123. message = null;
  124. reset.Set ();
  125. }
  126. catch (Exception e) {
  127. message = e.ToString ();
  128. }
  129. }
  130. [Test]
  131. [EnvironmentPermission (SecurityAction.Deny, Read = "USERNAME")]
  132. public void AsyncReceive ()
  133. {
  134. message = "AsyncReceive";
  135. reset.Reset ();
  136. NetworkStream ns = new NetworkStream (socket, false);
  137. StreamWriter sw = new StreamWriter (ns);
  138. sw.Write ("GET / HTTP/1.0\n\n");
  139. sw.Flush ();
  140. IAsyncResult r = socket.BeginReceive (new byte[1024], 0, 1024,
  141. SocketFlags.None, new AsyncCallback (ReceiveCallback), socket);
  142. Assert.IsNotNull (r, "IAsyncResult");
  143. if (!reset.WaitOne (timeout, true))
  144. Assert.Ignore ("Timeout");
  145. Assert.IsNull (message, message);
  146. }
  147. private void ReceiveFromCallback (IAsyncResult ar)
  148. {
  149. Socket s = (Socket)ar.AsyncState;
  150. s.EndReceiveFrom (ar, ref ep);
  151. try {
  152. // can we do something bad here ?
  153. Assert.IsNotNull (Environment.GetEnvironmentVariable ("USERNAME"));
  154. message = "Expected a SecurityException";
  155. }
  156. catch (SecurityException) {
  157. message = null;
  158. reset.Set ();
  159. }
  160. catch (Exception e) {
  161. message = e.ToString ();
  162. }
  163. }
  164. [Test]
  165. [EnvironmentPermission (SecurityAction.Deny, Read = "USERNAME")]
  166. public void AsyncReceiveFrom ()
  167. {
  168. message = "AsyncReceiveFrom";
  169. reset.Reset ();
  170. NetworkStream ns = new NetworkStream (socket, false);
  171. StreamWriter sw = new StreamWriter (ns);
  172. sw.Write ("GET / HTTP/1.0\n\n");
  173. sw.Flush ();
  174. IAsyncResult r = socket.BeginReceiveFrom (new byte[1024], 0, 1024,
  175. SocketFlags.None, ref ep, new AsyncCallback (ReceiveFromCallback), socket);
  176. Assert.IsNotNull (r, "IAsyncResult");
  177. if (!reset.WaitOne (timeout, true))
  178. Assert.Ignore ("Timeout");
  179. Assert.IsNull (message, message);
  180. }
  181. private void SendCallback (IAsyncResult ar)
  182. {
  183. Socket s = (Socket)ar.AsyncState;
  184. s.EndSend (ar);
  185. try {
  186. // can we do something bad here ?
  187. Assert.IsNotNull (Environment.GetEnvironmentVariable ("USERNAME"));
  188. message = "Expected a SecurityException";
  189. }
  190. catch (SecurityException) {
  191. message = null;
  192. reset.Set ();
  193. }
  194. catch (Exception e) {
  195. message = e.ToString ();
  196. }
  197. }
  198. [Test]
  199. [EnvironmentPermission (SecurityAction.Deny, Read = "USERNAME")]
  200. public void AsyncSend ()
  201. {
  202. message = "AsyncSend";
  203. reset.Reset ();
  204. byte[] get = Encoding.ASCII.GetBytes ("GET / HTTP/1.0\n\n");
  205. IAsyncResult r = socket.BeginSend (get, 0, get.Length, SocketFlags.None,
  206. new AsyncCallback (SendCallback), socket);
  207. Assert.IsNotNull (r, "IAsyncResult");
  208. if (!reset.WaitOne (timeout, true))
  209. Assert.Ignore ("Timeout");
  210. Assert.IsNull (message, message);
  211. }
  212. private void SendToCallback (IAsyncResult ar)
  213. {
  214. Socket s = (Socket)ar.AsyncState;
  215. s.EndSendTo (ar);
  216. try {
  217. // can we do something bad here ?
  218. Assert.IsNotNull (Environment.GetEnvironmentVariable ("USERNAME"));
  219. message = "Expected a SecurityException";
  220. }
  221. catch (SecurityException) {
  222. message = null;
  223. reset.Set ();
  224. }
  225. catch (Exception e) {
  226. message = e.ToString ();
  227. }
  228. }
  229. [Test]
  230. [EnvironmentPermission (SecurityAction.Deny, Read = "USERNAME")]
  231. public void AsyncSendTo ()
  232. {
  233. message = "AsyncSendTo";
  234. reset.Reset ();
  235. byte[] get = Encoding.ASCII.GetBytes ("GET / HTTP/1.0\n\n");
  236. IAsyncResult r = socket.BeginSendTo (get, 0, get.Length, SocketFlags.None,
  237. ep, new AsyncCallback (SendToCallback), socket);
  238. Assert.IsNotNull (r, "IAsyncResult");
  239. if (!reset.WaitOne (timeout, true))
  240. Assert.Ignore ("Timeout");
  241. Assert.IsNull (message, message);
  242. }
  243. }
  244. }