CodeDomProviderCas.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. //
  2. // CodeDomProviderCas.cs
  3. // - CAS unit tests for System.CodeDom.Compiler.CodeDomProvider
  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;
  32. using System.CodeDom.Compiler;
  33. using System.Configuration;
  34. using System.IO;
  35. using System.Reflection;
  36. using System.Security;
  37. using System.Security.Permissions;
  38. namespace MonoCasTests.System.CodeDom.Compiler {
  39. class CodeDomProviderTest: CodeDomProvider {
  40. public CodeDomProviderTest ()
  41. {
  42. }
  43. public override ICodeCompiler CreateCompiler ()
  44. {
  45. return null;
  46. }
  47. public override ICodeGenerator CreateGenerator ()
  48. {
  49. return null;
  50. }
  51. }
  52. [TestFixture]
  53. [Category ("CAS")]
  54. public class CodeDomProviderCas {
  55. private StringWriter writer;
  56. private CodeDomProviderTest cdp;
  57. [TestFixtureSetUp]
  58. public void FixtureSetUp ()
  59. {
  60. // at full trust
  61. writer = new StringWriter ();
  62. cdp = new CodeDomProviderTest ();
  63. }
  64. [SetUp]
  65. public void SetUp ()
  66. {
  67. if (!SecurityManager.SecurityEnabled)
  68. Assert.Ignore ("SecurityManager.SecurityEnabled is OFF");
  69. }
  70. [Test]
  71. [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
  72. public void Defaults ()
  73. {
  74. cdp = new CodeDomProviderTest (); // execute ctor not a full trust
  75. Assert.AreEqual (String.Empty, cdp.FileExtension, "FileExtension");
  76. Assert.AreEqual (LanguageOptions.None, cdp.LanguageOptions, "LanguageOptions");
  77. Assert.IsNull (cdp.CreateCompiler (), "CreateCompiler");
  78. Assert.IsNull (cdp.CreateGenerator (), "CreateGenerator");
  79. Assert.IsNull (cdp.CreateGenerator (String.Empty), "CreateGenerator(string)");
  80. Assert.IsNull (cdp.CreateGenerator (writer), "CreateGenerator(TextWriter)");
  81. Assert.IsNull (cdp.CreateParser (), "CreateParser()");
  82. Assert.IsNotNull (cdp.GetConverter (typeof (string)), "GetConverter");
  83. #if NET_2_0
  84. Assert.IsNotNull (CodeDomProvider.GetAllCompilerInfo (), "GetAllCompilerInfo");
  85. // mono returns null (missing config?)
  86. CodeDomProvider.GetCompilerInfo ("cs");
  87. CodeDomProvider.GetLanguageFromExtension ("cs");
  88. Assert.IsFalse (CodeDomProvider.IsDefinedExtension (String.Empty), "String.Empty");
  89. Assert.IsFalse (CodeDomProvider.IsDefinedLanguage (String.Empty), "String.Empty");
  90. #endif
  91. }
  92. #if NET_2_0
  93. [Test]
  94. [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
  95. [ExpectedException (typeof (NotImplementedException))]
  96. public void CompileAssemblyFromDom_Deny_Unrestricted ()
  97. {
  98. cdp.CompileAssemblyFromDom (null, null);
  99. }
  100. [Test]
  101. [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
  102. [ExpectedException (typeof (NotImplementedException))]
  103. public void CompileAssemblyFromFile_Deny_Unrestricted ()
  104. {
  105. cdp.CompileAssemblyFromFile (null, null);
  106. }
  107. [Test]
  108. [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
  109. [ExpectedException (typeof (NotImplementedException))]
  110. public void CompileAssemblyFromSource_Deny_Unrestricted ()
  111. {
  112. cdp.CompileAssemblyFromSource (null, null);
  113. }
  114. [Test]
  115. [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
  116. [ExpectedException (typeof (NotImplementedException))]
  117. public void CreateEscapedIdentifier_Deny_Unrestricted ()
  118. {
  119. cdp.CreateEscapedIdentifier (null);
  120. }
  121. [Test]
  122. [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
  123. [ExpectedException (typeof (NotImplementedException))]
  124. public void CreateValidIdentifier_Deny_Unrestricted ()
  125. {
  126. cdp.CreateValidIdentifier (null);
  127. }
  128. [Test]
  129. [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
  130. [ExpectedException (typeof (NotImplementedException))]
  131. public void GenerateCodeFromCompileUnit_Deny_Unrestricted ()
  132. {
  133. cdp.GenerateCodeFromCompileUnit (null, writer, null);
  134. }
  135. [Test]
  136. [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
  137. [ExpectedException (typeof (NotImplementedException))]
  138. public void GenerateCodeFromExpression_Deny_Unrestricted ()
  139. {
  140. cdp.GenerateCodeFromExpression (null, writer, null);
  141. }
  142. [Test]
  143. [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
  144. [ExpectedException (typeof (NotImplementedException))]
  145. public void GenerateCodeFromMember_Deny_Unrestricted ()
  146. {
  147. cdp.GenerateCodeFromMember (null, writer, null);
  148. }
  149. [Test]
  150. [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
  151. [ExpectedException (typeof (NotImplementedException))]
  152. public void GenerateCodeFromNamespace_Deny_Unrestricted ()
  153. {
  154. cdp.GenerateCodeFromNamespace (null, writer, null);
  155. }
  156. [Test]
  157. [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
  158. [ExpectedException (typeof (NotImplementedException))]
  159. public void GenerateCodeFromStatement_Deny_Unrestricted ()
  160. {
  161. cdp.GenerateCodeFromStatement (null, writer, null);
  162. }
  163. [Test]
  164. [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
  165. [ExpectedException (typeof (NotImplementedException))]
  166. public void GenerateCodeFromType_Deny_Unrestricted ()
  167. {
  168. cdp.GenerateCodeFromType (null, writer, null);
  169. }
  170. [Test]
  171. [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
  172. [ExpectedException (typeof (NotImplementedException))]
  173. public void GetTypeOutput_Deny_Unrestricted ()
  174. {
  175. cdp.GetTypeOutput (new CodeTypeReference ());
  176. }
  177. [Test]
  178. [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
  179. [ExpectedException (typeof (NotImplementedException))]
  180. public void IsValidIdentifier_Deny_Unrestricted ()
  181. {
  182. cdp.IsValidIdentifier (null);
  183. }
  184. [Test]
  185. [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
  186. [ExpectedException (typeof (NotImplementedException))]
  187. public void Parse_Deny_Unrestricted ()
  188. {
  189. cdp.Parse (null);
  190. }
  191. [Test]
  192. [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
  193. [ExpectedException (typeof (NotImplementedException))]
  194. public void Supports_Deny_Unrestricted ()
  195. {
  196. cdp.Supports (GeneratorSupport.ArraysOfArrays);
  197. }
  198. // static methods
  199. [Test]
  200. public void CreateProvider_Allow_Everything ()
  201. {
  202. CodeDomProvider.CreateProvider ("cs");
  203. // returns null on mono (missing config?)
  204. }
  205. [Test]
  206. [EnvironmentPermission (SecurityAction.Deny, Read = "MONO")]
  207. [ExpectedException (typeof (SecurityException))]
  208. [Category ("NotWorking")] // mono returns null not an instance
  209. public void CreateProvider_Deny_Anything ()
  210. {
  211. CodeDomProvider cdp = CodeDomProvider.CreateProvider ("cs");
  212. // requires full trust (i.e. unrestricted permission set)
  213. }
  214. [Test]
  215. public void LinkDemand_StaticMethods_Allow_Everything ()
  216. {
  217. object[] language = new object[1] { "cs" };
  218. object[] empty = new object[1] { String.Empty };
  219. MethodInfo mi = typeof (CodeDomProvider).GetMethod ("CreateProvider");
  220. mi.Invoke (null, language); // returns null on mono (missing config?)
  221. mi = typeof (CodeDomProvider).GetMethod ("GetAllCompilerInfo");
  222. Assert.IsNotNull (mi.Invoke (null, null), "GetAllCompilerInfo()");
  223. mi = typeof (CodeDomProvider).GetMethod ("GetCompilerInfo");
  224. mi.Invoke (null, language); // returns null on mono (missing config?)
  225. mi = typeof (CodeDomProvider).GetMethod ("GetLanguageFromExtension");
  226. mi.Invoke (null, language); // returns null on mono (missing config?)
  227. mi = typeof (CodeDomProvider).GetMethod ("IsDefinedExtension");
  228. Assert.IsFalse ((bool)mi.Invoke (null, empty), "IsDefinedExtension('')");
  229. mi = typeof (CodeDomProvider).GetMethod ("IsDefinedLanguage");
  230. Assert.IsFalse ((bool)mi.Invoke (null, empty), "IsDefinedLanguage('')");
  231. }
  232. [Test]
  233. [EnvironmentPermission (SecurityAction.Deny, Read = "MONO")]
  234. [ExpectedException (typeof (SecurityException))]
  235. public void LinkDemand_CreateProvider_Deny_Anything ()
  236. {
  237. MethodInfo mi = typeof (CodeDomProvider).GetMethod ("CreateProvider");
  238. Assert.IsNotNull (mi, "CreateProvider");
  239. Assert.IsNotNull (mi.Invoke (null, new object[1] { "cs" }), "CreateProvider(cs)");
  240. }
  241. [Test]
  242. [EnvironmentPermission (SecurityAction.Deny, Read = "MONO")]
  243. [ExpectedException (typeof (SecurityException))]
  244. public void LinkDemand_GetAllCompilerInfo_Deny_Anything ()
  245. {
  246. MethodInfo mi = typeof (CodeDomProvider).GetMethod ("GetAllCompilerInfo");
  247. Assert.IsNotNull (mi, "GetAllCompilerInfo");
  248. Assert.IsNotNull (mi.Invoke (null, null), "GetAllCompilerInfo()");
  249. // requires full trust (i.e. unrestricted permission set)
  250. }
  251. [Test]
  252. [EnvironmentPermission (SecurityAction.Deny, Read = "MONO")]
  253. [ExpectedException (typeof (SecurityException))]
  254. public void LinkDemand_GetCompilerInfo_Deny_Anything ()
  255. {
  256. MethodInfo mi = typeof (CodeDomProvider).GetMethod ("GetCompilerInfo");
  257. Assert.IsNotNull (mi, "GetCompilerInfo");
  258. Assert.IsNotNull (mi.Invoke (null, new object[1] { "cs" }), "GetCompilerInfo(cs)");
  259. // requires full trust (i.e. unrestricted permission set)
  260. }
  261. [Test]
  262. [EnvironmentPermission (SecurityAction.Deny, Read = "MONO")]
  263. [ExpectedException (typeof (SecurityException))]
  264. public void LinkDemand_GetLanguageFromExtension_Deny_Anything ()
  265. {
  266. MethodInfo mi = typeof (CodeDomProvider).GetMethod ("GetLanguageFromExtension");
  267. Assert.IsNotNull (mi, "GetLanguageFromExtension");
  268. Assert.IsNotNull (mi.Invoke (null, new object[1] { null }), "invoke (null)");
  269. // requires full trust (i.e. unrestricted permission set)
  270. }
  271. [Test]
  272. [EnvironmentPermission (SecurityAction.Deny, Read = "MONO")]
  273. [ExpectedException (typeof (SecurityException))]
  274. public void LinkDemand_IsDefinedExtension_Deny_Anything ()
  275. {
  276. MethodInfo mi = mi = typeof (CodeDomProvider).GetMethod ("IsDefinedExtension");
  277. Assert.IsNotNull (mi, "IsDefinedExtension");
  278. Assert.IsFalse ((bool) mi.Invoke (null, new object[1] { String.Empty }), "IsDefinedExtension('')");
  279. // requires full trust (i.e. unrestricted permission set)
  280. }
  281. [Test]
  282. [EnvironmentPermission (SecurityAction.Deny, Read = "MONO")]
  283. [ExpectedException (typeof (SecurityException))]
  284. public void LinkDemand_IsDefinedLanguage_Deny_Anything ()
  285. {
  286. MethodInfo mi = mi = typeof (CodeDomProvider).GetMethod ("IsDefinedLanguage");
  287. Assert.IsNotNull (mi, "IsDefinedLanguage");
  288. Assert.IsFalse ((bool) mi.Invoke (null, new object[1] { String.Empty }), "IsDefinedLanguage('')");
  289. // requires full trust (i.e. unrestricted permission set)
  290. }
  291. #endif
  292. [Test]
  293. [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
  294. public void LinkDemand_Deny_Unrestricted ()
  295. {
  296. ConstructorInfo ci = typeof (CodeDomProviderTest).GetConstructor (new Type[0]);
  297. Assert.IsNotNull (ci, "default .ctor()");
  298. Assert.IsNotNull (ci.Invoke (null), "invoke");
  299. }
  300. }
  301. }