2008-09-14 Zoltan Varga <vargaz@gmail.com>
[mono.git] / mcs / class / System / Test / System.Text.RegularExpressions / RegexCas.cs
1 //
2 // RegexCas.cs - CAS unit tests for System.Text.RegularExpressions.Regex
3 //
4 // Author:
5 //      Sebastien Pouliot  <sebastien@ximian.com>
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
29 using NUnit.Framework;
30
31 using System;
32 using System.IO;
33 using System.Reflection;
34 using System.Runtime.Serialization.Formatters.Binary;
35 using System.Security;
36 using System.Security.Permissions;
37 using System.Text.RegularExpressions;
38
39 namespace MonoCasTests.System.Text.RegularExpressions {
40
41         class TestRegex: Regex {
42
43                 public TestRegex ()
44                 {
45                         // note: test protected default ctor
46                 }
47
48                 public void TestProtectedStuff ()
49                 {
50                         pattern = String.Empty;
51                         roptions = RegexOptions.None;
52
53                         try {
54                                 InitializeReferences ();
55                         }
56                         catch (NotImplementedException) {
57                                 // mono
58                         }
59
60                         Assert.IsFalse (UseOptionC (), "UseOptionC");
61                         Assert.IsFalse (UseOptionR (), "UseOptionR");
62
63                         Assert.IsNull (capnames, "capnames");
64                         Assert.IsNull (caps, "caps");
65                         Assert.AreEqual (0, capsize, "capsize");
66                         Assert.IsNull (capslist, "capslist");
67                         Assert.IsNull (factory, "factory");
68                 }
69         }
70
71         [TestFixture]
72         [Category ("CAS")]
73         public class RegexCas {
74
75                 private AssemblyName aname;
76
77                 [TestFixtureSetUp]
78                 public void FixtureSetUp ()
79                 {
80                         aname = new AssemblyName ();
81                         aname.Name = "Cas.Test.RegularExpressions";
82                 }
83
84                 [SetUp]
85                 public void SetUp ()
86                 {
87                         if (!SecurityManager.SecurityEnabled)
88                                 Assert.Ignore ("SecurityManager.SecurityEnabled is OFF");
89                 }
90
91                 private string Evaluator (Match match)
92                 {
93                         return String.Empty;
94                 }
95
96                 [Test]
97                 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
98                 public void Static_Deny_Unrestricted ()
99                 {
100                         Assert.AreEqual (String.Empty, Regex.Escape (String.Empty), "Escape");
101                         Assert.AreEqual (String.Empty, Regex.Unescape (String.Empty), "Unescape");
102                         
103                         Assert.IsTrue (Regex.IsMatch (String.Empty, String.Empty), "IsMatch");
104                         Assert.IsTrue (Regex.IsMatch (String.Empty, String.Empty, RegexOptions.Singleline), "IsMatch2");
105                         
106                         Assert.IsNotNull (Regex.Match (String.Empty, String.Empty), "Match");
107                         Assert.IsNotNull (Regex.Match (String.Empty, String.Empty, RegexOptions.Singleline), "Match2");
108
109                         Assert.AreEqual (1, Regex.Matches (String.Empty, String.Empty).Count, "Matches");
110                         Assert.AreEqual (1, Regex.Matches (String.Empty, String.Empty, RegexOptions.Singleline).Count, "Matches2");
111
112                         Assert.AreEqual (String.Empty, Regex.Replace (String.Empty, String.Empty, new MatchEvaluator (Evaluator)), "Replace");
113                         Assert.AreEqual (String.Empty, Regex.Replace (String.Empty, String.Empty, new MatchEvaluator (Evaluator), RegexOptions.Singleline), "Replace2");
114                         Assert.AreEqual (String.Empty, Regex.Replace (String.Empty, String.Empty, String.Empty), "Replace3");
115                         Assert.AreEqual (String.Empty, Regex.Replace (String.Empty, String.Empty, String.Empty, RegexOptions.Singleline), "Replace4");
116
117                         Assert.AreEqual (2, Regex.Split (String.Empty, String.Empty).Length, "Split");
118                         Assert.AreEqual (2, Regex.Split (String.Empty, String.Empty, RegexOptions.Singleline).Length, "Split2");
119 #if NET_2_0
120                         Assert.AreEqual (15, Regex.CacheSize, "CacheSize");
121                         Regex.CacheSize = 1;
122 #endif
123                 }
124
125                 [Test]
126                 [SecurityPermission (SecurityAction.Deny, ControlEvidence = true)]
127                 [ExpectedException (typeof (SecurityException))]
128                 [Category ("NotWorking")]
129                 public void CompileToAssembly_Deny_ControlEvidence ()
130                 {
131                         RegexCompilationInfo info = new RegexCompilationInfo (String.Empty, RegexOptions.None, "name", String.Empty, false);
132                         Regex.CompileToAssembly (new RegexCompilationInfo[1] { info }, aname, null, null);
133                 }
134
135                 [Test]
136                 [FileIOPermission (SecurityAction.Deny, Unrestricted = true)]
137                 [ExpectedException (typeof (SecurityException))]
138                 [Category ("NotWorking")]
139                 public void CompileToAssembly_Deny_FileIOPermission ()
140                 {
141                         RegexCompilationInfo info = new RegexCompilationInfo (String.Empty, RegexOptions.None, "name", String.Empty, false);
142                         Regex.CompileToAssembly (new RegexCompilationInfo[1] { info }, aname, null, null);
143                 }
144
145                 [Test]
146                 [SecurityPermission (SecurityAction.PermitOnly, ControlEvidence = true)]
147                 [FileIOPermission (SecurityAction.PermitOnly, Unrestricted = true)]
148                 [Category ("NotWorking")]
149                 public void CompileToAssembly_PermitOnly_ControlEvidence ()
150                 {
151                         RegexCompilationInfo info = new RegexCompilationInfo (String.Empty, RegexOptions.None, "name", String.Empty, false);
152                         Regex.CompileToAssembly (new RegexCompilationInfo[1] { info }, aname, null, null);
153                 }
154
155                 [Test]
156                 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
157                 public void Instance_Deny_Unrestricted ()
158                 {
159                         Regex r = new Regex (String.Empty);
160                         Assert.AreEqual (RegexOptions.None, r.Options, "Options");
161                         Assert.IsFalse (r.RightToLeft, "RightToLeft");
162
163                         Assert.AreEqual (1, r.GetGroupNames ().Length, "GetGroupNames");
164                         Assert.AreEqual (1, r.GetGroupNumbers ().Length, "GetGroupNumbers");
165                         Assert.AreEqual ("0", r.GroupNameFromNumber (0), "GroupNameFromNumber");
166                         Assert.AreEqual (0, r.GroupNumberFromName ("0"), "GroupNumberFromName");
167
168                         Assert.IsTrue (r.IsMatch (String.Empty), "IsMatch");
169                         Assert.IsTrue (r.IsMatch (String.Empty, 0), "IsMatch2");
170
171                         Assert.IsNotNull (r.Match (String.Empty), "Match");
172                         Assert.IsNotNull (r.Match (String.Empty, 0), "Match2");
173                         Assert.IsNotNull (r.Match (String.Empty, 0, 0), "Match3");
174
175                         Assert.AreEqual (1, r.Matches (String.Empty).Count, "Matches");
176                         Assert.AreEqual (1, r.Matches (String.Empty, 0).Count, "Matches2");
177
178                         Assert.AreEqual (String.Empty, r.Replace (String.Empty, new MatchEvaluator (Evaluator)), "Replace");
179                         Assert.AreEqual (String.Empty, r.Replace (String.Empty, new MatchEvaluator (Evaluator), 0), "Replace2");
180                         Assert.AreEqual (String.Empty, r.Replace (String.Empty, new MatchEvaluator (Evaluator), 0, 0), "Replace3");
181                         Assert.AreEqual (String.Empty, r.Replace (String.Empty, String.Empty), "Replace4");
182                         Assert.AreEqual (String.Empty, r.Replace (String.Empty, String.Empty, 0), "Replace5");
183                         Assert.AreEqual (String.Empty, r.Replace (String.Empty, String.Empty, 0, 0), "Replace6");
184
185                         Assert.AreEqual (2, r.Split (String.Empty).Length, "Split");
186                         Assert.AreEqual (2, r.Split (String.Empty, 0).Length, "Split2");
187                         Assert.AreEqual (2, r.Split (String.Empty, 0, 0).Length, "Split3");
188
189                         Assert.AreEqual (String.Empty, r.ToString (), "ToString");
190                 }
191
192                 [Test]
193                 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
194                 public void Protected_Deny_Unrestricted ()
195                 {
196                         TestRegex r = new TestRegex ();
197                         r.TestProtectedStuff ();
198                 }
199
200                 [Test]
201                 [SecurityPermission (SecurityAction.Deny, SerializationFormatter = true)]
202                 [ExpectedException (typeof (SecurityException))]
203                 public void Serialization_Deny_SerializationFormatter ()
204                 {
205                         BinaryFormatter bf = new BinaryFormatter ();
206                         MemoryStream ms = new MemoryStream ();
207
208                         Regex r1 = new Regex (String.Empty, RegexOptions.Singleline);
209                         bf.Serialize (ms, r1);
210                 }
211
212                 [Test]
213                 [SecurityPermission (SecurityAction.PermitOnly, SerializationFormatter = true)]
214                 public void Serialization_PermitOnly_SerializationFormatter ()
215                 {
216                         BinaryFormatter bf = new BinaryFormatter ();
217                         MemoryStream ms = new MemoryStream ();
218
219                         Regex r1 = new Regex (String.Empty, RegexOptions.Singleline);
220                         bf.Serialize (ms, r1);
221
222                         ms.Position = 0;
223                         Regex r2 = (Regex) bf.Deserialize (ms);
224
225                         Assert.AreEqual (r1.Options, r2.Options, "Options");
226                         Assert.AreEqual (r1.ToString (), r2.ToString (), "ToString");
227                 }
228
229                 [Test]
230                 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
231                 public void LinkDemand_Deny_Unrestricted ()
232                 {
233                         Type[] types = new Type[1] { typeof (string) };
234                         ConstructorInfo ci = typeof (Regex).GetConstructor (types);
235                         Assert.IsNotNull (ci, ".ctor (string)");
236                         Assert.IsNotNull (ci.Invoke (new object[1] { String.Empty }), "invoke");
237                 }
238         }
239 }