TempFileCollectionCas.cs 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. //
  2. // TempFileCollectionCas.cs
  3. // - CAS unit tests for System.CodeDom.Compiler.TempFileCollection
  4. //
  5. // Author:
  6. // Sebastien Pouliot <[email protected]>
  7. //
  8. // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining
  11. // a copy of this software and associated documentation files (the
  12. // "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish,
  14. // distribute, sublicense, and/or sell copies of the Software, and to
  15. // permit persons to whom the Software is furnished to do so, subject to
  16. // the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. using NUnit.Framework;
  30. using System;
  31. using System.CodeDom.Compiler;
  32. using System.Collections;
  33. using System.IO;
  34. using System.Reflection;
  35. using System.Security;
  36. using System.Security.Permissions;
  37. namespace MonoCasTests.System.CodeDom.Compiler {
  38. [TestFixture]
  39. [Category ("CAS")]
  40. public class TempFileCollectionCas {
  41. private string temp;
  42. private string[] array;
  43. [TestFixtureSetUp]
  44. public void FixtureSetUp ()
  45. {
  46. // at full trust
  47. temp = Path.GetTempPath ();
  48. array = new string[1];
  49. }
  50. [SetUp]
  51. public void SetUp ()
  52. {
  53. if (!SecurityManager.SecurityEnabled)
  54. Assert.Ignore ("SecurityManager.SecurityEnabled is OFF");
  55. }
  56. [Test]
  57. [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
  58. public void Constructor0_Deny_Unrestricted ()
  59. {
  60. TempFileCollection tfc = new TempFileCollection ();
  61. Assert.AreEqual (0, tfc.Count, "Count");
  62. Assert.IsFalse (tfc.KeepFiles, "KeepFiles");
  63. Assert.AreEqual (String.Empty, tfc.TempDir, "TempDir");
  64. tfc.AddFile ("main.cs", false);
  65. tfc.CopyTo (array, 0);
  66. tfc.Delete ();
  67. (tfc as IDisposable).Dispose ();
  68. }
  69. [Test]
  70. [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
  71. public void Constructor1_Deny_Unrestricted ()
  72. {
  73. TempFileCollection tfc = new TempFileCollection (temp);
  74. Assert.AreEqual (0, tfc.Count, "Count");
  75. Assert.IsFalse (tfc.KeepFiles, "KeepFiles");
  76. Assert.AreEqual (temp, tfc.TempDir, "TempDir");
  77. tfc.AddFile ("main.cs", false);
  78. tfc.CopyTo (array, 0);
  79. tfc.Delete ();
  80. (tfc as IDisposable).Dispose ();
  81. }
  82. [Test]
  83. [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
  84. public void Constructor2_Deny_Unrestricted ()
  85. {
  86. TempFileCollection tfc = new TempFileCollection (temp, true);
  87. Assert.AreEqual (0, tfc.Count, "Count");
  88. Assert.IsTrue (tfc.KeepFiles, "KeepFiles");
  89. Assert.AreEqual (temp, tfc.TempDir, "TempDir");
  90. tfc.AddFile ("main.cs", false);
  91. tfc.CopyTo (array, 0);
  92. tfc.Delete ();
  93. (tfc as IDisposable).Dispose ();
  94. }
  95. [EnvironmentPermission (SecurityAction.PermitOnly, Unrestricted = true)]
  96. private void Cache (TempFileCollection tfc)
  97. {
  98. Assert.IsNotNull (tfc.BasePath, "BasePath-Restricted");
  99. // no failure because the BasePath was calculated when
  100. // unrestricted and cached for further calls. This is
  101. // design tradeoff between security and performance but
  102. // shouldn't occurs much in real life (but it's worth
  103. // keeping in mind ;-).
  104. }
  105. [Test]
  106. public void BasePath_Caching ()
  107. {
  108. TempFileCollection tfc = new TempFileCollection ();
  109. Assert.IsNotNull (tfc.BasePath, "BasePath-Unrestricted");
  110. Cache (tfc);
  111. }
  112. [Test]
  113. [EnvironmentPermission (SecurityAction.PermitOnly, Unrestricted = true)]
  114. [ExpectedException (typeof (SecurityException))]
  115. public void BasePath_PermitOnly_EnvironmentPermission ()
  116. {
  117. TempFileCollection tfc = new TempFileCollection ();
  118. // good but not enough
  119. Assert.IsNotNull (tfc.BasePath, "BasePath");
  120. }
  121. [Test]
  122. [EnvironmentPermission (SecurityAction.Deny, Read = "Mono")]
  123. [ExpectedException (typeof (SecurityException))]
  124. #if ONLY_1_1
  125. [Category ("NotDotNet")] // MS doesn't check for Environment under 1.x
  126. #endif
  127. public void BasePath_Deny_EnvironmentPermission ()
  128. {
  129. TempFileCollection tfc = new TempFileCollection ();
  130. // requires Unrestricted Environment access
  131. Assert.IsNotNull (tfc.BasePath, "BasePath");
  132. }
  133. [Test]
  134. [FileIOPermission (SecurityAction.PermitOnly, Unrestricted = true)]
  135. [ExpectedException (typeof (SecurityException))]
  136. #if ONLY_1_1
  137. [Category ("NotDotNet")] // MS doesn't check for Environment under 1.x
  138. #endif
  139. public void BasePath_PermitOnly_FileIOPermission ()
  140. {
  141. TempFileCollection tfc = new TempFileCollection ();
  142. // good but not enough
  143. Assert.IsNotNull (tfc.BasePath, "BasePath");
  144. }
  145. [Test]
  146. [FileIOPermission (SecurityAction.Deny, Unrestricted = true)]
  147. [ExpectedException (typeof (SecurityException))]
  148. public void BasePath_Deny_FileIOPermission ()
  149. {
  150. TempFileCollection tfc = new TempFileCollection ();
  151. // requires path access
  152. Assert.IsNotNull (tfc.BasePath, "BasePath");
  153. }
  154. [Test]
  155. [EnvironmentPermission (SecurityAction.PermitOnly, Unrestricted = true)]
  156. [ExpectedException (typeof (SecurityException))]
  157. public void AddExtension_PermitOnly_EnvironmentPermission ()
  158. {
  159. TempFileCollection tfc = new TempFileCollection ();
  160. // good but not enough
  161. tfc.AddExtension (".cs");
  162. }
  163. [Test]
  164. [EnvironmentPermission (SecurityAction.Deny, Read = "Mono")]
  165. [ExpectedException (typeof (SecurityException))]
  166. #if ONLY_1_1
  167. [Category ("NotDotNet")] // MS doesn't check for Environment under 1.x
  168. #endif
  169. public void AddExtension_Deny_EnvironmentPermission ()
  170. {
  171. TempFileCollection tfc = new TempFileCollection ();
  172. // requires Unrestricted Environment access
  173. tfc.AddExtension (".cs");
  174. }
  175. [Test]
  176. [FileIOPermission (SecurityAction.PermitOnly, Unrestricted = true)]
  177. [ExpectedException (typeof (SecurityException))]
  178. #if ONLY_1_1
  179. [Category ("NotDotNet")] // MS doesn't check for Environment under 1.x
  180. #endif
  181. public void AddExtension_PermitOnly_FileIOPermission ()
  182. {
  183. TempFileCollection tfc = new TempFileCollection ();
  184. // good but not enough
  185. tfc.AddExtension (".cs");
  186. }
  187. [Test]
  188. [FileIOPermission (SecurityAction.Deny, Unrestricted = true)]
  189. [ExpectedException (typeof (SecurityException))]
  190. public void AddExtension_Deny_FileIOPermission ()
  191. {
  192. TempFileCollection tfc = new TempFileCollection ();
  193. // requires path access
  194. tfc.AddExtension (".cs");
  195. }
  196. [Test]
  197. [EnvironmentPermission (SecurityAction.PermitOnly, Unrestricted = true)]
  198. [ExpectedException (typeof (SecurityException))]
  199. public void AddExtension2_PermitOnly_EnvironmentPermission ()
  200. {
  201. TempFileCollection tfc = new TempFileCollection ();
  202. // good but not enough
  203. tfc.AddExtension (".cx", true);
  204. }
  205. [Test]
  206. [EnvironmentPermission (SecurityAction.Deny, Read = "Mono")]
  207. [ExpectedException (typeof (SecurityException))]
  208. #if ONLY_1_1
  209. [Category ("NotDotNet")] // MS doesn't check for Environment under 1.x
  210. #endif
  211. public void AddExtension2_Deny_EnvironmentPermission ()
  212. {
  213. TempFileCollection tfc = new TempFileCollection ();
  214. // requires Unrestricted Environment access
  215. tfc.AddExtension (".cx", true);
  216. }
  217. [Test]
  218. [FileIOPermission (SecurityAction.PermitOnly, Unrestricted = true)]
  219. [ExpectedException (typeof (SecurityException))]
  220. #if ONLY_1_1
  221. [Category ("NotDotNet")] // MS doesn't check for Environment under 1.x
  222. #endif
  223. public void AddExtension2_PermitOnly_FileIOPermission ()
  224. {
  225. TempFileCollection tfc = new TempFileCollection ();
  226. // good but not enough
  227. tfc.AddExtension (".cx", true);
  228. }
  229. [Test]
  230. [FileIOPermission (SecurityAction.Deny, Unrestricted = true)]
  231. [ExpectedException (typeof (SecurityException))]
  232. public void AddExtension2_Deny_FileIOPermission ()
  233. {
  234. TempFileCollection tfc = new TempFileCollection ();
  235. // requires path access
  236. tfc.AddExtension (".cx", true);
  237. }
  238. [Test]
  239. [FileIOPermission (SecurityAction.PermitOnly, Unrestricted = true)]
  240. [EnvironmentPermission (SecurityAction.PermitOnly, Unrestricted = true)]
  241. public void PermitOnly_FileIOPermission_EnvironmentPermission ()
  242. {
  243. TempFileCollection tfc = new TempFileCollection ();
  244. // ok
  245. Assert.IsNotNull (tfc.BasePath, "BasePath");
  246. tfc.AddExtension (".cs");
  247. tfc.AddExtension (".cx", true);
  248. // both AddExtension methods depends on BasePath
  249. Assert.AreEqual (String.Empty, tfc.TempDir, "TempDir");
  250. // and that last one is *important*
  251. }
  252. [Test]
  253. [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
  254. public void ICollection_Deny_Unrestricted ()
  255. {
  256. ICollection coll = (ICollection) new TempFileCollection ();
  257. Assert.AreEqual (0, coll.Count, "Count");
  258. Assert.IsNull (coll.SyncRoot, "SyncRoot");
  259. Assert.IsFalse (coll.IsSynchronized, "IsSynchronized");
  260. coll.CopyTo (array, 0);
  261. }
  262. [Test]
  263. [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
  264. public void IEnumerable_Deny_Unrestricted ()
  265. {
  266. IEnumerable e = (IEnumerable) new TempFileCollection ();
  267. Assert.IsNotNull (e.GetEnumerator (), "GetEnumerator");
  268. }
  269. [Test]
  270. public void LinkDemand_No_Restriction ()
  271. {
  272. ConstructorInfo ci = typeof (TempFileCollection).GetConstructor (new Type[0]);
  273. Assert.IsNotNull (ci, "default .ctor");
  274. Assert.IsNotNull (ci.Invoke (null), "invoke");
  275. }
  276. [Test]
  277. [EnvironmentPermission (SecurityAction.Deny, Read = "Mono")]
  278. [ExpectedException (typeof (SecurityException))]
  279. public void LinkDemand_Deny_Unrestricted ()
  280. {
  281. // denying anything results in a non unrestricted permission set
  282. ConstructorInfo ci = typeof (TempFileCollection).GetConstructor (new Type[0]);
  283. Assert.IsNotNull (ci, "default .ctor");
  284. Assert.IsNotNull (ci.Invoke (null), "invoke");
  285. }
  286. }
  287. }