NetworkStreamCas.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. //
  2. // NetworkStreamCas.cs -CAS unit tests for System.Net.Sockets.NetworkStream
  3. //
  4. // Author:
  5. // Sebastien Pouliot <[email protected]>
  6. //
  7. // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. using NUnit.Framework;
  29. using System;
  30. using System.IO;
  31. using System.Net;
  32. using System.Net.Sockets;
  33. using System.Reflection;
  34. using System.Security;
  35. using System.Security.Permissions;
  36. using System.Threading;
  37. using System.Text;
  38. namespace MonoCasTests.System.Net.Sockets {
  39. [TestFixture]
  40. [Category ("CAS")]
  41. public class NetworkStreamCas {
  42. private const int timeout = 30000;
  43. private string message;
  44. static ManualResetEvent reset;
  45. static Socket socket;
  46. [TestFixtureSetUp]
  47. public void FixtureSetUp ()
  48. {
  49. reset = new ManualResetEvent (false);
  50. IPHostEntry host = Dns.Resolve ("www.google.com");
  51. IPAddress ip = host.AddressList[0];
  52. socket = new Socket (ip.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
  53. socket.Connect (new IPEndPoint (ip, 80));
  54. }
  55. [TestFixtureTearDown]
  56. public void FixtureTearDown ()
  57. {
  58. reset.Close ();
  59. socket.Close ();
  60. }
  61. [SetUp]
  62. public void SetUp ()
  63. {
  64. if (!SecurityManager.SecurityEnabled)
  65. Assert.Ignore ("SecurityManager.SecurityEnabled is OFF");
  66. }
  67. // async tests (for stack propagation)
  68. private void ReadCallback (IAsyncResult ar)
  69. {
  70. NetworkStream s = (NetworkStream)ar.AsyncState;
  71. s.EndRead (ar);
  72. try {
  73. // can we do something bad here ?
  74. Assert.IsNotNull (Environment.GetEnvironmentVariable ("USERNAME"));
  75. message = "Expected a SecurityException";
  76. }
  77. catch (SecurityException) {
  78. message = null;
  79. reset.Set ();
  80. }
  81. catch (Exception e) {
  82. message = e.ToString ();
  83. }
  84. }
  85. [Test]
  86. [EnvironmentPermission (SecurityAction.Deny, Read = "USERNAME")]
  87. [Category ("InetAccess")]
  88. public void AsyncRead ()
  89. {
  90. message = "AsyncRead";
  91. reset.Reset ();
  92. NetworkStream ns = new NetworkStream (socket, false);
  93. StreamWriter sw = new StreamWriter (ns);
  94. sw.Write ("GET / HTTP/1.0\n\n");
  95. sw.Flush ();
  96. IAsyncResult r = ns.BeginRead (new byte [1024], 0, 1024, new AsyncCallback (ReadCallback), ns);
  97. Assert.IsNotNull (r, "IAsyncResult");
  98. if (!reset.WaitOne (timeout, true))
  99. Assert.Ignore ("Timeout");
  100. Assert.IsNull (message, message);
  101. ns.Close ();
  102. }
  103. private void WriteCallback (IAsyncResult ar)
  104. {
  105. NetworkStream s = (NetworkStream)ar.AsyncState;
  106. s.EndWrite (ar);
  107. try {
  108. // can we do something bad here ?
  109. Assert.IsNotNull (Environment.GetEnvironmentVariable ("USERNAME"));
  110. message = "Expected a SecurityException";
  111. }
  112. catch (SecurityException) {
  113. message = null;
  114. reset.Set ();
  115. }
  116. catch (Exception e) {
  117. message = e.ToString ();
  118. }
  119. }
  120. [Test]
  121. [EnvironmentPermission (SecurityAction.Deny, Read = "USERNAME")]
  122. [Category ("InetAccess")]
  123. public void AsyncWrite ()
  124. {
  125. message = "AsyncWrite";
  126. reset.Reset ();
  127. NetworkStream ns = new NetworkStream (socket, false);
  128. byte[] get = Encoding.ASCII.GetBytes ("GET / HTTP/1.0\n\n");
  129. IAsyncResult r = ns.BeginWrite (get, 0, get.Length, new AsyncCallback (WriteCallback), ns);
  130. Assert.IsNotNull (r, "IAsyncResult");
  131. if (!reset.WaitOne (timeout, true))
  132. Assert.Ignore ("Timeout");
  133. Assert.IsNull (message, message);
  134. ns.Close ();
  135. }
  136. }
  137. }