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