IndentedTextWriterCas.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. //
  2. // IndentedTextWriterCas.cs
  3. // - CAS unit tests for System.CodeDom.Compiler.IndentedTextWriter
  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.IO;
  33. using System.Reflection;
  34. using System.Security;
  35. using System.Security.Permissions;
  36. namespace MonoCasTests.System.CodeDom.Compiler {
  37. [TestFixture]
  38. [Category ("CAS")]
  39. public class IndentedTextWriterCas {
  40. private TextWriter writer;
  41. [SetUp]
  42. public void SetUp ()
  43. {
  44. if (!SecurityManager.SecurityEnabled)
  45. Assert.Ignore ("SecurityManager.SecurityEnabled is OFF");
  46. writer = new StringWriter ();
  47. }
  48. [Test]
  49. [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
  50. public void Defaults_Deny_Unrestricted ()
  51. {
  52. // note: CAS doesn't apply to fields
  53. Assert.AreEqual (" ", IndentedTextWriter.DefaultTabString, "DefaultTabString");
  54. }
  55. private void TouchEverything (IndentedTextWriter itw)
  56. {
  57. Assert.AreSame (writer.Encoding, itw.Encoding, "Encoding");
  58. Assert.AreEqual (0, itw.Indent, "Indent");
  59. itw.Indent = 1;
  60. Assert.AreSame (writer, itw.InnerWriter, "InnerWriter");
  61. Assert.AreEqual (writer.NewLine, itw.NewLine, "NewLine");
  62. itw.Write (true);
  63. itw.Write (Char.MinValue);
  64. itw.Write (Path.InvalidPathChars); // char[]
  65. itw.Write (Double.MinValue);
  66. itw.Write (Int32.MinValue);
  67. itw.Write (Int64.MaxValue);
  68. itw.Write (new object ());
  69. itw.Write (Single.MinValue);
  70. itw.Write (String.Empty);
  71. itw.Write ("{0}", String.Empty);
  72. itw.Write ("{0}{1}", Int32.MinValue, Int32.MaxValue);
  73. itw.Write ("{0}{1}{2}", Int32.MinValue, 0, Int32.MaxValue);
  74. itw.Write (Path.InvalidPathChars, 0, Path.InvalidPathChars.Length);
  75. itw.WriteLine ();
  76. itw.WriteLine (true);
  77. itw.WriteLine (Char.MinValue);
  78. itw.WriteLine (Path.InvalidPathChars); // char[]
  79. itw.WriteLine (Double.MinValue);
  80. itw.WriteLine (Int32.MinValue);
  81. itw.WriteLine (Int64.MaxValue);
  82. itw.WriteLine (new object ());
  83. itw.WriteLine (Single.MinValue);
  84. itw.WriteLine (String.Empty);
  85. itw.WriteLine (UInt32.MaxValue);
  86. itw.WriteLine ("{0}", String.Empty);
  87. itw.WriteLine ("{0}{1}", Int32.MinValue, Int32.MaxValue);
  88. itw.WriteLine ("{0}{1}{2}", Int32.MinValue, 0, Int32.MaxValue);
  89. itw.WriteLine (Path.InvalidPathChars, 0, Path.InvalidPathChars.Length);
  90. itw.WriteLineNoTabs (String.Empty);
  91. itw.Flush ();
  92. itw.Close ();
  93. }
  94. [Test]
  95. [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
  96. public void Constructor1_Deny_Unrestricted ()
  97. {
  98. IndentedTextWriter itw = new IndentedTextWriter (writer);
  99. TouchEverything (itw);
  100. }
  101. [Test]
  102. [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
  103. public void Constructor2_Deny_Unrestricted ()
  104. {
  105. IndentedTextWriter itw = new IndentedTextWriter (writer, "\t");
  106. TouchEverything (itw);
  107. }
  108. [Test]
  109. public void LinkDemand_No_Restriction ()
  110. {
  111. Type[] types = new Type[1] { typeof (TextWriter) };
  112. ConstructorInfo ci = typeof (IndentedTextWriter).GetConstructor (types);
  113. Assert.IsNotNull (ci, ".ctor(TextWriter)");
  114. Assert.IsNotNull (ci.Invoke (new object[1] { writer }), "invoke");
  115. }
  116. [Test]
  117. [EnvironmentPermission (SecurityAction.Deny, Read = "Mono")]
  118. [ExpectedException (typeof (SecurityException))]
  119. public void LinkDemand_Deny_Anything ()
  120. {
  121. // denying anything results in a non unrestricted permission set
  122. Type[] types = new Type[1] { typeof (TextWriter) };
  123. ConstructorInfo ci = typeof (IndentedTextWriter).GetConstructor (types);
  124. Assert.IsNotNull (ci, ".ctor(TextWriter)");
  125. Assert.IsNotNull (ci.Invoke (new object[1] { writer }), "invoke");
  126. }
  127. }
  128. }