HttpWebRequestCas.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. //
  2. // HttpWebRequestCas.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.Security;
  14. using System.Security.Permissions;
  15. using System.Threading;
  16. namespace MonoCasTests.System.Net {
  17. [TestFixture]
  18. [Category ("CAS")]
  19. public class HttpWebRequestCas {
  20. private const int timeout = 30000;
  21. static ManualResetEvent reset;
  22. private string message;
  23. private string uri = "http://www.google.com";
  24. [TestFixtureSetUp]
  25. public void FixtureSetUp ()
  26. {
  27. reset = new ManualResetEvent (false);
  28. }
  29. [TestFixtureTearDown]
  30. public void FixtureTearDown ()
  31. {
  32. reset.Close ();
  33. }
  34. [SetUp]
  35. public void SetUp ()
  36. {
  37. if (!SecurityManager.SecurityEnabled)
  38. Assert.Ignore ("SecurityManager.SecurityEnabled is OFF");
  39. }
  40. // async tests (for stack propagation)
  41. private void GetRequestStreamCallback (IAsyncResult ar)
  42. {
  43. HttpWebRequest hwr = (HttpWebRequest)ar.AsyncState;
  44. hwr.EndGetRequestStream (ar);
  45. try {
  46. // can we do something bad here ?
  47. Assert.IsNotNull (Environment.GetEnvironmentVariable ("USERNAME"));
  48. message = "Expected a SecurityException";
  49. }
  50. catch (SecurityException) {
  51. message = null;
  52. reset.Set ();
  53. }
  54. catch (Exception e) {
  55. message = e.ToString ();
  56. }
  57. }
  58. [Test]
  59. [EnvironmentPermission (SecurityAction.Deny, Read = "USERNAME")]
  60. public void AsyncGetRequestStream ()
  61. {
  62. HttpWebRequest w = (HttpWebRequest)WebRequest.Create (uri);
  63. w.Method = "PUT";
  64. message = "AsyncGetRequestStream";
  65. reset.Reset ();
  66. IAsyncResult r = w.BeginGetRequestStream (new AsyncCallback (GetRequestStreamCallback), w);
  67. Assert.IsNotNull (r, "IAsyncResult");
  68. if (!reset.WaitOne (timeout, true))
  69. Assert.Ignore ("Timeout");
  70. Assert.IsNull (message, message);
  71. }
  72. private void GetResponseCallback (IAsyncResult ar)
  73. {
  74. HttpWebRequest hwr = (HttpWebRequest)ar.AsyncState;
  75. hwr.EndGetResponse (ar);
  76. try {
  77. // can we do something bad here ?
  78. Assert.IsNotNull (Environment.GetEnvironmentVariable ("USERNAME"));
  79. message = "Expected a SecurityException";
  80. }
  81. catch (SecurityException) {
  82. message = null;
  83. reset.Set ();
  84. }
  85. catch (Exception e) {
  86. message = e.ToString ();
  87. }
  88. }
  89. [Test]
  90. [EnvironmentPermission (SecurityAction.Deny, Read = "USERNAME")]
  91. public void AsyncGetResponse ()
  92. {
  93. HttpWebRequest w = (HttpWebRequest)WebRequest.Create (uri);
  94. message = "AsyncGetResponse";
  95. reset.Reset ();
  96. IAsyncResult r = w.BeginGetResponse (new AsyncCallback (GetResponseCallback), w);
  97. Assert.IsNotNull (r, "IAsyncResult");
  98. if (!reset.WaitOne (timeout, true))
  99. Assert.Ignore ("Timeout");
  100. Assert.IsNull (message, message);
  101. }
  102. }
  103. }