Merge pull request #2396 from akoeplinger/flaky-osx-socket-test
[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 #if !MOBILE
30
31 using NUnit.Framework;
32
33 using System;
34 using System.IO;
35 using System.Reflection;
36 using System.Runtime.Serialization.Formatters.Binary;
37 using System.Security;
38 using System.Security.Permissions;
39 using System.Text.RegularExpressions;
40
41 namespace MonoCasTests.System.Text.RegularExpressions {
42
43         class TestRegex: Regex {
44
45                 public TestRegex ()
46                 {
47                         // note: test protected default ctor
48                 }
49
50                 public void TestProtectedStuff ()
51                 {
52                         pattern = String.Empty;
53                         roptions = RegexOptions.None;
54
55                         try {
56                                 InitializeReferences ();
57                         }
58                         catch (NotImplementedException) {
59                                 // mono
60                         }
61
62                         Assert.IsFalse (UseOptionC (), "UseOptionC");
63                         Assert.IsFalse (UseOptionR (), "UseOptionR");
64
65                         Assert.IsNull (capnames, "capnames");
66                         Assert.IsNull (caps, "caps");
67                         Assert.AreEqual (0, capsize, "capsize");
68                         Assert.IsNull (capslist, "capslist");
69                         Assert.IsNull (factory, "factory");
70                 }
71         }
72
73         [TestFixture]
74         [Category ("CAS")]
75         public class RegexCas {
76
77                 private AssemblyName aname;
78
79                 [TestFixtureSetUp]
80                 public void FixtureSetUp ()
81                 {
82                         aname = new AssemblyName ();
83                         aname.Name = "Cas.Test.RegularExpressions";
84                 }
85
86                 [SetUp]
87                 public void SetUp ()
88                 {
89                         if (!SecurityManager.SecurityEnabled)
90                                 Assert.Ignore ("SecurityManager.SecurityEnabled is OFF");
91                 }
92
93                 private string Evaluator (Match match)
94                 {
95                         return String.Empty;
96                 }
97
98                 [Test]
99                 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
100                 public void Static_Deny_Unrestricted ()
101                 {
102                         Assert.AreEqual (String.Empty, Regex.Escape (String.Empty), "Escape");
103                         Assert.AreEqual (String.Empty, Regex.Unescape (String.Empty), "Unescape");
104                         
105                         Assert.IsTrue (Regex.IsMatch (String.Empty, String.Empty), "IsMatch");
106                         Assert.IsTrue (Regex.IsMatch (String.Empty, String.Empty, RegexOptions.Singleline), "IsMatch2");
107                         
108                         Assert.IsNotNull (Regex.Match (String.Empty, String.Empty), "Match");
109                         Assert.IsNotNull (Regex.Match (String.Empty, String.Empty, RegexOptions.Singleline), "Match2");
110
111                         Assert.AreEqual (1, Regex.Matches (String.Empty, String.Empty).Count, "Matches");
112                         Assert.AreEqual (1, Regex.Matches (String.Empty, String.Empty, RegexOptions.Singleline).Count, "Matches2");
113
114                         Assert.AreEqual (String.Empty, Regex.Replace (String.Empty, String.Empty, new MatchEvaluator (Evaluator)), "Replace");
115                         Assert.AreEqual (String.Empty, Regex.Replace (String.Empty, String.Empty, new MatchEvaluator (Evaluator), RegexOptions.Singleline), "Replace2");
116                         Assert.AreEqual (String.Empty, Regex.Replace (String.Empty, String.Empty, String.Empty), "Replace3");
117                         Assert.AreEqual (String.Empty, Regex.Replace (String.Empty, String.Empty, String.Empty, RegexOptions.Singleline), "Replace4");
118
119                         Assert.AreEqual (2, Regex.Split (String.Empty, String.Empty).Length, "Split");
120                         Assert.AreEqual (2, Regex.Split (String.Empty, String.Empty, RegexOptions.Singleline).Length, "Split2");
121                         Assert.AreEqual (15, Regex.CacheSize, "CacheSize");
122                         Regex.CacheSize = 1;
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 }
240
241 #endif