**** Merged from MCS ****
[mono-project.git] / mcs / class / corlib / Test / System.Security / SecurityElementTest.cs
blob8ef2b6b1093b5b52064478f1e3b8aee3db7667de
1 //
2 // SecurityElementTest.cs - NUnit Test Cases for System.Security.SecurityElement
3 //
4 // Authors:
5 // Lawrence Pit (loz@cable.a2000.nl)
6 // Sebastien Pouliot (spouliot@motus.com)
7 //
8 // Portions (C) 2004 Motus Technologies Inc. (http://www.motus.com)
9 //
11 using NUnit.Framework;
12 using System;
13 using System.Collections;
14 using System.Security;
16 namespace MonoTests.System.Security {
18 [TestFixture]
19 public class SecurityElementTest : Assertion {
21 SecurityElement elem;
23 [SetUp]
24 public void SetUp ()
26 elem = CreateElement ();
29 private SecurityElement CreateElement ()
31 SecurityElement elem = new SecurityElement ("IPermission");
32 elem.AddAttribute ("class", "System");
33 elem.AddAttribute ("version", "1");
35 SecurityElement child = new SecurityElement ("ConnectAccess");
36 elem.AddChild (child);
38 SecurityElement grandchild = new SecurityElement ("ENDPOINT", "some text");
39 grandchild.AddAttribute ("transport", "All");
40 grandchild.AddAttribute ("host", "localhost");
41 grandchild.AddAttribute ("port", "8080");
42 child.AddChild (grandchild);
44 SecurityElement grandchild2 = new SecurityElement ("ENDPOINT");
45 grandchild2.AddAttribute ("transport", "Tcp");
46 grandchild2.AddAttribute ("host", "www.ximian.com");
47 grandchild2.AddAttribute ("port", "All");
48 child.AddChild (grandchild2);
50 return elem;
53 [Test]
54 public void ConstructorsTagTest ()
56 SecurityElement se = new SecurityElement ("tag", "text");
57 AssertNull ("EmptyAttributes", se.Attributes);
58 AssertNull ("EmptyChildren", se.Children);
59 AssertEquals ("Tag", "tag", se.Tag);
60 AssertEquals ("Text", "text", se.Text);
63 [Test]
64 [ExpectedException (typeof (ArgumentNullException))]
65 public void ConstructorsTagNullText ()
67 SecurityElement se = new SecurityElement (null, "text");
70 [Test]
71 public void ConstructorsTagTextNull ()
73 SecurityElement se = new SecurityElement ("tag", null);
74 AssertNull ("EmptyAttributes", se.Attributes);
75 AssertNull ("EmptyChildren", se.Children);
76 AssertEquals ("Tag", "tag", se.Tag);
77 AssertNull ("Text", se.Text);
80 [Test]
81 public void ConstructorsTag ()
83 SecurityElement se = new SecurityElement ("tag");
84 AssertNull ("EmptyAttributes", se.Attributes);
85 AssertNull ("EmptyChildren", se.Children);
86 AssertEquals ("Tag", "tag", se.Tag);
87 AssertNull ("Text", se.Text);
90 [Test]
91 [ExpectedException (typeof (ArgumentNullException))]
92 public void ConstructorsTagNull ()
94 SecurityElement se = new SecurityElement (null);
97 [Test]
98 [ExpectedException (typeof (ArgumentNullException))]
99 public void AddAttribute_NameNullValue ()
101 elem.AddAttribute (null, "valid");
104 [Test]
105 [ExpectedException (typeof (ArgumentNullException))]
106 public void AddAttribute_NameValueNull ()
108 elem.AddAttribute ("valid", null);
111 [Test]
112 [ExpectedException (typeof (ArgumentException))]
113 public void AddAttribute_InvalidName ()
115 elem.AddAttribute ("<invalid>", "valid");
118 [Test]
119 [ExpectedException (typeof (ArgumentException))]
120 public void AddAttribute_InvalidValue ()
122 elem.AddAttribute ("valid", "invalid\"");
125 [Test]
126 public void AddAttribute_InvalidValue2 ()
128 elem.AddAttribute ("valid", "valid&");
129 // in xml world this is actually not considered valid
130 // but it is by MS.Net
133 [Test]
134 [ExpectedException (typeof (ArgumentException))]
135 public void AddAttribute_InvalidValue3 ()
137 elem.AddAttribute ("valid", "<invalid>");
140 [Test]
141 [ExpectedException (typeof (ArgumentException))]
142 public void AddAttribute_Duplicate ()
144 elem.AddAttribute ("valid", "first time");
145 elem.AddAttribute ("valid", "second time");
148 [Test]
149 public void AddAttribute ()
151 elem.AddAttribute ("valid", "valid\'");
154 [Test]
155 [ExpectedException (typeof (ArgumentNullException))]
156 public void AddChild_Null ()
158 elem.AddChild (null);
161 [Test]
162 public void AddChild ()
164 int n = elem.Children.Count;
165 // add itself
166 elem.AddChild (elem);
167 AssertEquals ("Count", (n+1), elem.Children.Count);
170 [Test]
171 [ExpectedException (typeof (ArgumentException))]
172 [Ignore ("this will result in an InvalidCastException on MS.Net - I have no clue why")]
173 public void Attributes_StrangeCase ()
175 Hashtable h = elem.Attributes;
176 h.Add ("<invalid>", "valid");
177 elem.Attributes = h;
180 [Test]
181 [ExpectedException (typeof (ArgumentException))]
182 public void Attributes_ArgumentException ()
184 Hashtable h = elem.Attributes;
185 h.Add ("valid", "\"invalid\"");
186 elem.Attributes = h;
189 [Test]
190 public void Attributes ()
192 Hashtable h = elem.Attributes;
194 h = elem.Attributes;
195 h.Add ("foo", "bar");
196 Assert ("#1", elem.Attributes.Count != h.Count);
198 elem.Attributes = h;
199 AssertNotNull ("#2", elem.Attribute ("foo"));
202 [Test]
203 public void Equal ()
205 int iTest = 0;
206 SecurityElement elem2 = CreateElement ();
207 iTest++;
208 Assert ("#1", elem.Equal (elem2));
209 iTest++;
210 SecurityElement child = (SecurityElement) elem2.Children [0];
211 iTest++;
212 child = (SecurityElement) child.Children [1];
213 iTest++;
214 child.Text = "some text";
215 iTest++;
216 Assert ("#2", !elem.Equal (elem2));
219 [Test]
220 public void Escape ()
222 AssertEquals ("#1", "foo&lt;&gt;&quot;&apos;&amp; bar", SecurityElement.Escape ("foo<>\"'& bar"));
225 [Test]
226 public void IsValidAttributeName ()
228 Assert ("#1", !SecurityElement.IsValidAttributeName ("x x"));
229 Assert ("#2", !SecurityElement.IsValidAttributeName ("x<x"));
230 Assert ("#3", !SecurityElement.IsValidAttributeName ("x>x"));
231 Assert ("#4", SecurityElement.IsValidAttributeName ("x\"x"));
232 Assert ("#5", SecurityElement.IsValidAttributeName ("x'x"));
233 Assert ("#6", SecurityElement.IsValidAttributeName ("x&x"));
236 [Test]
237 public void IsValidAttributeValue ()
239 Assert ("#1", SecurityElement.IsValidAttributeValue ("x x"));
240 Assert ("#2", !SecurityElement.IsValidAttributeValue ("x<x"));
241 Assert ("#3", !SecurityElement.IsValidAttributeValue ("x>x"));
242 Assert ("#4", !SecurityElement.IsValidAttributeValue ("x\"x"));
243 Assert ("#5", SecurityElement.IsValidAttributeValue ("x'x"));
244 Assert ("#6", SecurityElement.IsValidAttributeValue ("x&x"));
247 [Test]
248 public void IsValidTag ()
250 Assert ("#1", !SecurityElement.IsValidTag ("x x"));
251 Assert ("#2", !SecurityElement.IsValidTag ("x<x"));
252 Assert ("#3", !SecurityElement.IsValidTag ("x>x"));
253 Assert ("#4", SecurityElement.IsValidTag ("x\"x"));
254 Assert ("#5", SecurityElement.IsValidTag ("x'x"));
255 Assert ("#6", SecurityElement.IsValidTag ("x&x"));
258 [Test]
259 public void IsValidText ()
261 Assert ("#1", SecurityElement.IsValidText ("x x"));
262 Assert ("#2", !SecurityElement.IsValidText ("x<x"));
263 Assert ("#3", !SecurityElement.IsValidText ("x>x"));
264 Assert ("#4", SecurityElement.IsValidText ("x\"x"));
265 Assert ("#5", SecurityElement.IsValidText ("x'x"));
266 Assert ("#6", SecurityElement.IsValidText ("x&x"));
269 [Test]
270 [ExpectedException (typeof (ArgumentNullException))]
271 public void SearchForChildByTag_Null ()
273 SecurityElement child = elem.SearchForChildByTag (null);
276 [Test]
277 public void SearchForChildByTag ()
279 SecurityElement child = elem.SearchForChildByTag ("doesnotexist");
280 AssertNull ("#1", child);
282 child = elem.SearchForChildByTag ("ENDPOINT");
283 AssertNull ("#2", child);
285 child = (SecurityElement) elem.Children [0];
286 child = child.SearchForChildByTag ("ENDPOINT");
287 AssertEquals ("#3", "All", child.Attribute ("transport"));
290 [Test]
291 [ExpectedException (typeof (ArgumentNullException))]
292 public void SearchForTextOfTag_Null ()
294 string s = elem.SearchForTextOfTag (null);
297 [Test]
298 public void SearchForTextOfTag ()
300 string s = elem.SearchForTextOfTag ("ENDPOINT");
301 AssertEquals ("SearchForTextOfTag", "some text", s);
304 [Test]
305 [ExpectedException (typeof (ArgumentNullException))]
306 public void Tag_Null ()
308 elem.Tag = null;
309 AssertNull ("Tag", elem.Tag);
312 [Test]
313 public void Text_Null ()
315 elem.Text = null;
316 AssertNull ("Text", elem.Text);
319 [Test]
320 public void MultipleAttributes ()
322 SecurityElement se = new SecurityElement ("Multiple");
323 se.AddAttribute ("Attribute1", "One");
324 se.AddAttribute ("Attribute2", "Two");
325 string expected = String.Format ("<Multiple Attribute1=\"One\"{0} Attribute2=\"Two\"/>{0}", Environment.NewLine);
326 AssertEquals ("ToString()", expected, se.ToString ());