SecurityContextTest.cs 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. //
  2. // SecurityContextTest.cs - NUnit tests for SecurityContext
  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. #if FEATURE_COMPRESSEDSTACK
  29. using System;
  30. using System.Security;
  31. using System.Security.Principal;
  32. using System.Threading;
  33. using NUnit.Framework;
  34. namespace MonoTests.System.Security {
  35. [TestFixture]
  36. public class SecurityContextTest {
  37. static bool success;
  38. static void Callback (object o)
  39. {
  40. success = (bool) o;
  41. }
  42. [SetUp]
  43. public void SetUp ()
  44. {
  45. success = false;
  46. }
  47. [TearDown]
  48. public void TearDown ()
  49. {
  50. if (SecurityContext.IsFlowSuppressed () || SecurityContext.IsWindowsIdentityFlowSuppressed ())
  51. SecurityContext.RestoreFlow ();
  52. }
  53. [Test]
  54. public void Capture ()
  55. {
  56. SecurityContext sc = SecurityContext.Capture ();
  57. Assert.IsNotNull (sc, "Capture");
  58. AsyncFlowControl afc = SecurityContext.SuppressFlow ();
  59. Assert.IsTrue (SecurityContext.IsFlowSuppressed (), "IsFlowSuppressed-1");
  60. try {
  61. sc = SecurityContext.Capture ();
  62. Assert.IsNull (sc, "Capture with SuppressFlow");
  63. }
  64. finally {
  65. afc.Undo ();
  66. }
  67. afc = SecurityContext.SuppressFlowWindowsIdentity ();
  68. Assert.IsTrue (SecurityContext.IsWindowsIdentityFlowSuppressed (), "IsWindowsIdentityFlowSuppressed-1");
  69. try {
  70. sc = SecurityContext.Capture ();
  71. Assert.IsNotNull (sc, "Capture with SuppressFlowWindowsIdentity");
  72. }
  73. finally {
  74. afc.Undo ();
  75. }
  76. }
  77. [Test]
  78. public void Copy ()
  79. {
  80. SecurityContext sc = SecurityContext.Capture ();
  81. Assert.IsNotNull (sc, "Capture");
  82. SecurityContext copy = sc.CreateCopy ();
  83. Assert.IsNotNull (copy, "Copy of Capture");
  84. Assert.IsFalse (sc.Equals (copy));
  85. Assert.IsFalse (copy.Equals (sc));
  86. Assert.IsFalse (Object.ReferenceEquals (sc, copy));
  87. SecurityContext copy2nd = copy.CreateCopy ();
  88. Assert.IsNotNull (copy2nd, "2nd level copy of Capture");
  89. }
  90. [Test]
  91. public void IsFlowSuppressed ()
  92. {
  93. Assert.IsFalse (SecurityContext.IsFlowSuppressed (), "IsFlowSuppressed-1");
  94. AsyncFlowControl afc = SecurityContext.SuppressFlowWindowsIdentity ();
  95. Assert.IsFalse (SecurityContext.IsFlowSuppressed (), "IsFlowSuppressed-2");
  96. afc.Undo ();
  97. afc = SecurityContext.SuppressFlow ();
  98. Assert.IsTrue (SecurityContext.IsFlowSuppressed (), "IsFlowSuppressed-3");
  99. afc.Undo ();
  100. Assert.IsFalse (SecurityContext.IsFlowSuppressed (), "IsFlowSuppressed-4");
  101. }
  102. [Test]
  103. public void IsWindowsIdentityFlowSuppressed ()
  104. {
  105. Assert.IsFalse (SecurityContext.IsWindowsIdentityFlowSuppressed (), "IsWindowsIdentityFlowSuppressed-1");
  106. AsyncFlowControl afc = SecurityContext.SuppressFlow ();
  107. Assert.IsTrue (SecurityContext.IsWindowsIdentityFlowSuppressed (), "IsWindowsIdentityFlowSuppressed-2");
  108. afc.Undo ();
  109. afc = SecurityContext.SuppressFlowWindowsIdentity ();
  110. Assert.IsTrue (SecurityContext.IsWindowsIdentityFlowSuppressed (), "IsWindowsIdentityFlowSuppressed-3");
  111. afc.Undo ();
  112. Assert.IsFalse (SecurityContext.IsWindowsIdentityFlowSuppressed (), "IsWindowsIdentityFlowSuppressed-4");
  113. }
  114. [Test]
  115. [ExpectedException (typeof (InvalidOperationException))]
  116. public void RestoreFlow_None ()
  117. {
  118. SecurityContext.RestoreFlow ();
  119. }
  120. [Test]
  121. public void RestoreFlow_SuppressFlow ()
  122. {
  123. Assert.IsFalse (SecurityContext.IsFlowSuppressed (), "IsFlowSuppressed-1");
  124. SecurityContext.SuppressFlow ();
  125. Assert.IsTrue (SecurityContext.IsFlowSuppressed (), "IsFlowSuppressed-2");
  126. SecurityContext.RestoreFlow ();
  127. Assert.IsFalse (SecurityContext.IsFlowSuppressed (), "IsFlowSuppressed-3");
  128. }
  129. [Test]
  130. public void RestoreFlow_SuppressFlowWindowsIdentity ()
  131. {
  132. Assert.IsFalse (SecurityContext.IsWindowsIdentityFlowSuppressed (), "IsWindowsIdentityFlowSuppressed-1");
  133. SecurityContext.SuppressFlowWindowsIdentity ();
  134. Assert.IsTrue (SecurityContext.IsWindowsIdentityFlowSuppressed (), "IsWindowsIdentityFlowSuppressed-2");
  135. SecurityContext.RestoreFlow ();
  136. Assert.IsFalse (SecurityContext.IsWindowsIdentityFlowSuppressed (), "IsWindowsIdentityFlowSuppressed-3");
  137. }
  138. [Test]
  139. public void Run ()
  140. {
  141. Assert.IsFalse (success, "pre-check");
  142. SecurityContext.Run (SecurityContext.Capture (), new ContextCallback (Callback), true);
  143. Assert.IsTrue (success, "post-check");
  144. }
  145. [Test]
  146. [ExpectedException (typeof (InvalidOperationException))]
  147. public void Run_SuppressFlow ()
  148. {
  149. Assert.IsFalse (SecurityContext.IsFlowSuppressed ());
  150. AsyncFlowControl afc = SecurityContext.SuppressFlow ();
  151. Assert.IsTrue (SecurityContext.IsFlowSuppressed ());
  152. try {
  153. SecurityContext.Run (SecurityContext.Capture (), new ContextCallback (Callback), "Hello world.");
  154. }
  155. finally {
  156. afc.Undo ();
  157. }
  158. }
  159. [Test]
  160. public void SuppressFlow ()
  161. {
  162. Assert.IsFalse (SecurityContext.IsFlowSuppressed (), "IsFlowSuppressed-1");
  163. Assert.IsFalse (SecurityContext.IsWindowsIdentityFlowSuppressed (), "IsWindowsIdentityFlowSuppressed-1");
  164. AsyncFlowControl afc = SecurityContext.SuppressFlow ();
  165. Assert.IsTrue (SecurityContext.IsFlowSuppressed (), "IsFlowSuppressed-3");
  166. Assert.IsTrue (SecurityContext.IsWindowsIdentityFlowSuppressed (), "IsWindowsIdentityFlowSuppressed-3");
  167. afc.Undo ();
  168. Assert.IsFalse (SecurityContext.IsFlowSuppressed (), "IsFlowSuppressed-4");
  169. Assert.IsFalse (SecurityContext.IsWindowsIdentityFlowSuppressed (), "IsWindowsIdentityFlowSuppressed-4");
  170. }
  171. [Test]
  172. public void SuppressFlowWindowsIdentity ()
  173. {
  174. Assert.IsFalse (SecurityContext.IsFlowSuppressed (), "IsFlowSuppressed-1");
  175. Assert.IsFalse (SecurityContext.IsWindowsIdentityFlowSuppressed (), "IsWindowsIdentityFlowSuppressed-1");
  176. AsyncFlowControl afc = SecurityContext.SuppressFlowWindowsIdentity ();
  177. Assert.IsFalse (SecurityContext.IsFlowSuppressed (), "IsFlowSuppressed-2");
  178. Assert.IsTrue (SecurityContext.IsWindowsIdentityFlowSuppressed (), "IsWindowsIdentityFlowSuppressed-2");
  179. afc.Undo ();
  180. Assert.IsFalse (SecurityContext.IsFlowSuppressed (), "IsFlowSuppressed-4");
  181. Assert.IsFalse (SecurityContext.IsWindowsIdentityFlowSuppressed (), "IsWindowsIdentityFlowSuppressed-4");
  182. }
  183. [Test]
  184. public void SuppressFlow_Both ()
  185. {
  186. Assert.IsFalse (SecurityContext.IsFlowSuppressed (), "IsFlowSuppressed-1");
  187. Assert.IsFalse (SecurityContext.IsWindowsIdentityFlowSuppressed (), "IsWindowsIdentityFlowSuppressed-1");
  188. AsyncFlowControl afc = SecurityContext.SuppressFlowWindowsIdentity ();
  189. Assert.IsFalse (SecurityContext.IsFlowSuppressed (), "IsFlowSuppressed-2");
  190. Assert.IsTrue (SecurityContext.IsWindowsIdentityFlowSuppressed (), "IsWindowsIdentityFlowSuppressed-2");
  191. AsyncFlowControl afc2 = SecurityContext.SuppressFlow ();
  192. Assert.IsTrue (SecurityContext.IsFlowSuppressed (), "IsFlowSuppressed-2");
  193. Assert.IsTrue (SecurityContext.IsWindowsIdentityFlowSuppressed (), "IsWindowsIdentityFlowSuppressed-2");
  194. afc2.Undo ();
  195. // note: afc2 Undo return to the original (not the previous) state
  196. Assert.IsFalse (SecurityContext.IsFlowSuppressed (), "IsFlowSuppressed-2");
  197. Assert.IsFalse (SecurityContext.IsWindowsIdentityFlowSuppressed (), "IsWindowsIdentityFlowSuppressed-2");
  198. }
  199. [Test]
  200. [ExpectedException (typeof (InvalidOperationException))]
  201. public void SuppressFlow_Both_Undo ()
  202. {
  203. Assert.IsFalse (SecurityContext.IsFlowSuppressed (), "IsFlowSuppressed-1");
  204. Assert.IsFalse (SecurityContext.IsWindowsIdentityFlowSuppressed (), "IsWindowsIdentityFlowSuppressed-1");
  205. AsyncFlowControl afc = SecurityContext.SuppressFlowWindowsIdentity ();
  206. Assert.IsFalse (SecurityContext.IsFlowSuppressed (), "IsFlowSuppressed-2");
  207. Assert.IsTrue (SecurityContext.IsWindowsIdentityFlowSuppressed (), "IsWindowsIdentityFlowSuppressed-2");
  208. AsyncFlowControl afc2 = SecurityContext.SuppressFlow ();
  209. Assert.IsTrue (SecurityContext.IsFlowSuppressed (), "IsFlowSuppressed-2");
  210. Assert.IsTrue (SecurityContext.IsWindowsIdentityFlowSuppressed (), "IsWindowsIdentityFlowSuppressed-2");
  211. afc2.Undo ();
  212. // note: afc2 Undo return to the original (not the previous) state
  213. Assert.IsFalse (SecurityContext.IsFlowSuppressed (), "IsFlowSuppressed-2");
  214. Assert.IsFalse (SecurityContext.IsWindowsIdentityFlowSuppressed (), "IsWindowsIdentityFlowSuppressed-2");
  215. // we can't use the first AsyncFlowControl
  216. afc.Undo ();
  217. }
  218. // effects of ExecutionContext on SecurityContext
  219. [Test]
  220. public void ExecutionContext_SuppressFlow ()
  221. {
  222. Assert.IsFalse (ExecutionContext.IsFlowSuppressed (), "IsFlowSuppressed-1");
  223. AsyncFlowControl afc = ExecutionContext.SuppressFlow ();
  224. try {
  225. Assert.IsTrue (ExecutionContext.IsFlowSuppressed (), "IsFlowSuppressed-2");
  226. Assert.IsFalse (SecurityContext.IsFlowSuppressed (), "IsFlowSuppressed-3");
  227. Assert.IsNotNull (SecurityContext.Capture (), "Capture");
  228. }
  229. finally {
  230. afc.Undo ();
  231. }
  232. }
  233. [Test]
  234. public void WindowsIdentity_ ()
  235. {
  236. Thread.CurrentPrincipal = new WindowsPrincipal (WindowsIdentity.GetCurrent ());
  237. SecurityContext sc = SecurityContext.Capture ();
  238. Assert.IsNotNull (sc, "Capture");
  239. }
  240. }
  241. }
  242. #endif